Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 156 lines (120 sloc) 4.936 kB
d06fac6 @wpdavis Added plugin
wpdavis authored
1 <?php
2 /*
3 Plugin Name: Extend XMLRPC
4 Plugin Author: William P. Davis, Bangor Daily News
5 */
6
7
8 /*
9 Usage:
10 You can use the structure below to create and add your own xmlrpc methods, or you can use the function below yourself (it's pretty powerful)
11
12
13 Some example arrays:
14 array( 1, $username, $password, $post_type, $category, $numberposts, $extra );
15
16 Get 10 most recently published posts:
17 array( 1, $username, $password, 'post', false, 10, false );
18
19 Get 10 most recently modified posts
20 array( 1, $username, $password, 'post', false, 10, array( 'orderby' => 'modified' ) );
21
22 Get 10 most recently modified posts in the sports category ($category can be either a slug or an ID)
23 array( 1, $username, $password, 'post', 'sports', 10, array( 'orderby' => 'modified' ) );
24
25 Get 10 most recently modified posts or pages
26 array( 1, $username, $password, array( 'post', 'page' ), false, 10, array( 'orderby' => 'modified' ) );
27
28 Search for posts in the sports category with the term Tom Brady in it
29 array( 1, $username, $password, 'post', 'sports', 10, array( 's' => 'Tom Brady' ) );
30
31 Basically, it accepts anything get_posts accepts (http://codex.wordpress.org/Function_Reference/query_posts#Parameters)
32
33 */
34
35 add_filter( 'xmlrpc_methods', 'add_my_xmlrpc_methods' );
36
37 function add_my_xmlrpc_methods( $methods ) {
bbad670 @wpdavis Change the names
wpdavis authored
38 $methods['bdn.getPosts'] = 'bdn_xmlrpc_get_posts';
d06fac6 @wpdavis Added plugin
wpdavis authored
39 return $methods;
40 }
41
bbad670 @wpdavis Change the names
wpdavis authored
42 function bdn_xmlrpc_get_posts( $args ) {
d06fac6 @wpdavis Added plugin
wpdavis authored
43 global $wp_xmlrpc_server;
44 $wp_xmlrpc_server->escape( $args );
45
46 $blog_ID = (int) $args[0];
47 $username = $args[1];
48 $password = $args[2];
49 $post_type = $args[3];
50 $category = $args[4];
51 $numberposts = $args[5];
52 $extra = $args[6];
53
54 if ( !$user = $wp_xmlrpc_server->login($username, $password) ) {
55 return $wp_xmlrpc_server->error;
56 }
57
58 $category_int = (int) $category;
59
60 if( !empty( $category_int ) ) {
61 $category_call = 'cat';
62 } else {
63 $category_call = 'category_name';
64 }
65
66 $post_args = array(
67 'numberposts' => $numberposts,
76cd048 @wpdavis Adding coauthors support
wpdavis authored
68 'posts_per_page' => $numberposts,
d06fac6 @wpdavis Added plugin
wpdavis authored
69 $category_call => $category,
70 'post_type' => $post_type,
71 'post_status' => 'any'
72 );
73 if( is_array( $extra ) )
74 $post_args = array_merge( $post_args, $extra );
75
76 $posts_list = query_posts( $post_args );
77
78 if ( !$posts_list ) {
79 $wp_xmlrpc_server->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
80 return $wp_xmlrpc_server->error;
81 }
82
83 foreach ($posts_list as $entry) {
84 if ( !current_user_can( 'edit_post', $entry->ID ) )
85 continue;
86
87 $post_date = mysql2date('Ymd\TH:i:s', $entry->post_date, false);
88 $post_date_gmt = mysql2date('Ymd\TH:i:s', $entry->post_date_gmt, false);
89 $post_modified = mysql2date('Ymd\TH:i:s', $entry->post_modified, false);
90
91 // For drafts use the GMT version of the date
92 if ( $entry->post_status == 'draft' )
93 $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $entry->post_date ), 'Ymd\TH:i:s' );
94
95 $categories = array();
76cd048 @wpdavis Adding coauthors support
wpdavis authored
96 $cats = get_the_category( $entry->ID, 'category' );
97 foreach( $cats as $cat )
98 $categories[] = array( 'name' => $cat->cat_name, 'parent' => $cat->category_parent );
99
100 $publications = array();
101 $pubs = get_the_terms( $entry->ID, 'publication' );
102 foreach( $pubs as $pub )
103 $publications[] = $pub->name;
d06fac6 @wpdavis Added plugin
wpdavis authored
104
105 $post = get_extended( $entry->post_content );
106 $link = post_permalink( $entry->ID );
107
108 // Get the post author info.
76cd048 @wpdavis Adding coauthors support
wpdavis authored
109 $authors = (array) get_userdata( $entry->post_author );
110
111 if( function_exists( 'get_coauthors' ) )
112 $authors = get_coauthors( $entry->ID );
d06fac6 @wpdavis Added plugin
wpdavis authored
113
114 $allow_comments = ( 'open' == $entry->comment_status ) ? 1 : 0;
115 $allow_pings = ( 'open' == $entry->ping_status ) ? 1 : 0;
116
117 // Consider future posts as published
118 if ( $entry->post_status === 'future' )
119 $entry->post_status = 'publish';
120
121 $struct[] = array(
122 'dateCreated' => new IXR_Date( $post_date ),
123 'dateModified' => new IXR_Date( $post_modified ),
124 'userid' => $entry->post_author,
125 'postid' => $entry->ID,
126 'description' => $post['main'],
127 'title' => $entry->post_title,
128 'link' => $link,
129 'permaLink' => $link,
76cd048 @wpdavis Adding coauthors support
wpdavis authored
130 // commented out because no other tool seems to use this
131 // 'content' => $entry['post_content'],
d06fac6 @wpdavis Added plugin
wpdavis authored
132 'categories' => $categories,
133 'mt_excerpt' => $entry->post_excerpt,
134 'mt_text_more' => $post['extended'],
135 'mt_allow_comments' => $allow_comments,
136 'mt_allow_pings' => $allow_pings,
137 'wp_slug' => $entry->post_name,
138 'wp_password' => $entry->post_password,
139 'wp_author_id' => $author->ID,
76cd048 @wpdavis Adding coauthors support
wpdavis authored
140 'wp_authors' => $authors,
d06fac6 @wpdavis Added plugin
wpdavis authored
141 'date_created_gmt' => new IXR_Date($post_date_gmt),
142 'post_status' => $entry->post_status,
76cd048 @wpdavis Adding coauthors support
wpdavis authored
143 'custom_fields' => $wp_xmlrpc_server->get_custom_fields($entry->ID),
144 'publications' => $publications,
d06fac6 @wpdavis Added plugin
wpdavis authored
145 );
146
147 }
148
149 $recent_posts = array();
150 for ( $j=0; $j<count($struct); $j++ ) {
151 array_push($recent_posts, $struct[$j]);
152 }
153
154 return $recent_posts;
155
156 }
Something went wrong with that request. Please try again.