Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Created December 23, 2023 15:36
Show Gist options
  • Save Lonsdale201/a31c9eb8156c9cd87da2c861a83afb3a to your computer and use it in GitHub Desktop.
Save Lonsdale201/a31c9eb8156c9cd87da2c861a83afb3a to your computer and use it in GitHub Desktop.
JetEngine - Dynamic Visibility - Jetreviews condition (Current user review awaiting moderation)
// 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 submitted a review and its still awaiting moderation (so only this status) in the current source (eg: cpt current-post)
add_action( 'jet-engine/modules/dynamic-visibility/conditions/register', function( $conditions_manager ) {
class JE_Review_Awaiting_Moderation extends \Jet_Engine\Modules\Dynamic_Visibility\Conditions\Base {
public function get_id() {
return 'je-review-awaiting-moderation';
}
public function get_name() {
return __( 'Review Awaiting Moderation', '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 type can be 'post', 'user', etc.
$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=0",
$source,
$source_id,
$user_id
);
$reviews = jet_reviews()->db->wpdb()->get_results( $query );
$has_pending_reviews = !empty( $reviews );
$type = isset( $args['type'] ) ? $args['type'] : 'show';
if ( 'hide' === $type ) {
return !$has_pending_reviews;
} else {
return $has_pending_reviews;
}
}
public function is_for_fields() {
return false;
}
public function need_value_detect() {
return false;
}
}
$conditions_manager->register_condition( new JE_Review_Awaiting_Moderation() );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment