/** * Webhook callback as triggered from GitHub push. * * Reads the Webhook payload and syncs posts as necessary. * * @return boolean */ public function pull_posts() { $this->set_ajax(); 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::pull_posts()'))); } if (!$this->app->request()->is_secret_valid()) { return $this->app->response()->error(new WP_Error('invalid_headers', __('Failed to validate secret.', 'wordpress-github-sync'))); } $payload = $this->app->request()->payload(); if (!$payload->should_import()) { return $this->app->response()->error(new WP_Error('invalid_payload', sprintf(__("%s won't be imported.", 'wordpress-github-sync'), strtolower($payload->get_commit_id())))); } $this->app->semaphore()->lock(); remove_action('save_post', array($this, 'export_post')); remove_action('save_post', array($this, 'delete_post')); $result = $this->app->import()->payload($payload); $this->app->semaphore()->unlock(); if (is_wp_error($result)) { return $this->app->response()->error($result); } return $this->app->response()->success($result); }