| @@ -1,25 +1,101 @@ | ||
| 1 | -<?php | |
| 2 | -defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); | |
| 1 | +<?php | |
| 2 | +defined( 'ABSPATH' ) || exit; | |
| 3 | 3 | |
| 4 | -/* | |
| 4 | +add_filter( 'attachment_fields_to_edit', '_imagify_attachment_fields_to_edit', IMAGIFY_INT_MAX, 2 ); | |
| 5 | +/** | |
| 5 | 6 | * Add "Imagify" column in the Media Uploader |
| 6 | 7 | * |
| 7 | 8 | * @since 1.2 |
| 9 | + * @author Jonathan Buttigieg | |
| 10 | + * | |
| 11 | + * @param array $form_fields An array of attachment form fields. | |
| 12 | + * @param object $post The WP_Post attachment object. | |
| 13 | + * @return array | |
| 8 | 14 | */ |
| 9 | -add_filter( 'attachment_fields_to_edit', '_imagify_attachment_fields_to_edit', PHP_INT_MAX, 2 ); | |
| 10 | -function _imagify_attachment_fields_to_edit( $form_fields, $post ) { | |
| 11 | - global $pagenow; | |
| 12 | - if ( 'post.php' == $pagenow ) { | |
| 13 | - return $form_fields; | |
| 14 | - } | |
| 15 | - | |
| 16 | - $form_fields['imagify'] = array( | |
| 17 | - 'label' => 'Imagify', | |
| 18 | - 'input' => 'html', | |
| 19 | - 'html' => get_imagify_media_column_content( $post->ID ), | |
| 20 | - 'show_in_edit' => true, | |
| 21 | - 'show_in_modal' => true, | |
| 22 | - ); | |
| 23 | - | |
| 24 | - return $form_fields; | |
| 25 | -} | |
| 15 | +function _imagify_attachment_fields_to_edit( $form_fields, $post ) { | |
| 16 | + global $pagenow; | |
| 17 | + | |
| 18 | + if ( 'post.php' === $pagenow ) { | |
| 19 | + return $form_fields; | |
| 20 | + } | |
| 21 | + | |
| 22 | + if ( ! imagify_get_context( 'wp' )->current_user_can( 'manual-optimize', $post->ID ) ) { | |
| 23 | + return $form_fields; | |
| 24 | + } | |
| 25 | + | |
| 26 | + $process = imagify_get_optimization_process( $post->ID, 'wp' ); | |
| 27 | + | |
| 28 | + $form_fields['imagify'] = [ | |
| 29 | + 'label' => 'Imagify', | |
| 30 | + 'input' => 'html', | |
| 31 | + 'html' => get_imagify_media_column_content( $process ), | |
| 32 | + 'show_in_edit' => true, | |
| 33 | + 'show_in_modal' => true, | |
| 34 | + ]; | |
| 35 | + | |
| 36 | + return $form_fields; | |
| 37 | +} | |
| 38 | + | |
| 39 | +add_filter( 'media_row_actions', '_imagify_add_actions_to_media_list_row', IMAGIFY_INT_MAX, 2 ); | |
| 40 | +/** | |
| 41 | + * Add "Compare Original VS Optimized" link to the media row action | |
| 42 | + * | |
| 43 | + * @since 1.4.3 | |
| 44 | + * @author Geoffrey Crofte | |
| 45 | + * @param array $actions An array of action links for each attachment. | |
| 46 | + * Default 'Edit', 'Delete Permanently', 'View'. | |
| 47 | + * @param object $post WP_Post object for the current attachment. | |
| 48 | + * @return array | |
| 49 | + */ | |
| 50 | +function _imagify_add_actions_to_media_list_row( $actions, $post ) { | |
| 51 | + if ( ! imagify_get_context( 'wp' )->current_user_can( 'manual-optimize', $post->ID ) ) { | |
| 52 | + return $actions; | |
| 53 | + } | |
| 54 | + | |
| 55 | + $process = imagify_get_optimization_process( $post->ID, 'wp' ); | |
| 56 | + | |
| 57 | + if ( ! $process->is_valid() ) { | |
| 58 | + return $actions; | |
| 59 | + } | |
| 60 | + | |
| 61 | + $media = $process->get_media(); | |
| 62 | + | |
| 63 | + // If this media is not an image, do nothing. | |
| 64 | + if ( ! $media->is_supported() || ! $media->is_image() ) { | |
| 65 | + return $actions; | |
| 66 | + } | |
| 67 | + | |
| 68 | + $data = $process->get_data(); | |
| 69 | + | |
| 70 | + // If Imagify license not valid, or image is not optimized, do nothing. | |
| 71 | + if ( ! Imagify_Requirements::is_api_key_valid() || ! $data->is_optimized() ) { | |
| 72 | + return $actions; | |
| 73 | + } | |
| 74 | + | |
| 75 | + // If no backup, do nothing. | |
| 76 | + if ( ! $media->has_backup() ) { | |
| 77 | + return $actions; | |
| 78 | + } | |
| 79 | + | |
| 80 | + $dimensions = $media->get_dimensions(); | |
| 81 | + | |
| 82 | + // If full image is too small. See get_imagify_localize_script_translations(). | |
| 83 | + if ( $dimensions['width'] < 360 ) { | |
| 84 | + return $actions; | |
| 85 | + } | |
| 86 | + | |
| 87 | + // Else, add action link for comparison (JS triggered). | |
| 88 | + $actions['imagify-compare'] = Imagify_Views::get_instance()->get_template( | |
| 89 | + 'button/compare-images', | |
| 90 | + [ | |
| 91 | + 'url' => get_edit_post_link( $media->get_id() ) . '#imagify-compare', | |
| 92 | + 'backup_url' => $media->get_backup_url(), | |
| 93 | + 'original_url' => $media->get_fullsize_url(), | |
| 94 | + 'media_id' => $media->get_id(), | |
| 95 | + 'width' => $dimensions['width'], | |
| 96 | + 'height' => $dimensions['height'], | |
| 97 | + ] | |
| 98 | + ); | |
| 99 | + | |
| 100 | + return $actions; | |
| 101 | +} | |