| 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 | * 'author' => 'Scott', |
| 443 | * 'messsage' => "Hey, let's chat!" |
| 444 | * ), |
| 445 | * array( |
| 446 | * 'author' => 'Helen', |
| 447 | * 'message' => 'No.' |
| 448 | * ) |
| 449 | * ) |
| 450 | * ) |
| 451 | * @param string $content A string which might contain chat data. |
| 452 | * @param boolean $remove Whether to remove the found data from the passed content. |
| 453 | * @return array A chat log as structured data |
| 454 | */ |
| 455 | function get_content_chat( &$content, $remove = false ) { |
| 456 | if ( empty( $content ) || ! preg_match( '#\S+: \S+#', $content ) ) |
| 457 | return array(); |
| 458 | |
| 459 | $last_index = 0; |
| 460 | $stanzas = array(); |
| 461 | $lines = explode( "\n", $content ); |
| 462 | |
| 463 | $author = ''; |
| 464 | $data = array(); |
| 465 | $stanza = array(); |
| 466 | |
| 467 | foreach ( $lines as $index => $line ) { |
| 468 | $line = trim( $line ); |
| 469 | |
| 470 | if ( empty( $line ) ) { |
| 471 | if ( ! empty( $author ) ) { |
| 472 | $stanza[] = array( |
| 473 | 'author' => $author, |
| 474 | 'message' => join( ' ', $data ) |
| 475 | ); |
| 476 | } |
| 477 | |
| 478 | $stanzas[] = $stanza; |
| 479 | $last_index = $index; |
| 480 | $stanza = array(); |
| 481 | $author = ''; |
| 482 | $data = array(); |
| 483 | if ( ! empty( $lines[$index + 1] ) && ! strstr( $lines[$index + 1], ':' ) ) |
| 484 | break; |
| 485 | } |
| 486 | |
| 487 | $where = strpos( $line, ': ' ); |
| 488 | if ( $where && ! preg_match( '#\s#', substr( $line, 0, $where ) ) ) { |
| 489 | if ( ! empty( $author ) ) { |
| 490 | $stanza[] = array( |
| 491 | 'author' => $author, |
| 492 | 'message' => join( ' ', $data ) |
| 493 | ); |
| 494 | $data = array(); |
| 495 | } |
| 496 | |
| 497 | $chunks = explode( ': ', $line ); |
| 498 | $author = array_shift( $chunks ); |
| 499 | $data[] = trim( join( ': ', $chunks ) ); |
| 500 | } else { |
| 501 | $data[] = trim( $line ); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if ( ! empty( $author ) ) { |
| 506 | $stanza[] = array( |
| 507 | 'author' => $author, |
| 508 | 'message' => join( ' ', $data ) |
| 509 | ); |
| 510 | } |
| 511 | |
| 512 | if ( ! empty( $stanza ) ) |
| 513 | $stanzas[] = $stanza; |
| 514 | |
| 515 | if ( $remove ) |
| 516 | $content = trim( join( "\n", array_slice( $lines, $last_index ) ) ); |
| 517 | |
| 518 | return $stanzas; |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Retrieve structured chat data from the current or passed post |
| 523 | * |
| 524 | * @since 3.6.0 |
| 525 | * |
| 526 | * @param int $id Optional. Post ID |
| 527 | * @return type |
| 528 | */ |
| 529 | function get_the_chat( $id = 0 ) { |
| 530 | $post = empty( $id ) ? get_post() : get_post( $id ); |
| 531 | if ( empty( $post ) ) |
| 532 | return array(); |
| 533 | |
| 534 | $data = get_content_chat( $post->post_content ); |
| 535 | if ( empty( $data ) ) |
| 536 | return array(); |
| 537 | |
| 538 | return $data; |
| 539 | } |
| 540 | No newline at end of file |