-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from newfold-labs/enhance/PRESS2-856-analytics
Implement Analytics
- Loading branch information
Showing
28 changed files
with
511 additions
and
211 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
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
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
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,83 @@ | ||
<?php | ||
|
||
namespace NewfoldLabs\WP\Module\Onboarding\Services; | ||
|
||
use NewfoldLabs\WP\Module\Onboarding\Data\Events; | ||
|
||
/** | ||
* Class for handling analytics events. | ||
*/ | ||
class EventService { | ||
|
||
/** | ||
* Sends a Hiive Event to the data module API. | ||
* | ||
* @param array $event The event to send. | ||
* @return WP_REST_Response|WP_Error | ||
*/ | ||
public static function send( $event ) { | ||
$event = self::validate( $event ); | ||
if ( ! $event ) { | ||
return new \WP_Error( | ||
'nfd_module_onboarding_error', | ||
__( 'Bad event structure/value.', 'wp-module-onboarding' ) | ||
); | ||
} | ||
|
||
$event_data_request = new \WP_REST_Request( | ||
\WP_REST_Server::CREATABLE, | ||
NFD_MODULE_DATA_EVENTS_API | ||
); | ||
$event_data_request->set_body_params( $event ); | ||
|
||
$response = rest_do_request( $event_data_request ); | ||
if ( $response->is_error() ) { | ||
return $response->as_error(); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Validates the category of an event. | ||
* | ||
* @param string $category The category of an event. | ||
* @return boolean | ||
*/ | ||
public static function validate_category( $category ) { | ||
return Events::get_category() === $category; | ||
} | ||
|
||
/** | ||
* Validates the action performed in an event. | ||
* | ||
* @param string $action The action performed in an event. | ||
* @return boolean | ||
*/ | ||
public static function validate_action( $action ) { | ||
$valid_actions = Events::get_valid_actions(); | ||
if ( ! isset( $valid_actions[ $action ] ) ) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Sanitizes and validates the action and category parameters of an event. | ||
* | ||
* @param array $event The event to sanitize and validate. | ||
* @return array|boolean | ||
*/ | ||
public static function validate( $event ) { | ||
if ( ! isset( $event['action'] ) || ! self::validate_action( $event['action'] ) ) { | ||
return false; | ||
} | ||
|
||
if ( ! isset( $event['category'] ) || ! self::validate_category( $event['category'] ) ) { | ||
return false; | ||
} | ||
|
||
return $event; | ||
} | ||
} |
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
Oops, something went wrong.