/**
  * Returns whether payload should be imported.
  *
  * @return bool
  */
 public function should_import()
 {
     // @todo how do we get this without importing the whole api object just for this?
     if (strtolower($this->data->repository->full_name) !== strtolower($this->app->api()->fetch()->repository())) {
         return false;
     }
     // The last term in the ref is the payload_branch name.
     $refs = explode('/', $this->data->ref);
     $payload_branch = array_pop($refs);
     $sync_branch = apply_filters('wpghs_sync_branch', 'master');
     if (!$sync_branch) {
         throw new Exception(__('Sync branch not set. Filter `wpghs_sync_branch` misconfigured.', 'wordpress-github-sync'));
     }
     if ($sync_branch !== $payload_branch) {
         return false;
     }
     // We add a tag to commits we push out, so we shouldn't pull them in again.
     $tag = apply_filters('wpghs_commit_msg_tag', 'wpghs');
     if (!$tag) {
         throw new Exception(__('Commit message tag not set. Filter `wpghs_commit_msg_tag` misconfigured.', 'wordpress-github-sync'));
     }
     if ($tag === substr($this->message(), -1 * strlen($tag))) {
         return false;
     }
     return true;
 }
Example #2
0
 /**
  * Imports a single blob content into matching post.
  *
  * @param WordPress_GitHub_Sync_Blob $blob Blob to transform into a Post.
  *
  * @return WordPress_GitHub_Sync_Post
  */
 protected function blob_to_post(WordPress_GitHub_Sync_Blob $blob)
 {
     $args = array('post_content' => $blob->content_import());
     $meta = $blob->meta();
     if ($meta) {
         if (array_key_exists('layout', $meta)) {
             $args['post_type'] = $meta['layout'];
             unset($meta['layout']);
         }
         if (array_key_exists('published', $meta)) {
             $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft';
             unset($meta['published']);
         }
         if (array_key_exists('post_title', $meta)) {
             $args['post_title'] = $meta['post_title'];
             unset($meta['post_title']);
         }
         if (array_key_exists('ID', $meta)) {
             $args['ID'] = $meta['ID'];
             unset($meta['ID']);
         }
     }
     $meta['_sha'] = $blob->sha();
     $post = new WordPress_GitHub_Sync_Post($args, $this->app->api());
     $post->set_meta($meta);
     return $post;
 }
 /**
  * Queries for a post by provided sha.
  *
  * @param string $sha Post sha to fetch by.
  *
  * @return WordPress_GitHub_Sync_Post|WP_Error
  */
 public function fetch_by_sha($sha)
 {
     $query = new WP_Query(array('meta_key' => '_sha', 'meta_value' => $sha, 'meta_compare' => '=', 'posts_per_page' => 1, 'fields' => 'ids'));
     $post_id = $query->get_posts();
     $post_id = array_pop($post_id);
     if (!$post_id) {
         return new WP_Error('sha_not_found', sprintf(__('Post for sha %s not found.', 'wordpress-github-sync'), $sha));
     }
     return new WordPress_GitHub_Sync_Post($post_id, $this->app->api());
 }
Example #4
0
 /**
  * Fetches the provided sha or the repository's
  * master branch and caches it.
  *
  * ## OPTIONS
  *
  * <user_id>
  * : The user ID you'd like to save the commit as
  *
  * ## EXAMPLES
  *
  *     wp wpghs prime --branch=master
  *     wp wpghs prime --sha=<commit_sha>
  *
  * @synopsis [--sha=<commit_sha>] [--branch]
  *
  * @param array $args Command arguments.
  * @param array $assoc_args Command associated arguments.
  */
 public function prime($args, $assoc_args)
 {
     if (isset($assoc_args['branch'])) {
         WP_CLI::line(__('Starting branch import.', 'wordpress-github-sync'));
         $commit = $this->app->api()->fetch()->master();
         if (is_wp_error($commit)) {
             WP_CLI::error(sprintf(__('Failed to import and cache branch with error: %s', 'wordpress-github-sync'), $commit->get_error_message()));
         } else {
             WP_CLI::success(sprintf(__('Successfully imported and cached commit %s from branch.', 'wordpress-github-sync'), $commit->sha()));
         }
     } else {
         if (isset($assoc_args['sha'])) {
             WP_CLI::line('Starting sha import.');
             $commit = $this->app->api()->fetch()->commit($assoc_args['sha']);
             WP_CLI::success(sprintf(__('Successfully imported and cached commit %s.', 'wordpress-github-sync'), $commit->sha()));
         } else {
             WP_CLI::error('Invalid fetch.');
         }
     }
 }
 /**
  * Use the new tree to save sha data
  * for all the updated posts.
  *
  * @param WordPress_GitHub_Sync_Post[] $posts Posts to fetch updated shas for.
  *
  * @return string|WP_Error
  */
 protected function update_shas(array $posts)
 {
     $master = $this->app->api()->fetch()->master();
     $attempts = 1;
     while (is_wp_error($master) && $attempts < 5) {
         $master = $this->app->api()->fetch()->master();
         $attempts++;
     }
     if (is_wp_error($master)) {
         // @todo throw a big warning! not having the latest shas is BAD
         // Solution: Show error message and link to kick off sha importing.
         return $master;
     }
     foreach ($posts as $post) {
         $blob = $master->tree()->get_blob_by_path($post->github_path());
         if ($blob) {
             $this->app->database()->set_post_sha($post, $blob->sha());
         }
     }
     return __('Export to GitHub completed successfully.', 'wordpress-github-sync');
 }