| @@ -11,12 +11,13 @@ | ||
| 11 | 11 | * @link https://searchatlas.com/ |
| 12 | 12 | * @since 1.0.0 |
| 13 | 13 | * |
| 14 | 14 | * @wordpress-plugin |
| 15 | - * Plugin Name: Search Atlas: The Premier AI SEO Plugin for Instant Optimization | |
| 15 | + * Plugin Name: Search Atlas SEO - OTTO AI SEO Automation for WordPress | |
| 16 | 16 | * Plugin URI: https://searchatlas.com/ |
| 17 | 17 | * Description: Search Atlas SEO is an intuitive WordPress Plugin that transforms the most complicated, most labor-intensive SEO tasks into streamlined, straightforward processes. With a few clicks, the meta-bulk update feature automates the re-optimization of meta tags using AI to increase clicks. Stay up-to-date with the freshest Google Search data for your entire site or targeted URLs within the Meta Sync plug-in page. |
| 18 | - * Version: 2.6.5 | |
| 18 | + * Version: 2.7.2 | |
| 19 | + * Requires PHP: 8.1 | |
| 19 | 20 | * Author: Search Atlas |
| 20 | 21 | * Author URI: https://searchatlas.com |
| 21 | 22 | * License: GPL v3 |
| 22 | 23 | * License URI: https://www.gnu.org/licenses/gpl-3.0.txt |
| @@ -30,20 +31,41 @@ | ||
| 30 | 31 | |
| 31 | 32 | // Composer classmap autoloader — loads all plugin classes on demand. |
| 32 | 33 | require_once __DIR__ . '/vendor/autoload.php'; |
| 33 | 34 | |
| 35 | +// Canonical sanitizer — required explicitly (not via the committed classmap) | |
| 36 | +// so it is guaranteed loadable everywhere canonicals are read or written. | |
| 37 | +require_once __DIR__ . '/includes/class-metasync-canonical-sanitizer.php'; | |
| 38 | + | |
| 39 | +// Editor-settings feature flags — required explicitly for the same reason: they | |
| 40 | +// gate output in the OTTO buffer and in third-party filter callbacks, both of | |
| 41 | +// which can run before the classmap has resolved anything else. | |
| 42 | +require_once __DIR__ . '/includes/class-metasync-feature-flags.php'; | |
| 43 | +Metasync_Feature_Flags::register_invalidation(); | |
| 44 | + | |
| 45 | +// Consent gate and write-once backup for third-party SEO storage — required | |
| 46 | +// explicitly for the same reason: every writer into Yoast / Rank Math / AIOSEO | |
| 47 | +// consults it, and a class that fails to load would read as "no consent" and | |
| 48 | +// silently stop those writes. | |
| 49 | +require_once __DIR__ . '/includes/class-metasync-seo-backup.php'; | |
| 50 | + | |
| 51 | +// Loaded beside the backup class because it is the only reader of what that | |
| 52 | +// class writes. Restore is inert until something calls it, so loading it early | |
| 53 | +// costs nothing and keeps the pair together. | |
| 54 | +require_once __DIR__ . '/includes/class-metasync-seo-restore.php'; | |
| 55 | + | |
| 34 | 56 | /** |
| 35 | 57 | * Currently plugin version. |
| 36 | 58 | * Start at version 1.0.0 and use SemVer - https://semver.org |
| 37 | 59 | * Rename this for your plugin and update it as you release new versions. |
| 38 | 60 | */ |
| 39 | -$metasync_version = '2.6.5'; | |
| 61 | +$metasync_version = '2.7.2'; | |
| 40 | 62 | define('METASYNC_VERSION', preg_match('/^\d+\.\d+/', $metasync_version) ? $metasync_version : '9.9.9'); |
| 41 | 63 | /** |
| 42 | 64 | * Define the current required php version |
| 43 | 65 | * This will be used to validate whether the user can install the plugin or not |
| 44 | 66 | */ |
| 45 | -define('METASYNC_MIN_PHP', '8.2'); | |
| 67 | +define('METASYNC_MIN_PHP', '8.1'); | |
| 46 | 68 | |
| 47 | 69 | /** |
| 48 | 70 | * Define the current required php version |
| 49 | 71 | * This will be used to validate whether the user can install the plugin or not |
| @@ -73,8 +95,18 @@ | ||
| 73 | 95 | define('METASYNC_GA4_MEASUREMENT_ID', 'G-SBLWW1EMTJ'); |
| 74 | 96 | define('METASYNC_GA4_API_SECRET', 'nMGs22mxQ3qVUy-aInqfZA'); |
| 75 | 97 | |
| 76 | 98 | /** |
| 99 | + * PostHog product analytics configuration. | |
| 100 | + * The write key is a public project key embedded by design (same precedent | |
| 101 | + * as the GA4 secret above); set METASYNC_POSTHOG_API_KEY in the environment | |
| 102 | + * to override it - including to an empty string, which disables PostHog. | |
| 103 | + */ | |
| 104 | +define('METASYNC_POSTHOG_HOST', 'https://posthog.internal.searchatlas.com'); | |
| 105 | +$metasync_posthog_key = getenv('METASYNC_POSTHOG_API_KEY'); | |
| 106 | +define('METASYNC_POSTHOG_API_KEY', false !== $metasync_posthog_key ? $metasync_posthog_key : 'phc_ntSayCJE7Ad9eBHRG6ygBaY4BJMa8rFjXcXESun9ftkh'); // gitleaks:allow -- public write-only PostHog project key | |
| 107 | + | |
| 108 | +/** | |
| 77 | 109 | * Define whether to show the plugin status in WordPress admin top navigation bar |
| 78 | 110 | * Set to false to hide the status indicator |
| 79 | 111 | */ |
| 80 | 112 | define('METASYNC_SHOW_ADMIN_BAR_STATUS', true); |
| @@ -107,9 +139,9 @@ | ||
| 107 | 139 | return $sanitized; |
| 108 | 140 | } |
| 109 | 141 | } |
| 110 | 142 | |
| 111 | -// Skip heavy MetaSync init on admin-ajax requests that don't target our own actions (Sentry issue 7441226449 / WP-227). | |
| 143 | +// Skip heavy MetaSync init on admin-ajax requests that don't target our own actions (Sentry issue 7441226449). | |
| 112 | 144 | if (!function_exists('metasync_is_non_metasync_admin_ajax')) { |
| 113 | 145 | function metasync_is_non_metasync_admin_ajax() { |
| 114 | 146 | static $result = null; |
| 115 | 147 | if ($result !== null) { |
| @@ -114,13 +146,19 @@ | ||
| 114 | 146 | static $result = null; |
| 115 | 147 | if ($result !== null) { |
| 116 | 148 | return $result; |
| 117 | 149 | } |
| 150 | + // The MCP bridge intentionally boots WordPress with DOING_AJAX set, but it | |
| 151 | + // is a MetaSync entry point rather than an unrelated admin AJAX request. | |
| 152 | + // The constant is only ever defined as true by the bridge bootstraps. | |
| 153 | + if (defined('METASYNC_MCP_BRIDGE')) { | |
| 154 | + return ($result = false); | |
| 155 | + } | |
| 118 | 156 | if (!defined('DOING_AJAX') || !DOING_AJAX) { |
| 119 | 157 | return ($result = false); |
| 120 | 158 | } |
| 121 | - $action = isset($_POST['action']) ? sanitize_text_field(wp_unslash($_POST['action'])) | |
| 122 | - : (isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : ''); | |
| 159 | + $action = isset($_POST['action']) ? sanitize_text_field(wp_unslash($_POST['action'])) // phpcs:ignore WordPress.Security.NonceVerification.Missing -- read-only action-name prefix check during boot | |
| 160 | + : (isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : ''); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only action-name prefix check during boot | |
| 123 | 161 | if ($action === '') { |
| 124 | 162 | return ($result = true); |
| 125 | 163 | } |
| 126 | 164 | if (strpos($action, 'metasync_') === 0 || strpos($action, 'meta_sync_') === 0 || $action === 'sample-permalink') { |
| @@ -129,18 +167,58 @@ | ||
| 129 | 167 | return ($result = true); |
| 130 | 168 | } |
| 131 | 169 | } |
| 132 | 170 | |
| 171 | +// Lazy-load guard: only initialise the MCP server when the request is actually targeting | |
| 172 | +// the MCP REST route (or its sibling SEO-inventory route, which depends on $metasync_mcp_server | |
| 173 | +// for its permission callback). Saves ~2-5 MB / 10-50 ms on the 99.9% of requests that never | |
| 174 | +// touch MCP. | |
| 175 | +if (!function_exists('metasync_is_mcp_rest_request')) { | |
| 176 | + function metasync_is_mcp_rest_request(): bool { | |
| 177 | + $uri = isset($_SERVER['REQUEST_URI']) ? (string) $_SERVER['REQUEST_URI'] : ''; | |
| 178 | + if ($uri === '') { | |
| 179 | + return false; | |
| 180 | + } | |
| 181 | + $prefix = function_exists('rest_get_url_prefix') ? rest_get_url_prefix() : 'wp-json'; | |
| 182 | + $needles = [ | |
| 183 | + '/' . $prefix . '/metasync/v1/mcp', | |
| 184 | + '/' . $prefix . '/metasync/v1/seo-inventory', | |
| 185 | + 'rest_route=/metasync/v1/mcp', | |
| 186 | + 'rest_route=/metasync/v1/seo-inventory', | |
| 187 | + 'rest_route=%2Fmetasync%2Fv1%2Fmcp', | |
| 188 | + 'rest_route=%2Fmetasync%2Fv1%2Fseo-inventory', | |
| 189 | + ]; | |
| 190 | + foreach ($needles as $needle) { | |
| 191 | + if (stripos($uri, $needle) !== false) { | |
| 192 | + return true; | |
| 193 | + } | |
| 194 | + } | |
| 195 | + return false; | |
| 196 | + } | |
| 197 | +} | |
| 198 | + | |
| 133 | 199 | // Phase 2 — these files have file-level side effects (add_action outside class body) |
| 134 | 200 | // and must remain as explicit require_once until refactored. |
| 135 | 201 | require_once plugin_dir_path( __FILE__ ) . 'includes/class-metasync-api-backoff-rest.php'; |
| 202 | +require_once plugin_dir_path( __FILE__ ) . 'includes/class-metasync-data-store.php'; | |
| 136 | 203 | require_once plugin_dir_path( __FILE__ ) . 'includes/class-metasync-error-logger.php'; |
| 137 | 204 | |
| 205 | +// Shared helpers (custom/LPS page detection + query exclusion) — loaded in all | |
| 206 | +// request contexts (admin, REST, MCP, AJAX) so every SEO surface uses one rule. | |
| 207 | +require_once plugin_dir_path( __FILE__ ) . 'includes/metasync-helpers.php'; | |
| 208 | + | |
| 209 | +// Shared "results per page" selector helper used by the Redirections, 404 | |
| 210 | +// Monitor, Changes Log and Media Library admin list tables. Loaded early so | |
| 211 | +// the helper class is available before any list table instantiates. | |
| 212 | +require_once plugin_dir_path( __FILE__ ) . 'includes/class-metasync-per-page-helper.php'; | |
| 213 | + | |
| 138 | 214 | /** |
| 139 | 215 | * Include the Otto Pixel Php Code |
| 140 | 216 | */ |
| 141 | 217 | if (!metasync_is_non_metasync_admin_ajax()) { |
| 142 | 218 | require_once plugin_dir_path( __FILE__ ) . '/otto/otto_pixel.php'; |
| 219 | + require_once plugin_dir_path( __FILE__ ) . '/otto/class-metasync-otto-clone-meta-cleaner.php'; | |
| 220 | + Metasync_Otto_Clone_Meta_Cleaner::register(); | |
| 143 | 221 | } |
| 144 | 222 | |
| 145 | 223 | |
| 146 | 224 | /** |
| @@ -185,9 +263,9 @@ | ||
| 185 | 263 | #craft the message |
| 186 | 264 | sprintf( |
| 187 | 265 | '%s requires WordPress version %s or later. You are currently using version %s. Please update WordPress to activate this plugin.', |
| 188 | 266 | esc_html($plugin_name), |
| 189 | - METASYNC_MIN_WP, | |
| 267 | + esc_html(METASYNC_MIN_WP), | |
| 190 | 268 | esc_html($wp_version) |
| 191 | 269 | ), |
| 192 | 270 | |
| 193 | 271 | #the plugin title as page title |
| @@ -207,9 +285,9 @@ | ||
| 207 | 285 | #craft the message |
| 208 | 286 | sprintf( |
| 209 | 287 | '%s requires PHP version %s or later. You are currently using version %s. Please update PHP to activate this plugin.', |
| 210 | 288 | esc_html($plugin_name), |
| 211 | - METASYNC_MIN_PHP, | |
| 289 | + esc_html(METASYNC_MIN_PHP), | |
| 212 | 290 | esc_html($php_version) |
| 213 | 291 | ), |
| 214 | 292 | |
| 215 | 293 | #the plugin title as page title |
| @@ -225,11 +303,22 @@ | ||
| 225 | 303 | MetaSync_DBMigration::activation(); |
| 226 | 304 | |
| 227 | 305 | // Set initial version |
| 228 | 306 | update_option('metasync_version', METASYNC_VERSION); |
| 229 | - | |
| 307 | + | |
| 308 | + // Record activation timestamp so Divi CSS fix transients | |
| 309 | + // auto-invalidate after plugin deactivate/reactivate cycles. | |
| 310 | + update_option('metasync_activated_at', (string) time()); | |
| 311 | + | |
| 230 | 312 | // Clear all cache plugins to ensure fresh start |
| 231 | 313 | Metasync_Cache_Purge::purge_all('plugin_activation'); |
| 314 | + | |
| 315 | + // Migrate physical sitemap files on activation. | |
| 316 | + metasync_migrate_physical_sitemaps(); | |
| 317 | + | |
| 318 | + // Consent-gated analytics: the activation event only fires if the user | |
| 319 | + // previously opted in (re-activation) — a brand-new install is OFF. | |
| 320 | + Metasync_PostHog::get_instance()->track('plugin_activated', array()); | |
| 232 | 321 | } |
| 233 | 322 | |
| 234 | 323 | // Log-sync removed - error monitoring now handled by Sentry |
| 235 | 324 | // require_once plugin_dir_path(__FILE__) . 'log-sync/log-sync.php'; |
| @@ -250,18 +339,15 @@ | ||
| 250 | 339 | Metasync_Deactivator::deactivate(); |
| 251 | 340 | // class name is changed at class-db-migrations.php |
| 252 | 341 | MetaSync_DBMigration::deactivation(); |
| 253 | 342 | |
| 254 | - // Unschedule the sync log cleanup cron to avoid orphaned events. | |
| 255 | - $timestamp = wp_next_scheduled('metasync_sync_log_daily_cleanup'); | |
| 256 | - if ($timestamp) { | |
| 257 | - wp_unschedule_event($timestamp, 'metasync_sync_log_daily_cleanup'); | |
| 258 | - } | |
| 259 | - | |
| 260 | 343 | // Clear news/video sitemap caches |
| 261 | 344 | delete_transient('metasync_vsm_' . md5('news-sitemap.xml')); |
| 262 | 345 | delete_transient('metasync_vsm_' . md5('video-sitemap.xml')); |
| 263 | 346 | delete_option('metasync_sitemap_virtual_index'); |
| 347 | + | |
| 348 | + // Clear OTTO JS detection cache so re-activation gets a fresh check | |
| 349 | + delete_transient('metasync_otto_js_detected'); | |
| 264 | 350 | } |
| 265 | 351 | |
| 266 | 352 | register_activation_hook(__FILE__, 'activate_metasync'); |
| 267 | 353 | register_deactivation_hook(__FILE__, 'deactivate_metasync'); |
| @@ -266,8 +352,142 @@ | ||
| 266 | 352 | register_activation_hook(__FILE__, 'activate_metasync'); |
| 267 | 353 | register_deactivation_hook(__FILE__, 'deactivate_metasync'); |
| 268 | 354 | |
| 269 | 355 | /** |
| 356 | + * Migrate physical sitemap .xml files into transients and delete them. | |
| 357 | + * | |
| 358 | + * Physical files in ABSPATH cause nginx/Plesk to 403 before WordPress can | |
| 359 | + * serve them. Called from both the activation hook and the version-gate | |
| 360 | + * migration so it covers fresh activations and auto-updates. | |
| 361 | + */ | |
| 362 | +function metasync_migrate_physical_sitemaps() | |
| 363 | +{ | |
| 364 | + update_option('metasync_sitemap_virtual_mode', true, false); | |
| 365 | + | |
| 366 | + $candidates = []; | |
| 367 | + $globbed = glob(ABSPATH . 'sitemap*.xml'); | |
| 368 | + if (is_array($globbed)) { | |
| 369 | + foreach ($globbed as $file) { | |
| 370 | + $basename = basename($file); | |
| 371 | + if ($basename === 'sitemap_index.xml' || preg_match('/^sitemap\d*\.xml$/', $basename)) { | |
| 372 | + $candidates[] = $file; | |
| 373 | + } | |
| 374 | + } | |
| 375 | + } | |
| 376 | + foreach (['news-sitemap.xml', 'video-sitemap.xml'] as $extra) { | |
| 377 | + $path = ABSPATH . $extra; | |
| 378 | + if (file_exists($path)) { | |
| 379 | + $candidates[] = $path; | |
| 380 | + } | |
| 381 | + } | |
| 382 | + | |
| 383 | + if (empty($candidates)) { | |
| 384 | + return; | |
| 385 | + } | |
| 386 | + | |
| 387 | + // Ownership gate: other plugins (Yoast and friends) write files with the | |
| 388 | + // exact same names (sitemap_index.xml, sitemap1.xml, ...), so a name match | |
| 389 | + // alone must never authorize deleting a physical file. A file is only | |
| 390 | + // migrated when this plugin left evidence it wrote it. | |
| 391 | + $tracked_names = []; | |
| 392 | + $tracked_files = get_option('metasync_sitemap_files', []); | |
| 393 | + if (is_array($tracked_files)) { | |
| 394 | + foreach ($tracked_files as $entry) { | |
| 395 | + $name = is_array($entry) && isset($entry['filename']) ? $entry['filename'] : $entry; | |
| 396 | + if (is_string($name) && '' !== $name) { | |
| 397 | + $tracked_names[] = $name; | |
| 398 | + } | |
| 399 | + } | |
| 400 | + } | |
| 401 | + $virtual_index = get_option('metasync_sitemap_virtual_index', []); | |
| 402 | + if (!is_array($virtual_index)) { | |
| 403 | + $virtual_index = []; | |
| 404 | + } | |
| 405 | + $parked_names = array_keys($virtual_index); | |
| 406 | + $generator_ran = !empty($tracked_names) && false !== get_option('metasync_sitemap_last_generated'); | |
| 407 | + | |
| 408 | + $ours = []; | |
| 409 | + $metasync_files_found = false; | |
| 410 | + foreach ($candidates as $file) { | |
| 411 | + $bn = basename($file); | |
| 412 | + | |
| 413 | + // A parked name proves MetaSync owned the file at an earlier point, but | |
| 414 | + // an expired transient no longer proves that a file currently on disk | |
| 415 | + // is ours. A different plugin may have recreated the same basename. | |
| 416 | + if (in_array($bn, $parked_names, true)) { | |
| 417 | + if (false !== get_transient('metasync_vsm_' . md5($bn))) { | |
| 418 | + continue; | |
| 419 | + } | |
| 420 | + continue; | |
| 421 | + } | |
| 422 | + | |
| 423 | + // Chunk, index, news, and video files are migratable only when their | |
| 424 | + // exact basename is recorded by MetaSync's generator/configuration. | |
| 425 | + if (in_array($bn, $tracked_names, true) && $generator_ran) { | |
| 426 | + $ours[] = $file; | |
| 427 | + $metasync_files_found = true; | |
| 428 | + continue; | |
| 429 | + } | |
| 430 | + | |
| 431 | + if ($bn === 'news-sitemap.xml' && null !== get_option('metasync_news_sitemap_settings', null)) { | |
| 432 | + $ours[] = $file; | |
| 433 | + $metasync_files_found = true; | |
| 434 | + continue; | |
| 435 | + } | |
| 436 | + if ($bn === 'video-sitemap.xml' && null !== get_option('metasync_video_sitemap_settings', null)) { | |
| 437 | + $ours[] = $file; | |
| 438 | + $metasync_files_found = true; | |
| 439 | + } | |
| 440 | + } | |
| 441 | + | |
| 442 | + // Do not infer ownership of sitemap_index.xml from another sitemap: Yoast, | |
| 443 | + // Rank Math, and other SEO plugins commonly use the same root filename. | |
| 444 | + // It must be explicitly recorded in MetaSync's tracked file list. | |
| 445 | + $ours = array_values(array_unique($ours)); | |
| 446 | + | |
| 447 | + if (empty($ours)) { | |
| 448 | + return; | |
| 449 | + } | |
| 450 | + | |
| 451 | + $migrated_files = []; | |
| 452 | + | |
| 453 | + foreach ($ours as $file) { | |
| 454 | + $bn = basename($file); | |
| 455 | + $content = @file_get_contents($file); | |
| 456 | + if (false !== $content) { | |
| 457 | + $tkey = 'metasync_vsm_' . md5($bn); | |
| 458 | + // The news sitemap's entries are only valid inside Google News' | |
| 459 | + // 48-hour window, so it must not be migrated in under a 30-day | |
| 460 | + // TTL — that would re-introduce the staleness the generator's | |
| 461 | + // own bounded TTL exists to prevent. Regenerate-on-miss rebuilds | |
| 462 | + // it with a fresh date_query when this expires. | |
| 463 | + $ttl = ('news-sitemap.xml' === $bn) ? (int) (DAY_IN_SECONDS / 4) : 30 * DAY_IN_SECONDS; | |
| 464 | + set_transient($tkey, $content, $ttl); | |
| 465 | + if (false !== get_transient($tkey)) { | |
| 466 | + // The parked transient doubles as a 30-day backup of the | |
| 467 | + // content before the file is removed from disk. | |
| 468 | + @wp_delete_file($file); | |
| 469 | + $virtual_index[$bn] = $tkey; | |
| 470 | + if ($bn !== 'sitemap_index.xml' && $bn !== 'news-sitemap.xml' && $bn !== 'video-sitemap.xml') { | |
| 471 | + $migrated_files[] = [ | |
| 472 | + 'filename' => $bn, | |
| 473 | + 'url' => home_url('/' . $bn), | |
| 474 | + 'lastmod' => current_time('mysql', true), | |
| 475 | + ]; | |
| 476 | + } | |
| 477 | + } | |
| 478 | + } | |
| 479 | + } | |
| 480 | + | |
| 481 | + update_option('metasync_sitemap_virtual_index', $virtual_index, false); | |
| 482 | + | |
| 483 | + if (!empty($migrated_files)) { | |
| 484 | + update_option('metasync_sitemap_files', $migrated_files); | |
| 485 | + update_option('metasync_sitemap_last_generated', current_time('mysql')); | |
| 486 | + } | |
| 487 | +} | |
| 488 | + | |
| 489 | +/** | |
| 270 | 490 | * Check for plugin updates and run migrations if needed |
| 271 | 491 | */ |
| 272 | 492 | function check_metasync_updates() |
| 273 | 493 | { |
| @@ -277,10 +497,17 @@ | ||
| 277 | 497 | |
| 278 | 498 | $current_version = get_option('metasync_version', '0.0.0'); |
| 279 | 499 | $plugin_version = METASYNC_VERSION; |
| 280 | 500 | |
| 281 | - // If versions don't match, run migration | |
| 282 | - if (version_compare($current_version, $plugin_version, '<')) { | |
| 501 | + // If versions don't match, run migration. Dev builds store the 9.9.9 | |
| 502 | + // placeholder version, which compares greater than every real release — | |
| 503 | + // once stored, a plain "<" comparison never fires again and migrations | |
| 504 | + // are silently skipped forever. Also enter when the stored version is the | |
| 505 | + // placeholder but the running version is a real release, so the migration | |
| 506 | + // runner's dev-build escape hatch becomes reachable and the stored version | |
| 507 | + // is corrected to the real release. | |
| 508 | + if (version_compare($current_version, $plugin_version, '<') | |
| 509 | + || ('9.9.9' === $current_version && '9.9.9' !== $plugin_version)) { | |
| 283 | 510 | // Import whitelabel settings only if the JSON file is new or changed |
| 284 | 511 | // (prevents overwriting admin UI changes on every version check) |
| 285 | 512 | Metasync_Activator::check_whitelabel_settings_update(); |
| 286 | 513 | |
| @@ -286,8 +513,13 @@ | ||
| 286 | 513 | |
| 287 | 514 | // Run version-specific migrations first |
| 288 | 515 | MetaSync_DBMigration::run_version_migrations($current_version, $plugin_version); |
| 289 | 516 | |
| 517 | + // The developer endpoint-switching panel has been removed. Reset its | |
| 518 | + // persisted staging mode so upgrades cannot keep routing API requests | |
| 519 | + // to staging without a visible warning or a supported recovery path. | |
| 520 | + delete_option('metasync_dev_endpoint_mode'); | |
| 521 | + | |
| 290 | 522 | // Migration for v2.7.0+: Remove AI Agent, switch to plugin auth token, make MCP always-on |
| 291 | 523 | if (version_compare($current_version, '2.7.0', '<')) { |
| 292 | 524 | // Remove old MCP API key option |
| 293 | 525 | delete_option('metasync_mcp_api_key'); |
| @@ -307,8 +539,52 @@ | ||
| 307 | 539 | update_option('metasync_options', $options); |
| 308 | 540 | } |
| 309 | 541 | } |
| 310 | 542 | |
| 543 | + // One-time purge of stale OTTO SEO cron backlog. | |
| 544 | + // Prior versions could accumulate thousands of metasync_process_seo_job and | |
| 545 | + // metasync_process_otto_crawl_url_job events due to unbounded rescheduling. | |
| 546 | + // Clear the backlog once on update; the new code prevents re-accumulation. | |
| 547 | + if (!get_option('metasync_wp299_cron_cleanup_done')) { | |
| 548 | + wp_unschedule_hook('metasync_process_seo_job'); | |
| 549 | + wp_unschedule_hook('metasync_process_otto_crawl_url_job'); | |
| 550 | + wp_unschedule_hook('metasync_process_otto_batch_cache_job'); | |
| 551 | + update_option('metasync_wp299_cron_cleanup_done', true, false); | |
| 552 | + } | |
| 553 | + | |
| 554 | + // One-time cleanup of canonical values corrupted to the literal | |
| 555 | + // "Array" (emitted as http://Array once the 2.6.16 canonical filters | |
| 556 | + // started reading them). The sanitizer prevents new corruption; this | |
| 557 | + // repairs the rows already in the database. Cache purge below pushes | |
| 558 | + // the clean pages live. Claimed via add_option() — it fails if the row | |
| 559 | + // already exists, so concurrent requests can't run the cleanup twice, | |
| 560 | + // and the claim lands BEFORE the work: everything inside is idempotent | |
| 561 | + // and the read-side sanitizer already protects output if a run is | |
| 562 | + // interrupted. | |
| 563 | + if (false === get_option('metasync_canonical_cleanup_done') | |
| 564 | + && add_option('metasync_canonical_cleanup_done', 'running', '', false)) { | |
| 565 | + MetaSync_DBMigration::cleanup_corrupted_canonicals(); | |
| 566 | + update_option('metasync_canonical_cleanup_done', 'done', false); | |
| 567 | + } | |
| 568 | + | |
| 569 | + // One-time repair of Local Business logos corrupted to "http://<id>". | |
| 570 | + // The sanitizer fix stopped new corruption; this restores the | |
| 571 | + // attachment ID on sites that saved a logo before it. The corrupted | |
| 572 | + // value encodes the original ID exactly, so the rewrite is lossless. | |
| 573 | + // Claimed via add_option() — it fails when the row already exists, so | |
| 574 | + // concurrent requests can't run the repair twice, and the claim lands | |
| 575 | + // BEFORE the work: the repair itself is idempotent, and the read-side | |
| 576 | + // normalisation in the schema output and admin preview already | |
| 577 | + // protects output if a run is interrupted. | |
| 578 | + if (false === get_option('metasync_local_seo_logo_repair_done') | |
| 579 | + && add_option('metasync_local_seo_logo_repair_done', 'running', '', false)) { | |
| 580 | + MetaSync_DBMigration::repair_corrupted_local_seo_logo(); | |
| 581 | + update_option('metasync_local_seo_logo_repair_done', 'done', false); | |
| 582 | + } | |
| 583 | + | |
| 584 | + // Migrate physical sitemap files on version update. | |
| 585 | + metasync_migrate_physical_sitemaps(); | |
| 586 | + | |
| 311 | 587 | // Run full migration to ensure all tables are up to date |
| 312 | 588 | MetaSync_DBMigration::run_migrations(); |
| 313 | 589 | |
| 314 | 590 | // Update stored version |
| @@ -338,8 +614,23 @@ | ||
| 338 | 614 | if (!isset($hook_extra['type']) || $hook_extra['type'] !== 'plugin') { |
| 339 | 615 | return; |
| 340 | 616 | } |
| 341 | 617 | |
| 618 | + // By the time upgrader_process_complete fires, the upgrader may have | |
| 619 | + // deleted the directory this (old, still-in-memory) copy of the plugin was | |
| 620 | + // loaded from — e.g. when the installed dir name differs from the package's | |
| 621 | + // root dir ('metasync-develop' vs 'metasync'). The Composer classmap then | |
| 622 | + // points at files that no longer exist, so autoloading Metasync_Activator | |
| 623 | + // below would fatal. Bail instead; the whitelabel re-import runs on the next | |
| 624 | + // request via check_metasync_updates() once the new copy is active. | |
| 625 | + if (!class_exists('Metasync_Activator', false)) { | |
| 626 | + $activator = __DIR__ . '/includes/class-metasync-activator.php'; | |
| 627 | + if (!is_file($activator)) { | |
| 628 | + return; | |
| 629 | + } | |
| 630 | + require_once $activator; | |
| 631 | + } | |
| 632 | + | |
| 342 | 633 | // Only process install and update actions |
| 343 | 634 | if (!isset($hook_extra['action']) || !in_array($hook_extra['action'], ['install', 'update'], true)) { |
| 344 | 635 | return; |
| 345 | 636 | } |
| @@ -391,9 +682,9 @@ | ||
| 391 | 682 | } |
| 392 | 683 | } |
| 393 | 684 | |
| 394 | 685 | if ($should_import) { |
| 395 | - Metasync_Activator::check_whitelabel_settings_update(); | |
| 686 | + Metasync_Activator::check_whitelabel_settings_update(true); | |
| 396 | 687 | } |
| 397 | 688 | } |
| 398 | 689 | |
| 399 | 690 | // Hook into WordPress upgrader to detect plugin updates |
| @@ -402,26 +693,31 @@ | ||
| 402 | 693 | /** |
| 403 | 694 | * Fallback: Check for whitelabel file changes on admin pages |
| 404 | 695 | * This handles edge cases where upgrader_process_complete doesn't fire |
| 405 | 696 | * (e.g., FTP uploads, manual file replacements) |
| 406 | - * Only checks once per admin session to minimize performance impact | |
| 697 | + * Only checks once per admin request to minimize performance impact | |
| 407 | 698 | */ |
| 408 | -// function metasync_check_whitelabel_on_admin() | |
| 409 | -// { | |
| 410 | -// // Only check once per admin session to avoid overhead | |
| 411 | -// static $checked = false; | |
| 412 | -// if ($checked) { | |
| 413 | -// return; | |
| 414 | -// } | |
| 415 | -// $checked = true; | |
| 699 | +function metasync_check_whitelabel_on_admin() | |
| 700 | +{ | |
| 701 | + static $checked = false; | |
| 702 | + if ($checked || !current_user_can('manage_options')) { | |
| 703 | + return; | |
| 704 | + } | |
| 705 | + $checked = true; | |
| 416 | 706 | |
| 417 | -// require_once plugin_dir_path(__FILE__) . 'includes/class-metasync-activator.php'; | |
| 418 | -// Metasync_Activator::check_whitelabel_settings_update(); | |
| 419 | -// } | |
| 707 | + require_once plugin_dir_path(__FILE__) . 'includes/class-metasync-activator.php'; | |
| 420 | 708 | |
| 421 | -// Check for whitelabel changes on admin pages (fallback for edge cases) | |
| 422 | -// add_action('admin_init', 'metasync_check_whitelabel_on_admin', 1); | |
| 709 | + // Surface a persistently failing White Label import instead of | |
| 710 | + // retrying it silently on every admin request. | |
| 711 | + add_action('admin_notices', array('Metasync_Activator', 'render_whitelabel_import_failure_notice')); | |
| 423 | 712 | |
| 713 | + Metasync_Activator::check_whitelabel_settings_update(); | |
| 714 | +} | |
| 715 | + | |
| 716 | +// Retry package imports on an authorized admin request when an update completed | |
| 717 | +// through the public version-check or stale-classmap fallback path. | |
| 718 | +add_action('admin_init', 'metasync_check_whitelabel_on_admin', 1); | |
| 719 | + | |
| 424 | 720 | // Include Media Optimization Module |
| 425 | 721 | if (!metasync_is_non_metasync_admin_ajax()) { |
| 426 | 722 | require_once plugin_dir_path(__FILE__) . 'media-optimization/media-optimization-loader.php'; |
| 427 | 723 | } |
| @@ -438,269 +734,11 @@ | ||
| 438 | 734 | $plugin->run(); |
| 439 | 735 | } |
| 440 | 736 | run_metasync(); |
| 441 | 737 | |
| 442 | -/** | |
| 443 | - * Initialize WordPress MCP Server | |
| 444 | - * | |
| 445 | - * Creates and configures the MCP server instance, | |
| 446 | - * registers all available MCP tools for WordPress operations. | |
| 447 | - * Hooked to 'init' for proper WordPress lifecycle integration. | |
| 448 | - */ | |
| 449 | -function metasync_init_mcp_server() { | |
| 450 | - if (metasync_is_non_metasync_admin_ajax()) { | |
| 451 | - return; | |
| 452 | - } | |
| 453 | - // Initialize MCP server | |
| 454 | - global $metasync_mcp_server; | |
| 738 | +// MCP server bootstrap (server + tool registration) extracted to keep this entry file lean. | |
| 739 | +require_once plugin_dir_path( __FILE__ ) . 'includes/mcp-server-bootstrap.php'; | |
| 455 | 740 | |
| 456 | - try { | |
| 457 | - $metasync_mcp_server = new Metasync_MCP_Server(); | |
| 458 | - | |
| 459 | - // Attach MCP sync logger so all write tool calls are recorded in Sync History. | |
| 460 | - new Metasync_MCP_Sync_Logger(); | |
| 461 | - | |
| 462 | - // SEO Inventory: shared builder + standalone REST endpoint (WP-135) | |
| 463 | - new Metasync_REST_SEO_Inventory(); | |
| 464 | - } catch (Exception $e) { | |
| 465 | - error_log('MCP Server Initialization Error: ' . $e->getMessage()); | |
| 466 | - return; | |
| 467 | - } | |
| 468 | - | |
| 469 | - // Helper: register a single tool, logging failures without aborting subsequent registrations | |
| 470 | - $safe_register = function($tool) use ($metasync_mcp_server) { | |
| 471 | - try { | |
| 472 | - $metasync_mcp_server->register_tool($tool); | |
| 473 | - } catch (Exception $e) { | |
| 474 | - error_log('MetaSync MCP: Failed to register tool ' . get_class($tool) . ': ' . $e->getMessage()); | |
| 475 | - } | |
| 476 | - }; | |
| 477 | - | |
| 478 | - // Register MCP Tools (Total: 92 existing + 8 new = 100 tools total!) | |
| 479 | - // NEW in v2.8.0: +4 Taxonomy Meta tools, +4 Bulk Alt Text tools | |
| 480 | - | |
| 481 | - // Post Meta Operations (4 tools) | |
| 482 | - $safe_register(new MCP_Tool_Update_Post_Meta()); | |
| 483 | - $safe_register(new MCP_Tool_Get_Post_Meta()); | |
| 484 | - $safe_register(new MCP_Tool_Get_SEO_Meta()); | |
| 485 | - $safe_register(new MCP_Tool_Get_Hreflang_Links()); | |
| 486 | - | |
| 487 | - // Post Operations (4 tools) | |
| 488 | - $safe_register(new MCP_Tool_Get_Post()); | |
| 489 | - $safe_register(new MCP_Tool_Get_Post_By_URL()); | |
| 490 | - $safe_register(new MCP_Tool_List_Posts()); | |
| 491 | - $safe_register(new MCP_Tool_Update_Post()); | |
| 492 | - $safe_register(new MCP_Tool_Get_Post_Types()); | |
| 493 | - | |
| 494 | - // SEO Analysis (3 tools) | |
| 495 | - $safe_register(new MCP_Tool_Analyze_SEO()); | |
| 496 | - $safe_register(new MCP_Tool_Check_Indexability()); | |
| 497 | - $safe_register(new MCP_Tool_SEO_Health_Report()); | |
| 498 | - | |
| 499 | - // Search Operations (3 tools) | |
| 500 | - $safe_register(new MCP_Tool_Search_Posts()); | |
| 501 | - $safe_register(new MCP_Tool_Search_By_Keyword()); | |
| 502 | - $safe_register(new MCP_Tool_Find_Missing_Meta()); | |
| 503 | - | |
| 504 | - // Redirect Management (5 tools) | |
| 505 | - $safe_register(new MCP_Tool_Create_Redirect()); | |
| 506 | - $safe_register(new MCP_Tool_List_Redirects()); | |
| 507 | - $safe_register(new MCP_Tool_Delete_Redirect()); | |
| 508 | - $safe_register(new MCP_Tool_Update_Redirect()); | |
| 509 | - $safe_register(new MCP_Tool_Check_Redirects_Health()); | |
| 510 | - | |
| 511 | - // 404 Error Monitoring (5 tools) | |
| 512 | - $safe_register(new MCP_Tool_List_404_Errors()); | |
| 513 | - $safe_register(new MCP_Tool_Get_404_Stats()); | |
| 514 | - $safe_register(new MCP_Tool_Delete_404_Error()); | |
| 515 | - $safe_register(new MCP_Tool_Clear_404_Errors()); | |
| 516 | - $safe_register(new MCP_Tool_Create_Redirect_From_404()); | |
| 517 | - | |
| 518 | - // Robots.txt & Sitemap Management (11 tools - 7 existing + 4 new) | |
| 519 | - $safe_register(new MCP_Tool_Get_Robots_Txt()); | |
| 520 | - $safe_register(new MCP_Tool_Update_Robots_Txt()); | |
| 521 | - $safe_register(new MCP_Tool_Get_Sitemap_Status()); | |
| 522 | - $safe_register(new MCP_Tool_Regenerate_Sitemap()); | |
| 523 | - $safe_register(new MCP_Tool_Exclude_From_Sitemap()); | |
| 524 | - $safe_register(new MCP_Tool_Add_Robots_Rule()); | |
| 525 | - $safe_register(new MCP_Tool_Remove_Robots_Rule()); | |
| 526 | - $safe_register(new MCP_Tool_Parse_Robots_Txt()); | |
| 527 | - $safe_register(new MCP_Tool_Validate_Robots_Txt()); | |
| 528 | - $safe_register(new MCP_Tool_Get_News_Sitemap()); | |
| 529 | - $safe_register(new MCP_Tool_Get_Video_Sitemap()); | |
| 530 | - | |
| 531 | - // Plugin Settings Management (4 tools) | |
| 532 | - $safe_register(new MCP_Tool_Get_Plugin_Settings()); | |
| 533 | - $safe_register(new MCP_Tool_Update_Plugin_Settings()); | |
| 534 | - $safe_register(new MCP_Tool_List_Plugin_Settings_Schema()); | |
| 535 | - $safe_register(new MCP_Tool_Get_MCP_Settings()); | |
| 536 | - | |
| 537 | - // Schema Markup Management (7 tools) | |
| 538 | - $safe_register(new MCP_Tool_Get_Schema_Markup()); | |
| 539 | - $safe_register(new MCP_Tool_Update_Schema_Markup()); | |
| 540 | - $safe_register(new MCP_Tool_Add_Schema_Type()); | |
| 541 | - $safe_register(new MCP_Tool_Remove_Schema_Type()); | |
| 542 | - $safe_register(new MCP_Tool_Validate_Schema()); | |
| 543 | - $safe_register(new MCP_Tool_Get_Schema_Content()); | |
| 544 | - $safe_register(new MCP_Tool_Set_Schema_Content()); | |
| 545 | - | |
| 546 | - // Google Instant Index (6 tools) | |
| 547 | - $safe_register(new MCP_Tool_Instant_Index_Update()); | |
| 548 | - $safe_register(new MCP_Tool_Instant_Index_Delete()); | |
| 549 | - $safe_register(new MCP_Tool_Instant_Index_Status()); | |
| 550 | - $safe_register(new MCP_Tool_Instant_Index_Bulk_Update()); | |
| 551 | - $safe_register(new MCP_Tool_Get_Instant_Index_Settings()); | |
| 552 | - $safe_register(new MCP_Tool_Update_Instant_Index_Settings()); | |
| 553 | - | |
| 554 | - // Custom HTML Pages (5 tools) | |
| 555 | - $safe_register(new MCP_Tool_Create_Custom_Page()); | |
| 556 | - $safe_register(new MCP_Tool_Get_Custom_Page()); | |
| 557 | - $safe_register(new MCP_Tool_List_Custom_Pages()); | |
| 558 | - $safe_register(new MCP_Tool_Update_Custom_Page()); | |
| 559 | - $safe_register(new MCP_Tool_Delete_Custom_Page()); | |
| 560 | - | |
| 561 | - // HTML to Builder Converter (3 tools) | |
| 562 | - $safe_register(new MCP_Tool_Convert_HTML_To_Builder()); | |
| 563 | - $safe_register(new MCP_Tool_Create_Builder_Page_From_HTML()); | |
| 564 | - $safe_register(new MCP_Tool_Convert_Custom_Page_To_Builder()); | |
| 565 | - | |
| 566 | - // Code Snippets (6 tools) | |
| 567 | - $safe_register(new MCP_Tool_Get_Header_Snippet()); | |
| 568 | - $safe_register(new MCP_Tool_Update_Header_Snippet()); | |
| 569 | - $safe_register(new MCP_Tool_Get_Footer_Snippet()); | |
| 570 | - $safe_register(new MCP_Tool_Update_Footer_Snippet()); | |
| 571 | - $safe_register(new MCP_Tool_Get_Post_Snippets()); | |
| 572 | - $safe_register(new MCP_Tool_Update_Post_Snippets()); | |
| 573 | - | |
| 574 | - // Categories & Taxonomies (15 tools) | |
| 575 | - $safe_register(new MCP_Tool_List_Categories()); | |
| 576 | - $safe_register(new MCP_Tool_Get_Category()); | |
| 577 | - $safe_register(new MCP_Tool_Create_Category()); | |
| 578 | - $safe_register(new MCP_Tool_Update_Category()); | |
| 579 | - $safe_register(new MCP_Tool_Delete_Category()); | |
| 580 | - $safe_register(new MCP_Tool_Get_Post_Categories()); | |
| 581 | - $safe_register(new MCP_Tool_Set_Post_Categories()); | |
| 582 | - | |
| 583 | - // Tags (8 tools) | |
| 584 | - $safe_register(new MCP_Tool_List_Tags()); | |
| 585 | - $safe_register(new MCP_Tool_Get_Tag()); | |
| 586 | - $safe_register(new MCP_Tool_Create_Tag()); | |
| 587 | - $safe_register(new MCP_Tool_Update_Tag()); | |
| 588 | - $safe_register(new MCP_Tool_Delete_Tag()); | |
| 589 | - $safe_register(new MCP_Tool_Get_Post_Tags()); | |
| 590 | - $safe_register(new MCP_Tool_Set_Post_Tags()); | |
| 591 | - | |
| 592 | - // Featured Images & Media (6 tools) | |
| 593 | - $safe_register(new MCP_Tool_Get_Featured_Image()); | |
| 594 | - $safe_register(new MCP_Tool_Set_Featured_Image()); | |
| 595 | - $safe_register(new MCP_Tool_Upload_Featured_Image()); | |
| 596 | - $safe_register(new MCP_Tool_Remove_Featured_Image()); | |
| 597 | - $safe_register(new MCP_Tool_List_Media()); | |
| 598 | - $safe_register(new MCP_Tool_Get_Media_Details()); | |
| 599 | - | |
| 600 | - // Post CRUD Operations (1 tool - delete/restore disabled for safety) | |
| 601 | - $safe_register(new MCP_Tool_Create_Post()); | |
| 602 | - // $safe_register(new MCP_Tool_Delete_Post()); // DISABLED - safety | |
| 603 | - // $safe_register(new MCP_Tool_Restore_Post()); // DISABLED - safety | |
| 604 | - | |
| 605 | - // Bulk Operations (3 tools - bulk delete disabled for safety) | |
| 606 | - $safe_register(new MCP_Tool_Bulk_Update_Meta()); | |
| 607 | - $safe_register(new MCP_Tool_Bulk_Set_Categories()); | |
| 608 | - $safe_register(new MCP_Tool_Bulk_Change_Status()); | |
| 609 | - // $safe_register(new MCP_Tool_Bulk_Delete_Posts()); // DISABLED - safety | |
| 610 | - | |
| 611 | - // WordPress Core SEO Settings (10 tools) | |
| 612 | - $safe_register(new MCP_Tool_Get_Site_Info()); | |
| 613 | - $safe_register(new MCP_Tool_Update_Site_Info()); | |
| 614 | - $safe_register(new MCP_Tool_Get_Permalink_Structure()); | |
| 615 | - $safe_register(new MCP_Tool_Update_Permalink_Structure()); | |
| 616 | - $safe_register(new MCP_Tool_Get_Reading_Settings()); | |
| 617 | - $safe_register(new MCP_Tool_Update_Reading_Settings()); | |
| 618 | - $safe_register(new MCP_Tool_Get_Search_Visibility()); | |
| 619 | - $safe_register(new MCP_Tool_Update_Search_Visibility()); | |
| 620 | - $safe_register(new MCP_Tool_Get_Date_Format()); | |
| 621 | - $safe_register(new MCP_Tool_Get_Discussion_Settings()); | |
| 622 | - | |
| 623 | - // Taxonomy Meta Operations (4 tools - NEW in v2.8.0) | |
| 624 | - $safe_register(new MCP_Tool_Get_Term_Meta()); | |
| 625 | - $safe_register(new MCP_Tool_Update_Term_Meta()); | |
| 626 | - $safe_register(new MCP_Tool_Bulk_Update_Term_Meta()); | |
| 627 | - $safe_register(new MCP_Tool_List_Terms_With_Meta()); | |
| 628 | - | |
| 629 | - // Bulk Alt Text Operations (4 tools - NEW in v2.8.0) | |
| 630 | - $safe_register(new MCP_Tool_Audit_Alt_Text()); | |
| 631 | - $safe_register(new MCP_Tool_Bulk_Update_Alt_Text()); | |
| 632 | - $safe_register(new MCP_Tool_Generate_Alt_Text()); | |
| 633 | - $safe_register(new MCP_Tool_Validate_Alt_Text()); | |
| 634 | - | |
| 635 | - // OTTO Persistence Settings (2 tools) | |
| 636 | - if (class_exists('MCP_Tool_Get_Otto_Persistence_Settings')) { | |
| 637 | - $safe_register(new MCP_Tool_Get_Otto_Persistence_Settings()); | |
| 638 | - } | |
| 639 | - if (class_exists('MCP_Tool_Update_Otto_Persistence_Settings')) { | |
| 640 | - $safe_register(new MCP_Tool_Update_Otto_Persistence_Settings()); | |
| 641 | - } | |
| 642 | - | |
| 643 | - // OTTO Pipeline Tools (3 tools) | |
| 644 | - if (class_exists('MCP_Tool_Trigger_Otto_Optimization')) { | |
| 645 | - $safe_register(new MCP_Tool_Trigger_Otto_Optimization()); | |
| 646 | - } | |
| 647 | - if (class_exists('MCP_Tool_Get_Otto_Status')) { | |
| 648 | - $safe_register(new MCP_Tool_Get_Otto_Status()); | |
| 649 | - } | |
| 650 | - if (class_exists('MCP_Tool_Verify_SEO_Output')) { | |
| 651 | - $safe_register(new MCP_Tool_Verify_SEO_Output()); | |
| 652 | - } | |
| 653 | - | |
| 654 | - // System Diagnostics & Plugin Info (4 tools) | |
| 655 | - $safe_register(new MCP_Tool_System_Diagnostics()); | |
| 656 | - $safe_register(new MCP_Tool_List_All_Plugins()); | |
| 657 | - $safe_register(new MCP_Tool_Get_Cron_Jobs()); | |
| 658 | - $safe_register(new MCP_Tool_Get_WP_Option()); | |
| 659 | - | |
| 660 | - // SEO Inventory (1 tool — WP-135) | |
| 661 | - $safe_register(new MCP_Tool_List_Posts_SEO_Inventory()); | |
| 662 | - | |
| 663 | - // Read-Only Database Access (3 tools) | |
| 664 | - $safe_register(new MCP_Tool_DB_Tables()); | |
| 665 | - $safe_register(new MCP_Tool_DB_Describe()); | |
| 666 | - $safe_register(new MCP_Tool_DB_Select()); | |
| 667 | - | |
| 668 | - // Breadcrumb Tools (1 tool) | |
| 669 | - $safe_register(new MCP_Tool_Get_Breadcrumb_Path()); | |
| 670 | - | |
| 671 | - // Cache Purge (2 tools) | |
| 672 | - if (class_exists('MCP_Tool_Cache_Purge_All')) { | |
| 673 | - $safe_register(new MCP_Tool_Cache_Purge_All()); | |
| 674 | - } else { | |
| 675 | - error_log('MetaSync MCP: MCP_Tool_Cache_Purge_All class not found — skipping registration. Run composer dump-autoload to regenerate the classmap.'); | |
| 676 | - } | |
| 677 | - if (class_exists('MCP_Tool_Cache_Purge_URL')) { | |
| 678 | - $safe_register(new MCP_Tool_Cache_Purge_URL()); | |
| 679 | - } else { | |
| 680 | - error_log('MetaSync MCP: MCP_Tool_Cache_Purge_URL class not found — skipping registration. Run composer dump-autoload to regenerate the classmap.'); | |
| 681 | - } | |
| 682 | - | |
| 683 | - // LLMs.txt Tools (5 tools) | |
| 684 | - $safe_register(new MCP_Tool_Get_LLMs_Txt()); | |
| 685 | - $safe_register(new MCP_Tool_Regenerate_LLMs_Txt()); | |
| 686 | - $safe_register(new MCP_Tool_Get_LLMs_Txt_Settings()); | |
| 687 | - $safe_register(new MCP_Tool_Update_LLMs_Txt_Settings()); | |
| 688 | - $safe_register(new MCP_Tool_Get_Post_Markdown()); | |
| 689 | - | |
| 690 | - // SEO Plugin Audit (4 tools — WP-202) | |
| 691 | - $safe_register(new MCP_Tool_Read_SEO_Plugin_Data()); | |
| 692 | - $safe_register(new MCP_Tool_SEO_Plugin_Diff()); | |
| 693 | - if (class_exists('Metasync_Plugin_Sync')) { | |
| 694 | - $safe_register(new MCP_Tool_Sync_To_Active_Plugins()); | |
| 695 | - } | |
| 696 | - $safe_register(new MCP_Tool_Detect_SEO_Conflicts()); | |
| 697 | - | |
| 698 | - // Allow other plugins/themes to register tools | |
| 699 | - do_action('metasync_mcp_register_tools', $metasync_mcp_server); | |
| 700 | -} | |
| 701 | -add_action('init', 'metasync_init_mcp_server', 5); | |
| 702 | - | |
| 703 | 741 | /** |
| 704 | 742 | * Schedule a daily cron event to auto-purge Sync History records older than 90 days. |
| 705 | 743 | */ |
| 706 | 744 | function metasync_schedule_sync_log_cleanup() { |
| @@ -728,85 +766,49 @@ | ||
| 728 | 766 | echo '<script>window.__SA_DYO_INITIALIZED__=true;</script>' . "\n"; |
| 729 | 767 | } |
| 730 | 768 | add_action('wp_head', 'metasync_output_dyo_init_flag', 1); |
| 731 | 769 | |
| 732 | -/** | |
| 733 | - * Initialize GA4 Analytics for Admin Area | |
| 734 | - * Hooked to admin_init for proper WordPress lifecycle integration | |
| 735 | - * Only loads after WordPress, plugins, and themes are fully loaded | |
| 736 | - */ | |
| 737 | -function metasync_init_analytics() { | |
| 738 | - // Only initialize in admin area (excludes AJAX and REST API requests) | |
| 739 | - if (is_admin() && !wp_doing_ajax() && !defined('REST_REQUEST')) { | |
| 740 | - Metasync_GA4::get_instance(); | |
| 741 | - } | |
| 742 | -} | |
| 743 | -add_action('admin_init', 'metasync_init_analytics', 10); | |
| 770 | +// Shared analytics helper is loaded in all request contexts so server-side feature | |
| 771 | +// events can use the same consent gate as admin analytics. | |
| 772 | +require_once plugin_dir_path( __FILE__ ) . 'includes/class-metasync-posthog.php'; | |
| 773 | +require_once plugin_dir_path( __FILE__ ) . 'includes/class-metasync-analytics-consent-notice.php'; | |
| 744 | 774 | |
| 745 | 775 | /** |
| 746 | - * Initialize API Backoff System | |
| 747 | - * Hooked to init for proper WordPress lifecycle integration | |
| 748 | - * Monitors API responses and manages exponential backoff for rate limiting | |
| 749 | - * @since 2.7.1 | |
| 776 | + * Runtime feature initialisers (GA4, API backoff, review notice, JWT accessor, | |
| 777 | + * debug mode) extracted to keep this entry file lean. | |
| 750 | 778 | */ |
| 751 | -function metasync_init_api_backoff() { | |
| 752 | - if (metasync_is_non_metasync_admin_ajax()) { | |
| 753 | - return; | |
| 754 | - } | |
| 755 | - // Initialize backoff manager (registers HTTP response filters) | |
| 756 | - Metasync_API_Backoff_Manager::get_instance(); | |
| 779 | +require_once plugin_dir_path( __FILE__ ) . 'includes/metasync-runtime-init.php'; | |
| 757 | 780 | |
| 758 | - // Initialize admin notices (only in admin area) | |
| 759 | - if (is_admin()) { | |
| 760 | - Metasync_API_Backoff_Notices::get_instance(); | |
| 761 | - } | |
| 762 | -} | |
| 763 | -add_action('init', 'metasync_init_api_backoff', 5); | |
| 764 | - | |
| 765 | 781 | /** |
| 766 | - * Initialize Review Notice | |
| 767 | - * Shows a dismissible notice asking users to rate the plugin after a usage period | |
| 768 | - * @since 2.8.0 | |
| 782 | + * Append a "Website Studio" post state to LPS-synced / MetaSync custom pages in | |
| 783 | + * the admin Pages list, so site owners can tell at a glance which pages are | |
| 784 | + * managed by Website Studio and shouldn't be hand-edited. | |
| 785 | + * | |
| 786 | + * Hooks WordPress core's display_post_states filter — the same mechanism that | |
| 787 | + * renders the grey inline tags like "— Front Page" / "— Draft" — so the label | |
| 788 | + * is native-styled and only appears next to relevant page titles, with no | |
| 789 | + * custom admin column. | |
| 790 | + * | |
| 791 | + * @param string[] $post_states Existing post-state labels keyed by slug. | |
| 792 | + * @param WP_Post $post The post being listed. | |
| 793 | + * @return string[] Possibly-augmented post states. | |
| 769 | 794 | */ |
| 770 | -function metasync_init_review_notice() { | |
| 771 | - if (metasync_is_non_metasync_admin_ajax()) { | |
| 772 | - return; | |
| 795 | +function metasync_add_lps_post_state($post_states, $post) { | |
| 796 | + // metasync_is_custom_or_lps_page() lives in otto/otto_pixel.php, which is NOT | |
| 797 | + // loaded on non-MetaSync admin-ajax requests (e.g. Quick Edit's inline-save, | |
| 798 | + // where this filter still fires), so guard against the undefined function. | |
| 799 | + if (!function_exists('metasync_is_custom_or_lps_page')) { | |
| 800 | + return $post_states; | |
| 773 | 801 | } |
| 774 | - if (is_admin()) { | |
| 775 | - Metasync_Review_Notice::get_instance(); | |
| 802 | + if (!metasync_is_custom_or_lps_page($post->ID)) { | |
| 803 | + return $post_states; | |
| 776 | 804 | } |
| 805 | + $post_states['metasync_website_studio'] = __('Website Studio', 'metasync'); | |
| 806 | + return $post_states; | |
| 777 | 807 | } |
| 778 | -add_action('init', 'metasync_init_review_notice'); | |
| 779 | 808 | |
| 780 | -/** | |
| 781 | - * Global convenience function to get active JWT token | |
| 782 | - * Can be called from anywhere in WordPress (themes, other plugins, etc.) | |
| 783 | - * | |
| 784 | - * @param bool $force_refresh Force generation of new token even if cached one exists | |
| 785 | - * @return string|false JWT token on success, false on failure | |
| 786 | - */ | |
| 787 | -if (!function_exists('metasync_get_jwt_token')) { | |
| 788 | - function metasync_get_jwt_token($force_refresh = false) | |
| 789 | - { | |
| 790 | - return Metasync::get_jwt_token($force_refresh); | |
| 791 | - } | |
| 792 | -} | |
| 793 | 809 | |
| 794 | - | |
| 795 | 810 | /** |
| 796 | - * Initialize Debug Mode Manager | |
| 797 | - * Hooked to 'init' for proper WordPress lifecycle integration | |
| 798 | - */ | |
| 799 | -function metasync_init_debug_mode_manager() { | |
| 800 | - if (metasync_is_non_metasync_admin_ajax()) { | |
| 801 | - return; | |
| 802 | - } | |
| 803 | - // Initialize the Debug Mode Manager singleton | |
| 804 | - Metasync_Debug_Mode_Manager::get_instance(); | |
| 805 | -} | |
| 806 | -add_action('init', 'metasync_init_debug_mode_manager', 10); | |
| 807 | - | |
| 808 | -/** | |
| 809 | 811 | * Oxygen Builder Compatibility |
| 810 | 812 | * Auto re-signs [oxygen] dynamic-data shortcodes when their HMAC signatures |
| 811 | 813 | * are invalid (e.g. after design-set import or site migration). |
| 812 | 814 | * Runs once on admin_init; skips entirely when Oxygen is inactive. |
| @@ -812,5 +814,6 @@ | ||
| 812 | 814 | * Runs once on admin_init; skips entirely when Oxygen is inactive. |
| 813 | 815 | */ |
| 814 | 816 | if (is_admin()) { |
| 815 | 817 | add_action('admin_init', ['Metasync_Oxygen_Compat', 'maybe_resign_shortcodes'], 20); |
| 818 | + add_filter('display_post_states', 'metasync_add_lps_post_state', 10, 2); | |
| 816 | 819 | } |