# content-blocks-builder/1.1.0/content-blocks-builder.php

Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts, version 1.1.0. 264 lines.

- Page: https://pluginprobe.com/plugins/content-blocks-builder/1.1.0/code/content-blocks-builder.php
- Raw: https://pluginprobe.com/plugins/content-blocks-builder/1.1.0/raw/content-blocks-builder.php
- Modified: 2022-05-13T13:08:26+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/content-blocks-builder/1.1.0/code/content-blocks-builder.php#L10-L20`.

```php
<?php
/**
 * Plugin Name:       Content Blocks Builder by BoldBlocks
 * Description:       Gutenberg - custom content blocks made easy.
 * Requires at least: 5.8
 * Requires PHP:      7.0
 * Version:           1.1.0
 * Author:            Phi Phan
 * Author URI:        https://boldblocks.net
 * Text Domain:       boldblocks
 *
 * @package           BoldBlocks
 * @copyright         Copyright(c) 2021, Phi Phan
 */

namespace BoldBlocks;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'ContentBlocksBuilder' ) ) :
	/**
	 * The main class
	 */
	class ContentBlocksBuilder {
		/**
		 * Plugin version
		 *
		 * @var String
		 */
		protected $version = '1.1.0';

		/**
		 * Components
		 *
		 * @var Array
		 */
		protected $components = [];

		/**
		 * A dummy constructor
		 */
		public function __construct() {}

		/**
		 * Kick start function.
		 * Define constants, load dependencies
		 *
		 * @return void
		 */
		public function initialize() {
			// Setup constants.
			$this->setup_constants();

			// Register core components.
			$this->register_core_components();

			// Load dependencies.
			$this->load_dependencies();

			// Run hooks.
			$this->run();
		}

		/**
		 * Setup constants
		 *
		 * @return void
		 */
		public function setup_constants() {
			$this->define_constant( 'BOLDBLOCKS_CBB', true );
			$this->define_constant( 'BOLDBLOCKS_CBB_ROOT_FILE', __FILE__ );
			$this->define_constant( 'BOLDBLOCKS_CBB_VERSION', $this->version );
			$this->define_constant( 'BOLDBLOCKS_CBB_PATH', trailingslashit( plugin_dir_path( BOLDBLOCKS_CBB_ROOT_FILE ) ) );
			$this->define_constant( 'BOLDBLOCKS_CBB_URL', trailingslashit( plugin_dir_url( BOLDBLOCKS_CBB_ROOT_FILE ) ) );
			$this->define_constant( 'BOLDBLOCKS_CBB_EDITOR_SCRIPTS_HANDLE', 'boldblocks-editor-scripts' );
			$this->define_constant( 'BOLDBLOCKS_CBB_EDITOR_STYLE_HANDLE', 'boldblocks-editor-style' );
			$this->define_constant( 'BOLDBLOCKS_CBB_FRONTEND_STYLE_HANDLE', 'boldblocks-frontend-style' );
		}

		/**
		 * Load core components
		 *
		 * @return void
		 */
		public function register_core_components() {
			// Load & register core components: custom blocks, patterns, variations.
			$this->include_file( 'includes/custom-blocks.php' );
			$this->include_file( 'includes/patterns.php' );
			$this->include_file( 'includes/variations.php' );

			$core_components = [ CustomBlocks::class, Patterns::class, Variations::class ];
			foreach ( $core_components as $component ) {
				$this->register_component( $component );
			}
		}

		/**
		 * Load dependencies
		 *
		 * @return void
		 */
		public function load_dependencies() {}

		/**
		 * Run main hooks
		 *
		 * @return void
		 */
		public function run() {
			// Load translations.
			add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] );

			// Main hooks.
			add_action( 'init', [ $this, 'init' ], 5 );

			// Enqueue scripts for editor.
			add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );

			// Run all components.
			foreach ( $this->components as $component ) {
				$component->run();
			}
		}

		/**
		 * Process on init hook
		 *
		 * @return void
		 */
		public function init() {

			// Run a hook when the plugin initialized.
			do_action( 'boldblocks/init' );
		}

		/**
		 * Enqueue editor assets
		 *
		 * @return void
		 */
		public function enqueue_block_editor_assets() {
			// Index access file.
			$index_asset = $this->include_file( 'build/index.asset.php' );

			// Scripts.
			wp_enqueue_script(
				BOLDBLOCKS_CBB_EDITOR_SCRIPTS_HANDLE,
				$this->get_file_uri( 'build/index.js' ),
				[ 'wp-i18n', 'wp-polyfill', 'wp-components', 'wp-blocks', 'wp-core-data', 'wp-plugins', 'wp-edit-post' ],
				BOLDBLOCKS_CBB_VERSION,
				true
			);

			// For ajax requests.
			wp_localize_script(
				BOLDBLOCKS_CBB_EDITOR_SCRIPTS_HANDLE,
				'BoldBlocks',
				array(
					'admin_url' => get_admin_url( get_current_blog_id() ),
					'ajax_url'  => admin_url( 'admin-ajax.php' ),
					'nonce'     => wp_create_nonce( 'boldblocks-ajax-nonce' ),
				)
			);

			// Styles.
			wp_enqueue_style(
				BOLDBLOCKS_CBB_EDITOR_STYLE_HANDLE,
				$this->get_file_uri( 'build/index.css' ),
				[],
				BOLDBLOCKS_CBB_VERSION
			);
		}

		/**
		 * Load text domain
		 *
		 * @return void
		 */
		public function load_textdomain() {
			load_plugin_textdomain(
				'cbb',
				false,
				plugin_basename( realpath( __DIR__ . '/languages' ) )
			);
		}

		/**
		 * Register component
		 *
		 * @param string $classname The class name of the component.
		 * @return void
		 */
		public function register_component( $classname ) {
			$this->components[ $classname ] = new $classname();
		}

		/**
		 * Get a component by class name
		 *
		 * @param string $classname The class name of the component.
		 * @return mixed
		 */
		public function get_component( $classname ) {
			return $this->components[ $classname ] ?? false;
		}

		/**
		 * Define constant
		 *
		 * @param string $name The name of the constant.
		 * @param mixed  $value The value of the constant.
		 * @return void
		 */
		public function define_constant( $name, $value ) {
			if ( ! defined( $name ) ) {
				define( $name, $value );
			}
		}

		/**
		 * Include file path.
		 *
		 * @param string $path file path.
		 * @return string
		 */
		public function include_file( $path ) {
			return include_once BOLDBLOCKS_CBB_PATH . $path;
		}

		/**
		 * Get file uri by file path.
		 *
		 * @param string $path file path.
		 * @return string
		 */
		public function get_file_uri( $path ) {
			return BOLDBLOCKS_CBB_URL . $path;
		}
	}

	/**
	 * Kick start
	 *
	 * @return ContentBlocksBuilder instance
	 */
	function boldblocks_cbb_get_instance() {
		global $boldblocks_cbb;

		// Instantiate only once.
		if ( ! isset( $boldblocks_cbb ) ) {
			$boldblocks_cbb = new ContentBlocksBuilder();
			$boldblocks_cbb->initialize();
		}
		return $boldblocks_cbb;
	}

	// Instantiate.
	boldblocks_cbb_get_instance();
endif;




```
