.gitkeep
4 years ago
block-helpers.php
1 day ago
block-style-helpers.php
7 months ago
collection-helpers.php
1 month ago
cookie-helpers.php
1 year ago
kses-helpers.php
6 months ago
product-helpers.php
5 months ago
product-review-helpers.php
6 months ago
route-helper.php
2 years ago
state-helpers.php
2 years ago
template-helpers.php
5 months ago
block-helpers.php
99 lines
| 1 | <?php |
| 2 | if ( ! function_exists( 'sc_do_early_blocks' ) ) { |
| 3 | /** |
| 4 | * Run the early blocks. |
| 5 | * We need to remove the debug notice that is triggered by the `doing_it_wrong` function. |
| 6 | * |
| 7 | * We use this function in page builders that can sometimes render the blocks out of order, causing the debug notice to be displayed. |
| 8 | * |
| 9 | * @param string $content The content. |
| 10 | * |
| 11 | * @return string |
| 12 | */ |
| 13 | function sc_pre_render_blocks( $content ) { |
| 14 | add_filter( 'doing_it_wrong_trigger_error', 'sc_remove_interactivity_debug_notice', 10, 2 ); |
| 15 | |
| 16 | $content = do_blocks( $content ); |
| 17 | |
| 18 | remove_filter( 'doing_it_wrong_trigger_error', 'sc_remove_interactivity_debug_notice', 10 ); |
| 19 | |
| 20 | return $content; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | if ( ! function_exists( 'sc_remove_interactivity_debug_notice' ) ) { |
| 25 | /** |
| 26 | * Remove interactivity doing it wrong. |
| 27 | * |
| 28 | * This is because we need to render blocks out of order in order to |
| 29 | * add the attributes from bricks to the block. |
| 30 | * |
| 31 | * @param string $trigger Trigger. |
| 32 | * @param string $function_name Function name. |
| 33 | * |
| 34 | * @return string|false |
| 35 | */ |
| 36 | function sc_remove_interactivity_debug_notice( $trigger, $function_name ) { |
| 37 | if ( 'WP_Interactivity_API::evaluate' !== $function_name ) { |
| 38 | return $trigger; |
| 39 | } |
| 40 | return false; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if ( ! function_exists( 'sc_has_interactive_content' ) ) { |
| 45 | /** |
| 46 | * Does this markup contain an element that cannot legally sit inside a link? |
| 47 | * |
| 48 | * Narrower than "is anything clickable": a role="button" div nests inside an anchor |
| 49 | * fine and cancels its own click, so only real controls force the stretched overlay. |
| 50 | * |
| 51 | * @param string $html The rendered markup to inspect. |
| 52 | * |
| 53 | * @return bool |
| 54 | */ |
| 55 | function sc_has_interactive_content( $html ) { |
| 56 | if ( empty( $html ) ) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | // `label` and `details` count because both act on a click without being form controls. |
| 61 | $interactive_tags = [ 'A', 'BUTTON', 'SELECT', 'INPUT', 'TEXTAREA', 'DETAILS', 'LABEL' ]; |
| 62 | $tags = new \WP_HTML_Tag_Processor( $html ); |
| 63 | |
| 64 | while ( $tags->next_tag() ) { |
| 65 | if ( in_array( $tags->get_tag(), $interactive_tags, true ) ) { |
| 66 | return true; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Truncated markup stops the scan before the end of the string. Answer yes rather |
| 71 | // than let a caller wrap controls we simply never reached in a link. |
| 72 | return $tags->paused_at_incomplete_token(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if ( ! function_exists( 'sc_variant_pills_overflow_count' ) ) { |
| 77 | /** |
| 78 | * How many variant option pills should hide behind the "+N more" toggle? |
| 79 | * |
| 80 | * Deliberately literal — a cap of 3 shows 3 pills and "+1 more" even though |
| 81 | * the toggle takes the hidden pill's slot, because a setting that sometimes |
| 82 | * shows more than it says reads as broken. 0 means no cap. |
| 83 | * |
| 84 | * @param int $total Total number of option values. |
| 85 | * @param int $max_visible Maximum pills to show before capping (0 = no cap). |
| 86 | * |
| 87 | * @return int Number of pills to hide. 0 when everything should show. |
| 88 | */ |
| 89 | function sc_variant_pills_overflow_count( $total, $max_visible ) { |
| 90 | $max_visible = max( 0, (int) $max_visible ); |
| 91 | |
| 92 | if ( $max_visible < 1 ) { |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | return max( 0, (int) $total - $max_visible ); |
| 97 | } |
| 98 | } |
| 99 |