public function test_should_export_published_post_type_to_plural_folder() { register_post_type('widget', array('labels' => array('name' => 'Widgets'))); $id = $this->factory->post->create(array('post_type' => 'widget')); $post = new WordPress_GitHub_Sync_Post($id, $this->api); $this->assertEquals('_widgets/', $post->github_directory()); }
/** * Imports a single blob content into matching post. * * @param stdClass $blob */ protected function import_blob($blob) { // Break out meta, if present preg_match('/(^---(.*?)---$)?(.*)/ms', $blob->content, $matches); $body = array_pop($matches); if (3 === count($matches)) { $meta = cyps_load($matches[2]); if (isset($meta['permalink'])) { $meta['permalink'] = str_replace(home_url(), '', get_permalink($meta['permalink'])); } } else { $meta = array(); } if (function_exists('wpmarkdown_markdown_to_html')) { $body = wpmarkdown_markdown_to_html($body); } $args = array('post_content' => apply_filters('wpghs_content_import', $body)); if (!empty($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']); } } if (!isset($args['ID'])) { // @todo create a revision when we add revision author support $post_id = wp_insert_post($args); } else { $post_id = wp_update_post($args); } /** @var WordPress_GitHub_Sync_Post $post */ $post = new WordPress_GitHub_Sync_Post($post_id); $post->set_sha($blob->sha); foreach ($meta as $key => $value) { update_post_meta($post_id, $key, $value); } WordPress_GitHub_Sync::write_log(__('Updated blob ', 'wordpress-github-sync') . $blob->sha); }
/** * Bulk push all posts to GitHub */ function export() { global $wpdb; $posts = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ('post', 'page' )"); foreach ($posts as $post_id) { $post = new WordPress_GitHub_Sync_Post($post_id); $post->push(); } }
/** * Calls the content API to get the post's contents and metadata * * Returns Object the response from the API * * @param WordPress_GitHub_Sync_Post $post Post to retrieve remote contents for. * * @return mixed */ public function remote_contents($post) { return $this->call('GET', $this->content_endpoint() . $post->github_path()); }
public function test_should_return_global_post_edit_url() { $this->assertEquals($this->post->github_edit_url(), get_the_github_edit_url()); }
/** * Verifies that both the post's status & type * are currently whitelisted * * @param WordPress_GitHub_Sync_Post $post Post to verify. * * @return boolean True if supported, false if not. */ protected function is_post_supported(WordPress_GitHub_Sync_Post $post) { if (wp_is_post_revision($post->id)) { return false; } // We need to allow trashed posts to be queried, but they are not whitelisted for export. if (!in_array($post->status(), $this->get_whitelisted_post_statuses()) && 'trash' !== $post->status()) { return false; } if (!in_array($post->type(), $this->get_whitelisted_post_types())) { return false; } if ($post->has_password()) { return false; } return true; }
/** * Returns a blob for the provided post. * * @param WordPress_GitHub_Sync_Post $post Post to retrieve blob for. * * @return WordPress_GitHub_Sync_Blob */ protected function get_blob_for_post(WordPress_GitHub_Sync_Post $post) { if ($blob = $this->get_blob_by_sha($post->sha())) { return $blob; } if ($blob = $this->get_blob_by_path($post->github_path())) { return $blob; } return $post->to_blob(); }
/** * 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; }
/** * Verifies that both the post's status & type * are currently whitelisted * * @param WordPress_GitHub_Sync_Post $post post to verify * @return boolean true if supported, false if not */ protected function is_post_supported($post) { if (!in_array($post->status(), $this->get_whitelisted_post_statuses())) { return false; } if (!in_array($post->type(), $this->get_whitelisted_post_types())) { return false; } if ($post->has_password()) { return false; } return true; }
/** * Use the new tree to save sha data * for all the updated posts */ public function save_post_shas($tree) { foreach ($this->posts as $post_id) { $post = new WordPress_GitHub_Sync_Post($post_id); $match = false; foreach ($tree as $blob) { // this might be a problem if the filename changed since it was set // (i.e. post updated in middle mass export) // solution? if ($post->github_path() === $blob->path) { $post->set_sha($blob->sha); $match = true; break; } } if (!$match) { WordPress_GitHub_Sync::write_log(__('No sha matched for post ID ', WordPress_GitHub_Sync::$text_domain) . $post_id); } } }
/** * Returns the URL to edit the current post on GitHub. * * @return string */ function get_the_github_edit_url() { $wpghs_post = new WordPress_GitHub_Sync_Post(get_the_ID()); return $wpghs_post->github_edit_url(); }
public function test_should_build_github_edit_url() { $post = new WordPress_GitHub_Sync_Post($this->id); $this->assertEquals('https://github.com/owner/repo/edit/master/_posts/' . get_the_date('Y-m-d-', $this->id) . $this->post->post_name . '.md', $post->github_edit_url()); }
public function test_should_remove_post_by_path() { $sha = '1234567890qwertyuiop'; $this->blob->shouldReceive('sha')->andReturn($sha); $post_id = $this->factory->post->create(); $post = new WordPress_GitHub_Sync_Post($post_id, $this->api); $this->blob->shouldReceive('path')->andReturn($post->github_path()); $tree = new WordPress_GitHub_Sync_Tree(new stdClass()); $tree->add_blob($this->blob); $tree->remove_post_from_tree($post); $this->assertCount(0, $blobs = $tree->blobs()); $this->assertTrue($tree->is_changed()); $body = $tree->to_body(); $this->assertArrayHasKey('tree', $body); $this->assertCount(0, $body['tree']); }
/** * Use the new tree to save sha data * for all the updated posts */ public function save_post_shas() { foreach ($this->ids as $post_id) { $post = new WordPress_GitHub_Sync_Post($post_id); $blob = $this->tree->get_blob_for_path($post->github_path()); if ($blob) { $post->set_sha($blob->sha); } else { WordPress_GitHub_Sync::write_log(sprintf(__('No sha matched for post ID %d', 'wordpress-github-sync'), $post_id)); } } }
/** * Creates a blob with the data required for the tree. * * @param WordPress_GitHub_Sync_Post $post * * @return stdClass */ public function blob_from_post($post) { $blob = new stdClass(); $blob->path = $post->github_path(); $blob->mode = '100644'; $blob->type = 'blob'; $blob->content = $post->github_content(); return $blob; }