Skip to content

Commit e2f907d

Browse files
bfintalkaeizen
andauthored
fix: remove design library pattern registration and only call remote get when design library is opened (#3572)
* remove pattern registration * remove filters * change transient name * use the property cdnUrl --------- Co-authored-by: [email protected] <> Co-authored-by: Mikhaela Tapia <[email protected]>
1 parent ba5035f commit e2f907d

File tree

2 files changed

+21
-221
lines changed

2 files changed

+21
-221
lines changed

src/components/design-library-list/util.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* eslint-disable no-console */
22
import DEFAULT from './default.json'
3-
import { settings, isPro } from 'stackable'
3+
import {
4+
settings, isPro, cdnUrl,
5+
} from 'stackable'
46
import { parse, serialize } from '@wordpress/blocks'
57

68
const DEFAULT_CONTENT = { ...DEFAULT }
@@ -199,7 +201,7 @@ export const parseDisabledBlocks = parsedBlock => {
199201
return { block, blocksForSubstitution }
200202
}
201203

202-
const IMAGE_STORAGE = 'https://stackable-files.pages.dev/library-v4/images/'
204+
const IMAGE_STORAGE = cdnUrl.replace( /\/$/, '' ) + '/library-v4/images/'
203205

204206
export const addPlaceholderForPostsBlock = ( content, postsPlaceholder, defaultValues ) => {
205207
const remainingPosts = [ ...postsPlaceholder ]

src/design-library/init.php

Lines changed: 17 additions & 219 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ class Stackable_Design_Library {
2929
public function __construct() {
3030
add_action( 'rest_api_init', array( $this, 'register_route' ) );
3131

32-
add_filter( 'stackable_design_library_get_premium_designs', array( $this, 'get_designs_with_disabled_blocks' ) );
33-
add_filter( 'stackable_design_library_get_premium_designs', array( $this, 'get_premium_designs' ) );
34-
add_action( 'init', array( $this, 'register_design_pattern' ) );
3532
add_action( 'stackable_delete_design_library_cache', array( $this, 'delete_cache_v3' ) );
3633
}
3734

@@ -109,23 +106,9 @@ public function delete_cache_v3() {
109106
}
110107

111108
public function delete_cache() {
112-
$designs = $this->get_design_library_from_cloud();
113-
114-
$library = $designs[ self::API_VERSION ];
115-
foreach ( $library as $design_id => $design ) {
116-
if ( WP_Block_Patterns_Registry::get_instance()->is_registered( 'stackable_' . $design_id ) ) {
117-
$res = unregister_block_pattern( 'stackable_' . $design_id );
118-
}
119-
120-
if ( WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'stackable_' . $design[ 'category' ] ) ) {
121-
$res = unregister_block_pattern_category( 'stackable_' . $design[ 'category' ] );
122-
}
123-
}
124109
// Delete design library.
125-
delete_transient( 'stackable_get_design_library_v4' );
126110
delete_transient( 'stackable_get_design_library_json_v4' );
127-
128-
$this->register_design_pattern();
111+
delete_transient( 'stackable_get_design_library_v4' );
129112

130113
do_action( 'stackable_delete_design_library_cache' );
131114
}
@@ -240,12 +223,8 @@ public function get_design_library_image( $request ) {
240223
), 200 );
241224
}
242225

