/** * Imports a provided commit into the database. * * @param WordPress_GitHub_Sync_Commit|WP_Error $commit Commit to import. * * @return string|WP_Error */ protected function commit($commit) { if (is_wp_error($commit)) { return $commit; } if ($commit->already_synced()) { return new WP_Error('commit_synced', __('Already synced this commit.', 'wordpress-github-sync')); } $posts = array(); $new = array(); foreach ($commit->tree()->blobs() as $blob) { if (!$this->importable_blob($blob)) { continue; } $posts[] = $post = $this->blob_to_post($blob); if ($post->is_new()) { $new[] = $post; } } $result = $this->app->database()->save_posts($posts, $commit->author_email()); if (is_wp_error($result)) { return $result; } if ($new) { $result = $this->app->export()->new_posts($new); if (is_wp_error($result)) { return $result; } } return $posts; }
/** * Exports an individual post * all your posts to GitHub * * ## OPTIONS * * <post_id|all> * : The post ID to export or 'all' for full site * * <user_id> * : The user ID you'd like to save the commit as * * ## EXAMPLES * * wp wpghs export all 1 * wp wpghs export 1 1 * * @synopsis <post_id|all> <user_id> * * @param array $args Command arguments. */ public function export($args) { list($post_id, $user_id) = $args; if (!is_numeric($user_id)) { WP_CLI::error(__('Invalid user ID', 'wordpress-github-sync')); } $this->app->export()->set_user($user_id); if ('all' === $post_id) { WP_CLI::line(__('Starting full export to GitHub.', 'wordpress-github-sync')); $this->app->controller()->export_all(); } elseif (is_numeric($post_id)) { WP_CLI::line(sprintf(__('Exporting post ID to GitHub: %d', 'wordpress-github-sync'), $post_id)); $this->app->controller()->export_post((int) $post_id); } else { WP_CLI::error(__('Invalid post ID', 'wordpress-github-sync')); } }
/** * Removes the post from the tree. * * Called the delete_post hook. * * @param int $post_id Post ID. * * @return boolean */ public function delete_post($post_id) { if (!$this->app->semaphore()->is_open()) { return $this->app->response()->error(new WP_Error('semaphore_locked', sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'wordpress-github-sync'), 'Controller::delete_post()'))); } $this->app->semaphore()->lock(); $result = $this->app->export()->delete($post_id); $this->app->semaphore()->unlock(); if (is_wp_error($result)) { return $this->app->response()->error($result); } return $this->app->response()->success($result); }