v0.1.0
This commit is contained in:
commit
bc1cb5e00a
32 changed files with 4254 additions and 0 deletions
157
admin/class-robotstxt-manager-admin.php
Normal file
157
admin/class-robotstxt-manager-admin.php
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
<?php
|
||||
/**
|
||||
* Admin menu and main catalog page.
|
||||
*
|
||||
* @package Robotstxt_Manager
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class Robotstxt_Manager_Admin
|
||||
*
|
||||
* Adds the top-level "ROBOTSTXT Plugins" menu and renders the catalog table
|
||||
* that lists every plugin from the remote Plugins Core install alongside
|
||||
* its local install/active/update state.
|
||||
*/
|
||||
class Robotstxt_Manager_Admin {
|
||||
|
||||
/**
|
||||
* Admin page and menu slug.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public const PAGE_SLUG = 'robotstxt-manager';
|
||||
|
||||
/**
|
||||
* Registers all hooks via the loader.
|
||||
*
|
||||
* @param Robotstxt_Manager_Loader $loader The plugin hook loader.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register( Robotstxt_Manager_Loader $loader ): void {
|
||||
$loader->add_action( 'admin_menu', $this, 'add_menu' );
|
||||
$loader->add_action( 'admin_post_robotstxt_manager_refresh_catalog', $this, 'handle_refresh' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the top-level admin menu.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function add_menu(): void {
|
||||
add_menu_page(
|
||||
esc_html__( 'Manager (by ROBOTSTXT) — Plugins', 'robotstxt-manager' ),
|
||||
esc_html__( 'ROBOTSTXT', 'robotstxt-manager' ),
|
||||
'manage_options',
|
||||
self::PAGE_SLUG,
|
||||
array( $this, 'render_page' ),
|
||||
'dashicons-screenoptions',
|
||||
71
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the main catalog page.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function render_page(): void {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'robotstxt-manager' ) );
|
||||
}
|
||||
|
||||
$client = Robotstxt_Manager_Core_Client::from_options();
|
||||
$catalog = $client->get_catalog();
|
||||
$local = $this->resolve_local_state( $catalog );
|
||||
|
||||
require_once ROBOTSTXT_MANAGER_DIR . 'admin/views/page-catalog.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the "Refresh catalog" admin-post action.
|
||||
*
|
||||
* Clears the cached catalog response and redirects back to the page.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle_refresh(): void {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( esc_html__( 'Insufficient permissions.', 'robotstxt-manager' ) );
|
||||
}
|
||||
|
||||
check_admin_referer( 'robotstxt_manager_refresh_catalog' );
|
||||
|
||||
$client = Robotstxt_Manager_Core_Client::from_options();
|
||||
$client->clear_catalog_cache();
|
||||
|
||||
$redirect = add_query_arg(
|
||||
array(
|
||||
'page' => self::PAGE_SLUG,
|
||||
'refreshed' => '1',
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
|
||||
wp_safe_redirect( $redirect );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves local install state for every catalog entry.
|
||||
*
|
||||
* Reads the local plugin list once and matches by slug. Does not perform
|
||||
* any network calls.
|
||||
*
|
||||
* @param list<array<string,mixed>> $catalog Catalog entries from Core.
|
||||
*
|
||||
* @return array<string, array{installed:bool, active:bool, version:string}> Keyed by slug.
|
||||
*/
|
||||
private function resolve_local_state( array $catalog ): array {
|
||||
if ( ! function_exists( 'get_plugins' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
}
|
||||
|
||||
$all_plugins = get_plugins();
|
||||
$local = array();
|
||||
|
||||
// Build a slug → file map from local installs.
|
||||
$slug_to_file = array();
|
||||
foreach ( $all_plugins as $file => $data ) {
|
||||
$slug = dirname( $file );
|
||||
if ( '.' === $slug ) {
|
||||
$slug = basename( $file, '.php' );
|
||||
}
|
||||
$slug_to_file[ $slug ] = $file;
|
||||
}
|
||||
|
||||
foreach ( $catalog as $entry ) {
|
||||
$raw_slug = $entry['slug'] ?? '';
|
||||
$slug = is_string( $raw_slug ) ? $raw_slug : '';
|
||||
if ( '' === $slug ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$file = $slug_to_file[ $slug ] ?? null;
|
||||
$version = '';
|
||||
|
||||
if ( null !== $file && isset( $all_plugins[ $file ]['Version'] ) ) {
|
||||
$raw_ver = $all_plugins[ $file ]['Version'];
|
||||
$version = is_string( $raw_ver ) ? $raw_ver : '';
|
||||
}
|
||||
|
||||
$active = null !== $file && is_string( $file ) && is_plugin_active( $file );
|
||||
|
||||
$local[ $slug ] = array(
|
||||
'installed' => null !== $file,
|
||||
'active' => $active,
|
||||
'version' => $version,
|
||||
);
|
||||
}
|
||||
|
||||
return $local;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue