| 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?(?:(?:([^:]+):)|(?:<([^>]+)>))#'; |
| 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( '#<[^>]+>#', $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 | if ( $matched && ( ! empty( $matches[1] ) || ! empty( $matches[3] ) || ! preg_match( '#\s#', $matches[2] ) ) ) { |
| 497 | if ( ! empty( $author ) ) { |
| 498 | $stanza[] = array( |
| 499 | 'time' => $time, |
| 500 | 'author' => $author, |
| 501 | 'message' => join( ' ', $data ) |
| 502 | ); |
| 503 | $data = array(); |
| 504 | } |
| 505 | |
| 506 | $time = $matches[1]; |
| 507 | $author = empty( $matches[3] ) ? $matches[2] : $matches[3]; |
| 508 | $data[] = trim( str_replace( $matches[0], '', $line ) ); |
| 509 | } elseif ( preg_match( '#\S#', $line ) ) { |
| 510 | $data[] = $line; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | if ( ! empty( $author ) ) { |
| 515 | $stanza[] = array( |
| 516 | 'time' => $time, |
| 517 | 'author' => $author, |
| 518 | 'message' => trim( join( ' ', $data ) ) |
| 519 | ); |
| 520 | } |
| 521 | |
| 522 | if ( ! empty( $stanza ) ) |
| 523 | $stanzas[] = $stanza; |
| 524 | |
| 525 | if ( $remove ) |
| 526 | $content = trim( join( "\n", array_slice( $lines, $last_index ) ) ); |
| 527 | |
| 528 | return $stanzas; |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Retrieve structured chat data from the current or passed post |
| 533 | * |
| 534 | * @since 3.6.0 |
| 535 | * |
| 536 | * @param int $id Optional. Post ID |
| 537 | * @return array |
| 538 | */ |
| 539 | function get_the_chat( $id = 0 ) { |
| 540 | $post = empty( $id ) ? get_post() : get_post( $id ); |
| 541 | if ( empty( $post ) ) |
| 542 | return array(); |
| 543 | |
| 544 | $data = get_content_chat( $post->post_content ); |
| 545 | if ( empty( $data ) ) |
| 546 | return array(); |
| 547 | |
| 548 | return $data; |
| 549 | } |