Make WordPress Core

Changeset 35781


Ignore:
Timestamp:
12/06/2015 06:09:42 PM (11 years ago)
Author:
westonruter
Message:

Customizer: Return added instances for panels, sections, controls, and settings when calling WP_Customize_Manager::add_*() methods.

Add missing phpDoc.

Props fusillicode, jubstuff.
Fixes #34596.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r35724 r35781  
    10381038         *
    10391039         * @since 3.4.0
    1040          *
    1041          * @param WP_Customize_Setting|string $id Customize Setting object, or ID.
    1042          * @param array $args                     Setting arguments; passed to WP_Customize_Setting
    1043          *                                        constructor.
     1040         * @since 4.5.0 Return added WP_Customize_Setting instance.
     1041         * @access public
     1042         *
     1043         * @param WP_Customize_Setting|string $id   Customize Setting object, or ID.
     1044         * @param array                       $args Setting arguments; passed to WP_Customize_Setting
     1045         *                                          constructor.
     1046         * @return WP_Customize_Setting             The instance of the setting that was added.
    10441047         */
    10451048        public function add_setting( $id, $args = array() ) {
    … …  
    10491052                        $setting = new WP_Customize_Setting( $this, $id, $args );
    10501053                }
     1054
    10511055                $this->settings[ $setting->id ] = $setting;
     1056                return $setting;
    10521057        }
    10531058
    … …  
    10621067         *
    10631068         * @since 4.2.0
     1069         * @access public
    10641070         *
    10651071         * @param array $setting_ids The setting IDs to add.
    … …  
    11421148         *
    11431149         * @since 4.0.0
     1150         * @since 4.5.0 Return added WP_Customize_Panel instance.
    11441151         * @access public
    11451152         *
    11461153         * @param WP_Customize_Panel|string $id   Customize Panel object, or Panel ID.
    11471154         * @param array                     $args Optional. Panel arguments. Default empty array.
     1155         *
     1156         * @return WP_Customize_Panel             The instance of the panel that was added.
    11481157         */
    11491158        public function add_panel( $id, $args = array() ) {
    … …  
    11551164
    11561165                $this->panels[ $panel->id ] = $panel;
     1166                return $panel;
    11571167        }
    11581168
    … …  
    12171227         *
    12181228         * @since 3.4.0
     1229         * @since 4.5.0 Return added WP_Customize_Section instance.
     1230         * @access public
    12191231         *
    12201232         * @param WP_Customize_Section|string $id   Customize Section object, or Section ID.
    12211233         * @param array                       $args Section arguments.
     1234         *
     1235         * @return WP_Customize_Section             The instance of the section that was added.
    12221236         */
    12231237        public function add_section( $id, $args = array() ) {
    … …  
    12271241                        $section = new WP_Customize_Section( $this, $id, $args );
    12281242                }
     1243
    12291244                $this->sections[ $section->id ] = $section;
     1245                return $section;
    12301246        }
    12311247
    … …  
    12871303         *
    12881304         * @since 3.4.0
     1305         * @since 4.5.0 Return added WP_Customize_Control instance.
     1306         * @access public
    12891307         *
    12901308         * @param WP_Customize_Control|string $id   Customize Control object, or ID.
    12911309         * @param array                       $args Control arguments; passed to WP_Customize_Control
    12921310         *                                          constructor.
     1311         * @return WP_Customize_Control             The instance of the control that was added.
    12931312         */
    12941313        public function add_control( $id, $args = array() ) {
    … …  
    12981317                        $control = new WP_Customize_Control( $this, $id, $args );
    12991318                }
     1319
    13001320                $this->controls[ $control->id ] = $control;
     1321                return $control;
    13011322        }
    13021323
  • trunk/tests/phpunit/tests/customize/manager.php

    r35724 r35781  
    493493                $this->assertEquals( $added_control_ids, $sorted_control_ids );
    494494        }
     495
     496        /**
     497         * @ticket 34596
     498         */
     499        function test_add_section_return_instance() {
     500                $manager = new WP_Customize_Manager();
     501                wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
     502
     503                $section_id = 'foo-section';
     504                $result_section = $manager->add_section( $section_id, array(
     505                        'title'    => 'Section',
     506                        'priority' => 1,
     507                ) );
     508
     509                $this->assertInstanceOf( 'WP_Customize_Section', $result_section );
     510                $this->assertEquals( $section_id, $result_section->id );
     511
     512                $section = new WP_Customize_Section( $manager, $section_id, array(
     513                        'title'    => 'Section 2',
     514                        'priority' => 2,
     515                ) );
     516                $result_section = $manager->add_section( $section );
     517
     518                $this->assertInstanceOf( 'WP_Customize_Section', $result_section );
     519                $this->assertEquals( $section_id, $result_section->id );
     520                $this->assertEquals( $section, $result_section );
     521        }
     522
     523        /**
     524         * @ticket 34596
     525         */
     526        function test_add_setting_return_instance() {
     527                $manager = new WP_Customize_Manager();
     528                wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
     529
     530                $setting_id = 'foo-setting';
     531                $result_setting = $manager->add_setting( $setting_id );
     532
     533                $this->assertInstanceOf( 'WP_Customize_Setting', $result_setting );
     534                $this->assertEquals( $setting_id, $result_setting->id );
     535
     536                $setting = new WP_Customize_Setting( $manager, $setting_id );
     537                $result_setting = $manager->add_setting( $setting );
     538
     539                $this->assertInstanceOf( 'WP_Customize_Setting', $result_setting );
     540                $this->assertEquals( $setting, $result_setting );
     541                $this->assertEquals( $setting_id, $result_setting->id );
     542        }
     543
     544        /**
     545         * @ticket 34596
     546         */
     547        function test_add_panel_return_instance() {
     548                $manager = new WP_Customize_Manager();
     549                wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
     550
     551                $panel_id = 'foo-panel';
     552                $result_panel = $manager->add_panel( $panel_id, array(
     553                        'title'    => 'Test Panel',
     554                        'priority' => 2,
     555                ) );
     556
     557                $this->assertInstanceOf( 'WP_Customize_Panel', $result_panel );
     558                $this->assertEquals( $panel_id, $result_panel->id );
     559
     560                $panel = new WP_Customize_Panel( $manager, $panel_id, array(
     561                        'title' => 'Test Panel 2',
     562                ) );
     563                $result_panel = $manager->add_panel( $panel );
     564
     565                $this->assertInstanceOf( 'WP_Customize_Panel', $result_panel );
     566                $this->assertEquals( $panel, $result_panel );
     567                $this->assertEquals( $panel_id, $result_panel->id );
     568        }
     569
     570        /**
     571         * @ticket 34596
     572         */
     573        function test_add_control_return_instance() {
     574                $manager = new WP_Customize_Manager();
     575                $section_id = 'foo-section';
     576                wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
     577                $manager->add_section( $section_id, array(
     578                        'title'    => 'Section',
     579                        'priority' => 1,
     580                ) );
     581
     582                $control_id = 'foo-control';
     583                $manager->add_setting( $control_id );
     584
     585                $result_control = $manager->add_control( $control_id, array(
     586                        'section'  => $section_id,
     587                        'priority' => 1,
     588                        'setting'  => $control_id,
     589                ) );
     590                $this->assertInstanceOf( 'WP_Customize_Control', $result_control );
     591                $this->assertEquals( $control_id, $result_control->id );
     592
     593                $control = new WP_Customize_Control( $manager, $control_id, array(
     594                        'section'  => $section_id,
     595                        'priority' => 1,
     596                        'setting'  => $control_id,
     597                ) );
     598                $result_control = $manager->add_control( $control );
     599
     600                $this->assertInstanceOf( 'WP_Customize_Control', $result_control );
     601                $this->assertEquals( $control, $result_control );
     602                $this->assertEquals( $control_id, $result_control->id );
     603        }
    495604}
Note: See TracChangeset for help on using the changeset viewer.