| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHP and WordPress configuration compatibility functions for the Gutenberg |
| 4 |
* editor plugin changes related to i18n. |
| 5 |
* |
| 6 |
* @package gutenberg |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Override core's wp_get_word_count_type() introduced in WordPress 6.2. |
| 11 |
* Originally, get_word_count_type() method of the WP_Locale class is executed, |
| 12 |
* but the process is simulated here. |
| 13 |
* |
| 14 |
* This function should not be backported to core. |
| 15 |
*/ |
| 16 |
if ( ! function_exists( 'wp_get_word_count_type' ) ) { |
| 17 |
/** |
| 18 |
* Retrieves the word count type based on the locale. |
| 19 |
* |
| 20 |
* @return string Locale-specific word count type. |
| 21 |
*/ |
| 22 |
function wp_get_word_count_type() { |
| 23 |
$word_count_type = _x( 'words', 'Word count type. Do not translate!', 'gutenberg' ); |
| 24 |
|
| 25 |
// Check for valid types. |
| 26 |
if ( 'characters_excluding_spaces' !== $word_count_type && 'characters_including_spaces' !== $word_count_type ) { |
| 27 |
// Defaults to 'words'. |
| 28 |
$word_count_type = 'words'; |
| 29 |
} |
| 30 |
return $word_count_type; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
if ( ! function_exists( 'wp_word_count' ) ) { |
| 35 |
/** |
| 36 |
* Count words or characters in a provided text string. |
| 37 |
* |
| 38 |
* @param string $text Text to count elements in. |
| 39 |
* @param string $type The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'. |
| 40 |
* @param array $settings { |
| 41 |
* Optional. Array of arguments used to overrides for settings. |
| 42 |
* |
| 43 |
* @type string $html_regexp Optional. Regular expression to find HTML elements. |
| 44 |
* @type string $html_comment_regexp Optional. Regular expression to find HTML comments. |
| 45 |
* @type string $space_regexp Optional. Regular expression to find irregular space |
| 46 |
* characters. |
| 47 |
* @type string $html_entity_regexp Optional. Regular expression to find HTML entities. |
| 48 |
* @type string $connector_regexp Optional. Regular expression to find connectors that |
| 49 |
* split words. |
| 50 |
* @type string $remove_regexp Optional. Regular expression to find remove unwanted |
| 51 |
* characters to reduce false-positives. |
| 52 |
* @type string $astral_regexp Optional. Regular expression to find unwanted |
| 53 |
* characters when searching for non-words. |
| 54 |
* @type string $words_regexp Optional. Regular expression to find words by spaces. |
| 55 |
* @type string $characters_excluding_spaces_regexp Optional. Regular expression to find characters which |
| 56 |
* are non-spaces. |
| 57 |
* @type string $characters_including_spaces_regexp Optional. Regular expression to find characters |
| 58 |
* including spaces. |
| 59 |
* @type array $shortcodes Optional. Array of shortcodes that should be removed |
| 60 |
* from the text. |
| 61 |
* } |
| 62 |
* @return int The word or character count. |
| 63 |
*/ |
| 64 |
function wp_word_count( $text, $type, $settings = array() ) { |
| 65 |
$defaults = array( |
| 66 |
'html_regexp' => '/<\/?[a-z][^>]*?>/i', |
| 67 |
'html_comment_regexp' => '/<!--[\s\S]*?-->/', |
| 68 |
'space_regexp' => '/ | /i', |
| 69 |
'html_entity_regexp' => '/&\S+?;/', |
| 70 |
'connector_regexp' => "/--|\x{2014}/u", |
| 71 |
'remove_regexp' => "/[\x{0021}-\x{0040}\x{005B}-\x{0060}\x{007B}-\x{007E}\x{0080}-\x{00BF}\x{00D7}\x{00F7}\x{2000}-\x{2BFF}\x{2E00}-\x{2E7F}]/u", |
| 72 |
'astral_regexp' => "/[\x{010000}-\x{10FFFF}]/u", |
| 73 |
'words_regexp' => '/\S\s+/u', |
| 74 |
'characters_excluding_spaces_regexp' => '/\S/u', |
| 75 |
'characters_including_spaces_regexp' => "/[^\f\n\r\t\v\x{00AD}\x{2028}\x{2029}]/u", |
| 76 |
'shortcodes' => array(), |
| 77 |
); |
| 78 |
|
| 79 |
$count = 0; |
| 80 |
|
| 81 |
if ( ! $text ) { |
| 82 |
return $count; |
| 83 |
} |
| 84 |
|
| 85 |
$settings = wp_parse_args( $settings, $defaults ); |
| 86 |
|
| 87 |
// If there are any shortcodes, add this as a shortcode regular expression. |
| 88 |
if ( is_array( $settings['shortcodes'] ) && ! empty( $settings['shortcodes'] ) ) { |
| 89 |
$settings['shortcodes_regexp'] = '/\\[\\/?(?:' . implode( '|', $settings['shortcodes'] ) . ')[^\\]]*?\\]/'; |
| 90 |
} |
| 91 |
|
| 92 |
// Sanitize type to one of three possibilities: 'words', 'characters_excluding_spaces' or 'characters_including_spaces'. |
| 93 |
if ( 'characters_excluding_spaces' !== $type && 'characters_including_spaces' !== $type ) { |
| 94 |
$type = 'words'; |
| 95 |
} |
| 96 |
|
| 97 |
$text .= "\n"; |
| 98 |
|
| 99 |
// Replace all HTML with a new-line. |
| 100 |
$text = preg_replace( $settings['html_regexp'], "\n", $text ); |
| 101 |
|
| 102 |
// Remove all HTML comments. |
| 103 |
$text = preg_replace( $settings['html_comment_regexp'], '', $text ); |
| 104 |
|
| 105 |
// If a shortcode regular expression has been provided use it to remove shortcodes. |
| 106 |
if ( ! empty( $settings['shortcodes_regexp'] ) ) { |
| 107 |
$text = preg_replace( $settings['shortcodes_regexp'], "\n", $text ); |
| 108 |
} |
| 109 |
|
| 110 |
// Normalize non-breaking space to a normal space. |
| 111 |
$text = preg_replace( $settings['space_regexp'], ' ', $text ); |
| 112 |
|
| 113 |
if ( 'words' === $type ) { |
| 114 |
// Remove HTML Entities. |
| 115 |
$text = preg_replace( $settings['html_entity_regexp'], '', $text ); |
| 116 |
|
| 117 |
// Convert connectors to spaces to count attached text as words. |
| 118 |
$text = preg_replace( $settings['connector_regexp'], ' ', $text ); |
| 119 |
|
| 120 |
// Remove unwanted characters. |
| 121 |
$text = preg_replace( $settings['remove_regexp'], '', $text ); |
| 122 |
} else { |
| 123 |
// Convert HTML Entities to "a". |
| 124 |
$text = preg_replace( $settings['html_entity_regexp'], 'a', $text ); |
| 125 |
|
| 126 |
// Remove surrogate points. |
| 127 |
$text = preg_replace( $settings['astral_regexp'], 'a', $text ); |
| 128 |
} |
| 129 |
|
| 130 |
// Match with the selected type regular expression to count the items. |
| 131 |
preg_match_all( $settings[ $type . '_regexp' ], $text, $matches ); |
| 132 |
|
| 133 |
if ( $matches ) { |
| 134 |
return count( $matches[0] ); |
| 135 |
} |
| 136 |
|
| 137 |
return $count; |
| 138 |
} |
| 139 |
} |
| 140 |
|