Last active
August 29, 2015 13:58
-
-
Save marushu/9970889 to your computer and use it in GitHub Desktop.
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 | |
define('CRON_SCHEDULE_HANDLER', 'load_oricon_entry_38'); | |
// wp-cron で実行させたい処理 | |
function cron_schedule_handler() { | |
load_oricon(); | |
// 指定時間後に再度起動 | |
$time_interval = 5 * 60; // 5 * 60 : 5分おき | |
wp_schedule_single_event( time() + $time_interval, CRON_SCHEDULE_HANDLER ); | |
} | |
// wp-cron でスケジュールされていなければ、スケジュール登録 | |
$crons = _get_cron_array(); | |
$enabled = false; | |
foreach ( $crons as $time => $tasks ) { | |
foreach ( $tasks as $procname => $task ) { | |
if ( $procname === CRON_SCHEDULE_HANDLER ) { | |
$enabled = true; break; | |
} | |
} | |
if ( $enabled ) break; | |
} | |
if ( ! $enabled ) | |
wp_schedule_single_event( time(), CRON_SCHEDULE_HANDLER ); | |
unset( $tasks ); unset( $crons ); | |
// スケジュール実行 | |
add_action( CRON_SCHEDULE_HANDLER, 'cron_schedule_handler' ); | |
function load_oricon() { | |
// DBへ接続 | |
global $wpdb, $post; | |
$db_user = $wpdb->dbuser; | |
$db_pass = $wpdb->dbpassword; | |
$db_host = $wpdb->dbhost; | |
$db_name = $wpdb->dbname; | |
$another_wpdb = new wpdb( $db_user, $db_pass, $db_name, $db_host ); | |
// DB:oricon_newsよりデータを抽出 | |
// prepareメソッドで無害化 | |
$results = $another_wpdb->get_results( "SELECT * FROM oricon_news where updateflag=1 ORDER BY `oricon_news`.`time_open`" ); | |
// oricon_newsテーブルのidとwp_postsのpost_titleを比較 | |
$dsn = "mysql:host={$db_host};dbname={$db_name};charset=utf8"; | |
// h関数 | |
function h( $var ) { | |
if( is_array( $var ) ) { | |
return array_map( 'h', $var ); | |
} else { | |
return htmlspecialchars( $var, ENT_QUOTES, 'UTF-8' ); | |
} | |
} | |
try { | |
// mysqlへ接続 | |
$db = new PDO( $dsn, $db_user, $db_pass ); | |
//プリペアドステートメントのエミュレーションを無効化 | |
$db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false ); | |
// エラー発生時、例外をスローされる | |
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); | |
//echo "OK"; | |
// 接続できない場合PODExeption例外がスローされるのでキャッチ | |
} catch( PDOException $e ) { | |
//echo "NG" . h( $e->getMessage() ); | |
} | |
// SQL文準備 | |
$sql = "select * from wp_posts where post_type='oricon_news' AND post_title NOT IN (SELECT id from oricon_news)"; | |
$prepare = $db->prepare( $sql ); | |
// SQL文のプレースホルダーに値をバインドしてクエリを実行 | |
$prepare->execute(); | |
// 結果をオブジェクト配列として取得 | |
$check_results = $prepare->fetchAll( PDO::FETCH_OBJ ); | |
// 該当記事を削除 | |
foreach( $check_results as $check_result ) { | |
wp_delete_post( $check_result->ID, true ); | |
} | |
if( $results ) : | |
$count_results = count( $results ); | |
for( $i = 0; $i < $count_results; $i++ ) { | |
// 記事ID | |
$oricon_id = intval( $results[$i]->id ); | |
// 記事リビジョン | |
$oricon_revision = intval( $results[$i]->revision ); | |
// 記事ステータス( 1:公開, 9:公開終了 ) | |
$oricon_status_from_db = intval( $results[$i]->status ); | |
// 記事公開日時 | |
$oricon_time_open = $results[$i]->time_open; | |
// xxxx年xx月xx日へ | |
$oricon_time_open = strtotime( $oricon_time_open ); | |
$oricon_time_open = gmdate( 'Y年m月d日', $oricon_time_open ); | |
$oricon_time_open_for_insert = gmdate( 'Y-m-d H:i:s' ); | |
// 更新日時 | |
$oricon_time_update = $results[$i]->time_update; | |
// Now | |
$date_now = date_i18n( "Y-m-d H:i:s" ); | |
// カテゴリ | |
$oricon_category = intval( $results[$i]->category ); | |
// カテゴリ振り分け | |
if( $oricon_category ) { | |
switch( $oricon_category ) { | |
case $oricon_category == 10 : | |
case $oricon_category == 18 : | |
$category_name = '芸能・エンタメ'; | |
// set term to taxonomy: oricon_cat | |
wp_insert_term( | |
'芸能・エンタメ', | |
'oricon_cat', | |
array( | |
'slug' => 'oricon_enter' | |
) | |
); | |
delete_option("category_children"); | |
$term_slug = "oricon_enter"; | |
break; | |
case $oricon_category == 11 : | |
$category_name = '音楽'; | |
// set term to taxonomy: oricon_cat | |
wp_insert_term( | |
'音楽', | |
'oricon_cat', | |
array( | |
'slug' => 'oricon_music' | |
) | |
); | |
delete_option("category_children"); | |
$term_slug = "oricon_music"; | |
break; | |
case $oricon_category == 12 : | |
case $oricon_category == 20 : | |
case $oricon_category == 21 : | |
$category_name = '映画・アニメ'; | |
// set term to taxonomy: oricon_cat | |
wp_insert_term( | |
'映画・アニメ', | |
'oricon_cat', | |
array( | |
'slug' => 'oricon_movie_animation' | |
) | |
); | |
delete_option("category_children"); | |
$term_slug = "oricon_movie_animation"; | |
break; | |
case $oricon_category == 30 : | |
$category_name = 'トレンド'; | |
// set term to taxonomy: oricon_cat | |
wp_insert_term( | |
'トレンド', | |
'oricon_cat', | |
array( | |
'slug' => 'oricon_trend' | |
) | |
); | |
delete_option("category_children"); | |
$term_slug = "oricon_trend"; | |
break; | |
case $oricon_category == 100 : | |
$category_name = 'その他'; | |
// set term to taxonomy: oricon_cat | |
wp_insert_term( | |
'その他', | |
'oricon_cat', | |
array( | |
'slug' => 'oricon_other' | |
) | |
); | |
delete_option("category_children"); | |
$term_slug = "oricon_other"; | |
break; | |
} | |
} | |
// ニュースタイトル | |
$oricon_newstitle = apply_filters( 'the_title', $results[$i]->newstitle ); | |
// ニューステキスト | |
$oricon_newstext = apply_filters( 'the_content', $results[$i]->newstext ); | |
// links シリアライズされた連想配列 | |
$links = unserialize( $results[$i]->links ); | |
// images シリアライズされた連想配列 | |
$images = $results[$i]->images; | |
// カスタム投稿タイプ:oricon_news のオブジェクトを持ってくる | |
$post_obj_check = get_page_by_title( $oricon_id, 'OBJECT', $post_type = 'oricon_news' ); | |
$term = get_term_by( 'slug', $term_slug, 'oricon_cat' ); | |
if( empty( $post_obj_check ) ) { | |
//$term = get_term_by( 'slug', $term_slug, 'oricon_cat' ); | |
//$term_id = $term->term_id; | |
$post = array( | |
'post_title' => $oricon_id, | |
'post_content' => $oricon_newstext, | |
'post_status' => 'publish', | |
'post_type' => 'oricon_news', | |
'post_author' => '1', | |
'ping_status' => 'closed', | |
'comment_status' => 'closed', | |
'post_date' => date( $results[$i]->time_open ), | |
'tax_input' => array( 'oricon_cat' => $term->term_id ) | |
); | |
$id = wp_insert_post($post); | |
// set custom field:entry status oricon_status | |
$check_post_meta_entry_status = get_post_meta( $id, 'oricon_status_post', true ); | |
if( $check_post_meta_entry_status == 0 ) { | |
add_post_meta( $id, 'oricon_status', $oricon_status_from_db, true ); | |
} | |
// set custom field:news_title to oricon news_title :) | |
$check_post_meta_news_title = get_post_meta( $id, 'oricon_news_title', true ); | |
if( $check_post_meta_news_title == 0 ) { | |
add_post_meta( $id, 'oricon_news_title', esc_html( $oricon_newstitle ), true ); | |
} | |
// set custom field:oricon_thumb_image_caption :) | |
$check_post_meta_post_images = get_post_meta( $id, 'thumb', true ); | |
if( $check_post_meta_post_images == 0 ) { | |
add_post_meta( $id, 'oricon_thumb_image_caption', $images, true ); | |
} | |
// set revision | |
$check_post_meta_revision = get_post_meta( $id, 'oricon_revision', true ); | |
if( $check_post_meta_revision == 0 ) { | |
add_post_meta( $id, 'oricon_revision', $oricon_revision, true ); | |
} | |
wp_set_post_terms( $id, $term->term_id, 'oricon_cat' ); | |
$another_wpdb->update( | |
'oricon_news', | |
array( | |
'updateflag' => 0 | |
), | |
array( | |
'id' => $oricon_id | |
) | |
); | |
} elseif( $oricon_time_update <= $date_now ) { | |
// 読み込み済みでupdateflag=1の記事を更新 | |
$post = array( | |
'ID' => $post_obj_check->ID, | |
'post_title' => $oricon_id, | |
'post_content' => $oricon_newstext, | |
'post_status' => 'publish', | |
'post_type' => 'oricon_news', | |
'post_author' => '1', | |
'ping_status' => 'closed', | |
'comment_status' => 'closed', | |
'post_date' => date( $results[$i]->time_open ), | |
'tax_input' => array( 'oricon_cat' => $term->term_id ) | |
); | |
$id = wp_update_post($post); | |
// カスタムフィールを更新 | |
update_post_meta( $id, 'oricon_status', $oricon_status_from_db ); | |
update_post_meta( $id, 'oricon_news_title', esc_html( $oricon_newstitle ) ); | |
update_post_meta( $id, 'oricon_thumb_image_caption', $images ); | |
// 更新後、updateflagを0に変更 | |
$another_wpdb->update( | |
'oricon_news', | |
array( | |
'updateflag' => 0 | |
), | |
array( | |
'id' => $oricon_id | |
) | |
); | |
if( $check_post_meta_revision !== $oricon_revision ) { | |
update_post_meta( $id, 'oricon_revision', $oricon_revision ); | |
} | |
} | |
} | |
endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
元テーブルに変更があった場合、投稿内容(カスタムフィールド含む)をアップデート。
wp_insert_post,
wp_uppdate_post,
wp_delete_post
などがスゲー参考になった。