Make WordPress Core

Ticket #23625: 23625.diff

File 23625.diff, 2.9 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 6d32aea..ef1abf0 100644
    function post_formats_compat( $content, $id = 0 ) { 
    427427
    428428        return $output;
    429429}
     430
     431/**
     432 * Deliberately interpret passed content as a chat transcript that is optionally
     433 * followed by commentary
     434 *
     435 * If the content does not contain username syntax, assume that it does not contain
     436 * chat logs and return
     437 *
     438 * @since 3.6.0
     439 *
     440 * @param string $content A string which might contain chat data.
     441 * @param boolean $remove Whether to remove the found data from the passed content.
     442 * @return array A chat log as structured data
     443 *
     444 * Example:
     445 *
     446 * One stanza of chat:
     447 * Scott: Hey, let's chat!
     448 * Helen: No.
     449 *
     450 * $stanzas = array(
     451 *     array(
     452 *         array(
     453 *             'author' => 'Scott',
     454 *             'messsage' => "Hey, let's chat!"
     455 *         ),
     456 *         array(
     457 *             'author' => 'Helen',
     458 *             'message' => 'No.'
     459 *         )
     460 *     )
     461 * )
     462 */
     463function get_content_chat( &$content, $remove = false ) {
     464        if ( empty( $content ) || ! preg_match( '#\S+: \S+#', $content ) )
     465                return array();
     466
     467        $last_index = 0;
     468        $stanzas = array();
     469        $lines = explode( "\n", $content );
     470
     471        $author = '';
     472        $data = array();
     473        $stanza = array();
     474
     475        foreach ( $lines as $index => $line ) {
     476                $line = trim( $line );
     477
     478                if ( empty( $line ) ) {
     479                        if ( ! empty( $author ) ) {
     480                                $stanza[] = array(
     481                                        'author' => $author,
     482                                        'message' => join( ' ', $data )
     483                                );
     484                        }
     485
     486                        $stanzas[] = $stanza;
     487                        $last_index = $index;
     488                        if ( ! empty( $lines[$index + 1] ) && ! strstr( $lines[$index + 1], ':' ) )
     489                                break;
     490
     491                        $stanza = array();
     492                        $author = '';
     493                        $data = array();
     494                }
     495
     496                if ( strstr( $line, ': ' ) ) {
     497                        if ( ! empty( $author ) ) {
     498                                $stanza[] = array(
     499                                        'author' => $author,
     500                                        'message' => join( ' ', $data )
     501                                );
     502                                $data = array();
     503                        }
     504
     505                        $chunks = explode( ': ', $line );
     506                        $author = array_shift( $chunks );
     507                        $data[] = trim( join( ': ', $chunks ) );
     508                } else {
     509                        $data[] = trim( $line );
     510                }
     511        }
     512
     513        if ( ! empty( $author ) ) {
     514                $stanza[] = array(
     515                        'author' => $author,
     516                        'message' => join( ' ', $data )
     517                );
     518        }
     519
     520        if ( empty( $stanzas ) && ! empty( $stanza ) )
     521                $stanzas[] = $stanza;
     522
     523        if ( $remove )
     524                $content = trim( join( "\n", array_slice( $lines, $last_index ) ) );
     525
     526        return $stanzas;
     527}
     528
     529/**
     530 * Retrieve structured chat data from the current or passed post
     531 *
     532 * @since 3.6.0
     533 *
     534 * @param type $id
     535 * @return type
     536 */
     537function get_the_chat( $id = 0 ) {
     538        $post = empty( $id ) ? get_post() : get_post( $id );
     539        if ( empty( $post ) )
     540                return array();
     541
     542        $data = get_content_chat( $post->post_content );
     543        if ( empty( $data ) )
     544                return array();
     545
     546        return $data;
     547}
     548 No newline at end of file