Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Created December 23, 2023 15:34
Show Gist options
  • Save Lonsdale201/bad663ce80cca8a751e54aadeb552763 to your computer and use it in GitHub Desktop.
Save Lonsdale201/bad663ce80cca8a751e54aadeb552763 to your computer and use it in GitHub Desktop.
JetEngine - Dynamic Visibility - Jetreviews condition (Current user reviewed)
// place the code in the child theme functions.php or a custom code snippets plugin.
// This conditions required the JetReview plugin to be installed and activated on your site. This Conditions only work with
// LOGGED IN USERS, Not supported GUESTS
// Only check the Review, and not the Comments.
// This conditions check if the current user already submitted a review (and approved) in the current source (eg: cpt current-post)
add_action( 'jet-engine/modules/dynamic-visibility/conditions/register', function( $conditions_manager ) {
class JE_Current_User_Reviewed extends \Jet_Engine\Modules\Dynamic_Visibility\Conditions\Base {
public function get_id() {
return 'je-current-user-reviewed';
}
public function get_name() {
return __( 'Current User Reviewed', 'jet-engine' );
}
public function get_group() {
return 'JetReviews';
}
public function check( $args = array() ) {
if ( !is_user_logged_in() ) {
return false;
}
$user_id = get_current_user_id();
$source = isset( $args['source'] ) ? $args['source'] : 'post';
$source_instance = jet_reviews()->reviews_manager->sources->get_source_instance( $source );
$source_id = $source_instance->get_current_id();
$table_name = jet_reviews()->db->tables( 'reviews', 'name' );
$query = jet_reviews()->db->wpdb()->prepare(
"SELECT * FROM $table_name WHERE source=%s AND post_id=%d AND author=%d AND approved=1",
$source,
$source_id,
$user_id
);
$reviews = jet_reviews()->db->wpdb()->get_results( $query );
$has_reviewed = !empty( $reviews );
$type = isset( $args['type'] ) ? $args['type'] : 'show';
if ( 'hide' === $type ) {
return !$has_reviewed;
} else {
return $has_reviewed;
}
}
public function is_for_fields() {
return false;
}
public function need_value_detect() {
return false;
}
}
$conditions_manager->register_condition( new JE_Current_User_Reviewed() );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment