blocks
1 year ago
dynamic-tags
10 months ago
pattern-library
1 year ago
utils
2 years ago
class-do-css.php
2 years ago
class-dynamic-content.php
1 year ago
class-enqueue-css.php
1 year ago
class-legacy-attributes.php
4 years ago
class-map-deprecated-attributes.php
2 years ago
class-meta-handler.php
8 months ago
class-plugin-update.php
1 year ago
class-query-loop.php
2 years ago
class-query-utils.php
10 months ago
class-render-blocks.php
1 year ago
class-rest.php
1 year ago
class-settings.php
1 year ago
dashboard.php
1 year ago
defaults.php
1 year ago
deprecated.php
1 year ago
functions.php
8 months ago
general.php
8 months ago
general.php
743 lines
| 1 | <?php |
| 2 | /** |
| 3 | * General actions and filters. |
| 4 | * |
| 5 | * @package GenerateBlocks |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | add_action( 'enqueue_block_editor_assets', 'generateblocks_do_block_editor_assets' ); |
| 13 | /** |
| 14 | * Enqueue Gutenberg block assets for backend editor. |
| 15 | * |
| 16 | * @uses {wp-blocks} for block type registration & related functions. |
| 17 | * @uses {wp-element} for WP Element abstraction — structure of blocks. |
| 18 | * @uses {wp-i18n} to internationalize the block's text. |
| 19 | * @uses {wp-editor} for WP editor styles. |
| 20 | * @since 0.1 |
| 21 | */ |
| 22 | function generateblocks_do_block_editor_assets() { |
| 23 | wp_localize_script( |
| 24 | 'generateblocks-media-editor-script', |
| 25 | 'generateblocksBlockMedia', |
| 26 | [ |
| 27 | 'standardPlaceholder' => GENERATEBLOCKS_DIR_URL . 'assets/images/placeholder1280x720.png', |
| 28 | 'squarePlaceholder' => GENERATEBLOCKS_DIR_URL . 'assets/images/placeholder800x.png', |
| 29 | ] |
| 30 | ); |
| 31 | |
| 32 | wp_localize_script( |
| 33 | 'generateblocks-text-editor-script', |
| 34 | 'generateblocksBlockText', |
| 35 | [ |
| 36 | 'defaultButtonAttributes' => apply_filters( |
| 37 | 'generateblocks_default_button_attributes', |
| 38 | [ |
| 39 | 'styles' => [ |
| 40 | 'display' => 'inline-flex', |
| 41 | 'alignItems' => 'center', |
| 42 | 'backgroundColor' => '#215bc2', |
| 43 | 'color' => '#ffffff', |
| 44 | 'paddingTop' => '1rem', |
| 45 | 'paddingRight' => '2rem', |
| 46 | 'paddingBottom' => '1rem', |
| 47 | 'paddingLeft' => '2rem', |
| 48 | 'textDecoration' => 'none', |
| 49 | '&:is(:hover, :focus)' => [ |
| 50 | 'backgroundColor' => '#1a4a9b', |
| 51 | 'color' => '#ffffff', |
| 52 | ], |
| 53 | ], |
| 54 | ] |
| 55 | ), |
| 56 | ] |
| 57 | ); |
| 58 | |
| 59 | global $pagenow; |
| 60 | |
| 61 | $generateblocks_deps = array( 'wp-blocks', 'wp-i18n', 'wp-editor', 'wp-element', 'wp-compose', 'wp-data' ); |
| 62 | |
| 63 | if ( 'widgets.php' === $pagenow ) { |
| 64 | unset( $generateblocks_deps[2] ); |
| 65 | } |
| 66 | |
| 67 | $assets_file = GENERATEBLOCKS_DIR . 'dist/blocks.asset.php'; |
| 68 | $compiled_assets = file_exists( $assets_file ) |
| 69 | ? require $assets_file |
| 70 | : false; |
| 71 | |
| 72 | $assets = |
| 73 | isset( $compiled_assets['dependencies'] ) && |
| 74 | isset( $compiled_assets['version'] ) |
| 75 | ? $compiled_assets |
| 76 | : [ |
| 77 | 'dependencies' => $generateblocks_deps, |
| 78 | 'version' => filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.js' ), |
| 79 | ]; |
| 80 | |
| 81 | wp_enqueue_script( |
| 82 | 'generateblocks', |
| 83 | GENERATEBLOCKS_DIR_URL . 'dist/blocks.js', |
| 84 | $assets['dependencies'], |
| 85 | $assets['version'], |
| 86 | true |
| 87 | ); |
| 88 | |
| 89 | if ( function_exists( 'wp_set_script_translations' ) ) { |
| 90 | wp_set_script_translations( 'generateblocks', 'generateblocks' ); |
| 91 | } |
| 92 | |
| 93 | wp_enqueue_style( |
| 94 | 'generateblocks', |
| 95 | GENERATEBLOCKS_DIR_URL . 'dist/blocks.css', |
| 96 | array( 'wp-edit-blocks', 'generateblocks-packages' ), |
| 97 | filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.css' ) |
| 98 | ); |
| 99 | |
| 100 | $image_sizes = get_intermediate_image_sizes(); |
| 101 | $image_sizes = array_diff( $image_sizes, array( '1536x1536', '2048x2048' ) ); |
| 102 | $image_sizes[] = 'full'; |
| 103 | |
| 104 | wp_localize_script( |
| 105 | 'generateblocks', |
| 106 | 'generateBlocksInfo', |
| 107 | array( |
| 108 | 'imageSizes' => $image_sizes, |
| 109 | 'svgShapes' => generateblocks_get_svg_shapes(), |
| 110 | 'syncResponsivePreviews' => generateblocks_get_option( 'sync_responsive_previews' ), |
| 111 | 'excerptLength' => apply_filters( 'excerpt_length', 55 ), // phpcs:ignore -- Core filter. |
| 112 | 'excerptMore' => apply_filters( 'excerpt_more', ' ' . '[…]' ), // phpcs:ignore -- Core filter. |
| 113 | 'imagePlaceholders' => array( |
| 114 | 'standard' => GENERATEBLOCKS_DIR_URL . 'assets/images/image-placeholder.png', |
| 115 | 'square' => GENERATEBLOCKS_DIR_URL . 'assets/images/square-image-placeholder.png', |
| 116 | ), |
| 117 | 'globalContainerWidth' => generateblocks_get_global_container_width(), |
| 118 | 'queryLoopEditorPostsCap' => apply_filters( 'generateblocks_query_loop_editor_posts_cap', 50 ), |
| 119 | 'disableGoogleFonts' => generateblocks_get_option( 'disable_google_fonts' ), |
| 120 | 'typographyFontFamilyList' => generateblocks_get_font_family_list(), |
| 121 | 'useV1Blocks' => generateblocks_use_v1_blocks(), |
| 122 | ) |
| 123 | ); |
| 124 | |
| 125 | if ( function_exists( 'generate_get_color_defaults' ) ) { |
| 126 | $color_settings = wp_parse_args( |
| 127 | get_option( 'generate_settings', array() ), |
| 128 | generate_get_color_defaults() |
| 129 | ); |
| 130 | |
| 131 | $generatepressDefaultStyling = apply_filters( |
| 132 | 'generateblocks_gp_default_styling', |
| 133 | array( |
| 134 | 'buttonBackground' => $color_settings['form_button_background_color'], |
| 135 | 'buttonBackgroundHover' => $color_settings['form_button_background_color_hover'], |
| 136 | 'buttonText' => $color_settings['form_button_text_color'], |
| 137 | 'buttonTextHover' => $color_settings['form_button_text_color_hover'], |
| 138 | 'buttonPaddingTop' => '10px', |
| 139 | 'buttonPaddingRight' => '20px', |
| 140 | 'buttonPaddingBottom' => '10px', |
| 141 | 'buttonPaddingLeft' => '20px', |
| 142 | ) |
| 143 | ); |
| 144 | |
| 145 | $css = sprintf( |
| 146 | '.gb-button.button { |
| 147 | background-color: %1$s; |
| 148 | color: %2$s; |
| 149 | padding-top: %3$s; |
| 150 | padding-right: %4$s; |
| 151 | padding-bottom: %5$s; |
| 152 | padding-left: %6$s; |
| 153 | }', |
| 154 | $generatepressDefaultStyling['buttonBackground'], |
| 155 | $generatepressDefaultStyling['buttonText'], |
| 156 | $generatepressDefaultStyling['buttonPaddingTop'], |
| 157 | $generatepressDefaultStyling['buttonPaddingRight'], |
| 158 | $generatepressDefaultStyling['buttonPaddingBottom'], |
| 159 | $generatepressDefaultStyling['buttonPaddingLeft'] |
| 160 | ); |
| 161 | |
| 162 | $css .= sprintf( |
| 163 | '.gb-button.button:active, .gb-button.button:hover, .gb-button.button:focus { |
| 164 | background-color: %1$s; |
| 165 | color: %2$s; |
| 166 | }', |
| 167 | $generatepressDefaultStyling['buttonBackgroundHover'], |
| 168 | $generatepressDefaultStyling['buttonTextHover'] |
| 169 | ); |
| 170 | |
| 171 | wp_add_inline_style( 'generateblocks', $css ); |
| 172 | } |
| 173 | |
| 174 | $defaults = generateblocks_get_block_defaults(); |
| 175 | |
| 176 | wp_localize_script( |
| 177 | 'generateblocks', |
| 178 | 'generateBlocksDefaults', |
| 179 | $defaults |
| 180 | ); |
| 181 | |
| 182 | wp_localize_script( |
| 183 | 'generateblocks', |
| 184 | 'generateBlocksStyling', |
| 185 | generateblocks_get_default_styles() |
| 186 | ); |
| 187 | |
| 188 | wp_localize_script( |
| 189 | 'generateblocks', |
| 190 | 'generateBlocksLegacyDefaults', |
| 191 | array( |
| 192 | 'v_1_4_0' => GenerateBlocks_Legacy_Attributes::get_defaults( '1.4.0' ), |
| 193 | ) |
| 194 | ); |
| 195 | |
| 196 | $editor_sidebar_assets = generateblocks_get_enqueue_assets( 'editor-sidebar' ); |
| 197 | |
| 198 | wp_enqueue_script( |
| 199 | 'generateblocks-editor-sidebar', |
| 200 | GENERATEBLOCKS_DIR_URL . 'dist/editor-sidebar.js', |
| 201 | $editor_sidebar_assets['dependencies'], |
| 202 | $editor_sidebar_assets['version'], |
| 203 | true |
| 204 | ); |
| 205 | |
| 206 | if ( function_exists( 'wp_set_script_translations' ) ) { |
| 207 | wp_set_script_translations( 'generateblocks-editor-sidebar', 'generateblocks' ); |
| 208 | } |
| 209 | |
| 210 | wp_enqueue_style( |
| 211 | 'generateblocks-editor-sidebar', |
| 212 | GENERATEBLOCKS_DIR_URL . 'dist/editor-sidebar.css', |
| 213 | array( 'wp-components' ), |
| 214 | filemtime( GENERATEBLOCKS_DIR . 'dist/editor-sidebar.css' ) |
| 215 | ); |
| 216 | |
| 217 | $packages_asset_info = generateblocks_get_enqueue_assets( 'packages' ); |
| 218 | wp_register_style( |
| 219 | 'generateblocks-packages', |
| 220 | GENERATEBLOCKS_DIR_URL . 'dist/packages.css', |
| 221 | '', |
| 222 | $packages_asset_info['version'] |
| 223 | ); |
| 224 | |
| 225 | // Enqueue scripts for all edge22 packages in the plugin. |
| 226 | $package_json = GENERATEBLOCKS_DIR . 'package.json'; |
| 227 | |
| 228 | if ( file_exists( $package_json ) ) { |
| 229 | $package_json_parsed = json_decode( |
| 230 | file_get_contents( $package_json ), // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 231 | true |
| 232 | ); |
| 233 | |
| 234 | $edge22_packages = array_filter( |
| 235 | $package_json_parsed['dependencies'], |
| 236 | function( $package_name ) { |
| 237 | return 0 === strpos( $package_name, '@edge22/' ); |
| 238 | }, |
| 239 | ARRAY_FILTER_USE_KEY |
| 240 | ); |
| 241 | |
| 242 | foreach ( $edge22_packages as $name => $version ) { |
| 243 | $name = str_replace( '@edge22/', '', $name ); |
| 244 | $path = GENERATEBLOCKS_DIR . "dist/{$name}-imported.asset.php"; |
| 245 | |
| 246 | if ( ! file_exists( $path ) ) { |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | $package_info = require $path; |
| 251 | |
| 252 | wp_register_script( |
| 253 | "generateblocks-$name", |
| 254 | GENERATEBLOCKS_DIR_URL . 'dist/' . $name . '.js', |
| 255 | $package_info['dependencies'], |
| 256 | $version, |
| 257 | true |
| 258 | ); |
| 259 | |
| 260 | wp_register_style( |
| 261 | "generateblocks-$name", |
| 262 | GENERATEBLOCKS_DIR_URL . 'dist/' . $name . '.css', |
| 263 | [], |
| 264 | $version |
| 265 | ); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | $editor_assets = generateblocks_get_enqueue_assets( 'editor' ); |
| 270 | |
| 271 | wp_enqueue_script( |
| 272 | 'generateblocks-editor', |
| 273 | GENERATEBLOCKS_DIR_URL . 'dist/editor.js', |
| 274 | $editor_assets['dependencies'], |
| 275 | $editor_assets['version'], |
| 276 | true |
| 277 | ); |
| 278 | |
| 279 | $tags = GenerateBlocks_Register_Dynamic_Tag::get_tags(); |
| 280 | $tag_list = []; |
| 281 | |
| 282 | foreach ( $tags as $tag => $data ) { |
| 283 | $relevant_data = $data; |
| 284 | unset( $relevant_data['return'] ); |
| 285 | if ( $data ) { |
| 286 | $tag_list[] = $relevant_data; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | wp_localize_script( |
| 291 | 'generateblocks-editor', |
| 292 | 'generateBlocksEditor', |
| 293 | [ |
| 294 | 'useV1Blocks' => generateblocks_use_v1_blocks(), |
| 295 | 'dynamicTags' => $tag_list, |
| 296 | 'hasGPFontLibrary' => function_exists( 'generatepress_is_module_active' ) |
| 297 | ? generatepress_is_module_active( 'generate_package_font_library', 'GENERATE_FONT_LIBRARY' ) |
| 298 | : false, |
| 299 | 'dateFormat' => get_option( 'date_format' ), |
| 300 | 'wpContentUrl' => content_url(), |
| 301 | 'typographyFontFamilyList' => generateblocks_get_font_family_list(), |
| 302 | 'dynamicTagsPreview' => apply_filters( 'generateblocks_dynamic_tags_preview', true ) ? 'enabled' : 'disabled', |
| 303 | ] |
| 304 | ); |
| 305 | |
| 306 | wp_enqueue_style( |
| 307 | 'generateblocks-editor', |
| 308 | GENERATEBLOCKS_DIR_URL . 'dist/editor.css', |
| 309 | array( 'wp-edit-blocks', 'generateblocks-packages' ), |
| 310 | filemtime( GENERATEBLOCKS_DIR . 'dist/editor.css' ) |
| 311 | ); |
| 312 | } |
| 313 | |
| 314 | add_filter( 'block_categories_all', 'generateblocks_do_category' ); |
| 315 | /** |
| 316 | * Add GeneratePress category to Gutenberg. |
| 317 | * |
| 318 | * @param array $categories Existing categories. |
| 319 | * @since 0.1 |
| 320 | */ |
| 321 | function generateblocks_do_category( $categories ) { |
| 322 | array_unshift( |
| 323 | $categories, |
| 324 | [ |
| 325 | 'slug' => 'generateblocks', |
| 326 | 'title' => __( 'GenerateBlocks', 'generateblocks' ), |
| 327 | ] |
| 328 | ); |
| 329 | |
| 330 | return $categories; |
| 331 | } |
| 332 | |
| 333 | add_action( 'wp_enqueue_scripts', 'generateblocks_do_google_fonts' ); |
| 334 | add_action( 'enqueue_block_editor_assets', 'generateblocks_do_google_fonts' ); |
| 335 | /** |
| 336 | * Do Google Fonts. |
| 337 | * |
| 338 | * @since 0.1 |
| 339 | */ |
| 340 | function generateblocks_do_google_fonts() { |
| 341 | if ( generateblocks_get_option( 'disable_google_fonts' ) ) { |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | $fonts_url = generateblocks_get_google_fonts_uri(); |
| 346 | |
| 347 | if ( $fonts_url ) { |
| 348 | wp_enqueue_style( 'generateblocks-google-fonts', $fonts_url, array(), null, 'all' ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | add_filter( 'generateblocks_css_print_method', 'generateblocks_set_css_print_method' ); |
| 353 | /** |
| 354 | * Set our CSS print method. |
| 355 | * |
| 356 | * @param string $method Existing method. |
| 357 | */ |
| 358 | function generateblocks_set_css_print_method( $method ) { |
| 359 | $method = generateblocks_get_option( 'css_print_method' ); |
| 360 | |
| 361 | if ( is_single() ) { |
| 362 | $method = 'inline'; |
| 363 | } |
| 364 | |
| 365 | return $method; |
| 366 | } |
| 367 | |
| 368 | add_filter( 'excerpt_allowed_blocks', 'generateblocks_set_excerpt_allowed_blocks' ); |
| 369 | /** |
| 370 | * Add blocks that can be displayed in post excerpts. |
| 371 | * |
| 372 | * @param array $allowed Existing allowed blocks. |
| 373 | * @since 1.0 |
| 374 | */ |
| 375 | function generateblocks_set_excerpt_allowed_blocks( $allowed ) { |
| 376 | $allowed[] = 'generateblocks/headline'; |
| 377 | $allowed[] = 'generateblocks/container'; |
| 378 | $allowed[] = 'generateblocks/text'; |
| 379 | $allowed[] = 'generateblocks/element'; |
| 380 | |
| 381 | return $allowed; |
| 382 | } |
| 383 | |
| 384 | add_filter( 'excerpt_allowed_wrapper_blocks', 'generateblocks_set_excerpt_allowed_wrapper_blocks' ); |
| 385 | /** |
| 386 | * Allows excerpts to be generated from the `innerBlocks` of these wrappers. |
| 387 | * |
| 388 | * @param array $allowed Existing allowed wrapper blocks. |
| 389 | * @since 1.5.0 |
| 390 | */ |
| 391 | function generateblocks_set_excerpt_allowed_wrapper_blocks( $allowed ) { |
| 392 | $allowed[] = 'generateblocks/container'; |
| 393 | $allowed[] = 'generateblocks/element'; |
| 394 | |
| 395 | return $allowed; |
| 396 | } |
| 397 | |
| 398 | add_filter( 'generateblocks_before_container_close', 'generateblocks_do_shape_divider', 10, 2 ); |
| 399 | /** |
| 400 | * Add shape divider to Container. |
| 401 | * |
| 402 | * @since 1.2.0 |
| 403 | * @param string $output The current block output. |
| 404 | * @param array $attributes The current block attributes. |
| 405 | */ |
| 406 | function generateblocks_do_shape_divider( $output, $attributes ) { |
| 407 | $defaults = generateblocks_get_block_defaults(); |
| 408 | |
| 409 | $settings = wp_parse_args( |
| 410 | $attributes, |
| 411 | $defaults['container'] |
| 412 | ); |
| 413 | |
| 414 | if ( ! empty( $settings['shapeDividers'] ) ) { |
| 415 | $shapes = generateblocks_get_svg_shapes(); |
| 416 | $shape_values = array(); |
| 417 | |
| 418 | foreach ( $shapes as $group => $data ) { |
| 419 | if ( ! empty( $data['svgs'] ) && is_array( $data['svgs'] ) ) { |
| 420 | foreach ( $data['svgs'] as $key => $shape ) { |
| 421 | $shape_values[ $key ] = $shape['icon']; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | $output .= '<div class="gb-shapes">'; |
| 427 | |
| 428 | foreach ( (array) $settings['shapeDividers'] as $index => $option ) { |
| 429 | if ( ! empty( $option['shape'] ) ) { |
| 430 | if ( isset( $shape_values[ $option['shape'] ] ) ) { |
| 431 | $shapeNumber = $index + 1; |
| 432 | |
| 433 | $output .= sprintf( |
| 434 | '<div class="gb-shape gb-shape-' . $shapeNumber . '">%s</div>', |
| 435 | $shape_values[ $option['shape'] ] |
| 436 | ); |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | $output .= '</div>'; |
| 442 | } |
| 443 | |
| 444 | return $output; |
| 445 | } |
| 446 | |
| 447 | add_filter( 'generateblocks_do_content', 'generateblocks_do_widget_styling' ); |
| 448 | /** |
| 449 | * Process all widget content for potential styling. |
| 450 | * |
| 451 | * @since 1.3.4 |
| 452 | * @param string $content The existing content to process. |
| 453 | */ |
| 454 | function generateblocks_do_widget_styling( $content ) { |
| 455 | $widget_blocks = get_option( 'widget_block' ); |
| 456 | |
| 457 | foreach ( (array) $widget_blocks as $block ) { |
| 458 | if ( isset( $block['content'] ) ) { |
| 459 | $content .= $block['content']; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | return $content; |
| 464 | } |
| 465 | |
| 466 | add_filter( 'generateblocks_attr_container', 'generateblocks_set_inline_background_style', 10, 2 ); |
| 467 | /** |
| 468 | * Add our background image attribute to the Container. |
| 469 | * |
| 470 | * @since 1.5.0 |
| 471 | * @param array $attributes Existing attributes. |
| 472 | * @param array $settings Block settings. |
| 473 | */ |
| 474 | function generateblocks_set_inline_background_style( $attributes, $settings ) { |
| 475 | if ( generateblocks_has_background_image( $settings ) && $settings['bgImageInline'] ) { |
| 476 | $url = generateblocks_get_background_image_url( $settings ); |
| 477 | |
| 478 | if ( $url ) { |
| 479 | $attribute_name = 'background-image'; |
| 480 | |
| 481 | if ( 'element' !== $settings['bgOptions']['selector'] ) { |
| 482 | $attribute_name = '--' . $attribute_name; |
| 483 | } |
| 484 | |
| 485 | $attributes['style'] = $attribute_name . ': url(' . esc_url( $url ) . ');'; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return $attributes; |
| 490 | } |
| 491 | |
| 492 | add_filter( 'generateblocks_block_css_selector', 'generateblocks_set_block_css_selectors', 10, 3 ); |
| 493 | /** |
| 494 | * Change our block selectors if needed. |
| 495 | * |
| 496 | * @param string $selector Existing selector. |
| 497 | * @param string $name The block name. |
| 498 | * @param array $attributes The block attributes. |
| 499 | */ |
| 500 | function generateblocks_set_block_css_selectors( $selector, $name, $attributes ) { |
| 501 | $blockVersion = ! empty( $attributes['blockVersion'] ) ? $attributes['blockVersion'] : 1; |
| 502 | $defaults = generateblocks_get_block_defaults(); |
| 503 | |
| 504 | if ( 'button' === $name ) { |
| 505 | $settings = wp_parse_args( |
| 506 | $attributes, |
| 507 | $defaults['button'] |
| 508 | ); |
| 509 | |
| 510 | if ( $blockVersion < 3 ) { |
| 511 | // Old versions of the this block used this backwards logic |
| 512 | // to determine whether to remove the "a" to the selector. |
| 513 | $clean_selector = $selector; |
| 514 | $selector = 'a' . $selector; |
| 515 | |
| 516 | if ( isset( $attributes['hasUrl'] ) && ! $attributes['hasUrl'] ) { |
| 517 | $selector = $clean_selector; |
| 518 | } |
| 519 | } else { |
| 520 | $is_link = ( |
| 521 | ! empty( $settings['hasUrl'] ) || |
| 522 | ! empty( $settings['dynamicLinkType'] ) |
| 523 | ) && 'link' === $settings['buttonType']; |
| 524 | |
| 525 | if ( $is_link ) { |
| 526 | $selector = 'a' . $selector; |
| 527 | } |
| 528 | |
| 529 | if ( 'button' === $settings['buttonType'] ) { |
| 530 | $selector = 'button' . $selector; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | if ( $settings['hasButtonContainer'] || $blockVersion < 3 ) { |
| 535 | $selector = '.gb-button-wrapper ' . $selector; |
| 536 | } elseif ( isset( $settings['isPagination'] ) && $settings['isPagination'] ) { |
| 537 | $selector = '.gb-query-loop-pagination ' . $selector; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | if ( 'headline' === $name ) { |
| 542 | $settings = wp_parse_args( |
| 543 | $attributes, |
| 544 | $defaults['headline'] |
| 545 | ); |
| 546 | |
| 547 | if ( apply_filters( 'generateblocks_headline_selector_tagname', true, $attributes ) ) { |
| 548 | $selector = $settings['element'] . $selector; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | return $selector; |
| 553 | } |
| 554 | |
| 555 | add_action( 'init', 'generateblocks_register_user_meta' ); |
| 556 | /** |
| 557 | * Register GenerateBlocks custom user meta fields. |
| 558 | * |
| 559 | * @return void |
| 560 | */ |
| 561 | function generateblocks_register_user_meta() { |
| 562 | register_meta( |
| 563 | 'user', |
| 564 | GenerateBlocks_Rest::ONBOARDING_META_KEY, |
| 565 | array( |
| 566 | 'type' => 'object', |
| 567 | 'single' => true, |
| 568 | 'show_in_rest' => array( |
| 569 | 'schema' => array( |
| 570 | 'type' => 'object', |
| 571 | 'properties' => array( |
| 572 | 'insert_inner_container' => array( 'type' => 'boolean' ), |
| 573 | ), |
| 574 | 'additionalProperties' => array( |
| 575 | 'type' => 'boolean', |
| 576 | ), |
| 577 | ), |
| 578 | ), |
| 579 | ) |
| 580 | ); |
| 581 | } |
| 582 | |
| 583 | add_filter( 'block_editor_settings_all', 'generateblocks_do_block_css_reset', 15 ); |
| 584 | /** |
| 585 | * This resets the `max-width`, `margin-left`, and `margin-right` properties for our blocks in the editor. |
| 586 | * We have to do this as most themes use `.wp-block` to set a `max-width` and auto margins. |
| 587 | * |
| 588 | * We used to do this directly in the block CSS if those block attributes didn't exist, but this allows us |
| 589 | * to overwrite the reset in the `block_editor_settings_all` filter with a later priority. |
| 590 | * |
| 591 | * @param array $editor_settings The existing editor settings. |
| 592 | */ |
| 593 | function generateblocks_do_block_css_reset( $editor_settings ) { |
| 594 | $css = '.gb-container, .gb-headline, .gb-button {max-width:unset;margin-left:0;margin-right:0;}'; |
| 595 | $editor_settings['styles'][] = [ 'css' => $css ]; |
| 596 | |
| 597 | $blocks_to_reset = [ |
| 598 | '.editor-styles-wrapper .wp-block-generateblocks-text:where(:not(h1, h2, h3, h4, h5, h6, p))', |
| 599 | '.editor-styles-wrapper .wp-block-generateblocks-element', |
| 600 | '.editor-styles-wrapper .wp-block-generateblocks-shape', |
| 601 | '.editor-styles-wrapper .wp-block-generateblocks-media', |
| 602 | '.editor-styles-wrapper .wp-block-generateblocks-query', |
| 603 | '.editor-styles-wrapper .wp-block-generateblocks-query-no-results', |
| 604 | '.editor-styles-wrapper .wp-block-generateblocks-query-page-numbers', |
| 605 | '.editor-styles-wrapper .wp-block-generateblocks-looper', |
| 606 | '.editor-styles-wrapper .wp-block-generateblocks-loop-item', |
| 607 | ]; |
| 608 | |
| 609 | $heading_blocks_to_reset = [ |
| 610 | '.editor-styles-wrapper .wp-block-generateblocks-text:where(h1, h2, h3, h4, h5, h6, p)', |
| 611 | ]; |
| 612 | |
| 613 | $css = implode( ',', $blocks_to_reset ) . '{max-width:unset;margin:0;}'; |
| 614 | $css .= implode( ',', $heading_blocks_to_reset ) . '{max-width:unset;margin-left:0;margin-right:0;}'; |
| 615 | $editor_settings['styles'][] = [ 'css' => $css ]; |
| 616 | |
| 617 | return $editor_settings; |
| 618 | } |
| 619 | |
| 620 | add_filter( 'generateblocks_css_output', 'generateblocks_add_general_css' ); |
| 621 | /** |
| 622 | * Add general CSS that doesn't apply to our own blocks. |
| 623 | * |
| 624 | * @param string $css Existing CSS. |
| 625 | */ |
| 626 | function generateblocks_add_general_css( $css ) { |
| 627 | $container_width = generateblocks_get_global_container_width(); |
| 628 | |
| 629 | if ( $container_width ) { |
| 630 | $css .= ':root{--gb-container-width:' . $container_width . ';}'; |
| 631 | } |
| 632 | |
| 633 | $css .= '.gb-container .wp-block-image img{vertical-align:middle;}'; |
| 634 | $css .= '.gb-grid-wrapper .wp-block-image{margin-bottom:0;}'; |
| 635 | $css .= '.gb-highlight{background:none;}'; |
| 636 | $css .= '.gb-shape{line-height:0;}'; |
| 637 | |
| 638 | return $css; |
| 639 | } |
| 640 | |
| 641 | add_filter( 'block_editor_settings_all', 'generateblocks_do_block_editor_styles', 15 ); |
| 642 | /** |
| 643 | * Add our block editor styles. |
| 644 | * |
| 645 | * @param array $editor_settings The existing editor settings. |
| 646 | */ |
| 647 | function generateblocks_do_block_editor_styles( $editor_settings ) { |
| 648 | $container_width = generateblocks_get_global_container_width(); |
| 649 | |
| 650 | $editor_settings['styles'][] = array( |
| 651 | 'css' => ':root{--gb-container-width:' . $container_width . ';}', |
| 652 | ); |
| 653 | |
| 654 | $editor_settings['styles'][] = array( |
| 655 | 'css' => '.gb-shape{line-height:0;}', |
| 656 | ); |
| 657 | |
| 658 | return $editor_settings; |
| 659 | } |
| 660 | |
| 661 | add_action( 'enqueue_block_editor_assets', 'generateblocks_set_editor_permissions', 0 ); |
| 662 | /** |
| 663 | * Output permissions for use in the editor. |
| 664 | * |
| 665 | * @return void |
| 666 | */ |
| 667 | function generateblocks_set_editor_permissions() { |
| 668 | $permissions = apply_filters( |
| 669 | 'generateblocks_permissions', |
| 670 | [ |
| 671 | 'isAdminUser' => current_user_can( 'manage_options' ), |
| 672 | 'canEditPosts' => current_user_can( 'edit_posts' ), |
| 673 | 'isGbProActive' => is_plugin_active( 'generateblocks-pro/plugin.php' ), |
| 674 | 'isGpPremiumActive' => is_plugin_active( 'gp-premium/gp-premium.php' ), |
| 675 | ] |
| 676 | ); |
| 677 | |
| 678 | $permission_object = wp_json_encode( $permissions ); |
| 679 | wp_register_script( 'generateblocks-editor-permissions', '', [], '1.0', false ); |
| 680 | wp_enqueue_script( 'generateblocks-editor-permissions' ); |
| 681 | $script = sprintf( |
| 682 | 'const gbPermissions = %s; |
| 683 | Object.freeze( gbPermissions );', |
| 684 | $permission_object |
| 685 | ); |
| 686 | wp_add_inline_script( 'generateblocks-editor-permissions', $script ); |
| 687 | } |
| 688 | |
| 689 | add_filter( 'render_block', 'generateblocks_do_html_attributes_escaping', 20, 2 ); |
| 690 | /** |
| 691 | * Filter the rendered block content and escape HTML attributes. |
| 692 | * |
| 693 | * @param string $content The block content about to be appended to the post content. |
| 694 | * @param array $block The full block, including name and attributes. |
| 695 | * @return string |
| 696 | */ |
| 697 | function generateblocks_do_html_attributes_escaping( $content, $block ) { |
| 698 | $html_attributes = $block['attrs']['htmlAttributes'] ?? []; |
| 699 | $link_attributes = $block['attrs']['linkHtmlAttributes'] ?? []; |
| 700 | |
| 701 | if ( empty( $html_attributes ) && empty( $link_attributes ) ) { |
| 702 | return $content; |
| 703 | } |
| 704 | |
| 705 | $v1_block_names = generateblocks_get_v1_block_names(); |
| 706 | $block_name = $block['blockName'] ?? ''; |
| 707 | |
| 708 | // Only do this for our non-v1 blocks. |
| 709 | if ( |
| 710 | ! generateblocks_str_starts_with( $block_name, 'generateblocks' ) || |
| 711 | in_array( $block_name, $v1_block_names, true ) |
| 712 | ) { |
| 713 | return $content; |
| 714 | } |
| 715 | |
| 716 | $content = generateblocks_with_escaped_attributes( |
| 717 | $content, |
| 718 | [ |
| 719 | 'block_html_attrs' => $html_attributes, |
| 720 | 'link_html_attrs' => $link_attributes, |
| 721 | ] |
| 722 | ); |
| 723 | |
| 724 | return $content; |
| 725 | } |
| 726 | |
| 727 | add_filter( 'generateblocks_allowed_option_keys_rest_api', 'generateblocks_allow_additional_option_keys_rest_api' ); |
| 728 | /** |
| 729 | * Allow additional option keys to be accessible via the REST API. |
| 730 | * |
| 731 | * @param array $allowed_keys Existing allowed keys. |
| 732 | */ |
| 733 | function generateblocks_allow_additional_option_keys_rest_api( $allowed_keys ) { |
| 734 | if ( ! is_array( $allowed_keys ) ) { |
| 735 | $allowed_keys = []; |
| 736 | } |
| 737 | |
| 738 | $acf_option_keys = generateblocks_get_acf_option_field_keys(); |
| 739 | $allowed_keys = array_merge( $allowed_keys, $acf_option_keys ); |
| 740 | |
| 741 | return $allowed_keys; |
| 742 | } |
| 743 |