Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,42 @@ class Admin {
*/
const LINK_ACTION_NAME = 'newspack-network-link-site';

/**
* Capability required to administer here (unless Newspack Plugin's capability methods are unavailable).
*/
const CAPABILITY = 'newspack_network_admin';

/**
* 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' ] );
add_filter( 'newspack_capabilities_map', [ __CLASS__, 'newspack_capabilities_map' ] );
add_filter( 'newspack_capabilities_in_cme_plugin', [ __CLASS__, 'newspack_capabilities_in_cme_plugin' ] );
}

/**
* Get the capability required to administrate.
* If Newspack Plugin capability mapping is available, use the granular capability.
* Otherwise, use the generic 'manage_options' capability.
*/
public static function get_admin_cap() {
if ( method_exists( '\Newspack\Capabilities', 'map_capabilities' ) ) {
return self::CAPABILITY;
}
return 'manage_options';
}

/**
* Map this wizard capability from 'manage_options' capability.
*
* @param array $capabilities_map Mapping of capabilities.
*/
public static function newspack_capabilities_map( $capabilities_map ) {
$capabilities_map[ self::CAPABILITY ] = 'manage_options';
return $capabilities_map;
}

/**
Expand All @@ -51,7 +80,6 @@ public static function allowed_options( $allowed_options ) {
* @return void
*/
public static function register_settings() {

add_settings_section(
self::SETTINGS_SECTION,
esc_html__( 'Newspack Network Settings', 'newspack-network' ),
Expand Down Expand Up @@ -120,7 +148,7 @@ public static function add_admin_menu() {
$page_suffix = add_menu_page(
__( 'Newspack Network', 'newspack-network' ),
__( 'Newspack Network', 'newspack-network' ),
'manage_options',
self::get_admin_cap(),
self::PAGE_SLUG,
array( __CLASS__, 'render_page' ),
$icon,
Expand All @@ -145,7 +173,7 @@ public static function add_submenu_page( $title, $slug, $callback ) {
self::PAGE_SLUG,
$title,
$title,
'manage_options',
self::get_admin_cap(),
$slug,
$callback
);
Expand Down Expand Up @@ -198,4 +226,14 @@ public static function enqueue_scripts() {
public static function use_experimental_auditing_features() {
return defined( 'NEWSPACK_NETWORK_EXPERIMENTAL_AUDITING_FEATURES' ) ? NEWSPACK_NETWORK_EXPERIMENTAL_AUDITING_FEATURES : false;
}

/**
* Register this capability in Newspack capabilities list in the capability-manager-enhanced plugin.
*
* @param array $capabilities Mapping of capabilities.
*/
public static function newspack_capabilities_in_cme_plugin( $capabilities ) {
$capabilities[] = self::CAPABILITY;
return $capabilities;
}
}
11 changes: 7 additions & 4 deletions includes/hub/class-nodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ public static function register_post_type() {
'show_in_menu' => Network_Admin::PAGE_SLUG,
'can_export' => false,
'capability_type' => 'page',
'capabilities' => [
'edit_posts' => \Newspack_Network\Admin::get_admin_cap(),
],
'show_in_rest' => false,
'delete_with_user' => false,
'register_meta_box_cb' => [ __CLASS__, 'add_metabox' ],
Expand Down Expand Up @@ -344,19 +347,19 @@ public static function sync_nodes( $post_id ) {
* Highlight the correct submenu item.
*
* @param string $submenu_file The current submenu file.
*
*
* @return string
*/
public static function submenu_file( $submenu_file ) {
global $pagenow;

// Highlight "Nodes" submenu on Add New Node screen.
if ( $submenu_file === Network_Admin::PAGE_SLUG
&& $pagenow === 'post-new.php'
if ( $submenu_file === Network_Admin::PAGE_SLUG
&& $pagenow === 'post-new.php'
&& self::POST_TYPE_SLUG === filter_input( INPUT_GET, 'post_type', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) {
return 'edit.php?post_type=newspack_hub_nodes';
}

return $submenu_file;
}
}
4 changes: 3 additions & 1 deletion includes/hub/database/class-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public static function register_post_type() {
'can_export' => false,
'capability_type' => 'post',
'capabilities' => [
'create_posts' => 'not_a_real_capability', // Set to a fake capability to remove "add new" button.
'create_posts' => 'not_a_real_capability', // Set to a fake capability to remove "add new" button.
'edit_posts' => Network_Admin::get_admin_cap(),
'edit_others_posts' => Network_Admin::get_admin_cap(),
],
'show_in_rest' => false,
'delete_with_user' => false,
Expand Down
4 changes: 3 additions & 1 deletion includes/hub/database/class-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public static function register_post_type() {
'can_export' => false,
'capability_type' => 'post',
'capabilities' => [
'create_posts' => 'not_a_real_capability', // Set to a fake capability to remove "add new" button.
'create_posts' => 'not_a_real_capability', // Set to a fake capability to remove "add new" button.
'edit_posts' => Network_Admin::get_admin_cap(),
'edit_others_posts' => Network_Admin::get_admin_cap(),
],
'show_in_rest' => false,
'delete_with_user' => false,
Expand Down
4 changes: 2 additions & 2 deletions includes/node/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public static function process_connection_form() {
return;
}

if ( ! current_user_can( 'manage_options' ) ) {
if ( ! current_user_can( Admin::get_admin_cap() ) ) {
return;
}

Expand Down Expand Up @@ -580,7 +580,7 @@ public static function notice_connection_error() {
</div>
<?php
}

/**
* Adds the nodes and their bookmarks to the Admin Bar menu
*
Expand Down