150 lines
3.5 KiB
PHP
150 lines
3.5 KiB
PHP
<?php
|
|
/**
|
|
* ROBOTSTXT Manager dependency notice for iDrivee2 Media Upload.
|
|
*
|
|
* @package iDrivee2Media
|
|
* @since 1.4.2
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
namespace iDrivee2Media;
|
|
|
|
/**
|
|
* Prevent direct access to this file.
|
|
*/
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Checks for the ROBOTSTXT Manager plugin and warns when it is missing.
|
|
*
|
|
* Plugin updates are served through ROBOTSTXT Manager (Manager by ROBOTSTXT).
|
|
* When it is not installed and active, a dismissible notice is shown on the
|
|
* plugins list page and a persistent notice is shown on the plugin settings
|
|
* page, so administrators know updates will not be delivered.
|
|
*
|
|
* @since 1.4.2
|
|
*/
|
|
class Manager_Dependency {
|
|
|
|
/**
|
|
* ROBOTSTXT Manager plugin page URL.
|
|
*
|
|
* @since 1.4.2
|
|
*
|
|
* @var string
|
|
*/
|
|
private const MANAGER_PLUGIN_URL = 'https://www.robotstxt.software/plugins/robotstxt-manager/';
|
|
|
|
/**
|
|
* Register hooks for the admin notices.
|
|
*
|
|
* @since 1.4.2
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register(): void {
|
|
add_action( 'admin_notices', array( $this, 'render_plugins_page_notice' ) );
|
|
add_action( 'network_admin_notices', array( $this, 'render_plugins_page_notice' ) );
|
|
}
|
|
|
|
/**
|
|
* Whether the ROBOTSTXT Manager plugin is installed and active.
|
|
*
|
|
* Uses the ecosystem presence constant ROBOTSTXT_MANAGER_NOTICED
|
|
* (Manager 1.6.2+) and falls back to a plugin-list scan for older
|
|
* Manager versions.
|
|
*
|
|
* @since 1.4.2
|
|
* @since 1.4.3 Switched to the ecosystem presence constant with a
|
|
* plugin-list scan fallback for older Manager versions.
|
|
*
|
|
* @return bool True when ROBOTSTXT Manager is active.
|
|
*/
|
|
public static function is_manager_active(): bool {
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Render the dismissible notice on the plugins list page.
|
|
*
|
|
* @since 1.4.2
|
|
*
|
|
* @return void
|
|
*/
|
|
public function render_plugins_page_notice(): void {
|
|
global $pagenow;
|
|
|
|
$current_page = isset( $pagenow ) && is_string( $pagenow ) ? $pagenow : '';
|
|
if ( 'plugins.php' !== $current_page ) {
|
|
return;
|
|
}
|
|
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
return;
|
|
}
|
|
|
|
if ( self::is_manager_active() ) {
|
|
return;
|
|
}
|
|
|
|
printf(
|
|
'<div class="notice notice-warning is-dismissible"><p>%s</p></div>',
|
|
wp_kses_post( self::message_html() )
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Render the persistent notice for the plugin settings page.
|
|
*
|
|
* @since 1.4.2
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function render_settings_notice(): void {
|
|
if ( self::is_manager_active() ) {
|
|
return;
|
|
}
|
|
|
|
printf(
|
|
'<div class="notice notice-warning"><p>%s</p></div>',
|
|
wp_kses_post( self::message_html() )
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Build the shared warning message HTML.
|
|
*
|
|
* @since 1.4.2
|
|
*
|
|
* @return string Message HTML with an escaped link.
|
|
*/
|
|
private static function message_html(): string {
|
|
return sprintf(
|
|
/* translators: %s: link to the ROBOTSTXT Manager plugin page. */
|
|
esc_html__( 'To receive plugin updates, the ROBOTSTXT Manager plugin must be installed and active. Download it from %s.', 'idrivee2-media-upload' ),
|
|
'<a href="' . esc_url( self::MANAGER_PLUGIN_URL ) . '">ROBOTSTXT Manager</a>'
|
|
);
|
|
}
|
|
}
|