Skip to content

Commit

Permalink
feat: Add site role selection and unify menus
Browse files Browse the repository at this point in the history
  • Loading branch information
leogermani committed Jun 16, 2023
1 parent 2e431b4 commit bbc8eb5
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 173 deletions.
188 changes: 188 additions & 0 deletions includes/class-admin.php
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() {

}

}
33 changes: 19 additions & 14 deletions includes/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@ class Initializer {
* Runs the initialization.
*/
public static function init() {
Hub\Admin::init();
Hub\Nodes::init();
Hub\Webhook::init();
Hub\Pull_Endpoint::init();
Hub\Event_Listeners::init();
Hub\Database\Subscriptions::init();
Hub\Database\Orders::init();

Node\Admin::init();
Node\Settings::init();
if ( Node\Settings::get_hub_url() ) {
Node\Webhook::init();
Node\Pulling::init();
Node\Canonical_Url::init();
Admin::init();

if ( Site_Role::is_hub() ) {
Hub\Admin::init();
Hub\Nodes::init();
Hub\Webhook::init();
Hub\Pull_Endpoint::init();
Hub\Event_Listeners::init();
Hub\Database\Subscriptions::init();
Hub\Database\Orders::init();
}

if ( Site_Role::is_node() ) {
Node\Settings::init();
if ( Node\Settings::get_hub_url() ) {
Node\Webhook::init();
Node\Pulling::init();
Node\Canonical_Url::init();
}
}

Data_Listeners::init();
Expand Down
59 changes: 59 additions & 0 deletions includes/class-site-role.php
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;
}

}
6 changes: 3 additions & 3 deletions includes/hub/admin/class-event-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Newspack_Network\Hub\Admin;

use Newspack_Network\Hub\Admin;
use Newspack_Network\Admin as Network_Admin;

/**
* Class to handle the Event log admin page
Expand All @@ -30,7 +30,7 @@ public static function init() {
* @return void
*/
public static function add_admin_menu() {
Admin::add_submenu_page( __( 'Event Log', 'newspack-network-hub' ), self::PAGE_SLUG, [ __CLASS__, 'render_page' ] );
Network_Admin::add_submenu_page( __( 'Event Log', 'newspack-network-hub' ), self::PAGE_SLUG, [ __CLASS__, 'render_page' ] );
}

/**
Expand All @@ -39,7 +39,7 @@ public static function add_admin_menu() {
* @return void
*/
public static function admin_enqueue_scripts() {
$page_slug = Admin::PAGE_SLUG . '_page_' . self::PAGE_SLUG;
$page_slug = Network_Admin::PAGE_SLUG . '_page_' . self::PAGE_SLUG;
if ( get_current_screen()->id !== $page_slug ) {
return;
}
Expand Down
66 changes: 0 additions & 66 deletions includes/hub/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,81 +11,15 @@
* Class to handle the plugin admin pages
*/
class Admin {
const PAGE_SLUG = 'newspack-hub';

/**
* Runs the initialization.
*/
public static function init() {
add_action( 'admin_menu', array( __CLASS__, 'add_admin_menu' ) );

Admin\Event_Log::init();
Admin\Subscriptions::init();
Admin\Orders::init();
Distributor_Settings::init();
}

/**
* Adds the admin page
*
* @return void
*/
public static function add_admin_menu() {
$page_suffix = add_menu_page(
__( 'Newspack Hub', 'newspack-network-hub' ),
__( 'Newspack Hub', 'newspack-network-hub' ),
'manage_options',
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() {
echo '<div id="root"></div>';
}

/**
* 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() {

}

}
Loading

0 comments on commit bbc8eb5

Please sign in to comment.