Skip to content

Commit

Permalink
Add custom capabilities for story post type
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Jul 28, 2020
1 parent 7903d4c commit f028991
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
11 changes: 11 additions & 0 deletions includes/Database_Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function init() {
'2.0.2' => 'remove_broken_text_styles',
'2.0.3' => 'unify_color_presets',
'2.0.4' => 'update_publisher_logos',
'3.0.0' => 'add_stories_caps',
];

$version = get_option( self::OPTION, '0.0.0' );
Expand Down Expand Up @@ -230,6 +231,16 @@ protected function update_publisher_logos() {
update_option( Settings::SETTING_NAME_PUBLISHER_LOGOS, array_filter( [ $publisher_logo_id ] ), false );
}

/**
* Adds story capabilities to default user roles.
*
* @return void
*/
protected function add_stories_caps() {
$story_post_type = new Story_Post_Type();
$story_post_type->add_caps_to_roles();
}

/**
* Runs the needed cleanup after an update, setting the DB version to latest version, flushing caches etc.
*
Expand Down
51 changes: 51 additions & 0 deletions includes/Story_Post_Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Google\Web_Stories\Traits\Publisher;
use Google\Web_Stories\Traits\Types;
use WP_Post;
use WP_Role;
use WP_Screen;

/**
Expand Down Expand Up @@ -127,6 +128,8 @@ public function init() {
'show_ui' => true,
'show_in_rest' => true,
'rest_controller_class' => Stories_Controller::class,
'capability_type' => [ 'web-story', 'web-stories' ],
'map_meta_cap' => true,
]
);

Expand All @@ -152,6 +155,54 @@ public function init() {
}
}

/**
* Adds story capabilities to default user roles.
*
* This gives WordPress site owners more granular control over story management,
* as they can customize this to their liking.
*
* @return void
*/
public function add_caps_to_roles() {
$post_type_object = get_post_type_object( self::POST_TYPE_SLUG );

if ( ! $post_type_object ) {
return;
}

$all_capabilities = array_values( (array) $post_type_object->cap );

$administrator = get_role( 'administrator' );
$editor = get_role( 'editor' );
$author = get_role( 'author' );
$contributor = get_role( 'contributor' );

if ( $administrator instanceof WP_Role ) {
foreach ( $all_capabilities as $cap ) {
$administrator->add_cap( $cap );
}
}

if ( $editor instanceof WP_Role ) {
foreach ( $all_capabilities as $cap ) {
$editor->add_cap( $cap );
}
}

if ( $author instanceof WP_Role ) {
$author->add_cap( 'edit_web-stories' );
$author->add_cap( 'edit_published_web-stories' );
$author->add_cap( 'delete_web-stories' );
$author->add_cap( 'delete_published_web-stories' );
$author->add_cap( 'publish_web-stories' );
}

if ( $contributor instanceof WP_Role ) {
$contributor->add_cap( 'edit_web-stories' );
$contributor->add_cap( 'delete_web-stories' );
}
}

/**
* Base64 encoded svg icon.
*
Expand Down
1 change: 1 addition & 0 deletions includes/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function activate( $network_wide ) {

$story = new Story_Post_Type();
$story->init();
$story->add_caps_to_roles();
if ( ! defined( '\WPCOM_IS_VIP_ENV' ) || false === \WPCOM_IS_VIP_ENV ) {
flush_rewrite_rules( false ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.flush_rewrite_rules_flush_rewrite_rules
}
Expand Down
16 changes: 16 additions & 0 deletions tests/phpunit/tests/Story_Post_Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,20 @@ public function test_add_to_jetpack_sitemap() {
$story_post_type = new \Google\Web_Stories\Story_Post_Type();
$this->assertEqualSets( [ \Google\Web_Stories\Story_Post_Type::POST_TYPE_SLUG ], $story_post_type->add_to_jetpack_sitemap( [] ) );
}

/**
* @covers ::add_caps_to_roles
*/
public function test_add_caps_to_roles() {
$post_type_object = get_post_type_object( \Google\Web_Stories\Story_Post_Type::POST_TYPE_SLUG );
$all_capabilities = array_values( (array) $post_type_object->cap );

$administrator = get_role( 'administrator' );
$editor = get_role( 'editor' );

foreach ( $all_capabilities as $cap ) {
$administrator->has_cap( $cap );
$editor->has_cap( $cap );
}
}
}
2 changes: 1 addition & 1 deletion web-stories.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
}

define( 'WEBSTORIES_VERSION', '1.0.0-beta.1' );
define( 'WEBSTORIES_DB_VERSION', '2.0.4' );
define( 'WEBSTORIES_DB_VERSION', '3.0.0' );
define( 'WEBSTORIES_PLUGIN_FILE', __FILE__ );
define( 'WEBSTORIES_PLUGIN_DIR_PATH', plugin_dir_path( WEBSTORIES_PLUGIN_FILE ) );
define( 'WEBSTORIES_PLUGIN_DIR_URL', plugin_dir_url( WEBSTORIES_PLUGIN_FILE ) );
Expand Down

0 comments on commit f028991

Please sign in to comment.