Created
February 18, 2013 00:03
-
-
Save viktoras25/4974262 to your computer and use it in GitHub Desktop.
Git to Wordpress posting
http://www.charnad.com/blog/git-to-wordpress-posting/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Copyright (c) 2013 Viktoras Bezaras <charnad@gmail.com> | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
*/ | |
chdir('/home/viktoras/blocto'); | |
require './../public_html/blog/wp-load.php'; | |
/** | |
* This will prevent KSES from modifying the post content. | |
*/ | |
kses_remove_filters(); | |
ini_set('xdebug.var_display_max_data', -1); | |
function strstr53($haystack, $needle, $before_needle = false) | |
{ | |
if (!$before_needle) return strstr($haystack, $needle); | |
else return substr($haystack, 0, strpos($haystack, $needle)); | |
} | |
function getPostByName($name) | |
{ | |
global $wpdb; | |
$post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= 'post'", $name ) ); | |
if ($post) { | |
return get_post( $post, OBJECT ); | |
} | |
return null; | |
} | |
function getLocalPosts() | |
{ | |
$posts = array(); | |
foreach(glob('posts/*.md') as $file) { | |
$post = array( | |
'name' => basename($file, '.md') | |
); | |
// Get content and strip out CR symbol | |
$content = str_replace("\r", "", file_get_contents($file)); | |
// Extract file parts | |
$meta = strstr53($content, "\n\n\n", true); | |
$post_content = ltrim(strstr($content, "\n\n\n")); | |
if (false === $meta || false === $post_content) { | |
continue; | |
} | |
$post['meta'] = parseMeta($meta); | |
$post['content'] = $post_content; | |
$posts[] = $post; | |
} | |
return $posts; | |
} | |
function parseMeta($meta) | |
{ | |
// Initialize with defaults | |
$result = array( | |
'tags' => '', | |
'categories' => '', | |
'type' => 'post', | |
'author' => '1', | |
'title' => '', | |
'post_status' => 'open', | |
'comment_status' => 'open' | |
); | |
$meta_parts = explode("\n", $meta); | |
foreach ($meta_parts as $meta_part) { | |
$name = strtolower(trim( strstr53($meta_part, ':', true) )); | |
$value = trim(ltrim( strstr($meta_part, ':') , ':')); | |
if (($name == 'tags' || $name == 'categories') && !empty($value)) { | |
$value = array_map('trim', explode(',', $value)); | |
} | |
$result = array_merge($result, array($name => $value)); | |
} | |
return $result; | |
} | |
function toWPPOST(array $post_data) | |
{ | |
$rawPost = new stdClass(); | |
$rawPost->post_author = $post_data['meta']['author']; | |
$rawPost->post_date = $post_data['post_date']; | |
$rawPost->post_date_gmt = $post_data['post_date_gmt']; | |
$rawPost->post_content = $post_data['content']; | |
$rawPost->post_title = $post_data['meta']['title']; | |
$rawPost->comment_status = $post_data['meta']['comment_status']; | |
$rawPost->post_name = $post_data['name']; | |
$rawPost->post_type = $post_data['meta']['type']; | |
$rawPost->post_status = $post_data['meta']['post_status']; | |
$rawPost->ID = $post_data['ID']; | |
return new WP_Post($rawPost); | |
} | |
function convertCatNamesToIds($names, $createNew = false) | |
{ | |
if (!is_array($names) || empty($names)) { | |
return array(); | |
} | |
$categories = array(); | |
foreach ($names as $category) { | |
$categoryObject = get_category_by_slug(trim($category)); | |
// Category not found | |
if ($categoryObject) { | |
$categories[] = $categoryObject->cat_ID; | |
} elseif ($createNew) { | |
// Create new one | |
$categories[] = wp_create_category(trim($category)); | |
} | |
} | |
return $categories; | |
} | |
function compareTerms($post_id, $terms, $taxonomy) | |
{ | |
if (!is_array($terms)) { | |
$terms = array(); | |
} | |
$existingTerms = wp_get_post_terms($post_id, $taxonomy); | |
$existingTermsNames = array(); | |
foreach ($existingTerms as $term) { | |
$existingTermsNames[] = $term->name; | |
} | |
// Make both arrays lowercase | |
$terms = array_map('strtolower', $terms); | |
$existingTermsNames = array_map('strtolower', $existingTermsNames); | |
// Check both ways, because arrays might be empty | |
return count(array_diff($existingTermsNames, $terms) + array_diff($terms, $existingTermsNames)) !== 0; | |
} | |
$localPosts = getLocalPosts(); | |
printf("Found %d local posts\n", count($localPosts)); | |
foreach ($localPosts as $post) { | |
printf("Processing '%s'...\n", $post['name']); | |
// Create new post | |
$existingPost = getPostByName($post['name']); | |
if (empty($existingPost)) { | |
$wpPost = toWPPOST($post); | |
$id = wp_insert_post($wpPost); | |
wp_set_post_terms($id, $post['meta']['tags'], 'post_tag'); | |
$categories = convertCatNamesToIds($post['meta']['categories']); | |
wp_set_post_terms($id, $categories, 'category'); | |
printf("Created new post with id: %d\n", $id); | |
continue; | |
} | |
// Check tags | |
if (compareTerms($existingPost->ID, $post['meta']['tags'], 'post_tag')) { | |
wp_set_post_terms($existingPost->ID, $post['meta']['tags'], 'post_tag'); | |
echo "Updated tags.\n"; | |
} | |
// Check categories | |
if (compareTerms($existingPost->ID, $post['meta']['categories'], 'category')) { | |
$categories = convertCatNamesToIds($post['meta']['categories']); | |
wp_set_post_terms($existingPost->ID, $categories, 'category'); | |
echo "Updated categories.\n"; | |
} | |
// Preserving values | |
$post['ID'] = $existingPost->ID; | |
$post['post_date'] = $existingPost->post_date; | |
$post['post_date_gmt'] = $existingPost->post_date_gmt; | |
$wpPost = toWPPOST($post); | |
// Compare values to get changes | |
$update = array(); | |
$fields = array('post_content', 'post_author', 'post_title', 'post_status', 'comment_status', 'post_type'); | |
foreach ($fields as $field) { | |
if ($wpPost->$field !== $existingPost->$field) { | |
$update[$field] = $wpPost->$field; | |
} | |
} | |
if (!empty($update)) { | |
$update['ID'] = $post['ID']; | |
wp_update_post($wpPost); | |
wp_set_post_terms($existingPost->ID, $post['meta']['tags'], 'post_tag'); | |
wp_set_post_terms($existingPost->ID, $post['meta']['categories'], 'category'); | |
printf('Updating: %s\n', implode(', ', array_keys($update))); | |
} else { | |
echo "Post not modified - skipped.\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment