Example #1
0
 /**
  * Gets and builds a Commit model based on a WP_Post object
  *
  * @param  WP_Post $post model's WP_Post object
  * @return object       Commit model object
  * @since 0.5.0
  */
 public function by_post($post)
 {
     // @todo validate this post so it's a revision & pulling from a Zip
     $post->post_status = get_post_status($post->post_parent);
     $commit = $this->adapter->build('commit')->by_post($post);
     $commit->set_head_gist_id($this->head_query->gist_id_by_post_id($commit->get_head_id()));
     $commit->set_sync($this->head_query->sync_by_post_id($commit->get_head_id()));
     $meta = get_metadata('post', $commit->get_ID(), '_wpgp_commit_meta', true);
     foreach ($meta['state_ids'] as $state_id) {
         $state = $this->state_by_id($state_id);
         $commit->add_state($state);
     }
     if (array_key_exists('gist_id', $meta)) {
         $commit->set_gist_id($meta['gist_id']);
     }
     return $commit;
 }
Example #2
0
 /**
  * Saves new States for array of Files IDs
  *
  * @param  array $file_ids Array of File IDs
  * @return bool            whether this saved new files
  * @since  0.5.0
  */
 protected function states_by_file_ids($file_ids)
 {
     $changed = false;
     foreach ($file_ids as $file_id) {
         if (!is_integer($file_id)) {
             continue;
         }
         $state_id = wp_save_post_revision($file_id);
         $revisions = wp_get_post_revisions($file_id);
         if (null === $state_id || 0 === $state_id) {
             $prev_revision = array_shift($revisions);
             $state_id = $prev_revision->ID;
             $this->commit_meta['state_ids'][] = $state_id;
             continue;
         }
         $state_meta = array();
         // Removing the latest revision first
         array_shift($revisions);
         if (empty($revisions)) {
             // if not, set status to 'new'
             $state_meta['status'] = 'new';
         } else {
             // if so, set status to 'updated'
             $state_meta['status'] = 'updated';
             // Set prev filename as current gist_id
             $prev_revision = array_shift($revisions);
             $prev_state = $this->commit_query->state_by_id($prev_revision->ID);
             $state_meta['gist_id'] = $prev_state->get_filename();
         }
         update_metadata('post', $state_id, "_wpgp_state_meta", $state_meta);
         $lang_slug = $this->head_query->language_by_post_id($file_id)->get_slug();
         wp_set_object_terms($state_id, $lang_slug, 'wpgp_language', false);
         $this->changed = true;
         $changed = true;
         $this->commit_meta['state_ids'][] = $state_id;
     }
     return $changed;
 }
Example #3
0
 /**
  * Save the Zip to the database
  *
  * @param  Zip $post
  * @return int|\WP_Error    post_id on success, WP_Error on failure
  * @since  0.5.0
  */
 public function by_zip($zip)
 {
     $data = array('post_title' => $zip->get_description(), 'post_status' => $zip->get_status(), 'post_password' => $zip->get_password());
     if ($zip->get_ID()) {
         $data['ID'] = $zip->get_ID();
     }
     if ('' !== $zip->get_create_date()) {
         $data['post_date_gmt'] = $zip->get_create_date();
         $data['post_date'] = get_date_from_gmt($zip->get_create_date());
     }
     $result = $this->by_array($data);
     if (is_wp_error($result)) {
         return $result;
     }
     $zip_id = $result;
     $results = array();
     $results['zip'] = $zip_id;
     $results['files'] = array();
     $results['deleted'] = array();
     unset($result);
     if ('none' !== $zip->get_gist_id()) {
         $result = $this->set_gist_id($zip_id, $zip->get_gist_id());
         if (is_wp_error($result)) {
             return $result;
         }
     }
     $result = $this->set_sync($zip_id, $zip->get_sync());
     if (is_wp_error($result)) {
         return $result;
     }
     $files = $zip->get_files();
     $files_to_delete = $this->head_query->files_by_post(get_post($zip_id));
     foreach ($files as $id => $file) {
         $data = array('post_title' => $file->get_slug(), 'post_content' => $file->get_code(), 'post_status' => $zip->get_status(), 'post_parent' => $zip_id, 'post_password' => $zip->get_password());
         if ($file->get_ID()) {
             $data['ID'] = $file->get_ID();
         }
         $result = $this->by_array($data);
         if (is_wp_error($result)) {
             return $result;
         }
         $file_id = $result;
         unset($result);
         $result = wp_set_object_terms($file_id, $file->get_language()->get_slug(), 'wpgp_language', false);
         if (is_wp_error($result)) {
             return $result;
         }
         $results['files'][] = $file_id;
         unset($files_to_delete[$file_id]);
     }
     foreach ($files_to_delete as $id => $file) {
         $result = wp_update_post(array('ID' => $id, 'post_type' => 'gistpen', 'post_status' => 'inherit'), true);
         if (is_wp_error($result)) {
             return $result;
         }
         $deleted_file_id = $result;
         unset($result);
         $results['deleted'][] = $deleted_file_id;
     }
     return $results;
 }