blocks
3 years ago
class-do-css.php
3 years ago
class-dynamic-content.php
3 years ago
class-enqueue-css.php
3 years ago
class-legacy-attributes.php
4 years ago
class-plugin-update.php
5 years ago
class-query-loop.php
3 years ago
class-render-blocks.php
3 years ago
class-rest.php
3 years ago
class-settings.php
4 years ago
dashboard.php
3 years ago
defaults.php
3 years ago
functions.php
3 years ago
general.php
3 years ago
general.php
425 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 ), // phpcs:ignore -- Core filter. |
| 83 | ) |
| 84 | ); |
| 85 | |
| 86 | if ( function_exists( 'generate_get_color_defaults' ) ) { |
| 87 | $color_settings = wp_parse_args( |
| 88 | get_option( 'generate_settings', array() ), |
| 89 | generate_get_color_defaults() |
| 90 | ); |
| 91 | |
| 92 | $generatepressDefaultStyling = apply_filters( |
| 93 | 'generateblocks_gp_default_styling', |
| 94 | array( |
| 95 | 'buttonBackground' => $color_settings['form_button_background_color'], |
| 96 | 'buttonBackgroundHover' => $color_settings['form_button_background_color_hover'], |
| 97 | 'buttonText' => $color_settings['form_button_text_color'], |
| 98 | 'buttonTextHover' => $color_settings['form_button_text_color_hover'], |
| 99 | 'buttonPaddingTop' => '10px', |
| 100 | 'buttonPaddingRight' => '20px', |
| 101 | 'buttonPaddingBottom' => '10px', |
| 102 | 'buttonPaddingLeft' => '20px', |
| 103 | ) |
| 104 | ); |
| 105 | |
| 106 | $css = sprintf( |
| 107 | '.gb-button.button { |
| 108 | background-color: %1$s; |
| 109 | color: %2$s; |
| 110 | padding-top: %3$s; |
| 111 | padding-right: %4$s; |
| 112 | padding-bottom: %5$s; |
| 113 | padding-left: %6$s; |
| 114 | }', |
| 115 | $generatepressDefaultStyling['buttonBackground'], |
| 116 | $generatepressDefaultStyling['buttonText'], |
| 117 | $generatepressDefaultStyling['buttonPaddingTop'], |
| 118 | $generatepressDefaultStyling['buttonPaddingRight'], |
| 119 | $generatepressDefaultStyling['buttonPaddingBottom'], |
| 120 | $generatepressDefaultStyling['buttonPaddingLeft'] |
| 121 | ); |
| 122 | |
| 123 | $css .= sprintf( |
| 124 | '.gb-button.button:active, .gb-button.button:hover, .gb-button.button:focus { |
| 125 | background-color: %1$s; |
| 126 | color: %2$s; |
| 127 | }', |
| 128 | $generatepressDefaultStyling['buttonBackgroundHover'], |
| 129 | $generatepressDefaultStyling['buttonTextHover'] |
| 130 | ); |
| 131 | |
| 132 | wp_add_inline_style( 'generateblocks', $css ); |
| 133 | } |
| 134 | |
| 135 | $defaults = generateblocks_get_block_defaults(); |
| 136 | |
| 137 | wp_localize_script( |
| 138 | 'generateblocks', |
| 139 | 'generateBlocksDefaults', |
| 140 | $defaults |
| 141 | ); |
| 142 | |
| 143 | wp_localize_script( |
| 144 | 'generateblocks', |
| 145 | 'generateBlocksStyling', |
| 146 | generateblocks_get_default_styles() |
| 147 | ); |
| 148 | |
| 149 | wp_localize_script( |
| 150 | 'generateblocks', |
| 151 | 'generateBlocksLegacyDefaults', |
| 152 | array( |
| 153 | 'v_1_4_0' => GenerateBlocks_Legacy_Attributes::get_defaults( '1.4.0' ), |
| 154 | ) |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | if ( version_compare( $GLOBALS['wp_version'], '5.8-alpha-1', '<' ) ) { |
| 159 | add_filter( 'block_categories', 'generateblocks_do_category' ); |
| 160 | } else { |
| 161 | add_filter( 'block_categories_all', 'generateblocks_do_category' ); |
| 162 | } |
| 163 | /** |
| 164 | * Add GeneratePress category to Gutenberg. |
| 165 | * |
| 166 | * @param array $categories Existing categories. |
| 167 | * @since 0.1 |
| 168 | */ |
| 169 | function generateblocks_do_category( $categories ) { |
| 170 | return array_merge( |
| 171 | array( |
| 172 | array( |
| 173 | 'slug' => 'generateblocks', |
| 174 | 'title' => __( 'GenerateBlocks', 'generateblocks' ), |
| 175 | ), |
| 176 | ), |
| 177 | $categories |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | add_action( 'wp_enqueue_scripts', 'generateblocks_do_google_fonts' ); |
| 182 | add_action( 'enqueue_block_editor_assets', 'generateblocks_do_google_fonts' ); |
| 183 | /** |
| 184 | * Do Google Fonts. |
| 185 | * |
| 186 | * @since 0.1 |
| 187 | */ |
| 188 | function generateblocks_do_google_fonts() { |
| 189 | $fonts_url = generateblocks_get_google_fonts_uri(); |
| 190 | |
| 191 | if ( $fonts_url ) { |
| 192 | wp_enqueue_style( 'generateblocks-google-fonts', $fonts_url, array(), null, 'all' ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | add_filter( 'generateblocks_css_print_method', 'generateblocks_set_css_print_method' ); |
| 197 | /** |
| 198 | * Set our CSS print method. |
| 199 | * |
| 200 | * @param string $method Existing method. |
| 201 | */ |
| 202 | function generateblocks_set_css_print_method( $method ) { |
| 203 | $method = generateblocks_get_option( 'css_print_method' ); |
| 204 | |
| 205 | if ( is_single() ) { |
| 206 | $method = 'inline'; |
| 207 | } |
| 208 | |
| 209 | return $method; |
| 210 | } |
| 211 | |
| 212 | add_filter( 'excerpt_allowed_blocks', 'generateblocks_set_excerpt_allowed_blocks' ); |
| 213 | /** |
| 214 | * Add blocks that can be displayed in post excerpts. |
| 215 | * |
| 216 | * @param array $allowed Existing allowed blocks. |
| 217 | * @since 1.0 |
| 218 | */ |
| 219 | function generateblocks_set_excerpt_allowed_blocks( $allowed ) { |
| 220 | $allowed[] = 'generateblocks/headline'; |
| 221 | $allowed[] = 'generateblocks/container'; |
| 222 | |
| 223 | return $allowed; |
| 224 | } |
| 225 | |
| 226 | add_filter( 'excerpt_allowed_wrapper_blocks', 'generateblocks_set_excerpt_allowed_wrapper_blocks' ); |
| 227 | /** |
| 228 | * Allows excerpts to be generated from the `innerBlocks` of these wrappers. |
| 229 | * |
| 230 | * @param array $allowed Existing allowed wrapper blocks. |
| 231 | * @since 1.5.0 |
| 232 | */ |
| 233 | function generateblocks_set_excerpt_allowed_wrapper_blocks( $allowed ) { |
| 234 | $allowed[] = 'generateblocks/container'; |
| 235 | |
| 236 | return $allowed; |
| 237 | } |
| 238 | |
| 239 | add_filter( 'generateblocks_before_container_close', 'generateblocks_do_shape_divider', 10, 2 ); |
| 240 | /** |
| 241 | * Add shape divider to Container. |
| 242 | * |
| 243 | * @since 1.2.0 |
| 244 | * @param string $output The current block output. |
| 245 | * @param array $attributes The current block attributes. |
| 246 | */ |
| 247 | function generateblocks_do_shape_divider( $output, $attributes ) { |
| 248 | $defaults = generateblocks_get_block_defaults(); |
| 249 | |
| 250 | $settings = wp_parse_args( |
| 251 | $attributes, |
| 252 | $defaults['container'] |
| 253 | ); |
| 254 | |
| 255 | if ( ! empty( $settings['shapeDividers'] ) ) { |
| 256 | $shapes = generateblocks_get_svg_shapes(); |
| 257 | $shape_values = array(); |
| 258 | |
| 259 | foreach ( $shapes as $group => $data ) { |
| 260 | if ( ! empty( $data['svgs'] ) && is_array( $data['svgs'] ) ) { |
| 261 | foreach ( $data['svgs'] as $key => $shape ) { |
| 262 | $shape_values[ $key ] = $shape['icon']; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | $output .= '<div class="gb-shapes">'; |
| 268 | |
| 269 | foreach ( (array) $settings['shapeDividers'] as $index => $option ) { |
| 270 | if ( ! empty( $option['shape'] ) ) { |
| 271 | if ( isset( $shape_values[ $option['shape'] ] ) ) { |
| 272 | $shapeNumber = $index + 1; |
| 273 | |
| 274 | $output .= sprintf( |
| 275 | '<div class="gb-shape gb-shape-' . $shapeNumber . '">%s</div>', |
| 276 | $shape_values[ $option['shape'] ] |
| 277 | ); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | $output .= '</div>'; |
| 283 | } |
| 284 | |
| 285 | return $output; |
| 286 | } |
| 287 | |
| 288 | add_filter( 'generateblocks_do_content', 'generateblocks_do_widget_styling' ); |
| 289 | /** |
| 290 | * Process all widget content for potential styling. |
| 291 | * |
| 292 | * @since 1.3.4 |
| 293 | * @param string $content The existing content to process. |
| 294 | */ |
| 295 | function generateblocks_do_widget_styling( $content ) { |
| 296 | $widget_blocks = get_option( 'widget_block' ); |
| 297 | |
| 298 | foreach ( (array) $widget_blocks as $block ) { |
| 299 | if ( isset( $block['content'] ) ) { |
| 300 | $content .= $block['content']; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return $content; |
| 305 | } |
| 306 | |
| 307 | add_filter( 'generateblocks_attr_container', 'generateblocks_set_inline_background_style', 10, 2 ); |
| 308 | /** |
| 309 | * Add our background image attribute to the Container. |
| 310 | * |
| 311 | * @since 1.5.0 |
| 312 | * @param array $attributes Existing attributes. |
| 313 | * @param array $settings Block settings. |
| 314 | */ |
| 315 | function generateblocks_set_inline_background_style( $attributes, $settings ) { |
| 316 | if ( generateblocks_has_background_image( $settings ) && $settings['bgImageInline'] ) { |
| 317 | $url = generateblocks_get_background_image_url( $settings ); |
| 318 | |
| 319 | if ( $url ) { |
| 320 | $attribute_name = 'background-image'; |
| 321 | |
| 322 | if ( 'element' !== $settings['bgOptions']['selector'] ) { |
| 323 | $attribute_name = '--' . $attribute_name; |
| 324 | } |
| 325 | |
| 326 | $attributes['style'] = $attribute_name . ': url(' . esc_url( $url ) . ');'; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return $attributes; |
| 331 | } |
| 332 | |
| 333 | add_filter( 'generateblocks_block_css_selector', 'generateblocks_set_block_css_selectors', 10, 3 ); |
| 334 | /** |
| 335 | * Change our block selectors if needed. |
| 336 | * |
| 337 | * @param string $selector Existing selector. |
| 338 | * @param string $name The block name. |
| 339 | * @param array $attributes The block attributes. |
| 340 | */ |
| 341 | function generateblocks_set_block_css_selectors( $selector, $name, $attributes ) { |
| 342 | $blockVersion = ! empty( $attributes['blockVersion'] ) ? $attributes['blockVersion'] : 1; |
| 343 | $defaults = generateblocks_get_block_defaults(); |
| 344 | |
| 345 | if ( 'button' === $name ) { |
| 346 | $settings = wp_parse_args( |
| 347 | $attributes, |
| 348 | $defaults['button'] |
| 349 | ); |
| 350 | |
| 351 | if ( $blockVersion < 3 ) { |
| 352 | // Old versions of the this block used this backwards logic |
| 353 | // to determine whether to remove the "a" to the selector. |
| 354 | $clean_selector = $selector; |
| 355 | $selector = 'a' . $selector; |
| 356 | |
| 357 | if ( isset( $attributes['hasUrl'] ) && ! $attributes['hasUrl'] ) { |
| 358 | $selector = $clean_selector; |
| 359 | } |
| 360 | } else { |
| 361 | $is_link = ( |
| 362 | ! empty( $settings['hasUrl'] ) || |
| 363 | ! empty( $settings['dynamicLinkType'] ) |
| 364 | ) && 'link' === $settings['buttonType']; |
| 365 | |
| 366 | if ( $is_link ) { |
| 367 | $selector = 'a' . $selector; |
| 368 | } |
| 369 | |
| 370 | if ( 'button' === $settings['buttonType'] ) { |
| 371 | $selector = 'button' . $selector; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | if ( $settings['hasButtonContainer'] || $blockVersion < 3 ) { |
| 376 | $selector = '.gb-button-wrapper ' . $selector; |
| 377 | } elseif ( isset( $settings['isPagination'] ) && $settings['isPagination'] ) { |
| 378 | $selector = '.gb-query-loop-pagination ' . $selector; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if ( 'headline' === $name ) { |
| 383 | $settings = wp_parse_args( |
| 384 | $attributes, |
| 385 | $defaults['headline'] |
| 386 | ); |
| 387 | |
| 388 | if ( apply_filters( 'generateblocks_headline_selector_tagname', true, $attributes ) ) { |
| 389 | $selector = $settings['element'] . $selector; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | return $selector; |
| 394 | } |
| 395 | |
| 396 | add_action( 'init', 'generateblocks_register_user_meta' ); |
| 397 | /** |
| 398 | * Register GenerateBlocks custom user meta fields. |
| 399 | * |
| 400 | * @return void |
| 401 | */ |
| 402 | function generateblocks_register_user_meta() { |
| 403 | $onboarding_properties = apply_filters( |
| 404 | 'generateblocks_onboarding_user_meta_properties', |
| 405 | array( |
| 406 | 'insert_inner_container' => array( 'type' => 'boolean' ), |
| 407 | ) |
| 408 | ); |
| 409 | |
| 410 | register_meta( |
| 411 | 'user', |
| 412 | GenerateBlocks_Rest::ONBOARDING_META_KEY, |
| 413 | array( |
| 414 | 'type' => 'object', |
| 415 | 'single' => true, |
| 416 | 'show_in_rest' => array( |
| 417 | 'schema' => array( |
| 418 | 'type' => 'object', |
| 419 | 'properties' => $onboarding_properties, |
| 420 | ), |
| 421 | ), |
| 422 | ) |
| 423 | ); |
| 424 | } |
| 425 |