| | 423 | |
| | 424 | /** |
| | 425 | * Deliberately interpret passed content as a chat transcript that is optionally |
| | 426 | * followed by commentary |
| | 427 | * |
| | 428 | * If the content does not contain username syntax, assume that it does not contain |
| | 429 | * chat logs and return |
| | 430 | * |
| | 431 | * @since 3.6.0 |
| | 432 | * |
| | 433 | * Example: |
| | 434 | * |
| | 435 | * One stanza of chat: |
| | 436 | * Scott: Hey, let's chat! |
| | 437 | * Helen: No. |
| | 438 | * |
| | 439 | * $stanzas = array( |
| | 440 | * array( |
| | 441 | * array( |
| | 442 | * 'time' => '', |
| | 443 | * 'author' => 'Scott', |
| | 444 | * 'messsage' => "Hey, let's chat!" |
| | 445 | * ), |
| | 446 | * array( |
| | 447 | * 'time' => '', |
| | 448 | * 'author' => 'Helen', |
| | 449 | * 'message' => 'No.' |
| | 450 | * ) |
| | 451 | * ) |
| | 452 | * ) |
| | 453 | * @param string $content A string which might contain chat data. |
| | 454 | * @param boolean $remove Whether to remove the found data from the passed content. |
| | 455 | * @return array A chat log as structured data |
| | 456 | */ |
| | 457 | function get_content_chat( &$content, $remove = false ) { |
| | 458 | $trimmed = trim( $content ); |
| | 459 | $newline_regex = '#^(\[.+?\])?\s?(?:(?:([^:<>]+):)|(?:<([^>\s]+)>))#'; |
| | 460 | |
| | 461 | if ( empty( $trimmed ) || ! preg_match( $newline_regex, $trimmed ) ) |
| | 462 | return array(); |
| | 463 | |
| | 464 | $last_index = 0; |
| | 465 | $stanzas = array(); |
| | 466 | $lines = explode( "\n", make_clickable( $trimmed ) ); |
| | 467 | |
| | 468 | $author = $time = ''; |
| | 469 | $data = array(); |
| | 470 | $stanza = array(); |
| | 471 | |
| | 472 | foreach ( $lines as $index => $line ) { |
| | 473 | $line = trim( $line ); |
| | 474 | |
| | 475 | if ( empty( $line ) ) { |
| | 476 | if ( ! empty( $author ) ) { |
| | 477 | $stanza[] = array( |
| | 478 | 'time' => $time, |
| | 479 | 'author' => $author, |
| | 480 | 'message' => join( ' ', $data ) |
| | 481 | ); |
| | 482 | } |
| | 483 | |
| | 484 | $stanzas[] = $stanza; |
| | 485 | $last_index = $index; |
| | 486 | $stanza = array(); |
| | 487 | $author = $time = ''; |
| | 488 | $data = array(); |
| | 489 | $commentary = ! preg_match( '#[:]#', $lines[$index + 1] ) && ! preg_match( '#<[^>\s]+>#', $lines[$index + 1] ); |
| | 490 | if ( ! empty( $lines[$index + 1] ) && $commentary ) |
| | 491 | break; |
| | 492 | } |
| | 493 | |
| | 494 | $matches = array(); |
| | 495 | $matched = preg_match( $newline_regex, $line, $matches ); |
| | 496 | // assume username syntax if no whitespace is present |
| | 497 | $no_ws = $matched && ! preg_match( '#\s#', $matches[2] ); |
| | 498 | // allow script-like stanzas |
| | 499 | $has_ws = $matched && preg_match( '#\s#', $matches[2] ) && empty( $lines[$index + 1] ) && empty( $lines[$index - 1] ); |
| | 500 | if ( $matched && ( ! empty( $matches[1] ) || ( ! empty( $matches[3] ) || $no_ws || $has_ws ) ) ) { |
| | 501 | if ( ! empty( $author ) ) { |
| | 502 | $stanza[] = array( |
| | 503 | 'time' => $time, |
| | 504 | 'author' => $author, |
| | 505 | 'message' => join( ' ', $data ) |
| | 506 | ); |
| | 507 | $data = array(); |
| | 508 | } |
| | 509 | |
| | 510 | $time = $matches[1]; |
| | 511 | $author = empty( $matches[3] ) ? $matches[2] : $matches[3]; |
| | 512 | $data[] = trim( str_replace( $matches[0], '', $line ) ); |
| | 513 | } elseif ( preg_match( '#\S#', $line ) ) { |
| | 514 | $data[] = $line; |
| | 515 | } |
| | 516 | } |
| | 517 | |
| | 518 | if ( ! empty( $author ) ) { |
| | 519 | $stanza[] = array( |
| | 520 | 'time' => $time, |
| | 521 | 'author' => $author, |
| | 522 | 'message' => trim( join( ' ', $data ) ) |
| | 523 | ); |
| | 524 | } |
| | 525 | |
| | 526 | if ( ! empty( $stanza ) ) |
| | 527 | $stanzas[] = $stanza; |
| | 528 | |
| | 529 | if ( $remove ) |
| | 530 | $content = trim( join( "\n", array_slice( $lines, $last_index ) ) ); |
| | 531 | |
| | 532 | return $stanzas; |
| | 533 | } |
| | 534 | |
| | 535 | /** |
| | 536 | * Retrieve structured chat data from the current or passed post |
| | 537 | * |
| | 538 | * @since 3.6.0 |
| | 539 | * |
| | 540 | * @param int $id Optional. Post ID |
| | 541 | * @return array |
| | 542 | */ |
| | 543 | function get_the_chat( $id = 0 ) { |
| | 544 | $post = empty( $id ) ? get_post() : get_post( $id ); |
| | 545 | if ( empty( $post ) ) |
| | 546 | return array(); |
| | 547 | |
| | 548 | $data = get_content_chat( $post->post_content ); |
| | 549 | if ( empty( $data ) ) |
| | 550 | return array(); |
| | 551 | |
| | 552 | return $data; |
| | 553 | } |