-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5346a21
commit e58ade3
Showing
3 changed files
with
146 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
public_html/wp-content/plugins/camptix/inc/class-camptix-actions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
class Camptix_Actions { | ||
|
||
// Denotes ticketing is happening. | ||
public const TICKET_ACTION = 'tix_action'; | ||
|
||
// Coupons. | ||
public const COUPON = 'tix_coupon'; | ||
|
||
// Third Party for ticket purchases. | ||
public const SINGLE_TICKET_PURCHASE = 'tix_single_ticket_purchase'; | ||
|
||
// Passed along. | ||
public const TICKETS_SELECTED = 'tix_tickets_selected'; | ||
|
||
// Editing ticket information. | ||
public const ATTENDEE_ID = 'tix_attendee_id'; | ||
public const EDIT_TOKEN = 'tix_edit_token'; | ||
public const ACCESS_TOKEN = 'tix_access_token'; | ||
|
||
// Reservations. | ||
public const RESERVATION_ID = 'tix_reservation_id'; | ||
public const RESERVATION_TOKEN = 'tix_reservation_token'; | ||
|
||
// Generic errors. | ||
public const ERRORS = 'tix_errors'; | ||
|
||
private $type; | ||
private $sanitizer; | ||
|
||
private function __construct( string $type, callable $sanitizer ) { | ||
$this->type = $type; | ||
$this->sanitizer = $sanitizer; | ||
} | ||
|
||
public static function TEXT(): self { | ||
return new self( 'text', fn( $value ) => sanitize_text_field( $value ) ); | ||
} | ||
|
||
public static function INTEGER(): self { | ||
return new self( 'int', fn( $value ) => absint( $value ) ); | ||
} | ||
|
||
public static function ARRAY_INTEGER(): self { | ||
return new self( 'array_int', fn( $value ) => is_array( $value ) | ||
? array_map( 'absint', $value ) | ||
: array( absint( $value ) ) | ||
); | ||
} | ||
|
||
public static function ARRAY_STR(): self { | ||
return new self( 'array_str', fn( $value ) => is_array( $value ) | ||
? array_map( 'sanitize_text_field', $value ) | ||
: array( sanitize_text_field( $value ) ) | ||
); | ||
} | ||
|
||
public static function get_allowed_parameters(): array { | ||
return [ | ||
self::TICKET_ACTION => self::TEXT(), | ||
self::TICKETS_SELECTED => self::ARRAY_INTEGER(), | ||
self::COUPON => self::TEXT(), | ||
self::ATTENDEE_ID => self::INTEGER(), | ||
self::EDIT_TOKEN => self::TEXT(), | ||
self::ACCESS_TOKEN => self::TEXT(), | ||
self::RESERVATION_ID => self::INTEGER(), | ||
self::RESERVATION_TOKEN => self::TEXT(), | ||
self::ERRORS => self::ARRAY_STR(), | ||
]; | ||
} | ||
|
||
public function get_type(): string { | ||
return $this->type; | ||
} | ||
|
||
public function sanitize( $value ) { | ||
return call_user_func( $this->sanitizer, $value ); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
public_html/wp-content/plugins/camptix/tests/inc/test-class-camptix-actions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class Camptix_Actions_Test extends WP_UnitTestCase { | ||
|
||
public function testConstants() { | ||
$this->assertSame( 'tix_action', Camptix_Actions::TICKET_ACTION ); | ||
$this->assertSame( 'tix_coupon', Camptix_Actions::COUPON ); | ||
$this->assertSame( 'tix_single_ticket_purchase', Camptix_Actions::SINGLE_TICKET_PURCHASE ); | ||
$this->assertSame( 'tix_tickets_selected', Camptix_Actions::TICKETS_SELECTED ); | ||
$this->assertSame( 'tix_attendee_id', Camptix_Actions::ATTENDEE_ID ); | ||
$this->assertSame( 'tix_edit_token', Camptix_Actions::EDIT_TOKEN ); | ||
$this->assertSame( 'tix_access_token', Camptix_Actions::ACCESS_TOKEN ); | ||
$this->assertSame( 'tix_reservation_id', Camptix_Actions::RESERVATION_ID ); | ||
$this->assertSame( 'tix_reservation_token', Camptix_Actions::RESERVATION_TOKEN ); | ||
$this->assertSame( 'tix_errors', Camptix_Actions::ERRORS ); | ||
} | ||
|
||
public function testGetAllowedParameters() { | ||
$parameters = Camptix_Actions::get_allowed_parameters(); | ||
|
||
// Check the keys and their associated types | ||
$this->assertArrayHasKey( Camptix_Actions::TICKET_ACTION, $parameters ); | ||
$this->assertInstanceOf( Camptix_Actions::class, $parameters[ Camptix_Actions::TICKET_ACTION ] ); | ||
|
||
$this->assertArrayHasKey( Camptix_Actions::TICKETS_SELECTED, $parameters ); | ||
$this->assertInstanceOf( Camptix_Actions::class, $parameters[ Camptix_Actions::TICKETS_SELECTED ] ); | ||
|
||
// More assertions for each expected key | ||
$this->assertArrayHasKey( Camptix_Actions::COUPON, $parameters ); | ||
$this->assertArrayHasKey( Camptix_Actions::ATTENDEE_ID, $parameters ); | ||
$this->assertArrayHasKey( Camptix_Actions::EDIT_TOKEN, $parameters ); | ||
$this->assertArrayHasKey( Camptix_Actions::ACCESS_TOKEN, $parameters ); | ||
$this->assertArrayHasKey( Camptix_Actions::RESERVATION_ID, $parameters ); | ||
$this->assertArrayHasKey( Camptix_Actions::RESERVATION_TOKEN, $parameters ); | ||
$this->assertArrayHasKey( Camptix_Actions::ERRORS, $parameters ); | ||
} | ||
} |