PageRenderTime 29ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/bladencountyrecords/functions/admin-express-functions-deprecated.php

https://gitlab.com/blueprintmrk/bladencountyrecords
PHP | 385 lines | 244 code | 73 blank | 68 comment | 37 complexity | a1f907cd9c7ed5faaf128bc0a0b37b91 MD5 | raw file
  1. <?php
  2. /**
  3. * Set taxonomies for post
  4. *
  5. * @custom code since 2.8.4 added by Dheer Gupta http://webdisect.com
  6. *
  7. * @param int $post_id Post ID.
  8. * @param array $fields Taxonomy Fields
  9. * Enter Values as array
  10. * array ( 'tags' => '', 'taxonomy' => '' )
  11. */
  12. function set_new_taxonomy_tag($post_id, $fields) {
  13. $post_id = (int) $post_id;
  14. foreach ( (array) $fields as $tax ) {
  15. if ( isset($tax['id']) ) {
  16. $tax['id'] = (int) $tax['id'];
  17. if ( isset($tax['taxonomy']) ) {
  18. wp_set_post_terms($tax['id'], $tax['tags'], $tax['taxonomy']);
  19. }
  20. }
  21. elseif ($post_id != '') {
  22. if ( isset($tax['taxonomy']) ) {
  23. wp_set_post_terms($post_id, $tax['tags'], $tax['taxonomy']);
  24. }
  25. }
  26. }
  27. }
  28. /*
  29. * Express version
  30. *
  31. * Returns the API version number for future compatibility consideration
  32. *
  33. */
  34. function express_version() {
  35. return "1.0";
  36. }
  37. /*
  38. * Get Posts With Offset
  39. *
  40. * Returns in a specific range to enable paging.
  41. *
  42. */
  43. function express_getPostsWithOffset($args){
  44. global $wpdb;
  45. global $wp_xmlrpc_server;
  46. $wp_xmlrpc_server->escape($args);
  47. $blog_ID = (int) $args[0];
  48. $username = $args[1];
  49. $password = $args[2];
  50. $num_posts = (int) $args[3];
  51. $offset = (int) $args[4];
  52. $status = $args[5];
  53. if ( !$user = $wp_xmlrpc_server->login($username, $password) )
  54. return $wp_xmlrpc_server->error;
  55. do_action( 'xmlrpc_call', 'metaWeblog.getRecentPosts' );
  56. // -- Added code
  57. if ($status == '') $statuses = "'draft', 'publish', 'future', 'pending', 'private'";
  58. else {
  59. $status_array = explode( ",", $status);
  60. $statuses = "'".implode( "','",$status_array)."'";
  61. }
  62. $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status IN ( $statuses ) ORDER BY post_date DESC LIMIT $offset,$num_posts";
  63. $result = $wpdb->get_results($sql, ARRAY_A);
  64. $posts_list = $result ? $result : array();
  65. // End added code --
  66. if (!$posts_list) {
  67. return array( );
  68. }
  69. foreach ($posts_list as $entry) {
  70. if( !current_user_can( 'edit_post', $entry['ID'] ) )
  71. continue;
  72. $post_date = mysql2date( 'Ymd\TH:i:s', $entry['post_date'], false);
  73. $post_date_gmt = mysql2date( 'Ymd\TH:i:s', $entry['post_date_gmt'], false);
  74. // For drafts use the GMT version of the date
  75. if ( $entry['post_status'] == 'draft' ) {
  76. $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $entry['post_date'] ), 'Ymd\TH:i:s' );
  77. }
  78. $categories = array();
  79. $catids = wp_get_post_categories($entry['ID']);
  80. foreach($catids as $catid) {
  81. $categories[] = get_cat_name($catid);
  82. }
  83. $tagnames = array();
  84. $tags = wp_get_post_tags( $entry['ID'] );
  85. if ( !empty( $tags ) ) {
  86. foreach ( $tags as $tag ) {
  87. $tagnames[] = $tag->name;
  88. }
  89. $tagnames = implode( ', ', $tagnames );
  90. } else {
  91. $tagnames = '';
  92. }
  93. $post = get_extended($entry['post_content']);
  94. $link = post_permalink($entry['ID']);
  95. // Get the post author info.
  96. $author = get_userdata($entry['post_author']);
  97. $allow_comments = ( 'open' == $entry['comment_status']) ? 1 : 0;
  98. $allow_pings = ( 'open' == $entry['ping_status']) ? 1 : 0;
  99. // Consider future posts as published
  100. if( $entry['post_status'] === 'future' ) {
  101. $entry['post_status'] = 'publish';
  102. }
  103. $struct[] = array(
  104. 'dateCreated' => new IXR_Date($post_date),
  105. 'userid' => $entry['post_author'],
  106. 'postid' => $entry['ID'],
  107. 'description' => $post['main'],
  108. 'title' => $entry['post_title'],
  109. 'link' => $link,
  110. 'permaLink' => $link,
  111. // commented out because no other tool seems to use this
  112. // 'content' => $entry['post_content'],
  113. 'categories' => $categories,
  114. 'mt_excerpt' => $entry['post_excerpt'],
  115. 'mt_text_more' => $post['extended'],
  116. 'mt_allow_comments' => $allow_comments,
  117. 'mt_allow_pings' => $allow_pings,
  118. 'mt_keywords' => $tagnames,
  119. 'wp_slug' => $entry['post_name'],
  120. 'wp_password' => $entry['post_password'],
  121. 'wp_author_id' => $author->ID,
  122. 'wp_author_display_name' => $author->display_name,
  123. 'date_created_gmt' => new IXR_Date($post_date_gmt),
  124. 'post_status' => $entry['post_status'],
  125. 'custom_fields' => $wp_xmlrpc_server->get_custom_fields($entry['ID'])
  126. );
  127. }
  128. $recent_posts = array();
  129. for ($j=0; $j<count($struct); $j++) {
  130. array_push($recent_posts, $struct[$j]);
  131. }
  132. return $recent_posts;
  133. }
  134. /*
  135. * Upload file
  136. *
  137. * Adds the post_id in the returned value
  138. *
  139. */
  140. function express_uploadFile($args) {
  141. global $wpdb;
  142. global $wp_xmlrpc_server;
  143. $blog_ID = (int) $args[0];
  144. $username = $wpdb->escape($args[1]);
  145. $password = $wpdb->escape($args[2]);
  146. $data = $args[3];
  147. $name = sanitize_file_name( $data['name'] );
  148. $type = $data['type'];
  149. $bits = $data['bits'];
  150. logIO( 'O', '(MW) Received '.strlen($bits).' bytes' );
  151. if ( !$user = $wp_xmlrpc_server->login($username, $password) )
  152. return $wp_xmlrpc_server->error;
  153. do_action( 'xmlrpc_call', 'metaWeblog.newMediaObject' );
  154. if ( !current_user_can( 'upload_files') ) {
  155. logIO( 'O', '(MW) User does not have upload_files capability' );
  156. return new IXR_Error(401, __( 'You are not allowed to upload files to this site.'));
  157. }
  158. if ( $upload_err = apply_filters( "pre_upload_error", false ) )
  159. return new IXR_Error(500, $upload_err);
  160. if(!empty($data["overwrite"]) && ($data["overwrite"] == true)) {
  161. // Get postmeta info on the object.
  162. $old_file = $wpdb->get_row( "
  163. SELECT ID
  164. FROM {$wpdb->posts}
  165. WHERE post_title = '{$name}'
  166. AND post_type = 'attachment'
  167. " );
  168. // Delete previous file.
  169. wp_delete_attachment($old_file->ID);
  170. // Make sure the new name is different by pre-pending the
  171. // previous post id.
  172. $filename = preg_replace( "/^wpid\d+-/", "", $name);
  173. $name = "wpid{$old_file->ID}-{$filename}";
  174. }
  175. $upload = wp_upload_bits($name, $type, $bits);
  176. if ( ! empty($upload['error']) ) {
  177. $errorString = sprintf(__( 'Could not write file %1$s (%2$s)'), $name, $upload['error']);
  178. logIO( 'O', '(MW) ' . $errorString);
  179. return new IXR_Error(500, $errorString);
  180. }
  181. // Construct the attachment array
  182. // attach to post_id 0
  183. $post_id = 0;
  184. $attachment = array(
  185. 'post_title' => $name,
  186. 'post_content' => '',
  187. 'post_type' => 'attachment',
  188. 'post_parent' => $post_id,
  189. 'post_mime_type' => $type,
  190. 'guid' => $upload[ 'url' ]
  191. );
  192. // Save the data
  193. $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
  194. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
  195. return apply_filters( 'wp_handle_upload', array( 'file' => $name, 'url' => $upload[ 'url' ], 'type' => $type , 'id' => $id ) );
  196. }
  197. /*
  198. * Woo taxonomy
  199. *
  200. * Set the proper taxonomy
  201. *
  202. */
  203. function express_woo_taxonomy($args) {
  204. $content_struct = $args[3];
  205. // Re-assign the taxonomies so they are compatible with WooThemes themes
  206. $taxonomies = $content_struct['taxonomy'];
  207. if (is_array($taxonomies)) {
  208. $new_taxonomy = array();
  209. $woo_tags = array();
  210. foreach ($taxonomies as $taxonomy) {
  211. if ($taxonomy['taxonomy'] == 'tumblog') {
  212. foreach ($taxonomy['tags'] as $tag) {
  213. switch (strtolower($tag)) {
  214. case 'note':
  215. $woo_tags[] = get_option( 'woo_articles_term_id' );
  216. break;
  217. case 'link':
  218. $woo_tags[] = get_option( 'woo_links_term_id' );
  219. break;
  220. case 'quote':
  221. $woo_tags[] = get_option( 'woo_quotes_term_id' );
  222. break;
  223. case 'image':
  224. $woo_tags[] = get_option( 'woo_images_term_id' );
  225. break;
  226. default:
  227. $woo_tags[] = $tag;
  228. break;
  229. }
  230. }
  231. $taxonomy['tags'] = implode( ',', $woo_tags);
  232. }
  233. $new_taxonomy[] = $taxonomy;
  234. }
  235. $content_struct['taxonomy'] = $new_taxonomy;
  236. $args[3] = $content_struct;
  237. }
  238. return $args;
  239. }
  240. /*
  241. * New post
  242. *
  243. * Sets post attachements if specified
  244. * Sets post custom taxonomy
  245. *
  246. */
  247. function express_newPost($args) {
  248. global $wp_xmlrpc_server;
  249. $args = express_woo_taxonomy($args);
  250. $result = $wp_xmlrpc_server->mw_newPost($args);
  251. $post_ID = intval($result);
  252. if ($post_ID == 0) return $result;
  253. $content_struct = $args[3];
  254. // Insert taxonomies
  255. if ( isset($content_struct['taxonomy']) ) {
  256. set_new_taxonomy_tag($post_ID, $content_struct['taxonomy']);
  257. }
  258. // Add new attachments
  259. $attachments = $content_struct['attachments'];
  260. if (is_array($attachments)) {
  261. foreach ($attachments as $attachment_ID) {
  262. $attachment_post = wp_get_single_post($attachment_ID,ARRAY_A);
  263. extract($attachment_post, EXTR_SKIP);
  264. $post_parent = $post_ID;
  265. $postdata = compact( 'ID', 'post_parent', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt' );
  266. wp_update_post($postdata);
  267. }
  268. }
  269. return $result;
  270. }
  271. /*
  272. * Edit post
  273. *
  274. * Sets post attachements if specified
  275. * Sets post custom taxonomy
  276. *
  277. */
  278. function express_editPost($args) {
  279. global $wp_xmlrpc_server;
  280. $args = express_woo_taxonomy($args);
  281. $result = $wp_xmlrpc_server->mw_editPost($args);
  282. if ($result == false) return false;
  283. // Insert taxonomies
  284. if ( isset($content_struct['taxonomy']) ) {
  285. set_new_taxonomy_tag($post_ID, $content_struct['taxonomy']);
  286. }
  287. // TODO: Remove old attachments
  288. // Add new attachments
  289. $post_ID = (int)$args[0];
  290. $content_struct = $args[3];
  291. $attachments = $content_struct['attachments'];
  292. if (is_array($attachments)) {
  293. foreach ($attachments as $attachment_ID) {
  294. $attachment_post = wp_get_single_post($attachment_ID,ARRAY_A);
  295. extract($attachment_post, EXTR_SKIP);
  296. $post_parent = $post_ID;
  297. $postdata = compact( 'ID', 'post_parent', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt' );
  298. wp_update_post($postdata);
  299. }
  300. }
  301. return true;
  302. }
  303. add_filter( 'xmlrpc_methods', 'attach_express_methods' );
  304. function attach_express_methods($methods) {
  305. $methods['express.version'] = 'express_version';
  306. $methods['express.getPostsWithOffset'] = 'express_getPostsWithOffset';
  307. $methods['express.uploadFile'] = 'express_uploadFile';
  308. $methods['express.newPost'] = 'express_newPost';
  309. $methods['express.editPost'] = 'express_editPost';
  310. return $methods;
  311. }
  312. ?>