| 1 |
<?php |
| 2 |
/** |
| 3 |
* Blocks Initializer |
| 4 |
* |
| 5 |
* Enqueue CSS/JS of all the blocks. |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @package Genesis Blocks |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Enqueue block styles for the frontend. |
| 13 |
* |
| 14 |
* This intentionally runs on `init`, as it did before 3.1.9, so the frontend |
| 15 |
* stylesheet keeps its historical position before theme styles. |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
function genesis_blocks_block_assets() { |
| 20 |
|
| 21 |
if ( is_admin() ) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
// phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- Could be true or 'true'. |
| 26 |
$postfix = ( SCRIPT_DEBUG == true ) ? '' : '.min'; |
| 27 |
|
| 28 |
// Load the compiled styles. |
| 29 |
wp_enqueue_style( |
| 30 |
'genesis-blocks-style-css', |
| 31 |
plugins_url( 'dist/style-blocks.build.css', __DIR__ ), |
| 32 |
array(), |
| 33 |
filemtime( plugin_dir_path( genesis_blocks_main_plugin_file() ) . 'dist/style-blocks.build.css' ) |
| 34 |
); |
| 35 |
} |
| 36 |
add_action( 'init', 'genesis_blocks_block_assets' ); |
| 37 |
|
| 38 |
/** |
| 39 |
* Enqueue block styles for the editor iframe. |
| 40 |
* |
| 41 |
* This function uses the 'enqueue_block_assets' hook to ensure editor styles are |
| 42 |
* properly loaded in the iframe editor (WordPress 5.9+). This prevents the |
| 43 |
* warning: "genesis-blocks-block-editor-css-css was added to the iframe incorrectly". |
| 44 |
* |
| 45 |
* @since 3.1.9 |
| 46 |
*/ |
| 47 |
function genesis_blocks_editor_block_assets() { |
| 48 |
|
| 49 |
if ( is_admin() ) { |
| 50 |
wp_enqueue_style( |
| 51 |
'genesis-blocks-style-css', |
| 52 |
plugins_url( 'dist/style-blocks.build.css', __DIR__ ), |
| 53 |
array(), |
| 54 |
filemtime( plugin_dir_path( genesis_blocks_main_plugin_file() ) . 'dist/style-blocks.build.css' ) |
| 55 |
); |
| 56 |
|
| 57 |
wp_enqueue_style( |
| 58 |
'genesis-blocks-block-editor-css', |
| 59 |
plugins_url( 'dist/blocks.build.css', __DIR__ ), |
| 60 |
array( 'wp-edit-blocks' ), |
| 61 |
filemtime( plugin_dir_path( genesis_blocks_main_plugin_file() ) . 'dist/blocks.build.css' ) |
| 62 |
); |
| 63 |
} |
| 64 |
} |
| 65 |
add_action( 'enqueue_block_assets', 'genesis_blocks_editor_block_assets' ); |
| 66 |
|
| 67 |
/** |
| 68 |
* Enqueue assets for backend editor |
| 69 |
* |
| 70 |
* This function only handles JavaScript and localization now. |
| 71 |
* Editor styles have been moved to genesis_blocks_editor_block_assets() to work |
| 72 |
* correctly with the WordPress iframe editor (5.9+). |
| 73 |
* |
| 74 |
* @since 1.0.0 |
| 75 |
*/ |
| 76 |
function genesis_blocks_editor_assets() { |
| 77 |
|
| 78 |
// phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- Could be true or 'true'. |
| 79 |
$postfix = ( SCRIPT_DEBUG == true ) ? '' : '.min'; |
| 80 |
$js_metadata = require plugin_dir_path( genesis_blocks_main_plugin_file() ) . 'dist/blocks.build.asset.php'; |
| 81 |
|
| 82 |
// Load the compiled blocks into the editor. |
| 83 |
wp_enqueue_script( |
| 84 |
'genesis-blocks-block-js', |
| 85 |
plugins_url( '/dist/blocks.build.js', __DIR__ ), |
| 86 |
$js_metadata['dependencies'], |
| 87 |
$js_metadata['version'], |
| 88 |
true |
| 89 |
); |
| 90 |
|
| 91 |
// Note: Editor styles are now loaded via the |
| 92 |
// 'enqueue_block_assets' hook in genesis_blocks_editor_block_assets(). |
| 93 |
// This prevents the "added to iframe incorrectly" warning in WordPress 5.9+ |
| 94 |
|
| 95 |
$user_data = wp_get_current_user(); |
| 96 |
unset( $user_data->user_pass, $user_data->user_email ); |
| 97 |
|
| 98 |
// Pass in REST URL. |
| 99 |
wp_localize_script( |
| 100 |
'genesis-blocks-block-js', |
| 101 |
'genesis_blocks_globals', |
| 102 |
array( |
| 103 |
'rest_url' => esc_url( rest_url() ), |
| 104 |
'user_data' => $user_data, |
| 105 |
'pro_activated' => genesis_blocks_is_pro(), |
| 106 |
'is_wpe' => function_exists( 'is_wpe' ), |
| 107 |
'pattern_fallback_image' => plugins_url( 'dist/assets/images/gb-fallback-image.jpg', __DIR__ ), |
| 108 |
'featuresEnabled' => get_enabled_features(), |
| 109 |
) |
| 110 |
); |
| 111 |
} |
| 112 |
add_action( 'enqueue_block_editor_assets', 'genesis_blocks_editor_assets' ); |
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* Enqueue assets for frontend |
| 117 |
* |
| 118 |
* @since 1.0.0 |
| 119 |
*/ |
| 120 |
function genesis_blocks_frontend_assets() { |
| 121 |
|
| 122 |
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
// Load the dismissible notice js. |
| 127 |
wp_enqueue_script( |
| 128 |
'genesis-blocks-dismiss-js', |
| 129 |
plugins_url( '/dist/assets/js/dismiss.js', __DIR__ ), |
| 130 |
array(), |
| 131 |
filemtime( plugin_dir_path( genesis_blocks_main_plugin_file() ) . '/dist/assets/js/dismiss.js' ), |
| 132 |
true |
| 133 |
); |
| 134 |
} |
| 135 |
add_action( 'wp_enqueue_scripts', 'genesis_blocks_frontend_assets' ); |
| 136 |
|
| 137 |
/** |
| 138 |
* Adds the Genesis Blocks block category. |
| 139 |
* |
| 140 |
* @param array $categories Array of categories for block types. |
| 141 |
* @return array Updated block categories. |
| 142 |
*/ |
| 143 |
function genesis_blocks_add_custom_block_category( $categories ) { |
| 144 |
return array_merge( |
| 145 |
array( |
| 146 |
array( |
| 147 |
'slug' => 'genesis-blocks', |
| 148 |
'title' => __( 'Genesis Blocks', 'genesis-blocks' ), |
| 149 |
), |
| 150 |
), |
| 151 |
array_filter( |
| 152 |
$categories, |
| 153 |
function ( $category ) { |
| 154 |
return 'genesis-blocks' !== $category['slug']; |
| 155 |
} |
| 156 |
) |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
if ( class_exists( 'WP_Block_Editor_Context' ) ) { |
| 161 |
add_filter( 'block_categories_all', 'genesis_blocks_add_custom_block_category', PHP_INT_MAX ); |
| 162 |
} else { |
| 163 |
add_filter( 'block_categories', 'genesis_blocks_add_custom_block_category', PHP_INT_MAX ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Genesis Async Tagger appends the async prop to scripts when required. |
| 168 |
* |
| 169 |
* @param string $tag Tag name. |
| 170 |
* @param string $handle Script handle provided by wp_enqueue. |
| 171 |
* |
| 172 |
* @return string Maybe modified script. |
| 173 |
*/ |
| 174 |
function genesis_async_tagger( $tag, $handle ) { |
| 175 |
if ( ! strpos( $handle, '#async' ) ) { |
| 176 |
return $tag; |
| 177 |
} |
| 178 |
|
| 179 |
$tag = str_replace( '<script ', '<script async ', $tag ); |
| 180 |
|
| 181 |
return $tag; |
| 182 |
} |
| 183 |
add_filter( 'script_loader_tag', 'genesis_async_tagger', 10, 2 ); |
| 184 |
|
| 185 |
/** |
| 186 |
* Get which optional features of Genesis Blocks are enabled. |
| 187 |
* |
| 188 |
* @since 1.0.0 |
| 189 |
* @return array The list of features enabled. |
| 190 |
*/ |
| 191 |
function get_enabled_features() { |
| 192 |
return (array) apply_filters( 'genesis_blocks_features_enabled', [ 'responsiveFontSettings' ] ); |
| 193 |
} |
| 194 |
|