Skip to content

Commit

Permalink
Add custom capabilities for the Learn post types
Browse files Browse the repository at this point in the history
Copied from the Learn plugin: WPOrg_Learn\Capabilities
  • Loading branch information
adamwoodnz committed May 6, 2024
1 parent d6d769f commit 9036bc6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions wp-content/themes/pub/wporg-learn-2024/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
*/
require __DIR__ . '/inc/admin.php';

/**
* Capabilities.
*/
require __DIR__ . '/inc/capabilities.php';

/**
* Taxonomies.
*/
Expand Down
42 changes: 42 additions & 0 deletions wp-content/themes/pub/wporg-learn-2024/inc/capabilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace WordPressdotorg\Theme\Learn_2024\Capabilities;

defined( 'WPINC' ) || die();

/**
* Actions and filters.
*/
add_filter( 'map_meta_cap', __NAMESPACE__ . '\map_meta_caps', 20, 4 ); // Needs to fire after meta caps in wporg-internal-notes.

/**
* Map primitive caps to our custom caps.
*
* @param array $required_caps
* @param string $current_cap
* @param int $user_id
* @param mixed $args
*
* @return mixed
*/
function map_meta_caps( $required_caps, $current_cap, $user_id, $args ) {
switch ( $current_cap ) {
case 'edit_any_learn_content':
$required_caps = array();
$learn_content_types = array( 'course', 'lesson', 'meeting' );

// Grant `edit_any_learn_content` when the user has `edit_posts` for any of the learn post types.
foreach ( $learn_content_types as $post_type ) {
$object = get_post_type_object( $post_type );
if ( user_can( $user_id, $object->cap->edit_posts ) ) {
$required_caps[] = $object->cap->edit_posts;
break 2; // Breaks out of the foreach and the switch.
}
}

$required_caps[] = 'do_not_allow';
break;
}

return $required_caps;
}
2 changes: 1 addition & 1 deletion wp-content/themes/pub/wporg-learn-2024/inc/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function register_experience_level() {
'show_tagcloud' => false,
'show_in_rest' => true,
'capabilities' => array(
'assign_terms' => 'edit_lessons',
'assign_terms' => 'edit_any_learn_content', // See WordPressdotorg\Theme\Learn_2024\Capabilities\map_meta_caps.
),
);

Expand Down

0 comments on commit 9036bc6

Please sign in to comment.