PluginProbe ʕ •ᴥ•ʔ
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF / 2.0.6
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF v2.0.6
2.0.6 2.0.5 trunk 1.3.7 1.4.0 1.4.1 1.4.2 1.4.6 1.5.0 1.5.3 1.5.6 1.5.8 1.6.5 1.6.6 1.6.9 1.7.0 1.7.4 1.8.1 1.8.2 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4
robin-image-optimizer / admin / ajax / meta-migrations.php
robin-image-optimizer / admin / ajax Last commit date
backup.php 7 months ago index.php 3 years ago logs.php 7 months ago meta-migrations.php 7 months ago multisite-update-current-blog.php 7 months ago settings.php 7 months ago
meta-migrations.php
172 lines
1 <?php
2 /**
3 * Ajax action to migrate old architecture based on post meta into new table.
4 *
5 * @see RIO_Process_Queue for further information.
6 *
7 * @version 1.0
8 */
9
10 // Exit if accessed directly
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 add_action( 'wp_ajax_wrio_meta_migrations', 'wbcr_rio_migrate_postmeta_to_process_queue' );
16
17 /**
18 * Migrating postmeta to newly created table.
19 *
20 * @since 1.3.0
21 * @see RIO_Process_Queue as referce for new table.
22 */
23 function wbcr_rio_migrate_postmeta_to_process_queue() {
24 global $wpdb;
25
26 check_admin_referer( 'wrio-meta-migrations' );
27
28 if ( ! current_user_can( 'manage_options' ) ) {
29 wp_die( - 1 );
30 }
31
32 $error = (int) WRIO_Plugin::app()->request->post( 'error', 0 );
33
34 if ( $error ) {
35 WRIO_Plugin::app()->logger->error( 'Previous migration was not completed due to an error.' );
36 }
37
38 $limit = (int) WRIO_Plugin::app()->request->post( 'limit', 150 );
39
40 $processed_items = 0;
41
42 WRIO_Plugin::app()->logger->info( 'Start meta migration. Limit ' . $limit );
43 WRIO_Plugin::app()->logger->memory_usage();
44
45 $attachments = wbcr_rio_get_meta_to_migrate();
46
47 if ( isset( $attachments->posts ) && ( $attachments_total = count( $attachments->posts ) ) > 0 ) {
48
49 if ( $attachments_total < $limit ) {
50 $limit = $attachments_total;
51 }
52
53 WRIO_Plugin::app()->logger->info( 'Finded ' . $attachments_total . ' attachments for migration.' );
54
55 if ( function_exists( 'wp_raise_memory_limit' ) ) {
56 wp_raise_memory_limit( 'image' );
57 }
58
59 WRIO_Plugin::app()->logger->memory_usage();
60
61 /**
62 * @var WP_Post $attachment
63 */
64 for ( $i = 0; $i < $limit; $i++ ) {
65 $attachment = $attachments->posts[ $i ];
66 $post_meta = get_post_custom( $attachment->ID );
67
68 $extra_data = new RIO_Attachment_Extra_Data();
69
70 $is_backed_up = false;
71 $original_size = 0;
72 $final_size = 0;
73
74 if ( isset( $post_meta['wio_backuped'][0] ) && $post_meta['wio_backuped'][0] ) {
75 $is_backed_up = true;
76 }
77
78 if ( isset( $post_meta['wio_thumbnails_count'][0] ) ) {
79 $extra_data->set_thumbnails_count( intval( $post_meta['wio_thumbnails_count'][0] ) );
80 }
81
82 if ( isset( $post_meta['wio_original_size'][0] ) ) {
83 $original_size = intval( $post_meta['wio_original_size'][0] );
84 }
85
86 if ( isset( $post_meta['wio_optimized_size'][0] ) ) {
87 $final_size = intval( $post_meta['wio_optimized_size'][0] );
88 }
89
90 if ( isset( $post_meta['wio_original_main_size'][0] ) ) {
91 $extra_data->set_original_main_size( intval( $post_meta['wio_original_main_size'][0] ) );
92 }
93
94 if ( isset( $post_meta['wio_error'][0] ) ) {
95 $extra_data->set_error( 'optimization' );
96 $extra_data->set_error_msg( $post_meta['wio_error'][0] );
97 }
98
99 $level = 'normal';
100
101 if ( isset( $post_meta['wio_optimization_level'][0] ) && ! empty( $post_meta['wio_optimization_level'][0] ) ) {
102 $level = $post_meta['wio_optimization_level'][0];
103 }
104
105 $data = [
106 'server_id' => null,
107 'object_id' => $attachment->ID,
108 'object_name' => $wpdb->posts,
109 'item_type' => 'attachment',
110 'result_status' => ! $final_size ? 'error' : 'success',
111 'processing_level' => $level,
112 'is_backed_up' => $is_backed_up,
113 'original_size' => $original_size,
114 'final_size' => $final_size,
115 'original_mime_type' => $attachment->post_mime_type,
116 'final_mime_type' => $attachment->post_mime_type,
117 'extra_data' => (string) $extra_data,
118 'created_at' => time(),
119 ];
120
121 $format = [
122 '%s',
123 '%d',
124 '%s',
125 '%s',
126 '%s',
127 '%s',
128 '%d',
129 '%d',
130 '%d',
131 '%s',
132 '%s',
133 '%s',
134 '%d',
135 ];
136
137 $rows_inserted = $wpdb->insert( RIO_Process_Queue::table_name(), $data, $format );
138
139 if ( $rows_inserted > 0 ) {
140 ++$processed_items;
141
142 $attachment_id = absint( $attachment->ID );
143 $wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE post_id='{$attachment_id}' AND meta_key LIKE 'wio_%'" );
144 }
145 }
146
147 $left_items = $attachments_total - $processed_items;
148 // translators: %s is the number of items left to migrate
149 $message = sprintf( __( 'left to migrate: %s items', 'robin-image-optimizer' ), $left_items );
150 $need_more_time = true;
151
152 WRIO_Plugin::app()->logger->info( 'Succefull migrated ' . $processed_items . ' items.' );
153 } else {
154 WRIO_Plugin::app()->logger->info( 'Succefull migrated all items. Finishing-up...' );
155
156 // Assumed to be 2 after 010105.php migration
157 RIO_Process_Queue::update_db_version( 2 );
158
159 $need_more_time = false;
160 $message = __( 'Finishing-up...', 'robin-image-optimizer' );
161 }
162
163 WRIO_Plugin::app()->logger->memory_usage();
164
165 wp_send_json_success(
166 [
167 'need_more_time' => $need_more_time,
168 'message' => $message,
169 ]
170 );
171 }
172