Change to WordPress format
This commit is contained in:
@@ -15,48 +15,50 @@
|
||||
* 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')
|
||||
array(
|
||||
$this,
|
||||
'render_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' );
|
||||
}
|
||||
|
||||
@@ -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' ) ||
|
||||
false === $admin_only
|
||||
) {
|
||||
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
|
||||
{
|
||||
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,8 +125,7 @@ class HostingSupport1984UI
|
||||
*
|
||||
* @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' ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,8 +131,7 @@ class HostingSupport1984
|
||||
*
|
||||
* This is skipped if Patchstack is active on the site.
|
||||
*/
|
||||
public function activation_hook()
|
||||
{
|
||||
public function activation_hook() {
|
||||
if ( $this->is_patchstack_active() ) {
|
||||
// In case Patchstack is active, do not continue.
|
||||
return;
|
||||
@@ -139,8 +155,7 @@ class HostingSupport1984
|
||||
*
|
||||
* @return Boolean
|
||||
*/
|
||||
public function is_patchstack_active()
|
||||
{
|
||||
public function is_patchstack_active() {
|
||||
return is_plugin_active( 'patchstack/patchstack.php' );
|
||||
}
|
||||
|
||||
@@ -149,8 +164,7 @@ class HostingSupport1984
|
||||
*
|
||||
* 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() ) {
|
||||
// In case Patchstack is active, do not continue.
|
||||
return;
|
||||
@@ -167,8 +181,7 @@ class HostingSupport1984
|
||||
*
|
||||
* 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() ) {
|
||||
// In case Patchstack is active, do not continue.
|
||||
return;
|
||||
@@ -186,8 +199,7 @@ 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() ) {
|
||||
return $links;
|
||||
@@ -243,9 +255,10 @@ class HostingSupport1984
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function filter_plugin_action_links(array $actions, string $plugin_file, array $plugin_data,
|
||||
string $context): 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'] );
|
||||
@@ -261,8 +274,7 @@ class HostingSupport1984
|
||||
*
|
||||
* @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 );
|
||||
}
|
||||
|
||||
@@ -271,9 +283,9 @@ class HostingSupport1984
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
private function get_protected_plugins(): array
|
||||
{
|
||||
private function get_protected_plugins(): array {
|
||||
$own = plugin_basename( __FILE__ );
|
||||
|
||||
return array(
|
||||
$own,
|
||||
'patchstack/patchstack.php',
|
||||
@@ -283,8 +295,7 @@ 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'] ) : '';
|
||||
|
||||
Reference in New Issue
Block a user