| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly. |
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
if ( ! class_exists( 'Qi_Blocks_Blocks' ) ) { |
| 7 |
class Qi_Blocks_Blocks { |
| 8 |
private $blocks_namespace; |
| 9 |
private $block_type; |
| 10 |
private $block_name; |
| 11 |
private $block_title; |
| 12 |
private $block_subcategory; |
| 13 |
private $block_demo_url; |
| 14 |
private $block_video; |
| 15 |
private $block_documentation; |
| 16 |
private $block_style; |
| 17 |
private $block_script; |
| 18 |
private $block_editor_style; |
| 19 |
private $block_editor_script; |
| 20 |
private $block_3rd_party_scripts = array(); |
| 21 |
private $block_options = array(); |
| 22 |
|
| 23 |
public function __construct() { |
| 24 |
// Get block status |
| 25 |
$block_flag = true; |
| 26 |
$block_status = apply_filters( 'qi_blocks_filter_block_status', false ); |
| 27 |
|
| 28 |
// Set namespace for blocks |
| 29 |
$this->set_blocks_namespace( 'qi-blocks' ); |
| 30 |
|
| 31 |
// Add block into global list |
| 32 |
if ( ! empty( $this->get_block_title() ) ) { |
| 33 |
|
| 34 |
if ( in_array( $this->get_block_type(), array( 'premium', 'landing' ), true ) && ! $block_status ) { |
| 35 |
$block_flag = false; |
| 36 |
} |
| 37 |
|
| 38 |
if ( $block_flag ) { |
| 39 |
Qi_Blocks_Blocks_List::get_instance()->add_block( |
| 40 |
array( |
| 41 |
'key' => $this->get_block_name(), |
| 42 |
'value' => array( |
| 43 |
'type' => $this->get_block_type(), |
| 44 |
'title' => $this->get_block_title(), |
| 45 |
'subcategory' => $this->get_block_subcategory(), |
| 46 |
'demo' => $this->get_block_demo_url(), |
| 47 |
'documentation' => $this->get_block_documentation(), |
| 48 |
'video' => $this->get_block_video(), |
| 49 |
), |
| 50 |
) |
| 51 |
); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// Register block |
| 56 |
add_action( 'init', array( $this, 'register_block' ) ); |
| 57 |
|
| 58 |
// Loads core block assets only when they are rendered on the page - WordPress 5.8 |
| 59 |
add_filter( 'should_load_separate_core_block_assets', '__return_true' ); |
| 60 |
|
| 61 |
// Enqueue block 3rd party plugin's assets |
| 62 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_3rd_party_scripts' ) ); |
| 63 |
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_3rd_party_editor_scripts' ) ); |
| 64 |
} |
| 65 |
|
| 66 |
public function get_blocks_namespace() { |
| 67 |
return $this->blocks_namespace; |
| 68 |
} |
| 69 |
|
| 70 |
public function set_blocks_namespace( $blocks_namespace ) { |
| 71 |
$this->blocks_namespace = $blocks_namespace; |
| 72 |
} |
| 73 |
|
| 74 |
public function get_block_type() { |
| 75 |
return $this->block_type; |
| 76 |
} |
| 77 |
|
| 78 |
public function set_block_type( $block_type ) { |
| 79 |
$this->block_type = $block_type; |
| 80 |
} |
| 81 |
|
| 82 |
public function get_block_name() { |
| 83 |
return $this->block_name; |
| 84 |
} |
| 85 |
|
| 86 |
public function set_block_name( $block_name ) { |
| 87 |
$this->block_name = $block_name; |
| 88 |
} |
| 89 |
|
| 90 |
public function get_block_title() { |
| 91 |
return $this->block_title; |
| 92 |
} |
| 93 |
|
| 94 |
public function set_block_title( $block_title ) { |
| 95 |
$this->block_title = $block_title; |
| 96 |
} |
| 97 |
|
| 98 |
public function get_block_subcategory() { |
| 99 |
return $this->block_subcategory; |
| 100 |
} |
| 101 |
|
| 102 |
public function set_block_subcategory( $block_subcategory ) { |
| 103 |
$this->block_subcategory = $block_subcategory; |
| 104 |
} |
| 105 |
|
| 106 |
public function get_block_demo_url() { |
| 107 |
return $this->block_demo_url; |
| 108 |
} |
| 109 |
|
| 110 |
public function set_block_demo_url( $block_demo_url ) { |
| 111 |
$this->block_demo_url = $block_demo_url; |
| 112 |
} |
| 113 |
|
| 114 |
public function get_block_video() { |
| 115 |
return $this->block_video; |
| 116 |
} |
| 117 |
|
| 118 |
public function set_block_video( $block_video ) { |
| 119 |
$this->block_video = $block_video; |
| 120 |
} |
| 121 |
|
| 122 |
public function get_block_documentation() { |
| 123 |
return $this->block_documentation; |
| 124 |
} |
| 125 |
|
| 126 |
public function set_block_documentation( $block_documentation ) { |
| 127 |
$this->block_documentation = $block_documentation; |
| 128 |
} |
| 129 |
|
| 130 |
public function get_block_style() { |
| 131 |
return $this->block_style; |
| 132 |
} |
| 133 |
|
| 134 |
public function set_block_style( $block_style ) { |
| 135 |
$this->block_style = $block_style; |
| 136 |
} |
| 137 |
|
| 138 |
public function get_block_script() { |
| 139 |
return $this->block_script; |
| 140 |
} |
| 141 |
|
| 142 |
public function set_block_script( $block_script ) { |
| 143 |
$this->block_script = $block_script; |
| 144 |
} |
| 145 |
|
| 146 |
public function get_block_editor_style() { |
| 147 |
return $this->block_editor_style; |
| 148 |
} |
| 149 |
|
| 150 |
public function set_block_editor_style( $block_editor_style ) { |
| 151 |
$this->block_editor_style = $block_editor_style; |
| 152 |
} |
| 153 |
|
| 154 |
public function get_block_editor_script() { |
| 155 |
return $this->block_editor_script; |
| 156 |
} |
| 157 |
|
| 158 |
public function set_block_editor_script( $block_editor_script ) { |
| 159 |
$this->block_editor_script = $block_editor_script; |
| 160 |
} |
| 161 |
|
| 162 |
public function get_block_3rd_party_scripts() { |
| 163 |
return $this->block_3rd_party_scripts; |
| 164 |
} |
| 165 |
|
| 166 |
public function set_block_3rd_party_scripts( $block_3rd_party_scripts ) { |
| 167 |
$this->block_3rd_party_scripts = $block_3rd_party_scripts; |
| 168 |
} |
| 169 |
|
| 170 |
public function get_block_options() { |
| 171 |
return $this->block_options; |
| 172 |
} |
| 173 |
|
| 174 |
public function set_block_options( $block_options ) { |
| 175 |
$this->block_options = $block_options; |
| 176 |
} |
| 177 |
|
| 178 |
function register_block() { |
| 179 |
$disabled_blocks = get_option( QI_BLOCKS_DISABLED_BLOCKS ); |
| 180 |
$block_status = apply_filters( 'qi_blocks_filter_block_status', false ); |
| 181 |
|
| 182 |
// Prevent blocks loading if it's disabled |
| 183 |
if ( ! empty( $disabled_blocks ) && key_exists( $this->get_block_name(), $disabled_blocks ) ) { |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
if ( in_array( $this->get_block_type(), array( 'premium', 'landing' ), true ) && ! $block_status ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
// Get blocks options |
| 192 |
$block_options = $this->get_block_options(); |
| 193 |
|
| 194 |
// Set blocks scripts |
| 195 |
$this->set_blocks_scripts(); |
| 196 |
|
| 197 |
register_block_type( |
| 198 |
$this->get_blocks_namespace() . '/' . $this->get_block_name(), |
| 199 |
array_merge( |
| 200 |
array( |
| 201 |
'style' => $this->get_block_style(), |
| 202 |
'script' => $this->get_block_script(), |
| 203 |
'editor_style' => $this->get_block_editor_style(), |
| 204 |
'editor_script' => $this->get_block_editor_script(), |
| 205 |
), |
| 206 |
$block_options |
| 207 |
) |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
function set_blocks_scripts() { |
| 212 |
$block_type = $this->get_block_type(); |
| 213 |
$block_name = $this->get_block_name(); |
| 214 |
$block_status = apply_filters( 'qi_blocks_filter_block_status', false ); |
| 215 |
|
| 216 |
if ( ! empty( $block_name ) ) { |
| 217 |
$is_premium = qi_blocks_is_installed( 'premium' ) && $block_status; |
| 218 |
$is_landing = qi_blocks_is_installed( 'landing' ) && $block_status; |
| 219 |
$block_name = esc_attr( $block_name ); |
| 220 |
$block_dir_path = QI_BLOCKS_ASSETS_PATH . '/dist/' . $block_name; |
| 221 |
$block_url_path = QI_BLOCKS_ASSETS_URL_PATH . '/dist/' . $block_name; |
| 222 |
$text_domain = 'qi-blocks'; |
| 223 |
$languages_path = QI_BLOCKS_PLUGIN_LANGUAGES_PATH; |
| 224 |
|
| 225 |
if ( 'premium' === $block_type && $is_premium ) { |
| 226 |
$block_dir_path = QI_BLOCKS_PREMIUM_ASSETS_PATH . '/dist/' . $block_name; |
| 227 |
$block_url_path = QI_BLOCKS_PREMIUM_ASSETS_URL_PATH . '/dist/' . $block_name; |
| 228 |
|
| 229 |
$text_domain = 'qi-blocks-premium'; |
| 230 |
$languages_path = QI_BLOCKS_PREMIUM_PLUGIN_LANGUAGES_PATH; |
| 231 |
} elseif ( 'landing' === $block_type && $is_landing ) { |
| 232 |
$block_dir_path = QI_BLOCKS_LANDING_ASSETS_PATH . '/dist/' . $block_name; |
| 233 |
$block_url_path = QI_BLOCKS_LANDING_ASSETS_URL_PATH . '/dist/' . $block_name; |
| 234 |
} |
| 235 |
|
| 236 |
// Check if CSS file exists |
| 237 |
if ( file_exists( $block_dir_path . '.css' ) ) { |
| 238 |
// Set block style dependency |
| 239 |
$style_dependency = apply_filters( 'qi_blocks_filter_block_style_dependency', array() ); |
| 240 |
|
| 241 |
// Register block editor extended script |
| 242 |
if ( $is_premium && file_exists( QI_BLOCKS_PREMIUM_ASSETS_PATH . '/dist/' . $block_name . '-extended.css' ) ) { |
| 243 |
wp_register_style( |
| 244 |
'qi-blocks-' . $block_name . '-extended', |
| 245 |
QI_BLOCKS_PREMIUM_ASSETS_URL_PATH . '/dist/' . $block_name . '-extended.css', |
| 246 |
$style_dependency |
| 247 |
); |
| 248 |
|
| 249 |
$style_dependency[] = 'qi-blocks-' . $block_name . '-extended'; |
| 250 |
} elseif ( $is_landing && file_exists( QI_BLOCKS_LANDING_ASSETS_PATH . '/dist/' . $block_name . '-extended.css' ) ) { |
| 251 |
wp_register_style( |
| 252 |
'qi-blocks-' . $block_name . '-extended', |
| 253 |
QI_BLOCKS_LANDING_ASSETS_URL_PATH . '/dist/' . $block_name . '-extended.css', |
| 254 |
$style_dependency |
| 255 |
); |
| 256 |
|
| 257 |
$style_dependency[] = 'qi-blocks-' . $block_name . '-extended'; |
| 258 |
} |
| 259 |
|
| 260 |
// Register block style |
| 261 |
wp_register_style( 'qi-blocks-' . $block_name, $block_url_path . '.css', $style_dependency ); |
| 262 |
|
| 263 |
// Set block style |
| 264 |
$this->set_block_style( 'qi-blocks-' . $block_name ); |
| 265 |
} |
| 266 |
|
| 267 |
// Check if JS file exists |
| 268 |
if ( file_exists( $block_dir_path . '-script.js' ) ) { |
| 269 |
$script_dependency = array( 'qi-blocks-main' ); |
| 270 |
|
| 271 |
// Register block extended script |
| 272 |
if ( $is_premium && file_exists( QI_BLOCKS_PREMIUM_ASSETS_PATH . '/dist/' . $block_name . '-extended-script.js' ) ) { |
| 273 |
wp_register_script( |
| 274 |
'qi-blocks-' . $block_name . '-extended-script', |
| 275 |
QI_BLOCKS_PREMIUM_ASSETS_URL_PATH . '/dist/' . $block_name . '-extended-script.js', |
| 276 |
$script_dependency |
| 277 |
); |
| 278 |
|
| 279 |
$script_dependency[] = 'qi-blocks-' . $block_name . '-extended-script'; |
| 280 |
} elseif ( $is_landing && file_exists( QI_BLOCKS_LANDING_ASSETS_PATH . '/dist/' . $block_name . '-extended-script.js' ) ) { |
| 281 |
wp_register_script( |
| 282 |
'qi-blocks-' . $block_name . '-extended-script', |
| 283 |
QI_BLOCKS_LANDING_ASSETS_URL_PATH . '/dist/' . $block_name . '-extended-script.js', |
| 284 |
$script_dependency |
| 285 |
); |
| 286 |
|
| 287 |
$script_dependency[] = 'qi-blocks-' . $block_name . '-extended-script'; |
| 288 |
} |
| 289 |
|
| 290 |
// Register block editor script |
| 291 |
wp_register_script( 'qi-blocks-' . $block_name, $block_url_path . '-script.js', $script_dependency, false, true ); |
| 292 |
|
| 293 |
// Set block editor script |
| 294 |
$this->set_block_script( 'qi-blocks-' . $block_name ); |
| 295 |
} else { |
| 296 |
$register_script_flag = false; |
| 297 |
|
| 298 |
// Register block extended script |
| 299 |
if ( $is_premium && file_exists( QI_BLOCKS_PREMIUM_ASSETS_PATH . '/dist/' . $block_name . '-extended-script.js' ) ) { |
| 300 |
$register_script_flag = true; |
| 301 |
|
| 302 |
wp_register_script( |
| 303 |
'qi-blocks-' . $block_name . '-extended-script', |
| 304 |
QI_BLOCKS_PREMIUM_ASSETS_URL_PATH . '/dist/' . $block_name . '-extended-script.js', |
| 305 |
array( 'qi-blocks-main' ), |
| 306 |
false, |
| 307 |
true |
| 308 |
); |
| 309 |
} elseif ( $is_landing && file_exists( QI_BLOCKS_LANDING_ASSETS_PATH . '/dist/' . $block_name . '-extended-script.js' ) ) { |
| 310 |
$register_script_flag = true; |
| 311 |
|
| 312 |
wp_register_script( |
| 313 |
'qi-blocks-' . $block_name . '-extended-script', |
| 314 |
QI_BLOCKS_LANDING_ASSETS_URL_PATH . '/dist/' . $block_name . '-extended-script.js', |
| 315 |
array( 'qi-blocks-main' ), |
| 316 |
false, |
| 317 |
true |
| 318 |
); |
| 319 |
} |
| 320 |
|
| 321 |
if ( $register_script_flag ) { |
| 322 |
// Set block editor script |
| 323 |
$this->set_block_script( 'qi-blocks-' . $block_name . '-extended-script' ); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
// Check if editor CSS file exists |
| 328 |
if ( file_exists( $block_dir_path . '-editor.css' ) ) { |
| 329 |
// Set block style editor dependency |
| 330 |
$editor_style_dependency = apply_filters( 'qi_blocks_filter_block_style_dependency', array() ); |
| 331 |
|
| 332 |
// Register block editor style |
| 333 |
wp_register_style( 'qi-blocks-' . $block_name . '-editor', $block_url_path . '-editor.css', $editor_style_dependency ); |
| 334 |
|
| 335 |
// Set block editor style |
| 336 |
$this->set_block_editor_style( 'qi-blocks-' . $block_name . '-editor' ); |
| 337 |
} |
| 338 |
|
| 339 |
// Check if editor script file exists |
| 340 |
if ( file_exists( $block_dir_path . '.js' ) ) { |
| 341 |
$editor_script_dependency = array( 'qi-blocks-main-editor' ); |
| 342 |
|
| 343 |
// Register block editor extended script |
| 344 |
if ( $is_premium && file_exists( QI_BLOCKS_PREMIUM_ASSETS_PATH . '/dist/' . $block_name . '-extended.js' ) ) { |
| 345 |
wp_register_script( |
| 346 |
'qi-blocks-' . $block_name . '-editor-extended', |
| 347 |
QI_BLOCKS_PREMIUM_ASSETS_URL_PATH . '/dist/' . $block_name . '-extended.js', |
| 348 |
$editor_script_dependency |
| 349 |
); |
| 350 |
|
| 351 |
$editor_script_dependency[] = 'qi-blocks-' . $block_name . '-editor-extended'; |
| 352 |
|
| 353 |
// Enqueue localization data for our blocks |
| 354 |
if ( function_exists( 'wp_set_script_translations' ) ) { |
| 355 |
wp_set_script_translations( 'qi-blocks-' . $block_name . '-editor-extended', 'qi-blocks-premium', QI_BLOCKS_PREMIUM_PLUGIN_LANGUAGES_PATH ); |
| 356 |
} |
| 357 |
} elseif ( $is_landing && file_exists( QI_BLOCKS_LANDING_ASSETS_PATH . '/dist/' . $block_name . '-extended.js' ) ) { |
| 358 |
wp_register_script( |
| 359 |
'qi-blocks-' . $block_name . '-editor-extended', |
| 360 |
QI_BLOCKS_LANDING_ASSETS_URL_PATH . '/dist/' . $block_name . '-extended.js', |
| 361 |
$editor_script_dependency |
| 362 |
); |
| 363 |
|
| 364 |
$editor_script_dependency[] = 'qi-blocks-' . $block_name . '-editor-extended'; |
| 365 |
} |
| 366 |
|
| 367 |
// Register block editor script |
| 368 |
wp_register_script( 'qi-blocks-' . $block_name . '-editor', $block_url_path . '.js', $editor_script_dependency ); |
| 369 |
|
| 370 |
// Set block editor script |
| 371 |
$this->set_block_editor_script( 'qi-blocks-' . $block_name . '-editor' ); |
| 372 |
|
| 373 |
// Enqueue localization data for our blocks |
| 374 |
if ( function_exists( 'wp_set_script_translations' ) ) { |
| 375 |
wp_set_script_translations( 'qi-blocks-' . $block_name . '-editor', $text_domain, $languages_path ); |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
function enqueue_3rd_party_scripts() { |
| 382 |
$this->enqueue_3rd_party_scripts_logic(); |
| 383 |
} |
| 384 |
|
| 385 |
function enqueue_3rd_party_editor_scripts() { |
| 386 |
$this->enqueue_3rd_party_scripts_logic( true ); |
| 387 |
} |
| 388 |
|
| 389 |
function enqueue_3rd_party_scripts_logic( $editor_mode = false ) { |
| 390 |
$block_3rd_party_scripts = $this->get_block_3rd_party_scripts(); |
| 391 |
|
| 392 |
if ( ! empty( $block_3rd_party_scripts ) && is_array( $block_3rd_party_scripts ) ) { |
| 393 |
foreach ( $block_3rd_party_scripts as $script_key => $script_value ) { |
| 394 |
$script_dependency = array(); |
| 395 |
$is_script_style = false; |
| 396 |
$has_script_style = false; |
| 397 |
|
| 398 |
if ( isset( $script_value['dependency'] ) && ! empty( $script_value['dependency'] ) ) { |
| 399 |
$script_dependency = $script_value['dependency']; |
| 400 |
} |
| 401 |
|
| 402 |
if ( isset( $script_value['is_style'] ) ) { |
| 403 |
$is_script_style = $script_value['is_style']; |
| 404 |
} |
| 405 |
|
| 406 |
if ( isset( $script_value['has_style'] ) ) { |
| 407 |
$has_script_style = $script_value['has_style']; |
| 408 |
} |
| 409 |
|
| 410 |
// Check if block exist on the page and then try to load scripts |
| 411 |
$additional_conditional = true; |
| 412 |
if ( ! $editor_mode ) { |
| 413 |
$additional_conditional = function_exists( 'has_block' ) && has_block( 'qi-blocks/' . $script_value['block_name'] ); |
| 414 |
|
| 415 |
if ( ! $additional_conditional && function_exists( 'get_the_block_template_html' ) ) { |
| 416 |
$template_content = get_the_block_template_html(); |
| 417 |
|
| 418 |
// Check if block exist inside FSE template part |
| 419 |
if ( ! empty( $template_content ) && strpos( $template_content, 'qi-block-' . $script_value['block_name'] ) !== false ) { |
| 420 |
$additional_conditional = true; |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
if ( isset( $script_value['block_name'] ) && $additional_conditional ) { |
| 426 |
|
| 427 |
if ( ! empty( $script_value['url'] ) ) { |
| 428 |
if ( 'core' === $script_value['url'] ) { |
| 429 |
|
| 430 |
if ( $is_script_style ) { |
| 431 |
wp_enqueue_style( $script_key ); |
| 432 |
} else { |
| 433 |
wp_enqueue_script( $script_key ); |
| 434 |
|
| 435 |
if ( $has_script_style ) { |
| 436 |
wp_enqueue_style( $script_key ); |
| 437 |
} |
| 438 |
} |
| 439 |
} else { |
| 440 |
|
| 441 |
if ( $is_script_style ) { |
| 442 |
wp_enqueue_style( $script_key, $script_value['url'] ); |
| 443 |
} else { |
| 444 |
wp_enqueue_script( $script_key, $script_value['url'], $script_dependency, false, true ); |
| 445 |
|
| 446 |
if ( $has_script_style ) { |
| 447 |
wp_enqueue_style( $script_key, $script_value['url'] ); |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
|