Created
December 17, 2014 14:25
-
-
Save rxnlabs/a3e6fcf9f1fe1d999692 to your computer and use it in GitHub Desktop.
PHP WP - Verify that all FAQs are in Swiftype and add all FAQs to Swiftype
This file contains hidden or 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 | |
// change the DB_HOST constant to the localhost IP address so it works on a Mac. "Localhost" means something different in a Mac environment | |
define('DB_HOST','127.0.0.1'); | |
// load the wp-load.php file to use WordPress functions. File path is assumming this is being run from the current theme folder (any theme folder would work) | |
require 'wp-load.php'; | |
require 'wp-admin/includes/plugin.php'; | |
// check if PHP is being executed from the command line | |
if( php_sapi_name() === 'cli' && defined('SWIFTYPE_AUTH_TOKEN') ){ | |
$url = 'https://api.swiftype.com/api/v1/engines/agora-financial/document_types/faqs/documents.json?auth_token='.SWIFTYPE_AUTH_TOKEN.'&per_page=all'; | |
$response = wp_remote_get($url); | |
$body = json_decode(wp_remote_retrieve_body( $response ), true); | |
$stored = array(); | |
foreach( $body as $b ){ | |
$stored[] = $b['external_id']; | |
} | |
$faq = get_posts( array('post_type'=>'faq','posts_per_page'=>-1) ); | |
$permalinks = array(); | |
foreach( $faq as $a ){ | |
$permalinks[] = $a->post_name; | |
} | |
$difference1 = array_diff($permalinks,$stored); | |
$difference2 = array_diff($stored,$permalinks); | |
echo "FAQs in Swiftype but not in WordPress \n"; | |
var_dump($difference2); | |
echo "FAQs in WordPress but not in Swiftype \n"; | |
var_dump($difference1); | |
$right = (string)count($permalinks); | |
$wrong = (string)count($stored); | |
$difference = count($difference1); | |
echo "Right: $right\n"; | |
echo "Wrong: $wrong\n"; | |
echo "Swiftype missing $difference FAQs\n"; | |
foreach( $difference2 as $swiftype_error ){ | |
echo "Swiftype has additional and different FAQ ".$swiftype_error." in it's database\n\n"; | |
} | |
foreach( $difference1 as $wordpress ){ | |
echo "WordPress has additional and different FAQ ".$wordpress." in it's database\n\n"; | |
} | |
$url = 'https://api.swiftype.com/api/v1/engines/agora-financial/document_types/faqs/documents/bulk_destroy'; | |
$body = array('auth_token'=>SWIFTYPE_AUTH_TOKEN,'documents'=>$stored); | |
$destroy = wp_remote_post( $url, array( | |
'method'=>'POST', | |
'headers'=>array('Content-Type'=>'application/json'), | |
'body'=>json_encode($body) | |
) ); | |
$verify = wp_remote_retrieve_body( $destroy ); | |
echo "Response code from Swiftype when bulk deleting FAQs ".wp_remote_retrieve_response_code($destroy)."\n"; | |
if( wp_remote_retrieve_response_code($destroy) != 204 ){ | |
foreach( $stored as $delete ){ | |
$url = 'https://api.swiftype.com/api/v1/engines/agora-financial/document_types/faqs/documents/'.$delete.'?auth_token='.SWIFTYPE_AUTH_TOKEN; | |
$test = wp_remote_post( $url, array( | |
'method'=>'DELETE' | |
) ); | |
if( wp_remote_retrieve_response_code($test) == 204 ){ | |
echo "Deleted: $delete\n"; | |
}else{ | |
echo wp_remote_retrieve_response_code($test)."\n"; | |
} | |
} | |
} | |
echo $verify."\n"; | |
$new_faqs = array(); | |
foreach( $faq as $a ){ | |
$faq_categories = array(); | |
$faq_tags = array(); | |
$categories = wp_get_post_terms( $a->ID, 'faq-category' ); | |
$tags = wp_get_post_terms( $a->ID, 'faqtax' ); | |
foreach ($categories as $category) { | |
$faq_categories[] = $category->name; | |
} | |
foreach($tags as $tag ){ | |
$faq_tags[] = $tag->slug; | |
} | |
$new_faqs[] = array( | |
'external_id'=>$a->post_name, | |
'fields'=>array( | |
array('name'=>'title','value'=>$a->post_title,'type'=>'string'), | |
array('name'=>'content','value'=>wp_strip_all_tags( get_post_field('post_content', $a->ID, 'raw') ),'type'=>'text'), | |
array('name'=>'date','value'=>gmdate( 'Y-m-d H:i:s', strtotime($a->post_modified)),'type'=>'date'), | |
array('name'=>'tags','value'=>$faq_tags,'type'=>'string'), | |
array('name'=>'categories','value'=>$faq_categories,'type'=>'string'), | |
array('name'=>'permalink','value'=>$a->post_name,'type'=>'enum'), | |
array('name'=>'url', 'value'=>$a->post_name, 'type'=>'enum') | |
) | |
); | |
} | |
$url = 'https://api.swiftype.com/api/v1/engines/agora-financial/document_types/faqs/documents/bulk_create_or_update_verbose'; | |
$body = array( 'auth_token'=>SWIFTYPE_AUTH_TOKEN, 'documents'=>$new_faqs ); | |
echo "Number of new FAQs ".count($new_faqs)." to be created\n"; | |
$response = wp_remote_post( $url, array( | |
'method'=>'POST', | |
'headers'=>array('Content-Type'=>'application/json'), | |
'body'=>json_encode($body) | |
) ); | |
$verify = json_decode(wp_remote_retrieve_body( $response ), true); | |
echo "Result from Swiftype when adding FAQs\n"; | |
echo wp_remote_retrieve_body( $response )."\n"; | |
$all_added = array(); | |
$not_added = array(); | |
foreach( $verify as $key=>$v){ | |
if( $v == 'true' ){ | |
$all_added[] = true; | |
}else{ | |
$not_added[$key] = $v; | |
echo $v."\n"; | |
} | |
} | |
echo "Number of FAQs successfully created ".count($all_added)."\n"; | |
if( count($not_added) > 0 ){ | |
$not_created = array(); | |
foreach($not_added as $key=>$error_message){ | |
echo "FAQ ".$faq[$key]->post_name." NOT added to Swiftype because ".$error_message."\n"; | |
} | |
echo count($not_added); | |
} | |
} |
This file contains hidden or 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 | |
// change the DB_HOST constant to the localhost IP address so it works on a Mac. "Localhost" means something different in a Mac environment | |
define('DB_HOST','127.0.0.1'); | |
// load the wp-load.php file to use WordPress functions. File path is assumming this is being run from the current theme folder (any theme folder would work) | |
require 'wp-load.php'; | |
require 'wp-admin/includes/plugin.php'; | |
// check if PHP is being executed from the command line | |
if( php_sapi_name() === 'cli' && defined('SWIFTYPE_AUTH_TOKEN') ){ | |
$url = 'https://api.swiftype.com/api/v1/engines/agora-financial/document_types/faqs/documents.json?auth_token='.SWIFTYPE_AUTH_TOKEN.'&per_page=all'; | |
$response = wp_remote_get($url); | |
$body = json_decode(wp_remote_retrieve_body( $response ), true); | |
$stored = array(); | |
foreach( $body as $b ){ | |
$stored[] = $b['external_id']; | |
} | |
$faq = get_posts( array('post_type'=>'faq','posts_per_page'=>-1) ); | |
$permalinks = array(); | |
foreach( $faq as $a ){ | |
$permalinks[] = $a->post_name; | |
} | |
$difference1 = array_diff($permalinks,$stored); | |
$difference2 = array_diff($stored,$permalinks); | |
echo "FAQs in Swiftype but not in WordPress \n"; | |
var_dump($difference2); | |
echo "FAQs in WordPress but not in Swiftype \n"; | |
var_dump($difference1); | |
$right = (string)count($permalinks); | |
$wrong = (string)count($stored); | |
$difference = count($difference1); | |
echo "Right: $right\n"; | |
echo "Wrong: $wrong\n"; | |
echo "Swiftype missing $difference FAQs\n"; | |
foreach( $difference2 as $swiftype_error ){ | |
echo "Swiftype has additional and different FAQ ".$swiftype_error." in it's database\n\n"; | |
} | |
foreach( $difference1 as $wordpress ){ | |
echo "WordPress has additional and different FAQ ".$wordpress." in it's database\n\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment