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;