JudgeMe product review sync to WooCommerce

Synchronizes product reviews from JudgeMe into WooCommerce Product Reviews (native) to power schema and the use of standard product archives stars for performance.

// Settings
define( 'CCOM_JUDGEME_PERPAGE', 50 );

// Scheduler
add_action( 'admin_notices', function() {

	// Require Action Scheduler
	if( ! function_exists( 'as_has_scheduled_action' ) ) {
		printf(
			'
				<div class="notice notice-warning is-dismissible">
					<p>%s</p>
				</div>
			',
			'JudgeMe product review sync: Missing Action Scheduler'
		);
		return;
	}

	// Schedule: JudgeMe Scheduler
	$hook = 'ccom_judgeme_scheduler';
	if( ! as_has_scheduled_action( $hook ) ) {
		as_schedule_recurring_action(
			current_time( 'timestamp', 1 ),
			DAY_IN_SECONDS,
			$hook
		);
	}

} );

add_action( 'ccom_judgeme_scheduler', function() {

	// Get Reviews Total
	$args = [
		'body' => [
			'api_token' => get_option( 'judgeme_shop_token' ),
			'shop_domain' => get_option( 'judgeme_domain' ),
		],
	];
	$url = 'https://judge.me/api/v1/reviews/count';
	$response = wp_remote_get( $url, $args );
	$response_body = json_decode( wp_remote_retrieve_body( $response ) );
	if( empty( $response_body->count ) ) {
		return;
	}

	// Loop Product Batches
	for(
		$i = 0;
		$i < intval( $response_body->count ) + CCOM_JUDGEME_PERPAGE;
		$i += CCOM_JUDGEME_PERPAGE ) {

		// Schedule: JudgeMe Sync Interval
		$hook = 'ccom_judgeme_sync';
		$args = [ 'offset' => $i ];
		if( ! as_has_scheduled_action( $hook, $args ) ) {
			as_schedule_single_action(
				current_time( 'timestamp', 1 ) + $i,
				$hook, $args
			);
		}

	} // End Product Batch Loop

} );

add_action( 'ccom_judgeme_sync', function( $offset ) {

	// Get Reviews Per Page
	$args = [
		'body' => [
			'api_token' => get_option( 'judgeme_shop_token' ),
			'page' => 1 + floor( $offset / CCOM_JUDGEME_PERPAGE ),
			'per_page' => CCOM_JUDGEME_PERPAGE,
			'shop_domain' => get_option( 'judgeme_domain' ),
		],
	];
	$url = 'https://judge.me/api/v1/reviews';
	$response = wp_remote_get( $url, $args );
	$response_body = json_decode(
		wp_remote_retrieve_body( $response )
	);
	if( empty( $response_body->reviews ) ) {
		return;
	}

	// Loop Reviews
	global $wpdb;
	foreach( $response_body->reviews as $review ) {

		// Skip Hidden Reviews
		if( $review->hidden == '1' ) {
			continue;
		}

		// Product Reviews Only
		if( empty( $review->product_external_id ) ) {
			continue;
		}

		// Check Preexistence
		$sql = $wpdb->prepare(
			"
				SELECT `meta_id`
				FROM {$wpdb->prefix}commentmeta
				WHERE `meta_key` = 'judgeme_id'
				AND `meta_value` = {$review->id}
			"
		);
		$exists = $wpdb->get_var( $sql );
		if( $exists ) {
			continue;
		}

		// Get User Account
		$user = get_user_by( 'email', $review->reviewer->email );
		$user_id = $user ? $user->ID : '';

		// Insert Woo Review
		$args = [
			'comment_agent' => '',
			'comment_approved' => 1,
			'comment_author' => $review->reviewer->name,
			'comment_author_email' => $review->reviewer->email,
			'comment_author_IP' => $review->ip_address,
			'comment_author_url' => '',
			'comment_content' => $review->body,
			'comment_date' => $review->created_at,
			'comment_parent' => 0,
			'comment_post_ID' => $review->product_external_id,
			'comment_type' => 'review',
			'user_id' => $user_id,
		];
		$comment_id = wp_insert_comment( $args );

		// Insert Woo Review Metadata
		if( $comment_id ) {
			update_comment_meta( $comment_id, 'rating', $review->rating );
			update_comment_meta( $comment_id, 'judgeme_title', $review->title );
			update_comment_meta( $comment_id, 'judgeme_id', $review->id );
		}

	} // End Loop Reviews

} );

PHP Code Snippets

  1. Log into a staging, development, or locally hosted clone of your site.
  2. Install and activate the Code Snippets plugin.
  3. Navigate to WP Admin > Snippets > Add New.
  4. Copy and paste the code from the Description tab above.
  5. Check to ensure formatting came over properly and no syntax errors show up in the editor.
  6. Customize the code as desired.
  7. Add a meaningful title and description.
  8. Select whether to run on front-end or back-end (or both). Some snippets require both.
  9. Click the “Save and Activate” button.
  10. Test your site to ensure it works as expected.
  11. Disable if any problems, or recover as necessary.
  12. Repeat steps two onward on your live environment.

HTML Reusable Blocks

Blocks may be imported into the Code Editor view of the WordPress Block Editor (three dots in the upper-right, select code editor). This is best used as a Reusable Block, which you can dynamically add into any page or post. In order to import as a Reusable Block navigate to your site with the following URI: /wp-admin/edit.php?post_type=wp_block or see my code snippet Add reusable blocks link to the Appearance menu.

  1. Describe the issue and what you’ve observed.
  2. Describe your expected outcome(s).
  3. List steps to reproduce the issue.
  4. Optionally provide screen-shot or video URLs.

We code all of our code snippets directly. Our clients provide most of the ideas and demand for the functionality provided by our code snippets.

There are several sources one can find on Google for code that has inspired or contributed to this open-source library. Here’s some main ones:

All code snippets are licensed GPLv2 (or later) matching WordPress licensing.

Disclaimer of warranty:

Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.