PluginProbe
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts / 2.7.8
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts v2.7.8
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.8, at includes/settings.php

490 lines 14.7 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 ↗', 'content-blocks-builder' ),
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 ↗', 'content-blocks-builder' ),
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 esc_html_e( '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
109 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
110 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'] . '"' : '' );
111 ?>
112 <?php endforeach; ?>
113 </ul>
114 </div>
115 <?php
116 }
117 }
118
119 /**
120 * Create the admin page
121 *
122 * @return void
123 */
124 public function add_admin_page() {
125 // Make "Custom Blocks" as parent slug.
126 $parent_slug = 'edit.php?post_type=boldblocks_block';
127
128 add_submenu_page(
129 $parent_slug,
130 'Content Blocks Builder',
131 __( 'Settings', 'content-blocks-builder' ),
132 'manage_options',
133 'cbb-settings',
134 function () {
135 ?>
136 <div class="wrap">
137 <h2 class="screen-reader-text">Content Blocks Builder</h2>
138 <div class="boldblocks-settings js-boldblocks-settings-root"></div>
139 </div>
140 <?php
141 }
142 );
143 }
144
145 /**
146 * Enqueue scripts for the settings page.
147 *
148 * @param string $hook_suffix
149 * @return void
150 */
151 public function enqueue_settings_scripts( $hook_suffix ) {
152 // Only load scripts for the settings page.
153 if ( 'boldblocks_block_page_cbb-settings' === $hook_suffix ) {
154 // Settings asset file.
155 $settings_asset = $this->the_plugin_instance->include_file( 'build/settings.asset.php' );
156
157 // Enqueue scripts.
158 wp_enqueue_script(
159 $this->script_handle,
160 $this->the_plugin_instance->get_file_uri( 'build/settings.js' ),
161 $settings_asset['dependencies'] ?? [],
162 $this->the_plugin_instance->get_script_version( $settings_asset ),
163 [ 'in_footer' => true ]
164 );
165
166 // For debuging.
167 $this->the_plugin_instance->enqueue_debug_information( $this->script_handle );
168
169 // Add data to the script.
170 wp_add_inline_script(
171 $this->script_handle,
172 'const CBBSettings=' . wp_json_encode(
173 [
174 'nonce' => wp_create_nonce( 'cbb_purce_nonce' ),
175 'isPurgedCache' => $this->is_purged_cache ? 1 : 0,
176 ]
177 ),
178 'before'
179 );
180
181 // Enqueue style.
182 wp_enqueue_style(
183 $this->script_handle,
184 $this->the_plugin_instance->get_file_uri( 'build/settings.css' ),
185 [],
186 $this->the_plugin_instance->get_script_version( $settings_asset )
187 );
188
189 // Load components style.
190 wp_enqueue_style( 'wp-components' );
191 }
192 }
193
194 /**
195 * Run activated stuff
196 * https://wearnhardt.com/2020/03/redirecting-after-plugin-activation/
197 *
198 * @return void
199 */
200 public function run_the_plugin_setup() {
201 // Deploy core blocks.
202 if ( is_admin() && ! get_option( 'boldblocks_deployed_core_blocks' ) ) {
203 // Update core blocks.
204 $this->update_core_blocks();
205
206 // Add the marker to the settings to make sure this logic only runs one time.
207 add_option( 'boldblocks_deployed_core_blocks', 1 );
208 }
209
210 // Redirect to the getting started page, ignore bulk activation.
211 if (
212 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
213 ! ( ( isset( $_REQUEST['action'] ) && 'activate-selected' === $_REQUEST['action'] ) &&
214 // phpcs:ignore WordPress.Security.NonceVerification.Missing
215 ( isset( $_POST['checked'] ) && count( $_POST['checked'] ) > 1 ) ) ) {
216 add_option( 'boldblocks_activation_redirect', wp_get_current_user()->ID );
217 }
218 }
219
220 /**
221 * Maybe update core blocks
222 *
223 * @return void
224 */
225 public function maybe_update_core_blocks() {
226 if ( is_admin() && current_user_can( 'manage_options' ) ) {
227 $core_blocks_version = get_option( 'boldblocks_core_blocks_version' );
228 if ( ! $core_blocks_version || $core_blocks_version !== $this->core_blocks_version ) {
229 $this->update_core_blocks( $core_blocks_version, $this->core_blocks_version );
230 }
231 }
232 }
233
234 /**
235 * Parse core blocks
236 *
237 * @return array
238 */
239 private function get_core_blocks() {
240 $core_blocks = false;
241 $core_blocks_file_path = $this->the_plugin_instance->get_file_path( 'data/core-blocks/blocks.json' );
242
243 if ( \file_exists( $core_blocks_file_path ) ) {
244 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
245 $core_blocks = \file_get_contents( $core_blocks_file_path );
246 $core_blocks = \json_decode( $core_blocks, true );
247 }
248
249 return $core_blocks;
250 }
251
252 /**
253 * Update core blocks
254 *
255 * @param string $prev_blocks_version
256 * @param string $next_blocks_version
257 * @return void
258 */
259 private function update_core_blocks( $prev_blocks_version = '', $next_blocks_version = '' ) {
260 if ( is_admin() ) {
261 $core_blocks_data = $this->get_core_blocks();
262
263 if ( $core_blocks_data ) {
264 $core_blocks = $core_blocks_data['blocks'] ?? [];
265
266 foreach ( $core_blocks as $block ) {
267 $name = $block['slug'] ?? '';
268 $found_posts = get_posts(
269 [
270 'post_type' => 'boldblocks_block',
271 'name' => $name,
272 'posts_per_page' => 1,
273 ]
274 );
275
276 // There is no block with that slug.
277 if ( ! ( is_array( $found_posts ) && count( $found_posts ) > 0 ) ) {
278 $title = $block['title'] ?? '';
279 $content = $block['content'] ?? '';
280 $meta = $block['meta'] ?? [];
281
282 // Insert new block.
283 $block_id = wp_insert_post(
284 [
285 'post_type' => 'boldblocks_block',
286 'post_title' => $title,
287 'post_content' => $content,
288 'post_name' => $name,
289 'post_status' => 'publish',
290 'meta_input' => $meta,
291 ]
292 );
293 } else {
294 $found_post = $found_posts[0];
295 $meta = $block['meta'] ?? [];
296
297 $updating_meta = [];
298 $title = $found_post->post_title ?? '';
299 $description = $meta['boldblocks_block_description'] ?? '';
300 if ( is_array( $description ) && count( $description ) > 0 ) {
301 $description = $description[0] ?? '';
302 }
303 $updating_meta['boldblocks_block_description'] = $description;
304 $icon = $meta['boldblocks_block_icon'] ?? '';
305 if ( is_array( $icon ) && count( $icon ) > 0 ) {
306 $icon = $icon[0] ?? '';
307 }
308 $updating_meta['boldblocks_block_icon'] = $icon;
309
310 $supports = $meta['boldblocks_block_supports'] ?? [];
311 if ( ! empty( $supports ) ) {
312 $updating_meta['boldblocks_block_supports'] = $supports;
313 }
314
315 if ( version_compare( $prev_blocks_version, '2.3.4', '<' ) ) {
316 $updating_meta['boldblocks_enable_custom_attributes'] = $meta['boldblocks_enable_custom_attributes'] ?? false;
317 }
318
319 // New transformable in 2.4.1.
320 if ( $meta['boldblocks_is_transformable'] ?? false ) {
321 $updating_meta['boldblocks_is_transformable'] = $meta['boldblocks_is_transformable'];
322 }
323
324 $enable_repeater = $meta['boldblocks_enable_repeater'] ?? '';
325 if ( is_array( $enable_repeater ) && count( $enable_repeater ) > 0 ) {
326 $enable_repeater = $enable_repeater[0] ?? '';
327 }
328
329 if ( $enable_repeater ) {
330 $parent_title = $meta['boldblocks_parent_block_title'] ?? '';
331 if ( is_array( $parent_title ) && count( $parent_title ) > 0 ) {
332 $parent_title = $parent_title[0] ?? '';
333 }
334 $updating_meta['boldblocks_parent_block_title'] = $parent_title;
335
336 $parent_description = $meta['boldblocks_parent_block_description'] ?? '';
337 if ( is_array( $parent_description ) && count( $parent_description ) > 0 ) {
338 $parent_description = $parent_description[0] ?? '';
339 }
340 $updating_meta['boldblocks_parent_block_description'] = $parent_description;
341
342 $parent_icon = $meta['boldblocks_parent_block_icon'] ?? '';
343 if ( is_array( $parent_icon ) && count( $parent_icon ) > 0 ) {
344 $parent_icon = $parent_icon[0] ?? '';
345 }
346 $updating_meta['boldblocks_parent_block_icon'] = $parent_icon;
347
348 $parent_supports = $meta['boldblocks_parent_block_supports'] ?? [];
349 if ( ! empty( $parent_supports ) ) {
350 $updating_meta['boldblocks_parent_block_supports'] = $parent_supports;
351 }
352
353 if ( version_compare( $prev_blocks_version, '2.3.4', '<' ) ) {
354 $updating_meta['boldblocks_parent_enable_custom_attributes'] = $meta['boldblocks_parent_enable_custom_attributes'] ?? false;
355 }
356 }
357
358 $updating_data = [
359 'ID' => $found_post->ID,
360 'post_title' => $block['title'] ?? '',
361 'meta_input' => $updating_meta,
362 ];
363
364 $post_id = wp_update_post( $updating_data );
365 }
366 }
367
368 update_option( 'boldblocks_core_blocks_version', $this->core_blocks_version );
369 }
370 }
371 }
372
373 /**
374 * Build a custom endpoint to query docs.
375 *
376 * @return void
377 */
378 public function register_docs_endpoint() {
379 register_rest_route(
380 'boldblocks/v1',
381 '/getDocs/',
382 array(
383 'methods' => 'GET',
384 'callback' => [ $this, 'get_docs' ],
385 'permission_callback' => function () {
386 return current_user_can( 'publish_posts' );
387 },
388 )
389 );
390 }
391
392 /**
393 * Get docs.
394 *
395 * @param WP_REST_Request $request The request object.
396 * @return void
397 */
398 public function get_docs( $request ) {
399 $cache_key = 'bb_docs';
400 $data = get_transient( $cache_key );
401 $library_component = $this->the_plugin_instance->get_component( Library::class );
402 $library_url = $library_component ? $library_component->get_library_url() : 'https://boldpatterns.net';
403
404 if ( false === $data ) {
405 $response = wp_remote_get(
406 $library_url . '/wp-json/api.boldblocks/v1/getDocs',
407 [
408 'timeout' => 120,
409 'sslverify' => false,
410 ]
411 );
412
413 if ( ! is_wp_error( $response ) ) {
414 $data = json_decode( wp_remote_retrieve_body( $response ), true );
415
416 if ( $data ) {
417 set_transient( $cache_key, $data, DAY_IN_SECONDS );
418 }
419 }
420 }
421
422 wp_send_json(
423 [
424 'data' => $data,
425 'success' => true,
426 ]
427 );
428 }
429
430 /**
431 * Clear transient cache
432 *
433 * @return void
434 */
435 public function clear_transient_cache() {
436 delete_transient( 'bb_docs' );
437 }
438
439 /**
440 * Change the footer text for the settings pages
441 *
442 * @param string $footer_text
443 * @return string
444 */
445 public function admin_footer_text( $footer_text ) {
446 // Get current screen.
447 $current_screen = get_current_screen();
448 if ( 'edit.php?post_type=boldblocks_block' === $current_screen->parent_file ) {
449 $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">�
450
451
452
453
454 </span></a> to help us spread the word. Thank you from the CBB team!</i>';
455 }
456
457 return $footer_text;
458 }
459
460 /**
461 * Delete the entire cache contents.
462 *
463 * @return void
464 */
465 public function boldblocks_purge_cache() {
466 $nonce = sanitize_text_field( wp_unslash( $_REQUEST['_cbb_purge'] ?? '' ) );
467 if ( wp_verify_nonce( $nonce, 'cbb_purce_nonce' ) ) {
468 $cbb = $this->the_plugin_instance;
469
470 // Library cache.
471 $cbb->get_component( Library::class )->clear_library_cache();
472
473 // Blocks.
474 $cbb->get_component( CustomBlocks::class )->clear_transient_cache();
475
476 // Variations.
477 $cbb->get_component( Variations::class )->clear_transient_cache();
478
479 // Patterns.
480 $cbb->get_component( Patterns::class )->clear_transient_cache();
481
482 // Docs.
483 $this->clear_transient_cache();
484
485 $this->is_purged_cache = 1;
486 }
487 }
488 }
489 endif;
490