| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gutenberg Blocks |
| 4 |
* |
| 5 |
* Created Date: Wednesday September 2nd 2020 |
| 6 |
* Author: Michael Bourne |
| 7 |
* ----- |
| 8 |
* Last Modified: Thursday, February 26th 2026, 2:21:24 pm |
| 9 |
* Modified By: Michael Bourne |
| 10 |
* ----- |
| 11 |
* Copyright (c) 2020 URSA6 |
| 12 |
* |
| 13 |
* @package wp-commerce7 |
| 14 |
* @author Michael Bourne |
| 15 |
* @license GPL3 |
| 16 |
* @link https://ursa6.com |
| 17 |
* @since 1.0.8 |
| 18 |
*/ |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
// Check if Gutenberg is active. |
| 25 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
// Load validation helper |
| 30 |
require_once C7WP_ROOT . '/includes/class-c7wp-validation.php'; |
| 31 |
|
| 32 |
$elements = C7WP_Widgets::get_slugs_for_version( $this->widgetsver, 'gutenberg' ); |
| 33 |
$dir = in_array( $this->widgetsver, array( 'v2', 'v2-compat' ), true ) ? 'blocks-v2' : 'blocks'; |
| 34 |
|
| 35 |
C7WP_Widgets::register_clubselector_assets( $this ); |
| 36 |
C7WP_Widgets::register_clubselector_v2_assets( $this ); |
| 37 |
|
| 38 |
$ct_builder = filter_input( INPUT_GET, 'ct_builder', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 39 |
$is_ct_builder = ! empty( $ct_builder ); |
| 40 |
|
| 41 |
foreach ( $elements as $element ) { |
| 42 |
$block_slug = 'c7wp-' . $element; |
| 43 |
$block_name = 'c7wp/' . $element; |
| 44 |
|
| 45 |
$frontend_js_path = $dir . '/' . $element . '/frontend.js'; |
| 46 |
$frontend_css_path = $dir . '/' . $element . '/frontend.css'; |
| 47 |
$frontend_script_handle = 'c7wp-' . $element . '-frontend'; |
| 48 |
$frontend_style_handle = 'c7wp-' . $element . '-frontend'; |
| 49 |
$has_frontend_js = file_exists( C7WP_ROOT . '/includes/gutenberg/' . $frontend_js_path ); |
| 50 |
$has_frontend_css = file_exists( C7WP_ROOT . '/includes/gutenberg/' . $frontend_css_path ); |
| 51 |
|
| 52 |
// Check if block.json exists (new format) |
| 53 |
$block_json_path = C7WP_ROOT . '/includes/gutenberg/' . $dir . '/' . $element . '/block.json'; |
| 54 |
if ( file_exists( $block_json_path ) ) { |
| 55 |
// Register editor script with dependencies before registering block |
| 56 |
$editor_script_path = C7WP_ROOT . '/includes/gutenberg/' . $dir . '/' . $element . '/' . $block_slug . '.js'; |
| 57 |
if ( file_exists( $editor_script_path ) ) { |
| 58 |
wp_register_script( |
| 59 |
$block_slug, |
| 60 |
plugins_url( $dir . '/' . $element . '/' . $block_slug . '.js', __FILE__ ), |
| 61 |
array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n' ), |
| 62 |
C7WP_VERSION, |
| 63 |
true |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
// Register editor style before registering block |
| 68 |
$editor_style_path = C7WP_ROOT . '/includes/gutenberg/' . $dir . '/' . $element . '/' . $block_slug . '.css'; |
| 69 |
if ( file_exists( $editor_style_path ) ) { |
| 70 |
wp_register_style( |
| 71 |
$block_slug, |
| 72 |
plugins_url( $dir . '/' . $element . '/' . $block_slug . '.css', __FILE__ ), |
| 73 |
array(), |
| 74 |
C7WP_VERSION |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
// Register optional frontend script for block render only. |
| 79 |
$block_args = array(); |
| 80 |
if ( $has_frontend_js && ! $is_ct_builder ) { |
| 81 |
wp_register_script( |
| 82 |
$frontend_script_handle, |
| 83 |
plugins_url( $frontend_js_path, __FILE__ ), |
| 84 |
array(), |
| 85 |
C7WP_VERSION, |
| 86 |
true |
| 87 |
); |
| 88 |
|
| 89 |
if ( 'clubselector' === $element ) { |
| 90 |
wp_localize_script( |
| 91 |
$frontend_script_handle, |
| 92 |
'c7wp_settings', |
| 93 |
C7WP::getInstance()->get_public_js_settings() |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
$block_args['view_script'] = $frontend_script_handle; |
| 98 |
} |
| 99 |
|
| 100 |
// Register block using metadata - it will reference our registered handles |
| 101 |
register_block_type_from_metadata( $block_json_path, $block_args ); |
| 102 |
} else { |
| 103 |
// Fallback to old registration method |
| 104 |
// Add block script. |
| 105 |
wp_register_script( |
| 106 |
$block_slug, |
| 107 |
plugins_url( $dir . '/' . $element . '/' . $block_slug . '.js', __FILE__ ), |
| 108 |
array( 'wp-blocks', 'wp-element' ), |
| 109 |
C7WP_VERSION, |
| 110 |
1 |
| 111 |
); |
| 112 |
|
| 113 |
// Add block style. |
| 114 |
wp_register_style( |
| 115 |
$block_slug, |
| 116 |
plugins_url( $dir . '/' . $element . '/' . $block_slug . '.css', __FILE__ ), |
| 117 |
array(), |
| 118 |
C7WP_VERSION |
| 119 |
); |
| 120 |
|
| 121 |
if ( $has_frontend_js && ! $is_ct_builder ) { |
| 122 |
wp_register_script( |
| 123 |
$frontend_script_handle, |
| 124 |
plugins_url( $frontend_js_path, __FILE__ ), |
| 125 |
array(), |
| 126 |
C7WP_VERSION, |
| 127 |
true |
| 128 |
); |
| 129 |
|
| 130 |
if ( 'clubselector' === $element ) { |
| 131 |
wp_localize_script( |
| 132 |
$frontend_script_handle, |
| 133 |
'c7wp_settings', |
| 134 |
C7WP::getInstance()->get_public_js_settings() |
| 135 |
); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
// Register block script and style. |
| 140 |
$block_args = array( |
| 141 |
'editor_style' => $block_slug, // Loads both on editor. |
| 142 |
'editor_script' => $block_slug, // Loads only on editor. |
| 143 |
); |
| 144 |
|
| 145 |
if ( $has_frontend_js && ! $is_ct_builder ) { |
| 146 |
$block_args['view_script'] = $frontend_script_handle; |
| 147 |
} |
| 148 |
|
| 149 |
register_block_type( |
| 150 |
$block_name, |
| 151 |
$block_args |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
if ( $has_frontend_css ) { |
| 156 |
if ( function_exists( 'wp_enqueue_block_style' ) ) { |
| 157 |
wp_enqueue_block_style( |
| 158 |
$block_name, |
| 159 |
array( |
| 160 |
'handle' => $frontend_style_handle, |
| 161 |
'src' => plugins_url( $frontend_css_path, __FILE__ ), |
| 162 |
'path' => C7WP_ROOT . '/includes/gutenberg/' . $frontend_css_path, |
| 163 |
'ver' => C7WP_VERSION, |
| 164 |
) |
| 165 |
); |
| 166 |
} else { |
| 167 |
wp_register_style( |
| 168 |
$frontend_style_handle, |
| 169 |
plugins_url( $frontend_css_path, __FILE__ ), |
| 170 |
array(), |
| 171 |
C7WP_VERSION |
| 172 |
); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Club selector uses core buttons markup (.wp-block-buttons, .wp-block-button__link); |
| 178 |
* ensure core buttons style loads when this block is present. |
| 179 |
*/ |
| 180 |
if ( 'clubselector' === $element ) { |
| 181 |
wp_enqueue_block_style( 'c7wp/clubselector', array( 'handle' => 'wp-block-buttons' ) ); |
| 182 |
} |
| 183 |
} |
| 184 |
|