PluginProbe
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts / 2.7.7
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts v2.7.7
2.8.14 2.8.13 2.8.12 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.10 2.8.11 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 trunk 1.0.1 1.0.2 1.1.0 1.1.1 All 147 releases
content-blocks-builder / includes / settings.php

settings.php in Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts 2.7.7, at includes/settings.php

487 lines 14.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The settings: import/export blocks, variants, patterns.
4 *
5 * @package BoldBlocks
6 * @author Phi Phan <mrphipv@gmail.com>
7 * @copyright Copyright (c) 2022, Phi Phan
8 */
9
10 namespace BoldBlocks;
11
12 // Exit if accessed directly.
13 defined( 'ABSPATH' ) || exit;
14
15 if ( ! class_exists( Settings::class ) ) :
16 /**
17 * The controller class for the settings.
18 */
19 class Settings extends CoreComponent {
20 /**
21 * Store the version of core blocks
22 *
23 * @var string
24 */
25 protected $core_blocks_version = '2.7.3';
26
27 /**
28 * The script handle
29 *
30 * @var string
31 */
32 protected $script_handle = 'boldblocks-settings';
33
34 /**
35 * Purged cache status
36 *
37 * @var boolean
38 */
39 protected $is_purged_cache = false;
40
41 /**
42 * Run main hooks
43 *
44 * @return void
45 */
46 public function run() {
47 // Add admin toolbar.
48 add_action( 'in_admin_header', [ $this, 'in_admin_header' ] );
49
50 // Create the settings page.
51 add_action( 'admin_menu', [ $this, 'add_admin_page' ] );
52
53 // Enqueue settings script.
54 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_settings_scripts' ] );
55
56 // Do setting up stuff when the plugin is activated.
57 add_action( 'content_block_builder_activate', [ $this, 'run_the_plugin_setup' ] );
58
59 // Maybe update core blocks.
60 add_action( 'init', [ $this, 'maybe_update_core_blocks' ] );
61
62 // Add rest api endpoint to query docs.
63 add_action( 'rest_api_init', [ $this, 'register_docs_endpoint' ] );
64
65 // Clear the transient cache on upgraded.
66 add_action( 'cbb_version_upgraded', [ $this, 'clear_transient_cache' ] );
67
68 // Change the footer text for the settings pages.
69 add_action( 'admin_footer_text', [ $this, 'admin_footer_text' ] );
70
71 // Purge the cache.
72 add_action( 'admin_init', [ $this, 'boldblocks_purge_cache' ] );
73 }
74
75 /**
76 * Print the admin toolbar.
77 *
78 * @return void
79 */
80 public function in_admin_header() {
81 $screen = get_current_screen();
82 if ( 'boldblocks_block_page_cbb-settings' === $screen->base ) {
83 // Left links.
84 $left_links = [
85 [
86 'url' => 'https://wordpress.org/support/plugin/content-blocks-builder',
87 'title' => __( 'Feedback & Support ↗', 'display-a-meta-field-as-block' ),
88 'target' => '_blank',
89 'icon' => '<span class="dashicons dashicons-editor-help"></span> ',
90 'class' => '',
91 ],
92 [
93 'url' => 'https://wordpress.org/support/plugin/content-blocks-builder/reviews/#new-post',
94 'title' => __( 'Review ↗', 'display-a-meta-field-as-block' ),
95 'target' => '_blank',
96 'icon' => '<span class="dashicons dashicons-star-filled"></span> ',
97 'class' => '',
98 ],
99 ];
100
101 $left_links = apply_filters( 'content_blocks_builder_get_header_left_links', $left_links );
102
103 ?>
104 <div class="cbb-settings-header">
105 <h1><strong><?php printf( __( 'Content Blocks Builder', 'content-blocks-builder' ) ); ?></strong> <code><?php echo esc_html( $this->the_plugin_instance->get_plugin_version() ); ?></code></h1>
106 <ul class="lelf-links">
107 <?php foreach ( $left_links as $link ) : ?>
108 <?php printf( '<li %5$s><a href="%1$s" target="%3$s"%6$s>%4$s%2$s</a></li>', $link['url'], $link['title'], $link['target'], $link['icon'], ( $link['class'] ?? '' ) ? 'class="' . $link['class'] . '"' : '', $link['style'] ?? '' ? ' style="' . $link['style'] . '"' : '' ); ?>
109 <?php endforeach; ?>
110 </ul>
111 </div>
112 <?php
113 }
114 }
115
116 /**
117 * Create the admin page
118 *
119 * @return void
120 */
121 public function add_admin_page() {
122 // Make "Custom Blocks" as parent slug.
123 $parent_slug = 'edit.php?post_type=boldblocks_block';
124
125 add_submenu_page(
126 $parent_slug,
127 __( 'Content Blocks Builder' ),
128 __( 'Settings', 'content-blocks-builder' ),
129 'manage_options',
130 'cbb-settings',
131 function () {
132 ?>
133 <div class="wrap">
134 <h2 class="screen-reader-text">Content Blocks Builder</h2>
135 <div class="boldblocks-settings js-boldblocks-settings-root"></div>
136 </div>
137 <?php
138 }
139 );
140 }
141
142 /**
143 * Enqueue scripts for the settings page.
144 *
145 * @param string $hook_suffix
146 * @return void
147 */
148 public function enqueue_settings_scripts( $hook_suffix ) {
149 // Only load scripts for the settings page.
150 if ( 'boldblocks_block_page_cbb-settings' === $hook_suffix ) {
151 // Settings asset file.
152 $settings_asset = $this->the_plugin_instance->include_file( 'build/settings.asset.php' );
153
154 // Enqueue scripts.
155 wp_enqueue_script(
156 $this->script_handle,
157 $this->the_plugin_instance->get_file_uri( 'build/settings.js' ),
158 $settings_asset['dependencies'] ?? [],
159 $this->the_plugin_instance->get_script_version( $settings_asset ),
160 [ 'in_footer' => true ]
161 );
162
163 // For debuging.
164 $this->the_plugin_instance->enqueue_debug_information( $this->script_handle );
165
166 // Add data to the script.
167 wp_add_inline_script(
168 $this->script_handle,
169 'const CBBSettings=' . wp_json_encode(
170 [
171 'nonce' => wp_create_nonce( 'cbb_purce_nonce' ),
172 'isPurgedCache' => $this->is_purged_cache ? 1 : 0,
173 ]
174 ),
175 'before'
176 );
177
178 // Enqueue style.
179 wp_enqueue_style(
180 $this->script_handle,
181 $this->the_plugin_instance->get_file_uri( 'build/settings.css' ),
182 [],
183 $this->the_plugin_instance->get_script_version( $settings_asset )
184 );
185
186 // Load components style.
187 wp_enqueue_style( 'wp-components' );
188 }
189 }
190
191 /**
192 * Run activated stuff
193 * https://wearnhardt.com/2020/03/redirecting-after-plugin-activation/
194 *
195 * @return void
196 */
197 public function run_the_plugin_setup() {
198 // Deploy core blocks.
199 if ( is_admin() && ! get_option( 'boldblocks_deployed_core_blocks' ) ) {
200 // Update core blocks.
201 $this->update_core_blocks();
202
203 // Add the marker to the settings to make sure this logic only runs one time.
204 add_option( 'boldblocks_deployed_core_blocks', 1 );
205 }
206
207 // Redirect to the getting started page, ignore bulk activation.
208 if (
209 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
210 ! ( ( isset( $_REQUEST['action'] ) && 'activate-selected' === $_REQUEST['action'] ) &&
211 // phpcs:ignore WordPress.Security.NonceVerification.Missing
212 ( isset( $_POST['checked'] ) && count( $_POST['checked'] ) > 1 ) ) ) {
213 add_option( 'boldblocks_activation_redirect', wp_get_current_user()->ID );
214 }
215 }
216
217 /**
218 * Maybe update core blocks
219 *
220 * @return void
221 */
222 public function maybe_update_core_blocks() {
223 if ( is_admin() && current_user_can( 'manage_options' ) ) {
224 $core_blocks_version = get_option( 'boldblocks_core_blocks_version' );
225 if ( ! $core_blocks_version || $core_blocks_version !== $this->core_blocks_version ) {
226 $this->update_core_blocks( $core_blocks_version, $this->core_blocks_version );
227 }
228 }
229 }
230
231 /**
232 * Parse core blocks
233 *
234 * @return array
235 */
236 private function get_core_blocks() {
237 $core_blocks = false;
238 $core_blocks_file_path = $this->the_plugin_instance->get_file_path( 'data/core-blocks/blocks.json' );
239
240 if ( \file_exists( $core_blocks_file_path ) ) {
241 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
242 $core_blocks = \file_get_contents( $core_blocks_file_path );
243 $core_blocks = \json_decode( $core_blocks, true );
244 }
245
246 return $core_blocks;
247 }
248
249 /**
250 * Update core blocks
251 *
252 * @param string $prev_blocks_version
253 * @param string $next_blocks_version
254 * @return void
255 */
256 private function update_core_blocks( $prev_blocks_version = '', $next_blocks_version = '' ) {
257 if ( is_admin() ) {
258 $core_blocks_data = $this->get_core_blocks();
259
260 if ( $core_blocks_data ) {
261 $core_blocks = $core_blocks_data['blocks'] ?? [];
262
263 foreach ( $core_blocks as $block ) {
264 $name = $block['slug'] ?? '';
265 $found_posts = get_posts(
266 [
267 'post_type' => 'boldblocks_block',
268 'name' => $name,
269 'posts_per_page' => 1,
270 ]
271 );
272
273 // There is no block with that slug.
274 if ( ! ( is_array( $found_posts ) && count( $found_posts ) > 0 ) ) {
275 $title = $block['title'] ?? '';
276 $content = $block['content'] ?? '';
277 $meta = $block['meta'] ?? [];
278
279 // Insert new block.
280 $block_id = wp_insert_post(
281 [
282 'post_type' => 'boldblocks_block',
283 'post_title' => $title,
284 'post_content' => $content,
285 'post_name' => $name,
286 'post_status' => 'publish',
287 'meta_input' => $meta,
288 ]
289 );
290 } else {
291 $found_post = $found_posts[0];
292 $meta = $block['meta'] ?? [];
293
294 $updating_meta = [];
295 $title = $found_post->post_title ?? '';
296 $description = $meta['boldblocks_block_description'] ?? '';
297 if ( is_array( $description ) && count( $description ) > 0 ) {
298 $description = $description[0] ?? '';
299 }
300 $updating_meta['boldblocks_block_description'] = $description;
301 $icon = $meta['boldblocks_block_icon'] ?? '';
302 if ( is_array( $icon ) && count( $icon ) > 0 ) {
303 $icon = $icon[0] ?? '';
304 }
305 $updating_meta['boldblocks_block_icon'] = $icon;
306
307 $supports = $meta['boldblocks_block_supports'] ?? [];
308 if ( ! empty( $supports ) ) {
309 $updating_meta['boldblocks_block_supports'] = $supports;
310 }
311
312 if ( version_compare( $prev_blocks_version, '2.3.4', '<' ) ) {
313 $updating_meta['boldblocks_enable_custom_attributes'] = $meta['boldblocks_enable_custom_attributes'] ?? false;
314 }
315
316 // New transformable in 2.4.1.
317 if ( $meta['boldblocks_is_transformable'] ?? false ) {
318 $updating_meta['boldblocks_is_transformable'] = $meta['boldblocks_is_transformable'];
319 }
320
321 $enable_repeater = $meta['boldblocks_enable_repeater'] ?? '';
322 if ( is_array( $enable_repeater ) && count( $enable_repeater ) > 0 ) {
323 $enable_repeater = $enable_repeater[0] ?? '';
324 }
325
326 if ( $enable_repeater ) {
327 $parent_title = $meta['boldblocks_parent_block_title'] ?? '';
328 if ( is_array( $parent_title ) && count( $parent_title ) > 0 ) {
329 $parent_title = $parent_title[0] ?? '';
330 }
331 $updating_meta['boldblocks_parent_block_title'] = $parent_title;
332
333 $parent_description = $meta['boldblocks_parent_block_description'] ?? '';
334 if ( is_array( $parent_description ) && count( $parent_description ) > 0 ) {
335 $parent_description = $parent_description[0] ?? '';
336 }
337 $updating_meta['boldblocks_parent_block_description'] = $parent_description;
338
339 $parent_icon = $meta['boldblocks_parent_block_icon'] ?? '';
340 if ( is_array( $parent_icon ) && count( $parent_icon ) > 0 ) {
341 $parent_icon = $parent_icon[0] ?? '';
342 }
343 $updating_meta['boldblocks_parent_block_icon'] = $parent_icon;
344
345 $parent_supports = $meta['boldblocks_parent_block_supports'] ?? [];
346 if ( ! empty( $parent_supports ) ) {
347 $updating_meta['boldblocks_parent_block_supports'] = $parent_supports;
348 }
349
350 if ( version_compare( $prev_blocks_version, '2.3.4', '<' ) ) {
351 $updating_meta['boldblocks_parent_enable_custom_attributes'] = $meta['boldblocks_parent_enable_custom_attributes'] ?? false;
352 }
353 }
354
355 $updating_data = [
356 'ID' => $found_post->ID,
357 'post_title' => $block['title'] ?? '',
358 'meta_input' => $updating_meta,
359 ];
360
361 $post_id = wp_update_post( $updating_data );
362 }
363 }
364
365 update_option( 'boldblocks_core_blocks_version', $this->core_blocks_version );
366 }
367 }
368 }
369
370 /**
371 * Build a custom endpoint to query docs.
372 *
373 * @return void
374 */
375 public function register_docs_endpoint() {
376 register_rest_route(
377 'boldblocks/v1',
378 '/getDocs/',
379 array(
380 'methods' => 'GET',
381 'callback' => [ $this, 'get_docs' ],
382 'permission_callback' => function () {
383 return current_user_can( 'publish_posts' );
384 },
385 )
386 );
387 }
388
389 /**
390 * Get docs.
391 *
392 * @param WP_REST_Request $request The request object.
393 * @return void
394 */
395 public function get_docs( $request ) {
396 $cache_key = 'bb_docs';
397 $data = get_transient( $cache_key );
398 $library_component = $this->the_plugin_instance->get_component( Library::class );
399 $library_url = $library_component ? $library_component->get_library_url() : 'https://boldpatterns.net';
400
401 if ( false === $data ) {
402 $response = wp_remote_get(
403 $library_url . '/wp-json/api.boldblocks/v1/getDocs',
404 [
405 'timeout' => 120,
406 'sslverify' => false,
407 ]
408 );
409
410 if ( ! is_wp_error( $response ) ) {
411 $data = json_decode( wp_remote_retrieve_body( $response ), true );
412
413 if ( $data ) {
414 set_transient( $cache_key, $data, DAY_IN_SECONDS );
415 }
416 }
417 }
418
419 wp_send_json(
420 [
421 'data' => $data,
422 'success' => true,
423 ]
424 );
425 }
426
427 /**
428 * Clear transient cache
429 *
430 * @return void
431 */
432 public function clear_transient_cache() {
433 delete_transient( 'bb_docs' );
434 }
435
436 /**
437 * Change the footer text for the settings pages
438 *
439 * @param string $footer_text
440 * @return string
441 */
442 public function admin_footer_text( $footer_text ) {
443 // Get current screen.
444 $current_screen = get_current_screen();
445 if ( 'edit.php?post_type=boldblocks_block' === $current_screen->parent_file ) {
446 $footer_text = '<i><a href="https://contentblocksbuilder.com/?utm_source=CBB&utm_campaign=CBB+visit+site&utm_medium=link&utm_content=footer-text" target="_blank" title="' . __( 'Visit the Plugin website', 'content-blocks-builder' ) . '" style="text-decoration:none"><strong>CBB</strong></a> <code>' . esc_html__( $this->the_plugin_instance->get_plugin_version() ) . '</code>. Please <a target="_blank" href="https://wordpress.org/support/plugin/content-blocks-builder/reviews/#new-post" title="Rate the plugin" style="text-decoration:none">rate the plugin <span style="color:#ffb900">�
447
448
449
450
451 </span></a> to help us spread the word. Thank you from the CBB team!</i>';
452 }
453
454 return $footer_text;
455 }
456
457 /**
458 * Delete the entire cache contents.
459 *
460 * @return void
461 */
462 public function boldblocks_purge_cache() {
463 $nonce = sanitize_text_field( wp_unslash( $_REQUEST['_cbb_purge'] ?? '' ) );
464 if ( wp_verify_nonce( $nonce, 'cbb_purce_nonce' ) ) {
465 $cbb = $this->the_plugin_instance;
466
467 // Library cache.
468 $cbb->get_component( Library::class )->clear_library_cache();
469
470 // Blocks.
471 $cbb->get_component( CustomBlocks::class )->clear_transient_cache();
472
473 // Variations.
474 $cbb->get_component( Variations::class )->clear_transient_cache();
475
476 // Patterns.
477 $cbb->get_component( Patterns::class )->clear_transient_cache();
478
479 // Docs.
480 $this->clear_transient_cache();
481
482 $this->is_purged_cache = 1;
483 }
484 }
485 }
486 endif;
487