| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
/* |
| 5 |
* Get the optimization data list for a specific attachment. |
| 6 |
* |
| 7 |
* @since 1.0 |
| 8 |
* |
| 9 |
* @param int $attachment_id The attachment ID. |
| 10 |
* @return string The output to print. |
| 11 |
*/ |
| 12 |
function get_imagify_attachment_optimization_text( $attachment_id ) { |
| 13 |
global $pagenow; |
| 14 |
|
| 15 |
$attachment = new Imagify_Attachment( $attachment_id ); |
| 16 |
$data = $attachment->get_data(); |
| 17 |
$output = ( 'post.php' != $pagenow ) ? '<ul class="imagify-datas-list">' : ''; |
| 18 |
$output_before = ( 'post.php' != $pagenow ) ? '<li class="imagify-data-item">' : '<div class="misc-pub-section misc-pub-imagify imagify-data-item">'; |
| 19 |
$output_after = ( 'post.php' != $pagenow ) ? '</li>' : '</div>'; |
| 20 |
$reoptimize_output = ''; |
| 21 |
|
| 22 |
if ( $error = get_imagify_attachment_error_text( $attachment_id ) ) { |
| 23 |
$error = ( 'post.php' === $pagenow ) ? $output_before . $error . $output_after : $error; |
| 24 |
return $error; |
| 25 |
} |
| 26 |
|
| 27 |
$optimization_level = $attachment->get_optimization_level_label(); |
| 28 |
|
| 29 |
if ( imagify_valid_key() && $attachment->has_backup() ) { |
| 30 |
$reoptimize_link = get_imagify_attachment_reoptimize_link( $attachment_id ); |
| 31 |
$reoptimize_output = ( $reoptimize_link ) ? $reoptimize_link : ''; |
| 32 |
} |
| 33 |
|
| 34 |
if ( 'post.php' !== $pagenow ) { |
| 35 |
$output .= $output_before . '<span class="data">' . __( 'New Filesize:', 'imagify' ) . '</span> <strong class="big">' . size_format( $data['sizes']['full']['optimized_size'], 2 ) . '</strong>' . $output_after; |
| 36 |
} |
| 37 |
|
| 38 |
$chart = '<span class="imagify-chart"> |
| 39 |
<span class="imagify-chart-container"> |
| 40 |
<canvas id="imagify-consumption-chart" width="15" height="15"></canvas> |
| 41 |
</span> |
| 42 |
</span>'; |
| 43 |
|
| 44 |
$output .= $output_before . '<span class="data">' . __( 'Original Saving:', 'imagify' ) . '</span> <strong> |
| 45 |
' . ( ( 'post.php' != $pagenow ) ? $chart : '' ) . '<span class="imagify-chart-value">' . $data['sizes']['full']['percent'] . '</span>%</strong>' . $output_after; |
| 46 |
|
| 47 |
// more details section |
| 48 |
if ( 'post.php' != $pagenow ) { |
| 49 |
// new list |
| 50 |
$output .= '</ul>'; |
| 51 |
$output .= '<p class="imagify-datas-more-action"><a href="#imagify-view-details-' . $attachment_id . '" data-close="' . __( 'Close details', 'imagify' ) . '" data-open="' . __( 'View details', 'imagify' ) . '"><span class="the-text">' . __( 'View details', 'imagify' ) . '</span><span class="dashicons dashicons-arrow-down-alt2"></span></a></p>'; |
| 52 |
$output .= '<ul id="imagify-view-details-' . $attachment_id . '" class="imagify-datas-list imagify-datas-details">'; |
| 53 |
|
| 54 |
// not in metabox |
| 55 |
$output .= $output_before . '<span class="data">' . __( 'Original Filesize:', 'imagify' ) . '</span> <strong class="original">' . $attachment->get_original_size() . '</strong>' . $output_after; |
| 56 |
} |
| 57 |
|
| 58 |
$output .= $output_before . '<span class="data">' . __( 'Level:', 'imagify' ) . '</span> <strong>' . $optimization_level . '</strong>' . $output_after; |
| 59 |
|
| 60 |
if ( $total_optimized_thumbnails = $attachment->get_optimized_sizes_count() ) { |
| 61 |
$output .= $output_before . '<span class="data">' . __( 'Thumbnails Optimized:', 'imagify' ) . '</span> <strong>' . $total_optimized_thumbnails . '</strong>' . $output_after; |
| 62 |
$output .= $output_before . '<span class="data">' . __( 'Overall Saving:', 'imagify' ) . '</span> <strong>' . $data['stats']['percent'] . '%</strong>' . $output_after; |
| 63 |
} |
| 64 |
|
| 65 |
// end of list |
| 66 |
$output .= ( 'post.php' != $pagenow ) ? '</ul>' : ''; |
| 67 |
|
| 68 |
// actions section |
| 69 |
$output .= ( 'post.php' != $pagenow ) ? '' : $output_before; |
| 70 |
$output .= '<div class="imagify-datas-actions-links">'; |
| 71 |
$output .= $reoptimize_output; |
| 72 |
|
| 73 |
if ( $attachment->has_backup() ) { |
| 74 |
$class = ( 'post.php' !== $pagenow ) ? 'button-imagify-restore' : ''; |
| 75 |
$output .= '<a id="imagify-restore-' . $attachment_id . '" href="' . get_imagify_admin_url( 'restore-upload', $attachment_id ) . '" class="' . $class . '" data-waiting-label="' . esc_attr__( 'Restoring...', 'imagify' ) . '"><span class="dashicons dashicons-image-rotate"></span>' . __( 'Restore Original', 'imagify' ) . '</a>'; |
| 76 |
} |
| 77 |
|
| 78 |
$output .= '</div><!-- .imagify-datas-actions-links -->'; |
| 79 |
$output .= ( 'post.php' != $pagenow ) ? '' : $output_after; |
| 80 |
|
| 81 |
return $output; |
| 82 |
} |
| 83 |
|
| 84 |
/* |
| 85 |
* Get the error message for a specific attachment. |
| 86 |
* |
| 87 |
* @since 1.0 |
| 88 |
* |
| 89 |
* @param int $attachment_id The attachement ID. |
| 90 |
* @return string The output to print. |
| 91 |
*/ |
| 92 |
function get_imagify_attachment_error_text( $attachment_id ) { |
| 93 |
global $pagenow; |
| 94 |
$data = get_post_meta( $attachment_id, '_imagify_data', true ); |
| 95 |
$output = ''; |
| 96 |
|
| 97 |
if ( isset( $data['sizes']['full']['success'] ) && ! $data['sizes']['full']['success'] ) { |
| 98 |
$class = ( 'post.php' !== $pagenow ) ? 'button-imagify-manual-upload' : ''; |
| 99 |
$output .= '<strong>' . $data['sizes']['full']['error'] . '</strong><br/><a id="imagify-upload-' . $attachment_id . '" class="button ' . $class . '" href="' . get_imagify_admin_url( 'manual-upload', $attachment_id ) . '" data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '">' . __( 'Try again', 'imagify' ) . '</a>'; |
| 100 |
} |
| 101 |
|
| 102 |
return $output; |
| 103 |
} |
| 104 |
|
| 105 |
/* |
| 106 |
* Get the re-optimize link for a specific attachment. |
| 107 |
* |
| 108 |
* @since 1.0 |
| 109 |
* |
| 110 |
* @param int $attachment_id The attachement ID. |
| 111 |
* @return string The output to print. |
| 112 |
*/ |
| 113 |
function get_imagify_attachment_reoptimize_link( $attachment_id ) { |
| 114 |
global $pagenow; |
| 115 |
|
| 116 |
$attachment = new Imagify_Attachment( $attachment_id ); |
| 117 |
$level = (int) $attachment->get_optimization_level(); |
| 118 |
$args = array( 'attachment_id' => $attachment_id ); |
| 119 |
$output = ''; |
| 120 |
|
| 121 |
if ( $attachment->has_backup() ) { |
| 122 |
$class = ( 'post.php' !== $pagenow ) ? 'button-imagify-manual-override-upload' : ''; |
| 123 |
|
| 124 |
// Re-optimize to Ultra |
| 125 |
if ( 1 === $level || 0 === $level ) { |
| 126 |
$args['optimization_level'] = 2; |
| 127 |
$output .= '<a href="' . get_imagify_admin_url( 'manual-override-upload', $args ) . '" class="' . $class . '" data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '"><span class="dashicons dashicons-admin-generic"></span>' . sprintf( __( 'Re-Optimize to %s', 'imagify' ), __( 'Ultra', 'imagify' ) ) . '</a>'; |
| 128 |
} |
| 129 |
|
| 130 |
// Re-optimize to Aggressive |
| 131 |
if ( 2 === $level || 0 === $level ) { |
| 132 |
$args['optimization_level'] = 1; |
| 133 |
$output .= '<a href="' . get_imagify_admin_url( 'manual-override-upload', $args ) . '" class="' . $class . '" data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '"><span class="dashicons dashicons-admin-generic"></span>' . sprintf( __( 'Re-Optimize to %s', 'imagify' ), __( 'Aggressive', 'imagify' ) ) . '</a>'; |
| 134 |
} |
| 135 |
|
| 136 |
// Re-optimize to Normal |
| 137 |
if ( 2 === $level || 1 === $level ) { |
| 138 |
$args['optimization_level'] = 0; |
| 139 |
$output .= '<a href="' . get_imagify_admin_url( 'manual-override-upload', $args ) . '" class="' . $class . '" data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '"><span class="dashicons dashicons-admin-generic"></span>' . sprintf( __( 'Re-Optimize to %s', 'imagify' ), __( 'Normal', 'imagify' ) ) . '</a>'; |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
return $output; |
| 145 |
} |
| 146 |
|
| 147 |
/* |
| 148 |
* Get all data to diplay for a specific attachment. |
| 149 |
* |
| 150 |
* @since 1.2 |
| 151 |
* |
| 152 |
* @param int $attachment_id The attachement ID. |
| 153 |
* @return string The output to print. |
| 154 |
*/ |
| 155 |
function get_imagify_media_column_content( $attachment_id ) { |
| 156 |
$output = ''; |
| 157 |
$attachment = new Imagify_Attachment( $attachment_id ); |
| 158 |
$attachment_ext = $attachment->get_extension(); |
| 159 |
|
| 160 |
// Check if the attachment extension is allowed |
| 161 |
if ( ! wp_attachment_is_image( $attachment_id ) ) { |
| 162 |
$output = sprintf( __( '%s can\'t be optimized', 'imagify' ), strtoupper( $attachment_ext ) ); |
| 163 |
return $output; |
| 164 |
} |
| 165 |
|
| 166 |
// Check if the API key is valid |
| 167 |
if ( ! imagify_valid_key() && ! $attachment->is_optimized() ) { |
| 168 |
$output .= __( 'Invalid API key', 'imagify' ); |
| 169 |
$output .= '<br/>'; |
| 170 |
$output .= '<a href="' . get_imagify_admin_url( 'options-general' ) . '">' . __( 'Check your Settings', 'imagify' ) . '</a>'; |
| 171 |
return $output; |
| 172 |
} |
| 173 |
|
| 174 |
if ( false !== get_transient( 'imagify-async-in-progress-' . $attachment_id ) ) { |
| 175 |
$output = '<div class="button"><span class="imagify-spinner"></span>' . __( 'Optimizing...', 'imagify' ) . '</div>'; |
| 176 |
return $output; |
| 177 |
} |
| 178 |
|
| 179 |
// Check if the image was optimized |
| 180 |
if ( ! $attachment->is_optimized() && ! $attachment->has_error() ) { |
| 181 |
$output .= '<a id="imagify-upload-' . $attachment_id . '" href="' . get_imagify_admin_url( 'manual-upload', $attachment_id ) . '" class="button-primary button-imagify-manual-upload" data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '">' . __( 'Optimize', 'imagify' ) . '</a>'; |
| 182 |
return $output; |
| 183 |
} |
| 184 |
|
| 185 |
$output .= get_imagify_attachment_optimization_text( $attachment_id ); |
| 186 |
return $output; |
| 187 |
} |