Make WordPress Core

Ticket #23625: 23625.4.diff

File 23625.4.diff, 3.0 KB (added by wonderboymusic, 12 years ago)
  • wp-includes/post-formats.php

    diff --git wp-includes/post-formats.php wp-includes/post-formats.php
    index 828447b..0a0bc84 100644
    function post_formats_compat( $content, $id = 0 ) { 
    420420
    421421        return $output;
    422422}
     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 */
     455function get_content_chat( &$content, $remove = false ) {
     456        $trimmed = trim( $content );
     457        if ( empty( $trimmed ) || ! preg_match( '#^(.*?):#', $trimmed ) )
     458                return array();
     459
     460        $last_index = 0;
     461        $stanzas = array();
     462        $lines = explode( "\n", make_clickable( $trimmed ) );
     463
     464        $author = '';
     465        $data = array();
     466        $stanza = array();
     467
     468        foreach ( $lines as $index => $line ) {
     469                $line = trim( $line );
     470
     471                if ( empty( $line ) ) {
     472                        if ( ! empty( $author ) ) {
     473                                $stanza[] = array(
     474                                        'author' => $author,
     475                                        'message' => join( ' ', $data )
     476                                );
     477                        }
     478
     479                        $stanzas[] = $stanza;
     480                        $last_index = $index;
     481                        $stanza = array();
     482                        $author = '';
     483                        $data = array();
     484                        if ( ! empty( $lines[$index + 1] ) && ! strstr( $lines[$index + 1], ':' ) )
     485                                break;
     486                }
     487
     488                $where = strpos( $line, ': ' );
     489                if ( $where && ! preg_match( '#\s#', substr( $line, 0, $where ) ) ) {
     490                        if ( ! empty( $author ) ) {
     491                                $stanza[] = array(
     492                                        'author' => $author,
     493                                        'message' => join( ' ', $data )
     494                                );
     495                                $data = array();
     496                        }
     497
     498                        $chunks = explode( ': ', $line, 2 );
     499                        $author = array_shift( $chunks );
     500                        $data[] = trim( array_shift( $chunks ) );
     501                } elseif ( preg_match( '#\S#', $line ) ) {
     502                        $data[] = $line;
     503                }
     504        }
     505
     506        if ( ! empty( $author ) ) {
     507                $stanza[] = array(
     508                        'author' => $author,
     509                        'message' => trim( join( ' ', $data ) )
     510                );
     511        }
     512
     513        if ( ! empty( $stanza ) )
     514                $stanzas[] = $stanza;
     515
     516        if ( $remove )
     517                $content = trim( join( "\n", array_slice( $lines, $last_index ) ) );
     518
     519        return $stanzas;
     520}
     521
     522/**
     523 * Retrieve structured chat data from the current or passed post
     524 *
     525 * @since 3.6.0
     526 *
     527 * @param int $id Optional. Post ID
     528 * @return array
     529 */
     530function get_the_chat( $id = 0 ) {
     531        $post = empty( $id ) ? get_post() : get_post( $id );
     532        if ( empty( $post ) )
     533                return array();
     534
     535        $data = get_content_chat( $post->post_content );
     536        if ( empty( $data ) )
     537                return array();
     538
     539        return $data;
     540}
     541 No newline at end of file