← All changes
|
includes/blocks/helpers/class-convertkit-shortcode-post-helper.php
+17
-21
3.4.0
→
3.4.6
View file →
| @@ -36,9 +36,9 @@ | ||
| 36 | 36 | * @since 3.4.0 |
| 37 | 37 | * |
| 38 | 38 | * @param int $post_id Post ID. |
| 39 | 39 | * @param string $shortcode_tag Programmatic Shortcode Tag. |
| 40 | - * @return WP_Error|bool|array | |
| 40 | + * @return WP_Error|array | |
| 41 | 41 | */ |
| 42 | 42 | public static function find( $post_id, $shortcode_tag ) { |
| 43 | 43 | |
| 44 | 44 | // Get Post. |
| @@ -63,13 +63,8 @@ | ||
| 63 | 63 | 'attrs' => self::parse_attrs( $match ), |
| 64 | 64 | ); |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - // If no shortcodes found, return false. | |
| 68 | - if ( empty( $found ) ) { | |
| 69 | - return false; | |
| 70 | - } | |
| 71 | - | |
| 72 | 67 | return $found; |
| 73 | 68 | |
| 74 | 69 | } |
| 75 | 70 | |
| @@ -156,13 +151,13 @@ | ||
| 156 | 151 | // All other content is left byte-for-byte unchanged. |
| 157 | 152 | $snippet = self::pad_snippet( $shortcode, $content, $insert_at ); |
| 158 | 153 | $content = substr_replace( $content, $snippet, $insert_at, 0 ); |
| 159 | 154 | |
| 160 | - // Update Post. | |
| 155 | + // Update Post, slashing the content as wp_update_post() unslashes it. | |
| 161 | 156 | $result = wp_update_post( |
| 162 | 157 | array( |
| 163 | 158 | 'ID' => $post_id, |
| 164 | - 'post_content' => $content, | |
| 159 | + 'post_content' => wp_slash( $content ), | |
| 165 | 160 | ), |
| 166 | 161 | true |
| 167 | 162 | ); |
| 168 | 163 | |
| @@ -226,13 +221,13 @@ | ||
| 226 | 221 | |
| 227 | 222 | // Replace the matched shortcode text with the rebuilt shortcode. |
| 228 | 223 | $content = self::replace_match( $post->post_content, $match, $replacement ); |
| 229 | 224 | |
| 230 | - // Update Post. | |
| 225 | + // Update Post, slashing the content as wp_update_post() unslashes it. | |
| 231 | 226 | $result = wp_update_post( |
| 232 | 227 | array( |
| 233 | 228 | 'ID' => $post_id, |
| 234 | - 'post_content' => $content, | |
| 229 | + 'post_content' => wp_slash( $content ), | |
| 235 | 230 | ), |
| 236 | 231 | true |
| 237 | 232 | ); |
| 238 | 233 | |
| @@ -290,13 +285,13 @@ | ||
| 290 | 285 | |
| 291 | 286 | // Remove the matched shortcode text from the content. |
| 292 | 287 | $content = self::replace_match( $post->post_content, $matches[ (int) $occurrence_index ], '' ); |
| 293 | 288 | |
| 294 | - // Update Post. | |
| 289 | + // Update Post, slashing the content as wp_update_post() unslashes it. | |
| 295 | 290 | $result = wp_update_post( |
| 296 | 291 | array( |
| 297 | 292 | 'ID' => $post_id, |
| 298 | - 'post_content' => $content, | |
| 293 | + 'post_content' => wp_slash( $content ), | |
| 299 | 294 | ), |
| 300 | 295 | true |
| 301 | 296 | ); |
| 302 | 297 | |
| @@ -477,16 +472,15 @@ | ||
| 477 | 472 | if ( trim( (string) $content ) === '' ) { |
| 478 | 473 | return array(); |
| 479 | 474 | } |
| 480 | 475 | |
| 481 | - // Candidate offsets, one per regex-matched element-level opener. | |
| 482 | - $pattern = '/<(' . self::ELEMENT_LEVEL_TAGS . ')\b[^>]*>.*?<\/\1>/is'; | |
| 483 | - if ( ! preg_match_all( $pattern, $content, $matches, PREG_OFFSET_CAPTURE ) ) { | |
| 484 | - return array(); | |
| 485 | - } | |
| 486 | - | |
| 487 | 476 | // Fallback for WP < 6.2: regex offsets verbatim, no nesting awareness. |
| 488 | 477 | if ( ! class_exists( 'WP_HTML_Tag_Processor' ) ) { |
| 478 | + $pattern = '/<(' . self::ELEMENT_LEVEL_TAGS . ')\b[^>]*>.*?<\/\1>/is'; | |
| 479 | + if ( ! preg_match_all( $pattern, $content, $matches, PREG_OFFSET_CAPTURE ) ) { | |
| 480 | + return array(); | |
| 481 | + } | |
| 482 | + | |
| 489 | 483 | $starts = array(); |
| 490 | 484 | foreach ( $matches[0] as $match ) { |
| 491 | 485 | $starts[] = (int) $match[1]; |
| 492 | 486 | } |
| @@ -492,9 +486,10 @@ | ||
| 492 | 486 | } |
| 493 | 487 | return $starts; |
| 494 | 488 | } |
| 495 | 489 | |
| 496 | - // Per-tag queue of regex offsets in document order. | |
| 490 | + // Per-tag queue of opening tag offsets in document order, including nested and void tags such as <hr>. | |
| 491 | + preg_match_all( '/<(' . self::ELEMENT_LEVEL_TAGS . ')\b[^>]*>/i', $content, $matches, PREG_OFFSET_CAPTURE ); | |
| 497 | 492 | $queues = array(); |
| 498 | 493 | foreach ( $matches[1] as $i => $tag_match ) { |
| 499 | 494 | $queues[ strtoupper( $tag_match[0] ) ][] = (int) $matches[0][ $i ][1]; |
| 500 | 495 | } |
| @@ -518,11 +513,12 @@ | ||
| 518 | 513 | } |
| 519 | 514 | continue; |
| 520 | 515 | } |
| 521 | 516 | |
| 522 | - $offset = array_shift( $queues[ $tag ] ); | |
| 517 | + // Get this tag's offset, if one was matched. | |
| 518 | + $offset = ! empty( $queues[ $tag ] ) ? array_shift( $queues[ $tag ] ) : false; | |
| 523 | 519 | |
| 524 | - if ( $depth === 0 ) { | |
| 520 | + if ( $depth === 0 && false !== $offset ) { | |
| 525 | 521 | $starts[] = $offset; |
| 526 | 522 | } |
| 527 | 523 | |
| 528 | 524 | if ( $tag !== 'HR' ) { |