Advertisement
Guest User

Untitled

a guest
May 21st, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. <?php
  2. add_filter('xmlrpc_methods','add_xmlrpc_methods');
  3.  
  4. function add_xmlrpc_methods($methods){
  5. $methods['my.newMashine'] = 'newProduct';
  6. $methods['my.dellAll'] = 'dellAll';
  7. $methods['my.convFieldToTerm'] = 'convFieldToTerm';
  8. return $methods;
  9.  
  10. }
  11.  
  12. //массовое удаление в вордпрессе
  13. //type = media удаляет все картинки
  14. //type = trash очищает карзину
  15. //type = 'post_type' удаляет посты post_type
  16. function dellAll($args){
  17.  
  18. $blog_id = (int) $args[0];
  19. $username = $args[1];
  20. $password = $args[2];
  21. $post_data = $args[3];
  22. //аутентификация и валидация
  23. {
  24. $user = wp_authenticate($username, $password);
  25.  
  26. if (is_wp_error($user)) {
  27. return new IXR_Error('403', 'Не верные логин и пароль');
  28. }
  29.  
  30. wp_set_current_user($user->ID);
  31.  
  32. if (!current_user_can('edit_private_posts')) {
  33. return new IXR_Error(401, __('Sorry, you cannot edit private posts.'));
  34. }
  35.  
  36. if (!is_array($post_data)) {
  37. return new IXR_Error(401, __('Sorry, no post data not found.'));
  38. }
  39. }
  40.  
  41. if ($post_data['type'] == 'media') {
  42. $params = array(
  43. 'post_type' => 'attachment', // obvious
  44. 'posts_per_page' => -1 // get them all
  45. );
  46.  
  47. // get all attachments post ids
  48. $posts = get_posts($params);
  49. foreach ($posts as $post_id) {
  50. // get an array of image data
  51. $image_attributes = wp_get_attachment_image_src($post_id->ID);
  52. wp_delete_attachment(wp_delete_attachment($post_id->ID, true));
  53. }
  54. } else {
  55. $params = array(
  56. 'post_type' => $post_data['type'], // obvious
  57. 'posts_per_page' => -1, // get them all
  58. 'post_status' =>'any'
  59.  
  60. );
  61.  
  62.  
  63.  
  64. $posts = get_posts($params);
  65. foreach ($posts as $post_id) {
  66. // get an array of image data
  67. wp_delete_post($post_id->ID, TRUE);
  68. }
  69.  
  70. }
  71. return $posts;
  72. }
  73.  
  74. function newProduct($args){
  75. $blog_id = (int) $args[0];
  76. $username = $args[1];
  77. $password = $args[2];
  78. $post_data = $args[3];
  79. //аутентификация и валидация
  80. {
  81. $user = wp_authenticate($username, $password);
  82.  
  83. if (is_wp_error($user)) {
  84. return new IXR_Error(403, __('Bad login/pass combination.'));
  85. }
  86.  
  87. wp_set_current_user($user->ID);
  88.  
  89. if (!current_user_can('edit_private_posts')) {
  90. return new IXR_Error(401, __('Sorry, you cannot edit private posts.'));
  91. }
  92.  
  93. if (!is_array($post_data)) {
  94. return new IXR_Error(401, __('Sorry, no post data not found.'));
  95. }
  96. }
  97.  
  98. $post_id = wp_insert_post($post_data);
  99.  
  100. if(isset($post_data['custom_data'])){//вставить кустом филдс и тд
  101. if(isset($post_data['custom_data']['images'])){
  102. //оюработать входные ссылки на картинки
  103. $myimages = $post_data['custom_data']['images'];
  104.  
  105. $data_array = array();
  106. foreach ($myimages as $myimage) {
  107. //загрузить картинку
  108. $tmp = download_url($myimage);
  109. $file_array = array(
  110. 'name' => substr(basename($myimage), -10),
  111. 'tmp_name' => $tmp
  112. );
  113. if(is_wp_error($tmp)) @unlink($tmp);
  114. $image_id = media_handle_sideload($file_array, 0);
  115. if(is_wp_error($id)) @unlink($tmp);
  116. $att_url = wp_get_attachment_url($image_id);
  117. add_post_meta($post_id, 'wpcf-image', $att_url);
  118.  
  119. }
  120.  
  121. }
  122.  
  123.  
  124.  
  125.  
  126. //обработать остальные кастомные поля
  127. if(isset($post_data['custom_data']['images'])) {
  128. unset($post_data['custom_data']['images']);
  129. }
  130. if(count($post_data['custom_data'])!==0){
  131. foreach ($post_data['custom_data'] as $key => $value) {
  132. # code...
  133. if($curr_tax = taxonomy_exists($key)){
  134. //добавить термин в таксономию $key
  135. wp_set_object_terms( $post_id, $value, $key);
  136. }
  137. elseif (gettype($value) == 'string' OR gettype($value) == 'int')
  138. {
  139. add_post_meta($post_id, $key, $value);
  140. }elseif (gettype($value) == 'array') {
  141.  
  142. foreach ($value as $val) {
  143. # code...
  144.  
  145. add_post_meta($post_id, $key, $val);
  146. }
  147. }
  148. }
  149.  
  150. }
  151.  
  152. return $post_id;
  153.  
  154. }
  155.  
  156. }
  157.  
  158.  
  159. function convFieldToTerm($args){//конвертация полей в термины таксономии НЕДОДЕЛАНА
  160. $blog_id = (int) $args[0];
  161. $username = $args[1];
  162. $password = $args[2];
  163. $post_data = $args[3];
  164. //аутентификация и валидация
  165. {
  166. $user = wp_authenticate($username, $password);
  167.  
  168. if (is_wp_error($user)) {
  169. return new IXR_Error('403', 'Не верные логин и пароль');
  170. }
  171.  
  172. wp_set_current_user($user->ID);
  173.  
  174. if (!current_user_can('edit_private_posts')) {
  175. return new IXR_Error(401, __('Sorry, you cannot edit private posts.'));
  176. }
  177.  
  178. if (!is_array($post_data)) {
  179. return new IXR_Error(401, __('Sorry, no post data not found.'));
  180. }
  181. }
  182.  
  183.  
  184. return($post_data);
  185.  
  186. // foreach ($post_data('convertions') as $key => $value) {
  187. // # code...
  188. // $posts = get_posts(array(
  189. // 'post_type'=> $data['post_type'],
  190. // 'meta_key' => $key
  191. // )
  192. // );
  193.  
  194.  
  195.  
  196. //}
  197.  
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement