Last active
March 17, 2020 10:36
-
-
Save Xilonz/69e391d4108108c5b9b7555e8a8e9958 to your computer and use it in GitHub Desktop.
Bedrock SatisPress MU-Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Bedrock StatisPress | |
* Plugin URI: https://steenbergen.design | |
* Description: An MU Plugin that ties Bedrock and SatisPress together. Allows for autoupdates and cleans up the admin dasboard. | |
* Version: 1.0.0 | |
* Author: Steenbergen Design | |
* Author URI: https://steenbergen.design | |
* License: MIT License | |
*/ | |
namespace BedrockSatisPress; | |
use SatisPress\Capabilities; | |
class App { | |
function __construct() { | |
/** | |
* Always disable indexing | |
* Just making sure | |
*/ | |
add_action( 'pre_option_blog_public', '__return_zero' ); | |
/** | |
* WordPress enable the auto updater for everything except core | |
*/ | |
add_filter( 'auto_update_core', '__return_false' ); | |
add_filter( 'auto_update_plugin', '__return_true' ); | |
add_filter( 'auto_update_theme', '__return_true' ); | |
add_filter( 'auto_update_translation', '__return_true' ); | |
add_filter( 'automatic_updater_disabled', '__return_false' ); | |
add_filter( 'file_mod_allowed', array( $this, 'allow_updater_file_mod' ), 10, 2 ); | |
add_action( 'template_redirect', array( $this, 'maybe_redirect_to_admin' ) ); | |
/** Cleaning up WordPress Dashboard */ | |
add_action( 'init', array( $this, 'remove_roles' ) ); | |
add_action( 'init', array( $this, 'remove_capablilties' ) ); | |
add_action( 'admin_menu', array( $this, 'add_theme_install_menu' ) ); | |
add_action( 'submenu_file', array( $this, 'enable_theme_install_menu' ), 10, 2 ); | |
/** | |
* What we're here for! | |
* SatisPress | |
*/ | |
add_action( 'satispress_composed', array( $this, 'satispress_composed'), 10, 2 ); | |
/** | |
* wp-force-login plugin support, always redirect to the admin | |
*/ | |
add_filter( 'v_forcelogin_bypass', 'is_admin', 10, 0); | |
add_filter( 'v_forcelogin_redirect', 'get_admin_url', 10, 0); | |
} | |
/** | |
* Make an exception for file modification when auto updating | |
* @hook file_mod_allowed | |
*/ | |
public function allow_updater_file_mod( $allowed, $context ){ | |
if( $context == 'automatic_updater' ) return true; | |
return $allowed; | |
} | |
/** | |
* Redirect all frontend urls to wp-admin | |
* @hook template_redirect | |
*/ | |
public function maybe_redirect_to_admin(){ | |
if( !is_admin() ) { | |
wp_redirect( get_admin_url() ); | |
die; | |
} | |
} | |
/** | |
* Remove all unnessecairy roles | |
* @hook init | |
*/ | |
public function remove_roles(){ | |
$roles = wp_roles(); | |
foreach( array_keys( $roles->role_objects ) as $role ) { | |
if( $role == 'administrator' ) continue; | |
remove_role( $role ); | |
} | |
} | |
/** | |
* Remove all unnessecairy capabilities | |
* Essentialy cleaning up the dashboard | |
* @hook init | |
*/ | |
public function remove_capablilties(){ | |
$roles = wp_roles(); | |
$caps = array( | |
'edit_pages', | |
'edit_posts', | |
'delete_pages', | |
'delete_posts', | |
'edit_others_posts', | |
'edit_others_pages', | |
'delete_others_posts', | |
'delete_others_pages', | |
'manage_categories', | |
'manage_links', | |
'moderate_comments', | |
'publish_pages', | |
'publish_posts', | |
'upload_files', | |
'edit_theme_options', | |
); | |
foreach ( $roles->role_objects as $role ) { | |
foreach ( $caps as $cap ) { | |
$role->remove_cap( $cap ); | |
} | |
} | |
} | |
/** | |
* Add Theme install menu item | |
* @hook admin_menu | |
*/ | |
public function add_theme_install_menu(){ | |
global $submenu; | |
$submenu['themes.php'][10] = array( _x( 'Add New', 'theme' ), 'install_themes', 'theme-install.php' ); | |
} | |
/** | |
* Enable theme install menu item as submenu item | |
* Overwrites WordPress to correctly hightlight the menu item as active item on theme-install.php | |
* instead of theme.php | |
* @hook submenu_file | |
*/ | |
public function enable_theme_install_menu( $submenu_file, $parent_file){ | |
global $self; | |
if($self == 'theme-install.php') $submenu_file = 'theme-install.php'; | |
return $submenu_file; | |
} | |
/** | |
* SatisPress specific | |
* This hooks makes us sure SatisPress is activated and gives us access to settings | |
* @hook satispress_composed | |
*/ | |
public function satispress_composed( $satispress, $container ) { | |
$settings = $container->get( 'screen.settings' ); | |
// Remove original sub-menu-item | |
remove_action('admin_menu', [ $settings, 'add_menu_item' ]); | |
/** | |
* Add new menu items | |
* TODO: Somehow allow hightlighting the packages menu item | |
*/ | |
add_action( 'admin_menu', function () use ( $settings ) { | |
$page_hook = add_menu_page( | |
esc_html__( 'SatisPress', 'satispress' ), | |
esc_html__( 'SatisPress', 'satispress' ), | |
Capabilities::MANAGE_OPTIONS, | |
'satispress', | |
[ $settings, 'render_screen' ], | |
'dashicons-archive', | |
99 | |
); | |
add_submenu_page( | |
'satispress', | |
esc_html__( 'Settings', 'satispress' ), | |
esc_html__( 'Settings', 'satispress' ), | |
Capabilities::MANAGE_OPTIONS, | |
'satispress', | |
[ $settings, 'render_screen' ] | |
); | |
add_submenu_page( | |
'satispress', | |
esc_html__( 'Packages', 'satispress' ), | |
esc_html__( 'Packages', 'satispress' ), | |
Capabilities::MANAGE_OPTIONS, | |
'satispress#satispress-packages', | |
[ $settings, 'render_screen' ] | |
); | |
add_action( 'load-' . $page_hook, [ $settings, 'load_screen' ] ); | |
}); | |
} | |
} | |
$bedrock_satispress = new App(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment