191 lines
5.1 KiB
PHP
191 lines
5.1 KiB
PHP
<?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.
|
|
*
|
|
* Uses the ecosystem presence constant (Manager 1.6.2+) and falls back
|
|
* to a plugin-list scan for older Manager versions.
|
|
*
|
|
* @since 1.2.2
|
|
* @since 1.2.3 Added the ROBOTSTXT_MANAGER_NOTICED presence-constant check.
|
|
*
|
|
* @return bool True when Manager is present and activated.
|
|
*/
|
|
public function is_manager_active() {
|
|
if ( defined( 'ROBOTSTXT_MANAGER_NOTICED' ) && ROBOTSTXT_MANAGER_NOTICED ) {
|
|
return true;
|
|
}
|
|
|
|
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> — <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' )
|
|
);
|
|
}
|
|
}
|
|
}
|