197 lines
5.3 KiB
PHP
197 lines
5.3 KiB
PHP
<?php
|
|
/**
|
|
* Manager plugin dependency notice.
|
|
*
|
|
* @package RobotstxtTelemetry
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
if ( ! class_exists( 'Robotstxt_Telemetry_Manager_Notice' ) ) {
|
|
/**
|
|
* Class Robotstxt_Telemetry_Manager_Notice
|
|
*
|
|
* Detects the Manager (by ROBOTSTXT) plugin and reminds admins to install
|
|
* it when it is missing, so this plugin keeps receiving updates.
|
|
*/
|
|
class Robotstxt_Telemetry_Manager_Notice {
|
|
|
|
/**
|
|
* User meta key storing the dismissal state.
|
|
*
|
|
* @var string
|
|
*/
|
|
const DISMISS_META_KEY = 'robotstxt_telemetry_manager_notice_dismissed';
|
|
|
|
/**
|
|
* Register hooks.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register() {
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
|
|
add_action( 'admin_notices', array( $this, 'render_plugins_page_notice' ) );
|
|
add_action( 'network_admin_notices', array( $this, 'render_plugins_page_notice' ) );
|
|
add_action( 'wp_ajax_robotstxt_telemetry_dismiss_manager_notice', array( $this, 'handle_dismiss' ) );
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*
|
|
* @return bool True when Manager is present and activated.
|
|
*/
|
|
public static 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';
|
|
}
|
|
|
|
foreach ( get_plugins() as $file => $data ) {
|
|
$slug = dirname( $file );
|
|
|
|
if ( '.' === $slug ) {
|
|
$slug = basename( $file, '.php' );
|
|
}
|
|
|
|
if ( 'robotstxt-manager' === $slug ) {
|
|
return is_plugin_active( $file );
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Enqueue the inline dismissal script on the plugins list screen.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function enqueue_scripts() {
|
|
if ( ! $this->should_show_notice() ) {
|
|
return;
|
|
}
|
|
|
|
wp_enqueue_script( 'common' );
|
|
|
|
$script = '(function () {
|
|
document.addEventListener(
|
|
\'click\',
|
|
function ( event ) {
|
|
var notice = document.getElementById( \'robotstxt-telemetry-manager-notice\' );
|
|
if ( ! notice || ! event.target.closest( \'#robotstxt-telemetry-manager-notice .notice-dismiss\' ) ) {
|
|
return;
|
|
}
|
|
var data = new FormData();
|
|
data.append( \'action\', \'robotstxt_telemetry_dismiss_manager_notice\' );
|
|
data.append( \'nonce\', \'' . esc_js( wp_create_nonce( 'robotstxt_telemetry_manager_notice' ) ) . '\' );
|
|
fetch( window.ajaxurl, { method: \'POST\', credentials: \'same-origin\', body: data } );
|
|
}
|
|
);
|
|
})();';
|
|
|
|
if ( function_exists( 'wp_add_inline_script' ) ) {
|
|
wp_add_inline_script( 'common', $script, 'after' );
|
|
return;
|
|
}
|
|
|
|
// WordPress 4.0 - 4.4 fallback.
|
|
echo '<script>' . $script . '</script>'; // phpcs:ignore WordPress.Security.EscapeOutput -- Static script with an escaped nonce.
|
|
}
|
|
|
|
/**
|
|
* Render the dismissible notice on the plugins list screen.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function render_plugins_page_notice() {
|
|
if ( ! $this->should_show_notice() ) {
|
|
return;
|
|
}
|
|
|
|
$this->render_notice( 'notice-warning is-dismissible', 'robotstxt-telemetry-manager-notice' );
|
|
}
|
|
|
|
/**
|
|
* Render a permanent notice for the settings screen.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function render_settings_notice() {
|
|
$this->render_notice( 'notice-info', '' );
|
|
}
|
|
|
|
/**
|
|
* Render the Manager (by ROBOTSTXT) notice markup.
|
|
*
|
|
* @param string $classes Extra CSS classes.
|
|
* @param string $id Optional DOM id for the notice container.
|
|
* @return void
|
|
*/
|
|
private function render_notice( $classes, $id ) {
|
|
if ( self::is_manager_active() ) {
|
|
return;
|
|
}
|
|
|
|
printf(
|
|
'<div class="notice %s"%s><p>%s</p></div>',
|
|
esc_attr( $classes ),
|
|
$id ? sprintf( ' id="%s"', esc_attr( $id ) ) : '',
|
|
wp_kses_post(
|
|
sprintf(
|
|
/* translators: 1: Manager plugin URL, 2: link text. */
|
|
__( 'To receive updates for Telemetry disabler (by ROBOTSTXT), the <a href="%1$s">%2$s</a> plugin must be installed and active.', 'robotstxt-telemetry' ),
|
|
'https://www.robotstxt.software/plugins/robotstxt-manager/',
|
|
__( 'Manager (by ROBOTSTXT)', 'robotstxt-telemetry' )
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Whether the plugins page notice should be displayed.
|
|
*
|
|
* @return bool
|
|
*/
|
|
private function should_show_notice() {
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
return false;
|
|
}
|
|
|
|
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
|
|
if ( ! $screen || ! in_array( $screen->id, array( 'plugins', 'plugins-network' ), true ) ) {
|
|
return false;
|
|
}
|
|
|
|
if ( self::is_manager_active() ) {
|
|
return false;
|
|
}
|
|
|
|
return ! get_user_meta( get_current_user_id(), self::DISMISS_META_KEY, true );
|
|
}
|
|
|
|
/**
|
|
* Persist the notice dismissal for the current user.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle_dismiss() {
|
|
check_ajax_referer( 'robotstxt_telemetry_manager_notice' );
|
|
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
wp_die( esc_html__( 'You do not have sufficient permissions to perform this action.', 'robotstxt-telemetry' ) );
|
|
}
|
|
|
|
update_user_meta( get_current_user_id(), self::DISMISS_META_KEY, 1 );
|
|
|
|
wp_send_json_success();
|
|
}
|
|
}
|
|
}
|