Change to WordPress format

This commit is contained in:
2025-12-02 14:36:52 +00:00
parent bf71c6c8a0
commit b485445dfc
3 changed files with 130 additions and 119 deletions

View File

@@ -15,48 +15,50 @@
* Feel free to remove this file if you don't intend to use the 1984/HostingSupport1984 * Feel free to remove this file if you don't intend to use the 1984/HostingSupport1984
* user interface additions. * user interface additions.
*/ */
class HostingSupport1984UI class HostingSupport1984UI {
{
const VIEWS_DIR = './views/'; const VIEWS_DIR = './views/';
/** /**
* Build a new HostingSupport1984UI object * Build a new HostingSupport1984UI object
*/ */
public function __construct() public function __construct() {
{
// Add the 1984 Support Information dashboard widget. // Add the 1984 Support Information dashboard widget.
add_action( add_action(
'wp_dashboard_setup', 'wp_dashboard_setup',
array($this, array(
'add_dashboard_widget') $this,
'add_dashboard_widget',
)
); );
// Enqueue the admin JS and CSS. // Enqueue the admin JS and CSS.
add_action( add_action(
'admin_enqueue_scripts', 'admin_enqueue_scripts',
array($this, array(
'enqueue_admin_scripts') $this,
'enqueue_admin_scripts',
)
); );
} }
/** /**
* Add the 1984 Hosting dashboard widget. * Add the 1984 Hosting dashboard widget.
*/ */
public function add_dashboard_widget() public function add_dashboard_widget() {
{
wp_add_dashboard_widget( wp_add_dashboard_widget(
'1984_hosting_support_widget', '1984_hosting_support_widget',
__( '1984 Hosting Support', 'hostingsupport1984' ), __( '1984 Hosting Support', 'hostingsupport1984' ),
array($this, array(
'render_dashboard_widget') $this,
'render_dashboard_widget',
)
); );
} }
/** /**
* Render the 1984 Hosting dashboard widget. * Render the 1984 Hosting dashboard widget.
*/ */
public function render_dashboard_widget() public function render_dashboard_widget() {
{
$this->render_view( 'support-dashboard-widget.php' ); $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. * @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 ( if (
true === $admin_only && current_user_can( 'manage_options' ) || true === $admin_only && current_user_can( 'manage_options' ) ||
false === $admin_only false === $admin_only
) { ) {
require self::view_path( $view_file ); require self::view_path( $view_file );
return true; return true;
} }
return false; return false;
} }
@@ -87,16 +90,14 @@ class HostingSupport1984UI
* *
* @return String The path. * @return String The path.
*/ */
private function view_path(string $view_file): string private function view_path( string $view_file ): string {
{
return plugin_dir_path( __FILE__ ) . self::VIEWS_DIR . $view_file; return plugin_dir_path( __FILE__ ) . self::VIEWS_DIR . $view_file;
} }
/** /**
* Enqueue the 1984 wp-admin scripts * Enqueue the 1984 wp-admin scripts
*/ */
public function enqueue_admin_scripts() public function enqueue_admin_scripts() {
{
wp_enqueue_script( wp_enqueue_script(
'1984-hosting-support-admin', '1984-hosting-support-admin',
plugins_url( plugins_url(
@@ -124,8 +125,7 @@ class HostingSupport1984UI
* *
* @return bool [type] [description] * @return bool [type] [description]
*/ */
private function current_user_can_view_admin_panel(): bool private function current_user_can_view_admin_panel(): bool {
{
return ( true === current_user_can( 'manage_options' ) ); return ( true === current_user_can( 'manage_options' ) );
} }
} }

View File

@@ -21,50 +21,58 @@
* There are other classes that may depend on this file, so only remove it if * There are other classes that may depend on this file, so only remove it if
* you do not intend to use the plugin. * you do not intend to use the plugin.
*/ */
class HostingSupport1984 class HostingSupport1984 {
{
const HOSTING_SUPPORT_VERSION = '0.4.0'; const HOSTING_SUPPORT_VERSION = '0.4.0';
/** /**
* Construct a new HostingSupport1984 object * Construct a new HostingSupport1984 object
*/ */
public function __construct() public function __construct() {
{
// Loading the text domain for a mu-plugin is tricky. // Loading the text domain for a mu-plugin is tricky.
// We need to use admin_init instead of plugins_loaded as we are only // We need to use admin_init instead of plugins_loaded as we are only
// using the plugin in the admin interface. // using the plugin in the admin interface.
add_action( add_action(
'admin_init', 'admin_init',
array($this, array(
'load_textdomain') $this,
'load_textdomain',
)
); );
// Register the activation hook. // Register the activation hook.
register_activation_hook( register_activation_hook(
__FILE__, __FILE__,
array($this, array(
'activation_hook') $this,
'activation_hook',
)
); );
// Run on activation of any plugin. // Run on activation of any plugin.
add_action( add_action(
'activated_plugin', 'activated_plugin',
array($this, array(
'plugin_activation_hook') $this,
'plugin_activation_hook',
)
); );
// Run on activation of theme. // Run on activation of theme.
add_action( add_action(
'switch_theme', 'switch_theme',
array($this, array(
'theme_switch_hook') $this,
'theme_switch_hook',
)
); );
// Add a clear warning on the Patchstack plugin row to discourage removal. // Add a clear warning on the Patchstack plugin row to discourage removal.
add_filter( add_filter(
'plugin_row_meta', 'plugin_row_meta',
array($this, array(
'add_patchstack_warning_row_meta'), $this,
'add_patchstack_warning_row_meta',
),
10, 10,
2 2
); );
@@ -72,26 +80,36 @@ class HostingSupport1984
// Prevent deactivation/deletion of this plugin and Patchstack via UI links. // Prevent deactivation/deletion of this plugin and Patchstack via UI links.
add_filter( add_filter(
'plugin_action_links', 'plugin_action_links',
array($this, array(
'filter_plugin_action_links'), $this,
'filter_plugin_action_links',
),
10, 10,
4 4
); );
add_filter( add_filter(
'network_admin_plugin_action_links', 'network_admin_plugin_action_links',
array($this, array(
'filter_plugin_action_links'), $this,
'filter_plugin_action_links',
),
10, 10,
4 4
); );
// Guard against direct requests (single and bulk) on Plugins screens and updater endpoints. // Guard against direct requests (single and bulk) on Plugins screens and updater endpoints.
add_action('load-plugins.php', array($this, add_action( 'load-plugins.php', array(
'protect_plugins_admin_actions')); $this,
add_action('load-plugins-network.php', array($this, 'protect_plugins_admin_actions',
'protect_plugins_admin_actions')); ) );
add_action('load-update.php', array($this, add_action( 'load-plugins-network.php', array(
'protect_plugins_admin_actions')); $this,
'protect_plugins_admin_actions',
) );
add_action( 'load-update.php', array(
$this,
'protect_plugins_admin_actions',
) );
} }
/** /**
@@ -99,8 +117,7 @@ class HostingSupport1984
* *
* @return Boolean * @return Boolean
*/ */
public function load_textdomain(): bool public function load_textdomain(): bool {
{
return load_plugin_textdomain( return load_plugin_textdomain(
'hostingsupport1984', 'hostingsupport1984',
false, false,
@@ -114,8 +131,7 @@ class HostingSupport1984
* *
* This is skipped if Patchstack is active on the site. * This is skipped if Patchstack is active on the site.
*/ */
public function activation_hook() public function activation_hook() {
{
if ( $this->is_patchstack_active() ) { if ( $this->is_patchstack_active() ) {
// In case Patchstack is active, do not continue. // In case Patchstack is active, do not continue.
return; return;
@@ -139,8 +155,7 @@ class HostingSupport1984
* *
* @return Boolean * @return Boolean
*/ */
public function is_patchstack_active() public function is_patchstack_active() {
{
return is_plugin_active( 'patchstack/patchstack.php' ); return is_plugin_active( 'patchstack/patchstack.php' );
} }
@@ -149,8 +164,7 @@ class HostingSupport1984
* *
* This is skipped if Patchstack is active on the site. * This is skipped if Patchstack is active on the site.
*/ */
public function plugin_activation_hook($plugin) public function plugin_activation_hook( $plugin ) {
{
if ( $this->is_patchstack_active() ) { if ( $this->is_patchstack_active() ) {
// In case Patchstack is active, do not continue. // In case Patchstack is active, do not continue.
return; return;
@@ -167,8 +181,7 @@ class HostingSupport1984
* *
* This is skipped if Patchstack is active on the site. * This is skipped if Patchstack is active on the site.
*/ */
public function theme_switch_hook($theme) public function theme_switch_hook( $theme ) {
{
if ( $this->is_patchstack_active() ) { if ( $this->is_patchstack_active() ) {
// In case Patchstack is active, do not continue. // In case Patchstack is active, do not continue.
return; return;
@@ -186,8 +199,7 @@ class HostingSupport1984
* *
* @return array Modified row meta-links. * @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. // 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; return $links;
@@ -243,9 +255,10 @@ class HostingSupport1984
* *
* @return array * @return array
*/ */
public function filter_plugin_action_links(array $actions, string $plugin_file, array $plugin_data, public function filter_plugin_action_links(
string $context): array array $actions, string $plugin_file, array $plugin_data,
{ string $context
): array {
if ( $this->is_protected_plugin( $plugin_file ) ) { if ( $this->is_protected_plugin( $plugin_file ) ) {
unset( $actions['deactivate'] ); unset( $actions['deactivate'] );
unset( $actions['delete'] ); unset( $actions['delete'] );
@@ -261,8 +274,7 @@ class HostingSupport1984
* *
* @return bool * @return bool
*/ */
private function is_protected_plugin(string $plugin_file): bool private function is_protected_plugin( string $plugin_file ): bool {
{
return in_array( $plugin_file, $this->get_protected_plugins(), true ); return in_array( $plugin_file, $this->get_protected_plugins(), true );
} }
@@ -271,9 +283,9 @@ class HostingSupport1984
* *
* @return string[] * @return string[]
*/ */
private function get_protected_plugins(): array private function get_protected_plugins(): array {
{
$own = plugin_basename( __FILE__ ); $own = plugin_basename( __FILE__ );
return array( return array(
$own, $own,
'patchstack/patchstack.php', 'patchstack/patchstack.php',
@@ -283,8 +295,7 @@ class HostingSupport1984
/** /**
* Intercept admin actions attempting to deactivate or delete protected plugins and block them. * 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). // Collect requested action(s).
$action = isset( $_REQUEST['action'] ) ? sanitize_key( (string) $_REQUEST['action'] ) : ''; $action = isset( $_REQUEST['action'] ) ? sanitize_key( (string) $_REQUEST['action'] ) : '';
$action2 = isset( $_REQUEST['action2'] ) ? sanitize_key( (string) $_REQUEST['action2'] ) : ''; $action2 = isset( $_REQUEST['action2'] ) ? sanitize_key( (string) $_REQUEST['action2'] ) : '';