Skip to content

Commit

Permalink
Send site_launched event on deactivation
Browse files Browse the repository at this point in the history
  • Loading branch information
wpalani committed Jan 9, 2024
1 parent b595380 commit 8ef28e5
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
40 changes: 40 additions & 0 deletions includes/Deactivation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,49 @@ class Deactivation {
public function __construct( Container $container ) {
$this->container = $container;

register_deactivation_hook(
$container->plugin()->file,
array( $this, 'handle' )
);

// Plugin deactivation survey.
add_action( 'admin_head-plugins.php', function () {
new DeactivationSurvey();
} );
}

/**
* Handle deactivation.
*
* @return void
*/
public function handle() {
$this->site_launched_event();
$this->disable_coming_soon();
}

/**
* Send site launched event.
*
* @return void
*/
public function site_launched_event() {
$coming_soon_service = $this->container->has( 'comingSoon' ) ? $this->container->get( 'comingSoon' ) : null;
if ( $coming_soon_service && $coming_soon_service->is_enabled() ) {
$site_launch_event = new Events\SiteLaunched();
$site_launch_event->send();
}
}

/**
* Disable the coming soon page.
*
* @return void
*/
public function disable_coming_soon() {
$coming_soon_service = $this->container->has( 'comingSoon' ) ? $this->container->get( 'comingSoon' ) : null;
if ( $coming_soon_service && $coming_soon_service->is_enabled() ) {
$coming_soon_service->disable();
}
}
}
80 changes: 80 additions & 0 deletions includes/Events/Event.php
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 );
}
}
38 changes: 38 additions & 0 deletions includes/Events/SiteLaunched.php
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 );
}
}

0 comments on commit 8ef28e5

Please sign in to comment.