$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 ''; // 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( '

%s

', 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 %2$s 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(); } } }