Changeset 3306643
- Timestamp:
- 06/04/2025 05:28:44 PM (10 months ago)
- Location:
- bubuku-media-library
- Files:
-
- 8 edited
- 1 copied
-
tags/1.1.3 (copied) (copied from bubuku-media-library/trunk)
-
tags/1.1.3/bubuku-media-library.php (modified) (1 diff)
-
tags/1.1.3/readme.txt (modified) (2 diffs)
-
tags/1.1.3/src/BML_db.php (modified) (3 diffs)
-
tags/1.1.3/src/BML_plugin.php (modified) (1 diff)
-
trunk/bubuku-media-library.php (modified) (1 diff)
-
trunk/readme.txt (modified) (2 diffs)
-
trunk/src/BML_db.php (modified) (3 diffs)
-
trunk/src/BML_plugin.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
bubuku-media-library/tags/1.1.3/bubuku-media-library.php
r3299741 r3306643 6 6 * Requires at least: 5.2 7 7 * Requires PHP: 7.2 8 * Version: 1.1. 28 * Version: 1.1.3 9 9 * Author: Bubuku 10 10 * Author URI: https://www.bubuku.com/ -
bubuku-media-library/tags/1.1.3/readme.txt
r3299741 r3306643 5 5 Tested up to: 6.8 6 6 Requires PHP: 7.2 7 Stable tag: 1.1. 27 Stable tag: 1.1.3 8 8 License: GPLv3 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-3.0.html … … 38 38 39 39 == Changelog == 40 = 1.1.3 = 41 * Improve performance and simplify SQL queries. 42 * Implemented caching for the calculated number of images by size and those lacking alt attributes. 43 40 44 = 1.1.2 = 41 45 * Added security and performance improvements -
bubuku-media-library/tags/1.1.3/src/BML_db.php
r3299741 r3306643 44 44 { 45 45 global $wpdb; 46 $result = $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s", '_bkml_attachment_file_size')); 46 47 47 $batch_size = 100; 48 $last_id = 0; 49 $deleted_total = 0; 50 $meta_key = '_bkml_attachment_file_size'; 48 // Clear related cache after deletion 49 wp_cache_delete('bml_posts_by_meta_size', 'bubuku_media_library'); 51 50 52 do { 53 // Intentar obtener los IDs desde la caché primero 54 $cache_key = 'bml_attachment_ids_' . $last_id . '_' . $batch_size; 55 $attachment_ids = wp_cache_get($cache_key, 'bml_attachment_ids'); 56 57 if (false === $attachment_ids) { 58 // La caché está vacía, realizar la consulta a la base de datos 59 $attachment_ids = $wpdb->get_col( 60 $wpdb->prepare( 61 " 62 SELECT DISTINCT p.ID 63 FROM {$wpdb->posts} p 64 INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id 65 WHERE p.post_type = 'attachment' 66 AND pm.meta_key = %s 67 AND p.ID > %d 68 ORDER BY p.ID ASC 69 LIMIT %d 70 ", 71 $meta_key, 72 $last_id, 73 $batch_size 74 ) 75 ); 76 77 // Guardar en caché por 5 minutos (300 segundos) 78 wp_cache_set($cache_key, $attachment_ids, 'bml_attachment_ids', 300); 79 } 80 81 if (!empty($attachment_ids)) { 82 foreach ($attachment_ids as $attachment_id) { 83 // Usamos delete_post_meta que ya gestiona la caché internamente 84 $deleted = delete_post_meta($attachment_id, $meta_key); 85 if ($deleted) { 86 $deleted_total++; 87 } 88 89 // Actualizamos el último ID procesado 90 $last_id = $attachment_id; 91 } 92 } 93 } while (!empty($attachment_ids)); 94 95 return $deleted_total; 51 return $result !== false; 96 52 } 97 53 98 54 /** 99 * Counts posts by meta size using batched WP_Query55 * Counts posts by meta size using direct SQL query 100 56 * 101 57 * @param string $meta_key The meta key to search for … … 106 62 public function count_posts_by_meta_size($meta_key, $min_value, $max_value) 107 63 { 108 $count = 0; 109 $batch_size = 1000; // We can use a larger batch since we're only counting 110 $offset = 0; 64 global $wpdb; 111 65 112 while (true) { 113 $args = array( 114 'post_type' => 'attachment', 115 'post_status' => 'inherit', 116 'fields' => 'ids', 117 'posts_per_page' => $batch_size, 118 'offset' => $offset, 119 'no_found_rows' => true, // Improves performance when we don't need pagination 120 'meta_query' => array( 121 array( 122 'key' => $meta_key, 123 'value' => array($min_value, $max_value), 124 'type' => 'NUMERIC', 125 'compare' => 'BETWEEN', 126 ), 127 ), 128 ); 66 // Create cache key based on parameters 67 $cache_key = sprintf('count_meta_%s_%d_%d', sanitize_key($meta_key), $min_value, $max_value); 68 $cached_result = wp_cache_get($cache_key, 'bubuku_media_library'); 129 69 130 // If it's the largest range, we change the comparison 131 if ($max_value === PHP_INT_MAX) { 132 $args['meta_query'][0]['value'] = $min_value; 133 $args['meta_query'][0]['compare'] = '>='; 134 } 70 if ($cached_result !== false) { 71 return (int) $cached_result; 72 } 135 73 136 $query = new \WP_Query($args); 137 $batch_posts = $query->posts; 74 // If it's the largest range, use >= comparison 75 if ($max_value === PHP_INT_MAX) { 76 $count = (int) $wpdb->get_var($wpdb->prepare(" 77 SELECT COUNT(DISTINCT p.ID) 78 FROM {$wpdb->posts} p 79 INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id 80 WHERE p.post_type = 'attachment' 81 AND p.post_status = 'inherit' 82 AND pm.meta_key = %s 83 AND CAST(pm.meta_value AS UNSIGNED) >= %d 84 ", $meta_key, $min_value)); 85 } else { 86 $count = (int) $wpdb->get_var($wpdb->prepare(" 87 SELECT COUNT(DISTINCT p.ID) 88 FROM {$wpdb->posts} p 89 INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id 90 WHERE p.post_type = 'attachment' 91 AND p.post_status = 'inherit' 92 AND pm.meta_key = %s 93 AND CAST(pm.meta_value AS UNSIGNED) BETWEEN %d AND %d 94 ", $meta_key, $min_value, $max_value)); 95 } 138 96 139 // If there are no results, we finish 140 if (empty($batch_posts)) { 141 break; 142 } 143 144 // Increment the counter with the number of posts in this batch 145 $count += count($batch_posts); 146 147 // Move to the next batch 148 $offset += $batch_size; 149 } 97 // Cache the result for 1 hour 98 wp_cache_set($cache_key, $count, 'bubuku_media_library', HOUR_IN_SECONDS); 150 99 151 100 return $count; … … 159 108 public function calculate_img_alt_empty() 160 109 { 161 $count = 0; 162 $batch_size = 100; 163 $offset = 0; 110 global $wpdb; 164 111 165 while (true) { 166 $args = array( 167 'post_type' => 'attachment', 168 'post_mime_type' => 'image', 169 'post_status' => 'inherit', 170 'fields' => 'ids', 171 'posts_per_page' => $batch_size, 172 'offset' => $offset, 173 'no_found_rows' => true, 174 'meta_query' => array( 175 array( 176 'key' => '_wp_attachment_image_alt', 177 'compare' => 'NOT EXISTS' 178 ) 179 ) 180 ); 112 $cache_key = 'img_alt_empty_count'; 113 $cached_result = wp_cache_get($cache_key, 'bubuku_media_library'); 181 114 182 $query = new \WP_Query($args); 183 $attachment_ids = $query->posts; 184 185 if (empty($attachment_ids)) { 186 break; 187 } 188 189 $count += count($attachment_ids); 190 191 $offset += $batch_size; 115 if ($cached_result !== false) { 116 return number_format_i18n((int) $cached_result); 192 117 } 193 118 194 return number_format_i18n($count); 119 $count = $wpdb->get_var($wpdb->prepare(" 120 SELECT COUNT(*) 121 FROM {$wpdb->posts} 122 WHERE post_type = %s 123 AND post_status = %s 124 AND post_mime_type LIKE %s 125 AND ID NOT IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s) 126 ", 'attachment', 'inherit', 'image/%', '_wp_attachment_image_alt')); 127 128 // Cache the result for 1 hour 129 wp_cache_set($cache_key, $count, 'bubuku_media_library', HOUR_IN_SECONDS); 130 131 return number_format_i18n((int)$count); 195 132 } 196 133 } -
bubuku-media-library/tags/1.1.3/src/BML_plugin.php
r3299741 r3306643 26 26 define('BUBUKU_BML_PLUGIN_ASSETS_URL', BUBUKU_BML_PLUGIN_URL . '/assets'); 27 27 define('BUBUKU_BML_PLUGIN_ENDPOINTS_URL', 'bbk_medialibrary/v1'); 28 define('BUBUKU_BML_PLUGIN_VERSION', '1.1. 2');28 define('BUBUKU_BML_PLUGIN_VERSION', '1.1.3'); 29 29 define('BUBUKU_BML_PLUGIN_NONCE', wp_create_nonce('media-library/v1')); 30 30 -
bubuku-media-library/trunk/bubuku-media-library.php
r3299741 r3306643 6 6 * Requires at least: 5.2 7 7 * Requires PHP: 7.2 8 * Version: 1.1. 28 * Version: 1.1.3 9 9 * Author: Bubuku 10 10 * Author URI: https://www.bubuku.com/ -
bubuku-media-library/trunk/readme.txt
r3299741 r3306643 5 5 Tested up to: 6.8 6 6 Requires PHP: 7.2 7 Stable tag: 1.1. 27 Stable tag: 1.1.3 8 8 License: GPLv3 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-3.0.html … … 38 38 39 39 == Changelog == 40 = 1.1.3 = 41 * Improve performance and simplify SQL queries. 42 * Implemented caching for the calculated number of images by size and those lacking alt attributes. 43 40 44 = 1.1.2 = 41 45 * Added security and performance improvements -
bubuku-media-library/trunk/src/BML_db.php
r3299741 r3306643 44 44 { 45 45 global $wpdb; 46 $result = $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s", '_bkml_attachment_file_size')); 46 47 47 $batch_size = 100; 48 $last_id = 0; 49 $deleted_total = 0; 50 $meta_key = '_bkml_attachment_file_size'; 48 // Clear related cache after deletion 49 wp_cache_delete('bml_posts_by_meta_size', 'bubuku_media_library'); 51 50 52 do { 53 // Intentar obtener los IDs desde la caché primero 54 $cache_key = 'bml_attachment_ids_' . $last_id . '_' . $batch_size; 55 $attachment_ids = wp_cache_get($cache_key, 'bml_attachment_ids'); 56 57 if (false === $attachment_ids) { 58 // La caché está vacía, realizar la consulta a la base de datos 59 $attachment_ids = $wpdb->get_col( 60 $wpdb->prepare( 61 " 62 SELECT DISTINCT p.ID 63 FROM {$wpdb->posts} p 64 INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id 65 WHERE p.post_type = 'attachment' 66 AND pm.meta_key = %s 67 AND p.ID > %d 68 ORDER BY p.ID ASC 69 LIMIT %d 70 ", 71 $meta_key, 72 $last_id, 73 $batch_size 74 ) 75 ); 76 77 // Guardar en caché por 5 minutos (300 segundos) 78 wp_cache_set($cache_key, $attachment_ids, 'bml_attachment_ids', 300); 79 } 80 81 if (!empty($attachment_ids)) { 82 foreach ($attachment_ids as $attachment_id) { 83 // Usamos delete_post_meta que ya gestiona la caché internamente 84 $deleted = delete_post_meta($attachment_id, $meta_key); 85 if ($deleted) { 86 $deleted_total++; 87 } 88 89 // Actualizamos el último ID procesado 90 $last_id = $attachment_id; 91 } 92 } 93 } while (!empty($attachment_ids)); 94 95 return $deleted_total; 51 return $result !== false; 96 52 } 97 53 98 54 /** 99 * Counts posts by meta size using batched WP_Query55 * Counts posts by meta size using direct SQL query 100 56 * 101 57 * @param string $meta_key The meta key to search for … … 106 62 public function count_posts_by_meta_size($meta_key, $min_value, $max_value) 107 63 { 108 $count = 0; 109 $batch_size = 1000; // We can use a larger batch since we're only counting 110 $offset = 0; 64 global $wpdb; 111 65 112 while (true) { 113 $args = array( 114 'post_type' => 'attachment', 115 'post_status' => 'inherit', 116 'fields' => 'ids', 117 'posts_per_page' => $batch_size, 118 'offset' => $offset, 119 'no_found_rows' => true, // Improves performance when we don't need pagination 120 'meta_query' => array( 121 array( 122 'key' => $meta_key, 123 'value' => array($min_value, $max_value), 124 'type' => 'NUMERIC', 125 'compare' => 'BETWEEN', 126 ), 127 ), 128 ); 66 // Create cache key based on parameters 67 $cache_key = sprintf('count_meta_%s_%d_%d', sanitize_key($meta_key), $min_value, $max_value); 68 $cached_result = wp_cache_get($cache_key, 'bubuku_media_library'); 129 69 130 // If it's the largest range, we change the comparison 131 if ($max_value === PHP_INT_MAX) { 132 $args['meta_query'][0]['value'] = $min_value; 133 $args['meta_query'][0]['compare'] = '>='; 134 } 70 if ($cached_result !== false) { 71 return (int) $cached_result; 72 } 135 73 136 $query = new \WP_Query($args); 137 $batch_posts = $query->posts; 74 // If it's the largest range, use >= comparison 75 if ($max_value === PHP_INT_MAX) { 76 $count = (int) $wpdb->get_var($wpdb->prepare(" 77 SELECT COUNT(DISTINCT p.ID) 78 FROM {$wpdb->posts} p 79 INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id 80 WHERE p.post_type = 'attachment' 81 AND p.post_status = 'inherit' 82 AND pm.meta_key = %s 83 AND CAST(pm.meta_value AS UNSIGNED) >= %d 84 ", $meta_key, $min_value)); 85 } else { 86 $count = (int) $wpdb->get_var($wpdb->prepare(" 87 SELECT COUNT(DISTINCT p.ID) 88 FROM {$wpdb->posts} p 89 INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id 90 WHERE p.post_type = 'attachment' 91 AND p.post_status = 'inherit' 92 AND pm.meta_key = %s 93 AND CAST(pm.meta_value AS UNSIGNED) BETWEEN %d AND %d 94 ", $meta_key, $min_value, $max_value)); 95 } 138 96 139 // If there are no results, we finish 140 if (empty($batch_posts)) { 141 break; 142 } 143 144 // Increment the counter with the number of posts in this batch 145 $count += count($batch_posts); 146 147 // Move to the next batch 148 $offset += $batch_size; 149 } 97 // Cache the result for 1 hour 98 wp_cache_set($cache_key, $count, 'bubuku_media_library', HOUR_IN_SECONDS); 150 99 151 100 return $count; … … 159 108 public function calculate_img_alt_empty() 160 109 { 161 $count = 0; 162 $batch_size = 100; 163 $offset = 0; 110 global $wpdb; 164 111 165 while (true) { 166 $args = array( 167 'post_type' => 'attachment', 168 'post_mime_type' => 'image', 169 'post_status' => 'inherit', 170 'fields' => 'ids', 171 'posts_per_page' => $batch_size, 172 'offset' => $offset, 173 'no_found_rows' => true, 174 'meta_query' => array( 175 array( 176 'key' => '_wp_attachment_image_alt', 177 'compare' => 'NOT EXISTS' 178 ) 179 ) 180 ); 112 $cache_key = 'img_alt_empty_count'; 113 $cached_result = wp_cache_get($cache_key, 'bubuku_media_library'); 181 114 182 $query = new \WP_Query($args); 183 $attachment_ids = $query->posts; 184 185 if (empty($attachment_ids)) { 186 break; 187 } 188 189 $count += count($attachment_ids); 190 191 $offset += $batch_size; 115 if ($cached_result !== false) { 116 return number_format_i18n((int) $cached_result); 192 117 } 193 118 194 return number_format_i18n($count); 119 $count = $wpdb->get_var($wpdb->prepare(" 120 SELECT COUNT(*) 121 FROM {$wpdb->posts} 122 WHERE post_type = %s 123 AND post_status = %s 124 AND post_mime_type LIKE %s 125 AND ID NOT IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s) 126 ", 'attachment', 'inherit', 'image/%', '_wp_attachment_image_alt')); 127 128 // Cache the result for 1 hour 129 wp_cache_set($cache_key, $count, 'bubuku_media_library', HOUR_IN_SECONDS); 130 131 return number_format_i18n((int)$count); 195 132 } 196 133 } -
bubuku-media-library/trunk/src/BML_plugin.php
r3299741 r3306643 26 26 define('BUBUKU_BML_PLUGIN_ASSETS_URL', BUBUKU_BML_PLUGIN_URL . '/assets'); 27 27 define('BUBUKU_BML_PLUGIN_ENDPOINTS_URL', 'bbk_medialibrary/v1'); 28 define('BUBUKU_BML_PLUGIN_VERSION', '1.1. 2');28 define('BUBUKU_BML_PLUGIN_VERSION', '1.1.3'); 29 29 define('BUBUKU_BML_PLUGIN_NONCE', wp_create_nonce('media-library/v1')); 30 30
Note: See TracChangeset
for help on using the changeset viewer.