Created
October 25, 2018 06:08
-
-
Save craigedmonds/967867a47366b9e6ce6177d4716e75e9 to your computer and use it in GitHub Desktop.
This is a special function whcih pulls in the remote data from the dealer core and stores a local json file with the dealers data. The dealers details pages will actually be populated from the local json file so we do not need to do any major database processed which will save database overhead.
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 | |
############################ | |
//sync the dealer store pages | |
############################ | |
/* | |
This is a special function whcih pulls in the remote data from the dealer core | |
and stores a local json file with the dealers data. The dealers details pages will actually | |
be populated from the local json file so we do not need to do any major database processed | |
which will save database overhead. | |
*/ | |
add_action( 'wp_footer', 'brand_core_sync_dealer_store_pages', 100 ); | |
function brand_core_sync_dealer_store_pages() { | |
############# | |
//api settings | |
############# | |
$api_key = "API_KEY_HERE"; | |
$api_end_point_url = "END_POINT_URL_HERE"; | |
$local_json_file_name = "dealer-store-data.json"; | |
$store_cpt_name = "stores"; | |
$custom_post_template = "single-STORES.php"; | |
$get_option_name = "brand_core_sync_dealer_store_pages_last_update_time_gmt"; | |
$seconds_beteween_updates = 3600; //1 hour | |
$local_json_file = BRAND_CORE_PLUGIN_PATH . $local_json_file_name; | |
############# | |
//check the last update date exists | |
############# | |
if(!get_option($get_option_name)) { | |
update_option($get_option_name, gmdate('Y-m-d H:i:s') ); | |
} | |
############# | |
//check the interval between doing the updates | |
############# | |
$last_update_time = get_option($get_option_name); | |
$time1 = strtotime($last_update_time); | |
$time2 = strtotime(gmdate("Y-m-d H:i:s", time())); | |
$diff = $time2 - $time1; | |
if($diff < $seconds_beteween_updates) { | |
$output = ""; | |
$output .= PHP_EOL . PHP_EOL . PHP_EOL; | |
$output .= '<!-------------------------------------------------->' . PHP_EOL; | |
$output .= '<!-- dealer stores data was not updated -->' . PHP_EOL; | |
$output .= '<!-------------------------------------------------->' . PHP_EOL; | |
$output .= '<!-- Last update was '.$diff.' seconds ago at '.$last_update_time.'. The interval period is set to '.$seconds_beteween_updates.' seconds between updates -->'. PHP_EOL; | |
$output .= PHP_EOL . PHP_EOL . PHP_EOL; | |
echo $output; | |
return; | |
} | |
############# | |
//do curl to remote api | |
//see: https://api.arcticspascore.com/docs/get_dealer_map/ | |
############# | |
//set up post fields | |
$postfields = array(); | |
$postfields["action"] = "get_dealer_map"; | |
$postfields["show_hours"] = 1; | |
$postfields["show_other_areas"] = 1; | |
//urlify the data for the POST | |
$fields_string = http_build_query($postfields); | |
//prepare curl request | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $api_end_point_url); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Arctic-Dealer-Core-Api-Key:".$api_key]); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); | |
curl_setopt($ch, CURLOPT_POST, count($postfields)); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); | |
//needed for https | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
//make the request | |
$json_of_dealers = curl_exec($ch); | |
curl_close($ch); | |
//convert to php array | |
$array_of_dealers = json_decode($json_of_dealers,TRUE); | |
############# | |
//confirm that json feed was valid | |
############# | |
if(!is_array($array_of_dealers)) { | |
return; | |
} | |
############# | |
//check if the $local_json_file is same size as $json_of_dealers | |
//and if its the same we just stop the sync as there will be no change | |
############# | |
if (file_exists($local_json_file)) { | |
$local_json = file_get_contents($local_json_file); | |
if($local_json == $json_of_dealers) { | |
return; | |
} | |
} else { | |
//write a new file to local which we can read from | |
$myfile = fopen($local_json_file, "w"); | |
fwrite($myfile, $json_of_dealers); | |
fclose($myfile); | |
} | |
############# | |
//save the new $json_of_dealers data to local | |
############# | |
$myfile = fopen($local_json_file, "w"); | |
fwrite($myfile, $json_of_dealers); | |
fclose($myfile); | |
############# | |
//now we need to check if there is any changes to the records | |
############# | |
$array_of_dealers = $array_of_dealers["DealerData"]; | |
//echo "<pre class=\"notranslate\">"; | |
//echo "This is Line number: " . __LINE__ . " in " . basename(__FILE__) . PHP_EOL; | |
//print_r($array_of_dealers); | |
//echo "</pre>"; | |
############# | |
//loop through the php array of dealers grabbed fro the remote feed | |
############# | |
$count = 0; | |
foreach($array_of_dealers as $x){ | |
$dealer_id = $array_of_dealers[$count]["dealer_id"]; | |
$store_name = $array_of_dealers[$count]["store_name"]; | |
$store_town = $array_of_dealers[$count]["town"]; | |
$store_other_areas = $array_of_dealers[$count]["other_areas"]; | |
//create the yoast title and descriptions | |
if(empty($store_other_areas)) { | |
$yoast_wpseo_title = "$store_name - Hot Tub and Pool Store - $store_town"; | |
$yoast_wpseo_metadesc = "$store_name are the number #1 Hot Tub and Pool Store in $store_town. Visit $store_name $store_town"; | |
} else { | |
$yoast_wpseo_title = "$store_name - Hot Tub and Pool Store - $store_town - $store_other_areas"; | |
$yoast_wpseo_metadesc = "$store_name are the number #1 Hot Tub and Pool Store in $store_town and $store_other_areas"; | |
} | |
############# | |
//check if this dealer id exists and if exists set the $update_post_id | |
//so that we can update the records | |
############# | |
$posts = get_posts(array( | |
'numberposts' => -1, | |
'post_type' => $store_cpt_name, | |
'meta_key' => 'dealer_id', | |
'meta_value' => $dealer_id | |
)); | |
$total_posts = count($posts); | |
if($total_posts > 0) { | |
$update_post_id = $posts[0]->ID; | |
} | |
############# | |
//if there is 0 then create new post | |
############# | |
if($total_posts == 0) { | |
$new_post = array( | |
'post_title' => wp_strip_all_tags( $store_name ), | |
'post_content' => "", | |
'post_type' => $store_cpt_name, | |
'post_status' => 'publish', | |
'post_author' => '', | |
); | |
// Insert the post into the database | |
$new_post_id = wp_insert_post( $new_post , true); | |
add_post_meta($new_post_id, 'dealer_id', $dealer_id, true); | |
add_post_meta($new_post_id, 'custom_post_template', $custom_post_template, true); | |
add_post_meta($new_post_id, '_yoast_wpseo_title', $yoast_wpseo_title, true); | |
add_post_meta($new_post_id, '_yoast_wpseo_metadesc', $yoast_wpseo_metadesc, true); | |
############# | |
//otherwise update the dealers record | |
//try to avoid doing this as the interval period is very load so will update all 150 | |
//records every X seconds. | |
############# | |
} else { | |
//lets force update the yoast title and description | |
update_post_meta($update_post_id, '_yoast_wpseo_title', $yoast_wpseo_title); | |
update_post_meta($update_post_id, '_yoast_wpseo_metadesc', $yoast_wpseo_metadesc); | |
} | |
$count = $count + 1; | |
} | |
############# | |
//now we need to figure out if we need to delete any of the dealers locally | |
############# | |
$total_dealers_in_api = count($array_of_dealers); | |
$posts = get_posts(array( | |
'numberposts' => -1, | |
'post_type' => $store_cpt_name, | |
)); | |
$total_dealer_in_local = count($posts); | |
############# | |
//if there is a difference in delaer totals between local and remote | |
//then we need to perform integrity check | |
############# | |
if($total_dealers_in_api != $total_dealer_in_local) { | |
//we need to do something here | |
//at 25/10/2018 we are not removing any dealers | |
//maybe we do this in future | |
} | |
############# | |
//finally update the option and display comment in html page so we can see when things are updated | |
############# | |
update_option($get_option_name, gmdate('Y-m-d H:i:s') ); | |
$output = ""; | |
$output .= PHP_EOL . PHP_EOL . PHP_EOL; | |
$output .= '<!-------------------------------------------------->' . PHP_EOL; | |
$output .= '<!-- dealer stores data was just updated! -->' . PHP_EOL; | |
$output .= '<!-------------------------------------------------->' . PHP_EOL; | |
$output .= '<!-- Last update was '.$diff.' seconds ago at '.$last_update_time.'. The interval period is set to '.$seconds_beteween_updates.' seconds between updates -->'. PHP_EOL; | |
$output .= PHP_EOL . PHP_EOL . PHP_EOL; | |
echo $output; | |
//end of function | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment