Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Use post type's edit_posts capability for sections instead of default
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 10, 2016
1 parent f1210fb commit 8ba90b1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
37 changes: 34 additions & 3 deletions php/class-wp-customize-post-section.php
Expand Up @@ -22,11 +22,42 @@ class WP_Customize_Post_Section extends WP_Customize_Section {
public $type = self::TYPE;

/**
* Setting that this section is related to.
* Post ID.
*
* @var WP_Customize_Post_Setting
* @access public
* @var string
*/
public $post_id;

/**
* Post type.
*
* @access public
* @var string
*/
public $post_setting;
public $post_type;

/**
* WP_Customize_Post_Section constructor.
*
* @param WP_Customize_Manager $manager Manager.
* @param string $id Section ID.
* @param array $args Section args.
*/
public function __construct( WP_Customize_Manager $manager, $id, array $args = array() ) {

// Note we don't throw an exception here because sections with 'temp' as the ID are used by WP_Customize_Manager::register_section_type().
if ( preg_match( WP_Customize_Post_Setting::SETTING_ID_PATTERN, $id, $matches ) ) {
$args['post_id'] = (int) $matches['post_id'];
$args['post_type'] = $matches['post_type'];
$post_type_obj = get_post_type_object( $args['post_type'] );
if ( ! isset( $args['capability'] ) && $post_type_obj ) {
$args['capability'] = $post_type_obj->cap->edit_posts;
}
}

parent::__construct( $manager, $id, $args );
}

/**
* Export data to JS.
Expand Down
13 changes: 13 additions & 0 deletions tests/php/test-class-wp-customize-post-section.php
Expand Up @@ -102,6 +102,19 @@ public function customize_register() {
$this->wp_customize->add_section( $this->section( $setting ) );
}

/**
* Ensure that the expected capability is used for the post sections.
*
* @see WP_Customize_Post_Section::__construct()
*/
public function test_section_capability() {
$page_id = $this->factory()->post->create( array( 'post_type' => 'page' ) );
$page = get_post( $page_id );
$setting_id = WP_Customize_Post_Setting::get_post_setting_id( $page );
$section = new WP_Customize_Post_Section( $this->wp_customize, $setting_id );
$this->assertEquals( get_post_type_object( 'page' )->cap->edit_posts, $section->capability );
}

/**
* Test export data to JS.
*
Expand Down

0 comments on commit 8ba90b1

Please sign in to comment.