Ticket #23625: 23625.7.diff

File 23625.7.diff, 3.7 KB (added by wonderboymusic, 2 months ago)
Line 
1diff --git wp-includes/post-formats.php wp-includes/post-formats.php
2index 3896d11..903d3e9 100644
3--- wp-includes/post-formats.php
4+++ wp-includes/post-formats.php
5@@ -420,3 +420,134 @@ 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+ *             'time' => '',
29+ *             'author' => 'Scott',
30+ *             'messsage' => "Hey, let's chat!"
31+ *         ),
32+ *         array(
33+ *             'time' => '',
34+ *             'author' => 'Helen',
35+ *             'message' => 'No.'
36+ *         )
37+ *     )
38+ * )
39+ * @param string $content A string which might contain chat data.
40+ * @param boolean $remove Whether to remove the found data from the passed content.
41+ * @return array A chat log as structured data
42+ */
43+function get_content_chat( &$content, $remove = false ) {
44+       $trimmed = trim( $content );
45+       $newline_regex = '#^(\[.+?\])?\s?(?:(?:([^:<>]+):)|(?:<([^>\s]+)>))#';
46+
47+       if ( empty( $trimmed ) || ! preg_match( $newline_regex, $trimmed ) )
48+               return array();
49+
50+       $last_index = 0;
51+       $stanzas = array();
52+       $lines = explode( "\n", make_clickable( $trimmed ) );
53+
54+       $author = $time = '';
55+       $data = array();
56+       $stanza = array();
57+
58+       foreach ( $lines as $index => $line ) {
59+               $line = trim( $line );
60+
61+               if ( empty( $line ) ) {
62+                       if ( ! empty( $author ) ) {
63+                               $stanza[] = array(
64+                                       'time' => $time,
65+                                       'author' => $author,
66+                                       'message' => join( ' ', $data )
67+                               );
68+                       }
69+
70+                       $stanzas[] = $stanza;
71+                       $last_index = $index;
72+                       $stanza = array();
73+                       $author = $time = '';
74+                       $data = array();
75+                       $commentary = ! preg_match( '#[:]#', $lines[$index + 1] ) && ! preg_match( '#<[^>\s]+>#', $lines[$index + 1] );
76+                       if ( ! empty( $lines[$index + 1] ) && $commentary )
77+                               break;
78+               }
79+
80+               $matches = array();
81+               $matched = preg_match( $newline_regex, $line, $matches );
82+               // assume username syntax if no whitespace is present
83+               $no_ws = $matched && ! preg_match( '#\s#', $matches[2] );
84+               // allow script-like stanzas
85+               $has_ws = $matched && preg_match( '#\s#', $matches[2] ) && empty( $lines[$index + 1] ) && empty( $lines[$index - 1] );
86+               if ( $matched && ( ! empty( $matches[1] ) || ( ! empty( $matches[3] ) || $no_ws || $has_ws ) ) ) {
87+                       if ( ! empty( $author ) ) {
88+                               $stanza[] = array(
89+                                       'time' => $time,
90+                                       'author' => $author,
91+                                       'message' => join( ' ', $data )
92+                               );
93+                               $data = array();
94+                       }
95+
96+                       $time = $matches[1];
97+                       $author = empty( $matches[3] ) ? $matches[2] : $matches[3];
98+                       $data[] = trim( str_replace( $matches[0], '', $line ) );
99+               } elseif ( preg_match( '#\S#', $line ) ) {
100+                       $data[] = $line;
101+               }
102+       }
103+
104+       if ( ! empty( $author ) ) {
105+               $stanza[] = array(
106+                       'time' => $time,
107+                       'author' => $author,
108+                       'message' => trim( join( ' ', $data ) )
109+               );
110+       }
111+
112+       if ( ! empty( $stanza ) )
113+               $stanzas[] = $stanza;
114+
115+       if ( $remove )
116+               $content = trim( join( "\n", array_slice( $lines, $last_index ) ) );
117+
118+       return $stanzas;
119+}
120+
121+/**
122+ * Retrieve structured chat data from the current or passed post
123+ *
124+ * @since 3.6.0
125+ *
126+ * @param int $id Optional. Post ID
127+ * @return array
128+ */
129+function get_the_chat( $id = 0 ) {
130+       $post = empty( $id ) ? get_post() : get_post( $id );
131+       if ( empty( $post ) )
132+               return array();
133+
134+       $data = get_content_chat( $post->post_content );
135+       if ( empty( $data ) )
136+               return array();
137+
138+       return $data;
139+}