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

Issue #865: Refactor REST API validation to its own class #883

Merged
merged 3 commits into from
Sep 19, 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
140 changes: 6 additions & 134 deletions includes/core/classes/class-event-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ protected function email_route(): array {
'args' => array(
'post_id' => array(
'required' => true,
'validate_callback' => array( $this, 'validate_event_post_id' ),
'validate_callback' => array( Validate::class, 'event_post_id' ),
),
'message' => array(
'required' => false,
'validate_callback' => 'sanitize_text_field',
),
'send' => array(
'required' => true,
'validate_callback' => array( $this, 'validate_send' ),
'validate_callback' => array( Validate::class, 'send' ),
),
),
),
Expand Down Expand Up @@ -158,11 +158,11 @@ protected function rsvp_route(): array {
'args' => array(
'post_id' => array(
'required' => true,
'validate_callback' => array( $this, 'validate_event_post_id' ),
'validate_callback' => array( Validate::class, 'event_post_id' ),
),
'status' => array(
'required' => true,
'validate_callback' => array( $this, 'validate_rsvp_status' ),
'validate_callback' => array( Validate::class, 'rsvp_status' ),
),
),
),
Expand All @@ -188,11 +188,11 @@ protected function events_list_route(): array {
'args' => array(
'event_list_type' => array(
'required' => true,
'validate_callback' => array( $this, 'validate_event_list_type' ),
'validate_callback' => array( Validate::class, 'event_list_type' ),
),
'max_number' => array(
'required' => true,
'validate_callback' => array( $this, 'validate_number' ),
'validate_callback' => array( Validate::class, 'number' ),
),
'datetime_format' => array(
'required' => false,
Expand All @@ -205,134 +205,6 @@ protected function events_list_route(): array {
);
}

/**
* Validate RSVP status.
*
* Validates whether a given parameter is a valid RSVP status.
*
* @since 1.0.0
*
* @param string $param An RSVP status to validate.
* @return bool True if the parameter is a valid RSVP status, false otherwise.
*/
public function validate_rsvp_status( $param ): bool {
return in_array(
$param,
array(
'attending',
'waiting_list',
'not_attending',
'no_status',
),
true
);
}

/**
* Validate Event Post ID.
*
* Validates whether a given parameter is a valid Event Post ID.
*
* @since 1.0.0
*
* @param int|string $param A Post ID to validate.
* @return bool True if the parameter is a valid Event Post ID, false otherwise.
*/
public function validate_event_post_id( $param ): bool {
return (
$this->validate_number( $param ) &&
Event::POST_TYPE === get_post_type( $param )
);
}

/**
* Validate recipients for sending emails.
*
* Validates an array of email recipient options to ensure they are correctly structured.
*
* @since 1.0.0
*
* @param mixed $param An array of email recipients.
* @return bool True if the parameter is a valid array of email recipients, false otherwise.
*/
public function validate_send( $param ): bool {
$expected_params = array( 'all', 'attending', 'waiting_list', 'not_attending' );

if ( is_array( $param ) ) {
foreach ( $expected_params as $expected_param ) {
if (
! array_key_exists( $expected_param, $param ) ||
! is_bool( $param[ $expected_param ] )
) {
return false;
}
}

return true;
}

return false;
}

/**
* Validate a numeric value.
*
* Validates whether the given parameter is a valid numeric value greater than zero.
*
* @since 1.0.0
*
* @param int|string $param The value to validate.
* @return bool True if the parameter is a valid numeric value greater than zero, false otherwise.
*/
public function validate_number( $param ): bool {
return (
0 < intval( $param ) &&
is_numeric( $param )
);
}

/**
* Validate an event list type.
*
* Validates whether the given event list type parameter is valid (either 'upcoming' or 'past').
*
* @since 1.0.0
*
* @param string $param The event list type to validate.
* @return bool True if the parameter is a valid event list type, false otherwise.
*/
public function validate_event_list_type( string $param ): bool {
return in_array( $param, array( 'upcoming', 'past' ), true );
}

/**
* Validate a datetime string.
*
* Validates whether the given datetime string parameter is in the valid 'Y-m-d H:i:s' format.
*
* @since 1.0.0
*
* @param string $param The datetime string to validate.
* @return bool True if the parameter is a valid datetime string, false otherwise.
*/
public function validate_datetime( string $param ): bool {
return (bool) \DateTime::createFromFormat( 'Y-m-d H:i:s', $param );
}

/**
* Validate a timezone identifier.
*
* Validates whether the given timezone identifier parameter is valid.
*
* @since 1.0.0
*
* @param string $param The timezone identifier to validate.
* @return bool True if the parameter is a valid timezone identifier, false otherwise.
*/
public function validate_timezone( string $param ): bool {
return in_array( Utility::maybe_convert_utc_offset( $param ), Utility::list_timezone_and_utc_offsets(), true );
}

/**
* Send an event email notification to members.
*
Expand Down
160 changes: 160 additions & 0 deletions includes/core/classes/class-validate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
/**
* Handles misc. data validation
*
* This file contains the Validation class, which is responsible for validating
* object data.
*
* @package GatherPress\Core
* @since 1.0.0
*/

namespace GatherPress\Core;

use DateTime;

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

/**
* Class Validate.
*
* The Validate class is responsible for ensuring event/venue/object data is
* secure and of the correct format.
*
* @since 1.0.0
*/
class Validate {

/**
* Validate RSVP status.
*
* Validates whether a given parameter is a valid RSVP status.
*
* @since 1.0.0
*
* @param string $param An RSVP status to validate.
* @return bool True if the parameter is a valid RSVP status, false otherwise.
*/
public static function rsvp_status( $param ): bool {
return in_array(
$param,
array(
'attending',
'waiting_list',
'not_attending',
'no_status',
),
true
);
}

/**
* Validate Event Post ID.
*
* Validates whether a given parameter is a valid Event Post ID.
*
* @since 1.0.0
*
* @param int|string $param A Post ID to validate.
* @return bool True if the parameter is a valid Event Post ID, false otherwise.
*/
public static function event_post_id( $param ): bool {
return (
static::number( $param ) &&
Event::POST_TYPE === get_post_type( $param )
);
}

/**
* Validate a numeric value.
*
* Validates whether the given parameter is a valid numeric value greater than zero.
*
* @since 1.0.0
*
* @param int|string $param The value to validate.
* @return bool True if the parameter is a valid numeric value greater than zero, false otherwise.
*/
public static function number( $param ): bool {
return (
0 < intval( $param ) &&
is_numeric( $param )
);
}

/**
* Validate recipients for sending emails.
*
* Validates an array of email recipient options to ensure they are correctly structured.
*
* @since 1.0.0
*
* @param mixed $param An array of email recipients.
* @return bool True if the parameter is a valid array of email recipients, false otherwise.
*/
public static function send( $param ): bool {
$expected_params = array( 'all', 'attending', 'waiting_list', 'not_attending' );

if ( is_array( $param ) ) {
foreach ( $expected_params as $expected_param ) {
if (
! array_key_exists( $expected_param, $param ) ||
! is_bool( $param[ $expected_param ] )
) {
return false;
}
}

return true;
}

return false;
}

/**
* Validate an event list type.
*
* Validates whether the given event list type parameter is valid (either 'upcoming' or 'past').
*
* @since 1.0.0
*
* @param string $param The event list type to validate.
* @return bool True if the parameter is a valid event list type, false otherwise.
*/
public static function event_list_type( string $param ): bool {
return in_array( $param, array( 'upcoming', 'past' ), true );
}

/**
* Validate a datetime string.
*
* Validates whether the given datetime string parameter is in the valid 'Y-m-d H:i:s' format.
*
* @since 1.0.0
*
* @param string $param The datetime string to validate.
* @return bool True if the parameter is a valid datetime string, false otherwise.
*/
public static function datetime( string $param ): bool {
return (bool) DateTime::createFromFormat( 'Y-m-d H:i:s', $param );
}

/**
* Validate a timezone identifier.
*
* Validates whether the given timezone identifier parameter is valid.
*
* @since 1.0.0
*
* @param string $param The timezone identifier to validate.
* @return bool True if the parameter is a valid timezone identifier, false otherwise.
*/
public static function timezone( string $param ): bool {
return in_array(
Utility::maybe_convert_utc_offset( $param ),
Utility::list_timezone_and_utc_offsets(),
true
);
}
}
Loading
Loading