Blocks
2 weeks ago
Assets.php
2 weeks ago
Configuration.php
2 weeks ago
Configuration_Interface.php
2 weeks ago
Meta.php
2 weeks ago
Meta_Interface.php
2 weeks ago
Provider.php
2 weeks ago
Utils.php
2 weeks ago
Utils.php
143 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Events Gutenberg Utils |
| 5 | * |
| 6 | * @since 4.8 |
| 7 | */ |
| 8 | class Tribe__Editor__Utils { |
| 9 | |
| 10 | /** |
| 11 | * Adds the required prefix of a tribe block with the wp: prefix as well and escaped. |
| 12 | * |
| 13 | * @since 4.8 |
| 14 | * |
| 15 | * @param string $name |
| 16 | * |
| 17 | * @return string |
| 18 | */ |
| 19 | public function to_tribe_block_name( $name = '' ) { |
| 20 | return 'wp:tribe\/' . $name; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Remove all invalid characters in string that are used to set the name of a block |
| 25 | * |
| 26 | * @since 4.8 |
| 27 | * |
| 28 | * @param string $name |
| 29 | * |
| 30 | * @return string |
| 31 | */ |
| 32 | public function to_block_name( $name = '' ) { |
| 33 | return preg_replace( '/[^a-zA-Z0-9-]/', '', $name ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Replaces the content of a post where a block is located, removes the space before and after on the same line where |
| 38 | * the block is located, it replaces the content of the block with an empty string |
| 39 | * |
| 40 | * @since 4.8 |
| 41 | * |
| 42 | * @param $post_id |
| 43 | * @param string $block_name |
| 44 | * @param string $replacement |
| 45 | * |
| 46 | * @return bool |
| 47 | */ |
| 48 | public function remove_block( $post_id, $block_name = '', $replacement = '' ) { |
| 49 | $patttern = '/^\s*<!-- ' . $block_name . '.*\/-->\s*$/im'; |
| 50 | return $this->update_post_content( $post_id, $patttern, $replacement ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Function used to remove the inner blocks and the parent block as well inside of a post_content |
| 55 | * |
| 56 | * @since 4.8.2 |
| 57 | * |
| 58 | * @param $post_id |
| 59 | * @param $block_name The name of the block |
| 60 | * @param string $replacement The string used to replace the value of the searched block |
| 61 | * |
| 62 | * @return bool |
| 63 | */ |
| 64 | public function remove_inner_blocks( $post_id, $block_name, $replacement = '' ) { |
| 65 | $pattern = '/^\s*<!-- ' . $block_name . '.*-->\s.*<!-- \/' . $block_name . ' -->/ims'; |
| 66 | return $this->update_post_content( $post_id, $pattern, $replacement ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Update the content of a post using a pattern to search a specifc string, with a custom |
| 71 | * replacement |
| 72 | * |
| 73 | * @since 4.8.2 |
| 74 | * |
| 75 | * @param $post_id |
| 76 | * @param $pattern |
| 77 | * @param string $replacement The string used to replace the value of the searched block |
| 78 | * |
| 79 | * @return bool |
| 80 | */ |
| 81 | public function update_post_content( $post_id, $pattern, $replacement = '' ) { |
| 82 | $content = get_post_field( 'post_content', $post_id ); |
| 83 | |
| 84 | if ( empty( $content ) ) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | $next_content = preg_replace( $pattern, $replacement, $content ); |
| 89 | |
| 90 | /** |
| 91 | * Don't update post content if preg_replace fails or content is the update_content |
| 92 | * is same as current content on the post to avoid a DB operation. |
| 93 | */ |
| 94 | if ( $next_content === null || $next_content === $content ) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | return wp_update_post( [ |
| 99 | 'ID' => $post_id, |
| 100 | 'post_content' => $next_content, |
| 101 | ] ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Strip the dynamic blocks of the content |
| 106 | * |
| 107 | * @since 4.8.5 |
| 108 | * |
| 109 | * @param string $content The event content |
| 110 | * |
| 111 | * @return string |
| 112 | */ |
| 113 | public function strip_dynamic_blocks( $content = '' ) { |
| 114 | |
| 115 | if ( ! function_exists( 'strip_dynamic_blocks' ) ) { |
| 116 | return $content; |
| 117 | } |
| 118 | |
| 119 | return strip_dynamic_blocks( $content ); |
| 120 | |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Return the content without the tribe blocks |
| 125 | * |
| 126 | * @since 4.8.5 |
| 127 | * |
| 128 | * @param string $content The event content |
| 129 | * |
| 130 | * @return string |
| 131 | */ |
| 132 | public function exclude_tribe_blocks( $content = '' ) { |
| 133 | |
| 134 | $match_blocks_exp = '/\<\!\-\- \/?wp\:tribe.*\/?-->/i'; |
| 135 | |
| 136 | if ( ! preg_match( $match_blocks_exp, $content ) ) { |
| 137 | return $content; |
| 138 | } |
| 139 | |
| 140 | return preg_replace( $match_blocks_exp, '', $content ); |
| 141 | } |
| 142 | } |
| 143 |