Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Events | |
0.00% |
0 / 27 |
|
0.00% |
0 / 6 |
182 | |
0.00% |
0 / 1 |
| get_valid_actions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_category | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| send | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| validate_action | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| validate_category | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| validate | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | namespace NewfoldLabs\WP\Module\Patterns\Library; |
| 3 | |
| 4 | /** |
| 5 | * Contains data related to WonderBlocks Hiive Events. |
| 6 | */ |
| 7 | final class Events { |
| 8 | /** |
| 9 | * The category of an event. |
| 10 | * |
| 11 | * @var string |
| 12 | */ |
| 13 | protected static $category = 'wonder_blocks'; |
| 14 | |
| 15 | /** |
| 16 | * List of valid actions that an event can perform. |
| 17 | * |
| 18 | * A value of true indicates that the action is valid, set it to null if you want to invalidate an action. |
| 19 | * |
| 20 | * @var array |
| 21 | */ |
| 22 | protected static $valid_actions = array( |
| 23 | 'template_inserted' => true, |
| 24 | 'template_searched' => true, |
| 25 | 'pattern_searched' => true, |
| 26 | 'pattern_inserted' => true, |
| 27 | 'pattern_favorited' => true, |
| 28 | 'template_favorited' => true, |
| 29 | 'modal_open' => true, |
| 30 | ); |
| 31 | |
| 32 | /** |
| 33 | * Returns the list of valid actions that an event can perform |
| 34 | * |
| 35 | * @return array |
| 36 | */ |
| 37 | public static function get_valid_actions() { |
| 38 | return self::$valid_actions; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Valid category of on event. |
| 43 | * |
| 44 | * @return string |
| 45 | */ |
| 46 | public static function get_category() { |
| 47 | return self::$category; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Sends a Hiive Event to the data module API. |
| 52 | * |
| 53 | * @param array $event The event to send. |
| 54 | * @return WP_REST_Response|WP_Error |
| 55 | */ |
| 56 | public static function send( $event ) { |
| 57 | $event = self::validate( $event ); |
| 58 | if ( ! $event ) { |
| 59 | return new \WP_Error( |
| 60 | 'nfd_wonder_blocks_error', |
| 61 | __( 'Bad event structure/value.', 'nfd-wonder-blocks' ) |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | $event_data_request = new \WP_REST_Request( |
| 66 | \WP_REST_Server::CREATABLE, |
| 67 | NFD_MODULE_DATA_EVENTS_API |
| 68 | ); |
| 69 | $event_data_request->set_body_params( $event ); |
| 70 | |
| 71 | $response = rest_do_request( $event_data_request ); |
| 72 | |
| 73 | if ( $response->is_error() ) { |
| 74 | return $response->as_error(); |
| 75 | } |
| 76 | |
| 77 | return $response; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Validates the action performed in an event. |
| 82 | * |
| 83 | * @param string $action The action performed in an event. |
| 84 | * @return boolean |
| 85 | */ |
| 86 | public static function validate_action( $action ) { |
| 87 | $valid_actions = self::get_valid_actions(); |
| 88 | if ( ! isset( $valid_actions[ $action ] ) ) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Validates the category of an event. |
| 97 | * |
| 98 | * @param string $category The category of an event. |
| 99 | * @return boolean |
| 100 | */ |
| 101 | public static function validate_category( $category ) { |
| 102 | return self::get_category() === $category; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Sanitizes and validates the action and category parameters of an event. |
| 107 | * |
| 108 | * @param array $event The event to sanitize and validate. |
| 109 | * @return array|boolean |
| 110 | */ |
| 111 | public static function validate( $event ) { |
| 112 | if ( ! isset( $event['action'] ) || ! self::validate_action( $event['action'] ) ) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | if ( ! isset( $event['category'] ) || ! self::validate_category( $event['category'] ) ) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | return $event; |
| 121 | } |
| 122 | } |