| 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.9 |
| 8 |
* Requires PHP: 8.0 |
| 9 |
* Version: 2.8.13 |
| 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 __DIR__ . '/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.13'; |
| 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 |
* The 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 |
// Enqueue scripts for editor. |
| 167 |
add_action( 'enqueue_block_assets', [$this, 'enqueue_block_assets'] ); |
| 168 |
// Save version and trigger upgraded hook. |
| 169 |
add_action( 'plugins_loaded', [$this, 'version_upgrade'], 1 ); |
| 170 |
// Run all components. |
| 171 |
foreach ( $this->components as $component ) { |
| 172 |
$component->run(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Process on init hook |
| 178 |
* |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
public function init() { |
| 182 |
/** |
| 183 |
* Fires when CBB is initialized. |
| 184 |
* Run before components are running. |
| 185 |
* |
| 186 |
* @since 2.7.12 Use `cbb/init` instead of `boldblocks/init`. |
| 187 |
*/ |
| 188 |
do_action( 'cbb/init' ); |
| 189 |
do_action( 'boldblocks/init' ); |
| 190 |
// Deprecated. |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Enqueue editor assets |
| 195 |
* |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
public function enqueue_block_assets() { |
| 199 |
// Only load in the admin side. |
| 200 |
if ( !is_admin() ) { |
| 201 |
return; |
| 202 |
} |
| 203 |
// Index access file. |
| 204 |
$index_asset = $this->include_file( 'build/index.asset.php' ); |
| 205 |
// Scripts. |
| 206 |
wp_enqueue_script( |
| 207 |
BOLDBLOCKS_CBB_EDITOR_SCRIPTS_HANDLE, |
| 208 |
$this->get_file_uri( 'build/index.js' ), |
| 209 |
$index_asset['dependencies'] ?? [], |
| 210 |
$this->get_script_version( $index_asset ), |
| 211 |
true |
| 212 |
); |
| 213 |
// For debugging. |
| 214 |
$this->enqueue_debug_information( BOLDBLOCKS_CBB_EDITOR_SCRIPTS_HANDLE ); |
| 215 |
// Styles. |
| 216 |
wp_enqueue_style( |
| 217 |
BOLDBLOCKS_CBB_EDITOR_STYLE_HANDLE, |
| 218 |
$this->get_file_uri( 'build/index.css' ), |
| 219 |
[], |
| 220 |
$this->get_script_version( $index_asset ) |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Save version and trigger an upgrade hook |
| 226 |
* |
| 227 |
* @return void |
| 228 |
*/ |
| 229 |
public function version_upgrade() { |
| 230 |
if ( get_option( 'cbb_current_version' ) !== $this->version ) { |
| 231 |
do_action( 'cbb/version_upgraded', get_option( 'cbb_current_version' ), $this->version ); |
| 232 |
update_option( 'cbb_current_version', $this->version ); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Register component |
| 238 |
* |
| 239 |
* @param string $file The file path of the component. |
| 240 |
* @param string $classname The class name of the component. |
| 241 |
* @return void |
| 242 |
*/ |
| 243 |
public function register_component( $file, $classname ) { |
| 244 |
if ( $this->include_file( $file ) ) { |
| 245 |
$this->components[$classname] = new $classname($this); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Get a component by class name |
| 251 |
* |
| 252 |
* @param string $classname The class name of the component. |
| 253 |
* @return mixed |
| 254 |
*/ |
| 255 |
public function get_component( $classname ) { |
| 256 |
return $this->components[$classname] ?? false; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Define constant |
| 261 |
* |
| 262 |
* @param string $name The name of the constant. |
| 263 |
* @param mixed $value The value of the constant. |
| 264 |
* @return void |
| 265 |
*/ |
| 266 |
public function define_constant( $name, $value ) { |
| 267 |
if ( !defined( $name ) ) { |
| 268 |
define( $name, $value ); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Return file path for file or folder. |
| 274 |
* |
| 275 |
* @param string $path file path. |
| 276 |
* @return string |
| 277 |
*/ |
| 278 |
public function get_file_path( $path ) { |
| 279 |
return BOLDBLOCKS_CBB_PATH . $path; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Include file path. |
| 284 |
* |
| 285 |
* @param string $path file path. |
| 286 |
* @return mixed |
| 287 |
*/ |
| 288 |
public function include_file( $path ) { |
| 289 |
$file_path = $this->get_file_path( $path ); |
| 290 |
if ( !file_exists( $file_path ) ) { |
| 291 |
if ( $this->is_debug_mode() ) { |
| 292 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 293 |
error_log( "[CBB]: Missing file: {$file_path}" ); |
| 294 |
} |
| 295 |
return false; |
| 296 |
} |
| 297 |
return include_once $file_path; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Get file uri by file path. |
| 302 |
* |
| 303 |
* @param string $path file path. |
| 304 |
* @return string |
| 305 |
*/ |
| 306 |
public function get_file_uri( $path ) { |
| 307 |
return BOLDBLOCKS_CBB_URL . $path; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Create version for scripts/styles |
| 312 |
* |
| 313 |
* @param array $asset_file |
| 314 |
* @return string |
| 315 |
*/ |
| 316 |
public function get_script_version( $asset_file ) { |
| 317 |
return ( wp_get_environment_type() !== 'production' ? $asset_file['version'] ?? BOLDBLOCKS_CBB_VERSION : BOLDBLOCKS_CBB_VERSION ); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Get the suffix for the scripts |
| 322 |
* |
| 323 |
* @return string |
| 324 |
*/ |
| 325 |
public function get_script_suffix() { |
| 326 |
return $this->script_suffix; |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Get the plugin version |
| 331 |
* |
| 332 |
* @return string |
| 333 |
*/ |
| 334 |
public function get_plugin_version() { |
| 335 |
return $this->version; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Is Debugging CBB |
| 340 |
* |
| 341 |
* @return boolean |
| 342 |
*/ |
| 343 |
public function is_debug_mode() { |
| 344 |
return defined( 'CBB_DEBUG' ) && CBB_DEBUG || 'development' === wp_get_environment_type(); |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Enqueue debug log information |
| 349 |
* |
| 350 |
* @param string $handle |
| 351 |
* @return void |
| 352 |
*/ |
| 353 |
public function enqueue_debug_information( $handle ) { |
| 354 |
wp_add_inline_script( $handle, 'var BBLOG=' . wp_json_encode( [ |
| 355 |
'environmentType' => ( $this->is_debug_mode() ? 'development' : wp_get_environment_type() ), |
| 356 |
] ), 'before' ); |
| 357 |
} |
| 358 |
|
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Kick start |
| 363 |
* |
| 364 |
* @return ContentBlocksBuilder instance |
| 365 |
*/ |
| 366 |
function boldblocks_cbb_get_instance() { |
| 367 |
return ContentBlocksBuilder::get_instance(); |
| 368 |
} |
| 369 |
|
| 370 |
// Instantiate. |
| 371 |
boldblocks_cbb_get_instance(); |
| 372 |
} |
| 373 |
if ( !function_exists( __NAMESPACE__ . '\\content_block_builder_activate' ) ) { |
| 374 |
/** |
| 375 |
* Trigger an action when the plugin is activated. |
| 376 |
* |
| 377 |
* @return void |
| 378 |
*/ |
| 379 |
function content_block_builder_activate() { |
| 380 |
do_action( 'cbb/activate' ); |
| 381 |
} |
| 382 |
|
| 383 |
register_activation_hook( __FILE__, __NAMESPACE__ . '\\content_block_builder_activate' ); |
| 384 |
} |