settings = [ 'version' => get_plugin_data(__FILE__)['Version'], 'url' => plugin_dir_url(__FILE__), 'path' => plugin_dir_path(__FILE__), ]; $this->temp_path = null; // set text domain // https://codex.wordpress.org/Function_Reference/load_plugin_textdomain load_plugin_textdomain('acf-image-aspect-ratio-crop'); add_action('plugins_loaded', [$this, 'initialize_settings']); // include field add_action('acf/include_field_types', [ $this, 'include_field_types', ]); // v5 add_action('wp_ajax_acf_image_aspect_ratio_crop_crop', function () { // WTF WordPress $post = array_map('stripslashes_deep', $_POST); $data = json_decode($post['data'], true); $image_data = wp_get_attachment_metadata($data['id']); // If the difference between the images is less than half a percentage, use the original image // prettier-ignore if ($image_data['height'] - $data['height'] < $image_data['height'] * 0.005 && $image_data['width'] - $data['width'] < $image_data['width'] * 0.005 ) { wp_send_json(['id' => $data['id']]); wp_die(); } do_action('aiarc_pre_customize_upload_dir'); $media_dir = wp_upload_dir(); do_action('aiarc_after_customize_upload_dir'); // WP Smush compat: use original image if it exists $file = $media_dir['basedir'] . '/' . $image_data['file']; $parts = explode('.', $file); $extension = array_pop($parts); $backup_file = implode('.', $parts) . '.bak.' . $extension; $image = null; if (file_exists($backup_file)) { $image = wp_get_image_editor($backup_file); } else if (file_exists($file)) { $image = wp_get_image_editor($file); } else { // Let's attempt to get the file by URL $temp_name = Uuid::uuid4()->toString(); $temp_directory = get_temp_dir(); $this->temp_path = $temp_directory . $temp_name; try { $guzzle = new \GuzzleHttp\Client(); $fetched_image = $guzzle->get(wp_get_attachment_url($data['id'])); $result = @file_put_contents($this->temp_path, $fetched_image->getBody()); if ($result === false) { throw new Exception('Failed to save image'); } $image = wp_get_image_editor($this->temp_path); } catch (Exception $exception) { $this->cleanup(); wp_send_json('Failed fetch remote image', 500); wp_die(); } } if (is_wp_error($image)) { $this->cleanup(); wp_send_json('Failed to open image', 500); wp_die(); } $image->crop( $data['x'], $data['y'], $data['width'], $data['height'] ); // Retrieve original filename and seperate it from its file extension $original_file_name = explode( '.', basename($image_data['file']) ); // Retrieve and remove file extension from array $original_file_extension = array_pop($original_file_name); // Generate new base filename $target_file_name = implode('.', $original_file_name) . '-aspect-ratio-' . $data['aspectRatioWidth'] . 'x' . $data['aspectRatioHeight'] . '.' . $original_file_extension; // Generate target path new file using existing media library $target_file_path = $media_dir['path'] . '/' . wp_unique_filename($media_dir['path'], $target_file_name); // Get the relative path to save as the actual image url $target_relative_path = str_replace( $media_dir['basedir'] . '/', '', $target_file_path ); //$save = $image->save('test.jpg'); $save = $image->save($target_file_path); if (is_wp_error($save)) { $this->cleanup(); wp_send_json('Failed to crop', 500); wp_die(); } $wp_filetype = wp_check_filetype($target_relative_path, null); $attachment = [ 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace( '/\.[^.]+$/', '', $target_file_name ), 'post_content' => '', 'post_status' => 'publish', ]; $attachment_id = wp_insert_attachment( $attachment, $target_relative_path ); if (is_wp_error($attachment_id)) { $this->cleanup(); wp_send_json('Failed to save attachment', 500); wp_die(); } require_once ABSPATH . "wp-admin" . '/includes/image.php'; $attachment_data = wp_generate_attachment_metadata( $attachment_id, $target_file_path ); wp_update_attachment_metadata($attachment_id, $attachment_data); add_post_meta( $attachment_id, 'acf_image_aspect_ratio_crop', true, true ); add_post_meta( $attachment_id, 'acf_image_aspect_ratio_crop_original_image_id', $data['id'], true ); $this->cleanup(); wp_send_json(['id' => $attachment_id]); wp_die(); }); // Hide cropped images in media library grid view add_filter('ajax_query_attachments_args', function ($args) { // post__in is only defined when clicking edit button in attachment if (empty($args['post__in'])) { $args['meta_query'] = [ [ 'key' => 'acf_image_aspect_ratio_crop', 'compare' => 'NOT EXISTS', ], ]; } return $args; }); // Add plugin to WordPress admin menu add_action('admin_menu', function () { add_submenu_page( null, __("ACF Image Aspect Ratio Crop", "acf-image-aspect-ratio-crop"), __("ACF Image Aspect Ratio Crop", "acf-image-aspect-ratio-crop"), 'manage_options', "acf-image-aspect-ratio-crop", [$this, 'settings_page'] ); }); // Add settings link on the plugin page add_filter('plugin_action_links_' . plugin_basename(__FILE__), function ($links) { $settings_link = '' . __('Settings', "acf-image-aspect-ratio-crop") . ''; array_unshift($links, $settings_link); return $links; }); } /* * include_field_types * * This function will include the field type class * * @type function * @date 17/02/2016 * @since 1.0.0 * * @param $version (int) major ACF version. Defaults to false * @return n/a */ function include_field_types() { // include include_once 'fields/class-npx-acf-field-image-aspect-ratio-crop-v5.php'; } /** * Render WordPress plugin settings page */ public function settings_page() { $updated = false; $settings = $this->user_settings; if (!empty($_POST)) { check_admin_referer("acf-image-aspect-ratio-crop"); if (!empty($_POST['modal_type'])) { $settings['modal_type'] = $_POST['modal_type']; } update_option("acf-image-aspect-ratio-crop-settings", $settings); $updated = true; } $modal_type = $settings['modal_type']; echo '
' . __('Options have been updated', "acf-image-aspect-ratio-crop") . '
'; echo '