robotstxt-og/includes/class-robotstxt-og-manager-notice.php
2026-08-17 18:34:57 +00:00

170 lines
3.8 KiB
PHP

<?php
/**
* Manager Plugin Notice
*
* Recommends installing Manager (by ROBOTSTXT). Since the bundled
* self-updater was removed in 1.2.1, the Manager plugin provides updates
* for this plugin.
*
* @package ROBOTSTXT_OG
* @since 1.2.1
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Class Robotstxt_OG_Manager_Notice
*
* Shows a dismissible notice on the Plugins list page and a persistent
* message on the plugin settings page when Manager (by ROBOTSTXT) is not
* active.
*
* @since 1.2.1
*/
class Robotstxt_OG_Manager_Notice {
/**
* Basename of the Manager (by ROBOTSTXT) plugin.
*
* @since 1.2.1
* @var string
*/
const MANAGER_BASENAME = 'robotstxt-manager/robotstxt-manager.php';
/**
* Query argument used to dismiss the notice.
*
* @since 1.2.1
* @var string
*/
const DISMISS_ARG = 'robotstxt_og_dismiss_manager_notice';
/**
* Nonce action for the dismiss link.
*
* @since 1.2.1
* @var string
*/
const DISMISS_ACTION = 'robotstxt_og_dismiss_manager_notice';
/**
* User meta key storing the per-user dismissal.
*
* @since 1.2.1
* @var string
*/
const DISMISS_META = '_robotstxt_og_manager_notice_dismissed';
/**
* Manager plugin page URL.
*
* @since 1.2.1
* @var string
*/
const MANAGER_URL = 'https://www.robotstxt.software/plugins/robotstxt-manager/';
/**
* Whether the Manager (by ROBOTSTXT) plugin is installed and active.
*
* @since 1.2.1
*
* @return bool True if the Manager plugin is active.
*/
public static function is_manager_active(): bool {
return is_plugin_active( self::MANAGER_BASENAME );
}
/**
* Register hooks.
*
* @since 1.2.1
*
* @return void
*/
public function init(): void {
add_action( 'admin_init', array( $this, 'handle_dismiss' ) );
add_action( 'admin_notices', array( $this, 'render_notice' ) );
add_action( 'network_admin_notices', array( $this, 'render_notice' ) );
}
/**
* Persist the notice dismissal for the current user.
*
* Runs on admin_init; the notice check on admin_notices (fired later in
* the same request) then omits the notice immediately.
*
* @since 1.2.1
*
* @return void
*/
public function handle_dismiss(): void {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! isset( $_GET[ self::DISMISS_ARG ] ) ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
check_admin_referer( self::DISMISS_ACTION );
update_user_meta( get_current_user_id(), self::DISMISS_META, 1 );
}
/**
* Render the dismissible notice on the Plugins list page.
*
* Hooked to both `admin_notices` (single-site Plugins page) and
* `network_admin_notices` (Multisite network Plugins page); both screens
* share the `plugins` screen base.
*
* @since 1.2.1
*
* @return void
*/
public function render_notice(): void {
$screen = get_current_screen();
if ( null === $screen || 'plugins' !== $screen->base ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( self::is_manager_active() ) {
return;
}
if ( get_user_meta( get_current_user_id(), self::DISMISS_META, true ) ) {
return;
}
$dismiss_url = wp_nonce_url(
add_query_arg( array( self::DISMISS_ARG => 1 ) ),
self::DISMISS_ACTION
);
?>
<div class="notice notice-info">
<p>
<?php
echo wp_kses_post(
sprintf(
/* translators: %s: Manager (by ROBOTSTXT) plugin page URL. */
__( 'To receive plugin updates, the <a href="%s">Manager (by ROBOTSTXT)</a> plugin must be installed and active.', 'robotstxt-og' ),
esc_url( self::MANAGER_URL )
)
);
?>
</p>
<p>
<a class="button" href="<?php echo esc_url( $dismiss_url ); ?>"><?php esc_html_e( 'Dismiss this notice', 'robotstxt-og' ); ?></a>
</p>
</div>
<?php
}
}