This commit is contained in:
Javier Casares 2026-02-18 15:24:59 +00:00
commit 0ece5f3a6d
16 changed files with 2342 additions and 785 deletions

View file

@ -64,6 +64,13 @@ if ( ! class_exists( 'Robotstxt_Updater' ) ) {
*/
private array $plugin_data;
/**
* Initialized instances keyed by plugin basename.
*
* @var array<string, Robotstxt_Updater>
*/
private static array $instances = array();
/**
* Initialize the updater.
*
@ -72,10 +79,20 @@ if ( ! class_exists( 'Robotstxt_Updater' ) ) {
* Robotstxt_Updater::init( __FILE__ );
*
* @param string $plugin_file_path Absolute path to the main plugin file.
* @param bool $add_action_links Whether to add "Check for Update" action link (only for main plugin).
*/
public static function init( string $plugin_file_path ): void {
public static function init( string $plugin_file_path, bool $add_action_links = false ): void {
$basename = plugin_basename( $plugin_file_path );
// Avoid duplicate initialization.
if ( isset( self::$instances[ $basename ] ) ) {
return;
}
$instance = new self( $plugin_file_path );
$instance->register();
$instance->register( $add_action_links );
self::$instances[ $basename ] = $instance;
}
/**
@ -94,12 +111,20 @@ if ( ! class_exists( 'Robotstxt_Updater' ) ) {
/**
* Register WordPress hooks.
*
* @param bool $add_action_links Whether to add action links (only for main plugin).
*/
private function register(): void {
private function register( bool $add_action_links = false ): void {
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'inject_update_info' ) );
add_filter( 'plugins_api', array( $this, 'provide_plugin_details' ), 10, 3 );
add_action( 'admin_init', array( $this, 'handle_cache_clear' ) );
add_action( 'robotstxt_updater_clear_cache', array( $this, 'clear_cache' ) );
// Only add action links for the main SMTP plugin.
if ( $add_action_links ) {
add_filter( 'plugin_action_links_' . $this->plugin_basename, array( $this, 'add_action_links' ) );
add_filter( 'network_admin_plugin_action_links_' . $this->plugin_basename, array( $this, 'add_action_links' ) );
}
}
/**
@ -379,5 +404,35 @@ if ( ! class_exists( 'Robotstxt_Updater' ) ) {
delete_site_transient( $this->cache_key );
delete_site_transient( 'update_plugins' );
}
/**
* Add custom action links to plugin row.
*
* @param array $links Existing action links.
*
* @return array Modified action links.
*/
public function add_action_links( array $links ): array {
// Only show for users who can update plugins.
if ( ! current_user_can( 'update_plugins' ) ) {
return $links;
}
$url = wp_nonce_url(
add_query_arg( 'robotstxt_clear_update_cache', '1' ),
'robotstxt_clear_update_cache'
);
$check_link = sprintf(
'<a href="%s" title="%s">%s</a>',
esc_url( $url ),
esc_attr__( 'Clear update cache and check for new version', 'robotstxt-smtp' ),
esc_html__( 'Check for Update', 'robotstxt-smtp' )
);
$links[] = $check_link;
return $links;
}
}
}