blocks
2 years ago
pattern-library
2 years ago
utils
2 years ago
class-do-css.php
2 years ago
class-dynamic-content.php
2 years ago
class-enqueue-css.php
3 years ago
class-legacy-attributes.php
4 years ago
class-map-deprecated-attributes.php
2 years ago
class-plugin-update.php
5 years ago
class-query-loop.php
2 years ago
class-render-blocks.php
3 years ago
class-rest.php
2 years ago
class-settings.php
2 years ago
dashboard.php
3 years ago
defaults.php
2 years ago
functions.php
2 years ago
general.php
2 years ago
general.php
481 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 | global $pagenow; |
| 24 | |
| 25 | $generateblocks_deps = array( 'wp-blocks', 'wp-i18n', 'wp-editor', 'wp-element', 'wp-compose', 'wp-data' ); |
| 26 | |
| 27 | if ( 'widgets.php' === $pagenow ) { |
| 28 | unset( $generateblocks_deps[2] ); |
| 29 | } |
| 30 | |
| 31 | $assets_file = GENERATEBLOCKS_DIR . 'dist/blocks.asset.php'; |
| 32 | $compiled_assets = file_exists( $assets_file ) |
| 33 | ? require $assets_file |
| 34 | : false; |
| 35 | |
| 36 | $assets = |
| 37 | isset( $compiled_assets['dependencies'] ) && |
| 38 | isset( $compiled_assets['version'] ) |
| 39 | ? $compiled_assets |
| 40 | : [ |
| 41 | 'dependencies' => $generateblocks_deps, |
| 42 | 'version' => filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.js' ), |
| 43 | ]; |
| 44 | |
| 45 | wp_enqueue_script( |
| 46 | 'generateblocks', |
| 47 | GENERATEBLOCKS_DIR_URL . 'dist/blocks.js', |
| 48 | $assets['dependencies'], |
| 49 | $assets['version'], |
| 50 | true |
| 51 | ); |
| 52 | |
| 53 | if ( function_exists( 'wp_set_script_translations' ) ) { |
| 54 | wp_set_script_translations( 'generateblocks', 'generateblocks' ); |
| 55 | } |
| 56 | |
| 57 | wp_enqueue_style( |
| 58 | 'generateblocks', |
| 59 | GENERATEBLOCKS_DIR_URL . 'dist/blocks.css', |
| 60 | array( 'wp-edit-blocks' ), |
| 61 | filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.css' ) |
| 62 | ); |
| 63 | |
| 64 | $image_sizes = get_intermediate_image_sizes(); |
| 65 | $image_sizes = array_diff( $image_sizes, array( '1536x1536', '2048x2048' ) ); |
| 66 | $image_sizes[] = 'full'; |
| 67 | |
| 68 | wp_localize_script( |
| 69 | 'generateblocks', |
| 70 | 'generateBlocksInfo', |
| 71 | array( |
| 72 | 'imageSizes' => $image_sizes, |
| 73 | 'svgShapes' => generateblocks_get_svg_shapes(), |
| 74 | 'syncResponsivePreviews' => generateblocks_get_option( 'sync_responsive_previews' ), |
| 75 | 'excerptLength' => apply_filters( 'excerpt_length', 55 ), // phpcs:ignore -- Core filter. |
| 76 | 'excerptMore' => apply_filters( 'excerpt_more', ' ' . '[…]' ), // phpcs:ignore -- Core filter. |
| 77 | 'imagePlaceholders' => array( |
| 78 | 'standard' => GENERATEBLOCKS_DIR_URL . 'assets/images/image-placeholder.png', |
| 79 | 'square' => GENERATEBLOCKS_DIR_URL . 'assets/images/square-image-placeholder.png', |
| 80 | ), |
| 81 | 'globalContainerWidth' => generateblocks_get_global_container_width(), |
| 82 | 'queryLoopEditorPostsCap' => apply_filters( 'generateblocks_query_loop_editor_posts_cap', 50 ), |
| 83 | 'disableGoogleFonts' => generateblocks_get_option( 'disable_google_fonts' ), |
| 84 | 'typographyFontFamilyList' => generateblocks_get_font_family_list(), |
| 85 | ) |
| 86 | ); |
| 87 | |
| 88 | if ( function_exists( 'generate_get_color_defaults' ) ) { |
| 89 | $color_settings = wp_parse_args( |
| 90 | get_option( 'generate_settings', array() ), |
| 91 | generate_get_color_defaults() |
| 92 | ); |
| 93 | |
| 94 | $generatepressDefaultStyling = apply_filters( |
| 95 | 'generateblocks_gp_default_styling', |
| 96 | array( |
| 97 | 'buttonBackground' => $color_settings['form_button_background_color'], |
| 98 | 'buttonBackgroundHover' => $color_settings['form_button_background_color_hover'], |
| 99 | 'buttonText' => $color_settings['form_button_text_color'], |
| 100 | 'buttonTextHover' => $color_settings['form_button_text_color_hover'], |
| 101 | 'buttonPaddingTop' => '10px', |
| 102 | 'buttonPaddingRight' => '20px', |
| 103 | 'buttonPaddingBottom' => '10px', |
| 104 | 'buttonPaddingLeft' => '20px', |
| 105 | ) |
| 106 | ); |
| 107 | |
| 108 | $css = sprintf( |
| 109 | '.gb-button.button { |
| 110 | background-color: %1$s; |
| 111 | color: %2$s; |
| 112 | padding-top: %3$s; |
| 113 | padding-right: %4$s; |
| 114 | padding-bottom: %5$s; |
| 115 | padding-left: %6$s; |
| 116 | }', |
| 117 | $generatepressDefaultStyling['buttonBackground'], |
| 118 | $generatepressDefaultStyling['buttonText'], |
| 119 | $generatepressDefaultStyling['buttonPaddingTop'], |
| 120 | $generatepressDefaultStyling['buttonPaddingRight'], |
| 121 | $generatepressDefaultStyling['buttonPaddingBottom'], |
| 122 | $generatepressDefaultStyling['buttonPaddingLeft'] |
| 123 | ); |
| 124 | |
| 125 | $css .= sprintf( |
| 126 | '.gb-button.button:active, .gb-button.button:hover, .gb-button.button:focus { |
| 127 | background-color: %1$s; |
| 128 | color: %2$s; |
| 129 | }', |
| 130 | $generatepressDefaultStyling['buttonBackgroundHover'], |
| 131 | $generatepressDefaultStyling['buttonTextHover'] |
| 132 | ); |
| 133 | |
| 134 | wp_add_inline_style( 'generateblocks', $css ); |
| 135 | } |
| 136 | |
| 137 | $defaults = generateblocks_get_block_defaults(); |
| 138 | |
| 139 | wp_localize_script( |
| 140 | 'generateblocks', |
| 141 | 'generateBlocksDefaults', |
| 142 | $defaults |
| 143 | ); |
| 144 | |
| 145 | wp_localize_script( |
| 146 | 'generateblocks', |
| 147 | 'generateBlocksStyling', |
| 148 | generateblocks_get_default_styles() |
| 149 | ); |
| 150 | |
| 151 | wp_localize_script( |
| 152 | 'generateblocks', |
| 153 | 'generateBlocksLegacyDefaults', |
| 154 | array( |
| 155 | 'v_1_4_0' => GenerateBlocks_Legacy_Attributes::get_defaults( '1.4.0' ), |
| 156 | ) |
| 157 | ); |
| 158 | |
| 159 | $editor_sidebar_assets = generateblocks_get_enqueue_assets( 'editor-sidebar' ); |
| 160 | |
| 161 | wp_enqueue_script( |
| 162 | 'generateblocks-editor-sidebar', |
| 163 | GENERATEBLOCKS_DIR_URL . 'dist/editor-sidebar.js', |
| 164 | $editor_sidebar_assets['dependencies'], |
| 165 | $editor_sidebar_assets['version'], |
| 166 | true |
| 167 | ); |
| 168 | |
| 169 | if ( function_exists( 'wp_set_script_translations' ) ) { |
| 170 | wp_set_script_translations( 'generateblocks-editor-sidebar', 'generateblocks' ); |
| 171 | } |
| 172 | |
| 173 | wp_enqueue_style( |
| 174 | 'generateblocks-editor-sidebar', |
| 175 | GENERATEBLOCKS_DIR_URL . 'dist/editor-sidebar.css', |
| 176 | array( 'wp-components' ), |
| 177 | filemtime( GENERATEBLOCKS_DIR . 'dist/editor-sidebar.css' ) |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | if ( version_compare( $GLOBALS['wp_version'], '5.8-alpha-1', '<' ) ) { |
| 182 | add_filter( 'block_categories', 'generateblocks_do_category' ); |
| 183 | } else { |
| 184 | add_filter( 'block_categories_all', 'generateblocks_do_category' ); |
| 185 | } |
| 186 | /** |
| 187 | * Add GeneratePress category to Gutenberg. |
| 188 | * |
| 189 | * @param array $categories Existing categories. |
| 190 | * @since 0.1 |
| 191 | */ |
| 192 | function generateblocks_do_category( $categories ) { |
| 193 | return array_merge( |
| 194 | array( |
| 195 | array( |
| 196 | 'slug' => 'generateblocks', |
| 197 | 'title' => __( 'GenerateBlocks', 'generateblocks' ), |
| 198 | ), |
| 199 | ), |
| 200 | $categories |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | add_action( 'wp_enqueue_scripts', 'generateblocks_do_google_fonts' ); |
| 205 | add_action( 'enqueue_block_editor_assets', 'generateblocks_do_google_fonts' ); |
| 206 | /** |
| 207 | * Do Google Fonts. |
| 208 | * |
| 209 | * @since 0.1 |
| 210 | */ |
| 211 | function generateblocks_do_google_fonts() { |
| 212 | if ( generateblocks_get_option( 'disable_google_fonts' ) ) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | $fonts_url = generateblocks_get_google_fonts_uri(); |
| 217 | |
| 218 | if ( $fonts_url ) { |
| 219 | wp_enqueue_style( 'generateblocks-google-fonts', $fonts_url, array(), null, 'all' ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | add_filter( 'generateblocks_css_print_method', 'generateblocks_set_css_print_method' ); |
| 224 | /** |
| 225 | * Set our CSS print method. |
| 226 | * |
| 227 | * @param string $method Existing method. |
| 228 | */ |
| 229 | function generateblocks_set_css_print_method( $method ) { |
| 230 | $method = generateblocks_get_option( 'css_print_method' ); |
| 231 | |
| 232 | if ( is_single() ) { |
| 233 | $method = 'inline'; |
| 234 | } |
| 235 | |
| 236 | return $method; |
| 237 | } |
| 238 | |
| 239 | add_filter( 'excerpt_allowed_blocks', 'generateblocks_set_excerpt_allowed_blocks' ); |
| 240 | /** |
| 241 | * Add blocks that can be displayed in post excerpts. |
| 242 | * |
| 243 | * @param array $allowed Existing allowed blocks. |
| 244 | * @since 1.0 |
| 245 | */ |
| 246 | function generateblocks_set_excerpt_allowed_blocks( $allowed ) { |
| 247 | $allowed[] = 'generateblocks/headline'; |
| 248 | $allowed[] = 'generateblocks/container'; |
| 249 | |
| 250 | return $allowed; |
| 251 | } |
| 252 | |
| 253 | add_filter( 'excerpt_allowed_wrapper_blocks', 'generateblocks_set_excerpt_allowed_wrapper_blocks' ); |
| 254 | /** |
| 255 | * Allows excerpts to be generated from the `innerBlocks` of these wrappers. |
| 256 | * |
| 257 | * @param array $allowed Existing allowed wrapper blocks. |
| 258 | * @since 1.5.0 |
| 259 | */ |
| 260 | function generateblocks_set_excerpt_allowed_wrapper_blocks( $allowed ) { |
| 261 | $allowed[] = 'generateblocks/container'; |
| 262 | |
| 263 | return $allowed; |
| 264 | } |
| 265 | |
| 266 | add_filter( 'generateblocks_before_container_close', 'generateblocks_do_shape_divider', 10, 2 ); |
| 267 | /** |
| 268 | * Add shape divider to Container. |
| 269 | * |
| 270 | * @since 1.2.0 |
| 271 | * @param string $output The current block output. |
| 272 | * @param array $attributes The current block attributes. |
| 273 | */ |
| 274 | function generateblocks_do_shape_divider( $output, $attributes ) { |
| 275 | $defaults = generateblocks_get_block_defaults(); |
| 276 | |
| 277 | $settings = wp_parse_args( |
| 278 | $attributes, |
| 279 | $defaults['container'] |
| 280 | ); |
| 281 | |
| 282 | if ( ! empty( $settings['shapeDividers'] ) ) { |
| 283 | $shapes = generateblocks_get_svg_shapes(); |
| 284 | $shape_values = array(); |
| 285 | |
| 286 | foreach ( $shapes as $group => $data ) { |
| 287 | if ( ! empty( $data['svgs'] ) && is_array( $data['svgs'] ) ) { |
| 288 | foreach ( $data['svgs'] as $key => $shape ) { |
| 289 | $shape_values[ $key ] = $shape['icon']; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | $output .= '<div class="gb-shapes">'; |
| 295 | |
| 296 | foreach ( (array) $settings['shapeDividers'] as $index => $option ) { |
| 297 | if ( ! empty( $option['shape'] ) ) { |
| 298 | if ( isset( $shape_values[ $option['shape'] ] ) ) { |
| 299 | $shapeNumber = $index + 1; |
| 300 | |
| 301 | $output .= sprintf( |
| 302 | '<div class="gb-shape gb-shape-' . $shapeNumber . '">%s</div>', |
| 303 | $shape_values[ $option['shape'] ] |
| 304 | ); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | $output .= '</div>'; |
| 310 | } |
| 311 | |
| 312 | return $output; |
| 313 | } |
| 314 | |
| 315 | add_filter( 'generateblocks_do_content', 'generateblocks_do_widget_styling' ); |
| 316 | /** |
| 317 | * Process all widget content for potential styling. |
| 318 | * |
| 319 | * @since 1.3.4 |
| 320 | * @param string $content The existing content to process. |
| 321 | */ |
| 322 | function generateblocks_do_widget_styling( $content ) { |
| 323 | $widget_blocks = get_option( 'widget_block' ); |
| 324 | |
| 325 | foreach ( (array) $widget_blocks as $block ) { |
| 326 | if ( isset( $block['content'] ) ) { |
| 327 | $content .= $block['content']; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | return $content; |
| 332 | } |
| 333 | |
| 334 | add_filter( 'generateblocks_attr_container', 'generateblocks_set_inline_background_style', 10, 2 ); |
| 335 | /** |
| 336 | * Add our background image attribute to the Container. |
| 337 | * |
| 338 | * @since 1.5.0 |
| 339 | * @param array $attributes Existing attributes. |
| 340 | * @param array $settings Block settings. |
| 341 | */ |
| 342 | function generateblocks_set_inline_background_style( $attributes, $settings ) { |
| 343 | if ( generateblocks_has_background_image( $settings ) && $settings['bgImageInline'] ) { |
| 344 | $url = generateblocks_get_background_image_url( $settings ); |
| 345 | |
| 346 | if ( $url ) { |
| 347 | $attribute_name = 'background-image'; |
| 348 | |
| 349 | if ( 'element' !== $settings['bgOptions']['selector'] ) { |
| 350 | $attribute_name = '--' . $attribute_name; |
| 351 | } |
| 352 | |
| 353 | $attributes['style'] = $attribute_name . ': url(' . esc_url( $url ) . ');'; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return $attributes; |
| 358 | } |
| 359 | |
| 360 | add_filter( 'generateblocks_block_css_selector', 'generateblocks_set_block_css_selectors', 10, 3 ); |
| 361 | /** |
| 362 | * Change our block selectors if needed. |
| 363 | * |
| 364 | * @param string $selector Existing selector. |
| 365 | * @param string $name The block name. |
| 366 | * @param array $attributes The block attributes. |
| 367 | */ |
| 368 | function generateblocks_set_block_css_selectors( $selector, $name, $attributes ) { |
| 369 | $blockVersion = ! empty( $attributes['blockVersion'] ) ? $attributes['blockVersion'] : 1; |
| 370 | $defaults = generateblocks_get_block_defaults(); |
| 371 | |
| 372 | if ( 'button' === $name ) { |
| 373 | $settings = wp_parse_args( |
| 374 | $attributes, |
| 375 | $defaults['button'] |
| 376 | ); |
| 377 | |
| 378 | if ( $blockVersion < 3 ) { |
| 379 | // Old versions of the this block used this backwards logic |
| 380 | // to determine whether to remove the "a" to the selector. |
| 381 | $clean_selector = $selector; |
| 382 | $selector = 'a' . $selector; |
| 383 | |
| 384 | if ( isset( $attributes['hasUrl'] ) && ! $attributes['hasUrl'] ) { |
| 385 | $selector = $clean_selector; |
| 386 | } |
| 387 | } else { |
| 388 | $is_link = ( |
| 389 | ! empty( $settings['hasUrl'] ) || |
| 390 | ! empty( $settings['dynamicLinkType'] ) |
| 391 | ) && 'link' === $settings['buttonType']; |
| 392 | |
| 393 | if ( $is_link ) { |
| 394 | $selector = 'a' . $selector; |
| 395 | } |
| 396 | |
| 397 | if ( 'button' === $settings['buttonType'] ) { |
| 398 | $selector = 'button' . $selector; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if ( $settings['hasButtonContainer'] || $blockVersion < 3 ) { |
| 403 | $selector = '.gb-button-wrapper ' . $selector; |
| 404 | } elseif ( isset( $settings['isPagination'] ) && $settings['isPagination'] ) { |
| 405 | $selector = '.gb-query-loop-pagination ' . $selector; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if ( 'headline' === $name ) { |
| 410 | $settings = wp_parse_args( |
| 411 | $attributes, |
| 412 | $defaults['headline'] |
| 413 | ); |
| 414 | |
| 415 | if ( apply_filters( 'generateblocks_headline_selector_tagname', true, $attributes ) ) { |
| 416 | $selector = $settings['element'] . $selector; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | return $selector; |
| 421 | } |
| 422 | |
| 423 | add_action( 'init', 'generateblocks_register_user_meta' ); |
| 424 | /** |
| 425 | * Register GenerateBlocks custom user meta fields. |
| 426 | * |
| 427 | * @return void |
| 428 | */ |
| 429 | function generateblocks_register_user_meta() { |
| 430 | register_meta( |
| 431 | 'user', |
| 432 | GenerateBlocks_Rest::ONBOARDING_META_KEY, |
| 433 | array( |
| 434 | 'type' => 'object', |
| 435 | 'single' => true, |
| 436 | 'show_in_rest' => array( |
| 437 | 'schema' => array( |
| 438 | 'type' => 'object', |
| 439 | 'properties' => array( |
| 440 | 'insert_inner_container' => array( 'type' => 'boolean' ), |
| 441 | ), |
| 442 | 'additionalProperties' => array( |
| 443 | 'type' => 'boolean', |
| 444 | ), |
| 445 | ), |
| 446 | ), |
| 447 | ) |
| 448 | ); |
| 449 | } |
| 450 | |
| 451 | add_filter( 'block_editor_settings_all', 'generateblocks_do_block_css_reset', 15 ); |
| 452 | /** |
| 453 | * This resets the `max-width`, `margin-left`, and `margin-right` properties for our blocks in the editor. |
| 454 | * We have to do this as most themes use `.wp-block` to set a `max-width` and auto margins. |
| 455 | * |
| 456 | * We used to do this directly in the block CSS if those block attributes didn't exist, but this allows us |
| 457 | * to overwrite the reset in the `block_editor_settings_all` filter with a later priority. |
| 458 | * |
| 459 | * @param array $editor_settings The existing editor settings. |
| 460 | */ |
| 461 | function generateblocks_do_block_css_reset( $editor_settings ) { |
| 462 | $css = '.gb-container, .gb-headline, .gb-button {max-width:unset;margin-left:0;margin-right:0;}'; |
| 463 | $editor_settings['styles'][] = [ 'css' => $css ]; |
| 464 | |
| 465 | return $editor_settings; |
| 466 | } |
| 467 | |
| 468 | add_filter( 'generateblocks_css_output', 'generateblocks_add_general_css' ); |
| 469 | /** |
| 470 | * Add general CSS that doesn't apply to our own blocks. |
| 471 | * |
| 472 | * @param string $css Existing CSS. |
| 473 | */ |
| 474 | function generateblocks_add_general_css( $css ) { |
| 475 | $css .= '.gb-container .wp-block-image img{vertical-align:middle;}'; |
| 476 | $css .= '.gb-grid-wrapper .wp-block-image{margin-bottom:0;}'; |
| 477 | $css .= '.gb-highlight{background:none;}'; |
| 478 | |
| 479 | return $css; |
| 480 | } |
| 481 |