| @@ -48,11 +48,16 @@ | ||
| 48 | 48 | delete_option(self::UNINSTALLED_OPTION); |
| 49 | 49 | |
| 50 | 50 | $this->check_requirements(); |
| 51 | 51 | $this->create_database_tables(); |
| 52 | + // Both must precede set_default_options(): they read | |
| 53 | + // `thinkrank_version`, which that method creates. | |
| 54 | + $this->retire_sitemap_legacy_fallback(); | |
| 55 | + $this->seed_feed_defaults(); | |
| 52 | 56 | $this->set_default_options(); |
| 53 | 57 | $this->setup_indexnow_key(); |
| 54 | 58 | $this->schedule_cron_jobs(); |
| 59 | + $this->restore_webroot_artifacts(); | |
| 55 | 60 | $this->set_activation_flag(); |
| 56 | 61 | |
| 57 | 62 | // Grant the admin capabilities here rather than waiting for the `init` |
| 58 | 63 | // hook Role_Manager registers, so the menu is reachable on the very |
| @@ -178,8 +183,100 @@ | ||
| 178 | 183 | |
| 179 | 184 | |
| 180 | 185 | |
| 181 | 186 | /** |
| 187 | + * On a brand-new install, close the pre-2.1.1 sitemap ownership fallback | |
| 188 | + * before it can ever open. | |
| 189 | + * | |
| 190 | + * {@see thinkrank_webroot_sitemap_is_ours()} keeps one narrow escape hatch: | |
| 191 | + * a sitemap written before 2.1.1 with `enable_styling` off carries neither | |
| 192 | + * the marker nor our XSL href, so it can only be recognised by the name the | |
| 193 | + * stored settings derive. That fallback is gated on this install never | |
| 194 | + * having written a marked sitemap — but "never written one" describes two | |
| 195 | + * completely different sites: | |
| 196 | + * | |
| 197 | + * - a pre-2.1.1 install that has not regenerated since upgrading, which | |
| 198 | + * is exactly what the fallback exists to recover; and | |
| 199 | + * - a fresh install that simply has not generated yet, which cannot have | |
| 200 | + * a legacy file of ours on disk at all. | |
| 201 | + * | |
| 202 | + * On the second, the fallback has nothing to recover and can only delete | |
| 203 | + * somebody else's sitemap from one of the canonical names — #515 again, in | |
| 204 | + * a site that never had the problem the fallback addresses. It is not a | |
| 205 | + * narrow window either: `regenerate_sitemap_from_settings()` returns early | |
| 206 | + * while the master `enabled` flag is off, so a site with sitemaps disabled | |
| 207 | + * and styling saved off never records a marked write, and stays exposed for | |
| 208 | + * as long as it stays in that configuration. | |
| 209 | + * | |
| 210 | + * Recording the marker here on a fresh install separates the two cases. An | |
| 211 | + * upgrade does not reach this code — WordPress does not re-run the | |
| 212 | + * activation hook on update — so a genuine pre-2.1.1 site keeps the | |
| 213 | + * fallback until its first marked write, exactly as before. | |
| 214 | + * | |
| 215 | + * `thinkrank_version` is the signal: set_default_options() adds it only | |
| 216 | + * when absent and never updates it, so it is missing on the very first | |
| 217 | + * activation and present on every one after. | |
| 218 | + * | |
| 219 | + * @since 2.1.1 | |
| 220 | + * | |
| 221 | + * @return void | |
| 222 | + */ | |
| 223 | + private function retire_sitemap_legacy_fallback(): void { | |
| 224 | + if (get_option('thinkrank_version') !== false) { | |
| 225 | + return; | |
| 226 | + } | |
| 227 | + | |
| 228 | + require_once THINKRANK_PLUGIN_DIR . 'includes/cleanup-webroot.php'; | |
| 229 | + | |
| 230 | + add_option(THINKRANK_SITEMAP_MARKED_WRITE_OPTION, '1', '', false); | |
| 231 | + } | |
| 232 | + | |
| 233 | + /** | |
| 234 | + * Give a brand-new install the feed posture the competitors ship with. | |
| 235 | + * | |
| 236 | + * The feed controls (#635) default to off in | |
| 237 | + * {@see Site_Identity_Manager::get_default_settings()}, and they have to: | |
| 238 | + * two of the three change what a site already publishes. Signing every | |
| 239 | + * entry adds a line to what existing subscribers receive, and noindexing | |
| 240 | + * feeds withdraws URLs a site may have had indexed for years — on a podcast | |
| 241 | + * site, whose feed has to stay indexable, silently at that. Neither belongs | |
| 242 | + * in a plugin update. | |
| 243 | + * | |
| 244 | + * A first install has no subscribers and no indexed feed, so there is | |
| 245 | + * nothing to change and the protective defaults are simply the right | |
| 246 | + * starting point — which is what The SEO Framework, Yoast and Rank Math all | |
| 247 | + * ship. Seeding them here rather than in the defaults is what separates the | |
| 248 | + * two cases. | |
| 249 | + * | |
| 250 | + * Excerpt-only is left off even here: it changes what readers get rather | |
| 251 | + * than what scrapers can take, and that is the site owner's call. | |
| 252 | + * | |
| 253 | + * Same signal and same reasoning as {@see self::retire_sitemap_legacy_fallback()}: | |
| 254 | + * `thinkrank_version` is absent only on the very first activation, and an | |
| 255 | + * upgrade does not re-run the activation hook at all. | |
| 256 | + * | |
| 257 | + * @since 2.7.0 | |
| 258 | + * | |
| 259 | + * @return void | |
| 260 | + */ | |
| 261 | + private function seed_feed_defaults(): void { | |
| 262 | + if (get_option('thinkrank_version') !== false) { | |
| 263 | + return; | |
| 264 | + } | |
| 265 | + | |
| 266 | + $manager = new \ThinkRank\SEO\Site_Identity_Manager(); | |
| 267 | + | |
| 268 | + $manager->save_settings( | |
| 269 | + 'site', | |
| 270 | + null, | |
| 271 | + [ | |
| 272 | + 'feed_source_link' => true, | |
| 273 | + 'feed_noindex' => true, | |
| 274 | + ] | |
| 275 | + ); | |
| 276 | + } | |
| 277 | + | |
| 278 | + /** | |
| 182 | 279 | * Set default plugin options |
| 183 | 280 | * |
| 184 | 281 | * @return void |
| 185 | 282 | */ |
| @@ -186,9 +283,9 @@ | ||
| 186 | 283 | private function set_default_options(): void { |
| 187 | 284 | $default_options = [ |
| 188 | 285 | 'thinkrank_version' => THINKRANK_VERSION, |
| 189 | 286 | |
| 190 | - 'thinkrank_ai_provider' => 'openai', | |
| 287 | + 'thinkrank_ai_provider' => \ThinkRank\Core\Settings::AI_PROVIDER_NONE, | |
| 191 | 288 | 'thinkrank_cache_duration' => 3600, // 1 hour |
| 192 | 289 | 'thinkrank_max_requests_per_minute' => 10, |
| 193 | 290 | 'thinkrank_enable_logging' => true, |
| 194 | 291 | 'thinkrank_auto_optimize' => false, |
| @@ -203,10 +300,82 @@ | ||
| 203 | 300 | } |
| 204 | 301 | |
| 205 | 302 | |
| 206 | 303 | /** |
| 304 | + * Republish the web-root artifacts deactivation took away. | |
| 305 | + * | |
| 306 | + * Deactivation removes the published sitemap, robots.txt and llms.txt so an | |
| 307 | + * inactive ThinkRank stops shadowing whatever the user switched to (#510). | |
| 308 | + * That is only safe if switching the plugin back on puts them back, which is | |
| 309 | + * what this does. | |
| 310 | + * | |
| 311 | + * Restores strictly what {@see Deactivator::REPUBLISH_OPTION} recorded as | |
| 312 | + * having been removed — never "everything the settings would allow", which | |
| 313 | + * on a fresh install would publish files the site never had. | |
| 314 | + * | |
| 315 | + * The sitemap goes through schedule_regeneration() rather than being built | |
| 316 | + * inline: a full rebuild on a large site is far too slow to sit inside an | |
| 317 | + * activation request, and the debounced hook already respects the master | |
| 318 | + * `enabled` flag. robots.txt and llms.txt are single small writes, so they | |
| 319 | + * happen here. | |
| 320 | + * | |
| 321 | + * @since 2.1.0 | |
| 322 | + * | |
| 323 | + * @return void | |
| 324 | + */ | |
| 325 | + private function restore_webroot_artifacts(): void { | |
| 326 | + $republish = get_option(Deactivator::REPUBLISH_OPTION, null); | |
| 327 | + | |
| 328 | + if ($republish === null) { | |
| 329 | + // No recorded deactivation — a first install, or an activation that | |
| 330 | + // already consumed the record. | |
| 331 | + return; | |
| 332 | + } | |
| 333 | + | |
| 334 | + // Consume it first. A restore that fatals must not re-run on every | |
| 335 | + // subsequent activation, and each entry below is independently guarded. | |
| 336 | + delete_option(Deactivator::REPUBLISH_OPTION); | |
| 337 | + | |
| 338 | + if (!is_array($republish)) { | |
| 339 | + return; | |
| 340 | + } | |
| 341 | + | |
| 342 | + try { | |
| 343 | + if (in_array('sitemap', $republish, true) && class_exists('ThinkRank\\SEO\\Sitemap_Generator')) { | |
| 344 | + // Read-only instance: the hook-registering one would bind a | |
| 345 | + // second set of content-change listeners to this request. | |
| 346 | + (new \ThinkRank\SEO\Sitemap_Generator(false))->schedule_regeneration(); | |
| 347 | + } | |
| 348 | + | |
| 349 | + if (in_array('robots', $republish, true) && class_exists('ThinkRank\\SEO\\Site_Identity_Manager')) { | |
| 350 | + (new \ThinkRank\SEO\Site_Identity_Manager())->sync_robots_txt_file(); | |
| 351 | + } | |
| 352 | + | |
| 353 | + if (in_array('llms', $republish, true) && class_exists('ThinkRank\\SEO\\LLMs_Txt_Manager')) { | |
| 354 | + $llms = new \ThinkRank\SEO\LLMs_Txt_Manager(); | |
| 355 | + $content = $llms->get_published_content(); | |
| 356 | + | |
| 357 | + // write_llms_txt_to_file() enforces the enabled toggle and the | |
| 358 | + // delivery mode itself, so an empty document is the only case | |
| 359 | + // worth short-circuiting here. | |
| 360 | + if ($content !== '') { | |
| 361 | + $llms->write_llms_txt_to_file($content); | |
| 362 | + } | |
| 363 | + } | |
| 364 | + } catch (\Throwable $e) { | |
| 365 | + // A failed republish must not block activation — the user would be | |
| 366 | + // left unable to switch the plugin on at all. The artifacts rebuild | |
| 367 | + // on the next content or settings save. | |
| 368 | + if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 369 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | |
| 370 | + error_log('ThinkRank: failed to restore web-root artifacts: ' . $e->getMessage()); | |
| 371 | + } | |
| 372 | + } | |
| 373 | + } | |
| 374 | + | |
| 375 | + /** | |
| 207 | 376 | * Schedule cron jobs |
| 208 | - * | |
| 377 | + * | |
| 209 | 378 | * @return void |
| 210 | 379 | */ |
| 211 | 380 | private function schedule_cron_jobs(): void { |
| 212 | 381 | |