Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable coming soon upon plugin deactivation #6

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
}
}
Loading