PluginProbe
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts / 2.8.0
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts v2.8.0
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 / content-blocks-builder.php

content-blocks-builder.php in Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts 2.8.0, at content-blocks-builder.php

395 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Name: Content Blocks Builder
5 * Plugin URI: https://contentblocksbuilder.com?utm_source=CBB&utm_campaign=CBB+visit+site&utm_medium=link&utm_content=Plugin+URI
6 * Description: Create blocks by wrapping other blocks into containers or repeaters to create layouts like grid, carousel, popup, accordion — all in the Block Editor. Fast. Easy. Bloat-free.
7 * Requires at least: 6.6
8 * Requires PHP: 7.4
9 * Version: 2.8.0
10 * Author: Phi Phan
11 * Author URI: https://contentblocksbuilder.com?utm_source=CBB&utm_campaign=CBB+visit+site&utm_medium=link&utm_content=Author+URI
12 * License: GPL-3.0
13 *
14 * @package BoldBlocks
15 * @copyright Copyright(c) 2022, Phi Phan
16 *
17 */
18 namespace BoldBlocks;
19
20 // Exit if accessed directly.
21 defined( 'ABSPATH' ) || exit;
22 if ( function_exists( __NAMESPACE__ . '\\cbb_fs' ) ) {
23 cbb_fs()->set_basename( false, __FILE__ );
24 return;
25 }
26 // Include Freemius functions.
27 require_once dirname( __FILE__ ) . '/freemius.php';
28 if ( !class_exists( ContentBlocksBuilder::class ) ) {
29 /**
30 * The main class
31 */
32 class ContentBlocksBuilder {
33 /**
34 * Plugin version
35 *
36 * @var String
37 */
38 public $version = '2.8.0';
39
40 /**
41 * Components
42 *
43 * @var Array
44 */
45 protected $components = [];
46
47 /**
48 * The suffix for scripts
49 *
50 * @var string
51 */
52 protected $script_suffix = '';
53
54 /**
55 * Plugin instance
56 *
57 * @var ContentBlocksBuilder
58 */
59 private static $instance;
60
61 /**
62 * A dummy constructor
63 */
64 private function __construct() {
65 }
66
67 /**
68 * Initialize the instance.
69 *
70 * @return ContentBlocksBuilder
71 */
72 public static function get_instance() {
73 if ( !isset( self::$instance ) ) {
74 self::$instance = new ContentBlocksBuilder();
75 self::$instance->initialize();
76 }
77 return self::$instance;
78 }
79
80 /**
81 * Kick start function.
82 * Define constants
83 * Load dependencies
84 * Register components
85 * Run the main hooks
86 *
87 * @return void
88 */
89 public function initialize() {
90 // Setup constants.
91 $this->setup_constants();
92 // Load dependencies.
93 $this->load_dependencies();
94 // Register components.
95 $this->register_components();
96 // Run hooks.
97 $this->run();
98 }
99
100 /**
101 * Setup constants
102 *
103 * @return void
104 */
105 public function setup_constants() {
106 $this->define_constant( 'BOLDBLOCKS_CBB', true );
107 $this->define_constant( 'BOLDBLOCKS_CBB_ROOT_FILE', __FILE__ );
108 $this->define_constant( 'BOLDBLOCKS_CBB_VERSION', $this->version );
109 $this->define_constant( 'BOLDBLOCKS_CBB_PATH', trailingslashit( plugin_dir_path( BOLDBLOCKS_CBB_ROOT_FILE ) ) );
110 $this->define_constant( 'BOLDBLOCKS_CBB_URL', trailingslashit( plugin_dir_url( BOLDBLOCKS_CBB_ROOT_FILE ) ) );
111 $this->define_constant( 'BOLDBLOCKS_CBB_EDITOR_SCRIPTS_HANDLE', 'boldblocks-editor-scripts' );
112 $this->define_constant( 'BOLDBLOCKS_CBB_EDITOR_STYLE_HANDLE', 'boldblocks-editor-style' );
113 $this->define_constant( 'BOLDBLOCKS_CBB_FRONTEND_STYLE_HANDLE', 'boldblocks-frontend-style' );
114 }
115
116 /**
117 * Load components
118 *
119 * @return void
120 */
121 public function register_components() {
122 // Load & register core components: custom blocks, patterns, variations, etc.
123 $components = [
124 'includes/custom-blocks.php' => CustomBlocks::class,
125 'includes/variations.php' => Variations::class,
126 'includes/patterns.php' => Patterns::class,
127 'includes/copy-post.php' => CopyPost::class,
128 'includes/custom-style.php' => CustomStyle::class,
129 'includes/meta-revisioning.php' => MetaRevisioning::class,
130 'includes/settings.php' => Settings::class,
131 'includes/freemius-config.php' => FreemiusConfig::class,
132 'includes/theme.php' => Theme::class,
133 'includes/icon-library.php' => IconLibrary::class,
134 'includes/typography.php' => Typography::class,
135 'includes/library.php' => Library::class,
136 'includes/maintenance.php' => Maintenance::class,
137 'includes/block-overrides.php' => BlockOverrides::class,
138 ];
139 foreach ( $components as $file => $classname ) {
140 $this->register_component( $file, $classname );
141 }
142 // Register additional components.
143 do_action( 'cbb/register_components', $this );
144 }
145
146 /**
147 * Load dependencies
148 *
149 * @return void
150 */
151 public function load_dependencies() {
152 // Load core component.
153 $this->include_file( 'includes/core-component.php' );
154 // Load post type helper.
155 $this->include_file( 'includes/post-type.php' );
156 }
157
158 /**
159 * Run main hooks
160 *
161 * @return void
162 */
163 public function run() {
164 // Init hook.
165 add_action( 'init', [$this, 'init'], 5 );
166 // Load translations.
167 add_action( 'init', [$this, 'load_textdomain'] );
168 // Enqueue scripts for editor.
169 add_action( 'enqueue_block_assets', [$this, 'enqueue_block_assets'] );
170 // Save version and trigger upgraded hook.
171 add_action( 'admin_menu', [$this, 'version_upgrade'], 1 );
172 // Run all components.
173 foreach ( $this->components as $component ) {
174 $component->run();
175 }
176 }
177
178 /**
179 * Process on init hook
180 *
181 * @return void
182 */
183 public function init() {
184 /**
185 * Fires when CBB is initialized.
186 * Run before components are running.
187 *
188 * @since 2.7.12 Use `cbb/init` instead of `boldblocks/init`.
189 */
190 do_action( 'cbb/init' );
191 do_action( 'boldblocks/init' );
192 // Deprecated.
193 }
194
195 /**
196 * Enqueue editor assets
197 *
198 * @return void
199 */
200 public function enqueue_block_assets() {
201 // Only load in the admin side.
202 if ( !is_admin() ) {
203 return;
204 }
205 // Index access file.
206 $index_asset = $this->include_file( 'build/index.asset.php' );
207 // Scripts.
208 wp_enqueue_script(
209 BOLDBLOCKS_CBB_EDITOR_SCRIPTS_HANDLE,
210 $this->get_file_uri( 'build/index.js' ),
211 $index_asset['dependencies'] ?? [],
212 $this->get_script_version( $index_asset ),
213 true
214 );
215 // For debugging.
216 $this->enqueue_debug_information( BOLDBLOCKS_CBB_EDITOR_SCRIPTS_HANDLE );
217 // Styles.
218 wp_enqueue_style(
219 BOLDBLOCKS_CBB_EDITOR_STYLE_HANDLE,
220 $this->get_file_uri( 'build/index.css' ),
221 [],
222 $this->get_script_version( $index_asset )
223 );
224 }
225
226 /**
227 * Save version and trigger an upgrade hook
228 *
229 * @return void
230 */
231 public function version_upgrade() {
232 if ( get_option( 'cbb_current_version' ) !== $this->version ) {
233 do_action( 'cbb_version_upgraded', get_option( 'cbb_current_version' ), $this->version );
234 update_option( 'cbb_current_version', $this->version );
235 }
236 }
237
238 /**
239 * Load text domain
240 *
241 * @return void
242 */
243 public function load_textdomain() {
244 load_plugin_textdomain( 'content-blocks-builder', false, plugin_basename( realpath( __DIR__ . '/languages' ) ) );
245 }
246
247 /**
248 * Register component
249 *
250 * @param string $file The file path of the component.
251 * @param string $classname The class name of the component.
252 * @return void
253 */
254 public function register_component( $file, $classname ) {
255 if ( $this->include_file( $file ) ) {
256 $this->components[$classname] = new $classname($this);
257 }
258 }
259
260 /**
261 * Get a component by class name
262 *
263 * @param string $classname The class name of the component.
264 * @return mixed
265 */
266 public function get_component( $classname ) {
267 return $this->components[$classname] ?? false;
268 }
269
270 /**
271 * Define constant
272 *
273 * @param string $name The name of the constant.
274 * @param mixed $value The value of the constant.
275 * @return void
276 */
277 public function define_constant( $name, $value ) {
278 if ( !defined( $name ) ) {
279 define( $name, $value );
280 }
281 }
282
283 /**
284 * Return file path for file or folder.
285 *
286 * @param string $path file path.
287 * @return string
288 */
289 public function get_file_path( $path ) {
290 return BOLDBLOCKS_CBB_PATH . $path;
291 }
292
293 /**
294 * Include file path.
295 *
296 * @param string $path file path.
297 * @return mixed
298 */
299 public function include_file( $path ) {
300 $file_path = $this->get_file_path( $path );
301 if ( !file_exists( $file_path ) ) {
302 if ( $this->is_debug_mode() ) {
303 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
304 error_log( "[CBB]: Missing file: {$file_path}" );
305 }
306 return false;
307 }
308 return include_once $file_path;
309 }
310
311 /**
312 * Get file uri by file path.
313 *
314 * @param string $path file path.
315 * @return string
316 */
317 public function get_file_uri( $path ) {
318 return BOLDBLOCKS_CBB_URL . $path;
319 }
320
321 /**
322 * Create version for scripts/styles
323 *
324 * @param array $asset_file
325 * @return string
326 */
327 public function get_script_version( $asset_file ) {
328 return ( wp_get_environment_type() !== 'production' ? $asset_file['version'] ?? BOLDBLOCKS_CBB_VERSION : BOLDBLOCKS_CBB_VERSION );
329 }
330
331 /**
332 * Get the suffix for the scripts
333 *
334 * @return string
335 */
336 public function get_script_suffix() {
337 return $this->script_suffix;
338 }
339
340 /**
341 * Get the plugin version
342 *
343 * @return string
344 */
345 public function get_plugin_version() {
346 return $this->version;
347 }
348
349 /**
350 * Is Debugging CBB
351 *
352 * @return boolean
353 */
354 public function is_debug_mode() {
355 return defined( 'CBB_DEBUG' ) && CBB_DEBUG || 'development' === wp_get_environment_type();
356 }
357
358 /**
359 * Enqueue debug log information
360 *
361 * @param string $handle
362 * @return void
363 */
364 public function enqueue_debug_information( $handle ) {
365 wp_add_inline_script( $handle, 'var BBLOG=' . wp_json_encode( [
366 'environmentType' => ( $this->is_debug_mode() ? 'development' : wp_get_environment_type() ),
367 ] ), 'before' );
368 }
369
370 }
371
372 /**
373 * Kick start
374 *
375 * @return ContentBlocksBuilder instance
376 */
377 function boldblocks_cbb_get_instance() {
378 return ContentBlocksBuilder::get_instance();
379 }
380
381 // Instantiate.
382 boldblocks_cbb_get_instance();
383 }
384 if ( !function_exists( __NAMESPACE__ . '\\content_block_builder_activate' ) ) {
385 /**
386 * Trigger an action when the plugin is activated.
387 *
388 * @return void
389 */
390 function content_block_builder_activate() {
391 do_action( 'content_block_builder_activate' );
392 }
393
394 register_activation_hook( __FILE__, __NAMESPACE__ . '\\content_block_builder_activate' );
395 }