'.esc_html__('Settings','crop-thumbnails').'';
array_unshift( $links, $settings_link );
}
return $links;
}
public function addOptionsPage() {
add_options_page(esc_html__('Crop Post Thumbnail Page','crop-thumbnails'), 'Crop-Thumbnails', 'manage_options', 'page-cpt', array($this,'optionsPage'));
add_action('admin_init', array($this,'settingsInitialisation'));
}
public function optionsPage() { ?>
getOptionsKey(), array($this,'validateSettings') );
$_sectionID = 'choose_sizes_section';
add_settings_section($_sectionID, esc_html__('Sizes and Post Types','crop-thumbnails'), array($this,'sectionDescriptionChooseSizes'), 'page1');
$_sectionID = 'userPermission';
add_settings_section($_sectionID, esc_html__('User Permission','crop-thumbnails'), array($this,'emptySectionDescription'), 'page1');
$_tmpID = 'user_permission_only_on_edit_files';
add_settings_field($_tmpID, esc_html__('When active, only users who are able to edit files can crop thumbnails. Otherwise (default), any user who can upload files can also crop thumbnails.','crop-thumbnails'), array($this,'callback_'.$_tmpID), 'page1', $_sectionID, array( 'label_for' => self::$cssPrefix.$_tmpID ));
$_sectionID = 'quick_test';
add_settings_section($_sectionID, esc_html__('Plugin Test','crop-thumbnails'), array($this,'sectionDescriptionTest'), 'page1');
$_sectionID = 'developer';
add_settings_section($_sectionID, esc_html__('Developer Settings','crop-thumbnails'), array($this,'emptySectionDescription'), 'page1');
$_tmpID = 'debug_js';
add_settings_field($_tmpID, esc_html__('Enable JS-Debug.','crop-thumbnails'), array($this,'callback_'.$_tmpID), 'page1', $_sectionID, array( 'label_for' => self::$cssPrefix.$_tmpID ));
$_tmpID = 'debug_data';
add_settings_field($_tmpID, esc_html__('Enable Data-Debug.','crop-thumbnails'), array($this,'callback_'.$_tmpID), 'page1', $_sectionID, array( 'label_for' => self::$cssPrefix.$_tmpID ));
}
protected function vueSettingsScreen() {
$settings = array(
'options' => $GLOBALS['CROP_THUMBNAILS_HELPER']->getOptions(),
'post_types' => $GLOBALS['CROP_THUMBNAILS_HELPER']->getPostTypes(),
'image_sizes' => $GLOBALS['CROP_THUMBNAILS_HELPER']->getImageSizes(),
'lang' => array(
'choose_image_sizes' => esc_js(__('Choose the image sizes you do not want to show, if the user uses the button below the featured image box.','crop-thumbnails')),
'hide_on_post_type' => esc_js(__('Hide Crop-Thumbnails button below the featured image?','crop-thumbnails'))
)
);
?>
vueSettingsScreen();
}
public function emptySectionDescription() {/*empty*/ }
public function callback_user_permission_only_on_edit_files() {
$options = $GLOBALS['CROP_THUMBNAILS_HELPER']->getOptions();
$_id = 'user_permission_only_on_edit_files';
if(empty($options[$_id])) { $options[$_id] = ''; }
echo '';
?>
getOptions();
$_id = 'debug_js';
if(empty($options[$_id])) { $options[$_id] = ''; }
echo '';
}
public function callback_debug_data() {
$options = $GLOBALS['CROP_THUMBNAILS_HELPER']->getOptions();
$_id = 'debug_data';
if(empty($options[$_id])) { $options[$_id] = ''; }
echo '';
}
public function validateSettings($input) {
$sizes = $GLOBALS['CROP_THUMBNAILS_HELPER']->getImageSizes();
$post_types = $GLOBALS['CROP_THUMBNAILS_HELPER']->getPostTypes();
$storeInDb = array();
//check input[hide_post_type] --> are the post_types real there
if(!empty($input['hide_post_type'])) {
foreach($input['hide_post_type'] as $_post_type_name=>$value) {
if(isset($post_types[$_post_type_name])) {
$storeInDb['hide_post_type'][$_post_type_name] = '1';
}
}
}
//check $input[sizes] --> are post_types correct, are sizes real there
if(!empty($input['hide_size'])) {
foreach($input['hide_size'] as $_post_type_name=>$size_type) {
if(isset($post_types[$_post_type_name])) {
foreach($size_type as $_size_name=>$value) {
if(isset($sizes[$_size_name])) {
$storeInDb['hide_size'][$_post_type_name][$_size_name] = '1';
}
}
}
}
}
$_tmpID = 'user_permission_only_on_edit_files';
if(!empty($input[$_tmpID])) {
$storeInDb[$_tmpID] = 1;
}
/* Advanced Section */
$_tmpID = 'debug_js';
if(!empty($input[$_tmpID])) {
$storeInDb[$_tmpID] = 1;
}
$_tmpID = 'debug_data';
if(!empty($input[$_tmpID])) {
$storeInDb[$_tmpID] = 1;
}
return $storeInDb;
}
public function sectionDescriptionTest() {?>
getUploadDir().DIRECTORY_SEPARATOR.'testfile.jpg';
try {
$report[] = 'info Crop-Thumbnails '.CROP_THUMBNAILS_VERSION;
$report[] = 'info PHP '.phpversion();
$report[] = 'info PHP memory limit '.ini_get('memory_limit');
$report[] = 'info '._wp_image_editor_choose(array('mime_type' => 'image/jpeg')).' (choosed Wordpress imageeditor class for jpg)';
//check if tmp-folder can be generated
if(is_dir($GLOBALS['CROP_THUMBNAILS_HELPER']->getUploadDir())) {
$report[] = 'success Temporary directory exists';
} else {
if (!mkdir($GLOBALS['CROP_THUMBNAILS_HELPER']->getUploadDir())) {
throw new \Exception('fail Creating the temporary directory ('.esc_attr($GLOBALS['CROP_THUMBNAILS_HELPER']->getUploadDir()).') | is the upload-directory writable with PHP?');
} else {
$report[] = 'success Temporary directory could be created';
}
}
//creating the testfile in temporary directory
if(!@copy($sourceFile,$tempFile)) {
throw new \Exception('fail Copy testfile to temporary directory | is the tmp-directory writable with PHP?');
} else {
$report[] = 'success Copy testfile to temporary directory';
$doDeleteTempFile = true;
}
//try to upload the file
$_FILES['cpt_quicktest'] = array(
'name' => 'test_image.jpg',
'type' => 'image/jpeg',
'tmp_name' => $tempFile,
'error' => 0,
'size' => 102610
);
$attachmentId = media_handle_upload( 'cpt_quicktest', 0, array(), array( 'test_form' => false, 'action'=>'test' ) );
$doDeleteTempFile = false;//is be deleted automatically
if ( is_wp_error( $attachmentId ) ) {
throw new \Exception('fail Adding testfile to media-library ('.$attachmentId->get_error_message().') | is the upload-directory writable with PHP?');
} else {
$report[] = 'success Testfile was successfully added to media-library. (ID:'.$attachmentId.')';
$doDeleteAttachement = true;
}
//try to crop with the same function as the plugin does
$cropResult = wp_crop_image( // * @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
$attachmentId, // * @param string|int $src The source file or Attachment ID.
130, // * @param int $src_x The start x position to crop from.
275, // * @param int $src_y The start y position to crop from.
945, // * @param int $src_w The width to crop.
120, // * @param int $src_h The height to crop.
200, // * @param int $dst_w The destination width.
25, // * @param int $dst_h The destination height.
false, // * @param int $src_abs Optional. If the source crop points are absolute.
$tempFile // * @param string $dst_file Optional. The destination file to write to.
);
if ( is_wp_error( $cropResult ) ) {
throw new \Exception('fail Cropping the file ('.$cropResult->get_error_message().')');
} else {
$report[] = 'success Cropping the file';
$doDeleteTempFile = true;
$doDeleteAttachement = true;
}
//check if the dimensions are correct
$fileDimensions = getimagesize($tempFile);
if(!empty($fileDimensions[0]) && !empty($fileDimensions[1]) && !empty($fileDimensions['mime'])) {
$_checkDimensionsOk = true;
if($fileDimensions[0]!==200 || $fileDimensions[1]!==25) {
$_checkDimensionsOk = false;
$report[] = 'fail Cropped image dimensions are wrong.';
}
if($fileDimensions['mime']!=='image/jpeg') {
$_checkDimensionsOk = false;
$report[] = 'fail Cropped image dimensions mime-type is wrong.';
}
if($_checkDimensionsOk) {
$report[] = 'success Cropped image dimensions are correct.';
}
} else {
$report[] = 'fail Problem with getting the image dimensions of the cropped file.';
}
} catch(\Exception $e) {
$report[] = $e->getMessage();
}
//DO CLEANUP
//delete attachement file
if($doDeleteAttachement && $attachmentId!==-1) {
if ( false === wp_delete_attachment( $attachmentId ) ) {
$report[] = 'fail Error while deleting test attachment';
} else {
$report[] = 'success Test-attachement successfull deleted (ID:'.$attachmentId.')';
}
}
//deleting testfile form temporary directory
if($doDeleteTempFile) {
if(!@unlink($tempFile)) {
$report[] = 'fail Remove testfile from temporary directory';
} else {
$report[] = 'success Remove testfile from temporary directory';
}
}
$report[] = 'info Tests complete';
echo join($report,"
");
exit();
}
}
$cptSettingsScreen = new CropThumbnailsSettingsScreen();