Skip to content

Instantly share code, notes, and snippets.

@oddevan
Created August 30, 2019 16:16
Show Gist options
  • Save oddevan/90c5bd219314a9c14e242bdc8e9aabfd to your computer and use it in GitHub Desktop.
Save oddevan/90c5bd219314a9c14e242bdc8e9aabfd to your computer and use it in GitHub Desktop.
<?php
function import_cards() {
\WP_CLI::log( 'Querying pokemontcg.io...' );
$cards = Pokemon::Card( [ 'verify' => false ] )->where( [ 'setCode' => 'sm9', 'pageSize' => 1000 ] )->all();
foreach ( $cards as $card_obj ) {
$card = $card_obj->toArray();
$hash_text = $card['name'] . implode( ' ', $card['text'] );
if ( isset( $card['attacks'] ) ) {
foreach ( $card['attacks'] as $attack ) {
$hash_text .= $attack['name'] . $attack['text'];
}
}
$args = [
'post_type' => 'card',
'post_title' => $card['name'],
'post_status' => 'publish',
'post_name' => $card['id'],
'meta_input' => [
'card_number' => $card['number'],
'ptcg_id' => $card['id'],
],
];
$existing = get_page_by_path( $card['id'], OBJECT, 'card' );
if ( $existing ) {
$args['ID'] = $existing->ID;
}
$result = wp_insert_post( $args, true );
if ( is_wp_error( $result ) ) {
\WP_CLI::error( $result->get_error_message() );
}
wp_set_object_terms( $result, md5( $hash_text ), 'card_hash' );
\WP_CLI::success( 'Imported ' . $card['name'] );
}
\WP_CLI::success( 'Cards imported!' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment