diff --git a/classes/suggested-tasks/local-tasks/providers/class-content-abstract.php b/classes/suggested-tasks/local-tasks/providers/class-content-abstract.php index bf6557ae6..29f786efb 100644 --- a/classes/suggested-tasks/local-tasks/providers/class-content-abstract.php +++ b/classes/suggested-tasks/local-tasks/providers/class-content-abstract.php @@ -10,7 +10,14 @@ /** * Add tasks for content updates. */ -abstract class Content_Abstract { +abstract class Content_Abstract extends Local_Tasks_Abstract { + + /** + * The capability required to perform the task. + * + * @var string + */ + protected $capability = 'edit_others_posts'; /** * Get the task ID. diff --git a/classes/suggested-tasks/local-tasks/providers/class-content-create.php b/classes/suggested-tasks/local-tasks/providers/class-content-create.php index 916e70367..f7b58198b 100644 --- a/classes/suggested-tasks/local-tasks/providers/class-content-create.php +++ b/classes/suggested-tasks/local-tasks/providers/class-content-create.php @@ -7,13 +7,12 @@ namespace Progress_Planner\Suggested_Tasks\Local_Tasks\Providers; -use Progress_Planner\Suggested_Tasks\Local_Tasks\Providers\Local_Tasks_Interface; use Progress_Planner\Activities\Content_Helpers; /** * Add tasks for content creation. */ -class Content_Create extends Content_Abstract implements Local_Tasks_Interface { +class Content_Create extends Content_Abstract { /** * The provider ID. diff --git a/classes/suggested-tasks/local-tasks/providers/class-content-update.php b/classes/suggested-tasks/local-tasks/providers/class-content-update.php index 2e615a03d..43a359781 100644 --- a/classes/suggested-tasks/local-tasks/providers/class-content-update.php +++ b/classes/suggested-tasks/local-tasks/providers/class-content-update.php @@ -8,12 +8,11 @@ namespace Progress_Planner\Suggested_Tasks\Local_Tasks\Providers; use Progress_Planner\Suggested_Tasks\Local_Tasks\Local_Task_Factory; -use Progress_Planner\Suggested_Tasks\Local_Tasks\Providers\Local_Tasks_Interface; /** * Add tasks for content updates. */ -class Content_Update extends Content_Abstract implements Local_Tasks_Interface { +class Content_Update extends Content_Abstract { /** * The provider ID. diff --git a/classes/suggested-tasks/local-tasks/providers/class-core-update.php b/classes/suggested-tasks/local-tasks/providers/class-core-update.php index 51dc1f582..3149b4ff4 100644 --- a/classes/suggested-tasks/local-tasks/providers/class-core-update.php +++ b/classes/suggested-tasks/local-tasks/providers/class-core-update.php @@ -7,12 +7,17 @@ namespace Progress_Planner\Suggested_Tasks\Local_Tasks\Providers; -use Progress_Planner\Suggested_Tasks\Local_Tasks\Providers\Local_Tasks_Interface; - /** * Add tasks for Core updates. */ -class Core_Update implements Local_Tasks_Interface { +class Core_Update extends Local_Tasks_Abstract { + + /** + * The capability required to perform the task. + * + * @var string + */ + protected $capability = 'update_core'; /** * The provider ID. @@ -39,6 +44,11 @@ public function get_provider_type() { */ public function evaluate_task( $task_id ) { + // Early bail if the user does not have the capability to update the core, since \wp_get_update_data()['counts']['total'] will return 0. + if ( ! $this->capability_required() ) { + return false; + } + // Without this \wp_get_update_data() might not return correct data for the core updates (depending on the timing). if ( ! function_exists( 'get_core_updates' ) ) { require_once ABSPATH . 'wp-admin/includes/update.php'; // @phpstan-ignore requireOnce.fileNotFound @@ -56,7 +66,9 @@ public function evaluate_task( $task_id ) { * @return array */ public function get_tasks_to_inject() { - if ( true === $this->is_task_type_snoozed() ) { + + // Early bail if the user does not have the capability to update the core or if the task is snoozed. + if ( true === $this->is_task_type_snoozed() || ! $this->capability_required() ) { return []; } @@ -91,6 +103,7 @@ public function get_task_details( $task_id ) { 'priority' => 'high', 'type' => 'maintenance', 'points' => 1, + 'url' => $this->capability_required() ? \esc_url( \admin_url( 'update-core.php' ) ) : '', 'description' => '
' . \esc_html__( 'Perform all updates to ensure your website is secure and up-to-date.', 'progress-planner' ) . '
', ]; } diff --git a/classes/suggested-tasks/local-tasks/providers/class-local-tasks-abstract.php b/classes/suggested-tasks/local-tasks/providers/class-local-tasks-abstract.php new file mode 100644 index 000000000..6f2f1a250 --- /dev/null +++ b/classes/suggested-tasks/local-tasks/providers/class-local-tasks-abstract.php @@ -0,0 +1,34 @@ +capability + ? \current_user_can( $this->capability ) + : true; + } +} diff --git a/classes/suggested-tasks/local-tasks/providers/class-local-tasks-interface.php b/classes/suggested-tasks/local-tasks/providers/class-local-tasks-interface.php index 77b5c06d7..bb8be56cd 100644 --- a/classes/suggested-tasks/local-tasks/providers/class-local-tasks-interface.php +++ b/classes/suggested-tasks/local-tasks/providers/class-local-tasks-interface.php @@ -52,4 +52,11 @@ public function get_data_from_task_id( $task_id ); * @return string */ public function get_provider_type(); + + /** + * Check if the user has the capability to perform the task. + * + * @return bool + */ + public function capability_required(); } diff --git a/classes/suggested-tasks/local-tasks/providers/class-settings-saved.php b/classes/suggested-tasks/local-tasks/providers/class-settings-saved.php index 239a80190..193d1fd2c 100644 --- a/classes/suggested-tasks/local-tasks/providers/class-settings-saved.php +++ b/classes/suggested-tasks/local-tasks/providers/class-settings-saved.php @@ -7,12 +7,10 @@ namespace Progress_Planner\Suggested_Tasks\Local_Tasks\Providers; -use Progress_Planner\Suggested_Tasks\Local_Tasks\Providers\Local_Tasks_Interface; - /** * Add tasks for settings saved. */ -class Settings_Saved implements Local_Tasks_Interface { +class Settings_Saved extends Local_Tasks_Abstract { /** * The provider ID. @@ -38,6 +36,12 @@ public function get_provider_type() { * @return bool|string */ public function evaluate_task( $task_id ) { + + // Early bail if the user does not have the capability to manage options. + if ( ! $this->capability_required() ) { + return false; + } + if ( 0 === strpos( $task_id, self::TYPE ) && false !== \get_option( 'progress_planner_pro_license_key', false ) ) { return $task_id; } @@ -50,7 +54,9 @@ public function evaluate_task( $task_id ) { * @return array */ public function get_tasks_to_inject() { - if ( true === $this->is_task_type_snoozed() ) { + + // Early bail if the user does not have the capability to manage options or if the task is snoozed. + if ( true === $this->is_task_type_snoozed() || ! $this->capability_required() ) { return []; } @@ -93,7 +99,7 @@ public function get_task_details( $task_id ) { 'priority' => 'high', 'type' => 'maintenance', 'points' => 1, - 'url' => \esc_url( \admin_url( 'admin.php?page=progress-planner-settings' ) ), + 'url' => \current_user_can( 'manage_options' ) ? \esc_url( \admin_url( 'admin.php?page=progress-planner-settings' ) ) : '', 'description' => '' . \esc_html__( 'Head over to the settings page and fill in the required information.', 'progress-planner' ) . '
', ]; } diff --git a/classes/widgets/class-suggested-tasks.php b/classes/widgets/class-suggested-tasks.php index 6d57b5d61..0ceb62d22 100644 --- a/classes/widgets/class-suggested-tasks.php +++ b/classes/widgets/class-suggested-tasks.php @@ -9,6 +9,7 @@ use Progress_Planner\Widget; use Progress_Planner\Badges\Monthly; +use Progress_Planner\Suggested_Tasks\Local_Tasks\Local_Task_Factory; /** * Suggested_Tasks class. @@ -100,16 +101,21 @@ public function enqueue_scripts() { // Insert the pending celebration tasks as high priority tasks, so they are shown always. foreach ( $tasks['pending_celebration'] as $task_id ) { - $task_details = \progress_planner()->get_suggested_tasks()->get_local()->get_task_details( $task_id ); + $task_object = ( new Local_Task_Factory( $task_id ) )->get_task(); + $task_provider = \progress_planner()->get_suggested_tasks()->get_local()->get_task_provider( $task_object->get_provider_type() ); - if ( $task_details ) { - $task_details['priority'] = 'high'; // Celebrate tasks are always on top. - $task_details['action'] = 'celebrate'; - $tasks['details'][] = $task_details; - } + if ( $task_provider->capability_required() ) { + $task_details = \progress_planner()->get_suggested_tasks()->get_local()->get_task_details( $task_id ); + + if ( $task_details ) { + $task_details['priority'] = 'high'; // Celebrate tasks are always on top. + $task_details['action'] = 'celebrate'; + $tasks['details'][] = $task_details; + } - // Mark the pending celebration tasks as completed. - \progress_planner()->get_suggested_tasks()->transition_task_status( $task_id, 'pending_celebration', 'completed' ); + // Mark the pending celebration tasks as completed. + \progress_planner()->get_suggested_tasks()->transition_task_status( $task_id, 'pending_celebration', 'completed' ); + } } // Localize the script. diff --git a/views/dashboard-widgets/score.php b/views/dashboard-widgets/score.php index 806438850..d7480f598 100644 --- a/views/dashboard-widgets/score.php +++ b/views/dashboard-widgets/score.php @@ -37,9 +37,11 @@