Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 47 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| EventService | |
0.00% |
0 / 47 |
|
0.00% |
0 / 5 |
380 | |
0.00% |
0 / 1 |
| send | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
| validate_category | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| validate_action | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| validate | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
| add_timestamp_and_ttl | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Onboarding\Services; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Onboarding\Data\Events; |
| 6 | use NewfoldLabs\WP\Module\Onboarding\Data\Options; |
| 7 | |
| 8 | /** |
| 9 | * Class for handling analytics events. |
| 10 | */ |
| 11 | class EventService { |
| 12 | |
| 13 | /** |
| 14 | * Sends a Hiive Event to the data module API. |
| 15 | * |
| 16 | * @param array $event The event to send. |
| 17 | * @return WP_REST_Response|WP_Error |
| 18 | */ |
| 19 | public static function send( $event ) { |
| 20 | $event = self::validate( $event ); |
| 21 | if ( ! $event ) { |
| 22 | return new \WP_Error( |
| 23 | 'nfd_module_onboarding_error', |
| 24 | __( 'Bad event structure/value.', 'wp-module-onboarding' ) |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | // Add timestamp and ttl to specific events |
| 29 | $event = self::add_timestamp_and_ttl( $event ); |
| 30 | |
| 31 | $event_data_request = new \WP_REST_Request( |
| 32 | \WP_REST_Server::CREATABLE, |
| 33 | NFD_MODULE_DATA_EVENTS_API |
| 34 | ); |
| 35 | $event_data_request->set_body_params( $event ); |
| 36 | |
| 37 | $response = rest_do_request( $event_data_request ); |
| 38 | if ( $response->is_error() ) { |
| 39 | return $response->as_error(); |
| 40 | } |
| 41 | |
| 42 | return $response; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Validates the category of an event. |
| 47 | * |
| 48 | * @param string $category The category of an event. |
| 49 | * @return boolean |
| 50 | */ |
| 51 | public static function validate_category( $category ) { |
| 52 | $default_categories = Events::get_category(); |
| 53 | foreach ( $default_categories as $event_category ) { |
| 54 | if ( $event_category === $category ) { |
| 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Validates the action performed in an event. |
| 63 | * |
| 64 | * @param string $action The action performed in an event. |
| 65 | * @return boolean |
| 66 | */ |
| 67 | public static function validate_action( $action ) { |
| 68 | $valid_actions = Events::get_valid_actions(); |
| 69 | if ( ! isset( $valid_actions[ $action ] ) ) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Sanitizes and validates the action and category parameters of an event. |
| 78 | * |
| 79 | * @param array $event The event to sanitize and validate. |
| 80 | * @return array|boolean |
| 81 | */ |
| 82 | public static function validate( $event ) { |
| 83 | if ( ! isset( $event['action'] ) || ! self::validate_action( $event['action'] ) ) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | if ( ! isset( $event['category'] ) || ! self::validate_category( $event['category'] ) ) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | return $event; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Adds timestamp and ttl properties to specific events (onboarding_started and onboarding_complete). |
| 96 | * |
| 97 | * @param array $event The event to enhance. |
| 98 | * @return array The enhanced event. |
| 99 | */ |
| 100 | private static function add_timestamp_and_ttl( $event ) { |
| 101 | $current_time = time(); |
| 102 | |
| 103 | switch ( $event['action'] ) { |
| 104 | case 'onboarding_started': |
| 105 | // Add timestamp to onboarding_started event |
| 106 | $event['data']['timestamp'] = $current_time; |
| 107 | break; |
| 108 | |
| 109 | case 'onboarding_complete': |
| 110 | // Add timestamp and ttl to onboarding_complete event |
| 111 | $event['data']['timestamp'] = $current_time; |
| 112 | |
| 113 | // Use the same completion time that was stored in handle_completed() |
| 114 | $completion_time = get_option( Options::get_option_name( 'completed_time' ) ); |
| 115 | $start_time = get_option( Options::get_option_name( 'start_time' ) ); |
| 116 | |
| 117 | if ( $start_time ) { |
| 118 | if ( $completion_time ) { |
| 119 | // Use stored completion time |
| 120 | $ttl_seconds = $completion_time - $start_time; |
| 121 | } else { |
| 122 | // Fallback to current time if completion_time not found |
| 123 | $ttl_seconds = $current_time - $start_time; |
| 124 | } |
| 125 | |
| 126 | if ( $ttl_seconds >= 0 ) { |
| 127 | $event['data']['ttl'] = $ttl_seconds; |
| 128 | } |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | return $event; |
| 134 | } |
| 135 | } |