PluginProbe
Manage – Centralized site maintenance and monitoring / trunk
Manage – Centralized site maintenance and monitoring vtrunk
1.0.9 1.0.8 1.0.7 1.0.6 1.0.5 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4
manage / modules / connect / module.php

module.php in Manage – Centralized site maintenance and monitoring trunk, at modules/connect/module.php

189 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Manage\Modules\Connect;
3
4 use ElementorOne\Connect\Facade;
5 use Manage\Classes\Manage_Client;
6 use Manage\Classes\Logger;
7 use Manage\Classes\Module_Base;
8 use Manage\Classes\System_User;
9 use Manage\Modules\Settings\Components\Page;
10 use Manage\Modules\Connect\Classes\Config;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly
14 }
15
16 class Module extends Module_Base {
17
18 private static bool $sync_scheduled = false;
19
20 public function get_name(): string {
21 return 'connect';
22 }
23
24 public static function is_connected(): bool {
25 $facade = self::get_connect();
26 $access_token = $facade->data()->get_access_token();
27
28 return ! ! $access_token && $facade->utils()->is_valid_home_url();
29 }
30
31 public function __construct() {
32 add_action( 'elementor_one/manage_connected', [ $this, 'on_connect' ] );
33 add_action( 'elementor_one/manage_migration_run', [ $this, 'on_migration_run' ] );
34 add_action( 'permalink_structure_changed', [ $this, 'on_permalink_structure_changed' ], 10, 0 );
35 add_action( 'upgrader_process_complete', [ $this, 'on_upgrader_process_complete' ], 10, 2 );
36 add_action( 'activated_plugin', [ $this, 'on_activated_plugin' ] );
37 add_action( 'deactivated_plugin', [ $this, 'on_deactivated_plugin' ] );
38 add_action( 'switch_theme', [ $this, 'on_switch_theme' ] );
39
40 // Disable license check for Manage (Free version)
41 add_filter( 'elementor_one/' . Config::APP_PREFIX . '_license_check_enabled', '__return_false' );
42
43 Facade::make( [
44 'app_name' => Config::APP_NAME,
45 'app_prefix' => Config::APP_PREFIX,
46 'app_rest_namespace' => Config::APP_REST_NAMESPACE,
47 'base_url' => Config::BASE_URL,
48 'admin_page' => Config::ADMIN_PAGE,
49 'app_type' => Config::APP_TYPE,
50 'scopes' => Config::SCOPES,
51 'state_nonce' => Config::STATE_NONCE,
52 'connect_mode' => Config::CONNECT_MODE,
53 'plugin_slug' => Config::PLUGIN_SLUG,
54 ] );
55 }
56
57 public static function get_connect(): Facade {
58 return Facade::get( Config::PLUGIN_SLUG );
59 }
60
61 public function on_connect() {
62 $system_user = System_User::autofix_system_user();
63 if ( is_wp_error( $system_user ) ) {
64 Logger::error( 'Failed to autofix system user on connection: ' . esc_html( $system_user->get_error_message() ) );
65 }
66
67 $site_registered = Manage_Client::register_website();
68 if ( is_wp_error( $site_registered ) ) {
69 Logger::error( 'Failed to register website: ' . esc_html( $site_registered->get_error_message() ) );
70 }
71
72 $has_errors = is_wp_error( $system_user ) || is_wp_error( $site_registered );
73
74 if ( ! $has_errors ) {
75 $redirect_url = add_query_arg( [ 'ref' => 'connect' ], Page::get_manage_all_sites_url() );
76 wp_redirect( $redirect_url ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
77 exit;
78 }
79 }
80
81 public function on_migration_run() {
82 $system_user = System_User::autofix_system_user();
83 if ( is_wp_error( $system_user ) ) {
84 Logger::error( 'Failed to autofix system user on migration run: ' . esc_html( $system_user->get_error_message() ) );
85 }
86
87 $site_registered = Manage_Client::register_website();
88 if ( is_wp_error( $site_registered ) ) {
89 Logger::error( 'Failed to register website on migration run: ' . esc_html( $site_registered->get_error_message() ) );
90 }
91 }
92
93 public function on_permalink_structure_changed() {
94 if ( ! static::is_connected() ) {
95 return;
96 }
97
98 $site_registered = Manage_Client::register_website();
99 if ( is_wp_error( $site_registered ) ) {
100 Logger::error( 'Failed to re-register website after permalink change: ' . esc_html( $site_registered->get_error_message() ) );
101 }
102 }
103
104 /**
105 * `upgrader_process_complete` can fire multiple times within a single
106 * request — not just for manual bulk-selects (WP core coalesces those
107 * into a single firing after the whole batch), but also for WP-Cron
108 * auto-updates, which loop over plugins/themes/core one at a time and
109 * fire the hook after EACH item within the same request.
110 *
111 * Both `update` and `install` actions are synced (plugin, theme, or
112 * core) EXCEPT translation/language-pack updates or installs, which
113 * don't affect the site state we report on.
114 *
115 * The actual sync is deferred to `shutdown` (scheduled at most once,
116 * guarded by the static flag) rather than fired inline here. Since
117 * `shutdown` only runs after every line of PHP for the request has
118 * executed, this guarantees the outbound sync is triggered only after
119 * the ENTIRE batch of updates/installs has finished — not after just
120 * the first item — so the eventual async site-state fetch never races
121 * a still-in-progress update.
122 */
123 public function on_upgrader_process_complete( $upgrader, $hook_extra ) {
124 if ( ! isset( $hook_extra['action'] ) || ! in_array( $hook_extra['action'], [ 'update', 'install' ], true ) ) {
125 return;
126 }
127
128 // Translation/language-pack updates or installs don't change site state we report on, so skip syncing for those.
129 if ( isset( $hook_extra['type'] ) && 'translation' === $hook_extra['type'] ) {
130 return;
131 }
132
133 $this->schedule_sync_after_site_change();
134 }
135
136 /**
137 * Activating a plugin changes the site state we report on (active
138 * plugin list), so re-sync just like an update/install. Shares the
139 * same dedup flag and shutdown deferral as `on_upgrader_process_complete()`
140 * so a bulk-activate, or an activation happening in the same request
141 * as an update/install, still only triggers a single outbound sync.
142 */
143 public function on_activated_plugin() {
144 $this->schedule_sync_after_site_change();
145 }
146
147 /**
148 * Deactivating a plugin changes the site state we report on (active
149 * plugin list) just as much as activating one does, so it shares the
150 * same sync mechanism for symmetry.
151 */
152 public function on_deactivated_plugin() {
153 $this->schedule_sync_after_site_change();
154 }
155
156 /**
157 * Switching the active theme changes the site state we report on,
158 * so re-sync using the same shared, deduped mechanism.
159 */
160 public function on_switch_theme() {
161 $this->schedule_sync_after_site_change();
162 }
163
164 private function schedule_sync_after_site_change() {
165 if ( ! static::is_connected() ) {
166 return;
167 }
168
169 if ( static::$sync_scheduled ) {
170 return;
171 }
172
173 static::$sync_scheduled = true;
174
175 add_action( 'shutdown', [ $this, 'sync_website_on_shutdown' ] );
176 }
177
178 public function sync_website_on_shutdown() {
179 $site_synced = Manage_Client::sync_website();
180 if ( is_wp_error( $site_synced ) ) {
181 Logger::error( 'Failed to sync website after upgrade: ' . esc_html( $site_synced->get_error_message() ) );
182 }
183 }
184
185 public static function is_elementor_one(): bool {
186 return self::get_connect()->get_config( 'app_type' ) !== Config::APP_TYPE;
187 }
188 }
189