| @@ -1,64 +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 ) { | |
| 15 | +function _imagify_attachment_fields_to_edit( $form_fields, $post ) { | |
| 11 | 16 | global $pagenow; |
| 12 | - if ( 'post.php' == $pagenow ) { | |
| 17 | + | |
| 18 | + if ( 'post.php' === $pagenow ) { | |
| 13 | 19 | return $form_fields; |
| 14 | 20 | } |
| 15 | - | |
| 16 | - $form_fields['imagify'] = array( | |
| 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'] = [ | |
| 17 | 29 | 'label' => 'Imagify', |
| 18 | 30 | 'input' => 'html', |
| 19 | - 'html' => get_imagify_media_column_content( $post->ID ), | |
| 31 | + 'html' => get_imagify_media_column_content( $process ), | |
| 20 | 32 | 'show_in_edit' => true, |
| 21 | 33 | 'show_in_modal' => true, |
| 22 | - ); | |
| 23 | - | |
| 34 | + ]; | |
| 35 | + | |
| 24 | 36 | return $form_fields; |
| 25 | 37 | } |
| 26 | 38 | |
| 39 | +add_filter( 'media_row_actions', '_imagify_add_actions_to_media_list_row', IMAGIFY_INT_MAX, 2 ); | |
| 27 | 40 | /** |
| 28 | 41 | * Add "Compare Original VS Optimized" link to the media row action |
| 29 | 42 | * |
| 30 | 43 | * @since 1.4.3 |
| 31 | - * @author Geoffrey Crofte | |
| 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 | |
| 32 | 49 | */ |
| 33 | -add_filter( 'media_row_actions', '_imagify_add_actions_to_media_list_row', PHP_INT_MAX, 2 ); | |
| 34 | 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 | + } | |
| 35 | 54 | |
| 36 | - // if this attachment is not an image, do nothing | |
| 37 | - if ( ! wp_attachment_is_image( $post->ID ) ) { | |
| 55 | + $process = imagify_get_optimization_process( $post->ID, 'wp' ); | |
| 56 | + | |
| 57 | + if ( ! $process->is_valid() ) { | |
| 38 | 58 | return $actions; |
| 39 | 59 | } |
| 40 | 60 | |
| 41 | - $attachment = new Imagify_Attachment(); | |
| 61 | + $media = $process->get_media(); | |
| 42 | 62 | |
| 43 | - // if Imagify license not valid, or image is not optimized, do nothing | |
| 44 | - if ( ! imagify_valid_key() || ! $attachment->is_optimized() ) { | |
| 63 | + // If this media is not an image, do nothing. | |
| 64 | + if ( ! $media->is_supported() || ! $media->is_image() ) { | |
| 45 | 65 | return $actions; |
| 46 | 66 | } |
| 47 | - | |
| 48 | - // if was not activated for that image, do nothing | |
| 49 | - if ( '' === $attachment->get_backup_url() ) { | |
| 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() ) { | |
| 50 | 72 | return $actions; |
| 51 | 73 | } |
| 52 | 74 | |
| 53 | - $image = wp_get_attachment_image_src( $post->ID, 'full' ); | |
| 75 | + // If no backup, do nothing. | |
| 76 | + if ( ! $media->has_backup() ) { | |
| 77 | + return $actions; | |
| 78 | + } | |
| 54 | 79 | |
| 55 | - // if full image is too small | |
| 56 | - if ( (int) $image[1] < 360 ) { | |
| 80 | + $dimensions = $media->get_dimensions(); | |
| 81 | + | |
| 82 | + // If full image is too small. See get_imagify_localize_script_translations(). | |
| 83 | + if ( $dimensions['width'] < 360 ) { | |
| 57 | 84 | return $actions; |
| 58 | 85 | } |
| 59 | 86 | |
| 60 | - // else, add action link for comparison (JS triggered) | |
| 61 | - $actions['imagify-compare'] = '<a href="' . get_edit_post_link( $post->ID ) . '#imagify-compare" data-id="' . $post->ID . '" data-backup-src="' . $attachment->get_backup_url() . '" data-full-src="' . $image[0] . '" data-full-width="' . $image[1] . '" data-full-height="' . $image[2] . '" data-target="#imagify-comparison-' . $post->ID . '" class="imagify-compare-images imagify-modal-trigger">' . esc_html__('Compare Original VS Optimized', 'imagify') . '</a>'; | |
| 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 | + ); | |
| 62 | 99 | |
| 63 | 100 | return $actions; |
| 64 | -} | |
| 101 | +} | |