| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Content Blocks Builder by BoldBlocks |
| 4 |
* Description: Gutenberg - custom content blocks made easy. |
| 5 |
* Requires at least: 5.8 |
| 6 |
* Requires PHP: 7.0 |
| 7 |
* Version: 1.1.0 |
| 8 |
* Author: Phi Phan |
| 9 |
* Author URI: https://boldblocks.net |
| 10 |
* Text Domain: boldblocks |
| 11 |
* |
| 12 |
* @package BoldBlocks |
| 13 |
* @copyright Copyright(c) 2021, Phi Phan |
| 14 |
*/ |
| 15 |
|
| 16 |
namespace BoldBlocks; |
| 17 |
|
| 18 |
// Exit if accessed directly. |
| 19 |
defined( 'ABSPATH' ) || exit; |
| 20 |
|
| 21 |
if ( ! class_exists( 'ContentBlocksBuilder' ) ) : |
| 22 |
/** |
| 23 |
* The main class |
| 24 |
*/ |
| 25 |
class ContentBlocksBuilder { |
| 26 |
/** |
| 27 |
* Plugin version |
| 28 |
* |
| 29 |
* @var String |
| 30 |
*/ |
| 31 |
protected $version = '1.1.0'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Components |
| 35 |
* |
| 36 |
* @var Array |
| 37 |
*/ |
| 38 |
protected $components = []; |
| 39 |
|
| 40 |
/** |
| 41 |
* A dummy constructor |
| 42 |
*/ |
| 43 |
public function __construct() {} |
| 44 |
|
| 45 |
/** |
| 46 |
* Kick start function. |
| 47 |
* Define constants, load dependencies |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function initialize() { |
| 52 |
// Setup constants. |
| 53 |
$this->setup_constants(); |
| 54 |
|
| 55 |
// Register core components. |
| 56 |
$this->register_core_components(); |
| 57 |
|
| 58 |
// Load dependencies. |
| 59 |
$this->load_dependencies(); |
| 60 |
|
| 61 |
// Run hooks. |
| 62 |
$this->run(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Setup constants |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function setup_constants() { |
| 71 |
$this->define_constant( 'BOLDBLOCKS_CBB', true ); |
| 72 |
$this->define_constant( 'BOLDBLOCKS_CBB_ROOT_FILE', __FILE__ ); |
| 73 |
$this->define_constant( 'BOLDBLOCKS_CBB_VERSION', $this->version ); |
| 74 |
$this->define_constant( 'BOLDBLOCKS_CBB_PATH', trailingslashit( plugin_dir_path( BOLDBLOCKS_CBB_ROOT_FILE ) ) ); |
| 75 |
$this->define_constant( 'BOLDBLOCKS_CBB_URL', trailingslashit( plugin_dir_url( BOLDBLOCKS_CBB_ROOT_FILE ) ) ); |
| 76 |
$this->define_constant( 'BOLDBLOCKS_CBB_EDITOR_SCRIPTS_HANDLE', 'boldblocks-editor-scripts' ); |
| 77 |
$this->define_constant( 'BOLDBLOCKS_CBB_EDITOR_STYLE_HANDLE', 'boldblocks-editor-style' ); |
| 78 |
$this->define_constant( 'BOLDBLOCKS_CBB_FRONTEND_STYLE_HANDLE', 'boldblocks-frontend-style' ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Load core components |
| 83 |
* |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function register_core_components() { |
| 87 |
// Load & register core components: custom blocks, patterns, variations. |
| 88 |
$this->include_file( 'includes/custom-blocks.php' ); |
| 89 |
$this->include_file( 'includes/patterns.php' ); |
| 90 |
$this->include_file( 'includes/variations.php' ); |
| 91 |
|
| 92 |
$core_components = [ CustomBlocks::class, Patterns::class, Variations::class ]; |
| 93 |
foreach ( $core_components as $component ) { |
| 94 |
$this->register_component( $component ); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Load dependencies |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function load_dependencies() {} |
| 104 |
|
| 105 |
/** |
| 106 |
* Run main hooks |
| 107 |
* |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
public function run() { |
| 111 |
// Load translations. |
| 112 |
add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] ); |
| 113 |
|
| 114 |
// Main hooks. |
| 115 |
add_action( 'init', [ $this, 'init' ], 5 ); |
| 116 |
|
| 117 |
// Enqueue scripts for editor. |
| 118 |
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] ); |
| 119 |
|
| 120 |
// Run all components. |
| 121 |
foreach ( $this->components as $component ) { |
| 122 |
$component->run(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Process on init hook |
| 128 |
* |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
public function init() { |
| 132 |
|
| 133 |
// Run a hook when the plugin initialized. |
| 134 |
do_action( 'boldblocks/init' ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Enqueue editor assets |
| 139 |
* |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
public function enqueue_block_editor_assets() { |
| 143 |
// Index access file. |
| 144 |
$index_asset = $this->include_file( 'build/index.asset.php' ); |
| 145 |
|
| 146 |
// Scripts. |
| 147 |
wp_enqueue_script( |
| 148 |
BOLDBLOCKS_CBB_EDITOR_SCRIPTS_HANDLE, |
| 149 |
$this->get_file_uri( 'build/index.js' ), |
| 150 |
[ 'wp-i18n', 'wp-polyfill', 'wp-components', 'wp-blocks', 'wp-core-data', 'wp-plugins', 'wp-edit-post' ], |
| 151 |
BOLDBLOCKS_CBB_VERSION, |
| 152 |
true |
| 153 |
); |
| 154 |
|
| 155 |
// For ajax requests. |
| 156 |
wp_localize_script( |
| 157 |
BOLDBLOCKS_CBB_EDITOR_SCRIPTS_HANDLE, |
| 158 |
'BoldBlocks', |
| 159 |
array( |
| 160 |
'admin_url' => get_admin_url( get_current_blog_id() ), |
| 161 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 162 |
'nonce' => wp_create_nonce( 'boldblocks-ajax-nonce' ), |
| 163 |
) |
| 164 |
); |
| 165 |
|
| 166 |
// Styles. |
| 167 |
wp_enqueue_style( |
| 168 |
BOLDBLOCKS_CBB_EDITOR_STYLE_HANDLE, |
| 169 |
$this->get_file_uri( 'build/index.css' ), |
| 170 |
[], |
| 171 |
BOLDBLOCKS_CBB_VERSION |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Load text domain |
| 177 |
* |
| 178 |
* @return void |
| 179 |
*/ |
| 180 |
public function load_textdomain() { |
| 181 |
load_plugin_textdomain( |
| 182 |
'cbb', |
| 183 |
false, |
| 184 |
plugin_basename( realpath( __DIR__ . '/languages' ) ) |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Register component |
| 190 |
* |
| 191 |
* @param string $classname The class name of the component. |
| 192 |
* @return void |
| 193 |
*/ |
| 194 |
public function register_component( $classname ) { |
| 195 |
$this->components[ $classname ] = new $classname(); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Get a component by class name |
| 200 |
* |
| 201 |
* @param string $classname The class name of the component. |
| 202 |
* @return mixed |
| 203 |
*/ |
| 204 |
public function get_component( $classname ) { |
| 205 |
return $this->components[ $classname ] ?? false; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Define constant |
| 210 |
* |
| 211 |
* @param string $name The name of the constant. |
| 212 |
* @param mixed $value The value of the constant. |
| 213 |
* @return void |
| 214 |
*/ |
| 215 |
public function define_constant( $name, $value ) { |
| 216 |
if ( ! defined( $name ) ) { |
| 217 |
define( $name, $value ); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Include file path. |
| 223 |
* |
| 224 |
* @param string $path file path. |
| 225 |
* @return string |
| 226 |
*/ |
| 227 |
public function include_file( $path ) { |
| 228 |
return include_once BOLDBLOCKS_CBB_PATH . $path; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Get file uri by file path. |
| 233 |
* |
| 234 |
* @param string $path file path. |
| 235 |
* @return string |
| 236 |
*/ |
| 237 |
public function get_file_uri( $path ) { |
| 238 |
return BOLDBLOCKS_CBB_URL . $path; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Kick start |
| 244 |
* |
| 245 |
* @return ContentBlocksBuilder instance |
| 246 |
*/ |
| 247 |
function boldblocks_cbb_get_instance() { |
| 248 |
global $boldblocks_cbb; |
| 249 |
|
| 250 |
// Instantiate only once. |
| 251 |
if ( ! isset( $boldblocks_cbb ) ) { |
| 252 |
$boldblocks_cbb = new ContentBlocksBuilder(); |
| 253 |
$boldblocks_cbb->initialize(); |
| 254 |
} |
| 255 |
return $boldblocks_cbb; |
| 256 |
} |
| 257 |
|
| 258 |
// Instantiate. |
| 259 |
boldblocks_cbb_get_instance(); |
| 260 |
endif; |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|