From 168994896562625b046d2e8016426b52182f1901 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 6 Dec 2015 18:09:42 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@35781 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-customize-manager.php | 27 ++++- tests/phpunit/tests/customize/manager.php | 109 ++++++++++++++++++ 2 files changed, 133 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php index 9f15d31f57..e95e115b6d 100644 --- a/src/wp-includes/class-wp-customize-manager.php +++ b/src/wp-includes/class-wp-customize-manager.php @@ -1037,10 +1037,13 @@ final class WP_Customize_Manager { * Add a customize setting. * * @since 3.4.0 + * @since 4.5.0 Return added WP_Customize_Setting instance. + * @access public * - * @param WP_Customize_Setting|string $id Customize Setting object, or ID. - * @param array $args Setting arguments; passed to WP_Customize_Setting - * constructor. + * @param WP_Customize_Setting|string $id Customize Setting object, or ID. + * @param array $args Setting arguments; passed to WP_Customize_Setting + * constructor. + * @return WP_Customize_Setting The instance of the setting that was added. */ public function add_setting( $id, $args = array() ) { if ( $id instanceof WP_Customize_Setting ) { @@ -1048,7 +1051,9 @@ final class WP_Customize_Manager { } else { $setting = new WP_Customize_Setting( $this, $id, $args ); } + $this->settings[ $setting->id ] = $setting; + return $setting; } /** @@ -1061,6 +1066,7 @@ final class WP_Customize_Manager { * even though they are not directly created statically with code. * * @since 4.2.0 + * @access public * * @param array $setting_ids The setting IDs to add. * @return array The WP_Customize_Setting objects added. @@ -1141,10 +1147,13 @@ final class WP_Customize_Manager { * Add a customize panel. * * @since 4.0.0 + * @since 4.5.0 Return added WP_Customize_Panel instance. * @access public * * @param WP_Customize_Panel|string $id Customize Panel object, or Panel ID. * @param array $args Optional. Panel arguments. Default empty array. + * + * @return WP_Customize_Panel The instance of the panel that was added. */ public function add_panel( $id, $args = array() ) { if ( $id instanceof WP_Customize_Panel ) { @@ -1154,6 +1163,7 @@ final class WP_Customize_Manager { } $this->panels[ $panel->id ] = $panel; + return $panel; } /** @@ -1216,9 +1226,13 @@ final class WP_Customize_Manager { * Add a customize section. * * @since 3.4.0 + * @since 4.5.0 Return added WP_Customize_Section instance. + * @access public * * @param WP_Customize_Section|string $id Customize Section object, or Section ID. * @param array $args Section arguments. + * + * @return WP_Customize_Section The instance of the section that was added. */ public function add_section( $id, $args = array() ) { if ( $id instanceof WP_Customize_Section ) { @@ -1226,7 +1240,9 @@ final class WP_Customize_Manager { } else { $section = new WP_Customize_Section( $this, $id, $args ); } + $this->sections[ $section->id ] = $section; + return $section; } /** @@ -1286,10 +1302,13 @@ final class WP_Customize_Manager { * Add a customize control. * * @since 3.4.0 + * @since 4.5.0 Return added WP_Customize_Control instance. + * @access public * * @param WP_Customize_Control|string $id Customize Control object, or ID. * @param array $args Control arguments; passed to WP_Customize_Control * constructor. + * @return WP_Customize_Control The instance of the control that was added. */ public function add_control( $id, $args = array() ) { if ( $id instanceof WP_Customize_Control ) { @@ -1297,7 +1316,9 @@ final class WP_Customize_Manager { } else { $control = new WP_Customize_Control( $this, $id, $args ); } + $this->controls[ $control->id ] = $control; + return $control; } /** diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index f9034f2d44..272166be66 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -492,4 +492,113 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { $sorted_control_ids = wp_list_pluck( $manager->get_section( $section_id )->controls, 'id' ); $this->assertEquals( $added_control_ids, $sorted_control_ids ); } + + /** + * @ticket 34596 + */ + function test_add_section_return_instance() { + $manager = new WP_Customize_Manager(); + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + + $section_id = 'foo-section'; + $result_section = $manager->add_section( $section_id, array( + 'title' => 'Section', + 'priority' => 1, + ) ); + + $this->assertInstanceOf( 'WP_Customize_Section', $result_section ); + $this->assertEquals( $section_id, $result_section->id ); + + $section = new WP_Customize_Section( $manager, $section_id, array( + 'title' => 'Section 2', + 'priority' => 2, + ) ); + $result_section = $manager->add_section( $section ); + + $this->assertInstanceOf( 'WP_Customize_Section', $result_section ); + $this->assertEquals( $section_id, $result_section->id ); + $this->assertEquals( $section, $result_section ); + } + + /** + * @ticket 34596 + */ + function test_add_setting_return_instance() { + $manager = new WP_Customize_Manager(); + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + + $setting_id = 'foo-setting'; + $result_setting = $manager->add_setting( $setting_id ); + + $this->assertInstanceOf( 'WP_Customize_Setting', $result_setting ); + $this->assertEquals( $setting_id, $result_setting->id ); + + $setting = new WP_Customize_Setting( $manager, $setting_id ); + $result_setting = $manager->add_setting( $setting ); + + $this->assertInstanceOf( 'WP_Customize_Setting', $result_setting ); + $this->assertEquals( $setting, $result_setting ); + $this->assertEquals( $setting_id, $result_setting->id ); + } + + /** + * @ticket 34596 + */ + function test_add_panel_return_instance() { + $manager = new WP_Customize_Manager(); + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + + $panel_id = 'foo-panel'; + $result_panel = $manager->add_panel( $panel_id, array( + 'title' => 'Test Panel', + 'priority' => 2, + ) ); + + $this->assertInstanceOf( 'WP_Customize_Panel', $result_panel ); + $this->assertEquals( $panel_id, $result_panel->id ); + + $panel = new WP_Customize_Panel( $manager, $panel_id, array( + 'title' => 'Test Panel 2', + ) ); + $result_panel = $manager->add_panel( $panel ); + + $this->assertInstanceOf( 'WP_Customize_Panel', $result_panel ); + $this->assertEquals( $panel, $result_panel ); + $this->assertEquals( $panel_id, $result_panel->id ); + } + + /** + * @ticket 34596 + */ + function test_add_control_return_instance() { + $manager = new WP_Customize_Manager(); + $section_id = 'foo-section'; + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + $manager->add_section( $section_id, array( + 'title' => 'Section', + 'priority' => 1, + ) ); + + $control_id = 'foo-control'; + $manager->add_setting( $control_id ); + + $result_control = $manager->add_control( $control_id, array( + 'section' => $section_id, + 'priority' => 1, + 'setting' => $control_id, + ) ); + $this->assertInstanceOf( 'WP_Customize_Control', $result_control ); + $this->assertEquals( $control_id, $result_control->id ); + + $control = new WP_Customize_Control( $manager, $control_id, array( + 'section' => $section_id, + 'priority' => 1, + 'setting' => $control_id, + ) ); + $result_control = $manager->add_control( $control ); + + $this->assertInstanceOf( 'WP_Customize_Control', $result_control ); + $this->assertEquals( $control, $result_control ); + $this->assertEquals( $control_id, $result_control->id ); + } }