Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use comments table for RSVP instead of custom RSVP table. #692

Merged
merged 19 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/admin_style.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array(), 'version' => '95d396adaa06c1b8356f');
<?php return array('dependencies' => array(), 'version' => 'e113ae6fddb179ebc2fc');
8 changes: 4 additions & 4 deletions includes/core/classes/class-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public function get_datetime(): array {
$data = get_transient( $cache_key );

if ( empty( $data ) || ! is_array( $data ) ) {
$table = sprintf( static::TABLE_FORMAT, $wpdb->prefix );
$table = sprintf( self::TABLE_FORMAT, $wpdb->prefix );
$data = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT datetime_start, datetime_start_gmt, datetime_end, datetime_end_gmt, timezone FROM %i WHERE post_id = %d LIMIT 1', $table, $this->event->ID ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder
$data = ( ! empty( $data ) ) ? (array) current( $data ) : array();

Expand Down Expand Up @@ -721,11 +721,11 @@ public function maybe_get_online_event_link(): string {
return '';
}

$user = $this->rsvp->get( get_current_user_id() );
$response = $this->rsvp->get( get_current_user_id() );

if (
! isset( $user['status'] ) ||
'attending' !== $user['status'] ||
! isset( $response['status'] ) ||
'attending' !== $response['status'] ||
$this->has_event_past()
) {
return '';
Expand Down
184 changes: 184 additions & 0 deletions includes/core/classes/class-rsvp-query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php
/**
* Manages queries for RSVPs.
*
* This file contains the RSVP_Query class which handles the querying and manipulation
* of RSVP comments within the GatherPress plugin.
*
* @package GatherPress\Core
* @since 1.0.0
*/

namespace GatherPress\Core;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

use GatherPress\Core\Traits\Singleton;
use WP_comment;
use WP_Comment_Query;
use WP_Tax_Query;

/**
* Class Rsvp_Query
*
* Handles querying and manipulation of RSVP comments within the GatherPress plugin.
*
* @package GatherPress\Core
* @since 1.0.0
*/
class Rsvp_Query {
/**
* Enforces a single instance of this class.
*/
use Singleton;

/**
* Class constructor.
*
* This method initializes the object and sets up necessary hooks.
*
* @since 1.0.0
*/
protected function __construct() {
$this->setup_hooks();
}

/**
* Set up hooks for various purposes.
*
* This method adds hooks for different purposes as needed.
*
* @since 1.0.0
*
* @return void
*/
protected function setup_hooks(): void {
add_filter( 'pre_get_comments', array( $this, 'exclude_rsvp_from_comment_query' ) );
add_filter( 'comments_clauses', array( $this, 'taxonomy_query' ), 10, 2 );
}

/**
* Modify comment query clauses to include taxonomy query.
*
* This method adds the necessary SQL join and where clauses to a comment query
* based on a taxonomy query if one is present in the query variables.
*
* @since 1.0.0
*
* @param array $clauses The clauses for the query.
* @param WP_Comment_Query $comment_query The comment query object.
* @return array Modified query clauses.
*/
public function taxonomy_query( array $clauses, WP_Comment_Query $comment_query ): array {
global $wpdb;

if ( ! empty( $comment_query->query_vars['tax_query'] ) ) {
$comment_query->tax_query = new WP_Tax_Query( $comment_query->query_vars['tax_query'] );
$pieces = $comment_query->tax_query->get_sql( $wpdb->comments, 'comment_ID' );
$clauses['join'] .= $pieces['join'];
$clauses['where'] .= $pieces['where'];
}

return $clauses;
}

/**
* Retrieve a list of RSVP comments based on specified arguments.
*
* This method fetches RSVP comments by merging the provided arguments with default
* values specific to RSVPs. It ensures the count-only return is disabled and the
* RSVP comments are properly filtered.
*
* @since 1.0.0
*
* @param array $args Arguments for retrieving RSVPs.
* @return array List of RSVP comments.
*/
public function get_rsvps( array $args ): array {
$args = array_merge(
array(
'type' => Rsvp::COMMENT_TYPE,
'status' => 'approve',
),
$args
);

// Never allow count-only return, we always want array.
$args['count'] = false;

remove_filter( 'pre_get_comments', array( $this, 'exclude_rsvp_from_comment_query' ) );

$rsvps = get_comments( $args );

add_filter( 'pre_get_comments', array( $this, 'exclude_rsvp_from_comment_query' ) );

return (array) $rsvps;
}

/**
* Retrieve a single RSVP comment based on specified arguments.
*
* This method fetches a single RSVP comment by merging the provided arguments with default
* values specific to RSVPs. It ensures only one comment is returned.
*
* @since 1.0.0
*
* @param array $args Arguments for retrieving the RSVP.
* @return WP_Comment|null The RSVP comment or null if not found.
*/
public function get_rsvp( array $args ): ?WP_Comment {
$args = array_merge(
array(
'number' => 1,
),
$args
);

$rsvp = $this->get_rsvps( $args );

if ( empty( $rsvp ) ) {
return null;
}

return $rsvp[0];
}

/**
* Exclude RSVP comments from a query.
*
* This method modifies the comment query to exclude comments of the RSVP type. It
* ensures that RSVP comments are not included in the query results by adjusting the
* comment types in the query variables.
*
* @since 1.0.0
*
* @param WP_Comment_Query $query The comment query object.
* @return void
*/
public function exclude_rsvp_from_comment_query( $query ) {
if ( ! $query instanceof WP_Comment_Query ) {
return;
}

$current_comment_types = $query->query_vars['type'];

// Ensure comment type is not empty.
if ( ! empty( $current_comment_types ) ) {
if ( is_array( $current_comment_types ) ) {
// Remove the specific comment type from the array.
$current_comment_types = array_diff( $current_comment_types, array( Rsvp::COMMENT_TYPE ) );
} elseif ( Rsvp::COMMENT_TYPE === $current_comment_types ) {
// If the only type is the one to exclude, set it to empty.
$current_comment_types = '';
}
} else {
// If no specific type is set, make sure the one to exclude is not included.
$current_comment_types = array( 'comment', 'pingback', 'trackback' ); // Default types.
$current_comment_types = array_diff( $current_comment_types, array( Rsvp::COMMENT_TYPE ) );
}

// Update the query vars with the modified comment types.
$query->query_vars['type'] = $current_comment_types;
}
}
103 changes: 103 additions & 0 deletions includes/core/classes/class-rsvp-setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* File comment block for Rsvp_Setup class.
*
* This file contains the definition of the Rsvp_Setup class, which handles
* setup tasks related to RSVP functionality within the GatherPress plugin.
*
* @package GatherPress\Core
* @since 1.0.0
*/

namespace GatherPress\Core;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

use GatherPress\Core\Traits\Singleton;

/**
* Handles setup tasks related to RSVP functionality.
*
* The Rsvp_Setup class initializes necessary hooks and configurations for managing RSVPs.
* It registers a custom taxonomy for RSVPs and adjusts comment counts specifically for events.
*
* @package GatherPress\Core
* @since 1.0.0
*/
class Rsvp_Setup {
/**
* Enforces a single instance of this class.
*/
use Singleton;

/**
* Class constructor.
*
* This method initializes the object and sets up necessary hooks.
*
* @since 1.0.0
*/
protected function __construct() {
$this->setup_hooks();
}

/**
* Set up hooks for various purposes.
*
* This method adds hooks for different purposes as needed.
*
* @since 1.0.0
* @return void
*/
protected function setup_hooks(): void {
add_action( 'init', array( $this, 'register_taxonomy' ) );
add_filter( 'get_comments_number', array( $this, 'adjust_comments_number' ), 10, 2 );
}

/**
* Register custom comment taxonomy for RSVPs.
*
* Registers a custom taxonomy 'gatherpress_rsvp' for managing RSVP related functionalities specifically for comments.
*
* @since 1.0.0
* @return void
*/
public function register_taxonomy(): void {
register_taxonomy(
Rsvp::TAXONOMY,
'comment',
array(
'labels' => array(),
'hierarchical' => false,
'public' => true,
'show_ui' => false,
'show_admin_column' => false,
'query_var' => true,
'publicly_queryable' => false,
'show_in_rest' => true,
)
);
}

/**
* Adjusts the number of comments displayed for event posts.
*
* Retrieves and returns the count of approved RSVP comments for event posts.
*
* @since 1.0.0
*
* @param int $comments_number The original number of comments.
* @param int $post_id The ID of the post.
* @return int Adjusted number of comments.
*/
public function adjust_comments_number( int $comments_number, int $post_id ): int {
if ( Event::POST_TYPE !== get_post_type( $post_id ) ) {
return $comments_number;
}

$comment_count = get_comment_count( $post_id );

return $comment_count['approved'] ?? 0;
}
}
Loading
Loading