/** * Reads the Webhook payload and syncs posts as necessary * * @param stdClass $payload * * @return array */ public function pull($payload) { if (strtolower($payload->repository->full_name) !== strtolower($this->api->repository())) { $msg = strtolower($payload->repository->full_name) . __(' is an invalid repository.', 'wordpress-github-sync'); WordPress_GitHub_Sync::write_log($msg); return array('result' => 'error', 'message' => $msg); } // the last term in the ref is the branch name $refs = explode('/', $payload->ref); $branch = array_pop($refs); if ('master' !== $branch) { $msg = __('Not on the master branch.', 'wordpress-github-sync'); WordPress_GitHub_Sync::write_log($msg); return array('result' => 'error', 'message' => $msg); } // We add wpghs to commits we push out, so we shouldn't pull them in again if ('wpghs' === substr($payload->head_commit->message, -5)) { $msg = __('Already synced this commit.', 'wordpress-github-sync'); WordPress_GitHub_Sync::write_log($msg); return array('result' => 'error', 'message' => $msg); } $commit = $this->api->get_commit($payload->head_commit->id); if (is_wp_error($commit)) { $msg = __('Failed getting commit with error: ', 'wordpress-github-sync') . $commit->get_error_message(); WordPress_GitHub_Sync::write_log($msg); return array('result' => 'error', 'message' => $msg); } $import = new WordPress_GitHub_Sync_Import(); $import->run($commit->tree->sha); // Deleting posts from a payload is the only place // we need to search posts by path; another way? $removed = array(); foreach ($payload->commits as $commit) { $removed = array_merge($removed, $commit->removed); } foreach (array_unique($removed) as $path) { $post = new WordPress_GitHub_Sync_Post($path); wp_delete_post($post->id); } $msg = __('Payload processed', 'wordpress-github-sync'); WordPress_GitHub_Sync::write_log($msg); return array('result' => 'success', 'message' => $msg); }
/** * Reads the Webhook payload and syncs posts as necessary * * @param stdClass $payload * * @return array */ public function pull($payload) { if (strtolower($payload->repository->full_name) !== strtolower($this->api->repository())) { $msg = strtolower($payload->repository->full_name) . __(' is an invalid repository.', 'wordpress-github-sync'); WordPress_GitHub_Sync::write_log($msg); return array('result' => 'error', 'message' => $msg); } // the last term in the ref is the branch name $refs = explode('/', $payload->ref); $branch = array_pop($refs); if ('master' !== $branch) { $msg = __('Not on the master branch.', 'wordpress-github-sync'); WordPress_GitHub_Sync::write_log($msg); return array('result' => 'error', 'message' => $msg); } // We add wpghs to commits we push out, so we shouldn't pull them in again if ('wpghs' === substr($payload->head_commit->message, -5)) { $msg = __('Already synced this commit.', 'wordpress-github-sync'); WordPress_GitHub_Sync::write_log($msg); return array('result' => 'error', 'message' => $msg); } $commit = $this->api->get_commit($payload->head_commit->id); if (is_wp_error($commit)) { $msg = sprintf(__('Failed getting commit with error: %s', 'wordpress-github-sync'), $commit->get_error_message()); WordPress_GitHub_Sync::write_log($msg); return array('result' => 'error', 'message' => $msg); } $import = new WordPress_GitHub_Sync_Import(); $import->run($commit->tree->sha); $user = get_user_by('email', $payload->head_commit->author->email); if (!$user) { // use the default user $user = get_user_by('id', get_option('wpghs_default_user')); } // if we can't find a user and a default hasn't been set, // we're just going to set the revision author to 0 update_option('_wpghs_export_user_id', $user ? $user->ID : 0); global $wpdb; if ($updated_posts = $import->updated_posts()) { foreach ($updated_posts as $post_id) { $revision = wp_get_post_revision($post_id); if (!$revision) { $revision = wp_save_post_revision($post_id); if (!$revision || is_wp_error($revision)) { // there was a problem saving a new revision continue; } // wp_save_post_revision returns the ID, whereas get_post_revision returns the whole object // in order to be consistent, let's make sure we have the whole object before continuing $revision = get_post($revision); } $wpdb->update($wpdb->posts, array('post_author' => (int) get_option('_wpghs_export_user_id')), array('ID' => $revision->ID), array('%d'), array('%d')); } } // Deleting posts from a payload is the only place // we need to search posts by path; another way? $removed = array(); foreach ($payload->commits as $commit) { $removed = array_merge($removed, $commit->removed); } foreach (array_unique($removed) as $path) { $post = new WordPress_GitHub_Sync_Post($path); wp_delete_post($post->id); } if ($new_posts = $import->new_posts()) { // disable the lock to allow exporting global $wpghs; $wpghs->push_lock = false; WordPress_GitHub_Sync::write_log(sprintf(__('Updating new posts with IDs: %s', 'wordpress-github-sync'), implode(', ', $new_posts))); foreach ($new_posts as $post_id) { $wpdb->update($wpdb->posts, array('post_author' => (int) get_option('_wpghs_export_user_id')), array('ID' => $post_id), array('%d'), array('%d')); } $msg = apply_filters('wpghs_commit_msg_new_posts', 'Updating new posts from WordPress at ' . site_url() . ' (' . get_bloginfo('name') . ')') . ' - wpghs'; $export = new WordPress_GitHub_Sync_Export($new_posts, $msg); $export->run(); } $msg = __('Payload processed', 'wordpress-github-sync'); WordPress_GitHub_Sync::write_log($msg); return array('result' => 'success', 'message' => $msg); }