Ticket #7652: 7652-wpapp-done.diff

File 7652-wpapp-done.diff, 9.1 KB (added by rmccue, 2 years ago)

wp-app.php should be done now

Line 
1Index: wp-app.php
2===================================================================
3--- wp-app.php  (revision 16544)
4+++ wp-app.php  (working copy)
5@@ -16,7 +16,7 @@
6 require_once('./wp-load.php');
7 
8 /** Atom Publishing Protocol Class */
9-require_once(ABSPATH . WPINC . '/atomlib.php');
10+require_once(ABSPATH . WPINC . '/class-simplepie.php');
11 
12 /** Admin Image API for metadata updating */
13 require_once(ABSPATH . '/wp-admin/includes/image.php');
14@@ -76,7 +76,93 @@
15 }
16 add_filter('posts_where', 'wa_posts_where_include_drafts_filter');
17 
18+define('SIMPLEPIE_NAMESPACE_ATOMPUB', 'http://www.w3.org/2007/app');
19+
20 /**
21+ * SimplePie Helper for AtomPub
22+ *
23+ * @package WordPress
24+ * @subpackage Publishing
25+ * @since 3.1
26+ */
27+class SimplePieAtomPub_Item extends SimplePie_Item {
28+       /**
29+        * Constructor
30+        */
31+       function SimplePieAtomPub_Item($feed, $data) {
32+               parent::SimplePie_Item($feed, $data);
33+       }
34+
35+       /**
36+        * Get the status of the entry
37+        *
38+        * @return bool True if the item is a draft, false otherwise
39+        */
40+       function get_draft_status() {
41+               $draft = false;
42+               if (($control = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOMPUB, 'control')) && !empty($control[0]['child'][SIMPLEPIE_NAMESPACE_ATOMPUB]['draft'][0]['data'])) {
43+                       $draft = ('yes' == $control[0]['child'][SIMPLEPIE_NAMESPACE_ATOMPUB]['draft'][0]['data']);
44+               }
45+               return $draft;
46+       }
47+
48+       /**
49+        * Get the GMT timestamp of the entry
50+        *
51+        * @param string $format date() format
52+        * @return int|string|null
53+        */
54+       function get_gmdate($format = 'j F Y, g:i a') {
55+               return gmdate($format, $this->get_date('U'));
56+       }
57+
58+       /**
59+        * Get the updated timestamp of the entry
60+        *
61+        * AtomPub needs the distinction between "created" and "updated".
62+        * @param string $format date() format
63+        * @return string|int|null
64+        */
65+       function get_updated($format = 'j F Y, g:i a') {
66+               if ($updated = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'updated')) {
67+                       $return = date('Y-m-d H:i:s', $updated[0]['data']);
68+               }
69+               else {
70+                       return null;
71+               }
72+
73+               if ($return) {
74+                       $parser = SimplePie_Parse_Date::get();
75+                       $parsed = $parser->parse($return);
76+                       $date_format = (string) $date_format;
77+                       switch ($date_format) {
78+                               case '':
79+                                       return $this->sanitize($return, SIMPLEPIE_CONSTRUCT_TEXT);
80+
81+                               case 'U':
82+                                       return $parsed;
83+
84+                               default:
85+                                       return date($date_format, $parsed);
86+                       }
87+               }
88+               else {
89+                       return null;
90+               }
91+       }
92+
93+       /**
94+        * Get the updated GMT timestamp of the entry
95+        *
96+        * @param string $format date() format
97+        * @return int|string|null
98+        */
99+       function get_gmupdated($format = 'j F Y, g:i a') {
100+               return gmdate($format, $this->get_updated('U'));
101+       }
102+}
103+
104+/**
105  * WordPress AtomPub API implementation.
106  *
107  * @package WordPress
108@@ -258,8 +344,6 @@
109         * @since 2.2.0
110         */
111        function handle_request() {
112-               global $always_authenticate;
113-
114                if ( !empty( $_SERVER['ORIG_PATH_INFO'] ) )
115                        $path = $_SERVER['ORIG_PATH_INFO'];
116                else
117@@ -294,8 +378,7 @@
118                                        // authenticate regardless of the operation and set the current
119                                        // user. each handler will decide if auth is required or not.
120                                        if ( !$this->authenticate() ) {
121-                                               if ( $always_authenticate )
122-                                                       $this->auth_required('Credentials required.');
123+                                               $this->auth_required('Credentials required.');
124                                        }
125 
126                                        array_shift($matches);
127@@ -390,17 +473,14 @@
128                global $user_ID;
129                $this->get_accepted_content_type($this->atom_content_types);
130 
131-               $parser = new AtomParser();
132-               if ( !$parser->parse() )
133-                       $this->client_error();
134+               $feed = $this->get_parser();
135+               $entry = $feed->get_item(0);
136 
137-               $entry = array_pop($parser->feed->entries);
138+               log_app('Received entry:', $entry->get_title());
139 
140-               log_app('Received entry:', print_r($entry,true));
141-
142                $catnames = array();
143-               foreach ( $entry->categories as $cat ) {
144-                       array_push($catnames, $cat["term"]);
145+               foreach ( (array) $entry->get_categories() as $category ) {
146+                       array_push($catnames, $category->get_term());
147                }
148 
149                $wp_cats = get_categories(array('hide_empty' => false));
150@@ -412,7 +492,7 @@
151                                array_push($post_category, $cat->term_id);
152                }
153 
154-               $publish = ! ( isset( $entry->draft ) && 'yes' == trim( $entry->draft ) );
155+               $publish = !$entry->get_draft_status();
156 
157                $cap = ($publish) ? 'publish_posts' : 'edit_posts';
158 
159@@ -422,12 +502,11 @@
160                $blog_ID = get_current_blog_id();
161                $post_status = ($publish) ? 'publish' : 'draft';
162                $post_author = (int) $user_ID;
163-               $post_title = $entry->title[1];
164-               $post_content = $entry->content[1];
165-               $post_excerpt = $entry->summary[1];
166-               $pubtimes = $this->get_publish_time($entry->published);
167-               $post_date = $pubtimes[0];
168-               $post_date_gmt = $pubtimes[1];
169+               $post_title = $entry->get_title();
170+               $post_content = $entry->get_content();
171+               $post_excerpt = $entry->get_description();
172+               $post_date = $entry->get_date('Y-m-d H:i:s');
173+               $post_date_gmt = $entry->get_gmdate('Y-m-d H:i:s');
174 
175                if ( isset( $_SERVER['HTTP_SLUG'] ) )
176                        $post_name = $_SERVER['HTTP_SLUG'];
177@@ -490,14 +569,11 @@
178                // quick check and exit
179                $this->get_accepted_content_type($this->atom_content_types);
180 
181-               $parser = new AtomParser();
182-               if ( !$parser->parse() )
183-                       $this->bad_request();
184+               $feed = $this->get_parser();
185+               $parsed = $feed->get_item(0);
186 
187-               $parsed = array_pop($parser->feed->entries);
188+               log_app('Received UPDATED entry:', $parsed->get_title());
189 
190-               log_app('Received UPDATED entry:', print_r($parsed,true));
191-
192                // check for not found
193                global $entry;
194                $this->set_current_entry($postID);
195@@ -505,21 +581,21 @@
196                if ( !current_user_can('edit_post', $entry['ID']) )
197                        $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
198 
199-               $publish = ! ( isset($parsed->draft) && 'yes' == trim($parsed->draft) );
200+               $publish = !$parsed->get_draft_status();
201                $post_status = ($publish) ? 'publish' : 'draft';
202 
203                extract($entry);
204 
205-               $post_title = $parsed->title[1];
206-               $post_content = $parsed->content[1];
207-               $post_excerpt = $parsed->summary[1];
208-               $pubtimes = $this->get_publish_time($entry->published);
209-               $post_date = $pubtimes[0];
210-               $post_date_gmt = $pubtimes[1];
211-               $pubtimes = $this->get_publish_time($parsed->updated);
212-               $post_modified = $pubtimes[0];
213-               $post_modified_gmt = $pubtimes[1];
214+               $post_title = $parsed->get_title();
215+               $post_content = $parsed->get_content();
216+               $post_excerpt = $parsed->get_description();
217 
218+               $post_date = $parsed->get_date('Y-m-d H:i:s');
219+               $post_date_gmt = $parsed->get_gmdate('Y-m-d H:i:s');
220+
221+               $post_modified =  $parsed->get_updated('Y-m-d H:i:s');
222+               $post_modified_gmt =  $parsed->get_gmupdated('Y-m-d H:i:s');
223+
224                $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt');
225                $this->escape($postdata);
226 
227@@ -657,13 +733,9 @@
228                // quick check and exit
229                $this->get_accepted_content_type($this->atom_content_types);
230 
231-               $parser = new AtomParser();
232-               if (!$parser->parse()) {
233-                       $this->bad_request();
234-               }
235+               $feed = $this->get_parser();
236+               $parsed = $feed->get_item(0);
237 
238-               $parsed = array_pop($parser->feed->entries);
239-
240                // check for not found
241                global $entry;
242                $this->set_current_entry($postID);
243@@ -673,11 +745,10 @@
244 
245                extract($entry);
246 
247-               $post_title = $parsed->title[1];
248-               $post_content = $parsed->summary[1];
249-               $pubtimes = $this->get_publish_time($parsed->updated);
250-               $post_modified = $pubtimes[0];
251-               $post_modified_gmt = $pubtimes[1];
252+               $post_title = $parsed->get_title();
253+               $post_content = $parsed->get_description();
254+               $post_modified = $parsed->get_date('Y-m-d H:i:s');
255+               $post_modified_gmt = $parsed->get_gmdate('Y-m-d H:i:s');
256 
257                $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_modified', 'post_modified_gmt');
258                $this->escape($postdata);
259@@ -1232,6 +1303,7 @@
260                log_app('Status','400: Bad Request');
261                header('Content-Type: text/plain');
262                status_header('400');
263+               echo $msg;
264                exit;
265        }
266 
267@@ -1336,6 +1408,7 @@
268                log_app('Status','400: Client Error');
269                header('Content-Type: text/plain');
270                status_header('400');
271+               echo $msg;
272                exit;
273        }
274 
275@@ -1448,6 +1521,8 @@
276         * @return bool
277         */
278        function authenticate() {
279+               global $always_authenticate;
280+
281                log_app("authenticate()",print_r($_ENV, true));
282 
283                // if using mod_rewrite/ENV hack
284@@ -1474,6 +1549,12 @@
285                        }
286                }
287 
288+               // If we're forcing admin abilities
289+               if (!$always_authenticate) {
290+                       wp_set_current_user(1);
291+                       return true;
292+               }
293+
294                return false;
295        }
296 
297@@ -1598,6 +1679,27 @@
298                }
299        }
300 
301+       /**
302+        * Create a SimplePie parser with POST data
303+        *
304+        * @return SimplePie
305+        */
306+       function &get_parser() {
307+               $data = file_get_contents('php://input');
308+               // SimplePie expects the feed element to be the top element
309+               // This could probably be improved
310+               if (strpos($data, '<feed') === false) {
311+                       $data = str_replace('<entry', '<feed xmlns="' . SIMPLEPIE_NAMESPACE_ATOM_10 . '"><entry', $data);
312+                       $data = str_replace('</entry>', '</entry></feed>', $data);
313+               }
314+               $feed = new SimplePie();
315+               $feed->set_item_class('SimplePieAtomPub_Item');
316+               $feed->set_raw_data($data);
317+               $feed->init();
318+               if ( $feed->error() )
319+                       $this->bad_request($feed->error());
320+               return $feed;
321+       }
322 }
323 
324 /**