From b485445dfc0fa109ee37d22cf582310f5eb2be90 Mon Sep 17 00:00:00 2001 From: saevar Date: Tue, 2 Dec 2025 14:36:52 +0000 Subject: [PATCH] Change to WordPress format --- 1984-hosting-support-ui.php | 56 ++++----- 1984-hosting-support.php | 179 +++++++++++++++-------------- views/support-dashboard-widget.php | 14 +-- 3 files changed, 130 insertions(+), 119 deletions(-) diff --git a/1984-hosting-support-ui.php b/1984-hosting-support-ui.php index b730efa..fb5ed71 100644 --- a/1984-hosting-support-ui.php +++ b/1984-hosting-support-ui.php @@ -15,49 +15,51 @@ * Feel free to remove this file if you don't intend to use the 1984/HostingSupport1984 * user interface additions. */ -class HostingSupport1984UI -{ +class HostingSupport1984UI { const VIEWS_DIR = './views/'; /** * Build a new HostingSupport1984UI object */ - public function __construct() - { + public function __construct() { // Add the 1984 Support Information dashboard widget. add_action( 'wp_dashboard_setup', - array($this, - 'add_dashboard_widget') + array( + $this, + 'add_dashboard_widget', + ) ); // Enqueue the admin JS and CSS. add_action( 'admin_enqueue_scripts', - array($this, - 'enqueue_admin_scripts') + array( + $this, + 'enqueue_admin_scripts', + ) ); } /** * Add the 1984 Hosting dashboard widget. */ - public function add_dashboard_widget() - { + public function add_dashboard_widget() { wp_add_dashboard_widget( '1984_hosting_support_widget', - __('1984 Hosting Support', 'hostingsupport1984'), - array($this, - 'render_dashboard_widget') + __( '1984 Hosting Support', 'hostingsupport1984' ), + array( + $this, + 'render_dashboard_widget', + ) ); } /** * Render the 1984 Hosting dashboard widget. */ - public function render_dashboard_widget() - { - $this->render_view('support-dashboard-widget.php'); + public function render_dashboard_widget() { + $this->render_view( 'support-dashboard-widget.php' ); } /** @@ -68,15 +70,16 @@ class HostingSupport1984UI * * @return Boolean True if the user has access to the view. False if not. */ - public function render_view(string $view_file, bool $admin_only = true): bool - { + public function render_view( string $view_file, bool $admin_only = true ): bool { if ( - true === $admin_only && current_user_can('manage_options') || + true === $admin_only && current_user_can( 'manage_options' ) || false === $admin_only ) { - require self::view_path($view_file); + require self::view_path( $view_file ); + return true; } + return false; } @@ -87,16 +90,14 @@ class HostingSupport1984UI * * @return String The path. */ - private function view_path(string $view_file): string - { - return plugin_dir_path(__FILE__) . self::VIEWS_DIR . $view_file; + private function view_path( string $view_file ): string { + return plugin_dir_path( __FILE__ ) . self::VIEWS_DIR . $view_file; } /** * Enqueue the 1984 wp-admin scripts */ - public function enqueue_admin_scripts() - { + public function enqueue_admin_scripts() { wp_enqueue_script( '1984-hosting-support-admin', plugins_url( @@ -124,9 +125,8 @@ class HostingSupport1984UI * * @return bool [type] [description] */ - private function current_user_can_view_admin_panel(): bool - { - return (true === current_user_can('manage_options')); + private function current_user_can_view_admin_panel(): bool { + return ( true === current_user_can( 'manage_options' ) ); } } diff --git a/1984-hosting-support.php b/1984-hosting-support.php index b3c29a7..dfda6b7 100644 --- a/1984-hosting-support.php +++ b/1984-hosting-support.php @@ -21,50 +21,58 @@ * There are other classes that may depend on this file, so only remove it if * you do not intend to use the plugin. */ -class HostingSupport1984 -{ +class HostingSupport1984 { const HOSTING_SUPPORT_VERSION = '0.4.0'; /** * Construct a new HostingSupport1984 object */ - public function __construct() - { + public function __construct() { // Loading the text domain for a mu-plugin is tricky. // We need to use admin_init instead of plugins_loaded as we are only // using the plugin in the admin interface. add_action( 'admin_init', - array($this, - 'load_textdomain') + array( + $this, + 'load_textdomain', + ) ); // Register the activation hook. register_activation_hook( __FILE__, - array($this, - 'activation_hook') + array( + $this, + 'activation_hook', + ) ); // Run on activation of any plugin. add_action( 'activated_plugin', - array($this, - 'plugin_activation_hook') + array( + $this, + 'plugin_activation_hook', + ) ); // Run on activation of theme. add_action( 'switch_theme', - array($this, - 'theme_switch_hook') + array( + $this, + 'theme_switch_hook', + ) ); // Add a clear warning on the Patchstack plugin row to discourage removal. add_filter( 'plugin_row_meta', - array($this, - 'add_patchstack_warning_row_meta'), + array( + $this, + 'add_patchstack_warning_row_meta', + ), 10, 2 ); @@ -72,26 +80,36 @@ class HostingSupport1984 // Prevent deactivation/deletion of this plugin and Patchstack via UI links. add_filter( 'plugin_action_links', - array($this, - 'filter_plugin_action_links'), + array( + $this, + 'filter_plugin_action_links', + ), 10, 4 ); add_filter( 'network_admin_plugin_action_links', - array($this, - 'filter_plugin_action_links'), + array( + $this, + 'filter_plugin_action_links', + ), 10, 4 ); // Guard against direct requests (single and bulk) on Plugins screens and updater endpoints. - add_action('load-plugins.php', array($this, - 'protect_plugins_admin_actions')); - add_action('load-plugins-network.php', array($this, - 'protect_plugins_admin_actions')); - add_action('load-update.php', array($this, - 'protect_plugins_admin_actions')); + add_action( 'load-plugins.php', array( + $this, + 'protect_plugins_admin_actions', + ) ); + add_action( 'load-plugins-network.php', array( + $this, + 'protect_plugins_admin_actions', + ) ); + add_action( 'load-update.php', array( + $this, + 'protect_plugins_admin_actions', + ) ); } /** @@ -99,8 +117,7 @@ class HostingSupport1984 * * @return Boolean */ - public function load_textdomain(): bool - { + public function load_textdomain(): bool { return load_plugin_textdomain( 'hostingsupport1984', false, @@ -114,24 +131,23 @@ class HostingSupport1984 * * This is skipped if Patchstack is active on the site. */ - public function activation_hook() - { - if ($this->is_patchstack_active()) { + public function activation_hook() { + if ( $this->is_patchstack_active() ) { // In case Patchstack is active, do not continue. return; } // Auto-update WordPress core. - update_site_option('auto_update_core_major', 'enabled'); + update_site_option( 'auto_update_core_major', 'enabled' ); // Auto-update plugins. - $all_plugins = apply_filters('all_plugins', get_plugins()); + $all_plugins = apply_filters( 'all_plugins', get_plugins() ); - update_site_option('auto_update_plugins', array_keys($all_plugins)); + update_site_option( 'auto_update_plugins', array_keys( $all_plugins ) ); // Auto-update themes. $all_themes = wp_get_themes(); - update_site_option('auto_update_themes', array_keys($all_themes)); + update_site_option( 'auto_update_themes', array_keys( $all_themes ) ); } /** @@ -139,9 +155,8 @@ class HostingSupport1984 * * @return Boolean */ - public function is_patchstack_active() - { - return is_plugin_active('patchstack/patchstack.php'); + public function is_patchstack_active() { + return is_plugin_active( 'patchstack/patchstack.php' ); } /** @@ -149,17 +164,16 @@ class HostingSupport1984 * * This is skipped if Patchstack is active on the site. */ - public function plugin_activation_hook($plugin) - { - if ($this->is_patchstack_active()) { + public function plugin_activation_hook( $plugin ) { + if ( $this->is_patchstack_active() ) { // In case Patchstack is active, do not continue. return; } - $auto_updated_plugins = (array)get_site_option('auto_update_plugins', array()); + $auto_updated_plugins = (array) get_site_option( 'auto_update_plugins', array() ); $auto_updated_plugins[] = $plugin; - update_site_option('auto_update_plugins', array_unique($auto_updated_plugins)); + update_site_option( 'auto_update_plugins', array_unique( $auto_updated_plugins ) ); } /** @@ -167,15 +181,14 @@ class HostingSupport1984 * * This is skipped if Patchstack is active on the site. */ - public function theme_switch_hook($theme) - { - if ($this->is_patchstack_active()) { + public function theme_switch_hook( $theme ) { + if ( $this->is_patchstack_active() ) { // In case Patchstack is active, do not continue. return; } $all_themes = wp_get_themes(); - update_site_option('auto_update_themes', array_keys($all_themes)); + update_site_option( 'auto_update_themes', array_keys( $all_themes ) ); } /** @@ -186,21 +199,20 @@ class HostingSupport1984 * * @return array Modified row meta-links. */ - public function add_patchstack_warning_row_meta(array $links, string $file): array - { + public function add_patchstack_warning_row_meta( array $links, string $file ): array { // Only show the warning on the Patchstack row AND only if Patchstack is active. - if ('patchstack/patchstack.php' !== $file || !$this->is_patchstack_active()) { + if ( 'patchstack/patchstack.php' !== $file || ! $this->is_patchstack_active() ) { return $links; } $warning_text = - __('1984 Hosting: Please do not deactivate or delete this plugin - part of your WordPress Service Pack.', 'hostingsupport1984'); + __( '1984 Hosting: Please do not deactivate or delete this plugin - part of your WordPress Service Pack.', 'hostingsupport1984' ); - $logo_1984_url = plugins_url('icons/1984-hosting-logo.webp', __FILE__); - $logo_patchstack = plugins_url('icons/patchstack-logo.svg', __FILE__); + $logo_1984_url = plugins_url( 'icons/1984-hosting-logo.webp', __FILE__ ); + $logo_patchstack = plugins_url( 'icons/patchstack-logo.svg', __FILE__ ); - $title_1984 = __('Visit 1984 Hosting Homepage', 'hostingsupport1984'); - $title_patchstack = __('Visit Patchstack Homepage', 'hostingsupport1984'); + $title_1984 = __( 'Visit 1984 Hosting Homepage', 'hostingsupport1984' ); + $title_patchstack = __( 'Visit Patchstack Homepage', 'hostingsupport1984' ); $container_open = ''; @@ -208,21 +220,21 @@ class HostingSupport1984 $warning_icon = ''; - $text_html = '' . esc_html($warning_text) . ''; + $text_html = '' . esc_html( $warning_text ) . ''; $logos_html = '' . - '' + '' . - '1984 Hosting' + '1984 Hosting' . '' . - '' + '' . - 'Patchstack' + 'Patchstack' . '' . @@ -243,12 +255,13 @@ class HostingSupport1984 * * @return array */ - public function filter_plugin_action_links(array $actions, string $plugin_file, array $plugin_data, - string $context): array - { - if ($this->is_protected_plugin($plugin_file)) { - unset($actions['deactivate']); - unset($actions['delete']); + public function filter_plugin_action_links( + array $actions, string $plugin_file, array $plugin_data, + string $context + ): array { + if ( $this->is_protected_plugin( $plugin_file ) ) { + unset( $actions['deactivate'] ); + unset( $actions['delete'] ); } return $actions; @@ -261,9 +274,8 @@ class HostingSupport1984 * * @return bool */ - private function is_protected_plugin(string $plugin_file): bool - { - return in_array($plugin_file, $this->get_protected_plugins(), true); + private function is_protected_plugin( string $plugin_file ): bool { + return in_array( $plugin_file, $this->get_protected_plugins(), true ); } /** @@ -271,9 +283,9 @@ class HostingSupport1984 * * @return string[] */ - private function get_protected_plugins(): array - { - $own = plugin_basename(__FILE__); + private function get_protected_plugins(): array { + $own = plugin_basename( __FILE__ ); + return array( $own, 'patchstack/patchstack.php', @@ -283,23 +295,22 @@ class HostingSupport1984 /** * Intercept admin actions attempting to deactivate or delete protected plugins and block them. */ - public function protect_plugins_admin_actions() - { + public function protect_plugins_admin_actions() { // Collect requested action(s). - $action = isset($_REQUEST['action']) ? sanitize_key((string)$_REQUEST['action']) : ''; - $action2 = isset($_REQUEST['action2']) ? sanitize_key((string)$_REQUEST['action2']) : ''; + $action = isset( $_REQUEST['action'] ) ? sanitize_key( (string) $_REQUEST['action'] ) : ''; + $action2 = isset( $_REQUEST['action2'] ) ? sanitize_key( (string) $_REQUEST['action2'] ) : ''; $targets = array(); - if (!empty($_REQUEST['plugin'])) { - $targets[] = sanitize_text_field((string)$_REQUEST['plugin']); + if ( ! empty( $_REQUEST['plugin'] ) ) { + $targets[] = sanitize_text_field( (string) $_REQUEST['plugin'] ); } - if (!empty($_REQUEST['checked']) && is_array($_REQUEST['checked'])) { - foreach ($_REQUEST['checked'] as $pf) { - $targets[] = sanitize_text_field((string)$pf); + if ( ! empty( $_REQUEST['checked'] ) && is_array( $_REQUEST['checked'] ) ) { + foreach ( $_REQUEST['checked'] as $pf ) { + $targets[] = sanitize_text_field( (string) $pf ); } } - if (empty($targets)) { + if ( empty( $targets ) ) { return; } @@ -311,15 +322,15 @@ class HostingSupport1984 'delete-plugin', ); - if (!in_array($action, $blocked_actions, true) && !in_array($action2, $blocked_actions, true)) { + if ( ! in_array( $action, $blocked_actions, true ) && ! in_array( $action2, $blocked_actions, true ) ) { return; } $protected = $this->get_protected_plugins(); - $intersect = array_intersect($targets, $protected); - if (!empty($intersect)) { + $intersect = array_intersect( $targets, $protected ); + if ( ! empty( $intersect ) ) { wp_die( - __('For security, this action is blocked: 1984 Hosting Support and Patchstack cannot be deactivated or deleted while this policy is active.', 'hostingsupport1984'), + __( 'For security, this action is blocked: 1984 Hosting Support and Patchstack cannot be deactivated or deleted while this policy is active.', 'hostingsupport1984' ), 403 ); } diff --git a/views/support-dashboard-widget.php b/views/support-dashboard-widget.php index aad2937..7043c09 100644 --- a/views/support-dashboard-widget.php +++ b/views/support-dashboard-widget.php @@ -15,7 +15,7 @@
+ src="">