Ticket #23625: 23625.3.diff

File 23625.3.diff, 2.9 KB (added by wonderboymusic, 2 months ago)
Line 
1diff --git wp-includes/post-formats.php wp-includes/post-formats.php
2index 828447b..17689f3 100644
3--- wp-includes/post-formats.php
4+++ wp-includes/post-formats.php
5@@ -420,3 +420,120 @@ 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+       if ( empty( $content ) || ! preg_match( '#^(.*?):#', $content ) )
43+               return array();
44+
45+       $last_index = 0;
46+       $stanzas = array();
47+       $lines = explode( "\n", make_clickable( $content ) );
48+
49+       $author = '';
50+       $data = array();
51+       $stanza = array();
52+
53+       foreach ( $lines as $index => $line ) {
54+               $line = trim( $line );
55+
56+               if ( empty( $line ) ) {
57+                       if ( ! empty( $author ) ) {
58+                               $stanza[] = array(
59+                                       'author' => $author,
60+                                       'message' => join( ' ', $data )
61+                               );
62+                       }
63+
64+                       $stanzas[] = $stanza;
65+                       $last_index = $index;
66+                       $stanza = array();
67+                       $author = '';
68+                       $data = array();
69+                       if ( ! empty( $lines[$index + 1] ) && ! strstr( $lines[$index + 1], ':' ) )
70+                               break;
71+               }
72+
73+               $where = strpos( $line, ': ' );
74+               if ( $where && ! preg_match( '#\s#', substr( $line, 0, $where ) ) ) {
75+                       if ( ! empty( $author ) ) {
76+                               $stanza[] = array(
77+                                       'author' => $author,
78+                                       'message' => join( ' ', $data )
79+                               );
80+                               $data = array();
81+                       }
82+
83+                       $chunks = explode( ': ', $line, 2 );
84+                       $author = array_shift( $chunks );
85+                       $data[] = array_shift( $chunks );
86+               } else {
87+                       $data[] = trim( $line );
88+               }
89+       }
90+
91+       if ( ! empty( $author ) ) {
92+               $stanza[] = array(
93+                       'author' => $author,
94+                       'message' => join( ' ', $data )
95+               );
96+       }
97+
98+       if ( ! empty( $stanza ) )
99+               $stanzas[] = $stanza;
100+
101+       if ( $remove )
102+               $content = trim( join( "\n", array_slice( $lines, $last_index ) ) );
103+
104+       return $stanzas;
105+}
106+
107+/**
108+ * Retrieve structured chat data from the current or passed post
109+ *
110+ * @since 3.6.0
111+ *
112+ * @param int $id Optional. Post ID
113+ * @return array
114+ */
115+function get_the_chat( $id = 0 ) {
116+       $post = empty( $id ) ? get_post() : get_post( $id );
117+       if ( empty( $post ) )
118+               return array();
119+
120+       $data = get_content_chat( $post->post_content );
121+       if ( empty( $data ) )
122+               return array();
123+
124+       return $data;
125+}
126\ No newline at end of file