| 1 |
<?php |
| 2 |
|
| 3 |
namespace RTFL\App; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
use RTFL\App\Helpers\Request; |
| 8 |
use RTFL\App\Helpers\Settings; |
| 9 |
use RTFL\App\Helpers\Multisite; |
| 10 |
|
| 11 |
|
| 12 |
class Setup { |
| 13 |
/** |
| 14 |
* Init setup. |
| 15 |
*/ |
| 16 |
public static function init() { |
| 17 |
register_activation_hook( RTFL_PLUGIN_FILE, [ __CLASS__, 'activate' ] ); |
| 18 |
register_deactivation_hook( RTFL_PLUGIN_FILE, [ __CLASS__, 'deactivate' ] ); |
| 19 |
register_uninstall_hook( RTFL_PLUGIN_FILE, [ __CLASS__, 'uninstall' ] ); |
| 20 |
|
| 21 |
if ( Multisite::isMultiSite() ) { |
| 22 |
add_action( 'network_activate_' . plugin_basename( RTFL_PLUGIN_FILE ), [ __CLASS__, 'networkActivate' ] ); |
| 23 |
add_action( 'network_deactivate_' . plugin_basename( RTFL_PLUGIN_FILE ), [ __CLASS__, 'networkDeactivate' ] ); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Run plugin activation scripts. |
| 29 |
*/ |
| 30 |
public static function activate() { |
| 31 |
// Store the plugin version so the upgrade routine |
| 32 |
// (checked on plugins_loaded) can detect version changes. |
| 33 |
update_option( 'retainful_db_version', RTFL_VERSION ); |
| 34 |
|
| 35 |
// Re-enable any Retainful webhooks that may have been |
| 36 |
// auto-disabled by WooCommerce during a previous session. |
| 37 |
// Only meaningful when WC is active (webhooks are a WC feature). |
| 38 |
if ( function_exists( 'wc_get_webhook' ) ) { |
| 39 |
\RTFL\App\Controllers\Webhook::webhookHealthCheck(); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Run plugin network-wide activation scripts. |
| 45 |
*/ |
| 46 |
public static function networkActivate() { |
| 47 |
// Network-wide activation - store settings at network level |
| 48 |
self::activate(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Run plugin deactivation scripts. |
| 53 |
*/ |
| 54 |
public static function deactivate() { |
| 55 |
// Unschedule recurring actions so they don't fire while |
| 56 |
// the plugin is inactive. |
| 57 |
if ( function_exists( 'as_unschedule_all_actions' ) ) { |
| 58 |
as_unschedule_all_actions( 'retainful_webhook_health_check' ); |
| 59 |
as_unschedule_all_actions( 'retainful_action_scheduler_cleanup' ); |
| 60 |
// A cleanup pass chains itself, so an in-flight one would keep |
| 61 |
// queueing batches after the plugin is switched off. |
| 62 |
as_unschedule_all_actions( \RTFL\App\Controllers\CouponCleanup::HOOK ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Run plugin network-wide deactivation scripts. |
| 68 |
*/ |
| 69 |
public static function networkDeactivate() { |
| 70 |
// Network-wide deactivation |
| 71 |
self::deactivate(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Run plugin uninstall scripts. |
| 76 |
*/ |
| 77 |
public static function uninstall() { |
| 78 |
$settings = Settings::getRetainfulOption('sync', 'woocommerce'); |
| 79 |
|
| 80 |
if(empty($settings) || !is_array($settings) || empty($settings['webhook_secret']) || empty($settings['organization_uid']) || empty($settings['app_url'])) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$secret_key = $settings['webhook_secret']; |
| 85 |
$organization_uid = $settings['organization_uid']; |
| 86 |
$app_id = $settings['app_id'] ?? ''; |
| 87 |
|
| 88 |
$url = self::getDomain() . '/v5/api/woocommerce/'. $organization_uid .'/webhooks/uninstall'; |
| 89 |
|
| 90 |
$signature = hash_hmac('sha256', $organization_uid, $secret_key); |
| 91 |
$headers = [ |
| 92 |
'x-organization-uid' => $organization_uid, |
| 93 |
'x-signature' => $signature, |
| 94 |
'Content-Type' => 'application/json', |
| 95 |
]; |
| 96 |
$response = Request::request($url, [], 'post', '', $headers); |
| 97 |
if(!$response->success) { |
| 98 |
return; |
| 99 |
} |
| 100 |
$sync_options = Settings::getRetainfulOption( 'sync', 'woocommerce' ); |
| 101 |
if ( ! empty( $sync_options['webhook_secret'] ) && function_exists( 'wc_get_webhook' ) ) { |
| 102 |
\RTFL\App\Controllers\Webhook::deletePreviousWebhooksForThisOrganization( $sync_options['webhook_secret'] ); |
| 103 |
} |
| 104 |
if(!empty($app_id)){ |
| 105 |
Settings::deleteRestKeys($app_id); |
| 106 |
} |
| 107 |
Multisite::deleteOption( 'retainful_plugin_options' ); |
| 108 |
Multisite::deleteOption( 'retainful_general_settings' ); |
| 109 |
Multisite::deleteOption( 'retainful_popup_json' ); |
| 110 |
Multisite::deleteOption( 'rtfl_wpforms_integrations' ); |
| 111 |
|
| 112 |
if ( Multisite::isMultiSite() ) { |
| 113 |
global $wpdb; |
| 114 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 115 |
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs} WHERE public = '1' AND archived = '0' AND spam = '0' AND deleted = '0'" ); |
| 116 |
foreach ( $blog_ids as $blog_id ) { |
| 117 |
switch_to_blog( $blog_id ); |
| 118 |
delete_option( 'retainful_plugin_options' ); |
| 119 |
delete_option( 'retainful_general_settings' ); |
| 120 |
delete_option( 'retainful_popup_json' ); |
| 121 |
delete_option( 'rtfl_wpforms_integrations' ); |
| 122 |
restore_current_blog(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return true; |
| 127 |
} |
| 128 |
private static function getDomain(){ |
| 129 |
return apply_filters('rtfl_retainful_disconnect_domain', 'https://events.retainful.net'); |
| 130 |
} |
| 131 |
} |