From d8dfad765310577484a16b360972ddaadd0a7f9a Mon Sep 17 00:00:00 2001 From: Adam Cassis Date: Mon, 14 Apr 2025 11:59:23 +0200 Subject: [PATCH 1/3] feat: adjust capabilities --- includes/class-admin.php | 9 +++++++-- includes/hub/class-nodes.php | 11 +++++++---- includes/hub/database/class-orders.php | 4 +++- includes/hub/database/class-subscriptions.php | 4 +++- includes/node/class-settings.php | 4 ++-- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/includes/class-admin.php b/includes/class-admin.php index f797ee0c..a9557061 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -23,6 +23,11 @@ class Admin { */ const LINK_ACTION_NAME = 'newspack-network-link-site'; + /** + * Capability required to administer here. + */ + const REQUIRED_CAPABILITY = 'newspack_network_admin'; + /** * Runs the initialization. */ @@ -120,7 +125,7 @@ public static function add_admin_menu() { $page_suffix = add_menu_page( __( 'Newspack Network', 'newspack-network' ), __( 'Newspack Network', 'newspack-network' ), - 'manage_options', + self::REQUIRED_CAPABILITY, self::PAGE_SLUG, array( __CLASS__, 'render_page' ), $icon, @@ -145,7 +150,7 @@ public static function add_submenu_page( $title, $slug, $callback ) { self::PAGE_SLUG, $title, $title, - 'manage_options', + self::REQUIRED_CAPABILITY, $slug, $callback ); diff --git a/includes/hub/class-nodes.php b/includes/hub/class-nodes.php index e10ff9cb..8db30c1d 100644 --- a/includes/hub/class-nodes.php +++ b/includes/hub/class-nodes.php @@ -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::REQUIRED_CAPABILITY, + ], 'show_in_rest' => false, 'delete_with_user' => false, 'register_meta_box_cb' => [ __CLASS__, 'add_metabox' ], @@ -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; } } diff --git a/includes/hub/database/class-orders.php b/includes/hub/database/class-orders.php index 56c8700a..6b7b183e 100644 --- a/includes/hub/database/class-orders.php +++ b/includes/hub/database/class-orders.php @@ -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' => \Newspack_Network\Admin::REQUIRED_CAPABILITY, + 'edit_others_posts' => \Newspack_Network\Admin::REQUIRED_CAPABILITY, ], 'show_in_rest' => false, 'delete_with_user' => false, diff --git a/includes/hub/database/class-subscriptions.php b/includes/hub/database/class-subscriptions.php index 6275d1b3..82b4b796 100644 --- a/includes/hub/database/class-subscriptions.php +++ b/includes/hub/database/class-subscriptions.php @@ -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' => \Newspack_Network\Admin::REQUIRED_CAPABILITY, + 'edit_others_posts' => \Newspack_Network\Admin::REQUIRED_CAPABILITY, ], 'show_in_rest' => false, 'delete_with_user' => false, diff --git a/includes/node/class-settings.php b/includes/node/class-settings.php index a767507d..b3d9bf2a 100644 --- a/includes/node/class-settings.php +++ b/includes/node/class-settings.php @@ -487,7 +487,7 @@ public static function process_connection_form() { return; } - if ( ! current_user_can( 'manage_options' ) ) { + if ( ! current_user_can( \Newspack_Network\Admin::REQUIRED_CAPABILITY ) ) { return; } @@ -580,7 +580,7 @@ public static function notice_connection_error() { Date: Tue, 15 Apr 2025 10:34:12 +0200 Subject: [PATCH 2/3] feat: use newspack-plugin capability mapping --- includes/class-admin.php | 43 ++++++++++++++++--- includes/hub/class-nodes.php | 2 +- includes/hub/database/class-orders.php | 4 +- includes/hub/database/class-subscriptions.php | 4 +- includes/node/class-settings.php | 2 +- 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/includes/class-admin.php b/includes/class-admin.php index a9557061..c6db525c 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -24,9 +24,9 @@ class Admin { const LINK_ACTION_NAME = 'newspack-network-link-site'; /** - * Capability required to administer here. + * Capability required to administer here (unless Newspack Plugin's capability methods are unavailable). */ - const REQUIRED_CAPABILITY = 'newspack_network_admin'; + const CAPABILITY = 'newspack_network_admin'; /** * Runs the initialization. @@ -35,6 +35,30 @@ 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; } /** @@ -56,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' ), @@ -125,7 +148,7 @@ public static function add_admin_menu() { $page_suffix = add_menu_page( __( 'Newspack Network', 'newspack-network' ), __( 'Newspack Network', 'newspack-network' ), - self::REQUIRED_CAPABILITY, + self::get_admin_cap(), self::PAGE_SLUG, array( __CLASS__, 'render_page' ), $icon, @@ -150,7 +173,7 @@ public static function add_submenu_page( $title, $slug, $callback ) { self::PAGE_SLUG, $title, $title, - self::REQUIRED_CAPABILITY, + self::get_admin_cap(), $slug, $callback ); @@ -203,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; + } } diff --git a/includes/hub/class-nodes.php b/includes/hub/class-nodes.php index 8db30c1d..27601eac 100644 --- a/includes/hub/class-nodes.php +++ b/includes/hub/class-nodes.php @@ -144,7 +144,7 @@ public static function register_post_type() { 'can_export' => false, 'capability_type' => 'page', 'capabilities' => [ - 'edit_posts' => \Newspack_Network\Admin::REQUIRED_CAPABILITY, + 'edit_posts' => \Newspack_Network\Admin::get_admin_cap(), ], 'show_in_rest' => false, 'delete_with_user' => false, diff --git a/includes/hub/database/class-orders.php b/includes/hub/database/class-orders.php index 6b7b183e..c405e0b7 100644 --- a/includes/hub/database/class-orders.php +++ b/includes/hub/database/class-orders.php @@ -82,8 +82,8 @@ public static function register_post_type() { 'capability_type' => 'post', 'capabilities' => [ 'create_posts' => 'not_a_real_capability', // Set to a fake capability to remove "add new" button. - 'edit_posts' => \Newspack_Network\Admin::REQUIRED_CAPABILITY, - 'edit_others_posts' => \Newspack_Network\Admin::REQUIRED_CAPABILITY, + 'edit_posts' => \Newspack_Network\Admin::get_admin_cap(), + 'edit_others_posts' => \Newspack_Network\Admin::get_admin_cap(), ], 'show_in_rest' => false, 'delete_with_user' => false, diff --git a/includes/hub/database/class-subscriptions.php b/includes/hub/database/class-subscriptions.php index 82b4b796..73836e32 100644 --- a/includes/hub/database/class-subscriptions.php +++ b/includes/hub/database/class-subscriptions.php @@ -82,8 +82,8 @@ public static function register_post_type() { 'capability_type' => 'post', 'capabilities' => [ 'create_posts' => 'not_a_real_capability', // Set to a fake capability to remove "add new" button. - 'edit_posts' => \Newspack_Network\Admin::REQUIRED_CAPABILITY, - 'edit_others_posts' => \Newspack_Network\Admin::REQUIRED_CAPABILITY, + 'edit_posts' => \Newspack_Network\Admin::get_admin_cap(), + 'edit_others_posts' => \Newspack_Network\Admin::get_admin_cap(), ], 'show_in_rest' => false, 'delete_with_user' => false, diff --git a/includes/node/class-settings.php b/includes/node/class-settings.php index b3d9bf2a..49335805 100644 --- a/includes/node/class-settings.php +++ b/includes/node/class-settings.php @@ -487,7 +487,7 @@ public static function process_connection_form() { return; } - if ( ! current_user_can( \Newspack_Network\Admin::REQUIRED_CAPABILITY ) ) { + if ( ! current_user_can( \Newspack_Network\Admin::get_admin_cap() ) ) { return; } From aca30966af04f3fd3a4c4cf69554666f39bc6766 Mon Sep 17 00:00:00 2001 From: Adam Cassis Date: Tue, 15 Apr 2025 10:37:21 +0200 Subject: [PATCH 3/3] refacor: use already imported modules --- includes/hub/database/class-orders.php | 4 ++-- includes/hub/database/class-subscriptions.php | 4 ++-- includes/node/class-settings.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/hub/database/class-orders.php b/includes/hub/database/class-orders.php index c405e0b7..8fd121dd 100644 --- a/includes/hub/database/class-orders.php +++ b/includes/hub/database/class-orders.php @@ -82,8 +82,8 @@ public static function register_post_type() { 'capability_type' => 'post', 'capabilities' => [ 'create_posts' => 'not_a_real_capability', // Set to a fake capability to remove "add new" button. - 'edit_posts' => \Newspack_Network\Admin::get_admin_cap(), - 'edit_others_posts' => \Newspack_Network\Admin::get_admin_cap(), + 'edit_posts' => Network_Admin::get_admin_cap(), + 'edit_others_posts' => Network_Admin::get_admin_cap(), ], 'show_in_rest' => false, 'delete_with_user' => false, diff --git a/includes/hub/database/class-subscriptions.php b/includes/hub/database/class-subscriptions.php index 73836e32..0fa4cfd2 100644 --- a/includes/hub/database/class-subscriptions.php +++ b/includes/hub/database/class-subscriptions.php @@ -82,8 +82,8 @@ public static function register_post_type() { 'capability_type' => 'post', 'capabilities' => [ 'create_posts' => 'not_a_real_capability', // Set to a fake capability to remove "add new" button. - 'edit_posts' => \Newspack_Network\Admin::get_admin_cap(), - 'edit_others_posts' => \Newspack_Network\Admin::get_admin_cap(), + 'edit_posts' => Network_Admin::get_admin_cap(), + 'edit_others_posts' => Network_Admin::get_admin_cap(), ], 'show_in_rest' => false, 'delete_with_user' => false, diff --git a/includes/node/class-settings.php b/includes/node/class-settings.php index 49335805..4057147b 100644 --- a/includes/node/class-settings.php +++ b/includes/node/class-settings.php @@ -487,7 +487,7 @@ public static function process_connection_form() { return; } - if ( ! current_user_can( \Newspack_Network\Admin::get_admin_cap() ) ) { + if ( ! current_user_can( Admin::get_admin_cap() ) ) { return; }