243-
public function filter_patterns( $pattern ) {
244-
return strpos( $pattern[ 'name' ], 'stackable_' ) !== false;
245-
}
246-
247226
public function get_design_library_from_cloud() {
248-
$designs = get_transient( 'stackable_get_design_library_json_v4' );
227+
$designs = get_transient( 'stackable_get_design_library_v4' );
249228

250229
// Fetch designs.
251230
if ( empty( $designs ) ) {
@@ -262,8 +241,20 @@ public function get_design_library_from_cloud() {
262241
);
263242
} else {
264243
$content_body = wp_remote_retrieve_body( $response );
265-
$content = apply_filters( 'stackable_design_library_retreive_body', $content_body );
266-
$content = json_decode( $content, true );
244+
$designs = apply_filters( 'stackable_design_library_retreive_body', $content_body );
245+
$designs = json_decode( $designs, true );
246+
247+
$content = array();
248+
foreach ( $designs as $design_id => $design ) {
249+
$content[ $design_id ] = array(
250+
'title' => $design[ 'label' ],
251+
'content' => $design[ 'template' ],
252+
'category' => $design[ 'category' ],
253+
'description' => $design[ 'description' ],
254+
'plan' => $design[ 'plan' ],
255+
'designId' => $design_id
256+
);
257+
}
267258

268259
// Add our error message so we can see it in the network tab.
269260
if ( empty( $content ) ) {
@@ -280,40 +271,8 @@ public function get_design_library_from_cloud() {
280271
// Allow deprecated code to fetch other designs
281272
$designs = apply_filters( 'stackable_fetch_design_library', $designs );
282273

283-
// Cache results.
284-
set_transient( 'stackable_get_design_library_json_v4', $designs, DAY_IN_SECONDS );
285-
}
286-
287-
return apply_filters( 'stackable_design_library', $designs );
288-
}
289-
290-
public function _get_design_library( $outside_init = false ) {
291-
$designs = get_transient( 'stackable_get_design_library_v4' );
292-
// Fetch designs.
293-
if ( empty( $designs ) ) {
294-
$designs = array();
295-
$content = array();
296-
297-
$block_patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered( $outside_init );
298-
foreach ( $block_patterns as $pattern ) {
299-
if ( strpos( $pattern[ 'name' ], 'stackable_' ) !== false ) {
300-
$pattern[ 'title' ] = str_replace( sprintf( __( 'Stackable ', STACKABLE_I18N ) ), '', $pattern[ 'title' ] );
301-
$content[ $pattern[ 'designId' ] ] = $pattern;
302-
}
303-
}
304-
305-
// Get premium designs for v4
306-
$content = apply_filters( 'stackable_design_library_get_premium_designs', $content );
307-
308-
// We add the latest designs in the `v4` area.
309-
$designs[ self::API_VERSION ] = $content;
310-
311-
// Allow deprecated code to fetch other designs
312-
$designs = apply_filters( 'stackable_fetch_design_library', $designs );
313-
314274
// Cache results.
315275
set_transient( 'stackable_get_design_library_v4', $designs, DAY_IN_SECONDS );
316-
317276
}
318277

319278
return apply_filters( 'stackable_design_library', $designs );
@@ -328,170 +287,9 @@ public function get_design_library( $request ) {
328287
$this->delete_cache();
329288
}
330289

331-
return rest_ensure_response( $this->_get_design_library( $reset ) );
332-
}
333-
334-
public function get_disabled_blocks() {
335-
$disabled_blocks = get_option( 'stackable_block_states' );
336-
337-
if ( $disabled_blocks == false ) {
338-
return false;
339-
}
340-
341-
$disabled_blocks = array_filter( $disabled_blocks, function ( $block_state ) { return $block_state == 3; } );
342-
if ( count( $disabled_blocks ) ) {
343-
$disabled_blocks = array_keys( $disabled_blocks );
344-
$disabled_blocks = array_map( function ( $block ) { return preg_quote( $block, '/' ); }, $disabled_blocks );
345-
$disabled_blocks = '/' . implode( '|', $disabled_blocks ) . '/i';
346-
return $disabled_blocks;
347-
}
348-
349-
return false;
350-
}
351-
352-
public function check_for_disabled_block( $design, $disabled_blocks ) {
353-
if ( preg_match( $disabled_blocks, $design ) ) {
354-
return true;
355-
}
356-
357-
return false;
358-
}
359-
360-
public function get_premium_designs( $content ) {
361-
$designs = $this->get_design_library_from_cloud();
362-
363-
$library = $designs[ self::API_VERSION ];
364-
365-
$premium_designs = array();
366-
foreach ( $library as $design_id => $design ) {
367-
if ( $design[ 'plan' ] === 'premium' && STACKABLE_BUILD === 'premium' && sugb_fs()->can_use_premium_code() ) {
368-
continue;
369-
}
370-
371-
$premium_designs[ $design_id ] = array(
372-
'title' => $design[ 'label' ],
373-
'content' => $design[ 'template' ],
374-
'category' => $design[ 'category' ],
375-
'description' => $design[ 'description' ],
376-
'plan' => $design[ 'plan' ],
377-
'designId' => $design_id
378-
);
379-
}
380-
381-
$merged = array_merge( $content, $premium_designs );
382-
383-
uasort($merged, function( $design_1, $design_2 ) {
384-
return strnatcmp( $design_1[ 'title' ], $design_2[ 'title' ] );
385-
});
386-
387-
return $merged;
388-
}
389-
390-
public function get_designs_with_disabled_blocks( $content ) {
391-
$designs = $this->get_design_library_from_cloud();
392-
393-
$library = $designs[ self::API_VERSION ];
394-
395-
$designs_with_disabled = array();
396-
foreach ( $library as $design_id => $design ) {
397-
if ( isset( $content[ $design_id ] ) ) {
398-
continue;
399-
}
400-
401-
$designs_with_disabled[ $design_id ] = array(
402-
'title' => $design[ 'label' ],
403-
'content' => $design[ 'template' ],
404-
'category' => $design[ 'category' ],
405-
'description' => $design[ 'description' ],
406-
'plan' => $design[ 'plan' ],
407-
'designId' => $design_id,
408-
);
409-
}
410-
411-
$merged = array_merge( $content, $designs_with_disabled );
412-
413-
uasort($merged, function( $design_1, $design_2 ) {
414-
return strnatcmp( $design_1[ 'title' ], $design_2[ 'title' ] );
415-
});
416-
417-
return $merged;
418-
}
419-
420-
public function get_category_kebab_case( $category ) {
421-
$category = trim( strtolower( $category ) );
422-
return preg_replace( '/[^a-z0-9-]+/', '-', $category );
423-
}
424-
425-
public function get_template_with_placeholders( $template, $category ) {
426-
if ( ! class_exists( 'Stackable_Design_Library_Placeholders' ) ) {
427-
return $template;
428-
}
429-
430-
$default_placeholders = Stackable_Design_Library_Placeholders::get_default();
431-
432-
if ( ! isset( $default_placeholders[ $category ] ) ) {
433-
return $template;
434-
}
435-
436-
foreach( $default_placeholders[ $category ] as $placeholder => $value ) {
437-
if ( ! is_string( $value ) ) {
438-
continue;
439-
}
440-
$template = str_replace( $placeholder, $value, $template );
441-
}
442-
443-
return $template;
290+
return rest_ensure_response( $this->get_design_library_from_cloud() );
444291
}
445292

446-
public function register_design_pattern() {
447-
$designs = $this->get_design_library_from_cloud();
448-
449-
$library = $designs[ self::API_VERSION ];
450-
451-
if ( ! $library ) {
452-
return;
453-
}
454-
455-
456-
$disabled_blocks = $this->get_disabled_blocks();
457-
458-
459-
if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'stackable' ) ) {
460-
register_block_pattern_category( 'stackable', [
461-
'label' => __( 'Stackable', STACKABLE_I18N ),
462-
'description' => __( 'Patterns for Stackable Design Library', STACKABLE_I18N ),
463-
] );
464-
}
465-
466-
foreach ( $library as $design_id => $design ) {
467-
if ( $design[ 'plan' ] === 'premium' && ( STACKABLE_BUILD === 'free' || ! sugb_fs()->can_use_premium_code() ) ) {
468-
continue;
469-
}
470-
471-
if ( $disabled_blocks ) {
472-
$has_disabled = $this->check_for_disabled_block( $design[ 'template' ], $disabled_blocks );
473-
if ( $has_disabled ) continue;
474-
}
475-
476-
register_block_pattern_category( 'stackable_' . $this->get_category_kebab_case( $design[ 'category' ] ), [
477-
'label' => sprintf( __( 'Stackable %s', STACKABLE_I18N ), $design[ 'category' ] ),
478-
'description' => sprintf( __( '%s patterns for Stackable Design Library', STACKABLE_I18N ), $design[ 'category' ] ),
479-
] );
480-
481-
register_block_pattern(
482-
'stackable_' . $design_id,
483-
array(
484-
'title' => sprintf( __( 'Stackable %s', STACKABLE_I18N ), $design[ 'label' ] ),
485-
'content' => $this->get_template_with_placeholders( $design[ 'template' ], $design[ 'category' ] ),
486-
'categories' => array( 'stackable_' . $this->get_category_kebab_case( $design[ 'category' ] ), 'stackable' ), // used in Patterns
487-
'category' => $design[ 'category' ], // used in Design Library
488-
'description' => $design[ 'description' ],
489-
'plan' => $design[ 'plan' ],
490-
'designId' => $design_id
491-
)
492-
);
493-
}
494-
}
495293

496294
/**
497295
* Gets the URL of the CDN where to load our design library data. When

0 commit comments

Comments
 (0)