forked from newfold-labs/wp-module-deactivation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send
site_launched
event on deactivation
- Loading branch information
Showing
3 changed files
with
158 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** | ||
* Deactivation event. | ||
* | ||
* @package NewfoldLabs\WP\Module\Deactivation | ||
*/ | ||
|
||
namespace NewfoldLabs\WP\Module\Deactivation\Events; | ||
|
||
use WP_REST_Request; | ||
|
||
/** | ||
* Event class. | ||
*/ | ||
class Event { | ||
/** | ||
* Key representing the event action that occurred. | ||
* | ||
* @var string | ||
*/ | ||
public $action; | ||
|
||
/** | ||
* Event category. | ||
* | ||
* @var string | ||
*/ | ||
public $category; | ||
|
||
/** | ||
* Array of extra data related to the event. | ||
* | ||
* @var array | ||
*/ | ||
public $data; | ||
|
||
/** | ||
* Data module endpoint to send the event to. | ||
* | ||
* @var string | ||
*/ | ||
public $endpoint = '/newfold-data/v1/events/'; | ||
|
||
/** | ||
* WP_REST_Request instance. | ||
* | ||
* @var WP_REST_Request | ||
*/ | ||
public $request; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $action Key representing the event action that occurred. | ||
* @param string $category Event category. | ||
* @param array $data Array of extra data related to the event. | ||
*/ | ||
public function __construct( $category = 'plugin_deactivation', $data = array() ) { | ||
$this->category = $category; | ||
$this->data = $data; | ||
$this->request = new WP_REST_Request( 'POST', $this->endpoint ); | ||
} | ||
|
||
/** | ||
* Send the event to the data module endpoint. | ||
* | ||
* @return void | ||
*/ | ||
public function sendEvent() { | ||
$event = array( | ||
'action' => $this->action, | ||
'category' => $this->category, | ||
'data' => $this->data, | ||
'queue' => false, | ||
); | ||
|
||
$request = $this->request->set_body_params( $event ); | ||
rest_do_request( $request ); | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
/** | ||
* Site launched deactivation event. | ||
* | ||
* @package NewfoldLabs\WP\Module\Deactivation | ||
*/ | ||
|
||
namespace NewfoldLabs\WP\Module\Deactivation\Events; | ||
|
||
/** | ||
* Site launched event class. | ||
*/ | ||
class SiteLaunched extends Event { | ||
/** | ||
* Constructor. | ||
* | ||
* @return void | ||
*/ | ||
public function send() { | ||
$this->action = 'site_launched'; | ||
$this->data = array( | ||
'ttl' => $this->getInstallTime(), | ||
); | ||
|
||
return $this->sendEvent(); | ||
} | ||
|
||
/** | ||
* Calculate install time. | ||
* | ||
* @return int | ||
*/ | ||
private function getInstallTime() { | ||
$mm_install_time = get_option( 'mm_install_date', gmdate( 'M d, Y' ) ); | ||
|
||
return time() - strtotime( $mm_install_time ); | ||
} | ||
} |