Last active
November 8, 2017 21:46
-
-
Save clutchwave/c51d4c7036c026404e617d620c426b96 to your computer and use it in GitHub Desktop.
WordPress Discourse Reply Count Update Script
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 | |
// This script will look at ALL of your WordPress blog posts (post type 'post' in this case) | |
// and look at the reported number of comments in the post's meta / custom_field, 'pp_discourse_replies' | |
// | |
// It will then look at all associated Discourse Topics (as per the WP-Discourse plugin's discourse_topic_id) | |
// custom field and and check the actual number of replies. If they are different, it will update the custom field. | |
// | |
// You can then override your WP theme's comment count PHP to display this meta field (pp_discourse_replies) | |
// and run this script on cron, so you have regularly-updated comment counts, but are not slowing your site down | |
// retrieving them. | |
// | |
// This allows you to use embedded JS comments and is a HYBRID solution with WP-Discourse plugin that does NOT | |
// need to have the actual comments pulled into WordPress. But we do need the Topic IDs set in the | |
// discourse_topic_id custom field for each post. | |
// https://meta.discourse.org/t/embedding-discourse-comments-via-javascript/31963 | |
// | |
// This script requires cron to call it. It doesn't run via WP cron | |
// or really work inside of any WordPress installation | |
/* | |
* Site and Product Configs | |
*/ | |
$_SERVER[ 'HTTP_HOST' ] = 'blog.YOURDOMAIN.com'; | |
$wp_load_loc = "/PATH/TO/YOUR/WORDPESS/public_html/wp-load.php"; | |
require_once($wp_load_loc); | |
//test to make sure we can access database | |
//echo $wpdb->prefix . "\n"; | |
$discourse_api_key = 'YOUR_API_KEY_FOR_DISCOURSE'; | |
$discourse_api_user = "YOUR_API_USER_FOR_DISCOURSE"; | |
echo "Getting meta values...\n"; | |
// See the get_meta_values function below. If you care about other types of post | |
// types, you may need to tweak it | |
$discourse_posts = get_meta_values('discourse_topic_id','post'); | |
echo "Retrieved " . count($discourse_posts) . " meta values\n"; | |
$update_count = 0; | |
$error_count = 0; | |
foreach ($discourse_posts as $post_id => $topic_id) { | |
$request_url = "https://forum.priceplow.com/t/" . $topic_id . ".json?api_key=" . $discourse_api_key . '&api_user=' . $discourse_api_user; | |
$download_success = FALSE; | |
$fail_count = 0; | |
while (!$download_success && $fail_count < 5) { | |
$topic_data = wp_remote_get($request_url); | |
if (is_array($topic_data)) { | |
$body_json = $topic_data['body']; | |
$data = json_decode($body_json, TRUE); | |
$num_replies = $data['posts_count'] - 1; // This is the number of total actual REPLIES a topic has gotten | |
if($num_replies >= 0) { | |
$success = update_post_meta($post_id, 'pp_discourse_replies', $num_replies); | |
if($success === TRUE) { | |
$update_count++; | |
echo " Updated Post " . $post_id . " with " . $num_replies . " replies for https://forum.priceplow.com/t/" . $topic_id . "\n"; | |
} elseif (isInteger($success)) { | |
// When update_post_meta returns something outside of boolean, that means it created a new one, returning its new meta_id | |
echo " Added NEW Post " . $post_id . " with " . $num_replies . " replies for https://forum.priceplow.com/t/" . $topic_id . "\n"; | |
$update_count++; | |
} | |
$download_success = TRUE; | |
} else { | |
// Sometimes you get negative replies from Discourse. This means it wasn't a good download. | |
echo " Warning: Negative replies for $request_url\n"; | |
$download_success = FALSE; | |
$fail_count++; | |
sleep(3); | |
} | |
} else { | |
echo " Warning: Array Not Returned for $request_url\n"; | |
$fail_count++; | |
sleep(3); | |
} | |
if($fail_count == 5) { | |
echo "\n\n\tERROR: FIVE FAILURES FOR $request_url\n\n"; | |
$error_count++; | |
} | |
} | |
} | |
error_log("Discourse Comment Run: Updated " . $update_count . " posts with " . $error_count . " Unrecoverable Errors"); | |
// Not sure if this matters for cron | |
if($error_count > 0) { | |
return FALSE; | |
} elseif ($update_count == 0) { | |
return TRUE; // Not sure what happens when 0 returned to bash | |
} else { | |
return $update_count; | |
} | |
// function to grab all possible meta values of the chosen meta key. | |
// Returns as an associative array indexed with the post ID | |
// If you care about multiple post_types, you may want to change it into an array. | |
function get_meta_values( $meta_key, $post_type = 'post' ) { | |
$posts = get_posts( | |
array( | |
'post_type' => $post_type, | |
'meta_key' => $meta_key, | |
'posts_per_page' => -1, | |
) | |
); | |
$meta_values = array(); | |
foreach( $posts as $post ) { | |
$meta_values[$post->ID] = get_post_meta( $post->ID, $meta_key, true ); | |
} | |
return $meta_values; | |
} | |
// From http://php.net/manual/en/function.is-int.php | |
function isInteger($input) { | |
return(ctype_digit(strval($input))); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment