|<[^>]*>|&(?:#[0-9]+|#[xX][0-9a-fA-F]+|[A-Za-z][A-Za-z0-9]*);|./su'; /** * Characters matched as another character, because the writer reads the * rendered page, where WordPress turns straight quotes and dashes into * typographic ones. */ const EQUIVALENTS = array( "\u{2018}" => "'", "\u{2019}" => "'", "\u{201A}" => "'", "\u{201B}" => "'", "\u{2032}" => "'", "\u{201C}" => '"', "\u{201D}" => '"', "\u{201E}" => '"', "\u{201F}" => '"', "\u{2033}" => '"', "\u{2013}" => '-', "\u{2014}" => '-', "\u{2026}" => '...', ); /** * The plain text of some block HTML, as a reader would see it: tags * removed, character references decoded, whitespace collapsed, and one * line for each line break or block-level element. * * @param string $html Block HTML. * @return string */ public static function plain( $html ) { $html = (string) preg_replace( '##i', "\n", $html ); $html = (string) preg_replace( '##i', "\$0\n", $html ); $text = html_entity_decode( wp_strip_all_tags( $html ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ); $lines = array(); foreach ( explode( "\n", $text ) as $line ) { $line = trim( (string) preg_replace( '/[\s\x{00A0}]+/u', ' ', $line ) ); if ( '' !== $line ) { $lines[] = $line; } } return implode( "\n", $lines ); } /** * Normalizes text for matching: typographic quotes and dashes become * plain ones, and every run of whitespace becomes one space. * * @param string $text Plain text. * @return string */ public static function normalize( $text ) { $text = strtr( (string) $text, self::EQUIVALENTS ); return trim( (string) preg_replace( '/[\s\x{00A0}\x{2000}-\x{200A}\x{202F}]+/u', ' ', $text ) ); } /** * Whether the HTML holds something that looks like a shortcode, whether * or not a plugin on the site registers it now. * * @param string $html HTML. * @return bool */ public static function has_shortcode_like( $html ) { return 1 === preg_match( '/\[\/?[A-Za-z][\w-]*(?:[\s\/][^\]]*)?\]/', (string) $html ); } /** * Whether the text holds a shortcode registered on this site, which * WordPress would run when the page is shown. * * @param string $text Text or HTML. * @return bool */ public static function has_registered_shortcode( $text ) { global $shortcode_tags; if ( empty( $shortcode_tags ) || false === strpos( (string) $text, '[' ) ) { return false; } return 1 === preg_match( '/' . get_shortcode_regex() . '/', (string) $text ); } /** * Escapes plain text for use as HTML text. * * @param string $text Plain text. * @return string */ public static function escape( $text ) { return htmlspecialchars( (string) $text, ENT_NOQUOTES, 'UTF-8' ); } /** * Finds where a phrase occurs in a block's own HTML. * * The HTML is given as the string parts of the block's `innerContent`; * the positions of its inner blocks separate those parts. The phrase must * occur exactly once in the block's text, and that occurrence must sit * inside one text segment, so it never spans a tag. * * @param string[] $parts The block's HTML parts, keyed by their index in `innerContent`. * @param string $phrase The phrase, as plain text. * @return array{part: int, start: int, end: int}|\WP_Error Byte range of the phrase in one part. */ public static function find_phrase( array $parts, $phrase ) { $needle = self::normalize( $phrase ); if ( '' === $needle ) { return self::refusal( 'searchsmith_empty_phrase', __( 'The phrase is empty.', 'searchsmith' ) ); } list( $haystack, $units ) = self::units( $parts ); $found = array(); $at = strpos( $haystack, $needle ); while ( false !== $at ) { $found[] = $at; $at = strpos( $haystack, $needle, $at + 1 ); } if ( ! $found ) { return self::refusal( 'searchsmith_phrase_not_found', __( 'The phrase does not occur in the block.', 'searchsmith' ) ); } if ( count( $found ) > 1 ) { return self::refusal( 'searchsmith_phrase_ambiguous', __( 'The phrase occurs more than once in the block.', 'searchsmith' ), array( 'occurrences' => count( $found ) ) ); } $start = $found[0]; $end = $start + strlen( $needle ); $first = null; $last = null; foreach ( $units as $unit ) { if ( $unit['n_end'] === $unit['n_start'] ) { continue; } if ( null === $first && $unit['n_start'] === $start ) { $first = $unit; } if ( $unit['n_end'] === $end ) { $last = $unit; } } if ( null === $first || null === $last || $first['segment'] !== $last['segment'] ) { return self::refusal( 'searchsmith_phrase_crosses_markup', __( 'The phrase spans formatting or a link in the block.', 'searchsmith' ) ); } return array( 'part' => $first['part'], 'start' => $first['raw_start'], 'end' => $last['raw_end'], ); } /** * Splits HTML parts into units and builds their normalized text. * * @param string[] $parts HTML parts keyed by `innerContent` index. * @return array{0: string, 1: array[]} The normalized text and its units. */ private static function units( array $parts ) { $text = ''; $units = array(); $segment = 0; foreach ( $parts as $part_index => $html ) { ++$segment; preg_match_all( self::UNIT_PATTERN, (string) $html, $matches, PREG_OFFSET_CAPTURE ); foreach ( $matches[0] as $match ) { list( $raw, $offset ) = $match; if ( '<' === $raw[0] && strlen( $raw ) > 1 ) { ++$segment; continue; } $char = '&' === $raw[0] && strlen( $raw ) > 1 ? html_entity_decode( $raw, ENT_QUOTES | ENT_HTML5, 'UTF-8' ) : $raw; if ( 1 === preg_match( '/^[\s\x{00A0}\x{2000}-\x{200A}\x{202F}]+$/u', $char ) ) { // A run of whitespace counts as one space. $char = ( '' === $text || ' ' === substr( $text, -1 ) ) ? '' : ' '; } else { $char = strtr( $char, self::EQUIVALENTS ); } $units[] = array( 'part' => $part_index, 'segment' => $segment, 'raw_start' => $offset, 'raw_end' => $offset + strlen( $raw ), 'n_start' => strlen( $text ), 'n_end' => strlen( $text ) + strlen( $char ), ); $text .= $char; }//end foreach }//end foreach return array( $text, $units ); } /** * A refusal of an operation. * * @param string $code Error code. * @param string $message Message. * @param array $data Extra data. * @return \WP_Error */ private static function refusal( $code, $message, array $data = array() ) { return new \WP_Error( $code, $message, array_merge( array( 'status' => 422 ), $data ) ); } }