-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add site role selection and unify menus
- Loading branch information
1 parent
2e431b4
commit bbc8eb5
Showing
11 changed files
with
279 additions
and
173 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
<?php | ||
/** | ||
* Newspack Network plugin administration screen handling. | ||
* | ||
* @package Newspack | ||
*/ | ||
|
||
namespace Newspack_Network; | ||
|
||
use Newspack_Network\Admin; | ||
|
||
/** | ||
* Class to handle the plugin admin pages | ||
*/ | ||
class Admin { | ||
const PAGE_SLUG = 'newspack-network'; | ||
|
||
/** | ||
* The setting section constant | ||
*/ | ||
const SETTINGS_SECTION = 'newspack_network_settings'; | ||
|
||
/** | ||
* Runs the initialization. | ||
*/ | ||
public static function init() { | ||
add_action( 'admin_menu', array( __CLASS__, 'add_admin_menu' ) ); | ||
add_action( 'admin_init', [ __CLASS__, 'register_settings' ] ); | ||
add_filter( 'allowed_options', [ __CLASS__, 'allowed_options' ] ); | ||
} | ||
|
||
/** | ||
* Adds the options page to the allowed list of options | ||
* | ||
* @param array $allowed_options The allowed options. | ||
* @return array | ||
*/ | ||
public static function allowed_options( $allowed_options ) { | ||
$allowed_options[ self::SETTINGS_SECTION ] = [ | ||
Site_Role::OPTION_NAME, | ||
]; | ||
return $allowed_options; | ||
} | ||
|
||
/** | ||
* Register the settings | ||
* | ||
* @return void | ||
*/ | ||
public static function register_settings() { | ||
|
||
add_settings_section( | ||
self::SETTINGS_SECTION, | ||
esc_html__( 'Newspack Network Settings', 'newspack-network' ), | ||
[ __CLASS__, 'section_callback' ], | ||
self::PAGE_SLUG | ||
); | ||
|
||
$settings = [ | ||
[ | ||
'key' => Site_Role::OPTION_NAME, | ||
'label' => esc_html__( 'Site Role', 'newspack-network' ), | ||
'callback' => [ __CLASS__, 'site_role_callback' ], | ||
'args' => [ | ||
'sanitize_callback' => [ 'Newspack_Network\Site_Role', 'sanitize' ], | ||
], | ||
], | ||
]; | ||
foreach ( $settings as $setting ) { | ||
add_settings_field( | ||
$setting['key'], | ||
$setting['label'], | ||
$setting['callback'], | ||
self::PAGE_SLUG, | ||
self::SETTINGS_SECTION | ||
); | ||
register_setting( | ||
self::PAGE_SLUG, | ||
$setting['key'], | ||
$setting['args'] ?? [] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* The Settings page callback | ||
* | ||
* @return void | ||
*/ | ||
public static function section_callback() { | ||
// Nothing here for now. | ||
} | ||
|
||
/** | ||
* The hub_url setting callback | ||
* | ||
* @return void | ||
*/ | ||
public static function site_role_callback() { | ||
$role = Site_Role::get(); | ||
?> | ||
<select name="<?php echo esc_attr( Site_Role::OPTION_NAME ); ?>"> | ||
<option value="">---</option> | ||
<option value="node" <?php selected( 'node', $role ); ?>><?php esc_html_e( 'This site is a node in a network', 'newspack-network' ); ?></option> | ||
<option value="hub" <?php selected( 'hub', $role ); ?>><?php esc_html_e( 'This site acts as the Network Hub', 'newspack-network' ); ?></option> | ||
</select> | ||
<?php | ||
} | ||
|
||
/** | ||
* Adds the admin page | ||
* | ||
* @return void | ||
*/ | ||
public static function add_admin_menu() { | ||
$page_suffix = add_menu_page( | ||
__( 'Newspack Network', 'newspack-network' ), | ||
__( 'Newspack Network', 'newspack-network' ), | ||
'manage_options', | ||
self::PAGE_SLUG, | ||
array( __CLASS__, 'render_page' ) | ||
); | ||
|
||
self::add_submenu_page( __( 'Site Role', 'newspack-network' ), self::PAGE_SLUG, array( __CLASS__, 'render_page' ) ); | ||
|
||
add_action( 'load-' . $page_suffix, array( __CLASS__, 'admin_init' ) ); | ||
} | ||
|
||
/** | ||
* Adds a child admin page to the main Newspack Hub admin page | ||
* | ||
* @param string $title The menu title. | ||
* @param string $slug The menu slug. | ||
* @param callable $callback The function to be called to output the content for this page. | ||
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. | ||
*/ | ||
public static function add_submenu_page( $title, $slug, $callback ) { | ||
return add_submenu_page( | ||
self::PAGE_SLUG, | ||
$title, | ||
$title, | ||
'manage_options', | ||
$slug, | ||
$callback | ||
); | ||
} | ||
|
||
/** | ||
* Renders the page content | ||
* | ||
* @return void | ||
*/ | ||
public static function render_page() { | ||
?> | ||
<div class='wrap'> | ||
<?php settings_errors(); ?> | ||
<form method='post' action='options.php'> | ||
<?php | ||
do_settings_sections( self::PAGE_SLUG ); | ||
settings_fields( self::SETTINGS_SECTION ); | ||
?> | ||
<p class='submit'> | ||
<input name='submit' type='submit' id='submit' class='button-primary' value='<?php _e( 'Save Changes' ); ?>' /> | ||
</p> | ||
</form> | ||
</div> | ||
<?php | ||
} | ||
|
||
/** | ||
* Callback for the load admin page hook. | ||
* | ||
* @return void | ||
*/ | ||
public static function admin_init() { | ||
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) ); | ||
} | ||
|
||
/** | ||
* Enqueue admin page assets. | ||
* | ||
* @return void | ||
*/ | ||
public static function enqueue_scripts() { | ||
|
||
} | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Newspack Network Site Role handling. | ||
* | ||
* @package Newspack | ||
*/ | ||
|
||
namespace Newspack_Network; | ||
|
||
/** | ||
* Class to handle the Site Role option | ||
*/ | ||
class Site_Role { | ||
|
||
/** | ||
* The option name where the role is stored | ||
* | ||
* @var string | ||
*/ | ||
const OPTION_NAME = 'newspack_network_site_role'; | ||
|
||
/** | ||
* Gets the site role | ||
* | ||
* @return ?string | ||
*/ | ||
public static function get() { | ||
return get_option( self::OPTION_NAME ); | ||
} | ||
|
||
/** | ||
* Checks if the site role is "hub" | ||
* | ||
* @return boolean | ||
*/ | ||
public static function is_hub() { | ||
return 'hub' === self::get(); | ||
} | ||
|
||
/** | ||
* Checks if the site role is "node" | ||
* | ||
* @return boolean | ||
*/ | ||
public static function is_node() { | ||
return 'node' === self::get(); | ||
} | ||
|
||
/** | ||
* Validates a value to be used as the site role | ||
* | ||
* @param string $role The new value to be used as the site role. | ||
* @return string|bool | ||
*/ | ||
public static function sanitize( $role ) { | ||
return in_array( $role, [ 'hub', 'node' ], true ) ? $role : false; | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.