function json_save_media()
 {
     global $wpdb;
     $defaults = array('_method' => 'create', 'type' => 'image', 'description' => '', 'attachment_id' => '', 'embed' => '', 'featured' => 0, 'published' => 1);
     if (isset($_POST['test'])) {
         // Temp workaround for mysterious issue
         if ($_POST['test'] == '\\"') {
             foreach ($_POST as $key => $value) {
                 $_POST[$key] = stripslashes($value);
             }
         }
     }
     $args = array_merge($defaults, $_POST);
     $nonce = isset($_GET['nonce']) ? $_GET['nonce'] : '';
     if (!wp_verify_nonce($nonce, 'portfolio_media_nonce')) {
         echo 'Error: permission denied';
         die(1);
     }
     // create new
     if ($args['_method'] == 'create') {
         $media_item = PortfolioMedia::create($this->post_id);
         if ($media_item->exists()) {
             $media_item->meta_type = $args['type'];
             $media_item->meta_attachment_id = $args['attachment_id'];
             /**
              * Set menu order to current page's ID, it will be the highest
              * for the newest post.
              */
             $media_item->menu_order = $media_item->post->ID;
             echo json_encode($media_item->save()->to_array());
             die(0);
         }
         echo 'Error: unable to create media file.';
         die(1);
     }
     // update existing
     if ($args['_method'] == 'update' && is_numeric($_GET['id'])) {
         $media_item = new PortfolioMedia($args['id']);
         if ($media_item->exists()) {
             /**
              * If `featured` attribute has changed,
              * then let's remove it from any other media item.
              */
             if ($args['featured'] != $media_item->meta_featured) {
                 // Remove featured from every other object.
                 foreach (PortfolioMedia::all($this->post_id) as $item) {
                     if ($item->post->ID != $args['id']) {
                         $item->meta_featured = 0;
                         $item->save();
                     }
                 }
             }
             $media_item->meta_description = $args['description'];
             $media_item->meta_attachment_id = $args['attachment_id'];
             $media_item->meta_screenshot_id = $args['screenshot_id'];
             $media_item->meta_embed = $args['embed'];
             $media_item->meta_featured = $args['featured'];
             $media_item->meta_published = $args['published'];
             $media_item->post->menu_order = $args['order'];
             $media_item->save();
             echo json_encode($media_item->to_array());
             //echo '0';
             die(0);
         } else {
             echo 'Error: media item with ID ' . $args['id'] . ' cannot be found.';
             die(1);
         }
     }
     // delete record
     if ($args['_method'] == 'delete' && is_numeric($_GET['id'])) {
         $media_item = new PortfolioMedia($_GET['id'], $this->post_id);
         $media_item->delete();
         echo '0';
         die(0);
     }
     die('1');
 }
Esempio n. 2
0
 function upgrade_v1_to_v1_1()
 {
     /**
      * Upgrade Projects
      */
     $projects = PortfolioProject::all();
     if ($projects) {
         $args = array('post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'any', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC');
         foreach ($projects as $project) {
             $args['post_parent'] = $project->post->ID;
             $media = $project->get_media();
             if (!$this->clean && count($media)) {
                 break;
                 // Let's not update if we have some media already
             } elseif ($this->clean && count($media)) {
                 // Delete existing assigned media
                 foreach ($media as $m) {
                     $m->delete();
                 }
             }
             /**
              * Make sure featured image is added.
              */
             $featured_id = get_post_thumbnail_id($project->post->ID);
             $featured_image_added = false;
             $attachments = get_children($args);
             if ($attachments) {
                 $max_order = 0;
                 foreach ($attachments as $attachment) {
                     $max_order = $attachment->menu_order > $max_order ? $attachment->menu_order : $max_order;
                 }
                 foreach ($attachments as $attachment) {
                     $media_item = PortfolioMedia::create($project->post->ID);
                     $media_item->meta_type = 'image';
                     $media_item->meta_attachment_id = $attachment->ID;
                     $media_item->post->menu_order = $max_order - $attachment->menu_order;
                     // This is featured image
                     if ($featured_id == $attachment->ID) {
                         $media_item->meta_featured = 1;
                         $featured_image_added = true;
                     }
                     // If there was no featured image, then make the first one featured
                     if (!$featured_id && !$featured_image_added) {
                         $media_item->meta_featured = 1;
                         $featured_image_added = true;
                     }
                     $saved = $media_item->save();
                 }
             }
             /**
              * Featured image was not added, let's add it in the first position.
              */
             if (!$featured_image_added && $featured_id) {
                 $media_item = PortfolioMedia::create($project->post->ID);
                 $media_item->meta_type = 'image';
                 $media_item->meta_attachment_id = $featured_id;
                 $media_item->meta_featured = 1;
                 $media_item->post->menu_order = $max_order + 1;
                 $media_item->save();
             }
         }
     }
 }