@@ -960,60 +960,92 @@ private function get_images( $args = array(), $additional_mime_types = array() )
960960 return new WP_Query ( $ query_args );
961961 }
962962
963- /**
964- * Get the metadata for the passed intermediate image size.
965- *
966- * @param string $size The image size to get the metadata for.
967- *
968- * @return array The image size metadata.
969- */
970- private function get_intermediate_size_metadata ( $ size ) {
971- $ width = intval ( get_option ( "{$ size }_size_w " ) );
972- $ height = intval ( get_option ( "{$ size }_size_h " ) );
973- $ crop = get_option ( "{$ size }_crop " );
974-
975- return array (
976- 'name ' => $ size ,
977- 'width ' => $ width ,
978- 'height ' => $ height ,
979- 'crop ' => false !== $ crop ? 'hard ' : 'soft ' ,
980- 'ratio ' => false !== $ crop ? $ this ->get_ratio ( $ width , $ height ) : 'N/A ' ,
981- );
982- }
983-
984963 /**
985964 * Get all the registered image sizes along with their dimensions.
986965 *
987- * @global array $_wp_additional_image_sizes The additional image sizes to parse.
988- *
989- * @link https://wordpress.stackexchange.com/a/251602 Original solution.
990- *
991966 * @return array $image_sizes The image sizes
992967 */
993968 private function get_registered_image_sizes () {
994- global $ _wp_additional_image_sizes ;
969+ $ image_sizes = array () ;
995970
996- $ image_sizes = array ();
997- $ default_image_sizes = get_intermediate_image_sizes ();
971+ $ all_sizes = $ this ->wp_get_registered_image_subsizes ();
998972
999- foreach ( $ default_image_sizes as $ size ) {
1000- $ image_sizes [] = $ this ->get_intermediate_size_metadata ( $ size );
973+ foreach ( $ all_sizes as $ size => $ size_args ) {
974+ $ crop = filter_var ( $ size_args ['crop ' ], FILTER_VALIDATE_BOOLEAN );
975+
976+ $ image_sizes [] = array (
977+ 'name ' => $ size ,
978+ 'width ' => $ size_args ['width ' ],
979+ 'height ' => $ size_args ['height ' ],
980+ 'crop ' => empty ( $ crop ) || is_array ( $ size_args ['crop ' ] ) ? 'soft ' : 'hard ' ,
981+ 'ratio ' => empty ( $ crop ) || is_array ( $ size_args ['crop ' ] ) ? 'N/A ' : $ this ->get_ratio ( $ size_args ['width ' ], $ size_args ['height ' ] ),
982+ );
1001983 }
1002984
1003- if ( is_array ( $ _wp_additional_image_sizes ) ) {
1004- foreach ( $ _wp_additional_image_sizes as $ size => $ size_args ) {
1005- $ crop = filter_var ( $ size_args ['crop ' ], FILTER_VALIDATE_BOOLEAN );
1006- $ image_sizes [] = array (
1007- 'name ' => $ size ,
1008- 'width ' => $ size_args ['width ' ],
1009- 'height ' => $ size_args ['height ' ],
1010- 'crop ' => empty ( $ crop ) || is_array ( $ size_args ['crop ' ] ) ? 'soft ' : 'hard ' ,
1011- 'ratio ' => empty ( $ crop ) || is_array ( $ size_args ['crop ' ] ) ? 'N/A ' : $ this ->get_ratio ( $ size_args ['width ' ], $ size_args ['height ' ] ),
1012- );
985+ return $ image_sizes ;
986+ }
987+
988+ /**
989+ * Returns a normalized list of all currently registered image sub-sizes.
990+ *
991+ * If exists, uses output of wp_get_registered_image_subsizes() function (introduced in WP 5.3).
992+ * Definition of this method is modified version of core function wp_get_registered_image_subsizes().
993+ *
994+ * @global array $_wp_additional_image_sizes
995+ *
996+ * @return array[] Associative array of arrays of image sub-size information, keyed by image size name.
997+ */
998+ private function wp_get_registered_image_subsizes () {
999+ if ( Utils \wp_version_compare ( '5.3 ' , '>= ' ) ) {
1000+ return wp_get_registered_image_subsizes ();
1001+ }
1002+
1003+ global $ _wp_additional_image_sizes ;
1004+
1005+ $ additional_sizes = $ _wp_additional_image_sizes ? $ _wp_additional_image_sizes : array ();
1006+
1007+ $ all_sizes = array ();
1008+
1009+ foreach ( get_intermediate_image_sizes () as $ size_name ) {
1010+ $ size_data = array (
1011+ 'width ' => 0 ,
1012+ 'height ' => 0 ,
1013+ 'crop ' => false ,
1014+ );
1015+
1016+ if ( isset ( $ additional_sizes [ $ size_name ]['width ' ] ) ) {
1017+ // For sizes added by plugins and themes.
1018+ $ size_data ['width ' ] = (int ) $ additional_sizes [ $ size_name ]['width ' ];
1019+ } else {
1020+ // For default sizes set in options.
1021+ $ size_data ['width ' ] = (int ) get_option ( "{$ size_name }_size_w " );
10131022 }
1023+
1024+ if ( isset ( $ additional_sizes [ $ size_name ]['height ' ] ) ) {
1025+ $ size_data ['height ' ] = (int ) $ additional_sizes [ $ size_name ]['height ' ];
1026+ } else {
1027+ $ size_data ['height ' ] = (int ) get_option ( "{$ size_name }_size_h " );
1028+ }
1029+
1030+ if ( empty ( $ size_data ['width ' ] ) && empty ( $ size_data ['height ' ] ) ) {
1031+ // This size isn't set.
1032+ continue ;
1033+ }
1034+
1035+ if ( isset ( $ additional_sizes [ $ size_name ]['crop ' ] ) ) {
1036+ $ size_data ['crop ' ] = $ additional_sizes [ $ size_name ]['crop ' ];
1037+ } else {
1038+ $ size_data ['crop ' ] = get_option ( "{$ size_name }_crop " );
1039+ }
1040+
1041+ if ( ! is_array ( $ size_data ['crop ' ] ) || empty ( $ size_data ['crop ' ] ) ) {
1042+ $ size_data ['crop ' ] = (bool ) $ size_data ['crop ' ];
1043+ }
1044+
1045+ $ all_sizes [ $ size_name ] = $ size_data ;
10141046 }
10151047
1016- return $ image_sizes ;
1048+ return $ all_sizes ;
10171049 }
10181050
10191051 /**
0 commit comments