'; echo esc_html__( 'RankKernel requires PHP 8.2 or higher. Please upgrade PHP to use this plugin.', 'rankkernel' ); echo '

'; } ); return; } global $wp_version; if ( isset( $wp_version ) && version_compare( $wp_version, '6.5', '<' ) ) { add_action( 'admin_notices', static function () { echo '

'; echo esc_html__( 'RankKernel requires WordPress 6.5 or higher. Please upgrade WordPress to use this plugin.', 'rankkernel' ); echo '

'; } ); return; } // Single source of truth for the plugin version. Bump this one value on // release and every asset URL and stored version reference follows. define( 'RANKKERNEL_VERSION', '0.1.0' ); define( 'RANKKERNEL_FILE', __FILE__ ); define( 'RANKKERNEL_DIR', plugin_dir_path( __FILE__ ) ); define( 'RANKKERNEL_URL', plugin_dir_url( __FILE__ ) ); // Autoload. // // The plugin ships no Composer runtime dependencies. The composer.json require // block lists only the PHP version, and every other entry is a development tool // that is never deployed. Composer's autoloader therefore exists for one reason: // to map the plugin's own RankKernel namespace onto src/. // // Registering that mapping here as well means the plugin loads on a site where // the files were deployed without running composer install, which is the normal // case for a copy, a zip or a git clone. Composer's autoloader is still loaded // when it is present, so development tooling and any future runtime dependency // keep working exactly as before. spl_autoload_register( static function ( string $class_name ): void { $prefix = 'RankKernel\\'; if ( ! str_starts_with( $class_name, $prefix ) ) { return; } $file = RANKKERNEL_DIR . 'src/' . str_replace( '\\', '/', substr( $class_name, strlen( $prefix ) ) ) . '.php'; if ( is_readable( $file ) ) { require_once $file; } } ); $rankkernel_autoloader = RANKKERNEL_DIR . 'vendor/autoload.php'; if ( file_exists( $rankkernel_autoloader ) ) { require_once $rankkernel_autoloader; } // Defence in depth. The fallback above should always resolve the plugin's own // classes, so this only fires when something is genuinely wrong, such as an // incomplete copy of the plugin. Say so plainly rather than letting the first // class reference die later with a bare class not found the operator cannot act on. if ( ! class_exists( \RankKernel\Plugin::class ) ) { add_action( 'admin_notices', static function () { echo '

'; echo esc_html__( 'RankKernel could not load its own classes, so the plugin files look incomplete. Please reinstall RankKernel.', 'rankkernel' ); echo '

'; } ); return; } /** * Activation callback. * * @param bool $network_wide Whether WordPress is network-activating the plugin. */ function rankkernel_activate( $network_wide = false ): void { // Network activation is unsupported and would seed only the current site. // Refuse it before any option, cron or table setup so no partial network // state can be created. if ( $network_wide ) { if ( function_exists( 'deactivate_plugins' ) ) { deactivate_plugins( plugin_basename( RANKKERNEL_FILE ), true ); } if ( function_exists( 'wp_die' ) ) { wp_die( esc_html__( 'RankKernel supports single-site activation only. Network activation is not supported; please activate RankKernel on each site individually.', 'rankkernel' ), '', array( 'response' => 403, 'back_link' => true, ) ); } return; } // Check requirements at activation, deactivate self if missing. if ( version_compare( PHP_VERSION, '8.2.0', '<' ) ) { deactivate_plugins( plugin_basename( RANKKERNEL_FILE ) ); return; } global $wp_version; if ( isset( $wp_version ) && version_compare( $wp_version, '6.5', '<' ) ) { deactivate_plugins( plugin_basename( RANKKERNEL_FILE ) ); return; } // Seed rankkernel_modules, default ON modules. $default_modules = array( 'metadata', 'analysis', 'sitemaps', 'schema', 'breadcrumbs' ); if ( false === get_option( 'rankkernel_modules' ) ) { add_option( 'rankkernel_modules', $default_modules ); } // Seed rankkernel_settings defaults. if ( false === get_option( 'rankkernel_settings' ) ) { $defaults = \RankKernel\Settings\SettingsStore::defaults(); add_option( 'rankkernel_settings', $defaults ); } // Seed db version. if ( false === get_option( 'rankkernel_db_version' ) ) { add_option( 'rankkernel_db_version', '0.0.0', '', false ); } // Schedule the daily 404 retention prune once. The shutdown-after-insert // prune is a backstop only; without this a quiet site never enforces its // retention limits. The callback lives behind the monitor module gate, so // an orphan firing with the module off is a harmless no-op. if ( function_exists( 'wp_next_scheduled' ) && function_exists( 'wp_schedule_event' ) && ! wp_next_scheduled( 'rankkernel_daily_prune' ) ) { wp_schedule_event( time(), 'daily', 'rankkernel_daily_prune' ); } // Conflict detection seeds the notice option (cheap cache; renderer re-checks live). $conflicts = rankkernel_detect_seo_conflicts(); if ( array() !== $conflicts ) { update_option( 'rankkernel_conflict_notice', $conflicts ); } } register_activation_hook( __FILE__, 'rankkernel_activate' ); /** * Deactivation callback, leave data intact. */ function rankkernel_deactivate(): void { // Rewrite rules are not data: the sitemap and robots modules flush them // on boot, so deactivation flushes them back out. Without this the // submitted sitemap URLs hard-404 instead of cleanly disappearing. // Options, tables and files are otherwise left intact. if ( function_exists( 'flush_rewrite_rules' ) ) { flush_rewrite_rules( false ); } // Remove only the marked RankKernel block. Everything outside the // markers stays byte for byte. saveBlock() takes the same guarded backup // path as the admin save, so a missing table or read-only uploads // directory leaves the block in place rather than corrupting the file. if ( class_exists( \RankKernel\Admin\HtaccessFile::class ) ) { try { $rankkernel_htaccess_result = ( new \RankKernel\Admin\HtaccessFile() )->removeBlock(); if ( empty( $rankkernel_htaccess_result['saved'] ) && function_exists( 'do_action' ) ) { do_action( 'rankkernel/htaccess/deactivation_cleanup_failed', $rankkernel_htaccess_result['reason'] ?? 'unknown' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- public hook name, part of the plugin API, must stay stable. } } catch ( \Throwable $throwable ) { if ( function_exists( 'do_action' ) ) { do_action( 'rankkernel/htaccess/deactivation_cleanup_failed', $throwable ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- public hook name, part of the plugin API, must stay stable. } } } if ( function_exists( 'wp_clear_scheduled_hook' ) ) { wp_clear_scheduled_hook( 'rankkernel_daily_prune' ); wp_clear_scheduled_hook( 'rankkernel_indexnow_submit' ); } } register_deactivation_hook( __FILE__, 'rankkernel_deactivate' ); /** * Conflict admin notice. */ function rankkernel_seo_conflicts_map(): array { return array( 'wordpress-seo/wordpress-seo.php' => 'Yoast SEO', 'wordpress-seo-premium/wp-seo-premium.php' => 'Yoast SEO Premium', 'seo-by-rank-math/rank-math.php' => 'Rank Math', 'seo-by-rank-math-pro/rank-math-pro.php' => 'Rank Math PRO', 'wp-seopress/seopress.php' => 'SEOPress', 'wp-seopress-pro/seopress-pro.php' => 'SEOPress PRO', 'all-in-one-seo-pack/all_in_one_seo_pack.php' => 'All in One SEO', 'autodescription/autodescription.php' => 'The SEO Framework', 'slim-seo/slim-seo.php' => 'Slim SEO', ); } /** * Detect active SEO plugin conflicts from single-site and network-activated plugins. * * @return array List of conflicting plugin labels. */ function rankkernel_detect_seo_conflicts(): array { $active = (array) get_option( 'active_plugins', array() ); $network = get_option( 'active_sitewide_plugins', array() ); if ( is_array( $network ) ) { $active = array_merge( $active, array_keys( $network ) ); } $conflicts = array(); foreach ( rankkernel_seo_conflicts_map() as $basename => $label ) { if ( in_array( $basename, $active, true ) ) { $conflicts[] = $label; } } return $conflicts; } /** * Conflict admin notice. */ function rankkernel_conflict_notice(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } if ( ! function_exists( 'get_current_screen' ) ) { return; } $screen = get_current_screen(); if ( ! is_object( $screen ) || ! isset( $screen->id ) || ! is_string( $screen->id ) ) { return; } $screen_id = $screen->id; if ( 'toplevel_page_rankkernel' !== $screen_id && ! str_starts_with( $screen_id, 'rankkernel_page_' ) && 'rankkernel' !== $screen_id && 'plugins' !== $screen_id && 'plugins.php' !== $screen_id ) { return; } // Live re-check every call; the option is a cache only. $conflicts = rankkernel_detect_seo_conflicts(); $stored = get_option( 'rankkernel_conflict_notice', array() ); if ( array() === $conflicts ) { if ( is_array( $stored ) && array() !== $stored ) { delete_option( 'rankkernel_conflict_notice' ); } return; } if ( $stored !== $conflicts ) { update_option( 'rankkernel_conflict_notice', $conflicts ); } echo '

'; echo esc_html( sprintf( /* translators: %s: comma-separated list of conflicting plugins */ __( 'RankKernel detected another SEO plugin active (%s). Running multiple SEO plugins may cause duplicated meta tags. Please deactivate the one you do not need.', 'rankkernel' ), implode( ', ', $conflicts ) ) ); echo '

'; } add_action( 'admin_notices', 'rankkernel_conflict_notice' ); // Boot: plugins_loaded priority 10 -> registerCoreServices. add_action( 'plugins_loaded', static function (): void { \RankKernel\Plugin::getInstance()->registerCoreServices(); }, 10 ); // Boot: init -> bootModules. add_action( 'init', static function (): void { \RankKernel\Plugin::getInstance()->bootModules(); } );