Plugin Updates
This commit is contained in:
@@ -17,15 +17,15 @@ class Bulk {
|
||||
*/
|
||||
public function init() {
|
||||
add_action( 'imagify_optimize_media', [ $this, 'optimize_media' ], 10, 3 );
|
||||
add_action( 'imagify_convert_webp', [ $this, 'generate_webp_versions' ], 10, 2 );
|
||||
add_action( 'imagify_convert_webp_finished', [ $this, 'clear_webp_transients' ], 10, 2 );
|
||||
add_action( 'imagify_convert_next_gen', [ $this, 'generate_nextgen_versions' ], 10, 2 );
|
||||
add_action( 'wp_ajax_imagify_bulk_optimize', [ $this, 'bulk_optimize_callback' ] );
|
||||
add_action( 'wp_ajax_imagify_missing_webp_generation', [ $this, 'missing_webp_callback' ] );
|
||||
add_action( 'wp_ajax_imagify_missing_nextgen_generation', [ $this, 'missing_nextgen_callback' ] );
|
||||
add_action( 'wp_ajax_imagify_get_folder_type_data', [ $this, 'get_folder_type_data_callback' ] );
|
||||
add_action( 'wp_ajax_imagify_bulk_info_seen', [ $this, 'bulk_info_seen_callback' ] );
|
||||
add_action( 'wp_ajax_imagify_bulk_get_stats', [ $this, 'bulk_get_stats_callback' ] );
|
||||
add_action( 'imagify_after_optimize', [ $this, 'check_optimization_status' ], 10, 2 );
|
||||
add_action( 'imagify_deactivation', [ $this, 'delete_transients_data' ] );
|
||||
add_action( 'update_option_imagify_settings', [ $this, 'maybe_generate_missing_nextgen' ], 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,7 +37,7 @@ class Bulk {
|
||||
delete_transient( 'imagify_custom-folders_optimize_running' );
|
||||
delete_transient( 'imagify_wp_optimize_running' );
|
||||
delete_transient( 'imagify_bulk_optimization_complete' );
|
||||
delete_transient( 'imagify_missing_webp_total' );
|
||||
delete_transient( 'imagify_missing_next_gen_total' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -173,17 +173,31 @@ class Bulk {
|
||||
'message' => 'over-quota',
|
||||
];
|
||||
}
|
||||
$formats = imagify_nextgen_images_formats();
|
||||
$media_ids = [
|
||||
'ids' => [],
|
||||
'errors' => [
|
||||
'no_file_path' => [],
|
||||
'no_backup' => [],
|
||||
],
|
||||
];
|
||||
foreach ( $formats as $format ) {
|
||||
$result = $this->get_bulk_instance( $context )->get_optimized_media_ids_without_format( $format );
|
||||
$media_ids['ids'] = array_merge( $media_ids['ids'], $result['ids'] );
|
||||
}
|
||||
$get_unoptimized_media_ids = $this->get_bulk_instance( $context )->get_unoptimized_media_ids( $optimization_level );
|
||||
|
||||
$media_ids = $this->get_bulk_instance( $context )->get_unoptimized_media_ids( $optimization_level );
|
||||
$media_ids['ids'] = array_merge( $media_ids['ids'], $get_unoptimized_media_ids );
|
||||
|
||||
if ( empty( $media_ids ) ) {
|
||||
if ( empty( $media_ids['ids'] ) ) {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'no-images',
|
||||
];
|
||||
}
|
||||
$media_ids['ids'] = array_unique( $media_ids['ids'] );
|
||||
|
||||
foreach ( $media_ids as $media_id ) {
|
||||
foreach ( $media_ids['ids'] as $media_id ) {
|
||||
try {
|
||||
as_enqueue_async_action(
|
||||
'imagify_optimize_media',
|
||||
@@ -213,13 +227,14 @@ class Bulk {
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the WebP generation
|
||||
* Runs the next-gen generation
|
||||
*
|
||||
* @param array $contexts An array of contexts (WP/Custom folders).
|
||||
* @param array $formats An array of format to generate.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function run_generate_webp( array $contexts ) {
|
||||
public function run_generate_nextgen( array $contexts, array $formats ) {
|
||||
if ( ! $this->can_optimize() ) {
|
||||
return [
|
||||
'success' => false,
|
||||
@@ -227,28 +242,29 @@ class Bulk {
|
||||
];
|
||||
}
|
||||
|
||||
delete_transient( 'imagify_stat_without_webp' );
|
||||
delete_transient( 'imagify_stat_without_next_gen' );
|
||||
|
||||
$medias = [];
|
||||
|
||||
foreach ( $contexts as $context ) {
|
||||
$media = $this->get_bulk_instance( $context )->get_optimized_media_ids_without_webp();
|
||||
foreach ( $formats as $format ) {
|
||||
$media = $this->get_bulk_instance( $context )->get_optimized_media_ids_without_format( $format );
|
||||
if ( ! $media['ids'] && $media['errors']['no_backup'] ) {
|
||||
// No backup, no next-gen.
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'no-backup',
|
||||
];
|
||||
} elseif ( ! $media['ids'] && $media['errors']['no_file_path'] ) {
|
||||
// Error.
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => __( 'The path to the selected files could not be retrieved.', 'imagify' ),
|
||||
];
|
||||
}
|
||||
|
||||
if ( ! $media['ids'] && $media['errors']['no_backup'] ) {
|
||||
// No backup, no WebP.
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'no-backup',
|
||||
];
|
||||
} elseif ( ! $media['ids'] && $media['errors']['no_file_path'] ) {
|
||||
// Error.
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => __( 'The path to the selected files could not be retrieved.', 'imagify' ),
|
||||
];
|
||||
$medias[ $context ] = $media['ids'];
|
||||
}
|
||||
|
||||
$medias[ $context ] = $media['ids'];
|
||||
}
|
||||
|
||||
if ( empty( $medias ) ) {
|
||||
@@ -266,12 +282,12 @@ class Bulk {
|
||||
foreach ( $media_ids as $media_id ) {
|
||||
try {
|
||||
as_enqueue_async_action(
|
||||
'imagify_convert_webp',
|
||||
'imagify_convert_next_gen',
|
||||
[
|
||||
'id' => $media_id,
|
||||
'context' => $context,
|
||||
],
|
||||
"imagify-{$context}-convert-webp"
|
||||
"imagify-{$context}-convert-nextgen"
|
||||
);
|
||||
} catch ( Exception $exception ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
|
||||
// nothing to do.
|
||||
@@ -279,7 +295,7 @@ class Bulk {
|
||||
}
|
||||
}
|
||||
|
||||
set_transient( 'imagify_missing_webp_total', $total, HOUR_IN_SECONDS );
|
||||
set_transient( 'imagify_missing_next_gen_total', $total, HOUR_IN_SECONDS );
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
@@ -310,13 +326,13 @@ class Bulk {
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the name of the class to use for bulk process.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param int $class_name The class name.
|
||||
* @param string $context The context name.
|
||||
*/
|
||||
* Filter the name of the class to use for bulk process.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param int $class_name The class name.
|
||||
* @param string $context The context name.
|
||||
*/
|
||||
$class_name = apply_filters( 'imagify_bulk_class_name', $class_name, $context );
|
||||
|
||||
return '\\' . ltrim( $class_name, '\\' );
|
||||
@@ -374,7 +390,7 @@ class Bulk {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate WebP images if they are missing.
|
||||
* Generate next-gen images if they are missing.
|
||||
*
|
||||
* @since 2.1
|
||||
*
|
||||
@@ -383,12 +399,12 @@ class Bulk {
|
||||
*
|
||||
* @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure.
|
||||
*/
|
||||
public function generate_webp_versions( int $media_id, string $context ) {
|
||||
public function generate_nextgen_versions( int $media_id, string $context ) {
|
||||
if ( ! $this->can_optimize() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return imagify_get_optimization_process( $media_id, $context )->generate_webp_versions();
|
||||
return imagify_get_optimization_process( $media_id, $context )->generate_nextgen_versions();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -451,10 +467,6 @@ class Bulk {
|
||||
return (int) $level;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** BULK OPTIMIZATION CALLBACKS ============================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Launch the bulk optimization action
|
||||
*
|
||||
@@ -480,14 +492,14 @@ class Bulk {
|
||||
}
|
||||
|
||||
/**
|
||||
* Launch the missing WebP versions generation
|
||||
* Launch the missing Next-gen versions generation
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function missing_webp_callback() {
|
||||
public function missing_nextgen_callback() {
|
||||
imagify_check_nonce( 'imagify-bulk-optimize' );
|
||||
|
||||
$contexts = explode( '_', sanitize_key( wp_unslash( $_GET['context'] ) ) );
|
||||
$contexts = $this->get_contexts();
|
||||
|
||||
foreach ( $contexts as $context ) {
|
||||
if ( ! imagify_get_context( $context )->current_user_can( 'bulk-optimize' ) ) {
|
||||
@@ -495,8 +507,9 @@ class Bulk {
|
||||
}
|
||||
}
|
||||
|
||||
$data = $this->run_generate_webp( $contexts );
|
||||
$formats = imagify_nextgen_images_formats();
|
||||
|
||||
$data = $this->run_generate_nextgen( $contexts, $formats );
|
||||
if ( false === $data['success'] ) {
|
||||
wp_send_json_error( [ 'message' => $data['message'] ] );
|
||||
}
|
||||
@@ -575,4 +588,87 @@ class Bulk {
|
||||
|
||||
wp_send_json_success( imagify_get_bulk_stats( array_flip( $folder_types ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update Options callback to start bulk optimization.
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @param array $old_value The old option value.
|
||||
* @param array $value The new option value.
|
||||
*
|
||||
* Please note that the convert_to_avif new value is a checkbox,
|
||||
* so it equals 1 when it's set otherwise it's not set.
|
||||
* That's why we need to use empty function when checking its value.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function maybe_generate_missing_nextgen( $old_value, $value ) {
|
||||
if ( empty( $old_value['convert_to_avif'] ) === empty( $value['convert_to_avif'] ) ) {
|
||||
// Old value = new value so do nothing.
|
||||
return;
|
||||
}
|
||||
|
||||
if ( empty( $value['convert_to_avif'] ) ) {
|
||||
// new value is disabled, do nothing.
|
||||
return;
|
||||
}
|
||||
|
||||
$contexts = $this->get_contexts();
|
||||
$formats = imagify_nextgen_images_formats();
|
||||
|
||||
$this->run_generate_nextgen( $contexts, $formats );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the context for the bulk optimization page.
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @return array The array of unique contexts ('wp' or 'custom-folders').
|
||||
*/
|
||||
public function get_contexts() {
|
||||
$contexts = [];
|
||||
$types = [];
|
||||
|
||||
// Library: in each site.
|
||||
if ( ! is_network_admin() ) {
|
||||
$types['library|wp'] = 1;
|
||||
}
|
||||
|
||||
// Custom folders: in network admin only if network activated, in each site otherwise.
|
||||
if ( imagify_can_optimize_custom_folders() && ( imagify_is_active_for_network() && is_network_admin() || ! imagify_is_active_for_network() ) ) {
|
||||
$types['custom-folders|custom-folders'] = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the types to display in the bulk optimization page.
|
||||
*
|
||||
* @since 1.7.1
|
||||
*
|
||||
* @param array $types The folder types displayed on the page. If a folder type is "library", the context should be suffixed after a pipe character. They are passed as array keys.
|
||||
*/
|
||||
$types = apply_filters( 'imagify_bulk_page_types', $types );
|
||||
$types = array_filter( (array) $types );
|
||||
|
||||
if ( isset( $types['library|wp'] ) && ! in_array( 'wp', $contexts, true ) ) {
|
||||
$contexts[] = 'wp';
|
||||
}
|
||||
|
||||
if ( isset( $types['custom-folders|custom-folders'] ) ) {
|
||||
$folders_instance = \Imagify_Folders_DB::get_instance();
|
||||
|
||||
if ( ! $folders_instance->has_items() ) {
|
||||
// New Feature!
|
||||
if ( ! in_array( 'wp', $contexts, true ) ) {
|
||||
$contexts[] = 'wp';
|
||||
}
|
||||
} elseif ( $folders_instance->has_active_folders() && ! in_array( 'custom-folders', $contexts, true ) ) {
|
||||
$contexts[] = 'custom-folders';
|
||||
}
|
||||
}
|
||||
|
||||
return $contexts;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user