Ticket #7652: 7652-wpapp.diff
File 7652-wpapp.diff, 8.8 KB (added by , 14 years ago) |
---|
-
wp-app.php
16 16 require_once('./wp-load.php'); 17 17 18 18 /** Atom Publishing Protocol Class */ 19 require_once(ABSPATH . WPINC . '/ atomlib.php');19 require_once(ABSPATH . WPINC . '/class-simplepie.php'); 20 20 21 21 /** Admin Image API for metadata updating */ 22 22 require_once(ABSPATH . '/wp-admin/includes/image.php'); … … 76 76 } 77 77 add_filter('posts_where', 'wa_posts_where_include_drafts_filter'); 78 78 79 define('SIMPLEPIE_NAMESPACE_ATOMPUB', 'http://www.w3.org/2007/app'); 80 79 81 /** 82 * SimplePie Helper for AtomPub 83 * 84 * @package WordPress 85 * @subpackage Publishing 86 * @since 3.1 87 */ 88 class SimplePieAtomPub_Item extends SimplePie_Item { 89 /** 90 * Constructor 91 */ 92 function SimplePieAtomPub_Item($feed, $data) { 93 parent::SimplePie_Item($feed, $data); 94 } 95 96 /** 97 * Get the status of the entry 98 * 99 * @return bool True if the item is a draft, false otherwise 100 */ 101 function get_draft_status() { 102 $draft = false; 103 if (($control = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOMPUB, 'control')) && !empty($control[0]['child'][SIMPLEPIE_NAMESPACE_ATOMPUB]['draft'][0]['data'])) { 104 $draft = ('yes' == $control[0]['child'][SIMPLEPIE_NAMESPACE_ATOMPUB]['draft'][0]['data']); 105 } 106 return $draft; 107 } 108 109 /** 110 * Get the GMT timestamp of the entry 111 * 112 * @param string $format date() format 113 * @return int|string|null 114 */ 115 function get_gmdate($format = 'j F Y, g:i a') { 116 return gmdate($format, $this->get_date('U')); 117 } 118 119 /** 120 * Get the updated timestamp of the entry 121 * 122 * AtomPub needs the distinction between "created" and "updated". 123 * @param string $format date() format 124 * @return string|int|null 125 */ 126 function get_updated($format = 'j F Y, g:i a') { 127 if ($updated = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'updated')) { 128 $return = date('Y-m-d H:i:s', $updated[0]['data']); 129 } 130 else { 131 return null; 132 } 133 134 if ($return) { 135 $parser = SimplePie_Parse_Date::get(); 136 $parsed = $parser->parse($return); 137 $date_format = (string) $date_format; 138 switch ($date_format) { 139 case '': 140 return $this->sanitize($return, SIMPLEPIE_CONSTRUCT_TEXT); 141 142 case 'U': 143 return $parsed; 144 145 default: 146 return date($date_format, $parsed); 147 } 148 } 149 else { 150 return null; 151 } 152 } 153 154 /** 155 * Get the updated GMT timestamp of the entry 156 * 157 * @param string $format date() format 158 * @return int|string|null 159 */ 160 function get_gmupdated($format = 'j F Y, g:i a') { 161 return gmdate($format, $this->get_updated('U')); 162 } 163 } 164 165 /** 80 166 * WordPress AtomPub API implementation. 81 167 * 82 168 * @package WordPress … … 258 344 * @since 2.2.0 259 345 */ 260 346 function handle_request() { 261 global $always_authenticate;262 263 347 if ( !empty( $_SERVER['ORIG_PATH_INFO'] ) ) 264 348 $path = $_SERVER['ORIG_PATH_INFO']; 265 349 else … … 294 378 // authenticate regardless of the operation and set the current 295 379 // user. each handler will decide if auth is required or not. 296 380 if ( !$this->authenticate() ) { 297 if ( $always_authenticate ) 298 $this->auth_required('Credentials required.'); 381 $this->auth_required('Credentials required.'); 299 382 } 300 383 301 384 array_shift($matches); … … 390 473 global $user_ID; 391 474 $this->get_accepted_content_type($this->atom_content_types); 392 475 393 $parser = new AtomParser(); 394 if ( !$parser->parse() ) 395 $this->client_error(); 476 $feed = $this->get_parser(); 477 $entry = $feed->get_item(0); 396 478 397 $entry = array_pop($parser->feed->entries);479 log_app('Received entry:', $entry->get_title()); 398 480 399 log_app('Received entry:', print_r($entry,true));400 401 481 $catnames = array(); 402 foreach ( $entry->categories as $cat) {403 array_push($catnames, $cat ["term"]);482 foreach ( (array) $entry->get_categories() as $category ) { 483 array_push($catnames, $category->get_term()); 404 484 } 405 485 406 486 $wp_cats = get_categories(array('hide_empty' => false)); … … 412 492 array_push($post_category, $cat->term_id); 413 493 } 414 494 415 $publish = ! ( isset( $entry->draft ) && 'yes' == trim( $entry->draft ));495 $publish = !$entry->get_draft_status(); 416 496 417 497 $cap = ($publish) ? 'publish_posts' : 'edit_posts'; 418 498 … … 422 502 $blog_ID = get_current_blog_id(); 423 503 $post_status = ($publish) ? 'publish' : 'draft'; 424 504 $post_author = (int) $user_ID; 425 $post_title = $entry->title[1]; 426 $post_content = $entry->content[1]; 427 $post_excerpt = $entry->summary[1]; 428 $pubtimes = $this->get_publish_time($entry->published); 429 $post_date = $pubtimes[0]; 430 $post_date_gmt = $pubtimes[1]; 505 $post_title = $entry->get_title(); 506 $post_content = $entry->get_content(); 507 $post_excerpt = $entry->get_description(); 508 $post_date = $entry->get_date('Y-m-d H:i:s'); 509 $post_date_gmt = $entry->get_gmdate('Y-m-d H:i:s'); 431 510 432 511 if ( isset( $_SERVER['HTTP_SLUG'] ) ) 433 512 $post_name = $_SERVER['HTTP_SLUG']; … … 490 569 // quick check and exit 491 570 $this->get_accepted_content_type($this->atom_content_types); 492 571 493 $parser = new AtomParser(); 494 if ( !$parser->parse() ) 495 $this->bad_request(); 572 $feed = $this->get_parser(); 573 $parsed = $feed->get_item(0); 496 574 497 $parsed = array_pop($parser->feed->entries);498 499 575 log_app('Received UPDATED entry:', print_r($parsed,true)); 500 576 501 577 // check for not found … … 505 581 if ( !current_user_can('edit_post', $entry['ID']) ) 506 582 $this->auth_required(__('Sorry, you do not have the right to edit this post.')); 507 583 508 $publish = ! ( isset($parsed->draft) && 'yes' == trim($parsed->draft));584 $publish = !$parsed->get_draft_status(); 509 585 $post_status = ($publish) ? 'publish' : 'draft'; 510 586 511 587 extract($entry); 512 588 513 $post_title = $parsed->title[1]; 514 $post_content = $parsed->content[1]; 515 $post_excerpt = $parsed->summary[1]; 516 $pubtimes = $this->get_publish_time($entry->published); 517 $post_date = $pubtimes[0]; 518 $post_date_gmt = $pubtimes[1]; 519 $pubtimes = $this->get_publish_time($parsed->updated); 520 $post_modified = $pubtimes[0]; 521 $post_modified_gmt = $pubtimes[1]; 589 $post_title = $parsed->get_title(); 590 $post_content = $parsed->get_content(); 591 $post_excerpt = $parsed->get_description(); 522 592 593 $post_date = $parsed->get_date('Y-m-d H:i:s'); 594 $post_date_gmt = $parsed->get_gmdate('Y-m-d H:i:s'); 595 596 $post_modified = $parsed->get_updated('Y-m-d H:i:s'); 597 $post_modified_gmt = $parsed->get_gmupdated('Y-m-d H:i:s'); 598 523 599 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt'); 524 600 $this->escape($postdata); 525 601 … … 657 733 // quick check and exit 658 734 $this->get_accepted_content_type($this->atom_content_types); 659 735 660 $parser = new AtomParser(); 661 if (!$parser->parse()) { 662 $this->bad_request(); 663 } 736 $feed = $this->get_parser(); 737 $parsed = $feed->get_item(0); 664 738 665 $parsed = array_pop($parser->feed->entries);666 667 739 // check for not found 668 740 global $entry; 669 741 $this->set_current_entry($postID); … … 673 745 674 746 extract($entry); 675 747 676 $post_title = $parsed->title[1]; 677 $post_content = $parsed->summary[1]; 678 $pubtimes = $this->get_publish_time($parsed->updated); 679 $post_modified = $pubtimes[0]; 680 $post_modified_gmt = $pubtimes[1]; 748 $post_title = $parsed->get_title(); 749 $post_content = $parsed->get_description(); 750 $post_modified = $parsed->get_date('Y-m-d H:i:s'); 751 $post_modified_gmt = $parsed->get_gmdate('Y-m-d H:i:s'); 681 752 682 753 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_modified', 'post_modified_gmt'); 683 754 $this->escape($postdata); … … 1232 1303 log_app('Status','400: Bad Request'); 1233 1304 header('Content-Type: text/plain'); 1234 1305 status_header('400'); 1306 echo $msg; 1235 1307 exit; 1236 1308 } 1237 1309 … … 1336 1408 log_app('Status','400: Client Error'); 1337 1409 header('Content-Type: text/plain'); 1338 1410 status_header('400'); 1411 echo $msg; 1339 1412 exit; 1340 1413 } 1341 1414 … … 1448 1521 * @return bool 1449 1522 */ 1450 1523 function authenticate() { 1524 global $always_authenticate; 1525 1451 1526 log_app("authenticate()",print_r($_ENV, true)); 1452 1527 1453 1528 // if using mod_rewrite/ENV hack … … 1474 1549 } 1475 1550 } 1476 1551 1552 // If we're forcing admin abilities 1553 if (!$always_authenticate) { 1554 wp_set_current_user(1); 1555 return true; 1556 } 1557 1477 1558 return false; 1478 1559 } 1479 1560 … … 1598 1679 } 1599 1680 } 1600 1681 1682 function &get_parser() { 1683 $data = file_get_contents('php://input'); 1684 // SimplePie expects the feed element to be the top element 1685 if (strpos($data, '<feed') === false) { 1686 $data = str_replace('<entry', '<feed xmlns="' . SIMPLEPIE_NAMESPACE_ATOM_10 . '"><entry', $data); 1687 $data = str_replace('</entry>', '</entry></feed>', $data); 1688 } 1689 $feed = new SimplePie(); 1690 $feed->set_item_class('SimplePieAtomPub_Item'); 1691 $feed->set_raw_data($data); 1692 $feed->init(); 1693 if ( $feed->error() ) 1694 $this->bad_request($feed->error()); 1695 return $feed; 1696 } 1601 1697 } 1602 1698 1603 1699 /**