PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
← All changes | inc/classes/class-imagify-custom-folders.php +162 -91 1.10 → trunk View file →
@@ -1,6 +1,5 @@
1 1 <?php
2 -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
3 2
4 3 /**
5 4 * Class that regroups things about "custom folders".
6 5 *
@@ -146,9 +145,9 @@
146 145 *
147 146 * @param array $args An array of arguments to pass to Imagify_Files_DB::insert(). Required values are 'folder_id' and ( 'path' or 'file_path').
148 147 * @return int The file ID on success. 0 on failure.
149 148 */
150 - public static function insert_file( $args = array() ) {
149 + public static function insert_file( $args = [] ) {
151 150 if ( empty( $args['folder_id'] ) ) {
152 151 return 0;
153 152 }
154 153
@@ -179,9 +178,9 @@
179 178 }
180 179
181 180 if ( ( empty( $args['width'] ) || empty( $args['height'] ) ) && strpos( $args['mime_type'], 'image/' ) === 0 ) {
182 181 $file_size = $filesystem->get_image_size( $args['file_path'] );
183 - $args['width'] = $file_size ? $file_size['width'] : 0;
182 + $args['width'] = $file_size ? $file_size['width'] : 0;
184 183 $args['height'] = $file_size ? $file_size['height'] : 0;
185 184 }
186 185
187 186 if ( empty( $args['hash'] ) ) {
@@ -209,15 +208,18 @@
209 208 * @param array $args An array of arguments.
210 209 * At least: 'file_id'. At best: 'file_id', 'file_path' (or 'path' for the placeholder), and 'backup_path'.
211 210 */
212 211 public static function delete_file( $args = [] ) {
213 - $args = array_merge( [
214 - 'file_id' => 0,
215 - 'file_path' => '',
216 - 'path' => '',
217 - 'backup_path' => '',
218 - 'process' => false,
219 - ], $args );
212 + $args = array_merge(
213 + [
214 + 'file_id' => 0,
215 + 'file_path' => '',
216 + 'path' => '',
217 + 'backup_path' => '',
218 + 'process' => false,
219 + ],
220 + $args
221 + );
220 222
221 223 $filesystem = imagify_get_filesystem();
222 224
223 225 // Fill the blanks.
@@ -409,20 +411,23 @@
409 411 } else {
410 412 $size = false;
411 413 }
412 414
413 - $new_data = array_merge( $new_data, [
414 - 'file_date' => $filesystem->get_date( $file_path ),
415 - 'width' => $size ? $size['width'] : 0,
416 - 'height' => $size ? $size['height'] : 0,
417 - 'original_size' => $filesystem->size( $file_path ),
418 - 'optimized_size' => null,
419 - 'percent' => null,
420 - 'optimization_level' => null,
421 - 'status' => null,
422 - 'error' => null,
423 - 'data' => [],
424 - ] );
415 + $new_data = array_merge(
416 + $new_data,
417 + [
418 + 'file_date' => $filesystem->get_date( $file_path ),
419 + 'width' => $size ? $size['width'] : 0,
420 + 'height' => $size ? $size['height'] : 0,
421 + 'original_size' => $filesystem->size( $file_path ),
422 + 'optimized_size' => null,
423 + 'percent' => null,
424 + 'optimization_level' => null,
425 + 'status' => null,
426 + 'error' => null,
427 + 'data' => [],
428 + ]
429 + );
425 430
426 431 // Delete the backup of the previous file.
427 432 $process->delete_backup();
428 433 } else {
@@ -437,14 +442,17 @@
437 442 } else {
438 443 $size = false;
439 444 }
440 445
441 - $new_data = array_merge( $new_data, [
442 - 'file_date' => $file_date,
443 - 'width' => $size ? $size['width'] : 0,
444 - 'height' => $size ? $size['height'] : 0,
445 - 'original_size' => $filesystem->size( $path ),
446 - ] );
446 + $new_data = array_merge(
447 + $new_data,
448 + [
449 + 'file_date' => $file_date,
450 + 'width' => $size ? $size['width'] : 0,
451 + 'height' => $size ? $size['height'] : 0,
452 + 'original_size' => $filesystem->size( $path ),
453 + ]
454 + );
447 455
448 456 // WebP.
449 457 $webp_size = 'full' . $process::WEBP_SUFFIX;
450 458
@@ -506,9 +514,9 @@
506 514 * [folder_path] => /absolute/path/to/wp-content/another-custom-path/
507 515 * )
508 516 * )
509 517 */
510 - public static function get_folders( $args = array() ) {
518 + public static function get_folders( $args = [] ) {
511 519 global $wpdb;
512 520
513 521 $folders_db = Imagify_Folders_DB::get_instance();
514 522 $folders_table = $folders_db->get_table_name();
@@ -528,13 +536,13 @@
528 536 // Get the folders from the DB.
529 537 $results = $wpdb->get_results( "SELECT * FROM $folders_table $where_active;", ARRAY_A ); // WPCS: unprepared SQL ok.
530 538
531 539 if ( ! $results || ! is_array( $results ) ) {
532 - return array();
540 + return [];
533 541 }
534 542
535 543 // Cast results, add absolute paths.
536 - $folders = array();
544 + $folders = [];
537 545
538 546 foreach ( $results as $row_fields ) {
539 547 // Cast the row.
540 548 $row_fields = $folders_db->cast_row( $row_fields );
@@ -594,20 +602,20 @@
594 602 * ),
595 603 * )
596 604 * The fields 'optimization_level' and 'status' are set only if the argument 'optimization_level' was set.
597 605 */
598 - public static function get_files_from_folders( $folders, $args = array() ) {
606 + public static function get_files_from_folders( $folders, $args = [] ) {
599 607 global $wpdb;
600 608
601 609 if ( ! $folders ) {
602 - return array();
610 + return [];
603 611 }
604 612
605 - $filesystem = imagify_get_filesystem();
606 - $files_db = Imagify_Files_DB::get_instance();
607 - $files_table = $files_db->get_table_name();
608 - $files_key = $files_db->get_primary_key();
609 - $files_key_esc = esc_sql( $files_key );
613 + $filesystem = imagify_get_filesystem();
614 + $files_db = Imagify_Files_DB::get_instance();
615 + $files_table = $files_db->get_table_name();
616 + $files_key = $files_db->get_primary_key();
617 + $files_key_esc = esc_sql( $files_key );
610 618
611 619 $optimization = isset( $args['optimization_level'] ) && is_numeric( $args['optimization_level'] );
612 620 $no_new_files = ! empty( $args['return_only_old_files'] );
613 621 $add_inactive_folder_files = ! empty( $args['add_inactive_folder_files'] );
@@ -625,9 +633,9 @@
625 633 * [/absolute/path/to/wp-content/another-custom-path/image-3.jpg] => 2
626 634 * )
627 635 * )
628 636 */
629 - $files_from_scan = array();
637 + $files_from_scan = [];
630 638
631 639 foreach ( $folders as $folder_id => $folder ) {
632 640 $files_from_scan[ $folder_id ] = Imagify_Files_Scan::get_files_from_folder( $folder['folder_path'] );
633 641
@@ -640,11 +648,11 @@
640 648
641 649 /**
642 650 * Get the files from DB. $files_from_db will be in the same format as the function output.
643 651 */
644 - $already_optimized = array();
652 + $already_optimized = [];
645 653 $folder_ids = array_keys( $folders );
646 - $files_from_db = array_fill_keys( $folder_ids, array() );
654 + $files_from_db = array_fill_keys( $folder_ids, [] );
647 655 $folder_ids = Imagify_DB::prepare_values_list( $folder_ids );
648 656 $select_fields = "$files_key_esc, folder_id, path" . ( $optimization ? ', optimization_level, status' : '' );
649 657
650 658 if ( $optimization ) {
@@ -696,12 +704,14 @@
696 704 }
697 705
698 706 if ( ! $filesystem->exists( $row_fields['file_path'] ) ) {
699 707 // If the file doesn't exist: remove all traces of it and bail out.
700 - self::delete_file( array(
701 - 'file_id' => $row_fields[ $files_key ],
702 - 'file_path' => $row_fields['file_path'],
703 - ) );
708 + self::delete_file(
709 + [
710 + 'file_id' => $row_fields[ $files_key ],
711 + 'file_path' => $row_fields['file_path'],
712 + ]
713 + );
704 714 continue;
705 715 }
706 716
707 717 if ( $optimization && 'already_optimized' === $row_fields['status'] ) {
@@ -717,9 +727,9 @@
717 727 $files_from_scan = array_filter( $files_from_scan );
718 728
719 729 // Make sure files from the scan are not already in the DB with another folder (shouldn't be possible, but, you know...).
720 730 if ( $files_from_scan ) {
721 - $folders_by_placeholder = array();
731 + $folders_by_placeholder = [];
722 732
723 733 foreach ( $files_from_scan as $folder_id => $folder_files ) {
724 734 foreach ( $folder_files as $file_path => $i ) {
725 735 $placeholder = Imagify_Files_Scan::add_placeholder( $file_path );
@@ -728,14 +738,35 @@
728 738 $files_from_scan[ $folder_id ][ $file_path ] = $placeholder;
729 739 }
730 740 }
731 741
732 - $placeholders = Imagify_DB::prepare_values_list( array_keys( $folders_by_placeholder ) );
733 742 $select_fields = "$files_key_esc, folder_id, path" . ( $optimization ? ', optimization_level, status' : '' );
734 743
735 - $results = $wpdb->get_results( "SELECT $select_fields FROM $files_table WHERE path IN ( $placeholders ) ORDER BY folder_id, $files_key_esc;", ARRAY_A ); // WPCS: unprepared SQL ok.
744 + $results = [];
736 745
746 + foreach ( Imagify_DB::chunk_in_values( array_keys( $folders_by_placeholder ) ) as $placeholders_chunk ) {
747 + $placeholders = Imagify_DB::prepare_values_list( $placeholders_chunk );
748 + $chunk_results = $wpdb->get_results( "SELECT $select_fields FROM $files_table WHERE path IN ( $placeholders );", ARRAY_A ); // WPCS: unprepared SQL ok.
749 +
750 + if ( $chunk_results ) {
751 + $results = array_merge( $results, $chunk_results );
752 + }
753 + }
754 +
737 755 if ( $results ) {
756 + // Re-apply the ordering that was previously done in SQL (`ORDER BY folder_id, $files_key_esc`).
757 + usort(
758 + $results,
759 + function ( $a, $b ) use ( $files_key_esc ) {
760 + if ( $a['folder_id'] !== $b['folder_id'] ) {
761 + return $a['folder_id'] <=> $b['folder_id'];
762 + }
763 + return $a[ $files_key_esc ] <=> $b[ $files_key_esc ];
764 + }
765 + );
766 + }
767 +
768 + if ( $results ) {
738 769 // Damn...
739 770 $wpdb->flush();
740 771
741 772 foreach ( $results as $i => $row_fields ) {
@@ -778,19 +809,24 @@
778 809 }
779 810
780 811 if ( ! $filesystem->exists( $row_fields['file_path'] ) ) {
781 812 // If the file doesn't exist: remove all traces of it and bail out.
782 - self::delete_file( array(
783 - 'file_id' => $row_fields[ $files_key ],
784 - 'file_path' => $row_fields['file_path'],
785 - ) );
813 + self::delete_file(
814 + [
815 + 'file_id' => $row_fields[ $files_key ],
816 + 'file_path' => $row_fields['file_path'],
817 + ]
818 + );
786 819 continue;
787 820 }
788 821
789 822 // Set the correct folder ID in the DB.
790 - $success = $files_db->update( $row_fields[ $files_key ], array(
791 - 'folder_id' => $row_fields['folder_id'],
792 - ) );
823 + $success = $files_db->update(
824 + $row_fields[ $files_key ],
825 + [
826 + 'folder_id' => $row_fields['folder_id'],
827 + ]
828 + );
793 829
794 830 if ( $success ) {
795 831 if ( $optimization && 'already_optimized' === $row_fields['status'] ) {
796 832 $already_optimized[ '_' . $row_fields[ $files_key ] ] = 1;
@@ -815,16 +851,18 @@
815 851 continue;
816 852 }
817 853
818 854 foreach ( $placeholders as $file_path => $placeholder ) {
819 - $file_id = self::insert_file( array(
820 - 'folder_id' => $folder_id,
821 - 'path' => $placeholder,
822 - 'file_path' => $file_path,
823 - ) );
855 + $file_id = self::insert_file(
856 + [
857 + 'folder_id' => $folder_id,
858 + 'path' => $placeholder,
859 + 'file_path' => $file_path,
860 + ]
861 + );
824 862
825 863 if ( $file_id && ! $no_new_files ) {
826 - $files_from_db[ $folder_id ][ '_' . $file_id ] = array(
864 + $files_from_db[ $folder_id ][ '_' . $file_id ] = [
827 865 'file_id' => $file_id,
828 866 'folder_id' => $folder_id,
829 867 'path' => $placeholder,
830 868 'optimization_level' => null,
@@ -829,9 +867,9 @@
829 867 'path' => $placeholder,
830 868 'optimization_level' => null,
831 869 'status' => null,
832 870 'file_path' => $file_path,
833 - );
871 + ];
834 872 }
835 873 }
836 874
837 875 unset( $files_from_scan[ $folder_id ] );
@@ -840,12 +878,12 @@
840 878
841 879 $files_from_db = array_filter( $files_from_db );
842 880
843 881 if ( ! $files_from_db ) {
844 - return array();
882 + return [];
845 883 }
846 884
847 - $files_from_db = call_user_func_array( 'array_merge', $files_from_db );
885 + $files_from_db = call_user_func_array( 'array_merge', array_values( $files_from_db ) );
848 886
849 887 if ( $already_optimized ) {
850 888 // Put the files already optimized at the end of the list.
851 889 $already_optimized = array_intersect_key( $files_from_db, $already_optimized );
@@ -869,11 +907,14 @@
869 907 global $wpdb;
870 908 /**
871 909 * Get the files from DB, and from the folder.
872 910 */
873 - $files = self::get_files_from_folders( $folders, array(
874 - 'return_only_old_files' => true,
875 - ) );
911 + $files = self::get_files_from_folders(
912 + $folders,
913 + [
914 + 'return_only_old_files' => true,
915 + ]
916 + );
876 917
877 918 if ( ! $files ) {
878 919 // This folder doesn't have (new) images.
879 920 return;
@@ -883,16 +924,31 @@
883 924 $files_table = $files_db->get_table_name();
884 925 $files_key = $files_db->get_primary_key();
885 926 $files_key_esc = esc_sql( $files_key );
886 927 $file_ids = wp_list_pluck( $files, $files_key );
887 - $file_ids = Imagify_DB::prepare_values_list( $file_ids );
888 - $results = $wpdb->get_results( "SELECT * FROM $files_table WHERE $files_key IN ( $file_ids ) ORDER BY $files_key_esc;", ARRAY_A ); // WPCS: unprepared SQL ok.
928 + $results = [];
889 929
930 + foreach ( Imagify_DB::chunk_in_values( $file_ids ) as $file_ids_chunk ) {
931 + $file_ids_in = Imagify_DB::prepare_values_list( $file_ids_chunk );
932 + $chunk_results = $wpdb->get_results( "SELECT * FROM $files_table WHERE $files_key IN ( $file_ids_in );", ARRAY_A ); // WPCS: unprepared SQL ok.
933 +
934 + if ( $chunk_results ) {
935 + $results = array_merge( $results, $chunk_results );
936 + }
937 + }
938 +
890 939 if ( ! $results ) {
891 - // WAT?!
892 940 return;
893 941 }
894 942
943 + // Re-apply the ordering that was previously done in SQL (`ORDER BY $files_key_esc`).
944 + usort(
945 + $results,
946 + function ( $a, $b ) use ( $files_key_esc ) {
947 + return $a[ $files_key_esc ] <=> $b[ $files_key_esc ];
948 + }
949 + );
950 +
895 951 // Caching the folders will prevent unecessary SQL queries in Imagify_Custom_Folders::refresh_file().
896 952 foreach ( $folders as $folder_id => $folder ) {
897 953 wp_cache_set( 'custom_folder_' . $folder_id, $folder, 'imagify' );
898 954 }
@@ -935,9 +991,9 @@
935 991 * @author Grégory Viguier
936 992 *
937 993 * @param array|object|string $selected_paths A list of "placeholdered" paths corresponding to the selected folders.
938 994 */
939 - public static function deactivate_not_selected_folders( $selected_paths = array() ) {
995 + public static function deactivate_not_selected_folders( $selected_paths = [] ) {
940 996 global $wpdb;
941 997
942 998 $folders_table = Imagify_Folders_DB::get_instance()->get_table_name();
943 999
@@ -976,13 +1032,20 @@
976 1032 $folders_table = $folders_db->get_table_name();
977 1033 $folders_key = $folders_db->get_primary_key();
978 1034
979 1035 $selected_paths = (array) $selected_paths;
980 - $selected_in = Imagify_DB::prepare_values_list( $selected_paths );
1036 + $folders = [];
981 1037
982 1038 // Get folders that already are in the DB.
983 - $folders = $wpdb->get_results( "SELECT * FROM $folders_table WHERE path IN ( $selected_in );", ARRAY_A ); // WPCS: unprepared SQL ok.
1039 + foreach ( Imagify_DB::chunk_in_values( $selected_paths ) as $selected_paths_chunk ) {
1040 + $selected_in = Imagify_DB::prepare_values_list( $selected_paths_chunk );
1041 + $chunk_folders = $wpdb->get_results( "SELECT * FROM $folders_table WHERE path IN ( $selected_in );", ARRAY_A ); // WPCS: unprepared SQL ok.
984 1042
1043 + if ( $chunk_folders ) {
1044 + $folders = array_merge( $folders, $chunk_folders );
1045 + }
1046 + }
1047 +
985 1048 if ( ! $folders ) {
986 1049 return $selected_paths;
987 1050 }
988 1051
@@ -993,17 +1056,23 @@
993 1056
994 1057 if ( Imagify_Files_Scan::placeholder_path_exists( $folder['path'] ) ) {
995 1058 if ( ! $folder['active'] ) {
996 1059 // Add the active status only if not already set and if the folder exists.
997 - $folders_db->update( $folder[ $folders_key ], array(
998 - 'active' => 1,
999 - ) );
1060 + $folders_db->update(
1061 + $folder[ $folders_key ],
1062 + [
1063 + 'active' => 1,
1064 + ]
1065 + );
1000 1066 }
1001 1067 } else {
1002 1068 // Remove the active status if the folder does not exist.
1003 - $folders_db->update( $folder[ $folders_key ], array(
1004 - 'active' => 0,
1005 - ) );
1069 + $folders_db->update(
1070 + $folder[ $folders_key ],
1071 + [
1072 + 'active' => 0,
1073 + ]
1074 + );
1006 1075 }
1007 1076
1008 1077 // Remove the path from the selected list, so the remaining will be created.
1009 1078 unset( $selected_paths[ $folder['path'] ] );
@@ -1024,12 +1093,12 @@
1024 1093 * @return array An array of folder IDs.
1025 1094 */
1026 1095 public static function insert_folders( $folders ) {
1027 1096 if ( ! $folders ) {
1028 - return array();
1097 + return [];
1029 1098 }
1030 1099
1031 - $folder_ids = array();
1100 + $folder_ids = [];
1032 1101 $filesystem = imagify_get_filesystem();
1033 1102 $folders_db = Imagify_Folders_DB::get_instance();
1034 1103
1035 1104 foreach ( $folders as $placeholder ) {
@@ -1043,12 +1112,14 @@
1043 1112 if ( Imagify_Files_Scan::is_path_forbidden( trailingslashit( $full_path ) ) ) {
1044 1113 continue;
1045 1114 }
1046 1115
1047 - $folder_ids[] = $folders_db->insert( array(
1048 - 'path' => $placeholder,
1049 - 'active' => 1,
1050 - ) );
1116 + $folder_ids[] = $folders_db->insert(
1117 + [
1118 + 'path' => $placeholder,
1119 + 'active' => 1,
1120 + ]
1121 + );
1051 1122 }
1052 1123
1053 1124 return array_filter( $folder_ids );
1054 1125 }
@@ -1108,9 +1179,9 @@
1108 1179 if ( ! $active_folders ) {
1109 1180 return;
1110 1181 }
1111 1182
1112 - $active_folder_ids = array();
1183 + $active_folder_ids = [];
1113 1184 $has_site_root = false;
1114 1185
1115 1186 foreach ( $active_folders as $i => $active_folder ) {
1116 1187 $active_folders[ $i ] = $folders_db->cast_row( $active_folder );
@@ -1130,9 +1201,9 @@
1130 1201 return;
1131 1202 }
1132 1203
1133 1204 $filesystem = imagify_get_filesystem();
1134 - $file_ids_by_folder = array();
1205 + $file_ids_by_folder = [];
1135 1206 $active_folders = self::sort_folders( $active_folders, true );
1136 1207
1137 1208 foreach ( $inactive_files as $inactive_file ) {
1138 1209 $inactive_file = $files_db->cast_row( $inactive_file );
@@ -1150,9 +1221,9 @@
1150 1221 continue;
1151 1222 }
1152 1223
1153 1224 if ( ! isset( $file_ids_by_folder[ $folder_id ] ) ) {
1154 - $file_ids_by_folder[ $folder_id ] = array();
1225 + $file_ids_by_folder[ $folder_id ] = [];
1155 1226 }
1156 1227
1157 1228 if ( '{{ROOT}}/' === $active_folder['path'] ) {
1158 1229 // For the site's root: only direct childs.
@@ -1253,13 +1324,13 @@
1253 1324 * @return array
1254 1325 */
1255 1326 public static function sort_folders( $folders, $reverse = false ) {
1256 1327 if ( ! $folders ) {
1257 - return array();
1328 + return [];
1258 1329 }
1259 1330
1260 - $keyed_folders = array();
1261 - $keyed_paths = array();
1331 + $keyed_folders = [];
1332 + $keyed_paths = [];
1262 1333
1263 1334 foreach ( $folders as $folder ) {
1264 1335 $folder = (array) $folder;
1265 1336 $folder['full_path'] = Imagify_Files_Scan::remove_placeholder( $folder['path'] );