Plugins
This commit is contained in:
@@ -1,795 +0,0 @@
|
||||
<?php
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Count number of attachments.
|
||||
*
|
||||
* @since 1.0
|
||||
* @author Jonathan Buttigieg
|
||||
*
|
||||
* @return int The number of attachments.
|
||||
*/
|
||||
function imagify_count_attachments() {
|
||||
global $wpdb;
|
||||
static $count;
|
||||
|
||||
/**
|
||||
* Filter the number of attachments.
|
||||
* 3rd party will be able to override the result.
|
||||
*
|
||||
* @since 1.5
|
||||
*
|
||||
* @param int|bool $pre_count Default is false. Provide an integer.
|
||||
*/
|
||||
$pre_count = apply_filters( 'imagify_count_attachments', false );
|
||||
|
||||
if ( false !== $pre_count ) {
|
||||
return (int) $pre_count;
|
||||
}
|
||||
|
||||
if ( isset( $count ) ) {
|
||||
return $count;
|
||||
}
|
||||
|
||||
$mime_types = Imagify_DB::get_mime_types();
|
||||
$statuses = Imagify_DB::get_post_statuses();
|
||||
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause('p.ID', true, true,
|
||||
"AND p.post_mime_type IN ( $mime_types )
|
||||
AND p.post_type = 'attachment'
|
||||
AND p.post_status IN ( $statuses )");
|
||||
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
|
||||
$count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
|
||||
"
|
||||
SELECT COUNT( p.ID )
|
||||
FROM $wpdb->posts AS p
|
||||
$nodata_join
|
||||
WHERE p.post_mime_type IN ( $mime_types )
|
||||
AND p.post_type = 'attachment'
|
||||
AND p.post_status IN ( $statuses )
|
||||
$nodata_where"
|
||||
);
|
||||
|
||||
if ( $count > imagify_get_unoptimized_attachment_limit() ) {
|
||||
set_transient( 'imagify_large_library', 1 );
|
||||
} elseif ( get_transient( 'imagify_large_library' ) ) {
|
||||
// In case the number is decreasing under our limit.
|
||||
delete_transient( 'imagify_large_library' );
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count number of optimized attachments with an error.
|
||||
*
|
||||
* @since 1.0
|
||||
* @author Jonathan Buttigieg
|
||||
*
|
||||
* @return int The number of attachments.
|
||||
*/
|
||||
function imagify_count_error_attachments() {
|
||||
global $wpdb;
|
||||
static $count;
|
||||
|
||||
/**
|
||||
* Filter the number of optimized attachments with an error.
|
||||
* 3rd party will be able to override the result.
|
||||
*
|
||||
* @since 1.5
|
||||
*
|
||||
* @param int|bool $pre_count Default is false. Provide an integer.
|
||||
*/
|
||||
$pre_count = apply_filters( 'imagify_count_error_attachments', false );
|
||||
|
||||
if ( false !== $pre_count ) {
|
||||
return (int) $pre_count;
|
||||
}
|
||||
|
||||
if ( isset( $count ) ) {
|
||||
return $count;
|
||||
}
|
||||
|
||||
$mime_types = Imagify_DB::get_mime_types();
|
||||
$statuses = Imagify_DB::get_post_statuses();
|
||||
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
|
||||
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
|
||||
$count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
|
||||
"
|
||||
SELECT COUNT( DISTINCT p.ID )
|
||||
FROM $wpdb->posts AS p
|
||||
$nodata_join
|
||||
INNER JOIN $wpdb->postmeta AS mt1
|
||||
ON ( p.ID = mt1.post_id AND mt1.meta_key = '_imagify_status' )
|
||||
WHERE p.post_mime_type IN ( $mime_types )
|
||||
AND p.post_type = 'attachment'
|
||||
AND p.post_status IN ( $statuses )
|
||||
AND mt1.meta_value = 'error'
|
||||
$nodata_where"
|
||||
);
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count number of optimized attachments (by Imagify or an other tool before).
|
||||
*
|
||||
* @since 1.0
|
||||
* @author Jonathan Buttigieg
|
||||
*
|
||||
* @return int The number of attachments.
|
||||
*/
|
||||
function imagify_count_optimized_attachments() {
|
||||
global $wpdb;
|
||||
static $count;
|
||||
|
||||
/**
|
||||
* Filter the number of optimized attachments.
|
||||
* 3rd party will be able to override the result.
|
||||
*
|
||||
* @since 1.5
|
||||
*
|
||||
* @param int|bool $pre_count Default is false. Provide an integer.
|
||||
*/
|
||||
$pre_count = apply_filters( 'imagify_count_optimized_attachments', false );
|
||||
|
||||
if ( false !== $pre_count ) {
|
||||
return (int) $pre_count;
|
||||
}
|
||||
|
||||
if ( isset( $count ) ) {
|
||||
return $count;
|
||||
}
|
||||
|
||||
$mime_types = Imagify_DB::get_mime_types();
|
||||
$statuses = Imagify_DB::get_post_statuses();
|
||||
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
|
||||
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
|
||||
$count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
|
||||
"
|
||||
SELECT COUNT( DISTINCT p.ID )
|
||||
FROM $wpdb->posts AS p
|
||||
$nodata_join
|
||||
INNER JOIN $wpdb->postmeta AS mt1
|
||||
ON ( p.ID = mt1.post_id AND mt1.meta_key = '_imagify_status' )
|
||||
WHERE p.post_mime_type IN ( $mime_types )
|
||||
AND p.post_type = 'attachment'
|
||||
AND p.post_status IN ( $statuses )
|
||||
AND mt1.meta_value IN ( 'success', 'already_optimized' )
|
||||
$nodata_where"
|
||||
);
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count number of unoptimized attachments.
|
||||
*
|
||||
* @since 1.0
|
||||
* @author Jonathan Buttigieg
|
||||
*
|
||||
* @return int The number of attachments.
|
||||
*/
|
||||
function imagify_count_unoptimized_attachments() {
|
||||
/**
|
||||
* Filter the number of unoptimized attachments.
|
||||
* 3rd party will be able to override the result.
|
||||
*
|
||||
* @since 1.5
|
||||
*
|
||||
* @param int|bool $pre_count Default is false. Provide an integer.
|
||||
*/
|
||||
$pre_count = apply_filters( 'imagify_count_unoptimized_attachments', false );
|
||||
|
||||
if ( false !== $pre_count ) {
|
||||
return (int) $pre_count;
|
||||
}
|
||||
|
||||
return imagify_count_attachments() - imagify_count_optimized_attachments() - imagify_count_error_attachments();
|
||||
}
|
||||
|
||||
/**
|
||||
* Count percent of optimized attachments.
|
||||
*
|
||||
* @since 1.0
|
||||
* @author Jonathan Buttigieg
|
||||
*
|
||||
* @return int The percent of optimized attachments.
|
||||
*/
|
||||
function imagify_percent_optimized_attachments() {
|
||||
/**
|
||||
* Filter the percent of optimized attachments.
|
||||
* 3rd party will be able to override the result.
|
||||
*
|
||||
* @since 1.5
|
||||
*
|
||||
* @param int|bool $percent Default is false. Provide an integer.
|
||||
*/
|
||||
$percent = apply_filters( 'imagify_percent_optimized_attachments', false );
|
||||
|
||||
if ( false !== $percent ) {
|
||||
return (int) $percent;
|
||||
}
|
||||
|
||||
$total_attachments = imagify_count_attachments();
|
||||
$total_optimized_attachments = imagify_count_optimized_attachments();
|
||||
|
||||
if ( ! $total_attachments || ! $total_optimized_attachments ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return min( round( 100 * $total_optimized_attachments / $total_attachments ), 100 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Count percent, original & optimized size of all images optimized by Imagify.
|
||||
*
|
||||
* @since 1.0
|
||||
* @since 1.6.7 Revamped to handle huge libraries.
|
||||
* @author Jonathan Buttigieg
|
||||
*
|
||||
* @param string $key What data to return. Choices are between 'count', 'original_size', 'optimized_size', and 'percent'. If left empty, the whole array is returned.
|
||||
* @return array|int An array containing the optimization data. A single data if $key is provided.
|
||||
*/
|
||||
function imagify_count_saving_data( $key = '' ) {
|
||||
global $wpdb;
|
||||
|
||||
/**
|
||||
* Filter the query to get all optimized attachments.
|
||||
* 3rd party will be able to override the result.
|
||||
*
|
||||
* @since 1.5
|
||||
* @since 1.6.7 This filter should return an array containing the following keys: 'count', 'original_size', and 'optimized_size'.
|
||||
*
|
||||
* @param bool|array $attachments An array containing the keys ('count', 'original_size', and 'optimized_size'), or an array of attachments (back compat', deprecated), or false.
|
||||
*/
|
||||
$attachments = apply_filters( 'imagify_count_saving_data', false );
|
||||
|
||||
$original_size = 0;
|
||||
$optimized_size = 0;
|
||||
$count = 0;
|
||||
|
||||
if ( is_array( $attachments ) ) {
|
||||
/**
|
||||
* Bypass.
|
||||
*/
|
||||
if ( isset( $attachments['count'], $attachments['original_size'], $attachments['optimized_size'] ) ) {
|
||||
/**
|
||||
* We have the results we need.
|
||||
*/
|
||||
$attachments['percent'] = $attachments['optimized_size'] && $attachments['original_size'] ? ceil( ( ( $attachments['original_size'] - $attachments['optimized_size'] ) / $attachments['original_size'] ) * 100 ) : 0;
|
||||
|
||||
return $attachments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Back compat'.
|
||||
* The following shouldn't be used. Sites with a huge library won't like it.
|
||||
*/
|
||||
$attachments = array_map( 'maybe_unserialize', (array) $attachments );
|
||||
|
||||
if ( $attachments ) {
|
||||
foreach ( $attachments as $attachment_data ) {
|
||||
if ( ! $attachment_data ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
++$count;
|
||||
$original_data = $attachment_data['sizes']['full'];
|
||||
|
||||
// Increment the original sizes.
|
||||
$original_size += $original_data['original_size'] ? $original_data['original_size'] : 0;
|
||||
$optimized_size += $original_data['optimized_size'] ? $original_data['optimized_size'] : 0;
|
||||
|
||||
unset( $attachment_data['sizes']['full'] );
|
||||
|
||||
// Increment the thumbnails sizes.
|
||||
if ( $attachment_data['sizes'] ) {
|
||||
foreach ( $attachment_data['sizes'] as $size_data ) {
|
||||
if ( ! empty( $size_data['success'] ) ) {
|
||||
$original_size += $size_data['original_size'] ? $size_data['original_size'] : 0;
|
||||
$optimized_size += $size_data['optimized_size'] ? $size_data['optimized_size'] : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/**
|
||||
* Filter the chunk size of the requests fetching the data.
|
||||
* 15,000 seems to be a good balance between memory used, speed, and number of DB hits.
|
||||
*
|
||||
* @param int $limit The maximum number of elements per chunk.
|
||||
*/
|
||||
$limit = apply_filters( 'imagify_count_saving_data_limit', 15000 );
|
||||
$limit = absint( $limit );
|
||||
|
||||
$mime_types = Imagify_DB::get_mime_types();
|
||||
$statuses = Imagify_DB::get_post_statuses();
|
||||
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
|
||||
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
|
||||
$attachment_ids = $wpdb->get_col( // WPCS: unprepared SQL ok.
|
||||
"
|
||||
SELECT p.ID
|
||||
FROM $wpdb->posts AS p
|
||||
$nodata_join
|
||||
INNER JOIN $wpdb->postmeta AS mt1
|
||||
ON ( p.ID = mt1.post_id AND mt1.meta_key = '_imagify_status' )
|
||||
WHERE p.post_mime_type IN ( $mime_types )
|
||||
AND p.post_type = 'attachment'
|
||||
AND p.post_status IN ( $statuses )
|
||||
AND mt1.meta_value = 'success'
|
||||
$nodata_where
|
||||
ORDER BY CAST( p.ID AS UNSIGNED )"
|
||||
);
|
||||
$wpdb->flush();
|
||||
|
||||
$attachment_ids = array_map( 'absint', array_unique( $attachment_ids ) );
|
||||
$attachment_ids = array_chunk( $attachment_ids, $limit );
|
||||
|
||||
while ( $attachment_ids ) {
|
||||
$limit_ids = array_shift( $attachment_ids );
|
||||
$limit_ids = implode( ',', $limit_ids );
|
||||
|
||||
$attachments = $wpdb->get_col( // WPCS: unprepared SQL ok.
|
||||
"
|
||||
SELECT meta_value
|
||||
FROM $wpdb->postmeta
|
||||
WHERE post_id IN ( $limit_ids )
|
||||
AND meta_key = '_imagify_data'"
|
||||
);
|
||||
$wpdb->flush();
|
||||
|
||||
unset( $limit_ids );
|
||||
|
||||
if ( ! $attachments ) {
|
||||
// Uh?!
|
||||
continue;
|
||||
}
|
||||
|
||||
$attachments = array_map( 'maybe_unserialize', $attachments );
|
||||
|
||||
foreach ( $attachments as $attachment_data ) {
|
||||
if ( ! $attachment_data ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( empty( $attachment_data['sizes']['full']['success'] ) ) {
|
||||
/**
|
||||
* - Case where this attachment has multiple '_imagify_status' metas, and is fetched (in the above query) as a "success" while the '_imagify_data' says otherwise.
|
||||
* - Case where this meta has no "full" entry.
|
||||
* Don't ask how it's possible, I don't know ¯\(°_o)/¯
|
||||
*/
|
||||
continue;
|
||||
}
|
||||
|
||||
$original_data = $attachment_data['sizes']['full'];
|
||||
|
||||
++$count;
|
||||
|
||||
// Increment the original sizes.
|
||||
$original_size += ! empty( $original_data['original_size'] ) ? $original_data['original_size'] : 0;
|
||||
$optimized_size += ! empty( $original_data['optimized_size'] ) ? $original_data['optimized_size'] : 0;
|
||||
|
||||
unset( $attachment_data['sizes']['full'], $original_data );
|
||||
|
||||
// Increment the thumbnails sizes.
|
||||
if ( $attachment_data['sizes'] ) {
|
||||
foreach ( $attachment_data['sizes'] as $size_data ) {
|
||||
if ( ! empty( $size_data['success'] ) ) {
|
||||
$original_size += ! empty( $size_data['original_size'] ) ? $size_data['original_size'] : 0;
|
||||
$optimized_size += ! empty( $size_data['optimized_size'] ) ? $size_data['optimized_size'] : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unset( $size_data );
|
||||
}
|
||||
|
||||
unset( $attachments, $attachment_data );
|
||||
} // End while().
|
||||
} // End if().
|
||||
|
||||
$data = array(
|
||||
'count' => $count,
|
||||
'original_size' => $original_size,
|
||||
'optimized_size' => $optimized_size,
|
||||
'percent' => $original_size && $optimized_size ? ceil( ( ( $original_size - $optimized_size ) / $original_size ) * 100 ) : 0,
|
||||
);
|
||||
|
||||
if ( ! empty( $key ) ) {
|
||||
return isset( $data[ $key ] ) ? $data[ $key ] : 0;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the estimated total size of the images not optimized.
|
||||
*
|
||||
* We estimate the total size of the images in the library by getting the latest 250 images and their thumbnails
|
||||
* add up their filesizes, and doing some maths to get the total size.
|
||||
*
|
||||
* @since 1.6
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return int The current estimated total size of images not optimized.
|
||||
*/
|
||||
function imagify_calculate_total_size_images_library() {
|
||||
global $wpdb;
|
||||
|
||||
$mime_types = Imagify_DB::get_mime_types();
|
||||
$statuses = Imagify_DB::get_post_statuses();
|
||||
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
|
||||
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
|
||||
$image_ids = $wpdb->get_col( // WPCS: unprepared SQL ok.
|
||||
"
|
||||
SELECT p.ID
|
||||
FROM $wpdb->posts AS p
|
||||
$nodata_join
|
||||
WHERE p.post_mime_type IN ( $mime_types )
|
||||
AND p.post_type = 'attachment'
|
||||
AND p.post_status IN ( $statuses )
|
||||
$nodata_where
|
||||
LIMIT 250
|
||||
" );
|
||||
|
||||
if ( ! $image_ids ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$count_latest_images = count( $image_ids );
|
||||
$count_total_images = imagify_count_attachments();
|
||||
|
||||
return imagify_calculate_total_image_size( $image_ids, $count_latest_images, $count_total_images );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the estimated average size of the images uploaded per month.
|
||||
*
|
||||
* We estimate the average size of the images uploaded in the library per month by getting the latest 250 images and their thumbnails
|
||||
* for the 3 latest months, add up their filesizes, and doing some maths to get the total average size.
|
||||
*
|
||||
* @since 1.6
|
||||
* @since 1.7 Use wpdb instead of WP_Query.
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return int The current estimated average size of images uploaded per month.
|
||||
*/
|
||||
function imagify_calculate_average_size_images_per_month() {
|
||||
global $wpdb;
|
||||
|
||||
$mime_types = Imagify_DB::get_mime_types();
|
||||
$statuses = Imagify_DB::get_post_statuses();
|
||||
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause( "$wpdb->posts.ID" );
|
||||
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
|
||||
$limit = ' LIMIT 0, 250';
|
||||
$query = "
|
||||
SELECT $wpdb->posts.ID
|
||||
FROM $wpdb->posts
|
||||
$nodata_join
|
||||
WHERE $wpdb->posts.post_mime_type IN ( $mime_types )
|
||||
AND $wpdb->posts.post_type = 'attachment'
|
||||
AND $wpdb->posts.post_status IN ( $statuses )
|
||||
$nodata_where
|
||||
%date_query%";
|
||||
|
||||
// Queries per month.
|
||||
$date_query = new WP_Date_Query( array(
|
||||
array(
|
||||
'before' => 'now',
|
||||
'after' => '1 month ago',
|
||||
),
|
||||
) );
|
||||
|
||||
$partial_images_uploaded_last_month = $wpdb->get_col( str_replace( '%date_query%', $date_query->get_sql(), $query . $limit ) ); // WPCS: unprepared SQL ok.
|
||||
|
||||
$date_query = new WP_Date_Query( array(
|
||||
array(
|
||||
'before' => '1 month ago',
|
||||
'after' => '2 months ago',
|
||||
),
|
||||
) );
|
||||
|
||||
$partial_images_uploaded_two_months_ago = $wpdb->get_col( str_replace( '%date_query%', $date_query->get_sql(), $query . $limit ) ); // WPCS: unprepared SQL ok.
|
||||
|
||||
$date_query = new WP_Date_Query( array(
|
||||
array(
|
||||
'before' => '2 month ago',
|
||||
'after' => '3 months ago',
|
||||
),
|
||||
) );
|
||||
|
||||
$partial_images_uploaded_three_months_ago = $wpdb->get_col( str_replace( '%date_query%', $date_query->get_sql(), $query . $limit ) ); // WPCS: unprepared SQL ok.
|
||||
|
||||
// Total for the 3 months.
|
||||
$partial_images_uploaded_id = array_merge( $partial_images_uploaded_last_month, $partial_images_uploaded_two_months_ago, $partial_images_uploaded_three_months_ago );
|
||||
|
||||
if ( ! $partial_images_uploaded_id ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Total for the 3 months, without the "250" limit.
|
||||
$date_query = new WP_Date_Query( array(
|
||||
array(
|
||||
'before' => 'now',
|
||||
'after' => '3 month ago',
|
||||
),
|
||||
) );
|
||||
|
||||
$images_uploaded_id = $wpdb->get_col( str_replace( '%date_query%', $date_query->get_sql(), $query ) ); // WPCS: unprepared SQL ok.
|
||||
|
||||
if ( ! $images_uploaded_id ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Number of image attachments uploaded for the 3 latest months, limited to 250 per month.
|
||||
$partial_total_images_uploaded = count( $partial_images_uploaded_id );
|
||||
// Total number of image attachments uploaded for the 3 latest months.
|
||||
$total_images_uploaded = count( $images_uploaded_id );
|
||||
|
||||
return imagify_calculate_total_image_size( $partial_images_uploaded_id, $partial_total_images_uploaded, $total_images_uploaded ) / 3;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the estimated total size of images.
|
||||
*
|
||||
* @since 1.6
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $image_ids Array of image IDs.
|
||||
* @param int $partial_total_images The number of image attachments we're doing the calculation with.
|
||||
* @param int $total_images The total number of image attachments.
|
||||
* @return int The estimated total size of images.
|
||||
*/
|
||||
function imagify_calculate_total_image_size( $image_ids, $partial_total_images, $total_images ) {
|
||||
global $wpdb;
|
||||
|
||||
$image_ids = array_filter( array_map( 'absint', $image_ids ) );
|
||||
|
||||
if ( ! $image_ids ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$results = Imagify_DB::get_metas( array(
|
||||
// Get attachments filename.
|
||||
'filenames' => '_wp_attached_file',
|
||||
// Get attachments data.
|
||||
'data' => '_wp_attachment_metadata',
|
||||
// Get Imagify data.
|
||||
'imagify_data' => '_imagify_data',
|
||||
// Get attachments status.
|
||||
'statuses' => '_imagify_status',
|
||||
), $image_ids );
|
||||
|
||||
// Number of image attachments we're doing the calculation with. In case array_filter() removed results.
|
||||
$partial_total_images = count( $image_ids );
|
||||
// Total size of unoptimized size.
|
||||
$partial_size_images = 0;
|
||||
// Total number of thumbnails.
|
||||
$partial_total_intermediate_images = 0;
|
||||
|
||||
$filesystem = imagify_get_filesystem();
|
||||
$is_active_for_network = imagify_is_active_for_network();
|
||||
$disallowed_sizes = get_imagify_option( 'disallowed-sizes' );
|
||||
|
||||
foreach ( $image_ids as $i => $image_id ) {
|
||||
$attachment_status = isset( $results['statuses'][ $image_id ] ) ? $results['statuses'][ $image_id ] : false;
|
||||
|
||||
if ( 'success' === $attachment_status ) {
|
||||
/**
|
||||
* The image files have been optimized.
|
||||
*/
|
||||
// Original size.
|
||||
$partial_size_images += isset( $results['imagify_data'][ $image_id ]['stats']['original_size'] ) ? $results['imagify_data'][ $image_id ]['stats']['original_size'] : 0;
|
||||
// Number of thumbnails.
|
||||
$partial_total_intermediate_images += count( $results['imagify_data'][ $image_id ]['sizes'] );
|
||||
unset(
|
||||
$image_ids[ $i ],
|
||||
$results['filenames'][ $image_id ],
|
||||
$results['data'][ $image_id ],
|
||||
$results['imagify_data'][ $image_id ],
|
||||
$results['statuses'][ $image_id ]
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
/**
|
||||
* The image files are not optimized.
|
||||
*/
|
||||
// Create an array containing all this attachment files.
|
||||
$files = array(
|
||||
'full' => get_imagify_attached_file( $results['filenames'][ $image_id ] ),
|
||||
);
|
||||
|
||||
$sizes = isset( $results['data'][ $image_id ]['sizes'] ) ? $results['data'][ $image_id ]['sizes'] : array();
|
||||
|
||||
if ( $sizes && is_array( $sizes ) ) {
|
||||
if ( ! $is_active_for_network ) {
|
||||
$sizes = array_diff_key( $sizes, $disallowed_sizes );
|
||||
}
|
||||
|
||||
if ( $sizes ) {
|
||||
$full_dirname = $filesystem->dir_path( $files['full'] );
|
||||
|
||||
foreach ( $sizes as $size_key => $size_data ) {
|
||||
$files[ $size_key ] = $full_dirname . '/' . $size_data['file'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow to provide all files size and the number of thumbnails.
|
||||
*
|
||||
* @since 1.6.7
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $size_and_count False by default.
|
||||
* @param int $image_id The attachment ID.
|
||||
* @param array $files An array of file paths with thumbnail sizes as keys.
|
||||
* @param array $image_ids An array of all attachment IDs.
|
||||
* @return bool|array False by default. Provide an array with the keys 'filesize' (containing the total filesize) and 'thumbnails' (containing the number of thumbnails).
|
||||
*/
|
||||
$size_and_count = apply_filters( 'imagify_total_attachment_filesize', false, $image_id, $files, $image_ids );
|
||||
|
||||
if ( is_array( $size_and_count ) ) {
|
||||
$partial_size_images += $size_and_count['filesize'];
|
||||
$partial_total_intermediate_images += $size_and_count['thumbnails'];
|
||||
} else {
|
||||
foreach ( $files as $file ) {
|
||||
if ( $filesystem->exists( $file ) ) {
|
||||
$partial_size_images += $filesystem->size( $file );
|
||||
}
|
||||
}
|
||||
|
||||
unset( $files['full'] );
|
||||
$partial_total_intermediate_images += count( $files );
|
||||
}
|
||||
|
||||
unset(
|
||||
$image_ids[ $i ],
|
||||
$results['filenames'][ $image_id ],
|
||||
$results['data'][ $image_id ],
|
||||
$results['imagify_data'][ $image_id ],
|
||||
$results['statuses'][ $image_id ]
|
||||
);
|
||||
} // End foreach().
|
||||
|
||||
// Number of thumbnails per attachment = Number of thumbnails / Number of attachments.
|
||||
$intermediate_images_per_image = $partial_total_intermediate_images / $partial_total_images;
|
||||
/**
|
||||
* Note: Number of attachments ($partial_total_images) === Number of full sizes.
|
||||
* Average image size = Size of the images / ( Number of full sizes + Number of thumbnails ).
|
||||
* Average image size = Size of the images / Number of images.
|
||||
*/
|
||||
$average_size_images = $partial_size_images / ( $partial_total_images + $partial_total_intermediate_images );
|
||||
/**
|
||||
* Note: Total number of attachments ($total_images) === Total number of full sizes.
|
||||
* Total images size = Average image size * ( Total number of full sizes + ( Number of thumbnails per attachment * Total number of attachments ) ).
|
||||
* Total images size = Average image size * ( Total number of full sizes + Total number of thumbnails ).
|
||||
*/
|
||||
$total_size_images = $average_size_images * ( $total_images + ( $intermediate_images_per_image * $total_images ) );
|
||||
|
||||
return $total_size_images;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all generic stats to be used in the bulk optimization page.
|
||||
*
|
||||
* @since 1.7.1
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $types The folder types. If a folder type is "library", the context should be suffixed after a pipe character. They are passed as array keys.
|
||||
* @param array $args {
|
||||
* Optional. An array of arguments.
|
||||
*
|
||||
* @type bool $fullset True to return the full set of data. False to return only the main data.
|
||||
* @type bool $formatting Some of the data is returned formatted.
|
||||
* }
|
||||
* @return array
|
||||
*/
|
||||
function imagify_get_bulk_stats( $types, $args = array() ) {
|
||||
$types = $types && is_array( $types ) ? $types : array();
|
||||
$args = array_merge( array(
|
||||
'fullset' => false,
|
||||
'formatting' => true,
|
||||
), (array) $args );
|
||||
|
||||
$data = array(
|
||||
// Global chart.
|
||||
'total_attachments' => 0,
|
||||
'unoptimized_attachments' => 0,
|
||||
'optimized_attachments' => 0,
|
||||
'errors_attachments' => 0,
|
||||
// Stats block.
|
||||
'already_optimized_attachments' => 0,
|
||||
'original_human' => 0,
|
||||
'optimized_human' => 0,
|
||||
);
|
||||
|
||||
if ( isset( $types['library|wp'] ) ) {
|
||||
/**
|
||||
* Library.
|
||||
*/
|
||||
$saving_data = imagify_count_saving_data();
|
||||
|
||||
// Global chart.
|
||||
$data['total_attachments'] += imagify_count_attachments();
|
||||
$data['unoptimized_attachments'] += imagify_count_unoptimized_attachments();
|
||||
$data['optimized_attachments'] += imagify_count_optimized_attachments();
|
||||
$data['errors_attachments'] += imagify_count_error_attachments();
|
||||
// Stats block.
|
||||
$data['already_optimized_attachments'] += $saving_data['count'];
|
||||
$data['original_human'] += $saving_data['original_size'];
|
||||
$data['optimized_human'] += $saving_data['optimized_size'];
|
||||
}
|
||||
|
||||
if ( isset( $types['custom-folders|custom-folders'] ) ) {
|
||||
/**
|
||||
* Custom folders.
|
||||
*/
|
||||
// Global chart.
|
||||
$data['total_attachments'] += Imagify_Files_Stats::count_all_files();
|
||||
$data['unoptimized_attachments'] += Imagify_Files_Stats::count_no_status_files();
|
||||
$data['optimized_attachments'] += Imagify_Files_Stats::count_optimized_files();
|
||||
$data['errors_attachments'] += Imagify_Files_Stats::count_error_files();
|
||||
// Stats block.
|
||||
$data['already_optimized_attachments'] += Imagify_Files_Stats::count_success_files();
|
||||
$data['original_human'] += Imagify_Files_Stats::get_original_size();
|
||||
$data['optimized_human'] += Imagify_Files_Stats::get_optimized_size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Full set of data.
|
||||
*/
|
||||
if ( $args['fullset'] ) {
|
||||
// User account.
|
||||
$views = Imagify_Views::get_instance();
|
||||
|
||||
$data['unconsumed_quota'] = $views->get_quota_percent();
|
||||
$data['quota_class'] = $views->get_quota_class();
|
||||
$data['quota_icon'] = $views->get_quota_icon();
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the generic stats used in the bulk optimization page.
|
||||
*
|
||||
* @since 1.7.1
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $data The data.
|
||||
* @param array $types The folder types. They are passed as array keys.
|
||||
* @param array $args {
|
||||
* Optional. An array of arguments.
|
||||
*
|
||||
* @type bool $fullset True to return the full set of data. False to return only the main data.
|
||||
* @type bool $formatting Some of the data is returned formatted.
|
||||
* }
|
||||
*/
|
||||
$data = apply_filters( 'imagify_bulk_stats', $data, $types, $args );
|
||||
|
||||
/**
|
||||
* Percentages.
|
||||
*/
|
||||
if ( $data['total_attachments'] && $data['optimized_attachments'] ) {
|
||||
$data['optimized_attachments_percent'] = round( 100 * $data['optimized_attachments'] / $data['total_attachments'] );
|
||||
} else {
|
||||
$data['optimized_attachments_percent'] = 0;
|
||||
}
|
||||
|
||||
if ( $data['original_human'] && $data['optimized_human'] ) {
|
||||
$data['optimized_percent'] = ceil( 100 - ( 100 * $data['optimized_human'] / $data['original_human'] ) );
|
||||
} else {
|
||||
$data['optimized_percent'] = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formating.
|
||||
*/
|
||||
if ( $args['formatting'] ) {
|
||||
$data['already_optimized_attachments'] = number_format_i18n( $data['already_optimized_attachments'] );
|
||||
$data['original_human'] = imagify_size_format( $data['original_human'], 1 );
|
||||
$data['optimized_human'] = imagify_size_format( $data['optimized_human'], 1 );
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
@@ -1,541 +0,0 @@
|
||||
<?php
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Get the optimization data list for a specific media.
|
||||
*
|
||||
* @since 1.0
|
||||
* @since 1.9 Function signature changed.
|
||||
* @author Jonathan Buttigieg
|
||||
*
|
||||
* @param ProcessInterface $process The optimization process object.
|
||||
* @return string The output to print.
|
||||
*/
|
||||
function get_imagify_attachment_optimization_text( $process ) {
|
||||
if ( ! $process->is_valid() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$is_media_page = Imagify_Views::get_instance()->is_media_page();
|
||||
$is_library_page = Imagify_Views::get_instance()->is_wp_library_page();
|
||||
$output = $is_media_page ? '' : '<ul class="imagify-datas-list" id="imagify_data_sum">';
|
||||
$output_before = $is_media_page ? '' : '<li class="imagify-data-item">';
|
||||
$output_after = $is_media_page ? '<br/>' : '</li>';
|
||||
$reoptimize_link = get_imagify_attachment_reoptimize_link( $process );
|
||||
$reoptimize_link .= get_imagify_attachment_optimize_missing_thumbnails_link( $process );
|
||||
$reoptimize_link .= get_imagify_attachment_generate_nextgen_versions_link( $process );
|
||||
$reoptimize_link .= get_imagify_attachment_delete_nextgen_versions_link( $process );
|
||||
$reoptimize_output = $reoptimize_link ? $reoptimize_link : '';
|
||||
$reoptimize_output_before = '<div class="imagify-datas-actions-links">';
|
||||
$reoptimize_output_after = '</div><!-- .imagify-datas-actions-links -->';
|
||||
$error = get_imagify_attachment_error_text( $process );
|
||||
$media = $process->get_media();
|
||||
|
||||
if ( $error ) {
|
||||
if ( ! $is_media_page && $reoptimize_link && $media->has_backup() ) {
|
||||
$reoptimize_output .= '<span class="attachment-has-backup hidden"></span>';
|
||||
}
|
||||
|
||||
$reoptimize_output = $reoptimize_output_before . $reoptimize_output . $reoptimize_output_after;
|
||||
|
||||
return $is_media_page ? $output_before . $error . $reoptimize_output . $output_after : $error . $reoptimize_output;
|
||||
}
|
||||
|
||||
$data = $process->get_data();
|
||||
$optimized_data = $data->get_optimization_data();
|
||||
$attachment_id = $media->get_id();
|
||||
$optimization_level = imagify_get_optimization_level_label( $data->get_optimization_level() );
|
||||
|
||||
if ( ! $is_media_page ) {
|
||||
$output .= $output_before . '<span class="data">' . __( 'New Filesize:', 'imagify' ) . '</span> <strong class="big">' . $data->get_optimized_size() . '</strong>' . $output_after;
|
||||
}
|
||||
|
||||
if ( key_exists( 'message', $optimized_data ) && $optimized_data['message'] ) {
|
||||
$output .= $output_before . '<span class="data">' . __( 'Convert:', 'imagify' ) . '</span> <strong class="big">' . $optimized_data['message'] . '</strong>' . $output_after;
|
||||
}
|
||||
|
||||
$chart = '';
|
||||
|
||||
if ( ! $is_media_page ) {
|
||||
if ( ! $is_library_page ) {
|
||||
// No need to print this on the library page, the event whould be triggered before the handler is attached (the JS file is loaded in the footer).
|
||||
$chart = '<script type="text/javascript">jQuery( window ).trigger( "canvasprinted.imagify", [ ".imagify-consumption-chart-' . $attachment_id . '" ] ); </script>';
|
||||
}
|
||||
|
||||
$chart = '<span class="imagify-chart">
|
||||
<span class="imagify-chart-container">
|
||||
<canvas class="imagify-consumption-chart imagify-consumption-chart-' . $attachment_id . '" width="15" height="15"></canvas>
|
||||
' . $chart . '
|
||||
</span>
|
||||
</span>';
|
||||
}
|
||||
|
||||
$output .= $output_before;
|
||||
$output .= '<span class="data">' . __( 'Original Saving:', 'imagify' ) . '</span> ';
|
||||
$output .= '<strong>' . $chart . '<span class="imagify-chart-value">' . $data->get_saving_percent() . '</span>%</strong>';
|
||||
$output .= $output_after;
|
||||
|
||||
// More details section.
|
||||
if ( ! $is_media_page ) {
|
||||
// New list.
|
||||
$output .= '</ul>';
|
||||
$output .= '<p class="imagify-datas-more-action">';
|
||||
$output .= '<a href="#imagify-view-details-' . $attachment_id . '" data-close="' . __( 'Close details', 'imagify' ) . '" data-open="' . __( 'View details', 'imagify' ) . '">';
|
||||
$output .= '<span class="the-text">' . __( 'View details', 'imagify' ) . '</span>';
|
||||
$output .= '<span class="dashicons dashicons-arrow-down-alt2"></span>';
|
||||
$output .= '</a>';
|
||||
$output .= '</p>';
|
||||
$output .= '<ul id="imagify-view-details-' . $attachment_id . '" class="imagify-datas-list imagify-datas-details">';
|
||||
|
||||
// Not in metabox.
|
||||
$output .= $output_before . '<span class="data">' . __( 'Original Filesize:', 'imagify' ) . '</span> <strong class="original">' . $data->get_original_size() . '</strong>' . $output_after;
|
||||
}
|
||||
|
||||
$output .= $output_before . '<span class="data">' . __( 'Level:', 'imagify' ) . '</span> <strong>' . $optimization_level . '</strong>' . $output_after;
|
||||
|
||||
if ( $media->is_image() ) {
|
||||
$has_nextgen = $process->has_next_gen() ? __( 'Yes', 'imagify' ) : __( 'No', 'imagify' );
|
||||
|
||||
if ( $process->has_next_gen() ) {
|
||||
$has_nextgen = $process->is_full_next_gen() ? __( 'Yes', 'imagify' ) : __( 'Partially', 'imagify' );
|
||||
}
|
||||
$output .= $output_before . '<span class="data">' . __( 'Next-Gen generated:', 'imagify' ) . '</span> <strong class="big">' . esc_html( $has_nextgen ) . '</strong>' . $output_after;
|
||||
|
||||
$total_optimized_thumbnails = $data->get_optimized_sizes_count();
|
||||
|
||||
if ( $total_optimized_thumbnails ) {
|
||||
$output .= $output_before . '<span class="data">' . __( 'Thumbnails Optimized:', 'imagify' ) . '</span> <strong>' . $total_optimized_thumbnails . '</strong>' . $output_after;
|
||||
$output .= $output_before . '<span class="data">' . __( 'Overall Saving:', 'imagify' ) . '</span> <strong>' . $data->get_overall_saving_percent() . '%</strong>' . $output_after;
|
||||
}
|
||||
}
|
||||
|
||||
// End of list.
|
||||
$output .= $is_media_page ? '' : '</ul>';
|
||||
|
||||
// Actions section.
|
||||
$output .= $is_media_page ? $output_before : '';
|
||||
$output .= $reoptimize_output_before;
|
||||
$output .= $reoptimize_output;
|
||||
|
||||
if ( $media->has_backup() ) {
|
||||
$url = get_imagify_admin_url( 'restore', [
|
||||
'attachment_id' => $attachment_id,
|
||||
'context' => $media->get_context(),
|
||||
] );
|
||||
|
||||
$output .= Imagify_Views::get_instance()->get_template( 'button/restore', [
|
||||
'url' => $url,
|
||||
'atts' => [
|
||||
'class' => $is_media_page ? '' : null,
|
||||
],
|
||||
] );
|
||||
|
||||
if ( ! $is_library_page ) {
|
||||
$output .= '<input id="imagify-original-src" type="hidden" value="' . esc_url( $media->get_backup_url() ) . '">';
|
||||
$output .= '<input id="imagify-original-size" type="hidden" value="' . $data->get_original_size() . '">';
|
||||
$output .= '<input id="imagify-full-src" type="hidden" value="' . esc_url( $media->get_fullsize_url() ) . '">';
|
||||
|
||||
if ( $media->is_image() ) {
|
||||
$dimensions = $media->get_dimensions();
|
||||
|
||||
$output .= '<input id="imagify-full-width" type="hidden" value="' . $dimensions['width'] . '">';
|
||||
$output .= '<input id="imagify-full-height" type="hidden" value="' . $dimensions['height'] . '">';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$output .= $reoptimize_output_after;
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error message for a specific attachment.
|
||||
*
|
||||
* @since 1.0
|
||||
* @since 1.9 Function signature changed.
|
||||
* @author Jonathan Buttigieg
|
||||
*
|
||||
* @param ProcessInterface $process The optimization process object.
|
||||
* @return string The output to print.
|
||||
*/
|
||||
function get_imagify_attachment_error_text( $process ) {
|
||||
if ( ! $process->is_valid() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$data = $process->get_data()->get_optimization_data();
|
||||
|
||||
if ( ! isset( $data['sizes']['full']['success'] ) || $data['sizes']['full']['success'] ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$class = 'button';
|
||||
$media = $process->get_media();
|
||||
$url = get_imagify_admin_url( 'optimize', [
|
||||
'attachment_id' => $media->get_id(),
|
||||
'context' => $media->get_context(),
|
||||
] );
|
||||
|
||||
if ( ! Imagify_Views::get_instance()->is_media_page() ) {
|
||||
$class .= ' button-imagify-optimize';
|
||||
}
|
||||
|
||||
return Imagify_Views::get_instance()->get_template( 'button/retry-optimize', [
|
||||
'url' => $url,
|
||||
'error' => $data['sizes']['full']['error'],
|
||||
'atts' => [
|
||||
'class' => $class,
|
||||
],
|
||||
] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the re-optimize link for a specific attachment.
|
||||
*
|
||||
* @since 1.0
|
||||
* @since 1.9 Function signature changed.
|
||||
* @author Jonathan Buttigieg
|
||||
*
|
||||
* @param ProcessInterface $process The optimization process object.
|
||||
* @return string The output to print.
|
||||
*/
|
||||
function get_imagify_attachment_reoptimize_link( $process ) {
|
||||
if ( ! $process->is_valid() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$data = $process->get_data();
|
||||
|
||||
if ( ! $data->get_optimization_status() ) {
|
||||
// Not optimized yet.
|
||||
return '';
|
||||
}
|
||||
|
||||
// Stop the process if the API key isn't valid.
|
||||
if ( ! Imagify_Requirements::is_api_key_valid() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$is_already_optimized = $data->is_already_optimized();
|
||||
$media = $process->get_media();
|
||||
$can_reoptimize = $is_already_optimized || $media->has_backup();
|
||||
|
||||
// Don't display anything if there is no backup or the image has been optimized.
|
||||
if ( ! $can_reoptimize ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$output = '';
|
||||
$views = Imagify_Views::get_instance();
|
||||
$media_level = $data->get_optimization_level();
|
||||
$data = [];
|
||||
$url_args = [
|
||||
'attachment_id' => $media->get_id(),
|
||||
'context' => $media->get_context(),
|
||||
];
|
||||
|
||||
if ( Imagify_Views::get_instance()->is_media_page() ) {
|
||||
$data['atts'] = [
|
||||
'class' => '',
|
||||
];
|
||||
}
|
||||
|
||||
if ( $media_level < 1 ) {
|
||||
$url_args['optimization_level'] = 2;
|
||||
$data['optimization_level'] = 2;
|
||||
$data['url'] = get_imagify_admin_url( 'manual-reoptimize', $url_args );
|
||||
|
||||
$output .= $views->get_template( 'button/re-optimize', $data );
|
||||
} elseif ( $media_level > 0 ) {
|
||||
$url_args['optimization_level'] = 0;
|
||||
$data['optimization_level'] = 0;
|
||||
$data['url'] = get_imagify_admin_url( 'manual-reoptimize', $url_args );
|
||||
|
||||
$output .= $views->get_template( 'button/re-optimize', $data );
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the link to optimize missing thumbnail sizes for a specific attachment.
|
||||
*
|
||||
* @since 1.6.10
|
||||
* @since 1.9 Function signature changed.
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param ProcessInterface $process The optimization process object.
|
||||
* @return string The output to print.
|
||||
*/
|
||||
function get_imagify_attachment_optimize_missing_thumbnails_link( $process ) {
|
||||
if ( ! $process->is_valid() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$media = $process->get_media();
|
||||
|
||||
if ( ! $media->is_image() || ! Imagify_Requirements::is_api_key_valid() || ! $media->has_backup() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$context = $media->get_context();
|
||||
|
||||
/**
|
||||
* Allow to not display the "Optimize missing thumbnails" link.
|
||||
*
|
||||
* @since 1.6.10
|
||||
* @since 1.9 The $attachment object is replaced by a $process object.
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $display True to display the link. False to not display it.
|
||||
* @param ProcessInterface $process The optimization process object.
|
||||
* @param string $context The context.
|
||||
*/
|
||||
$display = apply_filters( 'imagify_display_missing_thumbnails_link', true, $process, $context );
|
||||
|
||||
// Stop the process if the filter is false.
|
||||
if ( ! $display ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$missing_sizes = $process->get_missing_sizes();
|
||||
|
||||
if ( ! $missing_sizes || is_wp_error( $missing_sizes ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$url = get_imagify_admin_url( 'optimize-missing-sizes', [
|
||||
'attachment_id' => $media->get_id(),
|
||||
'context' => $context,
|
||||
] );
|
||||
|
||||
return Imagify_Views::get_instance()->get_template( 'button/optimize-missing-sizes', [
|
||||
'url' => $url,
|
||||
'count' => count( $missing_sizes ),
|
||||
] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the link to generate next-gen versions if they are missing.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param ProcessInterface $process The optimization process object.
|
||||
*
|
||||
* @return string The output to print.
|
||||
*/
|
||||
function get_imagify_attachment_generate_nextgen_versions_link( $process ) {
|
||||
if ( ! $process->is_valid() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$formats = imagify_nextgen_images_formats();
|
||||
|
||||
if ( empty( $formats ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$media = $process->get_media();
|
||||
|
||||
if ( ! $media->is_image() || ! Imagify_Requirements::is_api_key_valid() || ! $media->has_backup() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$format = get_imagify_option( 'optimization_format' );
|
||||
|
||||
if (
|
||||
'avif' === $format
|
||||
&&
|
||||
'image/avif' === $media->get_mime_type()
|
||||
) {
|
||||
return '';
|
||||
} elseif ( 'image/webp' === $media->get_mime_type() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$data = $process->get_data();
|
||||
|
||||
if ( ! $data->is_optimized() && ! $data->is_already_optimized() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ( $process->has_next_gen() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$context = $media->get_context();
|
||||
|
||||
$display = apply_filters_deprecated( 'imagify_display_generate_webp_versions_link', array( true, $process, $context ), '2.2', 'imagify_display_generate_next_gen_versions_link' );
|
||||
|
||||
/**
|
||||
* Allow to not display the "Generate next-gen versions" link.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $display True to display the link. False to not display it.
|
||||
* @param ProcessInterface $process The optimization process object.
|
||||
* @param string $context The context.
|
||||
*/
|
||||
$display = apply_filters( 'imagify_display_generate_next_gen_versions_link', $display, $process, $context );
|
||||
|
||||
// Stop the process if the filter is false.
|
||||
if ( ! $display ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$url = get_imagify_admin_url( 'generate-nextgen-versions', [
|
||||
'attachment_id' => $media->get_id(),
|
||||
'context' => $context,
|
||||
] );
|
||||
|
||||
$output = Imagify_Views::get_instance()->get_template( 'button/generate-webp', [
|
||||
'url' => $url,
|
||||
] );
|
||||
|
||||
return $output . '<br/>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the link to delete next-gen versions when the status is "already_optimized".
|
||||
*
|
||||
* @since 1.9.6
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param ProcessInterface $process The optimization process object.
|
||||
* @return string The output to print.
|
||||
*/
|
||||
function get_imagify_attachment_delete_nextgen_versions_link( $process ) {
|
||||
if ( ! $process->is_valid() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$media = $process->get_media();
|
||||
$context = $media->get_context();
|
||||
$media_id = $media->get_id();
|
||||
|
||||
if ( ! imagify_get_context( $context )->current_user_can( 'manual-restore', $media_id ) ) {
|
||||
imagify_die();
|
||||
}
|
||||
|
||||
$data = $process->get_data();
|
||||
|
||||
if ( ! $data->is_already_optimized() || ! $process->has_next_gen() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$class = '';
|
||||
$url = get_imagify_admin_url( 'delete-nextgen-versions', [
|
||||
'attachment_id' => $media_id,
|
||||
'context' => $context,
|
||||
] );
|
||||
|
||||
if ( ! Imagify_Views::get_instance()->is_media_page() ) {
|
||||
$class .= 'button-imagify-delete-webp';
|
||||
}
|
||||
|
||||
return Imagify_Views::get_instance()->get_template( 'button/delete-webp', [
|
||||
'url' => $url,
|
||||
'atts' => [
|
||||
'class' => $class,
|
||||
],
|
||||
] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all data to diplay for a specific media.
|
||||
*
|
||||
* @since 1.2
|
||||
* @since 1.9 Function signature changed.
|
||||
* @author Jonathan Buttigieg
|
||||
*
|
||||
* @param ProcessInterface $process The optimization process object.
|
||||
* @param bool $with_container Set to false to not return the HTML container.
|
||||
* @return string The output to print.
|
||||
*/
|
||||
function get_imagify_media_column_content( $process, $with_container = true ) {
|
||||
if ( ! $process->is_valid() ) {
|
||||
return __( 'This media is not valid.', 'imagify' );
|
||||
}
|
||||
|
||||
if ( ! $process->current_user_can( 'manual-optimize' ) ) {
|
||||
return __( 'You are not allowed to optimize this file.', 'imagify' );
|
||||
}
|
||||
|
||||
$media = $process->get_media();
|
||||
|
||||
// Check if the media is supported.
|
||||
if ( ! $media->is_supported() ) {
|
||||
return __( 'This media is not supported.', 'imagify' );
|
||||
}
|
||||
|
||||
// Check if the media has the required WP data.
|
||||
if ( ! $media->has_required_media_data() ) {
|
||||
return __( 'This media lacks the required metadata and cannot be optimized.', 'imagify' );
|
||||
}
|
||||
|
||||
$data = $process->get_data();
|
||||
|
||||
// Check if the API key is valid.
|
||||
if ( ! Imagify_Requirements::is_api_key_valid() && ! $data->is_optimized() ) {
|
||||
$output = __( 'Invalid API key', 'imagify' );
|
||||
$output .= '<br/>';
|
||||
$output .= '<a href="' . esc_url( get_imagify_admin_url() ) . '">' . __( 'Check your Settings', 'imagify' ) . '</a>';
|
||||
return $output;
|
||||
}
|
||||
|
||||
$media_id = $media->get_id();
|
||||
$context = $media->get_context();
|
||||
$views = Imagify_Views::get_instance();
|
||||
$is_locked = $process->is_locked();
|
||||
|
||||
if ( $is_locked ) {
|
||||
switch ( $is_locked ) {
|
||||
case 'optimizing':
|
||||
$lock_label = __( 'Optimizing...', 'imagify' );
|
||||
break;
|
||||
case 'restoring':
|
||||
$lock_label = __( 'Restoring...', 'imagify' );
|
||||
break;
|
||||
default:
|
||||
$lock_label = __( 'Processing...', 'imagify' );
|
||||
}
|
||||
|
||||
if ( ! $with_container ) {
|
||||
return $views->get_template( 'button/processing', [ 'label' => $lock_label ] );
|
||||
}
|
||||
|
||||
return $views->get_template( 'container/data-actions', [
|
||||
'media_id' => $media_id,
|
||||
'context' => $context,
|
||||
'content' => $views->get_template( 'button/processing', [ 'label' => $lock_label ] ),
|
||||
] );
|
||||
}
|
||||
|
||||
// Check if the image was optimized.
|
||||
if ( ! $data->get_optimization_status() ) {
|
||||
$output = Imagify_Views::get_instance()->get_template( 'button/optimize', [
|
||||
'url' => get_imagify_admin_url( 'manual-optimize', [
|
||||
'attachment_id' => $media_id,
|
||||
'context' => $context,
|
||||
] ),
|
||||
] );
|
||||
|
||||
if ( $media->has_backup() ) {
|
||||
$output .= '<span class="attachment-has-backup hidden"></span>';
|
||||
}
|
||||
} else {
|
||||
$output = get_imagify_attachment_optimization_text( $process );
|
||||
}
|
||||
|
||||
if ( ! $with_container ) {
|
||||
return $output;
|
||||
}
|
||||
|
||||
return $views->get_template( 'container/data-actions', [
|
||||
'media_id' => $media_id,
|
||||
'context' => $context,
|
||||
'content' => $output,
|
||||
] );
|
||||
}
|
||||
@@ -1,437 +0,0 @@
|
||||
<?php
|
||||
use Imagify\Notices\Notices;
|
||||
use Imagify\User\User;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Tell if the current screen is what we're looking for.
|
||||
*
|
||||
* @since 1.6.10
|
||||
*
|
||||
* @param string $identifier The screen "name".
|
||||
* @return bool
|
||||
*/
|
||||
function imagify_is_screen( $identifier ) {
|
||||
global $post_id;
|
||||
|
||||
if ( ! $identifier ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$current_screen = get_current_screen();
|
||||
|
||||
if ( ! $current_screen || ! $current_screen->in_admin() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ( $identifier ) {
|
||||
case 'imagify-settings':
|
||||
// Imagify Settings or Imagify Network Settings.
|
||||
$slug = Imagify_Views::get_instance()->get_settings_page_slug();
|
||||
return 'settings_page_' . $slug === $current_screen->id || $slug . '_page_' . $slug . '-network' === $current_screen->id || 'toplevel_page_' . $slug . '-network' === $current_screen->id;
|
||||
|
||||
case 'imagify-network-settings':
|
||||
// Imagify Network Settings.
|
||||
$slug = Imagify_Views::get_instance()->get_settings_page_slug();
|
||||
return $slug . '_page_' . $slug . '-network' === $current_screen->id || 'toplevel_page_' . $slug . '-network' === $current_screen->id;
|
||||
|
||||
case 'library':
|
||||
// Media Library.
|
||||
return 'upload' === $current_screen->id;
|
||||
|
||||
case 'upload':
|
||||
// Upload New Media.
|
||||
return 'media' === $current_screen->id;
|
||||
|
||||
case 'post':
|
||||
// Edit Post, Page, Attachment, etc.
|
||||
return 'post' === $current_screen->base;
|
||||
|
||||
case 'attachment':
|
||||
case 'post-attachment':
|
||||
// Edit Attachment.
|
||||
return 'post' === $current_screen->base && 'attachment' === $current_screen->id && $post_id && imagify_is_attachment_mime_type_supported( $post_id );
|
||||
|
||||
case 'bulk':
|
||||
case 'bulk-optimization':
|
||||
// Bulk Optimization (any).
|
||||
$slug = Imagify_Views::get_instance()->get_bulk_page_slug();
|
||||
return 'toplevel_page_' . $slug . '-network' === $current_screen->id || 'media_page_' . $slug === $current_screen->id;
|
||||
|
||||
case 'files-bulk-optimization':
|
||||
// Bulk Optimization (custom folders).
|
||||
$slug = Imagify_Views::get_instance()->get_bulk_page_slug();
|
||||
return 'toplevel_page_' . $slug . '-network' === $current_screen->id || 'media_page_' . $slug === $current_screen->id;
|
||||
|
||||
case 'files':
|
||||
case 'files-list':
|
||||
// "Custom folders" files list.
|
||||
$slug = Imagify_Views::get_instance()->get_files_page_slug();
|
||||
return 'imagify_page_' . $slug . '-network' === $current_screen->id || 'media_page_' . $slug === $current_screen->id;
|
||||
|
||||
case 'media-modal':
|
||||
// Media modal.
|
||||
return did_action( 'wp_enqueue_media' ) || doing_filter( 'wp_enqueue_media' );
|
||||
|
||||
default:
|
||||
return $identifier === $current_screen->id;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL related to specific admin page or action.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $action An action.
|
||||
* @param array|string $arg An array of arguments. It can contain an attachment ID and/or a context.
|
||||
* @return string The URL of the specific admin page or action.
|
||||
*/
|
||||
function get_imagify_admin_url( $action = 'settings', $arg = [] ) {
|
||||
if ( is_array( $arg ) ) {
|
||||
$id = isset( $arg['attachment_id'] ) ? $arg['attachment_id'] : 0;
|
||||
$context = isset( $arg['context'] ) ? $arg['context'] : 'wp';
|
||||
$level = isset( $arg['optimization_level'] ) ? $arg['optimization_level'] : '';
|
||||
}
|
||||
|
||||
switch ( $action ) {
|
||||
case 'manual-reoptimize':
|
||||
case 'manual-override-upload': // Deprecated.
|
||||
return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_manual_reoptimize&attachment_id=' . $id . '&optimization_level=' . $level . '&context=' . $context ), 'imagify-manual-reoptimize-' . $id . '-' . $context );
|
||||
|
||||
case 'optimize-missing-sizes':
|
||||
return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_optimize_missing_sizes&attachment_id=' . $id . '&context=' . $context ), 'imagify-optimize-missing-sizes-' . $id . '-' . $context );
|
||||
|
||||
case 'generate-nextgen-versions':
|
||||
return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_generate_nextgen_versions&attachment_id=' . $id . '&context=' . $context ), 'imagify-generate-nextgen-versions-' . $id . '-' . $context );
|
||||
|
||||
case 'delete-nextgen-versions':
|
||||
return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_delete_nextgen_versions&attachment_id=' . $id . '&context=' . $context ), 'imagify-delete-nextgen-versions-' . $id . '-' . $context );
|
||||
|
||||
case 'optimize':
|
||||
case 'manual-upload': // Deprecated.
|
||||
case 'manual-optimize':
|
||||
return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_manual_optimize&attachment_id=' . $id . '&context=' . $context ), 'imagify-optimize-' . $id . '-' . $context );
|
||||
|
||||
case 'restore':
|
||||
case 'restore-upload': // Deprecated.
|
||||
return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_restore&attachment_id=' . $id . '&context=' . $context ), 'imagify-restore-' . $id . '-' . $context );
|
||||
|
||||
case 'optimize-file':
|
||||
case 'restore-file':
|
||||
case 'refresh-file-modified':
|
||||
$action = 'imagify_' . str_replace( '-', '_', $action );
|
||||
return wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . '&id=' . $id ), $action );
|
||||
|
||||
case 'reoptimize-file':
|
||||
$action = 'imagify_' . str_replace( '-', '_', $action );
|
||||
return wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . '&id=' . $id . '&level=' . $level ), $action );
|
||||
|
||||
case 'get-files-tree':
|
||||
return wp_nonce_url( admin_url( 'admin-ajax.php?action=imagify_get_files_tree' ), 'get-files-tree' );
|
||||
|
||||
case 'bulk-optimization':
|
||||
return admin_url( 'upload.php?page=' . Imagify_Views::get_instance()->get_bulk_page_slug() );
|
||||
|
||||
case 'files-bulk-optimization':
|
||||
$page = '?page=' . Imagify_Views::get_instance()->get_bulk_page_slug();
|
||||
return imagify_is_active_for_network() ? network_admin_url( 'admin.php' . $page ) : admin_url( 'upload.php' . $page );
|
||||
|
||||
case 'files-list':
|
||||
$page = '?page=' . Imagify_Views::get_instance()->get_files_page_slug();
|
||||
return imagify_is_active_for_network() ? network_admin_url( 'admin.php' . $page ) : admin_url( 'upload.php' . $page );
|
||||
|
||||
case 'folder-errors':
|
||||
switch ( $arg ) {
|
||||
case 'wp':
|
||||
return add_query_arg( array(
|
||||
'mode' => 'list',
|
||||
'imagify-status' => 'errors',
|
||||
), admin_url( 'upload.php' ) );
|
||||
|
||||
case 'custom-folders':
|
||||
return add_query_arg( array(
|
||||
'status-filter' => 'errors',
|
||||
), get_imagify_admin_url( 'files-list' ) );
|
||||
}
|
||||
/**
|
||||
* Provide a URL to a page displaying optimization errors for the given context.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param string $url The URL.
|
||||
* @param string $arg The context.
|
||||
*/
|
||||
return apply_filters( 'imagify_optimization_errors_url', '', $arg );
|
||||
|
||||
case 'dismiss-notice':
|
||||
return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_dismiss_notice¬ice=' . $arg ), Notices::DISMISS_NONCE_ACTION );
|
||||
|
||||
default:
|
||||
$page = '?page=' . Imagify_Views::get_instance()->get_settings_page_slug();
|
||||
return imagify_is_active_for_network() ? network_admin_url( 'admin.php' . $page ) : admin_url( 'options-general.php' . $page );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get maximal width and height from all thumbnails.
|
||||
*
|
||||
* @since 1.1
|
||||
*
|
||||
* @return array An array containing the max width and height.
|
||||
*/
|
||||
function get_imagify_max_intermediate_image_size() {
|
||||
$width = 0;
|
||||
$height = 0;
|
||||
$limit = 9999;
|
||||
|
||||
foreach ( get_imagify_thumbnail_sizes() as $_size ) {
|
||||
if ( $_size['width'] > $width && $_size['width'] < $limit ) {
|
||||
$width = $_size['width'];
|
||||
}
|
||||
|
||||
if ( $_size['height'] > $height && $_size['height'] < $limit ) {
|
||||
$height = $_size['height'];
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'width' => $width,
|
||||
'height' => $height,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple helper to get the WP Rocket's site URL.
|
||||
* The URL is localized and contains some utm_*** parameters.
|
||||
*
|
||||
* @since 1.6.8
|
||||
* @since 1.6.9 Added $path and $query parameters.
|
||||
*
|
||||
* @param string $path A path to add to the URL (URI). Not in use yet.
|
||||
* @param array $query An array of query arguments (utm_*).
|
||||
* @return string The URL.
|
||||
*/
|
||||
function imagify_get_wp_rocket_url( $path = false, $query = array() ) {
|
||||
$wprocket_url = 'https://wp-rocket.me/';
|
||||
|
||||
// Current lang.
|
||||
$lang = imagify_get_current_lang_in( array( 'de', 'es', 'fr', 'it' ) );
|
||||
|
||||
if ( 'en' !== $lang ) {
|
||||
$wprocket_url .= $lang . '/';
|
||||
}
|
||||
|
||||
// URI.
|
||||
$paths = array(
|
||||
'pricing' => array(
|
||||
'de' => 'preise',
|
||||
'en' => 'pricing',
|
||||
'es' => 'precios',
|
||||
'fr' => 'offres',
|
||||
'it' => 'offerte',
|
||||
),
|
||||
);
|
||||
|
||||
if ( $path ) {
|
||||
$path = trim( $path, '/' );
|
||||
|
||||
if ( isset( $paths[ $path ] ) ) {
|
||||
$wprocket_url .= $paths[ $path ][ $lang ] . '/';
|
||||
} else {
|
||||
$wprocket_url .= $path . '/';
|
||||
}
|
||||
}
|
||||
|
||||
// Query args.
|
||||
$query = array_merge( array(
|
||||
'utm_source' => 'imagify-coupon',
|
||||
'utm_medium' => 'plugin',
|
||||
'utm_campaign' => 'imagify',
|
||||
), $query );
|
||||
|
||||
return add_query_arg( $query, $wprocket_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for nonce.
|
||||
*
|
||||
* @since 1.9.11 Return true when nonce is good.
|
||||
* @since 1.6.10
|
||||
*
|
||||
* @param string $action Action nonce.
|
||||
* @param string|bool $query_arg Optional. Key to check for the nonce in `$_REQUEST`. If false, `$_REQUEST` values will be evaluated for '_ajax_nonce', and '_wpnonce' (in that order). Default false.
|
||||
*
|
||||
* @return bool True if the nonce is good; otherwise terminates.
|
||||
*/
|
||||
function imagify_check_nonce( $action, $query_arg = false ) {
|
||||
if ( ! check_ajax_referer( $action, $query_arg, false ) ) {
|
||||
imagify_die();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Die Today.
|
||||
*
|
||||
* @since 1.6.10
|
||||
*
|
||||
* @param string $message A message to display.
|
||||
*/
|
||||
function imagify_die( $message = null ) {
|
||||
if ( ! isset( $message ) ) {
|
||||
/* translators: This sentense already exists in WordPress. */
|
||||
$message = __( 'Sorry, you are not allowed to do that.', 'imagify' );
|
||||
} elseif ( is_wp_error( $message ) ) {
|
||||
$message = imagify_translate_api_message( $message->get_error_message() );
|
||||
}
|
||||
|
||||
if ( is_array( $message ) ) {
|
||||
if ( ! empty( $message['error'] ) ) {
|
||||
$message['error'] = imagify_translate_api_message( $message['error'] );
|
||||
} elseif ( ! empty( $message['detail'] ) ) {
|
||||
$message['detail'] = imagify_translate_api_message( $message['detail'] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( wp_doing_ajax() ) {
|
||||
wp_send_json_error( $message );
|
||||
}
|
||||
|
||||
if ( is_array( $message ) ) {
|
||||
if ( ! empty( $message['error'] ) ) {
|
||||
$message = $message['error'];
|
||||
} elseif ( ! empty( $message['detail'] ) ) {
|
||||
$message = $message['detail'];
|
||||
} else {
|
||||
$message = reset( $message );
|
||||
}
|
||||
}
|
||||
|
||||
if ( wp_get_referer() ) {
|
||||
$message .= '</p><p>';
|
||||
$message .= sprintf( '<a href="%s">%s</a>',
|
||||
esc_url( remove_query_arg( 'updated', wp_get_referer() ) ),
|
||||
/* translators: This sentense already exists in WordPress. */
|
||||
__( 'Go back', 'imagify' )
|
||||
);
|
||||
}
|
||||
|
||||
/* translators: %s is the plugin name. */
|
||||
wp_die( $message, sprintf( __( '%s Failure Notice', 'imagify' ), 'Imagify' ), 403 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect if not an ajax request.
|
||||
*
|
||||
* @since 1.6.10
|
||||
*
|
||||
* @param string $message A message to display in an admin notice once redirected.
|
||||
* @param array|string $args_or_url An array of query args to add to the redirection URL. If a string, the complete URL.
|
||||
*/
|
||||
function imagify_maybe_redirect( $message = false, $args_or_url = array() ) {
|
||||
if ( wp_doing_ajax() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $args_or_url && is_array( $args_or_url ) ) {
|
||||
$redirect = add_query_arg( $args_or_url, wp_get_referer() );
|
||||
} elseif ( $args_or_url && is_string( $args_or_url ) ) {
|
||||
$redirect = $args_or_url;
|
||||
} else {
|
||||
$redirect = wp_get_referer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the URL to redirect to.
|
||||
*
|
||||
* @since 1.6.10
|
||||
*
|
||||
* @param string $redirect The URL to redirect to.
|
||||
*/
|
||||
$redirect = apply_filters( 'imagify_redirect_to', $redirect );
|
||||
|
||||
if ( $message ) {
|
||||
if ( is_multisite() && strpos( $redirect, network_admin_url( '/' ) ) === 0 ) {
|
||||
Notices::get_instance()->add_network_temporary_notice( $message );
|
||||
} else {
|
||||
Notices::get_instance()->add_site_temporary_notice( $message );
|
||||
}
|
||||
}
|
||||
|
||||
wp_safe_redirect( esc_url_raw( $redirect ) );
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached Imagify user data.
|
||||
* This is usefull to prevent triggering an HTTP request to our server on every page load, but it can be used only where the data doesn't need to be in real time.
|
||||
*
|
||||
* @since 1.7
|
||||
*
|
||||
* @return object|bool An object on success. False otherwise.
|
||||
*/
|
||||
function imagify_get_cached_user() {
|
||||
if ( ! Imagify_Requirements::is_api_key_valid() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( imagify_is_active_for_network() ) {
|
||||
$user = get_site_transient( 'imagify_user' );
|
||||
} else {
|
||||
$user = get_transient( 'imagify_user' );
|
||||
}
|
||||
|
||||
return is_object( $user ) ? $user : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache Imagify user data for 5 minutes.
|
||||
* Runs every methods to store the results. Also stores formatted data like the quota and the next update date.
|
||||
*
|
||||
* @since 1.7
|
||||
*
|
||||
* @return object|bool An object on success. False otherwise.
|
||||
*/
|
||||
function imagify_cache_user() {
|
||||
if ( ! Imagify_Requirements::is_api_key_valid() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$user = new User();
|
||||
$data = (object) get_object_vars( $user );
|
||||
$methods = get_class_methods( $user );
|
||||
|
||||
foreach ( $methods as $method ) {
|
||||
if ( '__construct' !== $method ) {
|
||||
$data->$method = $user->$method();
|
||||
}
|
||||
}
|
||||
|
||||
$data->quota_formatted = imagify_size_format( $user->quota * pow( 1024, 2 ) );
|
||||
$data->next_date_update_formatted = date_i18n( get_option( 'date_format' ), strtotime( $user->next_date_update ) );
|
||||
|
||||
if ( imagify_is_active_for_network() ) {
|
||||
set_site_transient( 'imagify_user', $data, 5 * MINUTE_IN_SECONDS );
|
||||
} else {
|
||||
set_transient( 'imagify_user', $data, 5 * MINUTE_IN_SECONDS );
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete cached Imagify user data.
|
||||
*
|
||||
* @since 1.9.5
|
||||
*/
|
||||
function imagify_delete_cached_user() {
|
||||
if ( imagify_is_active_for_network() ) {
|
||||
delete_site_transient( 'imagify_user' );
|
||||
} else {
|
||||
delete_transient( 'imagify_user' );
|
||||
}
|
||||
}
|
||||
@@ -1,315 +0,0 @@
|
||||
<?php
|
||||
use Imagify\CLI\CommandInterface;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Returns the main instance of the Imagify class.
|
||||
*
|
||||
* @since 1.6.5
|
||||
*
|
||||
* @return object The Imagify instance.
|
||||
*/
|
||||
function imagify() {
|
||||
return Imagify::get_instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new user on Imagify.
|
||||
*
|
||||
* @param array $data All user data.
|
||||
* @return object
|
||||
*/
|
||||
function add_imagify_user( $data ) {
|
||||
return imagify()->create_user( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update your Imagify account.
|
||||
*
|
||||
* @param string $data All user data.
|
||||
* @return object
|
||||
*/
|
||||
function update_imagify_user( $data ) {
|
||||
return imagify()->update_user( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get your Imagify account infos.
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
function get_imagify_user() {
|
||||
return imagify()->get_user();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Imagify API version.
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
function get_imagify_api_version() {
|
||||
return imagify()->get_api_version();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check your Imagify API key status.
|
||||
*
|
||||
* @param string $data An API key.
|
||||
* @return bool
|
||||
*/
|
||||
function get_imagify_status( $data ) {
|
||||
return imagify()->get_status( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimize an image by uploading it on Imagify.
|
||||
*
|
||||
* @param array $data All image data.
|
||||
* @return object
|
||||
*/
|
||||
function fetch_imagify_image( $data ) {
|
||||
return imagify()->fetch_image( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimize an image by sharing its URL on Imagify.
|
||||
*
|
||||
* @since 1.6.7 $data['image'] can contain the file path (prefered) or the result of `curl_file_create()`.
|
||||
*
|
||||
* @param array $data All image data.
|
||||
* @return object
|
||||
*/
|
||||
function upload_imagify_image( $data ) {
|
||||
return imagify()->upload_image( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Imagify Plans Prices.
|
||||
*
|
||||
* @since 1.5
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
function get_imagify_plans_prices() {
|
||||
return imagify()->get_plans_prices();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Imagify All Prices (plans).
|
||||
*
|
||||
* @since 1.5.4
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
function get_imagify_all_prices() {
|
||||
return imagify()->get_all_prices();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Coupon Code exists.
|
||||
*
|
||||
* @since 1.6
|
||||
*
|
||||
* @param string $coupon the coupon code to check.
|
||||
* @return object
|
||||
*/
|
||||
function check_imagify_coupon_code( $coupon ) {
|
||||
return imagify()->check_coupon_code( $coupon );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Discount/Promotion is available.
|
||||
*
|
||||
* @since 1.6.3
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
function check_imagify_discount() {
|
||||
return imagify()->check_discount();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Maximum image size for free plan.
|
||||
*
|
||||
* @since 1.5.6
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function get_imagify_max_image_size() {
|
||||
$max_image_size = get_transient( 'imagify_max_image_size' );
|
||||
|
||||
if ( false === $max_image_size ) {
|
||||
$max_image_size = imagify()->get_public_info();
|
||||
|
||||
if ( ! is_wp_error( $max_image_size ) ) {
|
||||
$max_image_size = $max_image_size->max_image_size;
|
||||
set_transient( 'imagify_max_image_size', $max_image_size, 6 * HOUR_IN_SECONDS );
|
||||
}
|
||||
}
|
||||
|
||||
return $max_image_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a message from our servers.
|
||||
*
|
||||
* @since 1.6.10
|
||||
*
|
||||
* @see Imagify::curl_http_call()
|
||||
* @see Imagify::handle_response()
|
||||
*
|
||||
* @param string $message The message from the server (in English).
|
||||
* @return string If in our list, the translated message. The original message otherwise.
|
||||
*/
|
||||
function imagify_translate_api_message( $message ) {
|
||||
if ( ! $message ) {
|
||||
$message = 'Unknown error occurred';
|
||||
}
|
||||
|
||||
if ( is_wp_error( $message ) ) {
|
||||
if ( $message->errors ) {
|
||||
foreach ( (array) $message->errors as $code => $messages ) {
|
||||
if ( $messages ) {
|
||||
$message->errors[ $code ] = array_map( 'imagify_translate_api_message', (array) $messages );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
if ( is_object( $message ) && ! empty( $message->detail ) ) {
|
||||
$message->detail = imagify_translate_api_message( $message->detail );
|
||||
}
|
||||
|
||||
if ( ! is_string( $message ) ) {
|
||||
return $message;
|
||||
}
|
||||
|
||||
$trim_message = trim( $message, '. ' );
|
||||
|
||||
$messages = [
|
||||
// Local messages from Imagify::curl_http_call() and Imagify::handle_response().
|
||||
'Could not initialize a new cURL handle' => __( 'Could not initialize a new cURL handle.', 'imagify' ),
|
||||
'Unknown error occurred' => sprintf(
|
||||
// translators: %1$s = opening link tag, %2$s = closing link tag.
|
||||
__( 'An unknown error occurred: %1$sMore info and possible solutions%2$s', 'imagify' ),
|
||||
'<a href="https://imagify.io/documentation/optimization-is-stuck/" rel="noopener" target="_blank">',
|
||||
'</a>'
|
||||
),
|
||||
'Your image is too big to be uploaded on our server' => __( 'Your file is too big to be uploaded on our server.', 'imagify' ),
|
||||
'Webp is less performant than original' => __( 'WebP file is larger than the original image', 'imagify' ),
|
||||
'Our server returned an invalid response' => __( 'Our server returned an invalid response.', 'imagify' ),
|
||||
'cURL isn\'t installed on the server' => __( 'cURL is not available on the server.', 'imagify' ),
|
||||
// API messages.
|
||||
'Authentification not provided' => __( 'Authentication not provided.', 'imagify' ),
|
||||
'Cannot create client token' => __( 'Cannot create client token.', 'imagify' ),
|
||||
'Confirm your account to continue optimizing image' => __( 'Confirm your account to continue optimizing files.', 'imagify' ),
|
||||
'Coupon doesn\'t exist' => __( 'Coupon does not exist.', 'imagify' ),
|
||||
'Email field shouldn\'t be empty' => __( 'Email field should not be empty.', 'imagify' ),
|
||||
'Email or Password field shouldn\'t be empty' => __( 'This account already exists.', 'imagify' ),
|
||||
'Error uploading to data Storage' => __( 'Error uploading to Data Storage.', 'imagify' ),
|
||||
'Not able to connect to Data Storage API to get the token' => __( 'Unable to connect to Data Storage API to get the token.', 'imagify' ),
|
||||
'Not able to connect to Data Storage API' => __( 'Unable to connect to Data Storage API.', 'imagify' ),
|
||||
'Not able to retrieve the token from DataStorage API' => __( 'Unable to retrieve the token from Data Storage API.', 'imagify' ),
|
||||
'This email is already registered, you should try another email' => __( 'This email is already registered, you should try another email.', 'imagify' ),
|
||||
'This user doesn\'t exit' => __( 'This user does not exist.', 'imagify' ),
|
||||
'Too many request, be patient' => __( 'Too many requests, please be patient.', 'imagify' ),
|
||||
'Unable to regenerate access token' => __( 'Unable to regenerate access token.', 'imagify' ),
|
||||
'User not valid' => __( 'User not valid.', 'imagify' ),
|
||||
'WELL DONE. This image is already compressed, no further compression required' => __( 'WELL DONE. This media file is already optimized, no further optimization is required.', 'imagify' ),
|
||||
'You are not authorized to perform this action' => __( 'You are not authorized to perform this action.', 'imagify' ),
|
||||
'You\'ve consumed all your data. You have to upgrade your account to continue' => __( 'You have consumed all your data. You have to upgrade your account to continue.', 'imagify' ),
|
||||
'Invalid token' => __( 'Invalid API key', 'imagify' ),
|
||||
'Upload a valid image. The file you uploaded was either not an image or a corrupted image' => __( 'Invalid or corrupted file.', 'imagify' ),
|
||||
];
|
||||
|
||||
if ( isset( $messages[ $trim_message ] ) ) {
|
||||
return $messages[ $trim_message ];
|
||||
}
|
||||
|
||||
// Local message.
|
||||
if ( preg_match( '@^(?:Unknown|An) error occurred \((.+)\)$@', $trim_message, $matches ) ) {
|
||||
/* translators: %s is an error message. */
|
||||
return sprintf( __( 'An error occurred (%s).', 'imagify' ), esc_html( wp_strip_all_tags( $matches[1] ) ) );
|
||||
}
|
||||
|
||||
// Local message.
|
||||
if ( preg_match( '@^Our server returned an error \((.+)\)$@', $trim_message, $matches ) ) {
|
||||
/* translators: %s is an error message. */
|
||||
return sprintf( __( 'Our server returned an error (%s).', 'imagify' ), esc_html( wp_strip_all_tags( $matches[1] ) ) );
|
||||
}
|
||||
|
||||
// API message.
|
||||
if ( preg_match( '@^Custom one time plan starts from (\d+) MB$@', $trim_message, $matches ) ) {
|
||||
/* translators: %s is a formatted number, dont use %d. */
|
||||
return sprintf( __( 'Custom One Time plan starts from %s MB.', 'imagify' ), number_format_i18n( (int) $matches[1] ) );
|
||||
}
|
||||
|
||||
// API message.
|
||||
if ( preg_match( '@^(.*) is not a valid extension$@', $trim_message, $matches ) ) {
|
||||
/* translators: %s is a file extension. */
|
||||
return sprintf( __( '%s is not a valid extension.', 'imagify' ), sanitize_text_field( $matches[1] ) );
|
||||
}
|
||||
|
||||
// API message.
|
||||
if ( preg_match( '@^Request was throttled\. Expected available in ([\d.]+) second$@', $trim_message, $matches ) ) {
|
||||
/* translators: %s is a float number. */
|
||||
return sprintf( _n( 'Request was throttled. Expected available in %s second.', 'Request was throttled. Expected available in %s seconds.', (int) $matches[1], 'imagify' ), sanitize_text_field( $matches[1] ) );
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the bulk optimization
|
||||
*
|
||||
* @param array $contexts An array of contexts (WP/Custom folders).
|
||||
* @param int $optimization_level Optimization level to use.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function imagify_bulk_optimize( $contexts, $optimization_level ) {
|
||||
foreach ( $contexts as $context ) {
|
||||
Imagify\Bulk\Bulk::get_instance()->run_optimize( $context, $optimization_level );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the next-gen generation
|
||||
*
|
||||
* @param array $contexts An array of contexts (WP/Custom folders).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function imagify_generate_nextgen( $contexts ) {
|
||||
Imagify\Bulk\Bulk::get_instance()->run_generate_nextgen( $contexts );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add command to WP CLI
|
||||
*
|
||||
* @param CommandInterface $command Command object.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function imagify_add_command( CommandInterface $command ) {
|
||||
if ( ! defined( 'WP_CLI' ) || ! WP_CLI || ! class_exists( '\WP_CLI' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
\WP_CLI::add_command( $command->get_name(), $command, [
|
||||
'shortdesc' => $command->get_description(),
|
||||
'synopsis' => $command->get_synopsis(),
|
||||
] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the API key is valid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function imagify_is_api_key_valid() {
|
||||
return Imagify_Requirements::is_api_key_valid();
|
||||
}
|
||||
@@ -1,379 +0,0 @@
|
||||
<?php
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Get all mime types which could be optimized by Imagify.
|
||||
*
|
||||
* @since 1.7
|
||||
* @since 1.8 Added $type parameter.
|
||||
*
|
||||
* @param string $type One of 'image', 'not-image'. Any other value will return all mime types.
|
||||
* @return array The mime types.
|
||||
*/
|
||||
function imagify_get_mime_types( $type = null ) {
|
||||
$mimes = [];
|
||||
|
||||
if ( 'not-image' !== $type ) {
|
||||
$mimes = [
|
||||
'jpg|jpeg|jpe' => 'image/jpeg',
|
||||
'png' => 'image/png',
|
||||
'gif' => 'image/gif',
|
||||
'webp' => 'image/webp',
|
||||
];
|
||||
}
|
||||
|
||||
if ( 'image' !== $type ) {
|
||||
$mimes['pdf'] = 'application/pdf';
|
||||
}
|
||||
|
||||
return $mimes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if an attachment has a supported mime type.
|
||||
* Was previously Imagify_AS3CF::is_mime_type_supported() since 1.6.6.
|
||||
*
|
||||
* @since 1.6.8
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param int $attachment_id The attachment ID.
|
||||
* @return bool
|
||||
*/
|
||||
function imagify_is_attachment_mime_type_supported( $attachment_id ) {
|
||||
static $is = array( false );
|
||||
|
||||
$attachment_id = absint( $attachment_id );
|
||||
|
||||
if ( isset( $is[ $attachment_id ] ) ) {
|
||||
return $is[ $attachment_id ];
|
||||
}
|
||||
|
||||
$mime_types = imagify_get_mime_types();
|
||||
$mime_types = array_flip( $mime_types );
|
||||
$mime_type = (string) get_post_mime_type( $attachment_id );
|
||||
|
||||
$is[ $attachment_id ] = isset( $mime_types[ $mime_type ] );
|
||||
|
||||
return $is[ $attachment_id ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get post statuses related to attachments.
|
||||
*
|
||||
* @since 1.7
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function imagify_get_post_statuses() {
|
||||
static $statuses;
|
||||
|
||||
if ( isset( $statuses ) ) {
|
||||
return $statuses;
|
||||
}
|
||||
|
||||
$statuses = array(
|
||||
'inherit' => 'inherit',
|
||||
'private' => 'private',
|
||||
);
|
||||
|
||||
$custom_statuses = get_post_stati( array( 'public' => true ) );
|
||||
unset( $custom_statuses['publish'] );
|
||||
|
||||
if ( $custom_statuses ) {
|
||||
$statuses = array_merge( $statuses, $custom_statuses );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the post statuses Imagify is allowed to optimize.
|
||||
*
|
||||
* @since 1.7
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $statuses An array of post statuses. Kays and values are set.
|
||||
*/
|
||||
$statuses = apply_filters( 'imagify_post_statuses', $statuses );
|
||||
|
||||
return $statuses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the site has attachments (only the ones Imagify would optimize) without the required WP metadata.
|
||||
*
|
||||
* @since 1.7
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function imagify_has_attachments_without_required_metadata() {
|
||||
global $wpdb;
|
||||
static $has;
|
||||
|
||||
if ( isset( $has ) ) {
|
||||
return $has;
|
||||
}
|
||||
|
||||
$mime_types = Imagify_DB::get_mime_types();
|
||||
$statuses = Imagify_DB::get_post_statuses();
|
||||
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause(
|
||||
'p.ID',
|
||||
false,
|
||||
false,
|
||||
"AND p.post_mime_type IN ( $mime_types )
|
||||
AND p.post_type = 'attachment'
|
||||
AND p.post_status IN ( $statuses )"
|
||||
);
|
||||
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause( array(
|
||||
'matching' => false,
|
||||
'test' => false,
|
||||
) );
|
||||
$has = (bool) $wpdb->get_var( // WPCS: unprepared SQL ok.
|
||||
"
|
||||
SELECT p.ID
|
||||
FROM $wpdb->posts AS p
|
||||
$nodata_join
|
||||
WHERE p.post_mime_type IN ( $mime_types )
|
||||
AND p.post_type = 'attachment'
|
||||
AND p.post_status IN ( $statuses )
|
||||
$nodata_where
|
||||
LIMIT 1"
|
||||
);
|
||||
|
||||
return $has;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to the backups directory.
|
||||
*
|
||||
* @since 1.6.8
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $bypass_error True to return the path even if there is an error. This is used when we want to display this path in a message for example.
|
||||
* @return string|bool Path to the backups directory. False on failure.
|
||||
*/
|
||||
function get_imagify_backup_dir_path( $bypass_error = false ) {
|
||||
static $backup_dir;
|
||||
|
||||
if ( isset( $backup_dir ) ) {
|
||||
return $backup_dir;
|
||||
}
|
||||
|
||||
$upload_basedir = get_imagify_upload_basedir( $bypass_error );
|
||||
|
||||
if ( ! $upload_basedir ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$backup_dir = $upload_basedir . 'backup/';
|
||||
|
||||
/**
|
||||
* Filter the backup directory path.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $backup_dir The backup directory path.
|
||||
*/
|
||||
$backup_dir = apply_filters( 'imagify_backup_directory', $backup_dir );
|
||||
$backup_dir = imagify_get_filesystem()->normalize_dir_path( $backup_dir );
|
||||
|
||||
return $backup_dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the folder containing the backups is writable.
|
||||
*
|
||||
* @since 1.6.8
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function imagify_backup_dir_is_writable() {
|
||||
return imagify_get_filesystem()->make_dir( get_imagify_backup_dir_path() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the backup path of a specific attachement.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $file_path The file path.
|
||||
* @return string|bool The backup path. False on failure.
|
||||
*/
|
||||
function get_imagify_attachment_backup_path( $file_path ) {
|
||||
$file_path = wp_normalize_path( (string) $file_path );
|
||||
$upload_basedir = get_imagify_upload_basedir();
|
||||
$backup_dir = get_imagify_backup_dir_path();
|
||||
|
||||
if ( ! $file_path || ! $upload_basedir ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return preg_replace( '@^' . preg_quote( $upload_basedir, '@' ) . '@', $backup_dir, $file_path );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve file path for an attachment based on filename.
|
||||
*
|
||||
* @since 1.4.5
|
||||
*
|
||||
* @param int $file_path The file path.
|
||||
* @return string|false The file path to where the attached file should be, false otherwise.
|
||||
*/
|
||||
function get_imagify_attached_file( $file_path ) {
|
||||
$file_path = wp_normalize_path( (string) $file_path );
|
||||
$upload_basedir = get_imagify_upload_basedir();
|
||||
|
||||
if ( ! $file_path || ! $upload_basedir ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The file path is absolute.
|
||||
if ( strpos( $file_path, '/' ) === 0 || preg_match( '|^.:\\\|', $file_path ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Prepend upload dir.
|
||||
return $upload_basedir . $file_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the URL for an attachment based on file path.
|
||||
*
|
||||
* @since 1.4.5
|
||||
*
|
||||
* @param string $file_path A relative or absolute file path.
|
||||
* @return string|bool File URL, otherwise false.
|
||||
*/
|
||||
function get_imagify_attachment_url( $file_path ) {
|
||||
$file_path = wp_normalize_path( (string) $file_path );
|
||||
$upload_basedir = get_imagify_upload_basedir();
|
||||
|
||||
if ( ! $file_path || ! $upload_basedir ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$upload_baseurl = get_imagify_upload_baseurl();
|
||||
|
||||
// Check that the upload base exists in the (absolute) file location.
|
||||
if ( 0 === strpos( $file_path, $upload_basedir ) ) {
|
||||
// Replace file location with url location.
|
||||
return preg_replace( '@^' . preg_quote( $upload_basedir, '@' ) . '@', $upload_baseurl, $file_path );
|
||||
}
|
||||
|
||||
if ( false !== strpos( '/' . $file_path, '/wp-content/uploads/' ) ) {
|
||||
// Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
|
||||
return trailingslashit( $upload_baseurl . _wp_get_attachment_relative_path( $file_path ) ) . imagify_get_filesystem()->file_name( $file_path );
|
||||
}
|
||||
|
||||
// It's a newly-uploaded file, therefore $file is relative to the basedir.
|
||||
return $upload_baseurl . $file_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get size information for all currently registered thumbnail sizes.
|
||||
*
|
||||
* @since 1.5.10
|
||||
* @since 1.6.10 For consistency, revamped the function like WP does with wp_generate_attachment_metadata().
|
||||
* Removed the filter, added crop value to each size.
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array {
|
||||
* Data for the currently registered thumbnail sizes.
|
||||
* Size names are used as array keys.
|
||||
*
|
||||
* @type int $width The image width.
|
||||
* @type int $height The image height.
|
||||
* @type bool $crop True to crop, false to resize.
|
||||
* @type string $name The size name.
|
||||
* }
|
||||
*/
|
||||
function get_imagify_thumbnail_sizes() {
|
||||
// All image size names.
|
||||
$intermediate_image_sizes = get_intermediate_image_sizes();
|
||||
$intermediate_image_sizes = array_flip( $intermediate_image_sizes );
|
||||
// Additional image size attributes.
|
||||
$additional_image_sizes = wp_get_additional_image_sizes();
|
||||
|
||||
// Create the full array with sizes and crop info.
|
||||
foreach ( $intermediate_image_sizes as $size_name => $s ) {
|
||||
$intermediate_image_sizes[ $size_name ] = array(
|
||||
'width' => '',
|
||||
'height' => '',
|
||||
'crop' => false,
|
||||
'name' => $size_name,
|
||||
);
|
||||
|
||||
if ( isset( $additional_image_sizes[ $size_name ]['width'] ) ) {
|
||||
// For theme-added sizes.
|
||||
$intermediate_image_sizes[ $size_name ]['width'] = (int) $additional_image_sizes[ $size_name ]['width'];
|
||||
} else {
|
||||
// For default sizes set in options.
|
||||
$intermediate_image_sizes[ $size_name ]['width'] = (int) get_option( "{$size_name}_size_w" );
|
||||
}
|
||||
|
||||
if ( isset( $additional_image_sizes[ $size_name ]['height'] ) ) {
|
||||
// For theme-added sizes.
|
||||
$intermediate_image_sizes[ $size_name ]['height'] = (int) $additional_image_sizes[ $size_name ]['height'];
|
||||
} else {
|
||||
// For default sizes set in options.
|
||||
$intermediate_image_sizes[ $size_name ]['height'] = (int) get_option( "{$size_name}_size_h" );
|
||||
}
|
||||
|
||||
if ( isset( $additional_image_sizes[ $size_name ]['crop'] ) ) {
|
||||
// For theme-added sizes.
|
||||
$intermediate_image_sizes[ $size_name ]['crop'] = (int) $additional_image_sizes[ $size_name ]['crop'];
|
||||
} else {
|
||||
// For default sizes set in options.
|
||||
$intermediate_image_sizes[ $size_name ]['crop'] = (int) get_option( "{$size_name}_crop" );
|
||||
}
|
||||
}
|
||||
|
||||
return $intermediate_image_sizes;
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple helper to get the upload basedir.
|
||||
*
|
||||
* @since 1.6.7
|
||||
* @since 1.6.8 Added the $bypass_error parameter.
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $bypass_error True to return the path even if there is an error. This is used when we want to display this path in a message for example.
|
||||
* @return string|bool The path. False on failure.
|
||||
*/
|
||||
function get_imagify_upload_basedir( $bypass_error = false ) {
|
||||
return imagify_get_filesystem()->get_upload_basedir( $bypass_error );
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple helper to get the upload baseurl.
|
||||
*
|
||||
* @since 1.6.7
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string|bool The URL. False on failure.
|
||||
*/
|
||||
function get_imagify_upload_baseurl() {
|
||||
return imagify_get_filesystem()->get_upload_baseurl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the maximal number of unoptimized attachments to fetch.
|
||||
*
|
||||
* @since 1.6.14
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function imagify_get_unoptimized_attachment_limit() {
|
||||
/**
|
||||
* Filter the unoptimized attachments limit query.
|
||||
*
|
||||
* @since 1.4.4
|
||||
*
|
||||
* @param int $limit The limit (-1 for unlimited).
|
||||
*/
|
||||
$limit = (int) apply_filters( 'imagify_unoptimized_attachment_limit', 10000 );
|
||||
|
||||
return -1 === $limit ? PHP_INT_MAX : abs( $limit );
|
||||
}
|
||||
@@ -1,646 +0,0 @@
|
||||
<?php
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Get the list of the names of the Imagify context currently in use.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array An array of strings.
|
||||
*/
|
||||
function imagify_get_context_names() {
|
||||
static $contexts;
|
||||
|
||||
if ( isset( $contexts ) ) {
|
||||
return $contexts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register new contexts.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $contexts An array of context names.
|
||||
*/
|
||||
$contexts = (array) apply_filters( 'imagify_register_context', [] );
|
||||
|
||||
$contexts = array_filter( $contexts, function( $context ) {
|
||||
return $context && is_string( $context );
|
||||
} );
|
||||
$contexts = array_merge( [ 'wp', 'custom-folders' ], $contexts );
|
||||
|
||||
sort( $contexts );
|
||||
|
||||
return $contexts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize an optimization context.
|
||||
*
|
||||
* @since 1.6.11
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $context The context.
|
||||
* @return string
|
||||
*/
|
||||
function imagify_sanitize_context( $context ) {
|
||||
return sanitize_key( $context );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Imagify context instance.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $context The context name. Default values are 'wp' and 'custom-folders'.
|
||||
* @return ContextInterface The context instance.
|
||||
*/
|
||||
function imagify_get_context( $context ) {
|
||||
$class_name = imagify_get_context_class_name( $context );
|
||||
return $class_name::get_instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Imagify context class name.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $context The context name. Default values are 'wp' and 'custom-folders'.
|
||||
* @return string The context class name.
|
||||
*/
|
||||
function imagify_get_context_class_name( $context ) {
|
||||
$context = imagify_sanitize_context( $context );
|
||||
|
||||
switch ( $context ) {
|
||||
case 'wp':
|
||||
$class_name = '\\Imagify\\Context\\WP';
|
||||
break;
|
||||
|
||||
case 'custom-folders':
|
||||
$class_name = '\\Imagify\\Context\\CustomFolders';
|
||||
break;
|
||||
|
||||
default:
|
||||
$class_name = '\\Imagify\\Context\\Noop';
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the name of the class to use to define a context.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param int $class_name The class name.
|
||||
* @param string $context The context name.
|
||||
*/
|
||||
$class_name = apply_filters( 'imagify_context_class_name', $class_name, $context );
|
||||
|
||||
return '\\' . ltrim( $class_name, '\\' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Imagify process instance depending on a context.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param int $media_id The media ID.
|
||||
* @param string $context The context name. Default values are 'wp' and 'custom-folders'.
|
||||
* @return ProcessInterface The optimization process instance.
|
||||
*/
|
||||
function imagify_get_optimization_process( $media_id, $context ) {
|
||||
$class_name = imagify_get_optimization_process_class_name( $context );
|
||||
return new $class_name( $media_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Imagify process class name depending on a context.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $context The context name. Default values are 'wp' and 'custom-folders'.
|
||||
* @return string The optimization process class name.
|
||||
*/
|
||||
function imagify_get_optimization_process_class_name( $context ) {
|
||||
$context = imagify_sanitize_context( $context );
|
||||
|
||||
switch ( $context ) {
|
||||
case 'wp':
|
||||
$class_name = '\\Imagify\\Optimization\\Process\\WP';
|
||||
break;
|
||||
|
||||
case 'custom-folders':
|
||||
$class_name = '\\Imagify\\Optimization\\Process\\CustomFolders';
|
||||
break;
|
||||
|
||||
default:
|
||||
$class_name = '\\Imagify\\Optimization\\Process\\Noop';
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the name of the class to use for the optimization.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param int $class_name The class name.
|
||||
* @param string $context The context name.
|
||||
*/
|
||||
$class_name = apply_filters( 'imagify_process_class_name', $class_name, $context );
|
||||
|
||||
return '\\' . ltrim( $class_name, '\\' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get WP Direct filesystem object. Also define chmod constants if not done yet.
|
||||
*
|
||||
* @since 1.6.5
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return object A Imagify_Filesystem object.
|
||||
*/
|
||||
function imagify_get_filesystem() {
|
||||
return Imagify_Filesystem::get_instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a path (or URL) to its WebP version.
|
||||
* To keep the function simple:
|
||||
* - Not tested if it's an image.
|
||||
* - File existance is not tested.
|
||||
* - If an URL is given, make sure it doesn't contain query args.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $path A file path or URL.
|
||||
* @return string
|
||||
*/
|
||||
function imagify_path_to_webp( $path ) {
|
||||
return $path . '.webp';
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a path (or URL) to its next-gen version.
|
||||
* To keep the function simple:
|
||||
* - Not tested if it's an image.
|
||||
* - File existance is not tested.
|
||||
* - If an URL is given, make sure it doesn't contain query args.
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @param string $path A file path or URL.
|
||||
* @param string $format format we are targeting.
|
||||
* @return string
|
||||
*/
|
||||
function imagify_path_to_nextgen( $path, string $format ) {
|
||||
switch ( $format ) {
|
||||
case 'webp':
|
||||
$path = $path . '.webp';
|
||||
break;
|
||||
case 'avif':
|
||||
$path = $path . '.avif';
|
||||
break;
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the current user can optimize custom folders.
|
||||
*
|
||||
* @since 1.7
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function imagify_can_optimize_custom_folders() {
|
||||
static $can;
|
||||
|
||||
if ( isset( $can ) ) {
|
||||
return $can;
|
||||
}
|
||||
|
||||
// Check if the DB tables are ready.
|
||||
if ( ! Imagify_Folders_DB::get_instance()->can_operate() || ! Imagify_Files_DB::get_instance()->can_operate() ) {
|
||||
$can = false;
|
||||
return $can;
|
||||
}
|
||||
|
||||
// Check for user capacity.
|
||||
$can = imagify_get_context( 'custom-folders' )->current_user_can( 'optimize' );
|
||||
|
||||
return $can;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple helper to get some external URLs, like to the documentation.
|
||||
*
|
||||
* @since 1.6.12
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $target What we want.
|
||||
* @param array $query_args An array of query arguments.
|
||||
* @return string The URL.
|
||||
*/
|
||||
function imagify_get_external_url( $target, $query_args = array() ) {
|
||||
$site_url = IMAGIFY_SITE_DOMAIN . '/';
|
||||
$app_url = IMAGIFY_APP_DOMAIN . '/#/';
|
||||
|
||||
switch ( $target ) {
|
||||
case 'plugin':
|
||||
/* translators: Plugin URI of the plugin/theme */
|
||||
$url = __( 'https://wordpress.org/plugins/imagify/', 'imagify' );
|
||||
break;
|
||||
|
||||
case 'rate':
|
||||
$url = 'https://wordpress.org/support/view/plugin-reviews/imagify?rate=5#postform';
|
||||
break;
|
||||
|
||||
case 'share-twitter':
|
||||
$url = rawurlencode( imagify_get_external_url( 'plugin' ) );
|
||||
$url = 'https://twitter.com/intent/tweet?source=webclient&original_referer=' . $url . '&url=' . $url . '&related=imagify&hastags=performance,web,wordpress';
|
||||
break;
|
||||
|
||||
case 'share-facebook':
|
||||
$url = rawurlencode( imagify_get_external_url( 'plugin' ) );
|
||||
$url = 'https://www.facebook.com/sharer/sharer.php?u=' . $url;
|
||||
break;
|
||||
|
||||
case 'contact':
|
||||
$lang = imagify_get_current_lang_in( 'fr' );
|
||||
$paths = array(
|
||||
'en' => 'contact',
|
||||
'fr' => 'fr/contact',
|
||||
);
|
||||
|
||||
$url = $site_url . $paths[ $lang ] . '/';
|
||||
break;
|
||||
|
||||
case 'documentation':
|
||||
$url = $site_url . 'documentation/';
|
||||
break;
|
||||
|
||||
case 'documentation-imagick-gd':
|
||||
$url = $site_url . 'documentation/solve-imagemagick-gd-required/';
|
||||
break;
|
||||
|
||||
case 'register':
|
||||
$partner = imagify_get_partner();
|
||||
|
||||
if ( $partner ) {
|
||||
$query_args['partner'] = $partner;
|
||||
}
|
||||
|
||||
$url = $app_url . 'register';
|
||||
break;
|
||||
|
||||
case 'subscription':
|
||||
$url = $app_url . 'subscription';
|
||||
break;
|
||||
|
||||
case 'get-api-key':
|
||||
$url = $app_url . 'api';
|
||||
break;
|
||||
|
||||
case 'payment':
|
||||
// Don't remove the trailing slash.
|
||||
$url = $app_url . 'plugin/';
|
||||
break;
|
||||
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
|
||||
if ( $query_args ) {
|
||||
$url = add_query_arg( $query_args, $url );
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current lang ('fr', 'en', 'de'...), limited to a given list.
|
||||
*
|
||||
* @since 1.6.14
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $langs An array of langs, like array( 'de', 'es', 'fr', 'it' ).
|
||||
* @return string The current lang. Default is 'en'.
|
||||
*/
|
||||
function imagify_get_current_lang_in( $langs ) {
|
||||
static $locale;
|
||||
|
||||
if ( ! isset( $locale ) ) {
|
||||
$locale = imagify_get_locale();
|
||||
$locale = explode( '_', strtolower( $locale . '_' ) ); // Trailing underscore is to make sure $locale[1] is set.
|
||||
}
|
||||
|
||||
foreach ( (array) $langs as $lang ) {
|
||||
if ( $lang === $locale[0] || $lang === $locale[1] ) {
|
||||
return $lang;
|
||||
}
|
||||
}
|
||||
|
||||
return 'en';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current locale.
|
||||
*
|
||||
* @since 1.6.14
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string The current locale.
|
||||
*/
|
||||
function imagify_get_locale() {
|
||||
$locale = function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
|
||||
/**
|
||||
* Filter the locale used by Imagify.
|
||||
*
|
||||
* @since 1.6.14
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $locale The current locale.
|
||||
*/
|
||||
return apply_filters( 'imagify_locale', $locale );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the label corresponding to the given optimization label.
|
||||
*
|
||||
* @since 1.7
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param int|bool $level Optimization level (between 0 and 2). False if no level.
|
||||
* @param string $format Format to display the label. Use %ICON% for the icon and %s for the label.
|
||||
* @return string The label.
|
||||
*/
|
||||
function imagify_get_optimization_level_label( $level, $format = '%s' ) {
|
||||
if ( ! is_numeric( $level ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ( strpos( $format, '%ICON%' ) !== false ) {
|
||||
$icon = '<svg width="12" height="12" viewBox="0 0 12 12" xmlns="http://www.w3.org/2000/svg"><g fill="#40B1D0" fill-rule="evenodd">';
|
||||
|
||||
switch ( $level ) {
|
||||
case 2:
|
||||
case 1:
|
||||
$icon .= '<polygon points="11.6054688 11.6054688 8.7890625 11.6054688 8.7890625 0.39453125 11.6054688 0.39453125"/><polygon points="7.39453125 11.6054688 4.60546875 11.6054688 4.60546875 3.89453125 7.39453125 3.89453125"/><polygon points="3.2109375 11.6054688 0.39453125 11.6054688 0.39453125 6 3.2109375 6"/>';
|
||||
break;
|
||||
case 0:
|
||||
$icon .= '<polygon fill="#CCD1D6" points="11.6054688 11.6054688 8.7890625 11.6054688 8.7890625 0.39453125 11.6054688 0.39453125"/><polygon fill="#CCD1D6" points="7.39453125 11.6054688 4.60546875 11.6054688 4.60546875 3.89453125 7.39453125 3.89453125"/><polygon points="3.2109375 11.6054688 0.39453125 11.6054688 0.39453125 6 3.2109375 6"/>';
|
||||
}
|
||||
|
||||
$icon .= '</g></svg>';
|
||||
|
||||
$format = str_replace( '%ICON%', $icon, $format );
|
||||
}
|
||||
|
||||
switch ( $level ) {
|
||||
case 2:
|
||||
case 1:
|
||||
return sprintf( $format, __( 'Smart', 'imagify' ) );
|
||||
case 0:
|
||||
return sprintf( $format, __( 'Lossless', 'imagify' ) );
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* `array_merge()` + `array_intersect_key()`.
|
||||
*
|
||||
* @since 1.7
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $values The array we're interested in.
|
||||
* @param array $default The array we use as boundaries.
|
||||
* @return array
|
||||
*/
|
||||
function imagify_merge_intersect( $values, $default ) {
|
||||
$values = array_merge( $default, (array) $values );
|
||||
return array_intersect_key( $values, $default );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true.
|
||||
* Useful for returning true to filters easily.
|
||||
* Similar to WP's __return_true() function, it allows to remove it from a filter without removing another one added by another plugin.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True.
|
||||
*/
|
||||
function imagify_return_true() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns false.
|
||||
* Useful for returning false to filters easily.
|
||||
* Similar to WP's __return_false() function, it allows to remove it from a filter without removing another one added by another plugin.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool False.
|
||||
*/
|
||||
function imagify_return_false() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks a class as deprecated and informs when it has been used.
|
||||
* Similar to _deprecated_constructor(), but with different strings.
|
||||
* The current behavior is to trigger a user error if `WP_DEBUG` is true.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $class The class containing the deprecated constructor.
|
||||
* @param string $version The version of WordPress that deprecated the function.
|
||||
* @param string $replacement Optional. The function that should have been called. Default null.
|
||||
* @param string $parent_class Optional. The parent class calling the deprecated constructor. Default empty string.
|
||||
*/
|
||||
function imagify_deprecated_class( $class, $version, $replacement = null, $parent_class = '' ) {
|
||||
|
||||
/**
|
||||
* Fires when a deprecated class is called.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $class The class containing the deprecated constructor.
|
||||
* @param string $version The version of WordPress that deprecated the function.
|
||||
* @param string $replacement Optional. The function that should have been called.
|
||||
* @param string $parent_class The parent class calling the deprecated constructor.
|
||||
*/
|
||||
do_action( 'imagify_deprecated_class_run', $class, $version, $replacement, $parent_class );
|
||||
|
||||
if ( ! WP_DEBUG ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters whether to trigger an error for deprecated classes.
|
||||
*
|
||||
* `WP_DEBUG` must be true in addition to the filter evaluating to true.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $trigger Whether to trigger the error for deprecated classes. Default true.
|
||||
*/
|
||||
if ( ! apply_filters( 'imagify_deprecated_class_trigger_error', true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( function_exists( '__' ) ) {
|
||||
if ( ! empty( $parent_class ) ) {
|
||||
/**
|
||||
* With parent class.
|
||||
*/
|
||||
if ( ! empty( $replacement ) ) {
|
||||
/**
|
||||
* With replacement.
|
||||
*/
|
||||
call_user_func(
|
||||
'trigger_error',
|
||||
sprintf(
|
||||
/* translators: 1: PHP class name, 2: PHP parent class name, 3: version number, 4: replacement class name. */
|
||||
__( 'The called class %1$s extending %2$s is <strong>deprecated</strong> since version %3$s! Use %4$s instead.', 'imagify' ),
|
||||
'<code>' . $class . '</code>',
|
||||
'<code>' . $parent_class . '</code>',
|
||||
'<strong>' . $version . '</strong>',
|
||||
'<code>' . $replacement . '</code>'
|
||||
)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Without replacement.
|
||||
*/
|
||||
call_user_func(
|
||||
'trigger_error',
|
||||
sprintf(
|
||||
/* translators: 1: PHP class name, 2: PHP parent class name, 3: version number. */
|
||||
__( 'The called class %1$s extending %2$s is <strong>deprecated</strong> since version %3$s!', 'imagify' ),
|
||||
'<code>' . $class . '</code>',
|
||||
'<code>' . $parent_class . '</code>',
|
||||
'<strong>' . $version . '</strong>'
|
||||
)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Without parent class.
|
||||
*/
|
||||
if ( ! empty( $replacement ) ) {
|
||||
/**
|
||||
* With replacement.
|
||||
*/
|
||||
call_user_func(
|
||||
'trigger_error',
|
||||
sprintf(
|
||||
/* translators: 1: PHP class name, 2: version number, 3: replacement class name. */
|
||||
__( 'The called class %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', 'imagify' ),
|
||||
'<code>' . $class . '</code>',
|
||||
'<strong>' . $version . '</strong>',
|
||||
'<code>' . $replacement . '</code>'
|
||||
)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Without replacement.
|
||||
*/
|
||||
call_user_func(
|
||||
'trigger_error',
|
||||
sprintf(
|
||||
/* translators: 1: PHP class name, 2: version number. */
|
||||
__( 'The called class %1$s is <strong>deprecated</strong> since version %2$s!', 'imagify' ),
|
||||
'<code>' . $class . '</code>',
|
||||
'<strong>' . $version . '</strong>'
|
||||
)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! empty( $parent_class ) ) {
|
||||
/**
|
||||
* With parent class.
|
||||
*/
|
||||
if ( ! empty( $replacement ) ) {
|
||||
/**
|
||||
* With replacement.
|
||||
*/
|
||||
call_user_func(
|
||||
'trigger_error',
|
||||
sprintf(
|
||||
'The called class %1$s extending %2$s is <strong>deprecated</strong> since version %3$s! Use %4$s instead.',
|
||||
'<code>' . $class . '</code>',
|
||||
'<code>' . $parent_class . '</code>',
|
||||
'<strong>' . $version . '</strong>',
|
||||
'<code>' . $replacement . '</code>'
|
||||
)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Without replacement.
|
||||
*/
|
||||
call_user_func(
|
||||
'trigger_error',
|
||||
sprintf(
|
||||
'The called class %1$s extending %2$s is <strong>deprecated</strong> since version %3$s!',
|
||||
'<code>' . $class . '</code>',
|
||||
'<code>' . $parent_class . '</code>',
|
||||
'<strong>' . $version . '</strong>'
|
||||
)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Without parent class.
|
||||
*/
|
||||
if ( ! empty( $replacement ) ) {
|
||||
/**
|
||||
* With replacement.
|
||||
*/
|
||||
call_user_func(
|
||||
'trigger_error',
|
||||
sprintf(
|
||||
'The called class %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.',
|
||||
'<code>' . $class . '</code>',
|
||||
'<strong>' . $version . '</strong>',
|
||||
'<code>' . $replacement . '</code>'
|
||||
)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Without replacement.
|
||||
*/
|
||||
call_user_func(
|
||||
'trigger_error',
|
||||
sprintf(
|
||||
'The called class %1$s is <strong>deprecated</strong> since version %2$s!',
|
||||
'<code>' . $class . '</code>',
|
||||
'<strong>' . $version . '</strong>'
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
<?php
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Round UP to nearest half integer.
|
||||
*
|
||||
* @since 1.0
|
||||
* @source http://stackoverflow.com/a/13526408
|
||||
*
|
||||
* @param int|float|string $number The number to round up.
|
||||
* @return float The formatted number.
|
||||
*/
|
||||
function imagify_round_half_five( $number ) {
|
||||
$number = strval( $number );
|
||||
$number = explode( '.', $number );
|
||||
|
||||
if ( ! isset( $number[1] ) ) {
|
||||
return $number[0];
|
||||
}
|
||||
|
||||
$decimal = floatval( '0.' . substr( $number[1], 0, 2 ) ); // Cut only 2 numbers.
|
||||
|
||||
if ( $decimal > 0 ) {
|
||||
if ( $decimal <= 0.5 ) {
|
||||
return floatval( $number[0] ) + 0.5;
|
||||
}
|
||||
if ( $decimal <= 0.99 ) {
|
||||
return floatval( $number[0] ) + 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
return floatval( $number );
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert number of bytes largest unit bytes will fit into.
|
||||
* This is a clone of size_format(), but with a non-breaking space.
|
||||
*
|
||||
* @since 1.7
|
||||
* @since 1.8.1 Automatic $decimals.
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param int|string $bytes Number of bytes. Note max integer size for integers.
|
||||
* @param int $decimals Optional. Precision of number of decimal places.
|
||||
* If negative or not an integer, $decimals value is "automatic": 0 if $bytes <= 1GB, or 1 if > 1GB.
|
||||
* @return string|false False on failure. Number string on success.
|
||||
*/
|
||||
function imagify_size_format( $bytes, $decimals = -1 ) {
|
||||
|
||||
if ( $decimals < 0 || ! is_int( $decimals ) ) {
|
||||
$decimals = $bytes > pow( 1024, 3 ) ? 1 : 0;
|
||||
}
|
||||
|
||||
$bytes = @size_format( $bytes, $decimals );
|
||||
return str_replace( ' ', ' ', $bytes );
|
||||
}
|
||||
@@ -1,333 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Imagify\Imagifybeat\Actions;
|
||||
use Imagify\Imagifybeat\Core;
|
||||
use Imagify\Stats\OptimizedMediaWithoutNextGen;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Get all translations we can use with wp_localize_script().
|
||||
*
|
||||
* @since 1.5
|
||||
*
|
||||
* @param string $context The translation context.
|
||||
*
|
||||
* @return array $translations The translations.
|
||||
*/
|
||||
function get_imagify_localize_script_translations( $context ) {
|
||||
global $post_id;
|
||||
|
||||
$imagifybeat_actions = Actions::get_instance();
|
||||
|
||||
switch ( $context ) {
|
||||
case 'admin-bar':
|
||||
if ( is_admin() ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
'ajaxurl' => admin_url( 'admin-ajax.php' ),
|
||||
];
|
||||
|
||||
case 'notices':
|
||||
return [
|
||||
'labels' => [
|
||||
/* translators: Don't use escaped HTML entities here (like ). */
|
||||
'signupTitle' => __( 'Let\'s get you started!', 'imagify' ),
|
||||
'signupText' => __( 'Enter your email to get an API key:', 'imagify' ),
|
||||
'signupConfirmButtonText' => __( 'Sign Up', 'imagify' ),
|
||||
'signupErrorEmptyEmail' => __( 'You need to specify an email!', 'imagify' ),
|
||||
/* translators: Don't use escaped HTML entities here (like ). */
|
||||
'signupSuccessTitle' => __( 'Congratulations!', 'imagify' ),
|
||||
'signupSuccessText' => __( 'Your account has been successfully created. Please check your mailbox, you are going to receive an email with API key.', 'imagify' ),
|
||||
/* translators: Don't use escaped HTML entities here (like ). */
|
||||
'saveApiKeyTitle' => __( 'Connect to Imagify!', 'imagify' ),
|
||||
'saveApiKeyText' => __( 'Paste your API key below:', 'imagify' ),
|
||||
'saveApiKeyConfirmButtonText' => __( 'Connect me', 'imagify' ),
|
||||
'ApiKeyErrorEmpty' => __( 'You need to specify your API key!', 'imagify' ),
|
||||
'ApiKeyCheckSuccessTitle' => __( 'Congratulations!', 'imagify' ),
|
||||
'ApiKeyCheckSuccessText' => __( 'Your API key is valid. You can now configure the Imagify settings to optimize your images.', 'imagify' ),
|
||||
],
|
||||
];
|
||||
|
||||
case 'sweetalert':
|
||||
return [
|
||||
'labels' => [
|
||||
'cancelButtonText' => __( 'Cancel' ),
|
||||
],
|
||||
];
|
||||
|
||||
case 'options':
|
||||
$translations = [
|
||||
'getFilesTree' => imagify_can_optimize_custom_folders() ? get_imagify_admin_url( 'get-files-tree' ) : false,
|
||||
'labels' => [
|
||||
'ValidApiKeyText' => __( 'Your API key is valid.', 'imagify' ),
|
||||
'waitApiKeyCheckText' => __( 'Check in progress...', 'imagify' ),
|
||||
'ApiKeyCheckSuccessTitle' => __( 'Congratulations!', 'imagify' ),
|
||||
'ApiKeyCheckSuccessText' => __( 'Your API key is valid. You can now configure the Imagify settings to optimize your images.', 'imagify' ),
|
||||
'noBackupTitle' => __( 'Don\'t Need a Parachute?', 'imagify' ),
|
||||
'noBackupText' => __( 'If you keep this option deactivated, you won\'t be able to re-optimize your images to another compression level and restore your original images in case of need.', 'imagify' ),
|
||||
'removeFolder' => _x( 'Remove', 'custom folder', 'imagify' ),
|
||||
'filesTreeTitle' => __( 'Select Folders', 'imagify' ),
|
||||
'filesTreeSubTitle' => __( 'Select one or several folders to optimize.', 'imagify' ),
|
||||
'cleaningInfo' => __( 'Some folders that do not contain any images are hidden.', 'imagify' ),
|
||||
'confirmFilesTreeBtn' => __( 'Select Folders', 'imagify' ),
|
||||
'customFilesLegend' => __( 'Choose the folders to optimize', 'imagify' ),
|
||||
'error' => __( 'Error', 'imagify' ),
|
||||
'themesAdded' => __( 'Added! All Good!', 'imagify' ),
|
||||
],
|
||||
];
|
||||
|
||||
if ( OptimizedMediaWithoutNextGen::get_instance()->get_cached_stat() ) {
|
||||
$contexts = imagify_get_context_names();
|
||||
$translations['bulk'] = [
|
||||
'curlMissing' => ! Imagify_Requirements::supports_curl(),
|
||||
'editorMissing' => ! Imagify_Requirements::supports_image_editor(),
|
||||
'extHttpBlocked' => Imagify_Requirements::is_imagify_blocked(),
|
||||
'apiDown' => ! Imagify_Requirements::is_api_up(),
|
||||
'keyIsValid' => Imagify_Requirements::is_api_key_valid(),
|
||||
'isOverQuota' => Imagify_Requirements::is_over_quota(),
|
||||
'imagifybeatIDs' => [
|
||||
'progress' => $imagifybeat_actions->get_imagifybeat_id( 'options_optimization_status' ),
|
||||
'requirements' => $imagifybeat_actions->get_imagifybeat_id( 'requirements' ),
|
||||
],
|
||||
'ajaxActions' => [
|
||||
'MissingNextGen' => 'imagify_missing_nextgen_generation',
|
||||
],
|
||||
'ajaxNonce' => wp_create_nonce( 'imagify-bulk-optimize' ),
|
||||
'contexts' => $contexts,
|
||||
'progress_next_gen' => [
|
||||
'remaining' => OptimizedMediaWithoutNextGen::get_instance()->get_stat(),
|
||||
'total' => get_transient( 'imagify_missing_next_gen_total' ),
|
||||
],
|
||||
'labels' => [
|
||||
'curlMissing' => __( 'cURL is not available on the server.', 'imagify' ),
|
||||
'editorMissing' => sprintf(
|
||||
// translators: %s is a "More info?" link.
|
||||
__( 'No php extensions are available to edit images on the server. ImageMagick or GD is required. %s', 'imagify' ),
|
||||
'<a href="' . esc_url( imagify_get_external_url( 'documentation-imagick-gd' ) ) . '" target="_blank">' . __( 'More info?', 'imagify' ) . '</a>'
|
||||
),
|
||||
'extHttpBlocked' => __( 'External HTTP requests are blocked.', 'imagify' ),
|
||||
'apiDown' => __( 'Sorry, our servers are temporarily unavailable. Please, try again in a couple of minutes.', 'imagify' ),
|
||||
'invalidAPIKeyTitle' => __( 'Your API key is not valid!', 'imagify' ),
|
||||
'overQuotaTitle' => __( 'You have used all your credits!', 'imagify' ),
|
||||
'nothingToDoTitle' => __( 'Hold on!', 'imagify' ),
|
||||
'nothingToDoText' => __( 'All your optimized images already have a next-gen version. Congratulations!', 'imagify' ),
|
||||
'nothingToDoNoBackupText' => __( 'Because the selected images did not have a backup copy, Imagify was unable to create next-gen versions.', 'imagify' ),
|
||||
'error' => __( 'Error', 'imagify' ),
|
||||
'ajaxErrorText' => __( 'The operation failed.', 'imagify' ),
|
||||
'getUnoptimizedImagesErrorTitle' => __( 'Oops, There is something wrong!', 'imagify' ),
|
||||
'getUnoptimizedImagesErrorText' => __( 'An unknown error occurred when we tried to get all your unoptimized media files. Try again and if the issue still persists, please contact us!', 'imagify' ),
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Filter the number of parallel queries generating WebP images by bulk method.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param int $bufferSize Number of parallel queries.
|
||||
*/
|
||||
$translations['bulk']['bufferSize'] = apply_filters( 'imagify_bulk_generate_webp_buffer_size', 4 );
|
||||
$translations['bulk']['bufferSize'] = max( 1, (int) $translations['bulk']['bufferSize'] );
|
||||
}
|
||||
|
||||
return $translations;
|
||||
|
||||
case 'pricing-modal':
|
||||
$translations = [
|
||||
'imagify_app_domain' => IMAGIFY_APP_DOMAIN,
|
||||
'labels' => [
|
||||
'errorCouponAPI' => __( 'Error with checking this coupon.', 'imagify' ),
|
||||
/* translators: 1 is a percentage, 2 is a coupon code. */
|
||||
'successCouponAPI' => sprintf( _x( '%1$s off with %2$s', 'coupon validated', 'imagify' ), '<span class="imagify-coupon-offer"></span>', '<strong class="imagify-coupon-word"></strong>' ),
|
||||
'errorPriceAPI' => __( 'Something went wrong with getting our updated offers. Please retry later.', 'imagify' ),
|
||||
'defaultCouponLabel' => __( 'If you have a <strong>coupon code</strong><br> use it here:', 'imagify' ),
|
||||
'errorCouponPlan' => __( 'This coupon is not valid with this plan.', 'imagify' ),
|
||||
],
|
||||
];
|
||||
|
||||
if ( Imagify_Requirements::is_api_key_valid() ) {
|
||||
$translations['userDataCache'] = [
|
||||
'deleteAction' => 'imagify_delete_user_data_cache',
|
||||
'deleteNonce' => wp_create_nonce( 'imagify_delete_user_data_cache' ),
|
||||
];
|
||||
}
|
||||
|
||||
return $translations;
|
||||
|
||||
case 'twentytwenty':
|
||||
$image = [
|
||||
'src' => '',
|
||||
'width' => 0,
|
||||
'height' => 0,
|
||||
];
|
||||
|
||||
if ( imagify_is_screen( 'attachment' ) && wp_attachment_is_image( $post_id ) ) {
|
||||
$process = imagify_get_optimization_process( $post_id, 'wp' );
|
||||
|
||||
if ( $process->is_valid() ) {
|
||||
$media = $process->get_media();
|
||||
|
||||
if ( $media->is_image() ) {
|
||||
$dimensions = $media->get_dimensions();
|
||||
$image = [
|
||||
'src' => $media->get_fullsize_url(),
|
||||
'width' => $dimensions['width'],
|
||||
'height' => $dimensions['height'],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'imageSrc' => $image['src'],
|
||||
'imageWidth' => $image['width'],
|
||||
'imageHeight' => $image['height'],
|
||||
'widthLimit' => 360, // See _imagify_add_actions_to_media_list_row().
|
||||
'labels' => [
|
||||
'filesize' => __( 'File Size:', 'imagify' ),
|
||||
'saving' => __( 'Original Saving:', 'imagify' ),
|
||||
'close' => __( 'Close', 'imagify' ),
|
||||
'originalL' => __( 'Original File', 'imagify' ),
|
||||
'optimizedL' => __( 'Optimized File', 'imagify' ),
|
||||
'compare' => __( 'Compare Original VS Optimized', 'imagify' ),
|
||||
'optimize' => __( 'Optimize', 'imagify' ),
|
||||
],
|
||||
];
|
||||
|
||||
case 'beat':
|
||||
return Core::get_instance()->get_settings();
|
||||
|
||||
case 'media-modal':
|
||||
return [
|
||||
'imagifybeatID' => $imagifybeat_actions->get_imagifybeat_id( 'library_optimization_status' ),
|
||||
];
|
||||
|
||||
case 'library':
|
||||
return [
|
||||
'backupOption' => get_imagify_option( 'backup' ),
|
||||
'labels' => [
|
||||
'bulkActionsOptimize' => __( 'Optimize', 'imagify' ),
|
||||
'bulkActionsOptimizeMissingSizes' => __( 'Optimize Missing Sizes', 'imagify' ),
|
||||
'bulkActionsRestore' => __( 'Restore Original', 'imagify' ),
|
||||
],
|
||||
];
|
||||
|
||||
case 'bulk':
|
||||
$translations = [
|
||||
'curlMissing' => ! Imagify_Requirements::supports_curl(),
|
||||
'editorMissing' => ! Imagify_Requirements::supports_image_editor(),
|
||||
'extHttpBlocked' => Imagify_Requirements::is_imagify_blocked(),
|
||||
'apiDown' => Imagify_Requirements::is_imagify_blocked() || ! Imagify_Requirements::is_api_up(),
|
||||
'keyIsValid' => ! Imagify_Requirements::is_imagify_blocked() && Imagify_Requirements::is_api_up() && Imagify_Requirements::is_api_key_valid(),
|
||||
'isOverQuota' => ! Imagify_Requirements::is_imagify_blocked() && Imagify_Requirements::is_api_up() && Imagify_Requirements::is_api_key_valid() && Imagify_Requirements::is_over_quota(),
|
||||
'imagifybeatIDs' => [
|
||||
'stats' => $imagifybeat_actions->get_imagifybeat_id( 'bulk_optimization_stats' ),
|
||||
'queue' => $imagifybeat_actions->get_imagifybeat_id( 'bulk_optimization_status' ),
|
||||
'requirements' => $imagifybeat_actions->get_imagifybeat_id( 'requirements' ),
|
||||
],
|
||||
'waitImageUrl' => IMAGIFY_ASSETS_IMG_URL . 'popin-loader.svg',
|
||||
'ajaxActions' => [
|
||||
'bulkProcess' => 'imagify_bulk_optimize',
|
||||
'getFolderData' => 'imagify_get_folder_type_data',
|
||||
'bulkInfoSeen' => 'imagify_bulk_info_seen',
|
||||
],
|
||||
'ajaxNonce' => wp_create_nonce( 'imagify-bulk-optimize' ),
|
||||
'bufferSizes' => [
|
||||
'wp' => 4,
|
||||
'custom-folders' => 4,
|
||||
],
|
||||
'optimizing' => get_transient( 'imagify_custom-folders_optimize_running' ) || get_transient( 'imagify_wp_optimize_running' ),
|
||||
'labels' => [
|
||||
'overviewChartLabels' => [
|
||||
'unoptimized' => __( 'Unoptimized', 'imagify' ),
|
||||
'optimized' => __( 'Optimized', 'imagify' ),
|
||||
'error' => __( 'Error', 'imagify' ),
|
||||
],
|
||||
'curlMissing' => __( 'cURL is not available on the server.', 'imagify' ),
|
||||
'editorMissing' => sprintf(
|
||||
/* translators: %s is a "More info?" link. */
|
||||
__( 'No php extensions are available to edit images on the server. ImageMagick or GD is required. %s', 'imagify' ),
|
||||
'<a href="' . esc_url( imagify_get_external_url( 'documentation-imagick-gd' ) ) . '" target="_blank">' . __( 'More info?', 'imagify' ) . '</a>'
|
||||
),
|
||||
'extHttpBlocked' => __( 'External HTTP requests are blocked.', 'imagify' ),
|
||||
'apiDown' => __( 'Sorry, our servers are temporarily unavailable. Please, try again in a couple of minutes.', 'imagify' ),
|
||||
'invalidAPIKeyTitle' => __( 'Your API key is not valid!', 'imagify' ),
|
||||
'overQuotaTitle' => __( 'You have used all your credits!', 'imagify' ),
|
||||
'waitTitle' => __( 'Please wait...', 'imagify' ),
|
||||
'waitText' => __( 'We are trying to get your unoptimized media files, it may take time depending on the number of files.', 'imagify' ),
|
||||
'nothingToDoTitle' => __( 'Hold on!', 'imagify' ),
|
||||
'nothingToDoText' => [
|
||||
'optimize' => __( 'All your media files have been optimized by Imagify. Congratulations!', 'imagify' ),
|
||||
'generate_webp' => __( 'All your optimized images already have a next-gen version. Congratulations!', 'imagify' ),
|
||||
],
|
||||
'optimizing' => __( 'Optimizing', 'imagify' ),
|
||||
'error' => __( 'Error', 'imagify' ),
|
||||
'ajaxErrorText' => __( 'The operation failed.', 'imagify' ),
|
||||
'complete' => _x( 'Complete', 'adjective', 'imagify' ),
|
||||
'alreadyOptimized' => _x( 'Already Optimized', 'file', 'imagify' ),
|
||||
/* translators: %s is a number. Don't use %d. */
|
||||
'nbrFiles' => __( '%s file(s)', 'imagify' ),
|
||||
'notice' => _x( 'Notice', 'noun', 'imagify' ),
|
||||
/* translators: %s is a number. Don't use %d. */
|
||||
'nbrErrors' => __( '%s error(s)', 'imagify' ),
|
||||
'getUnoptimizedImagesErrorTitle' => __( 'Oops, There is something wrong!', 'imagify' ),
|
||||
'getUnoptimizedImagesErrorText' => __( 'An unknown error occurred when we tried to get all your unoptimized media files. Try again and if the issue still persists, please contact us!', 'imagify' ),
|
||||
'waitingOtimizationsText' => __( 'Waiting other optimizations to finish.', 'imagify' ),
|
||||
/* translators: %s is a formatted number, dont use %d. */
|
||||
'imagesOptimizedText' => __( '%s Media File(s) Optimized', 'imagify' ),
|
||||
/* translators: %s is a formatted number, dont use %d. */
|
||||
'imagesErrorText' => __( '%s Error(s)', 'imagify' ),
|
||||
'bulkInfoTitle' => __( 'Information', 'imagify' ),
|
||||
'confirmBulk' => __( 'Start the optimization', 'imagify' ),
|
||||
],
|
||||
];
|
||||
|
||||
if ( get_transient( 'imagify_large_library' ) ) {
|
||||
// On huge media libraries, don't use Imagifybeat, and fetch stats only when the process ends.
|
||||
$translations['ajaxActions']['getStats'] = 'imagify_bulk_get_stats';
|
||||
}
|
||||
|
||||
if ( isset( $translations['bufferSizes']['wp'] ) ) {
|
||||
/**
|
||||
* Filter the number of parallel queries during the Bulk Optimization (library).
|
||||
*
|
||||
* @since 1.5.4
|
||||
* @since 1.7 Deprecated
|
||||
* @deprecated
|
||||
*
|
||||
* @param int $buffer_size Number of parallel queries.
|
||||
*/
|
||||
$translations['bufferSizes']['wp'] = apply_filters_deprecated( 'imagify_bulk_buffer_size', [ $translations['bufferSizes']['wp'] ], '1.7', 'imagify_bulk_buffer_sizes' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the number of parallel queries during the Bulk Optimization.
|
||||
*
|
||||
* @since 1.7
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $buffer_sizes An array of number of parallel queries, keyed by context.
|
||||
*/
|
||||
$translations['bufferSizes'] = apply_filters( 'imagify_bulk_buffer_sizes', $translations['bufferSizes'] );
|
||||
|
||||
return $translations;
|
||||
|
||||
case 'files-list':
|
||||
return [
|
||||
'backupOption' => get_imagify_option( 'backup' ),
|
||||
'context' => 'custom-folders',
|
||||
'imagifybeatID' => $imagifybeat_actions->get_imagifybeat_id( 'custom_folders_optimization_status' ),
|
||||
'labels' => [
|
||||
'bulkActionsOptimize' => __( 'Optimize', 'imagify' ),
|
||||
'bulkActionsRestore' => __( 'Restore Original', 'imagify' ),
|
||||
],
|
||||
];
|
||||
|
||||
default:
|
||||
return [];
|
||||
} // End switch().
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Trigger a hook that should happen before a media is deleted.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param ProcessInterface $process An optimization process.
|
||||
*/
|
||||
function imagify_trigger_delete_media_hook( $process ) {
|
||||
/**
|
||||
* Triggered bifore a media is deleted.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param ProcessInterface $process An optimization process.
|
||||
*/
|
||||
do_action( 'imagify_delete_media', $process );
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
<?php
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* A wrapper to easily get imagify option.
|
||||
*
|
||||
* @since 1.0
|
||||
* @since 1.7 The $default parameter was removed.
|
||||
*
|
||||
* @param string $key The option name.
|
||||
* @return mixed The option value.
|
||||
*/
|
||||
function get_imagify_option( $key ) {
|
||||
return Imagify_Options::get_instance()->get( $key );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an Imagify option.
|
||||
*
|
||||
* @since 1.6
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $key The option name.
|
||||
* @param mixed $value The value of the option.
|
||||
*/
|
||||
function update_imagify_option( $key, $value ) {
|
||||
Imagify_Options::get_instance()->set( $key, $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Autoload network options and put them in cache.
|
||||
*
|
||||
* @since 1.7
|
||||
* @author Grégory Viguier
|
||||
* @source SecuPress
|
||||
*
|
||||
* @param array $option_names A list of option names to cache.
|
||||
* @param string|array $prefixes A prefix for the option names. Handy for transients for example (`_site_transient_` and `_site_transient_timeout_`).
|
||||
*/
|
||||
function imagify_load_network_options( $option_names, $prefixes = '' ) {
|
||||
global $wpdb;
|
||||
|
||||
$prefixes = (array) $prefixes;
|
||||
|
||||
if ( ! $option_names || count( $option_names ) * count( $prefixes ) === 1 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get values.
|
||||
$not_exist = array();
|
||||
$option_names = array_flip( array_flip( $option_names ) );
|
||||
$options = '';
|
||||
|
||||
foreach ( $prefixes as $prefix ) {
|
||||
$options .= "'$prefix" . implode( "','$prefix", esc_sql( $option_names ) ) . "',";
|
||||
}
|
||||
|
||||
$options = rtrim( $options, ',' );
|
||||
|
||||
if ( is_multisite() ) {
|
||||
$network_id = function_exists( 'get_current_network_id' ) ? get_current_network_id() : (int) $wpdb->siteid;
|
||||
$cache_prefix = "$network_id:";
|
||||
$notoptions_key = "$network_id:notoptions";
|
||||
$cache_group = 'site-options';
|
||||
$results = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key as name, meta_value as value FROM $wpdb->sitemeta WHERE meta_key IN ( $options ) AND site_id = %d", $network_id ), OBJECT_K ); // WPCS: unprepared SQL ok.
|
||||
} else {
|
||||
$cache_prefix = '';
|
||||
$notoptions_key = 'notoptions';
|
||||
$cache_group = 'options';
|
||||
$results = $wpdb->get_results( "SELECT option_name as name, option_value as value FROM $wpdb->options WHERE option_name IN ( $options )", OBJECT_K ); // WPCS: unprepared SQL ok.
|
||||
}
|
||||
|
||||
foreach ( $prefixes as $prefix ) {
|
||||
foreach ( $option_names as $option_name ) {
|
||||
$option_name = $prefix . $option_name;
|
||||
|
||||
if ( isset( $results[ $option_name ] ) ) {
|
||||
// Cache the value.
|
||||
$value = $results[ $option_name ]->value;
|
||||
$value = maybe_unserialize( $value );
|
||||
wp_cache_set( "$cache_prefix$option_name", $value, $cache_group );
|
||||
} else {
|
||||
// No value.
|
||||
$not_exist[ $option_name ] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $not_exist ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Cache the options that don't exist in the DB.
|
||||
$notoptions = wp_cache_get( $notoptions_key, $cache_group );
|
||||
$notoptions = is_array( $notoptions ) ? $notoptions : array();
|
||||
$notoptions = array_merge( $notoptions, $not_exist );
|
||||
|
||||
wp_cache_set( $notoptions_key, $notoptions, $cache_group );
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Get the partner ID stored in the database.
|
||||
*
|
||||
* @since 1.6.14
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string|bool The partner ID. False otherwise.
|
||||
*/
|
||||
function imagify_get_partner() {
|
||||
if ( class_exists( 'Imagify_Partner' ) ) {
|
||||
return Imagify_Partner::get_stored_partner();
|
||||
}
|
||||
|
||||
$partner = get_option( 'imagifyp_id' );
|
||||
|
||||
if ( $partner && is_string( $partner ) ) {
|
||||
$partner = preg_replace( '@[^a-z0-9_-]@', '', strtolower( $partner ) );
|
||||
}
|
||||
|
||||
return $partner ? $partner : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the partner ID stored in the database.
|
||||
*
|
||||
* @since 1.6.14
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
function imagify_delete_partner() {
|
||||
if ( class_exists( 'Imagify_Partner' ) ) {
|
||||
Imagify_Partner::delete_stored_partner();
|
||||
} elseif ( false !== get_option( 'imagifyp_id' ) ) {
|
||||
delete_option( 'imagifyp_id' );
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
<?php
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Run an async job to optimize images in background.
|
||||
*
|
||||
* @param array $body Contains the usual $_POST.
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
function imagify_do_async_job( $body ) {
|
||||
$args = [
|
||||
'timeout' => 0.01,
|
||||
'blocking' => false,
|
||||
'body' => $body,
|
||||
'cookies' => isset( $_COOKIE ) && is_array( $_COOKIE ) ? $_COOKIE : [],
|
||||
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
|
||||
];
|
||||
|
||||
/**
|
||||
* Filter the arguments used to launch an async job.
|
||||
*
|
||||
* @since 1.6.6
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $args An array of arguments passed to wp_remote_post().
|
||||
*/
|
||||
$args = apply_filters( 'imagify_do_async_job_args', $args );
|
||||
|
||||
/**
|
||||
* It can be a XML-RPC request. The problem is that XML-RPC doesn't use cookies.
|
||||
*/
|
||||
if ( defined( 'XMLRPC_REQUEST' ) && get_current_user_id() ) {
|
||||
/**
|
||||
* In old WP versions, the field "option_name" in the wp_options table was limited to 64 characters.
|
||||
* From 64, remove 19 characters for "_transient_timeout_" = 45.
|
||||
* Then remove 12 characters for "imagify_rpc_" (transient name) = 33.
|
||||
* Luckily, a md5 is 32 characters long.
|
||||
*/
|
||||
$rpc_id = md5( maybe_serialize( $body ) );
|
||||
|
||||
// Send the request to our RPC bridge instead.
|
||||
$args['body']['imagify_rpc_action'] = $args['body']['action'];
|
||||
$args['body']['action'] = 'imagify_rpc';
|
||||
$args['body']['imagify_rpc_id'] = $rpc_id;
|
||||
$args['body']['imagify_rpc_nonce'] = wp_create_nonce( 'imagify_rpc_' . $rpc_id );
|
||||
|
||||
// Since we can't send cookies to keep the user logged in, store the user ID in a transient.
|
||||
set_transient( 'imagify_rpc_' . $rpc_id, get_current_user_id(), 30 );
|
||||
}
|
||||
|
||||
$url = admin_url( 'admin-ajax.php' );
|
||||
|
||||
/**
|
||||
* Filter the URL to use for async jobs.
|
||||
*
|
||||
* @since 1.9.5
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $url An URL.
|
||||
* @param array $args An array of arguments passed to wp_remote_post().
|
||||
*/
|
||||
$url = apply_filters( 'imagify_async_job_url', $url, $args );
|
||||
|
||||
wp_remote_post( $url, $args );
|
||||
}
|
||||
Reference in New Issue
Block a user