@@ -143,34 +143,17 @@ public function delete_cache() {
143143 public function get_design_library_image ( $ request ) {
144144 $ url = $ request ->get_param ( 'image_url ' );
145145
146- $ temp_filepath = download_url ( $ url );
147-
148- if ( is_wp_error ( $ temp_filepath ) ) {
149- return new WP_REST_Response ( array (
150- 'success ' => false ,
151- 'message ' => 'Failed to retrieve image from the provided URL. '
152- ), 500 );
153- }
154-
155- if ( ! file_exists ( $ temp_filepath ) || ! wp_filesize ( $ temp_filepath ) ) {
156- @unlink ( $ temp_filepath );
157- return new WP_REST_Response ( array (
158- 'success ' => false ,
159- 'message ' => 'Invalid file content retrieved from the provided URL. '
160- ), 400 );
161- }
162-
163- $ image_hash = hash_file ( 'md5 ' , $ temp_filepath );
146+ $ basename = sanitize_file_name ( wp_basename ( parse_url ( $ url , PHP_URL_PATH ) ) );
164147
165148 $ args = array (
166149 'post_type ' => 'attachment ' ,
167150 'post_status ' => 'inherit ' ,
168151 'posts_per_page ' => 1 ,
169152 'meta_query ' => array (
170153 array (
171- 'key ' => 'stk_image_hash ' ,
172- 'value ' => $ image_hash ,
173- 'compare ' => '= '
154+ 'key ' => '_wp_attached_file ' ,
155+ 'value ' => $ basename ,
156+ 'compare ' => 'LIKE '
174157 )
175158 )
176159 );
@@ -183,7 +166,6 @@ public function get_design_library_image( $request ) {
183166 $ media_url = wp_get_attachment_url ( $ media_id );
184167
185168 wp_reset_postdata ();
186- @unlink ( $ temp_filepath );
187169
188170 return new WP_REST_Response ( array (
189171 'success ' => true ,
@@ -192,10 +174,27 @@ public function get_design_library_image( $request ) {
192174 ), 200 );
193175 }
194176
177+ $ temp_filepath = download_url ( $ url );
178+
179+ if ( is_wp_error ( $ temp_filepath ) ) {
180+ return new WP_REST_Response ( array (
181+ 'success ' => false ,
182+ 'message ' => 'Failed to retrieve image from the provided URL. '
183+ ), 500 );
184+ }
185+
186+ if ( ! file_exists ( $ temp_filepath ) || ! wp_filesize ( $ temp_filepath ) ) {
187+ @unlink ( $ temp_filepath );
188+ return new WP_REST_Response ( array (
189+ 'success ' => false ,
190+ 'message ' => 'Invalid file content retrieved from the provided URL. '
191+ ), 400 );
192+ }
193+
195194 $ valid_mimes = [ 'image/jpeg ' => 1 , 'image/jpg ' => 1 , 'image/png ' => 1 , 'image/gif ' => 1 , 'image/webp ' => 1 , 'video/mp4 ' => 1 ];
196195
197196 $ file_array = array (
198- 'name ' => sanitize_file_name ( wp_basename ( parse_url ( $ url , PHP_URL_PATH ) ) ) ,
197+ 'name ' => $ basename ,
199198 'type ' => mime_content_type ( $ temp_filepath ),
200199 'tmp_name ' => $ temp_filepath ,
201200 'size ' => wp_filesize ( $ temp_filepath )
@@ -230,7 +229,6 @@ public function get_design_library_image( $request ) {
230229 ), 500 );
231230 }
232231
233- update_post_meta ( $ media_id , 'stk_image_hash ' , $ image_hash );
234232 $ media_url = wp_get_attachment_url ( $ media_id );
235233
236234 return new WP_REST_Response ( array (
@@ -297,7 +295,7 @@ public function _get_design_library( $outside_init = false ) {
297295 $ block_patterns = WP_Block_Patterns_Registry::get_instance ()->get_all_registered ( $ outside_init );
298296 foreach ( $ block_patterns as $ pattern ) {
299297 if ( strpos ( $ pattern [ 'name ' ], 'stackable/ ' ) !== false ) {
300- $ pattern [ 'categories ' ][ 0 ] = str_replace ( ' stackable/ ' , '' , $ pattern [ 'categories ' ][ 0 ] );
298+ $ pattern [ 'title ' ] = str_replace ( sprintf ( __ ( ' Stackable ' , STACKABLE_I18N ) ), '' , $ pattern [ 'title ' ] );
301299 $ content [ $ pattern [ 'designId ' ] ] = $ pattern ;
302300 }
303301 }
@@ -369,9 +367,9 @@ public function get_premium_designs( $content ) {
369367 }
370368
371369 $ premium_designs [ $ design_id ] = array (
372- 'title ' => sprintf ( __ ( ' Stackable %s ' , STACKABLE_I18N ), $ design [ 'label ' ] ) ,
370+ 'title ' => $ design [ 'label ' ],
373371 'content ' => $ design [ 'template ' ],
374- 'categories ' => array ( $ design [ 'category ' ], ' stackable ' ) ,
372+ 'category ' => $ design [ 'category ' ],
375373 'description ' => $ design [ 'description ' ],
376374 'plan ' => $ design [ 'plan ' ],
377375 'designId ' => $ design_id
@@ -399,9 +397,9 @@ public function get_designs_with_disabled_blocks( $content ) {
399397 }
400398
401399 $ designs_with_disabled [ $ design_id ] = array (
402- 'title ' => sprintf ( __ ( ' Stackable %s ' , STACKABLE_I18N ), $ design [ 'label ' ] ) ,
400+ 'title ' => $ design [ 'label ' ],
403401 'content ' => $ design [ 'template ' ],
404- 'categories ' => array ( $ design [ 'category ' ], ' stackable ' ) ,
402+ 'category ' => $ design [ 'category ' ],
405403 'description ' => $ design [ 'description ' ],
406404 'plan ' => $ design [ 'plan ' ],
407405 'designId ' => $ design_id ,
@@ -417,6 +415,11 @@ public function get_designs_with_disabled_blocks( $content ) {
417415 return $ merged ;
418416 }
419417
418+ public function get_category_kebab_case ( $ category ) {
419+ $ category = trim ( strtolower ( $ category ) );
420+ return preg_replace ( '/[^a-z0-9-]+/ ' , '- ' , $ category );
421+ }
422+
420423 public function register_design_pattern () {
421424 $ designs = $ this ->get_design_library_from_cloud ();
422425
@@ -441,7 +444,7 @@ public function register_design_pattern() {
441444 if ( $ has_disabled ) continue ;
442445 }
443446
444- register_block_pattern_category ( 'stackable/ ' . $ design [ 'category ' ], [
447+ register_block_pattern_category ( 'stackable/ ' . $ this -> get_category_kebab_case ( $ design [ 'category ' ] ) , [
445448 'label ' => sprintf ( __ ( 'Stackable %s ' , STACKABLE_I18N ), $ design [ 'category ' ] ),
446449 'description ' => sprintf ( __ ( '%s patterns for Stackable Design Library ' , STACKABLE_I18N ), $ design [ 'category ' ] ),
447450 ] );
@@ -451,7 +454,8 @@ public function register_design_pattern() {
451454 array (
452455 'title ' => sprintf ( __ ( 'Stackable %s ' , STACKABLE_I18N ), $ design [ 'label ' ] ),
453456 'content ' => $ design [ 'template ' ],
454- 'categories ' => array ( 'stackable/ ' . $ design [ 'category ' ], 'stackable ' ),
457+ 'categories ' => array ( 'stackable/ ' . $ this ->get_category_kebab_case ( $ design [ 'category ' ] ), 'stackable ' ), // used in Patterns
458+ 'category ' => $ design [ 'category ' ], // used in Design Library
455459 'description ' => $ design [ 'description ' ],
456460 'plan ' => $ design [ 'plan ' ],
457461 'designId ' => $ design_id
0 commit comments