/**
  * Upload
  * Ajax callback function
  *
  * @return string Error or (XML-)response
  */
 static function handle_upload()
 {
     global $wpdb;
     $post_id = is_numeric($_REQUEST['post_id']) ? $_REQUEST['post_id'] : 0;
     $field_id = isset($_REQUEST['field_id']) ? $_REQUEST['field_id'] : '';
     check_ajax_referer("rwmb-upload-images_{$field_id}");
     // You can use WP's wp_handle_upload() function:
     $file = $_FILES['async-upload'];
     $file_attr = wp_handle_upload($file, array('test_form' => false));
     //Get next menu_order
     $meta = get_post_meta($post_id, $field_id, false);
     if (empty($meta)) {
         $next = 0;
     } else {
         $meta = implode(',', (array) $meta);
         $max = $wpdb->get_var("\n\t\t\t\t\tSELECT MAX(menu_order) FROM {$wpdb->posts}\n\t\t\t\t\tWHERE post_type = 'attachment'\n\t\t\t\t\tAND ID in ({$meta})\n\t\t\t\t");
         $next = is_numeric($max) ? (int) $max + 1 : 0;
     }
     $attachment = array('guid' => $file_attr['url'], 'post_mime_type' => $file_attr['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file['name'])), 'post_content' => '', 'post_status' => 'inherit', 'menu_order' => $next);
     // Adds file as attachment to WordPress
     $id = wp_insert_attachment($attachment, $file_attr['file'], $post_id);
     if (!is_wp_error($id)) {
         wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file_attr['file']));
         // Save file ID in meta field
         add_post_meta($post_id, $field_id, $id, false);
         RW_Meta_Box::ajax_response(self::img_html($id), 'success');
     }
     exit;
 }
 /**
  * Ajax callback for attaching media to field
  *
  * @return void
  */
 static function wp_ajax_attach_media()
 {
     $post_id = is_numeric($_REQUEST['post_id']) ? $_REQUEST['post_id'] : 0;
     $field_id = isset($_POST['field_id']) ? $_POST['field_id'] : 0;
     $attachment_id = isset($_POST['attachment_id']) ? $_POST['attachment_id'] : 0;
     check_ajax_referer("rwmb-attach-media_{$field_id}");
     add_post_meta($post_id, $field_id, $attachment_id, false);
     RW_Meta_Box::ajax_response(self::img_html($attachment_id), 'success');
 }
Esempio n. 3
0
 /**
  * Ajax callback for returning oEmbed HTML
  *
  * @return void
  */
 static function wp_ajax_get_embed()
 {
     global $post;
     $url = isset($_POST['oembed_url']) ? $_POST['oembed_url'] : 0;
     $post_id = is_numeric($_REQUEST['post_id']) ? (int) $_REQUEST['post_id'] : 0;
     if (isset($_REQUEST['post_id'])) {
         $post = get_post($_REQUEST['post_id']);
     }
     $embed = self::get_embed($url);
     RW_Meta_Box::ajax_response($embed, 'success');
     exit;
 }
Esempio n. 4
0
 /**
  * Ajax callback for reordering images
  *
  * @return void
  */
 static function wp_ajax_reorder_images()
 {
     $field_id = isset($_POST['field_id']) ? $_POST['field_id'] : 0;
     $order = isset($_POST['order']) ? $_POST['order'] : 0;
     check_admin_referer("rwmb-reorder-images_{$field_id}");
     parse_str($order, $items);
     $items = $items['item'];
     $order = 1;
     foreach ($items as $item) {
         wp_update_post(array('ID' => $item, 'menu_order' => $order++));
     }
     RW_Meta_Box::ajax_response(__('Order saved', 'wpsight'), 'success');
 }
Esempio n. 5
0
 /**
  * Ajax callback for deleting files.
  * Modified from a function used by "Verve Meta Boxes" plugin
  *
  * @link http://goo.gl/LzYSq
  * @return void
  */
 static function wp_ajax_delete_file()
 {
     $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
     $field_id = isset($_POST['field_id']) ? $_POST['field_id'] : 0;
     $attachment_id = isset($_POST['attachment_id']) ? intval($_POST['attachment_id']) : 0;
     check_admin_referer("rwmb-delete-file_{$field_id}");
     $ok = delete_post_meta($post_id, $field_id, $attachment_id);
     $ok = $ok && wp_delete_attachment($attachment_id);
     if ($ok) {
         RW_Meta_Box::ajax_response('', 'success');
     } else {
         RW_Meta_Box::ajax_response(__('Error: Cannot delete file', 'bootstrap'), 'error');
     }
 }
