← All changes
|
modules/gutenberg/classes/class-spec-gb-helper.php
+109
-17
0.0.12
→
2.12.8
View file →
| @@ -113,8 +113,26 @@ | ||
| 113 | 113 | */ |
| 114 | 114 | public static $icon_array_merged = []; |
| 115 | 115 | |
| 116 | 116 | /** |
| 117 | + * Seen Refs Array | |
| 118 | + * This array will store the block IDs which have already been processed. | |
| 119 | + * | |
| 120 | + * @since 1.4.2 | |
| 121 | + * @var array | |
| 122 | + */ | |
| 123 | + private static $seen_refs = []; | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * Processed form IDs Array | |
| 127 | + * This array will store the SRFM form IDs which have already been processed to prevent duplicate styles. | |
| 128 | + * | |
| 129 | + * @since 1.11.0 | |
| 130 | + * @var array | |
| 131 | + */ | |
| 132 | + private static $processed_srfm_ids = []; | |
| 133 | + | |
| 134 | + /** | |
| 117 | 135 | * Initiator |
| 118 | 136 | * |
| 119 | 137 | * @since 0.0.1 |
| 120 | 138 | */ |
| @@ -132,16 +150,38 @@ | ||
| 132 | 150 | require SRFM_DIR . 'modules/gutenberg/classes/class-spec-block-config.php'; |
| 133 | 151 | require SRFM_DIR . 'modules/gutenberg/classes/class-spec-block-helper.php'; |
| 134 | 152 | require SRFM_DIR . 'modules/gutenberg/classes/class-spec-block-js.php'; |
| 135 | 153 | |
| 136 | - self::$block_list = Spec_Block_Config::get_block_attributes(); | |
| 154 | + /** | |
| 155 | + * Introduce this action to execute Spec_Block_Config::get_block_attributes(). | |
| 156 | + * This is necessary because translation functions are called within it. | |
| 157 | + * WordPress 6.7 introduced warnings when hook execution order is incorrect, | |
| 158 | + * which can lead to notices. This action ensures proper hook sequence. | |
| 159 | + */ | |
| 160 | + add_action( 'init', [ $this, 'init_block_attributes' ] ); | |
| 137 | 161 | |
| 138 | - add_action( 'wp', [ $this, 'wp_actions' ], 10 ); | |
| 162 | + $theme_name = wp_get_theme()->get( 'Name' ); | |
| 139 | 163 | |
| 164 | + if ( 'Divi' === $theme_name ) { | |
| 165 | + add_action( 'et_after_main_content', [ $this, 'wp_actions' ], 10 ); | |
| 166 | + } else { | |
| 167 | + add_action( 'wp', [ $this, 'wp_actions' ], 10 ); | |
| 168 | + } | |
| 169 | + | |
| 140 | 170 | add_filter( 'render_block', [ $this, 'generate_render_styles' ], 10, 2 ); |
| 141 | 171 | } |
| 142 | 172 | |
| 143 | 173 | /** |
| 174 | + * Initialize block attributes. | |
| 175 | + * | |
| 176 | + * @since 1.0.6 | |
| 177 | + * @return void | |
| 178 | + */ | |
| 179 | + public static function init_block_attributes() { | |
| 180 | + self::$block_list = Spec_Block_Config::get_block_attributes(); | |
| 181 | + } | |
| 182 | + | |
| 183 | + /** | |
| 144 | 184 | * Get form id content. |
| 145 | 185 | * |
| 146 | 186 | * @param string $id form id. |
| 147 | 187 | * @since 0.0.1 |
| @@ -349,8 +389,60 @@ | ||
| 349 | 389 | |
| 350 | 390 | } |
| 351 | 391 | |
| 352 | 392 | /** |
| 393 | + * | |
| 394 | + * Recursively collects all SRFM form blocks from a given block structure. | |
| 395 | + * | |
| 396 | + * This method traverses the provided block array (including any nested inner blocks), | |
| 397 | + * finds `srfm/form` blocks, retrieves the corresponding post content for those forms, | |
| 398 | + * parses it into block data, and merges the results into the return array. | |
| 399 | + * | |
| 400 | + * @param array $blocks The current block data (including attributes and inner blocks). | |
| 401 | + * @param array $blocks_to_return The array of collected SRFM form blocks to return. | |
| 402 | + * | |
| 403 | + * @return array Collected SRFM form blocks. | |
| 404 | + * @since 1.11.0 | |
| 405 | + */ | |
| 406 | + public function get_srfm_form_blocks( $blocks, $blocks_to_return = [] ) { | |
| 407 | + // Ensure $blocks is an array and has required structure. | |
| 408 | + if ( ! is_array( $blocks ) || empty( $blocks ) ) { | |
| 409 | + return $blocks_to_return; | |
| 410 | + } | |
| 411 | + | |
| 412 | + // Prevent duplicate processing by checking ID early. | |
| 413 | + if ( isset( $blocks['attrs']['id'] ) && in_array( $blocks['attrs']['id'], self::$processed_srfm_ids, true ) ) { | |
| 414 | + return $blocks_to_return; | |
| 415 | + } | |
| 416 | + | |
| 417 | + // Check for SRFM form block. | |
| 418 | + if ( isset( $blocks['blockName'] ) && 'srfm/form' === $blocks['blockName'] ) { | |
| 419 | + if ( ! empty( $blocks['attrs']['id'] ) ) { | |
| 420 | + $form_id = $blocks['attrs']['id']; | |
| 421 | + self::$processed_srfm_ids[] = $form_id; | |
| 422 | + | |
| 423 | + $get_block_from_the_form = get_post( $form_id ); | |
| 424 | + if ( $get_block_from_the_form && ! empty( $get_block_from_the_form->post_content ) ) { | |
| 425 | + $parsed_blocks = $this->parse( $get_block_from_the_form->post_content ); | |
| 426 | + if ( is_array( $parsed_blocks ) ) { | |
| 427 | + $blocks_to_return = array_merge( $blocks_to_return, $parsed_blocks ); | |
| 428 | + } | |
| 429 | + } | |
| 430 | + } | |
| 431 | + } | |
| 432 | + | |
| 433 | + // Process inner blocks if any. | |
| 434 | + if ( ! empty( $blocks['innerBlocks'] ) && is_array( $blocks['innerBlocks'] ) ) { | |
| 435 | + foreach ( $blocks['innerBlocks'] as $inner_block ) { | |
| 436 | + $blocks_to_return = $this->get_srfm_form_blocks( $inner_block, $blocks_to_return ); | |
| 437 | + } | |
| 438 | + } | |
| 439 | + | |
| 440 | + return $blocks_to_return; | |
| 441 | + } | |
| 442 | + | |
| 443 | + | |
| 444 | + /** | |
| 353 | 445 | * Generates stylesheet in loop. |
| 354 | 446 | * |
| 355 | 447 | * @param object $this_post Current Post Object. |
| 356 | 448 | * @since 0.0.1 |
| @@ -362,14 +454,13 @@ | ||
| 362 | 454 | $blocks = $this->parse( $this_post->post_content ); |
| 363 | 455 | |
| 364 | 456 | $count = count( $blocks ); |
| 365 | 457 | for ( $i = 0; $i < $count; $i++ ) { |
| 366 | - if ( isset( $blocks[ $i ]['blockName'] ) && 'srfm/form' === $blocks[ $i ]['blockName'] ) { | |
| 367 | - if ( isset( $blocks[ $i ]['attrs']['id'] ) && $blocks[ $i ]['attrs']['id'] ) { | |
| 368 | - $form_post = get_post( $blocks[ $i ]['attrs']['id'] ); | |
| 369 | - if ( $form_post ) { | |
| 370 | - $blocks = array_merge( $blocks, $this->parse( $form_post->post_content ) ); | |
| 371 | - } | |
| 458 | + | |
| 459 | + if ( isset( $blocks[ $i ]['blockName'] ) && ! empty( $blocks[ $i ]['blockName'] ) ) { | |
| 460 | + $get_form_blocks = $this->get_srfm_form_blocks( $blocks[ $i ] ); | |
| 461 | + if ( ! empty( $get_form_blocks ) ) { | |
| 462 | + $blocks = array_merge( $blocks, $get_form_blocks ); | |
| 372 | 463 | } |
| 373 | 464 | } |
| 374 | 465 | |
| 375 | 466 | if ( has_shortcode( $this_post->post_content, 'sureforms' ) && isset( $blocks[ $i ]['blockName'] ) && 'core/shortcode' === $blocks[ $i ]['blockName'] ) { |
| @@ -467,11 +558,14 @@ | ||
| 467 | 558 | } |
| 468 | 559 | if ( 'core/block' === $block['blockName'] ) { |
| 469 | 560 | $id = ( isset( $block['attrs']['ref'] ) ) ? $block['attrs']['ref'] : 0; |
| 470 | 561 | |
| 471 | - if ( $id ) { | |
| 472 | - $content = get_post_field( 'post_content', $id ); | |
| 562 | + $is_block_seen = in_array( $id, self::$seen_refs, true ); | |
| 473 | 563 | |
| 564 | + if ( $id && ! $is_block_seen ) { | |
| 565 | + self::$seen_refs[] = $id; | |
| 566 | + $content = get_post_field( 'post_content', $id ); | |
| 567 | + | |
| 474 | 568 | $reusable_blocks = $this->parse( $content ); |
| 475 | 569 | |
| 476 | 570 | $assets = $this->get_assets( $reusable_blocks ); |
| 477 | 571 | |
| @@ -797,20 +891,18 @@ | ||
| 797 | 891 | public function get_block_css_and_js( $block ) { |
| 798 | 892 | |
| 799 | 893 | $block = (array) $block; |
| 800 | 894 | |
| 801 | - $name = $block['blockName']; | |
| 895 | + $name = isset( $block['blockName'] ) ? $block['blockName'] : ''; | |
| 802 | 896 | $css = []; |
| 803 | 897 | $js = ''; |
| 804 | 898 | $block_id = ''; |
| 805 | 899 | |
| 806 | - if ( isset( $name ) ) { | |
| 900 | + if ( ! empty( $name ) && isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) { | |
| 807 | 901 | |
| 808 | - if ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) { | |
| 809 | - $blockattr = $block['attrs']; | |
| 810 | - if ( isset( $blockattr['block_id'] ) ) { | |
| 811 | - $block_id = $blockattr['block_id']; | |
| 812 | - } | |
| 902 | + $blockattr = $block['attrs']; | |
| 903 | + if ( isset( $blockattr['block_id'] ) ) { | |
| 904 | + $block_id = $blockattr['block_id']; | |
| 813 | 905 | } |
| 814 | 906 | |
| 815 | 907 | self::$current_block_list[] = $name; |
| 816 | 908 | |