| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
add_filter( 'attachment_fields_to_edit', '_imagify_attachment_fields_to_edit', IMAGIFY_INT_MAX, 2 ); |
| 5 |
/** |
| 6 |
* Add "Imagify" column in the Media Uploader |
| 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 |
| 14 |
*/ |
| 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_current_user_can( 'manual-optimize', $post->ID ) ) { |
| 23 |
return $form_fields; |
| 24 |
} |
| 25 |
|
| 26 |
$attachment = get_imagify_attachment( 'wp', $post->ID, 'attachment_fields_to_edit' ); |
| 27 |
|
| 28 |
$form_fields['imagify'] = array( |
| 29 |
'label' => 'Imagify', |
| 30 |
'input' => 'html', |
| 31 |
'html' => get_imagify_media_column_content( $attachment ), |
| 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_current_user_can( 'manual-optimize', $post->ID ) ) { |
| 52 |
return $actions; |
| 53 |
} |
| 54 |
|
| 55 |
$attachment = get_imagify_attachment( 'wp', $post->ID, 'media_row_actions' ); |
| 56 |
|
| 57 |
// If this attachment is not an image, do nothing. |
| 58 |
if ( ! $attachment->is_extension_supported() ) { |
| 59 |
return $actions; |
| 60 |
} |
| 61 |
|
| 62 |
// If Imagify license not valid, or image is not optimized, do nothing. |
| 63 |
if ( ! imagify_valid_key() || ! $attachment->is_optimized() ) { |
| 64 |
return $actions; |
| 65 |
} |
| 66 |
|
| 67 |
// If was not activated for that image, do nothing. |
| 68 |
if ( ! $attachment->get_backup_url() ) { |
| 69 |
return $actions; |
| 70 |
} |
| 71 |
|
| 72 |
$image = wp_get_attachment_image_src( $post->ID, 'full' ); |
| 73 |
|
| 74 |
// If full image is too small. See get_imagify_localize_script_translations(). |
| 75 |
if ( ! $image || (int) $image[1] < 360 ) { |
| 76 |
return $actions; |
| 77 |
} |
| 78 |
|
| 79 |
// Else, add action link for comparison (JS triggered). |
| 80 |
$actions['imagify-compare'] = sprintf( |
| 81 |
'<a href="%1$s#imagify-compare" data-id="%2$d" data-backup-src="%3$s" data-full-src="%4$s" data-full-width="%5$d" data-full-height="%6$d" data-target="#imagify-comparison-%2$d" class="imagify-compare-images imagify-modal-trigger">%7$s</a>', |
| 82 |
esc_url( get_edit_post_link( $post->ID ) ), |
| 83 |
$post->ID, |
| 84 |
esc_url( $attachment->get_backup_url() ), |
| 85 |
esc_url( $image[0] ), |
| 86 |
$image[1], |
| 87 |
$image[2], |
| 88 |
esc_html__( 'Compare Original VS Optimized', 'imagify' ) |
| 89 |
); |
| 90 |
|
| 91 |
return $actions; |
| 92 |
} |
| 93 |
|