Esempio n. 6
0
 /**
  * Ajax callback for reordering images
  *
  * @return void
  */
 static function wp_ajax_reorder_images()
 {
     $field_id = isset($_POST['field_id']) ? $_POST['field_id'] : 0;
     $order = isset($_POST['order']) ? $_POST['order'] : 0;
     $post_id = isset($_POST['post_id']) ? (int) $_POST['post_id'] : 0;
     check_ajax_referer("rwmb-reorder-images_{$field_id}");
     parse_str($order, $items);
     $items = $items['item'];
     delete_post_meta($post_id, $field_id);
     foreach ($items as $item) {
         add_post_meta($post_id, $field_id, $item, false);
     }
     RW_Meta_Box::ajax_response(__('Order saved', 'rwmb'), 'success');
 }
Esempio n. 7
0
 static function wp_ajax_save_map()
 {
     $post_id = isset($_POST['post_id']) ? $_POST['post_id'] : 0;
     $map_address = isset($_POST['map_address']) ? $_POST['map_address'] : '';
     $map_location = isset($_POST['map_location']) ? $_POST['map_location'] : '';
     if ($post_id) {
         if (!empty($map_location)) {
             update_post_meta($post_id, '_map_geo', $map_location);
             update_post_meta($post_id, '_map_location', $map_location);
         }
         if (!empty($map_address)) {
             update_post_meta($post_id, '_map_address', $map_address);
         }
         RW_Meta_Box::ajax_response(__('Map Address saved', 'wpsight'), 'success');
     } else {
         RW_Meta_Box::ajax_response(__('Map Address saving fail', 'wpsight'), 'error');
     }
 }
Esempio n. 8
0
 /**
  * Ajax callback for reordering images
  *
  * @return void
  */
 static function wp_ajax_reorder_images()
 {
     $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
     $field_id = isset($_POST['field_id']) ? $_POST['field_id'] : 0;
     $order = isset($_POST['order']) ? $_POST['order'] : 0;
     check_admin_referer("rwmb-reorder-images_{$field_id}");
     parse_str($order, $items);
     $items = $items['item'];
     $order = 1;
     // Delete old meta values
     delete_post_meta($post_id, $field_id);
     foreach ($items as $item) {
         wp_update_post(array('ID' => $item, 'post_parent' => $post_id, 'menu_order' => $order++));
         // Save images in that order to meta field
         // That helps retrieving values easier
         add_post_meta($post_id, $field_id, $item, false);
     }
     RW_Meta_Box::ajax_response(__('Order saved', 'rwmb'), 'success');
 }
Esempio n. 9
0
 /**
  * Upload
  * Ajax callback function
  *
  * @return string Error or (XML-)response
  */
 static function handle_upload()
 {
     $post_id = is_numeric($_REQUEST['post_id']) ? $_REQUEST['post_id'] : 0;
     $field_id = isset($_REQUEST['field_id']) ? $_REQUEST['field_id'] : '';
     check_admin_referer("rwmb-upload-images_{$field_id}");
     // You can use WP's wp_handle_upload() function:
     $file = $_FILES['async-upload'];
     $file_attr = wp_handle_upload($file, array('test_form' => false));
     $attachment = array('guid' => $file_attr['url'], 'post_mime_type' => $file_attr['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file['name'])), 'post_content' => '', 'post_status' => 'inherit');
     // Adds file as attachment to WordPress
     $id = wp_insert_attachment($attachment, $file_attr['file'], $post_id);
     if (!is_wp_error($id)) {
         wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file_attr['file']));
         // Save file ID in meta field
         add_post_meta($post_id, $field_id, $id, false);
         // Fake field array. We need ID and force_delete only
         $field = array('id' => $field_id, 'force_delete' => isset($_REQUEST['force_delete']) ? intval($_REQUEST['force_delete']) : 0);
         RW_Meta_Box::ajax_response(self::img_html($id, $field), 'success');
     }
     exit;
 }
Esempio n. 10
0
 /**
  * Ajax callback for deleting files.
  * Modified from a function used by "Verve Meta Boxes" plugin
  *
  * @link http://goo.gl/LzYSq
  * @return void
  */
 static function wp_ajax_delete_file()
 {
     $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
     $field_id = isset($_POST['field_id']) ? $_POST['field_id'] : 0;
     $attachment_id = isset($_POST['attachment_id']) ? intval($_POST['attachment_id']) : 0;
     $force_delete = isset($_POST['force_delete']) ? intval($_POST['force_delete']) : 0;
     check_ajax_referer("rwmb-delete-file_{$field_id}");
     // get saved ids
     $saved_ids = get_post_meta($post_id, $field_id, true);
     // sanitize
     $saved_ids = array_map('absint', $saved_ids);
     // exclude attachment from saved list
     $saved_ids = array_diff($saved_ids, array(absint($attachment_id)));
     // update
     update_post_meta($post_id, $field_id, $saved_ids);
     $ok = $force_delete ? wp_delete_attachment($attachment_id) : true;
     if ($ok) {
         RW_Meta_Box::ajax_response('', 'success');
     } else {
         RW_Meta_Box::ajax_response(__('Error: Cannot delete file', LANGUAGE_ZONE), 'error');
     }
     exit;
 }