Skip to content

Commit

Permalink
Merge pull request #6 from newfold-labs/add/coming-soon
Browse files Browse the repository at this point in the history
Disable coming soon upon plugin deactivation
  • Loading branch information
wpalani authored Jan 11, 2024
2 parents b595380 + f366e9c commit e88fb69
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
26 changes: 26 additions & 0 deletions includes/Deactivation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,35 @@ 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->disable_coming_soon();
}

/**
* 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 WP_REST_Response REST response
*/
public function sendEvent() {
$event = array(
'action' => $this->action,
'category' => $this->category,
'data' => $this->data,
'queue' => false,
);

$this->request->set_body( wp_json_encode( $event ) );
return rest_do_request( $this->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 {
/**
* send event.
*
* @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 e88fb69

Please sign in to comment.