Ticket #18429: dates_and_featured_image4.patch
File dates_and_featured_image4.patch, 18.4 KB (added by , 13 years ago) |
---|
-
wp-includes/class-wp-xmlrpc-server.php
467 467 } 468 468 469 469 /** 470 * Convert a WordPress date string to an IXR_Date object. 471 * 472 * @access protected 473 * 474 * @param $date 475 * @return IXR_Date 476 */ 477 protected function _convert_date( $date, $format = 'Ymd\TH:i:s' ) { 478 if ( $date === '0000-00-00 00:00:00' ) { 479 return new IXR_Date( '00000000T00:00:00Z' ); 480 } 481 return new IXR_Date( mysql2date( $format, $date, false ) ); 482 } 483 484 /** 485 * Convert a WordPress date string to an IXR_Date object. 486 * 487 * @access protected 488 * 489 * @param $date 490 * @return IXR_Date 491 */ 492 protected function _convert_date_gmt( $date, $format = 'Ymd\TH:i:s' ) { 493 if ( $date === '0000-00-00 00:00:00' ) { 494 return new IXR_Date( '00000000T00:00:00Z' ); 495 } 496 return new IXR_Date( get_gmt_from_date( mysql2date( $format, $date, false ) ) ); 497 } 498 499 /** 470 500 * Prepares post data for return in an XML-RPC object. 471 501 * 472 502 * @access private … … 477 507 */ 478 508 function _prepare_post( $post, $fields ) { 479 509 // holds the data for this post. built up based on $fields 480 $_post = array( 'post_id' => $post['ID']);510 $_post = array( 'post_id' => strval( $post['ID'] ) ); 481 511 482 512 // prepare common post fields 483 513 $post_fields = array( 484 514 'post_title' => $post['post_title'], 485 'post_date' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $post['post_date'], false )),486 'post_date_gmt' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $post['post_date_gmt'], false )),487 'post_modified' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $post['post_modified'], false )),488 'post_modified_gmt' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $post['post_modified_gmt'], false )),515 'post_date' => $this->_convert_date( $post['post_date'] ), 516 'post_date_gmt' => $this->_convert_date( $post['post_date_gmt'] ), 517 'post_modified' => $this->_convert_date( $post['post_modified'] ), 518 'post_modified_gmt' => $this->_convert_date( $post['post_modified_gmt'] ), 489 519 'post_status' => $post['post_status'], 490 520 'post_type' => $post['post_type'], 491 521 'post_name' => $post['post_name'], … … 499 529 'sticky' => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ), 500 530 ); 501 531 532 if( current_theme_supports( 'post-thumbnails' ) ) { 533 $post_fields['featured_image'] = get_post_thumbnail_id( $post['ID'] ); 534 $post_fields['featured_image_url'] = wp_get_attachment_url( $post_fields['featured_image'] ); 535 } 536 502 537 // Consider future posts as published 503 538 if ( $post_fields['post_status'] === 'future' ) 504 539 $post_fields['post_status'] = 'publish'; … … 591 626 * - comment_status - can be 'open' | 'closed' 592 627 * - ping_status - can be 'open' | 'closed' 593 628 * - sticky 629 * - featured_image - ID of a media item to use as the featured image 594 630 * - custom_fields - array, with each element containing 'key' and 'value' 595 631 * - terms - array, with taxonomy names as keys and arrays of term IDs as values 596 632 * - terms_names - array, with taxonomy names as keys and arrays of term names as values … … 724 760 stick_post( $post_ID ); 725 761 } 726 762 763 if ( isset ( $post_data['featured_image'] ) ) { 764 if( current_theme_supports( 'post-thumbnails' ) ) { 765 // empty value deletes, non-empty value adds/updates 766 if ( empty( $post_data['featured_image'] ) ) { 767 delete_post_thumbnail( $post_ID ); 768 } 769 else { 770 if ( set_post_thumbnail( $post_ID, $post_data['featured_image'] ) === false ) 771 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) ); 772 } 773 unset( $content_struct['featured_image'] ); 774 } 775 else { 776 return new IXR_Error( 401, __( "Sorry, current theme doens't supports featured images." ) ); 777 } 778 } 779 727 780 if ( isset ( $post_data['custom_fields'] ) && post_type_supports( $post_data['post_type'], 'custom-fields' ) ) { 728 781 $this->set_custom_fields( $post_ID, $post_data['custom_fields'] ); 729 782 } … … 880 933 return new IXR_Error( 404, __( 'Invalid post ID.' ) ); 881 934 882 935 // convert the date field back to IXR form 883 $post['post_date'] = new IXR_Date( mysql2date( 'Ymd\TH:i:s', $post['post_date'], false ));936 $post['post_date'] = $this->_convert_date( $post['post_date'] ); 884 937 885 938 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct, 886 939 // since _insert_post will ignore the non-GMT date if the GMT date is set 887 940 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) ) 888 941 unset( $post['post_date_gmt'] ); 889 942 else 890 $post['post_date_gmt'] = new IXR_Date( mysql2date( 'Ymd\TH:i:s', $post['post_date_gmt'], false ));943 $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] ); 891 944 892 945 $this->escape( $post ); 893 946 $merged_content_struct = array_merge( $post, $content_struct ); … … 1080 1133 $query['order'] = $filter['order']; 1081 1134 } 1082 1135 1083 do_action( 'xmlrpc_call', 'wp.getPosts' );1084 1085 1136 $posts_list = wp_get_recent_posts( $query ); 1086 1137 1087 1138 if ( ! $posts_list ) … … 1151 1202 $allow_pings = pings_open($page->ID) ? 1 : 0; 1152 1203 1153 1204 // Format page date. 1154 $page_date = mysql2date('Ymd\TH:i:s', $page->post_date, false);1155 $page_date_gmt = mysql2date('Ymd\TH:i:s', $page->post_date_gmt, false);1205 $page_date = $this->_convert_date( $page->post_date ); 1206 $page_date_gmt = $this->_convert_date( $page->post_date_gmt ); 1156 1207 1157 1208 // For drafts use the GMT version of the date 1158 if ( $page->post_status == 'draft')1159 $page_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $page->post_date ), 'Ymd\TH:i:s');1209 if ( in_array( $page->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) 1210 $page_date_gmt = $this->_convert_date_gmt( $page->post_date ); 1160 1211 1161 1212 // Pull the categories info together. 1162 1213 $categories = array(); … … 1172 1223 $page_template = 'default'; 1173 1224 1174 1225 $page_struct = array( 1175 'dateCreated' => new IXR_Date($page_date),1226 'dateCreated' => $page_date, 1176 1227 'userid' => $page->post_author, 1177 1228 'page_id' => $page->ID, 1178 1229 'page_status' => $page->post_status, … … 1193 1244 'wp_page_order' => $page->menu_order, 1194 1245 'wp_author_id' => (string) $author->ID, 1195 1246 'wp_author_display_name' => $author->display_name, 1196 'date_created_gmt' => new IXR_Date($page_date_gmt),1247 'date_created_gmt' => $page_date_gmt, 1197 1248 'custom_fields' => $this->get_custom_fields($page_id), 1198 1249 'wp_page_template' => $page_template 1199 1250 ); … … 1410 1461 // The date needs to be formatted properly. 1411 1462 $num_pages = count($page_list); 1412 1463 for ( $i = 0; $i < $num_pages; $i++ ) { 1413 $p ost_date = mysql2date('Ymd\TH:i:s', $page_list[$i]->post_date, false);1414 $p ost_date_gmt = mysql2date('Ymd\TH:i:s', $page_list[$i]->post_date_gmt, false);1464 $page_list[$i]->dateCreated = $this->_convert_date( $page_list[$i]->post_date ); 1465 $page_list[$i]->date_created_gmt = $this->_convert_date( $page_list[$i]->post_date_gmt ); 1415 1466 1416 $page_list[$i]->dateCreated = new IXR_Date($post_date);1417 $page_list[$i]->date_created_gmt = new IXR_Date($post_date_gmt);1418 1419 1467 // For drafts use the GMT version of the date 1420 if ( $page_list[$i]->post_status == 'draft' ) { 1421 $page_list[$i]->date_created_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $page_list[$i]->post_date ), 'Ymd\TH:i:s' ); 1422 $page_list[$i]->date_created_gmt = new IXR_Date( $page_list[$i]->date_created_gmt ); 1468 if ( in_array( $page_list[$i]->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) { 1469 $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date ); 1423 1470 } 1424 1471 1425 1472 unset($page_list[$i]->post_date_gmt); … … 1658 1705 return new IXR_Error( 404, __( 'Invalid comment ID.' ) ); 1659 1706 1660 1707 // Format page date. 1661 $comment_date = mysql2date('Ymd\TH:i:s', $comment->comment_date, false);1662 $comment_date_gmt = mysql2date('Ymd\TH:i:s', $comment->comment_date_gmt, false);1708 $comment_date = $this->_convert_date( $comment->comment_date ); 1709 $comment_date_gmt = $this->_convert_date( $comment->comment_date_gmt ); 1663 1710 1664 1711 if ( '0' == $comment->comment_approved ) 1665 1712 $comment_status = 'hold'; … … 1673 1720 $link = get_comment_link($comment); 1674 1721 1675 1722 $comment_struct = array( 1676 'date_created_gmt' => new IXR_Date($comment_date_gmt),1723 'date_created_gmt' => $comment_date_gmt, 1677 1724 'user_id' => $comment->user_id, 1678 1725 'comment_id' => $comment->comment_ID, 1679 1726 'parent' => $comment->comment_parent, … … 2241 2288 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) ); 2242 2289 2243 2290 // Format page date. 2244 $attachment_date = mysql2date('Ymd\TH:i:s', $attachment->post_date, false);2245 $attachment_date_gmt = mysql2date('Ymd\TH:i:s', $attachment->post_date_gmt, false);2291 $attachment_date = $this->_convert_date( $attachment->post_date ); 2292 $attachment_date_gmt = $this->_convert_date( $attachment->post_date_gmt ); 2246 2293 2247 2294 $link = wp_get_attachment_url($attachment->ID); 2248 2295 $thumbnail_link = wp_get_attachment_thumb_url($attachment->ID); 2249 2296 2250 2297 $attachment_struct = array( 2251 'date_created_gmt' => new IXR_Date($attachment_date_gmt),2298 'date_created_gmt' => $attachment_date_gmt, 2252 2299 'parent' => $attachment->post_parent, 2253 2300 'link' => $link, 2254 2301 'thumbnail' => $thumbnail_link, … … 2503 2550 2504 2551 $struct = array( 2505 2552 'userid' => $post_data['post_author'], 2506 'dateCreated' => new IXR_Date(mysql2date('Ymd\TH:i:s', $post_data['post_date'], false)),2553 'dateCreated' => $this->_convert_date( $post_data['post_date'] ), 2507 2554 'content' => $content, 2508 2555 'postid' => (string) $post_data['ID'] 2509 2556 ); … … 2548 2595 if ( !current_user_can( 'edit_post', $entry['ID'] ) ) 2549 2596 continue; 2550 2597 2551 $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);2598 $post_date = $this->_convert_date( $entry['post_date'] ); 2552 2599 $categories = implode(',', wp_get_post_categories($entry['ID'])); 2553 2600 2554 2601 $content = '<title>'.stripslashes($entry['post_title']).'</title>'; … … 2557 2604 2558 2605 $struct[] = array( 2559 2606 'userid' => $entry['post_author'], 2560 'dateCreated' => new IXR_Date($post_date),2607 'dateCreated' => $post_date, 2561 2608 'content' => $content, 2562 2609 'postid' => (string) $entry['ID'], 2563 2610 ); … … 2816 2863 * - wp_page_parent_id 2817 2864 * - wp_page_order 2818 2865 * - wp_author_id 2866 * - wp_featured_image 2819 2867 * - post_status | page_status - can be 'draft', 'private', 'publish', or 'pending' 2820 2868 * - mt_allow_comments - can be 'open' or 'closed' 2821 2869 * - mt_allow_pings - can be 'open' or 'closed' … … 3067 3115 if ( isset($content_struct['custom_fields']) ) 3068 3116 $this->set_custom_fields($post_ID, $content_struct['custom_fields']); 3069 3117 3118 if ( isset ( $post_data['wp_featured_image'] ) ) { 3119 if( current_theme_supports( 'post-thumbnails' ) && ! empty( $post_data['wp_featured_image'] ) ) { 3120 if ( set_post_thumbnail( $post_ID, $post_data['wp_featured_image'] ) === false ) 3121 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) ); 3122 3123 unset( $content_struct['wp_featured_image'] ); 3124 } 3125 else { 3126 return new IXR_Error( 401, __( "Sorry, current theme doens't supports featured images." ) ); 3127 } 3128 } 3129 3070 3130 // Handle enclosures 3071 3131 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null; 3072 3132 $this->add_enclosure_if_new($post_ID, $thisEnclosure); … … 3365 3425 if ( isset($content_struct['custom_fields']) ) 3366 3426 $this->set_custom_fields($post_ID, $content_struct['custom_fields']); 3367 3427 3428 if ( isset ( $post_data['wp_featured_image'] ) ) { 3429 if( current_theme_supports( 'post-thumbnails' ) ) { 3430 // empty value deletes, non-empty value adds/updates 3431 if ( empty( $post_data['wp_featured_image'] ) ) { 3432 delete_post_thumbnail( $post_ID ); 3433 } 3434 else { 3435 if ( set_post_thumbnail( $post_ID, $post_data['wp_featured_image'] ) === false ) 3436 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) ); 3437 } 3438 unset( $content_struct['wp_featured_image'] ); 3439 } 3440 else { 3441 return new IXR_Error( 401, __( "Sorry, current theme doens't supports featured images." ) ); 3442 } 3443 } 3444 3368 3445 // Handle enclosures 3369 3446 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null; 3370 3447 $this->add_enclosure_if_new($post_ID, $thisEnclosure); … … 3406 3483 $postdata = wp_get_single_post($post_ID, ARRAY_A); 3407 3484 3408 3485 if ($postdata['post_date'] != '') { 3409 $post_date = mysql2date('Ymd\TH:i:s', $postdata['post_date'], false);3410 $post_date_gmt = mysql2date('Ymd\TH:i:s', $postdata['post_date_gmt'], false);3411 $post_modified = mysql2date('Ymd\TH:i:s', $postdata['post_modified'], false);3412 $post_modified_gmt = mysql2date('Ymd\TH:i:s', $postdata['post_modified_gmt'], false);3486 $post_date = $this->_convert_date( $postdata['post_date'] ); 3487 $post_date_gmt = $this->_convert_date( $postdata['post_date_gmt'] ); 3488 $post_modified = $this->_convert_date( $postdata['post_modified'] ); 3489 $post_modified_gmt = $this->_convert_date( $postdata['post_modified_gmt'] ); 3413 3490 3414 3491 // For drafts use the GMT version of the post date 3415 3492 if ( $postdata['post_status'] == 'draft' ) { 3416 $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $postdata['post_date'] ), 'Ymd\TH:i:s');3417 $post_modified_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $postdata['post_modified'] ), 'Ymd\TH:i:s');3493 $post_date_gmt = $this->_convert_date_gmt( $postdata['post_date'] ); 3494 $post_modified_gmt = $this->_convert_date_gmt( $postdata['post_modified'] ); 3418 3495 } 3419 3496 3420 3497 $categories = array(); … … 3468 3545 } 3469 3546 3470 3547 $resp = array( 3471 'dateCreated' => new IXR_Date($post_date),3548 'dateCreated' => $post_date, 3472 3549 'userid' => $postdata['post_author'], 3473 3550 'postid' => $postdata['ID'], 3474 3551 'description' => $post['main'], … … 3486 3563 'wp_slug' => $postdata['post_name'], 3487 3564 'wp_password' => $postdata['post_password'], 3488 3565 'wp_author_id' => (string) $author->ID, 3489 'wp_author_display_name' 3490 'date_created_gmt' => new IXR_Date($post_date_gmt),3566 'wp_author_display_name' => $author->display_name, 3567 'date_created_gmt' => $post_date_gmt, 3491 3568 'post_status' => $postdata['post_status'], 3492 3569 'custom_fields' => $this->get_custom_fields($post_ID), 3493 3570 'wp_post_format' => $post_format, 3494 3571 'sticky' => $sticky, 3495 'date_modified' => new IXR_Date( $post_modified ),3496 'date_modified_gmt' => new IXR_Date( $post_modified_gmt )3572 'date_modified' => $post_modified, 3573 'date_modified_gmt' => $post_modified_gmt 3497 3574 ); 3498 3575 3499 3576 if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure; 3500 3577 3578 if( current_theme_supports( 'post-thumbnails' ) ) { 3579 $resp['wp_featured_image'] = get_post_thumbnail_id( $postdata['ID'] ); 3580 $resp['wp_featured_image_url'] = wp_get_attachment_url( $resp['wp_featured_image'] ); 3581 } 3582 3501 3583 return $resp; 3502 3584 } else { 3503 3585 return new IXR_Error(404, __('Sorry, no such post.')); … … 3538 3620 if ( !current_user_can( 'edit_post', $entry['ID'] ) ) 3539 3621 continue; 3540 3622 3541 $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);3542 $post_date_gmt = mysql2date('Ymd\TH:i:s', $entry['post_date_gmt'], false);3543 $post_modified = mysql2date('Ymd\TH:i:s', $entry['post_modified'], false);3544 $post_modified_gmt = mysql2date('Ymd\TH:i:s', $entry['post_modified_gmt'], false);3623 $post_date = $this->_convert_date( $entry['post_date'] ); 3624 $post_date_gmt = $this->_convert_date( $entry['post_date_gmt'] ); 3625 $post_modified = $this->_convert_date( $entry['post_modified'] ); 3626 $post_modified_gmt = $this->_convert_date( $entry['post_modified_gmt'] ); 3545 3627 3546 3628 // For drafts use the GMT version of the date 3547 3629 if ( $entry['post_status'] == 'draft' ) { 3548 $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $entry['post_date'] ), 'Ymd\TH:i:s');3549 $post_modified_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $entry['post_modified'] ), 'Ymd\TH:i:s');3630 $post_date_gmt = $this->_convert_date_gmt( $entry['post_date'] ); 3631 $post_modified_gmt = $this->_convert_date_gmt( $entry['post_modified'] ); 3550 3632 } 3551 3633 3552 3634 $categories = array(); … … 3584 3666 $post_format = 'standard'; 3585 3667 3586 3668 $struct[] = array( 3587 'dateCreated' => new IXR_Date($post_date),3669 'dateCreated' => $post_date, 3588 3670 'userid' => $entry['post_author'], 3589 3671 'postid' => (string) $entry['ID'], 3590 3672 'description' => $post['main'], … … 3603 3685 'wp_password' => $entry['post_password'], 3604 3686 'wp_author_id' => (string) $author->ID, 3605 3687 'wp_author_display_name' => $author->display_name, 3606 'date_created_gmt' => new IXR_Date($post_date_gmt),3688 'date_created_gmt' => $post_date_gmt, 3607 3689 'post_status' => $entry['post_status'], 3608 3690 'custom_fields' => $this->get_custom_fields($entry['ID']), 3609 3691 'wp_post_format' => $post_format, 3610 'date_modified' => new IXR_Date( $post_modified ),3611 'date_modified_gmt' => new IXR_Date( $post_modified_gmt )3692 'date_modified' => $post_modified, 3693 'date_modified_gmt' => $post_modified_gmt 3612 3694 ); 3613 3695 3696 $entry_index = count( $struct ) - 1; 3697 if( current_theme_supports( 'post-thumbnails' ) ) { 3698 $struct[ $entry_index ][ 'wp_featured_image' ] = get_post_thumbnail_id( $entry['ID'] ); 3699 $struct[ $entry_index ][ 'wp_featured_image_url' ] = wp_get_attachment_url( $struct[ $entry_index ][ 'wp_featured_image' ] ); 3700 } 3614 3701 } 3615 3702 3616 3703 $recent_posts = array(); … … 3783 3870 if ( !current_user_can( 'edit_post', $entry['ID'] ) ) 3784 3871 continue; 3785 3872 3786 $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);3787 $post_date_gmt = mysql2date('Ymd\TH:i:s', $entry['post_date_gmt'], false);3873 $post_date = $this->_convert_date( $entry['post_date'] ); 3874 $post_date_gmt = $this->_convert_date( $entry['post_date_gmt'] ); 3788 3875 3789 3876 // For drafts use the GMT version of the date 3790 if ( $entry['post_status'] == 'draft')3791 $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $entry['post_date'] ), 'Ymd\TH:i:s');3877 if ( in_array( $entry['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) 3878 $post_date_gmt = $this->_convert_date_gmt( $entry['post_date'] ); 3792 3879 3793 3880 $struct[] = array( 3794 'dateCreated' => new IXR_Date($post_date),3881 'dateCreated' => $post_date, 3795 3882 'userid' => $entry['post_author'], 3796 3883 'postid' => (string) $entry['ID'], 3797 3884 'title' => $entry['post_title'], 3798 3885 'post_status' => $entry['post_status'], 3799 'date_created_gmt' => new IXR_Date($post_date_gmt)3886 'date_created_gmt' => $post_date_gmt 3800 3887 ); 3801 3888 3802 3889 }