| 1 | <?php |
| 2 | /** |
| 3 | * Shortcode API: WP_Shortcode_Parser class |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @since x.x.x |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * A collection of methods for parsing and executing shortcodes in content. |
| 11 | * |
| 12 | * @since x.x.x |
| 13 | */ |
| 14 | class WP_Shortcode_Parser { |
| 15 | /** |
| 16 | * The content being parsed. |
| 17 | * |
| 18 | * @since x.x.x |
| 19 | * |
| 20 | * @var string $content |
| 21 | */ |
| 22 | private $content; |
| 23 | |
| 24 | /** |
| 25 | * The current state of the parser. |
| 26 | * |
| 27 | * @since x.x.x |
| 28 | * |
| 29 | * @var int $state One of the `SHORTCODE_PARSE_STATE_*` constants listed below. |
| 30 | */ |
| 31 | private $state; |
| 32 | |
| 33 | /** |
| 34 | * The current position of the parsing cursor in the content. |
| 35 | * |
| 36 | * @since x.x.x |
| 37 | * |
| 38 | * @var int $cursor_position |
| 39 | */ |
| 40 | private $cursor_position; |
| 41 | |
| 42 | /** |
| 43 | * The stack of unprocessed shortcodes. |
| 44 | * |
| 45 | * As shortcodes are opened, they are placed on the stack, and as they're |
| 46 | * closed, they're processed and removed. |
| 47 | * |
| 48 | * @since x.x.x |
| 49 | * |
| 50 | * @var array $stack |
| 51 | */ |
| 52 | private $stack; |
| 53 | |
| 54 | /** |
| 55 | * A list of all registered shortcode tag names. |
| 56 | * |
| 57 | * @since x.x.x |
| 58 | * |
| 59 | * @var string[] $tagnames |
| 60 | */ |
| 61 | private $tagnames; |
| 62 | |
| 63 | /** |
| 64 | * The shortcode currently being parsed. |
| 65 | * |
| 66 | * As a shortcode is being parsed, it is stored here. If a new shortcode is |
| 67 | * found before parsing is complete, this shortcode is moved to the stack, |
| 68 | * and the new shortcode is stored here. |
| 69 | * |
| 70 | * @since x.x.x |
| 71 | * |
| 72 | * @var array $current_shortcode { |
| 73 | * @type string $full_tag The content that makes up this tag, from the opening |
| 74 | * bracket to the closing bracket. |
| 75 | * @type string $extra_opening_bracket Either the character '[' or an empty string, |
| 76 | * depending on whether there was an extra opening |
| 77 | * bracket for this shortcode. Essentially a flag. |
| 78 | * @type string $tag_slug The shortcode tag slug. |
| 79 | * @type string $atts_and_values The (unparsed) part of the shortcode tag that contains |
| 80 | * attributes and their optional values. |
| 81 | * @type string $self_closing_slash Like $extra_opening_bracket, this is either the |
| 82 | * slash used to self-close the shortcode tag, or an |
| 83 | * empty string. |
| 84 | * @type int $cursor_position The cursor position where this shortcode began. |
| 85 | * } |
| 86 | */ |
| 87 | private $current_shortcode; |
| 88 | |
| 89 | /** |
| 90 | * The default parsing state -- the cursor is not in a shortcode tag or |
| 91 | * shortcode content or a quoted string in a shortcode attribute value. |
| 92 | * |
| 93 | * @since x.x.x |
| 94 | * |
| 95 | * @var int $SHORTCODE_PARSE_STATE_DEFAULT |
| 96 | */ |
| 97 | const SHORTCODE_PARSE_STATE_DEFAULT = 0; |
| 98 | |
| 99 | /** |
| 100 | * The cursor is inside the shortcode tag, past the shortcode tag slug. |
| 101 | * |
| 102 | * @since x.x.x |
| 103 | * |
| 104 | * @var int $SHORTCODE_PARSE_STATE_IN_TAG |
| 105 | */ |
| 106 | const SHORTCODE_PARSE_STATE_IN_TAG = 1; |
| 107 | |
| 108 | /** |
| 109 | * The cursor is in the content of the shortcode -- past the opening tag |
| 110 | * but not yet to the closing tag. |
| 111 | * |
| 112 | * @since x.x.x |
| 113 | * |
| 114 | * @var int $SHORTCODE_PARSE_STATE_IN_CONTENT |
| 115 | */ |
| 116 | const SHORTCODE_PARSE_STATE_IN_CONTENT = 2; |
| 117 | |
| 118 | /** |
| 119 | * The cursor is inside of a quoted string in the shortcode tag. |
| 120 | * |
| 121 | * @since x.x.x |
| 122 | * |
| 123 | * @var int $SHORTCODE_PARSE_STATE_IN_QUOTED_STRING |
| 124 | */ |
| 125 | const SHORTCODE_PARSE_STATE_IN_QUOTED_STRING = 3; |
| 126 | |
| 127 | /** |
| 128 | * Store the content and tag names for later use. |
| 129 | * |
| 130 | * @since x.x.x |
| 131 | * |
| 132 | * @param string $content The HTML content to parse for shortcodes. |
| 133 | * @param string[] $tagnames An array of string shortcode tag names. |
| 134 | */ |
| 135 | public function __construct( $content, $tagnames ) { |
| 136 | $this->content = $content; |
| 137 | $this->tagnames = $tagnames; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Parse shortcodes in content and replace them with the output that their |
| 142 | * handler functions generate. |
| 143 | * |
| 144 | * @since x.x.x |
| 145 | * |
| 146 | * @return string The content with shortcodes replaced by their output. |
| 147 | */ |
| 148 | public function parse() { |
| 149 | $this->stack = array(); |
| 150 | |
| 151 | /* |
| 152 | * A regular expression that checks whether a string appears to begin |
| 153 | * with a tag for a registered shortcode. |
| 154 | */ |
| 155 | $registered_shortcode_regex = '/^(?P<extra_opening_bracket>\\[?)(?P<opening_bracket>\\[)(?P<tag_slug>' . join( '|', array_map( 'preg_quote', $this->tagnames ) ) . ')(?![\\w-])/u'; |
| 156 | |
| 157 | $this->cursor_position = 0; |
| 158 | |
| 159 | // Save some parsing time by starting a few characters before the first bracket. |
| 160 | $this->forward_cursor_to_next_bracket(); |
| 161 | |
| 162 | $this->state = self::SHORTCODE_PARSE_STATE_DEFAULT; |
| 163 | |
| 164 | $is_escaped = false; |
| 165 | $delimiter = null; |
| 166 | |
| 167 | while ( $this->cursor_position < strlen( $this->content ) ) { |
| 168 | $char = substr( $this->content, $this->cursor_position, 1 ); |
| 169 | |
| 170 | $found_escape_character = false; |
| 171 | |
| 172 | switch ( $this->state ) { |
| 173 | case self::SHORTCODE_PARSE_STATE_DEFAULT: |
| 174 | case self::SHORTCODE_PARSE_STATE_IN_CONTENT: |
| 175 | if ( ! $is_escaped && '[' === $char && preg_match( $registered_shortcode_regex, substr( $this->content, $this->cursor_position ), $m ) ) { |
| 176 | if ( $this->current_shortcode ) { |
| 177 | $this->stack[] = $this->current_shortcode; |
| 178 | } |
| 179 | |
| 180 | // We have found the beginning of a shortcode. |
| 181 | $this->current_shortcode = array( |
| 182 | 'full_tag' => $m[0], |
| 183 | 'extra_opening_bracket' => $m['extra_opening_bracket'], |
| 184 | 'tag_slug' => $m['tag_slug'], |
| 185 | 'atts_and_values' => '', |
| 186 | 'self_closing_slash' => '', |
| 187 | 'inner_content' => '', |
| 188 | 'extra_closing_bracket' => '', |
| 189 | 'cursor_position' => $this->cursor_position, |
| 190 | ); |
| 191 | |
| 192 | $this->cursor_position += strlen( $m[0] ); |
| 193 | |
| 194 | // Move back one so it's as if we just processed the last character of the shortcode slug. |
| 195 | $this->cursor_position--; |
| 196 | |
| 197 | $this->state = self::SHORTCODE_PARSE_STATE_IN_TAG; |
| 198 | } elseif ( self::SHORTCODE_PARSE_STATE_IN_CONTENT === $this->state ) { |
| 199 | $this->current_shortcode['full_tag'] .= $char; |
| 200 | |
| 201 | if ( '[' === $char ) { |
| 202 | // Check whether it's a closing tag of any currently open shortcode. |
| 203 | $rest_of_closing_tag = '/' . $this->current_shortcode['tag_slug'] . ']'; |
| 204 | |
| 205 | if ( substr( $this->content, $this->cursor_position + 1, strlen( $rest_of_closing_tag ) ) === $rest_of_closing_tag ) { |
| 206 | // The end of this shortcode. |
| 207 | |
| 208 | $this->current_shortcode['full_tag'] .= $rest_of_closing_tag; |
| 209 | |
| 210 | // Move the cursor to the end of the closing tag. |
| 211 | $this->cursor_position += strlen( $rest_of_closing_tag ); |
| 212 | |
| 213 | if ( $this->current_shortcode['extra_opening_bracket'] ) { |
| 214 | if ( ']' === substr( $this->content, $this->cursor_position + 1, 1 ) ) { |
| 215 | $this->current_shortcode['full_tag'] .= ']'; |
| 216 | $this->current_shortcode['extra_closing_bracket'] = ']'; |
| 217 | $this->cursor_position++; |
| 218 | } else { |
| 219 | // If there was an extra opening bracket but not an extra closing bracket, ignore the extra opening bracket. |
| 220 | |
| 221 | $this->current_shortcode['full_tag'] = substr( $this->current_shortcode['full_tag'], 1 ); |
| 222 | $this->current_shortcode['extra_opening_bracket'] = ''; |
| 223 | |
| 224 | // We initially thought it had an extra opening bracket, but it doesn't so it started one character later than we thought. |
| 225 | $this->current_shortcode['cursor_position'] += 1; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | $this->process_current_shortcode(); |
| 230 | } else { |
| 231 | $found_matching_shortcode = false; |
| 232 | |
| 233 | for ( $stack_index = count( $this->stack ) - 1; $stack_index >= 0; $stack_index-- ) { |
| 234 | $rest_of_closing_tag = '/' . $this->stack[ $stack_index ]['tag_slug'] . ']'; |
| 235 | |
| 236 | if ( substr( $this->content, $this->cursor_position + 1, strlen( $rest_of_closing_tag ) ) === $rest_of_closing_tag ) { |
| 237 | // Yes, it closes this one. |
| 238 | $found_matching_shortcode = true; |
| 239 | |
| 240 | /* |
| 241 | * We already saved the bracket as part of the full tag, expecting that the |
| 242 | * closing tag would be for the current shortcode. It's not, so remove it. |
| 243 | */ |
| 244 | $this->current_shortcode['full_tag'] = substr( $this->current_shortcode['full_tag'], 0, -1 ); |
| 245 | |
| 246 | // This means that the "current" shortcode and any others above this one on the stack need to be closed out, because they are self-closing. |
| 247 | do { |
| 248 | $this->current_shortcode['full_tag'] = substr( $this->current_shortcode['full_tag'], 0, -1 * strlen( $this->current_shortcode['inner_content'] ) ); |
| 249 | |
| 250 | // And there is no inner content. |
| 251 | $this->current_shortcode['inner_content'] = ''; |
| 252 | |
| 253 | $this->process_current_shortcode(); // This sets $current_shortcode using the top stack item, so we don't need to do it. |
| 254 | } while ( count( $this->stack ) > $stack_index + 1 ); |
| 255 | |
| 256 | /* |
| 257 | * At this point, the shortcode that is being closed right now is $this->current_shortcode. |
| 258 | * The easiest way to process this without duplicating code is to reprocess the current |
| 259 | * character with the new stack and current shortcode, so the section above will get |
| 260 | * triggered, since the closing tag will be for the current shortcode. |
| 261 | */ |
| 262 | |
| 263 | continue 3; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if ( ! $found_matching_shortcode ) { |
| 268 | $this->current_shortcode['inner_content'] .= $char; |
| 269 | } |
| 270 | } |
| 271 | } else { |
| 272 | $this->current_shortcode['inner_content'] .= $char; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | break; |
| 277 | case self::SHORTCODE_PARSE_STATE_IN_TAG: |
| 278 | $this->current_shortcode['full_tag'] .= $char; |
| 279 | |
| 280 | if ( '/' === $char && substr( $this->content, $this->cursor_position + 1, 1 ) === ']' ) { |
| 281 | // The shortcode is over. |
| 282 | $this->current_shortcode['self_closing_slash'] = '/'; |
| 283 | $this->current_shortcode['full_tag'] .= ']'; |
| 284 | $this->cursor_position++; |
| 285 | |
| 286 | // If the shortcode had an extra opening bracket but doesn't have an extra closing bracket, ignore the extra opening bracket. |
| 287 | |
| 288 | if ( $this->current_shortcode['extra_opening_bracket'] ) { |
| 289 | if ( ']' === substr( $this->content, $this->cursor_position + 1, 1 ) ) { |
| 290 | $this->current_shortcode['extra_closing_bracket'] = ']'; |
| 291 | $this->current_shortcode['full_tag'] .= ']'; |
| 292 | $this->cursor_position++; |
| 293 | } else { |
| 294 | $this->current_shortcode['full_tag'] = substr( $this->current_shortcode['full_tag'], 1 ); |
| 295 | $this->current_shortcode['extra_opening_bracket'] = ''; |
| 296 | |
| 297 | /* |
| 298 | * We initially thought it had an extra opening bracket, but it doesn't, |
| 299 | * so it started one character later than we thought. |
| 300 | */ |
| 301 | $this->current_shortcode['cursor_position'] += 1; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | $this->process_current_shortcode(); |
| 306 | |
| 307 | break; |
| 308 | } elseif ( ']' === $char ) { |
| 309 | if ( $this->current_shortcode['extra_opening_bracket'] ) { |
| 310 | /* |
| 311 | * This makes the assumption that this shortcode is closed as soon as the double brackets are found: |
| 312 | * |
| 313 | * [[my-shortcode]][/my-shortcode]] |
| 314 | * |
| 315 | * But in theory, this could just be a shortcode with the content "]". |
| 316 | */ |
| 317 | |
| 318 | if ( ']' === substr( $this->content, $this->cursor_position + 1, 1 ) ) { |
| 319 | $this->current_shortcode['extra_closing_bracket'] = ']'; |
| 320 | $this->current_shortcode['full_tag'] .= ']'; |
| 321 | $this->cursor_position++; |
| 322 | |
| 323 | $this->process_current_shortcode(); |
| 324 | break; |
| 325 | } else { |
| 326 | // There was not an extra closing bracket. |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | if ( false === strpos( substr( $this->content, $this->cursor_position ), '[/' . $this->current_shortcode['tag_slug'] . ']' ) ) { |
| 331 | // If there's no closing tag, it's a self-enclosed shortcode, and we're done with it. |
| 332 | $this->process_current_shortcode(); |
| 333 | } else { |
| 334 | $this->state = self::SHORTCODE_PARSE_STATE_IN_CONTENT; |
| 335 | |
| 336 | $current_cursor_position = $this->cursor_position; |
| 337 | $this->forward_cursor_to_next_bracket(); |
| 338 | |
| 339 | if ( $this->cursor_position !== $current_cursor_position ) { |
| 340 | // The +1 is because the character at $current_cursor_position has already been recorded. |
| 341 | $skipped_content = substr( $this->content, $current_cursor_position + 1, $this->cursor_position - $current_cursor_position ); |
| 342 | |
| 343 | $this->current_shortcode['inner_content'] .= $skipped_content; |
| 344 | $this->current_shortcode['full_tag'] .= $skipped_content; |
| 345 | } |
| 346 | } |
| 347 | } else { |
| 348 | $this->current_shortcode['atts_and_values'] .= $char; |
| 349 | |
| 350 | if ( '"' === $char || "'" === $char ) { |
| 351 | $this->state = self::SHORTCODE_PARSE_STATE_IN_QUOTED_STRING; |
| 352 | $delimiter = $char; |
| 353 | } else { |
| 354 | // Nothing to do. |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | break; |
| 359 | case self::SHORTCODE_PARSE_STATE_IN_QUOTED_STRING: |
| 360 | $this->current_shortcode['full_tag'] .= $char; |
| 361 | $this->current_shortcode['atts_and_values'] .= $char; |
| 362 | |
| 363 | if ( $is_escaped ) { |
| 364 | // Nothing to do. This is just an escaped character to be taken literally. |
| 365 | } else { |
| 366 | // Not escaped. |
| 367 | if ( '\\' === $char ) { |
| 368 | // The next character is escaped. |
| 369 | $found_escape_character = true; |
| 370 | } elseif ( $char === $delimiter ) { |
| 371 | $this->state = self::SHORTCODE_PARSE_STATE_IN_TAG; |
| 372 | $delimiter = null; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | // Is the next character escaped? |
| 380 | if ( $found_escape_character ) { |
| 381 | $is_escaped = true; |
| 382 | } else { |
| 383 | // If we didn't find an escape character here, then no. |
| 384 | $is_escaped = false; |
| 385 | } |
| 386 | |
| 387 | $this->cursor_position++; |
| 388 | } |
| 389 | |
| 390 | if ( self::SHORTCODE_PARSE_STATE_IN_QUOTED_STRING === $this->state ) { |
| 391 | /* |
| 392 | * example: This is my content [footag foo=" [bartag] |
| 393 | * Should it be reprocessed in order to convert [bartag] or is this considered malformed? |
| 394 | */ |
| 395 | } |
| 396 | |
| 397 | if ( self::SHORTCODE_PARSE_STATE_IN_TAG === $this->state ) { |
| 398 | /* |
| 399 | * example: This is my content [footag foo="abc" bar="def" [bartag] |
| 400 | * Should it be reprocessed in order to convert [bartag] or is this considered malformed? |
| 401 | */ |
| 402 | } |
| 403 | |
| 404 | if ( $this->current_shortcode ) { |
| 405 | /* |
| 406 | * If we end with shortcodes still on the stack, then there was a situation like this: |
| 407 | * |
| 408 | * [footag] [bartag] [baztag] [footag]content[/footag] |
| 409 | * |
| 410 | * i.e., a scenario where the parser was unsure whether the first [footag] was self-closing or not. |
| 411 | * |
| 412 | * By this point, $content will be in this format: |
| 413 | * |
| 414 | * [footag] bartag-output baztag-output footag-content-output |
| 415 | * |
| 416 | * so we need to back up and process the still-stored shortcodes as unclosed. |
| 417 | * |
| 418 | * An extreme version of this would look like: |
| 419 | * |
| 420 | * [footag] [footag] [footag] [footag] [footag] [footag] [footag] [footag] ... [footag][/footag] |
| 421 | * |
| 422 | * where the last tag would be the only one processed normally above and there would be n-1 [footag]s still on the stack. |
| 423 | */ |
| 424 | while ( $this->current_shortcode ) { |
| 425 | // What we thought was part of this tag was just regular content. |
| 426 | $this->current_shortcode['full_tag'] = substr( $this->current_shortcode['full_tag'], 0, -1 * strlen( $this->current_shortcode['inner_content'] ) ); |
| 427 | |
| 428 | // And there is no inner content. |
| 429 | $this->current_shortcode['inner_content'] = ''; |
| 430 | |
| 431 | $this->process_current_shortcode(); // This sets $current_shortcode, so we don't need to do it. |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | return $this->content; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Create an argument to pass to do_shortcode_tag. |
| 440 | * |
| 441 | * The format of this argument was determined by the capture groups of the |
| 442 | * regular expression that used to be used to parse shortcodes out of content. |
| 443 | * |
| 444 | * @since x.x.x |
| 445 | * |
| 446 | * @param array $shortcode An associative array comprising data about a shortcode in the text. |
| 447 | * @return array A numerically-indexed array of the shortcode data ready for do_shortcode_tag(). |
| 448 | */ |
| 449 | private function shortcode_argument( $shortcode ) { |
| 450 | return array( |
| 451 | $shortcode['full_tag'], |
| 452 | $shortcode['extra_opening_bracket'], |
| 453 | $shortcode['tag_slug'], |
| 454 | $shortcode['atts_and_values'], |
| 455 | $shortcode['self_closing_slash'], |
| 456 | $shortcode['inner_content'], |
| 457 | $shortcode['extra_closing_bracket'], |
| 458 | ); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Process the shortcode at the top of the stack. |
| 463 | * |
| 464 | * @since x.x.x |
| 465 | * |
| 466 | * The shortcode at the top of the stack is complete and can be processed. |
| 467 | * Process it and modify the enclosing shortcode as if the content was passed in |
| 468 | * with this shortcode already converted into HTML. |
| 469 | */ |
| 470 | private function process_current_shortcode() { |
| 471 | $argument_for_do_shortcode_tag = $this->shortcode_argument( $this->current_shortcode ); |
| 472 | |
| 473 | $shortcode_output = do_shortcode_tag( $argument_for_do_shortcode_tag ); |
| 474 | |
| 475 | /* |
| 476 | * Replace based on position rather than find and replace, since this content is possible: |
| 477 | * |
| 478 | * Test 123 [some-shortcode] To use my shortcode, type [[some-shortcode]]. |
| 479 | */ |
| 480 | $this->content = |
| 481 | substr( $this->content, 0, $this->current_shortcode['cursor_position'] ) |
| 482 | . $shortcode_output |
| 483 | . substr( $this->content, $this->current_shortcode['cursor_position'] + strlen( $this->current_shortcode['full_tag'] ) ); |
| 484 | |
| 485 | /* |
| 486 | * Update the cursor position to the end of this shortcode's output. |
| 487 | * The -1 is because the position is incremented after this gets called to move it to the next character. |
| 488 | */ |
| 489 | $this->cursor_position = $this->current_shortcode['cursor_position'] + strlen( $shortcode_output ) - 1; |
| 490 | |
| 491 | // For any enclosing shortcode, its inner content needs to include the full output of this shortcode. |
| 492 | if ( ! empty( $this->stack ) ) { |
| 493 | $this->current_shortcode = array_pop( $this->stack ); |
| 494 | |
| 495 | $this->current_shortcode['inner_content'] .= $shortcode_output; |
| 496 | $this->current_shortcode['full_tag'] .= $shortcode_output; |
| 497 | |
| 498 | $this->state = self::SHORTCODE_PARSE_STATE_IN_CONTENT; |
| 499 | |
| 500 | $current_cursor_position = $this->cursor_position; |
| 501 | $this->forward_cursor_to_next_bracket(); |
| 502 | |
| 503 | if ( $this->cursor_position !== $current_cursor_position ) { |
| 504 | /* |
| 505 | * The +1 is because the character at $current_cursor_position has already been recorded. |
| 506 | */ |
| 507 | $skipped_content = substr( $this->content, $current_cursor_position + 1, $this->cursor_position - $current_cursor_position ); |
| 508 | |
| 509 | $this->current_shortcode['inner_content'] .= $skipped_content; |
| 510 | $this->current_shortcode['full_tag'] .= $skipped_content; |
| 511 | } |
| 512 | } else { |
| 513 | $this->current_shortcode = null; |
| 514 | |
| 515 | $this->state = self::SHORTCODE_PARSE_STATE_DEFAULT; |
| 516 | |
| 517 | // In the default state, we can skip over any content that couldn't be a shortcode, so let's move forward near the next bracket. |
| 518 | $this->forward_cursor_to_next_bracket(); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Moves the parsing cursor to the next possible location that might |
| 524 | * include a shortcode. |
| 525 | * |
| 526 | * The specific location is directly before the next bracket or the end |
| 527 | * of the content if there is no next bracket. |
| 528 | * |
| 529 | * @since x.x.x |
| 530 | */ |
| 531 | private function forward_cursor_to_next_bracket() { |
| 532 | /* |
| 533 | * The max() here is because $cursor_position can be -1 if a shortcode |
| 534 | * at the beginning of the content didn't have any output and reset the |
| 535 | * cursor back to the beginning. It's -1 instead of zero because it will |
| 536 | * be incremented later in the loop to set it to zero for the next iteration. |
| 537 | */ |
| 538 | $next_bracket_location = strpos( $this->content, '[', max( 0, $this->cursor_position ) ); |
| 539 | |
| 540 | if ( false === $next_bracket_location ) { |
| 541 | // There is no next bracket, so fast-forward to the end. |
| 542 | $next_bracket_location = strlen( $this->content ); |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Again, the -1 is because this will be incremented before it is used, |
| 547 | * and we really want it to have a minimum value of zero. |
| 548 | */ |
| 549 | $this->cursor_position = max( -1, $next_bracket_location - 1 ); |
| 550 | } |
| 551 | } |