Skip to content

Instantly share code, notes, and snippets.

@whyisjake
Created May 9, 2013 22:54
Show Gist options
  • Save whyisjake/5551216 to your computer and use it in GitHub Desktop.
Save whyisjake/5551216 to your computer and use it in GitHub Desktop.
<?php
WP_CLI::add_command( 'makerfaire', 'MAKE_CLI' );
class MAKE_CLI extends WP_CLI_Command {
/**
* Add tags and cats to posts.
* Read the category and tag out of the JSON array, and then assign to the post.
*
* @subcommand cats
*
*/
public function copy_category_to_tag( $args, $assoc_args ) {
$args = array(
'posts_per_page' => 1000,
'post_type' => 'mf_form',
'post_status' => 'any',
// Prevent new posts from affecting the order
'orderby' => 'ID',
'order' => 'ASC',
// Speed this up
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
);
// Get the first set of posts
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
global $post;
setup_postdata($post);
WP_CLI::line( get_the_title() );
$json_post = json_decode( str_replace( "\'", "'", get_the_content() ) );
if ( isset( $json_post->cats ) ) {
$catsobj = $json_post->cats;
} else {
$catsobj = null;
}
$cats = null;
if ( is_array( $catsobj ) ) {
$cats = $catsobj;
} else {
$cats = explode(',', $catsobj);
}
if ( !empty( $cats[0] ) ) {
WP_CLI::line('Categories:');
foreach ($cats as $cat) {
$result = wp_set_object_terms( get_the_ID(), $cat, 'category', true );
if ( !empty( $result ) ) {
WP_CLI::success( $cat );
} else {
WP_CLI::error( $cat );
}
}
}
if ( isset( $json_post->tags ) ) {
$tagsobj = $json_post->tags;
} else {
$tagsobj = null;
}
$tags = null;
if ( is_array( $tagsobj ) ) {
$tags = $tagsobj;
} else {
$tags = explode(',', $tagsobj);
}
if ( !empty( $tags[0] ) ) {
WP_CLI::line('Tags:');
foreach ($tags as $tag) {
$result = wp_set_object_terms( get_the_ID(), $tag, 'post_tag', true );
if ( !empty( $result ) ) {
WP_CLI::success( $tag );
} else {
WP_CLI::error( $tag );
}
}
}
WP_CLI::line( '' );
endwhile;
WP_CLI::success( "Boom!" );
}
/**
* Inserts places from Make: Projects
*
* @subcommand places
*
*/
public function mf_location_import() {
include_once 'placement.php';
foreach ($placement as $place) {
WP_CLI::line();
WP_CLI::line( get_the_title( $place['CS_ID'] ) );
$del = delete_post_meta( $place['CS_ID'], 'booth' );
$pid = add_post_meta( $place['CS_ID'], 'booth', $place['Location'] );
if ( !$del ) {
WP_CLI::warning( "Nothing to delete" );
} else {
WP_CLI::success( 'Deleted ' . $place['CS_ID'] );
}
if ( $pid == null ) {
WP_CLI::warning( "Booth number isn't set, unfortunately..." );
} else {
WP_CLI::success( 'Inserted booth number: ' . $place['Location'] );
}
if ( !empty( $place['New Subarea'] ) ) {
$result = wp_set_object_terms( $place['CS_ID'], $place['New Subarea'], 'location', false );
if ( !empty( $result ) ) {
WP_CLI::success( 'Subarea: ' . $place['New Subarea'] );
} else {
WP_CLI::warning( $place['New Subarea'] );
}
} else {
$result = wp_set_object_terms( $place['CS_ID'], $place['Area'], 'location', false );
if ( !empty( $result ) ) {
WP_CLI::success( 'Area was used, instead of subarea: ' . $place['Area'] );
} else {
WP_CLI::warning( $place['Area'] );
}
}
}
}
/**
* Add tags and cats to posts.
* Read the category and tag out of the JSON array, and then assign to the post.
*
* @subcommand makers
*
*/
public function mf_create_makers( $args, $assoc_args ) {
$args = array(
'posts_per_page' => 2000,
'post_type' => 'mf_form',
'post_status' => 'any',
// Prevent new posts from affecting the order
'orderby' => 'ID',
'order' => 'ASC',
// Speed this up
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
);
// Get the first set of posts
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
global $post;
setup_postdata($post);
//WP_CLI::line( get_the_title() );
$exhibit = json_decode( html_entity_decode( str_replace( array("\'", "u03a9"), array("'", '&#8486;'), $post->post_content ), ENT_COMPAT, 'utf-8' ) );
$type = (isset($exhibit->form_type)) ? $exhibit->form_type : 'null' ;
// /$type = $exhibit->form_type;
if ($type == 'exhibit') {
if ($exhibit->maker == 'A group or association' ) {
$title = $exhibit->group_name ? $exhibit->group_name : $exhibit->name;
$maker = get_page_by_title( $title, 'OBJECT', 'mf_form' );
var_dump($maker);
echo (isset($maker->post_title)) ? WP_CLI::success( $maker->post_title ) : WP_CLI::warning( 'no title...' ) ;
if ( !is_object( $maker ) ) {
// Setup post object...
$content = ($exhibit->group_bio ? htmlspecialchars_decode( $exhibit->group_bio ) : null);
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_type' => 'maker'
);
$pid = wp_insert_post( $my_post );
if ( is_wp_error( $pid ) ) {
//WP_CLI::warning( $title );
} else {
//WP_CLI::success( $title );
}
$video = ($exhibit->project_video ? $exhibit->project_video : null);
$vid = update_post_meta( $pid, 'video', $video );
if ( !empty( $vid ) ) {
WP_CLI::success( $video );
} else {
WP_CLI::error( $video );
}
} else {
// $pid = $maker->post_ID;
// $video = ($exhibit->project_video ? $exhibit->project_video : null);
// $vid = update_post_meta( $pid, 'video', $video );
// if ( !empty( $vid ) ) {
// WP_CLI::success( $video );
// } else {
// WP_CLI::error( $video );
// }
}
}
// $url = $exhibit->group_photo;
// if (!empty($url)) {
// $url = add_query_arg( 'w', 80, $url );
// $url = add_query_arg( 'h', 80, $url );
// $url = add_query_arg( 'crop', 1, $url );
// $jsonpost["thumb_img_url"] = $url;
// $jsonpost["large_img_url"] = $exhibit->group_photo;
// } else {
// $jsonpost["thumb_img_url"] = null;
// $jsonpost["large_img_url"] = null;
// }
// $jsonpost['description'] = ($exhibit->group_bio ? htmlspecialchars_decode( $exhibit->group_bio ) : null);
// $jsonpost['youtube_url'] = ($exhibit->project_video ? $exhibit->project_video : null);
// } elseif ($exhibit->maker == 'One maker') {
// $jsonpost['name'] = ($exhibit->maker_name ? $exhibit->maker_name : $exhibit->name);
// $url = $exhibit->maker_photo;
// if (!empty($url)) {
// $url = add_query_arg( 'w', 80, $url );
// $url = add_query_arg( 'h', 80, $url );
// $url = add_query_arg( 'crop', 1, $url );
// $jsonpost["thumb_img_url"] = $url;
// $jsonpost["large_img_url"] = $exhibit->maker_photo;
// } else {
// $jsonpost["thumb_img_url"] = null;
// $jsonpost["large_img_url"] = null;
// }
// $jsonpost['description'] = ($exhibit->maker_bio ? htmlspecialchars_decode( $exhibit->maker_bio ) : null);
// $jsonpost['youtube_url'] = ($exhibit->project_video ? $exhibit->project_video : null);
// } elseif ($exhibit->maker == 'A list of makers' || empty( $exhibit->maker ) ) {
// $jsonpost['name'] = ($exhibit->maker_name ? $exhibit->maker_name : $exhibit->name);
// $url = $exhibit->m_maker_photo_thumb;
// if (!empty($url)) {
// $url = add_query_arg( 'w', 80, $url );
// $url = add_query_arg( 'h', 80, $url );
// $url = add_query_arg( 'crop', 1, $url );
// $jsonpost["thumb_img_url"] = $url;
// $jsonpost["large_img_url"] = $exhibit->m_maker_photo_thumb;
// } else {
// $jsonpost["thumb_img_url"] = null;
// $jsonpost["large_img_url"] = null;
// }
// $jsonpost['description'] = ($exhibit->maker_bio ? htmlspecialchars_decode( $exhibit->maker_bio ) : null);
// $jsonpost['youtube_url'] = ($exhibit->project_video ? $exhibit->project_video : null);
// }
}
WP_CLI::line( '' );
endwhile;
WP_CLI::success( "Boom!" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment