This commit is contained in:
Javier Casares 2026-08-17 17:47:35 +00:00
commit d4839310cf
15 changed files with 1045 additions and 1061 deletions

View file

@ -0,0 +1,183 @@
<?php
/**
* Manager (by ROBOTSTXT) presence detection and admin notices.
*
* Updates for the ROBOTSTXT plugin ecosystem are delivered through
* Manager (by ROBOTSTXT). When Manager is missing or inactive, this
* component nudges the administrator:
*
* - On the Plugins list screens (site and network): a dismissible admin notice.
* - On this plugin's settings pages: a persistent (non-dismissible) warning.
*
* @package ROBOTSTXT\AI_Translator
* @since 1.2.2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'AI_Translator_Manager_Notice' ) ) {
/**
* Detects Manager (by ROBOTSTXT) and renders admin notices.
*
* @since 1.2.2
*/
class AI_Translator_Manager_Notice {
/**
* Manager plugin slug (directory name).
*
* @since 1.2.2
* @var string
*/
const MANAGER_SLUG = 'robotstxt-manager';
/**
* Manager plugin page on the ROBOTSTXT software site.
*
* @since 1.2.2
* @var string
*/
const MANAGER_URL = 'https://www.robotstxt.software/plugins/robotstxt-manager/';
/**
* Option key storing the dismissed state of the plugins-screen notice.
*
* @since 1.2.2
* @var string
*/
const DISMISSED_OPTION = 'ai_translator_manager_notice_dismissed';
/**
* Registers the WordPress hooks.
*
* @since 1.2.2
*
* @return void
*/
public function register() {
add_action( 'admin_init', array( $this, 'handle_dismiss' ) );
add_action( 'admin_notices', array( $this, 'render_plugins_screen_notice' ) );
add_action( 'network_admin_notices', array( $this, 'render_plugins_screen_notice' ) );
}
/**
* Returns whether Manager (by ROBOTSTXT) is installed and active.
*
* @since 1.2.2
*
* @return bool True when Manager is present and activated.
*/
public function is_manager_active() {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$all_plugins = get_plugins();
foreach ( $all_plugins as $file => $data ) {
$slug = dirname( $file );
if ( '.' === $slug ) {
$slug = basename( $file, '.php' );
}
if ( self::MANAGER_SLUG === $slug ) {
return is_plugin_active( $file );
}
}
return false;
}
/**
* Persists the dismissal of the plugins-screen notice, then redirects
* back to a clean URL.
*
* @since 1.2.2
*
* @return void
*/
public function handle_dismiss() {
if ( ! isset( $_GET['ai_translator_dismiss_manager_notice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- verified below before any state change.
return;
}
check_admin_referer( 'ai_translator_dismiss_manager_notice' );
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
update_option( self::DISMISSED_OPTION, '1', false );
wp_safe_redirect( remove_query_arg( array( 'ai_translator_dismiss_manager_notice', '_wpnonce' ) ) );
exit;
}
/**
* Renders the dismissible notice on the Plugins list screens when
* Manager is missing or inactive.
*
* @since 1.2.2
*
* @return void
*/
public function render_plugins_screen_notice() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
if ( ! is_object( $screen ) || ! in_array( $screen->id, array( 'plugins', 'plugins-network' ), true ) ) {
return;
}
if ( $this->is_manager_active() ) {
return;
}
if ( '1' === get_option( self::DISMISSED_OPTION, '' ) ) {
return;
}
$dismiss_url = wp_nonce_url(
add_query_arg( 'ai_translator_dismiss_manager_notice', '1' ),
'ai_translator_dismiss_manager_notice'
);
printf(
'<div class="notice notice-warning"><p>%1$s <a href="%2$s" target="_blank" rel="noopener noreferrer">%3$s</a> &mdash; <a href="%4$s">%5$s</a></p></div>',
esc_html__( 'To get updates for the ROBOTSTXT plugins, the plugin below must be installed and active:', 'robotstxt-ai-translator' ),
esc_url( self::MANAGER_URL ),
esc_html__( 'Manager (by ROBOTSTXT)', 'robotstxt-ai-translator' ),
esc_url( $dismiss_url ),
esc_html__( 'Dismiss this notice', 'robotstxt-ai-translator' )
);
}
/**
* Renders the persistent (non-dismissible) warning for the settings
* pages when Manager is missing or inactive.
*
* @since 1.2.2
*
* @return void
*/
public function render_settings_notice() {
if ( $this->is_manager_active() ) {
return;
}
printf(
'<div class="notice notice-warning inline"><p>%1$s <a href="%2$s" target="_blank" rel="noopener noreferrer">%3$s</a></p></div>',
esc_html__( 'To get updates for the ROBOTSTXT plugins, the plugin below must be installed and active:', 'robotstxt-ai-translator' ),
esc_url( self::MANAGER_URL ),
esc_html__( 'Manager (by ROBOTSTXT)', 'robotstxt-ai-translator' )
);
}
}
}