| 1 | diff --git wp-includes/post-formats.php wp-includes/post-formats.php |
|---|
| 2 | index 828447b..0a0bc84 100644 |
|---|
| 3 | --- wp-includes/post-formats.php |
|---|
| 4 | +++ wp-includes/post-formats.php |
|---|
| 5 | @@ -420,3 +420,121 @@ function post_formats_compat( $content, $id = 0 ) { |
|---|
| 6 | |
|---|
| 7 | return $output; |
|---|
| 8 | } |
|---|
| 9 | + |
|---|
| 10 | +/** |
|---|
| 11 | + * Deliberately interpret passed content as a chat transcript that is optionally |
|---|
| 12 | + * followed by commentary |
|---|
| 13 | + * |
|---|
| 14 | + * If the content does not contain username syntax, assume that it does not contain |
|---|
| 15 | + * chat logs and return |
|---|
| 16 | + * |
|---|
| 17 | + * @since 3.6.0 |
|---|
| 18 | + * |
|---|
| 19 | + * Example: |
|---|
| 20 | + * |
|---|
| 21 | + * One stanza of chat: |
|---|
| 22 | + * Scott: Hey, let's chat! |
|---|
| 23 | + * Helen: No. |
|---|
| 24 | + * |
|---|
| 25 | + * $stanzas = array( |
|---|
| 26 | + * array( |
|---|
| 27 | + * array( |
|---|
| 28 | + * 'author' => 'Scott', |
|---|
| 29 | + * 'messsage' => "Hey, let's chat!" |
|---|
| 30 | + * ), |
|---|
| 31 | + * array( |
|---|
| 32 | + * 'author' => 'Helen', |
|---|
| 33 | + * 'message' => 'No.' |
|---|
| 34 | + * ) |
|---|
| 35 | + * ) |
|---|
| 36 | + * ) |
|---|
| 37 | + * @param string $content A string which might contain chat data. |
|---|
| 38 | + * @param boolean $remove Whether to remove the found data from the passed content. |
|---|
| 39 | + * @return array A chat log as structured data |
|---|
| 40 | + */ |
|---|
| 41 | +function get_content_chat( &$content, $remove = false ) { |
|---|
| 42 | + $trimmed = trim( $content ); |
|---|
| 43 | + if ( empty( $trimmed ) || ! preg_match( '#^(.*?):#', $trimmed ) ) |
|---|
| 44 | + return array(); |
|---|
| 45 | + |
|---|
| 46 | + $last_index = 0; |
|---|
| 47 | + $stanzas = array(); |
|---|
| 48 | + $lines = explode( "\n", make_clickable( $trimmed ) ); |
|---|
| 49 | + |
|---|
| 50 | + $author = ''; |
|---|
| 51 | + $data = array(); |
|---|
| 52 | + $stanza = array(); |
|---|
| 53 | + |
|---|
| 54 | + foreach ( $lines as $index => $line ) { |
|---|
| 55 | + $line = trim( $line ); |
|---|
| 56 | + |
|---|
| 57 | + if ( empty( $line ) ) { |
|---|
| 58 | + if ( ! empty( $author ) ) { |
|---|
| 59 | + $stanza[] = array( |
|---|
| 60 | + 'author' => $author, |
|---|
| 61 | + 'message' => join( ' ', $data ) |
|---|
| 62 | + ); |
|---|
| 63 | + } |
|---|
| 64 | + |
|---|
| 65 | + $stanzas[] = $stanza; |
|---|
| 66 | + $last_index = $index; |
|---|
| 67 | + $stanza = array(); |
|---|
| 68 | + $author = ''; |
|---|
| 69 | + $data = array(); |
|---|
| 70 | + if ( ! empty( $lines[$index + 1] ) && ! strstr( $lines[$index + 1], ':' ) ) |
|---|
| 71 | + break; |
|---|
| 72 | + } |
|---|
| 73 | + |
|---|
| 74 | + $where = strpos( $line, ': ' ); |
|---|
| 75 | + if ( $where && ! preg_match( '#\s#', substr( $line, 0, $where ) ) ) { |
|---|
| 76 | + if ( ! empty( $author ) ) { |
|---|
| 77 | + $stanza[] = array( |
|---|
| 78 | + 'author' => $author, |
|---|
| 79 | + 'message' => join( ' ', $data ) |
|---|
| 80 | + ); |
|---|
| 81 | + $data = array(); |
|---|
| 82 | + } |
|---|
| 83 | + |
|---|
| 84 | + $chunks = explode( ': ', $line, 2 ); |
|---|
| 85 | + $author = array_shift( $chunks ); |
|---|
| 86 | + $data[] = trim( array_shift( $chunks ) ); |
|---|
| 87 | + } elseif ( preg_match( '#\S#', $line ) ) { |
|---|
| 88 | + $data[] = $line; |
|---|
| 89 | + } |
|---|
| 90 | + } |
|---|
| 91 | + |
|---|
| 92 | + if ( ! empty( $author ) ) { |
|---|
| 93 | + $stanza[] = array( |
|---|
| 94 | + 'author' => $author, |
|---|
| 95 | + 'message' => trim( join( ' ', $data ) ) |
|---|
| 96 | + ); |
|---|
| 97 | + } |
|---|
| 98 | + |
|---|
| 99 | + if ( ! empty( $stanza ) ) |
|---|
| 100 | + $stanzas[] = $stanza; |
|---|
| 101 | + |
|---|
| 102 | + if ( $remove ) |
|---|
| 103 | + $content = trim( join( "\n", array_slice( $lines, $last_index ) ) ); |
|---|
| 104 | + |
|---|
| 105 | + return $stanzas; |
|---|
| 106 | +} |
|---|
| 107 | + |
|---|
| 108 | +/** |
|---|
| 109 | + * Retrieve structured chat data from the current or passed post |
|---|
| 110 | + * |
|---|
| 111 | + * @since 3.6.0 |
|---|
| 112 | + * |
|---|
| 113 | + * @param int $id Optional. Post ID |
|---|
| 114 | + * @return array |
|---|
| 115 | + */ |
|---|
| 116 | +function get_the_chat( $id = 0 ) { |
|---|
| 117 | + $post = empty( $id ) ? get_post() : get_post( $id ); |
|---|
| 118 | + if ( empty( $post ) ) |
|---|
| 119 | + return array(); |
|---|
| 120 | + |
|---|
| 121 | + $data = get_content_chat( $post->post_content ); |
|---|
| 122 | + if ( empty( $data ) ) |
|---|
| 123 | + return array(); |
|---|
| 124 | + |
|---|
| 125 | + return $data; |
|---|
| 126 | +} |
|---|
| 127 | \ No newline at end of file |
|---|