fix
This commit is contained in:
108
wp-content/plugins/robin-image-optimizer/admin/ajax/backup.php
Normal file
108
wp-content/plugins/robin-image-optimizer/admin/ajax/backup.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/**
|
||||
* Back-up related filters.
|
||||
*
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
// Exit if accessed directly
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX обработчик восстановления из резервной копии
|
||||
*/
|
||||
add_action(
|
||||
'wp_ajax_wio_restore_backup',
|
||||
function () {
|
||||
check_admin_referer( 'wio-iph' );
|
||||
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( - 1 );
|
||||
}
|
||||
|
||||
$max_process_per_request = 25;
|
||||
|
||||
// $blog_id = WRIO_Plugin::app()->request->post( 'blog_id', null, true );
|
||||
|
||||
/*
|
||||
if ( $blog_id !== null ) {
|
||||
switch_to_blog( $blog_id );
|
||||
}*/
|
||||
|
||||
// Total number of remained images to restore
|
||||
$remane_count = 0;
|
||||
|
||||
$total = 0;
|
||||
|
||||
$filter_results = apply_filters( 'wbcr/rio/backup/restore_filter', $max_process_per_request );
|
||||
|
||||
if ( isset( $filter_results['remane'] ) ) {
|
||||
$remane_count += $filter_results['remane'];
|
||||
}
|
||||
|
||||
if ( isset( $filter_results['total'] ) ) {
|
||||
$total += $filter_results['total'];
|
||||
}
|
||||
|
||||
$media_library = WRIO_Media_Library::get_instance();
|
||||
|
||||
$total += $media_library->getOptimizedCount();
|
||||
|
||||
$restored_data = $media_library->restoreAllFromBackup( $max_process_per_request );
|
||||
|
||||
if ( isset( $restored_data['remain'] ) ) {
|
||||
$remane_count += $restored_data['remain'];
|
||||
}
|
||||
|
||||
/*
|
||||
if ( $blog_id !== null ) {
|
||||
restore_current_blog();
|
||||
}*/
|
||||
|
||||
$restored_data['total'] = $total;
|
||||
|
||||
if ( $total > 0 ) {
|
||||
$restored_data['percent'] = 100 - ( $remane_count * 100 / $total );
|
||||
} else {
|
||||
$restored_data['percent'] = 0;
|
||||
}
|
||||
|
||||
// если изображения закончились - посылаем команду завершения
|
||||
if ( $remane_count <= 0 ) {
|
||||
$restored_data['end'] = true;
|
||||
}
|
||||
|
||||
wp_send_json( $restored_data );
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* AJAX обработчик очистки папки с бекапами
|
||||
*/
|
||||
add_action(
|
||||
'wp_ajax_wio_clear_backup',
|
||||
function () {
|
||||
check_admin_referer( 'wio-iph' );
|
||||
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( - 1 );
|
||||
}
|
||||
|
||||
$backup = WIO_Backup::get_instance();
|
||||
$blogs = WRIO_Plugin::app()->request->post( 'blogs', [], true );
|
||||
|
||||
if ( ! empty( $blogs ) ) {
|
||||
foreach ( $blogs as $blog_id ) {
|
||||
switch_to_blog( intval( $blog_id ) );
|
||||
$backup->removeBlogBackupDir();
|
||||
restore_current_blog();
|
||||
}
|
||||
} else {
|
||||
$backup->removeBackupDir();
|
||||
}
|
||||
|
||||
wp_send_json( true );
|
||||
}
|
||||
);
|
||||
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
// silence is golden
|
||||
31
wp-content/plugins/robin-image-optimizer/admin/ajax/logs.php
Normal file
31
wp-content/plugins/robin-image-optimizer/admin/ajax/logs.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Used to clean-up logs.
|
||||
*/
|
||||
add_action(
|
||||
'wp_ajax_wrio_logs_cleanup',
|
||||
function () {
|
||||
check_admin_referer( 'wrio_clean_logs', 'nonce' );
|
||||
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( - 1 );
|
||||
}
|
||||
|
||||
if ( ! WRIO_Plugin::app()->logger->clean_up() ) {
|
||||
wp_send_json_error(
|
||||
[
|
||||
'message' => esc_html__( 'Failed to clear logs. Please try again later.', 'robin-image-optimizer' ),
|
||||
'type' => 'danger',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
wp_send_json(
|
||||
[
|
||||
'message' => esc_html__( 'Logs cleared successfully.', 'robin-image-optimizer' ),
|
||||
'type' => 'success',
|
||||
]
|
||||
);
|
||||
}
|
||||
);
|
||||
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
/**
|
||||
* Ajax action to migrate old architecture based on post meta into new table.
|
||||
*
|
||||
* @see RIO_Process_Queue for further information.
|
||||
*
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
// Exit if accessed directly
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
add_action( 'wp_ajax_wrio_meta_migrations', 'wbcr_rio_migrate_postmeta_to_process_queue' );
|
||||
|
||||
/**
|
||||
* Migrating postmeta to newly created table.
|
||||
*
|
||||
* @since 1.3.0
|
||||
* @see RIO_Process_Queue as referce for new table.
|
||||
*/
|
||||
function wbcr_rio_migrate_postmeta_to_process_queue() {
|
||||
global $wpdb;
|
||||
|
||||
check_admin_referer( 'wrio-meta-migrations' );
|
||||
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( - 1 );
|
||||
}
|
||||
|
||||
$error = (int) WRIO_Plugin::app()->request->post( 'error', 0 );
|
||||
|
||||
if ( $error ) {
|
||||
WRIO_Plugin::app()->logger->error( 'Previous migration was not completed due to an error.' );
|
||||
}
|
||||
|
||||
$limit = (int) WRIO_Plugin::app()->request->post( 'limit', 150 );
|
||||
|
||||
$processed_items = 0;
|
||||
|
||||
WRIO_Plugin::app()->logger->info( 'Start meta migration. Limit ' . $limit );
|
||||
WRIO_Plugin::app()->logger->memory_usage();
|
||||
|
||||
$attachments = wbcr_rio_get_meta_to_migrate();
|
||||
|
||||
if ( isset( $attachments->posts ) && ( $attachments_total = count( $attachments->posts ) ) > 0 ) {
|
||||
|
||||
if ( $attachments_total < $limit ) {
|
||||
$limit = $attachments_total;
|
||||
}
|
||||
|
||||
WRIO_Plugin::app()->logger->info( 'Finded ' . $attachments_total . ' attachments for migration.' );
|
||||
|
||||
if ( function_exists( 'wp_raise_memory_limit' ) ) {
|
||||
wp_raise_memory_limit( 'image' );
|
||||
}
|
||||
|
||||
WRIO_Plugin::app()->logger->memory_usage();
|
||||
|
||||
/**
|
||||
* @var WP_Post $attachment
|
||||
*/
|
||||
for ( $i = 0; $i < $limit; $i++ ) {
|
||||
$attachment = $attachments->posts[ $i ];
|
||||
$post_meta = get_post_custom( $attachment->ID );
|
||||
|
||||
$extra_data = new RIO_Attachment_Extra_Data();
|
||||
|
||||
$is_backed_up = false;
|
||||
$original_size = 0;
|
||||
$final_size = 0;
|
||||
|
||||
if ( isset( $post_meta['wio_backuped'][0] ) && $post_meta['wio_backuped'][0] ) {
|
||||
$is_backed_up = true;
|
||||
}
|
||||
|
||||
if ( isset( $post_meta['wio_thumbnails_count'][0] ) ) {
|
||||
$extra_data->set_thumbnails_count( intval( $post_meta['wio_thumbnails_count'][0] ) );
|
||||
}
|
||||
|
||||
if ( isset( $post_meta['wio_original_size'][0] ) ) {
|
||||
$original_size = intval( $post_meta['wio_original_size'][0] );
|
||||
}
|
||||
|
||||
if ( isset( $post_meta['wio_optimized_size'][0] ) ) {
|
||||
$final_size = intval( $post_meta['wio_optimized_size'][0] );
|
||||
}
|
||||
|
||||
if ( isset( $post_meta['wio_original_main_size'][0] ) ) {
|
||||
$extra_data->set_original_main_size( intval( $post_meta['wio_original_main_size'][0] ) );
|
||||
}
|
||||
|
||||
if ( isset( $post_meta['wio_error'][0] ) ) {
|
||||
$extra_data->set_error( 'optimization' );
|
||||
$extra_data->set_error_msg( $post_meta['wio_error'][0] );
|
||||
}
|
||||
|
||||
$level = 'normal';
|
||||
|
||||
if ( isset( $post_meta['wio_optimization_level'][0] ) && ! empty( $post_meta['wio_optimization_level'][0] ) ) {
|
||||
$level = $post_meta['wio_optimization_level'][0];
|
||||
}
|
||||
|
||||
$data = [
|
||||
'server_id' => null,
|
||||
'object_id' => $attachment->ID,
|
||||
'object_name' => $wpdb->posts,
|
||||
'item_type' => 'attachment',
|
||||
'result_status' => ! $final_size ? 'error' : 'success',
|
||||
'processing_level' => $level,
|
||||
'is_backed_up' => $is_backed_up,
|
||||
'original_size' => $original_size,
|
||||
'final_size' => $final_size,
|
||||
'original_mime_type' => $attachment->post_mime_type,
|
||||
'final_mime_type' => $attachment->post_mime_type,
|
||||
'extra_data' => (string) $extra_data,
|
||||
'created_at' => time(),
|
||||
];
|
||||
|
||||
$format = [
|
||||
'%s',
|
||||
'%d',
|
||||
'%s',
|
||||
'%s',
|
||||
'%s',
|
||||
'%s',
|
||||
'%d',
|
||||
'%d',
|
||||
'%d',
|
||||
'%s',
|
||||
'%s',
|
||||
'%s',
|
||||
'%d',
|
||||
];
|
||||
|
||||
$rows_inserted = $wpdb->insert( RIO_Process_Queue::table_name(), $data, $format );
|
||||
|
||||
if ( $rows_inserted > 0 ) {
|
||||
++$processed_items;
|
||||
|
||||
$attachment_id = absint( $attachment->ID );
|
||||
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE post_id='{$attachment_id}' AND meta_key LIKE 'wio_%'" );
|
||||
}
|
||||
}
|
||||
|
||||
$left_items = $attachments_total - $processed_items;
|
||||
// translators: %s is the number of items left to migrate
|
||||
$message = sprintf( __( 'left to migrate: %s items', 'robin-image-optimizer' ), $left_items );
|
||||
$need_more_time = true;
|
||||
|
||||
WRIO_Plugin::app()->logger->info( 'Succefull migrated ' . $processed_items . ' items.' );
|
||||
} else {
|
||||
WRIO_Plugin::app()->logger->info( 'Succefull migrated all items. Finishing-up...' );
|
||||
|
||||
// Assumed to be 2 after 010105.php migration
|
||||
RIO_Process_Queue::update_db_version( 2 );
|
||||
|
||||
$need_more_time = false;
|
||||
$message = __( 'Finishing-up...', 'robin-image-optimizer' );
|
||||
}
|
||||
|
||||
WRIO_Plugin::app()->logger->memory_usage();
|
||||
|
||||
wp_send_json_success(
|
||||
[
|
||||
'need_more_time' => $need_more_time,
|
||||
'message' => $message,
|
||||
]
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* Ajax действие, которое выполняется для смены текущего multisite блога
|
||||
*
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
// Exit if accessed directly
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/*
|
||||
add_action( 'wp_ajax_wbcr_rio_update_current_blog', function () {
|
||||
check_ajax_referer( 'update_blog_id', 'wpnonce' );
|
||||
$blog_id = (int) WRIO_Plugin::app()->request->post( 'current_blog_id' );
|
||||
$context = sanitize_text_field( WRIO_Plugin::app()->request->post( 'context' ) );
|
||||
WRIO_Plugin::app()->updatePopulateOption( 'current_blog', $blog_id );
|
||||
$image_statistics = WIO_OptimizationTools::getImageStatistics( $context );
|
||||
|
||||
switch_to_blog( $blog_id );
|
||||
$statistic_data = $image_statistics->load();
|
||||
restore_current_blog();
|
||||
|
||||
wp_send_json_success( array(
|
||||
'statistic' => $statistic_data,
|
||||
) );
|
||||
} );*/
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* Ajax действие, которое выполняется при сохранении настроек
|
||||
*
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
// Exit if accessed directly
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX обработчик массовой сохранения уровня сжатия
|
||||
*/
|
||||
add_action(
|
||||
'wp_ajax_wio_settings_update_level',
|
||||
function () {
|
||||
check_admin_referer( 'wio-iph' );
|
||||
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( - 1 );
|
||||
}
|
||||
|
||||
$level = sanitize_text_field( $_POST['level'] );
|
||||
|
||||
if ( ! $level ) {
|
||||
die();
|
||||
}
|
||||
|
||||
if ( ! in_array( $level, [ 'normal', 'aggresive', 'ultra' ] ) ) {
|
||||
die();
|
||||
}
|
||||
|
||||
WRIO_Plugin::app()->updatePopulateOption( 'image_optimization_level', $level );
|
||||
die();
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user