Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_filter('xmlrpc_methods','add_xmlrpc_methods');
- function add_xmlrpc_methods($methods){
- $methods['my.newMashine'] = 'newProduct';
- $methods['my.dellAll'] = 'dellAll';
- $methods['my.convFieldToTerm'] = 'convFieldToTerm';
- return $methods;
- }
- //массовое удаление в вордпрессе
- //type = media удаляет все картинки
- //type = trash очищает карзину
- //type = 'post_type' удаляет посты post_type
- function dellAll($args){
- $blog_id = (int) $args[0];
- $username = $args[1];
- $password = $args[2];
- $post_data = $args[3];
- //аутентификация и валидация
- {
- $user = wp_authenticate($username, $password);
- if (is_wp_error($user)) {
- return new IXR_Error('403', 'Не верные логин и пароль');
- }
- wp_set_current_user($user->ID);
- if (!current_user_can('edit_private_posts')) {
- return new IXR_Error(401, __('Sorry, you cannot edit private posts.'));
- }
- if (!is_array($post_data)) {
- return new IXR_Error(401, __('Sorry, no post data not found.'));
- }
- }
- if ($post_data['type'] == 'media') {
- $params = array(
- 'post_type' => 'attachment', // obvious
- 'posts_per_page' => -1 // get them all
- );
- // get all attachments post ids
- $posts = get_posts($params);
- foreach ($posts as $post_id) {
- // get an array of image data
- $image_attributes = wp_get_attachment_image_src($post_id->ID);
- wp_delete_attachment(wp_delete_attachment($post_id->ID, true));
- }
- } else {
- $params = array(
- 'post_type' => $post_data['type'], // obvious
- 'posts_per_page' => -1, // get them all
- 'post_status' =>'any'
- );
- $posts = get_posts($params);
- foreach ($posts as $post_id) {
- // get an array of image data
- wp_delete_post($post_id->ID, TRUE);
- }
- }
- return $posts;
- }
- function newProduct($args){
- $blog_id = (int) $args[0];
- $username = $args[1];
- $password = $args[2];
- $post_data = $args[3];
- //аутентификация и валидация
- {
- $user = wp_authenticate($username, $password);
- if (is_wp_error($user)) {
- return new IXR_Error(403, __('Bad login/pass combination.'));
- }
- wp_set_current_user($user->ID);
- if (!current_user_can('edit_private_posts')) {
- return new IXR_Error(401, __('Sorry, you cannot edit private posts.'));
- }
- if (!is_array($post_data)) {
- return new IXR_Error(401, __('Sorry, no post data not found.'));
- }
- }
- $post_id = wp_insert_post($post_data);
- if(isset($post_data['custom_data'])){//вставить кустом филдс и тд
- if(isset($post_data['custom_data']['images'])){
- //оюработать входные ссылки на картинки
- $myimages = $post_data['custom_data']['images'];
- $data_array = array();
- foreach ($myimages as $myimage) {
- //загрузить картинку
- $tmp = download_url($myimage);
- $file_array = array(
- 'name' => substr(basename($myimage), -10),
- 'tmp_name' => $tmp
- );
- if(is_wp_error($tmp)) @unlink($tmp);
- $image_id = media_handle_sideload($file_array, 0);
- if(is_wp_error($id)) @unlink($tmp);
- $att_url = wp_get_attachment_url($image_id);
- add_post_meta($post_id, 'wpcf-image', $att_url);
- }
- }
- //обработать остальные кастомные поля
- if(isset($post_data['custom_data']['images'])) {
- unset($post_data['custom_data']['images']);
- }
- if(count($post_data['custom_data'])!==0){
- foreach ($post_data['custom_data'] as $key => $value) {
- # code...
- if($curr_tax = taxonomy_exists($key)){
- //добавить термин в таксономию $key
- wp_set_object_terms( $post_id, $value, $key);
- }
- elseif (gettype($value) == 'string' OR gettype($value) == 'int')
- {
- add_post_meta($post_id, $key, $value);
- }elseif (gettype($value) == 'array') {
- foreach ($value as $val) {
- # code...
- add_post_meta($post_id, $key, $val);
- }
- }
- }
- }
- return $post_id;
- }
- }
- function convFieldToTerm($args){//конвертация полей в термины таксономии НЕДОДЕЛАНА
- $blog_id = (int) $args[0];
- $username = $args[1];
- $password = $args[2];
- $post_data = $args[3];
- //аутентификация и валидация
- {
- $user = wp_authenticate($username, $password);
- if (is_wp_error($user)) {
- return new IXR_Error('403', 'Не верные логин и пароль');
- }
- wp_set_current_user($user->ID);
- if (!current_user_can('edit_private_posts')) {
- return new IXR_Error(401, __('Sorry, you cannot edit private posts.'));
- }
- if (!is_array($post_data)) {
- return new IXR_Error(401, __('Sorry, no post data not found.'));
- }
- }
- return($post_data);
- // foreach ($post_data('convertions') as $key => $value) {
- // # code...
- // $posts = get_posts(array(
- // 'post_type'=> $data['post_type'],
- // 'meta_key' => $key
- // )
- // );
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement