Changeset 57562
- Timestamp:
- 02/08/2024 08:55:18 AM (10 months ago)
- Location:
- trunk
- Files:
-
- 1 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-bindings.php
r57560 r57562 89 89 * the block's original value, null, false to remove an attribute, etc. 90 90 * } 91 * @return array|false Source when the registration was successful, or `false` on failure.91 * @return WP_Block_Bindings_Source|false Source when the registration was successful, or `false` on failure. 92 92 */ 93 93 function register_block_bindings_source( string $source_name, array $source_properties ) { … … 101 101 * 102 102 * @param string $source_name Block bindings source name including namespace. 103 * @return array|false The unregistered block bindings source on success and `false` otherwise.103 * @return WP_Block_Bindings_Source|false The unregistered block bindings source on success and `false` otherwise. 104 104 */ 105 105 function unregister_block_bindings_source( string $source_name ) { … … 112 112 * @since 6.5.0 113 113 * 114 * @return arrayThe array of registered block bindings sources.114 * @return WP_Block_Bindings_Source[] The array of registered block bindings sources. 115 115 */ 116 116 function get_all_registered_block_bindings_sources() { … … 124 124 * 125 125 * @param string $source_name The name of the source. 126 * @return array|null The registered block bindings source, or `null` if it is not registered.126 * @return WP_Block_Bindings_Source|null The registered block bindings source, or `null` if it is not registered. 127 127 */ 128 128 function get_block_bindings_source( string $source_name ) { -
trunk/src/wp-includes/class-wp-block-bindings-registry.php
r57560 r57562 21 21 * 22 22 * @since 6.5.0 23 * @var array23 * @var WP_Block_Bindings_Source[] 24 24 */ 25 25 private $sources = array(); … … 67 67 * the block's original value, null, false to remove an attribute, etc. 68 68 * } 69 * @return array|false Source when the registration was successful, or `false` on failure.69 * @return WP_Block_Bindings_Source|false Source when the registration was successful, or `false` on failure. 70 70 */ 71 71 public function register( string $source_name, array $source_properties ) { … … 108 108 } 109 109 110 $source = array_merge( 111 array( 'name' => $source_name ), 110 /* Validate that the source properties contain the label */ 111 if ( ! isset( $source_properties['label'] ) ) { 112 _doing_it_wrong( 113 __METHOD__, 114 __( 'The $source_properties must contain a "label".' ), 115 '6.5.0' 116 ); 117 return false; 118 } 119 120 /* Validate that the source properties contain the get_value_callback */ 121 if ( ! isset( $source_properties['get_value_callback'] ) ) { 122 _doing_it_wrong( 123 __METHOD__, 124 __( 'The $source_properties must contain a "get_value_callback".' ), 125 '6.5.0' 126 ); 127 return false; 128 } 129 130 /* Validate that the get_value_callback is a valid callback */ 131 if ( ! is_callable( $source_properties['get_value_callback'] ) ) { 132 _doing_it_wrong( 133 __METHOD__, 134 __( 'The "get_value_callback" parameter must be a valid callback.' ), 135 '6.5.0' 136 ); 137 return false; 138 } 139 140 $source = new WP_Block_Bindings_Source( 141 $source_name, 112 142 $source_properties 113 143 ); … … 124 154 * 125 155 * @param string $source_name Block bindings source name including namespace. 126 * @return array|false The unregistered block bindings source on success and `false` otherwise.156 * @return WP_Block_Bindings_Source|false The unregistered block bindings source on success and `false` otherwise. 127 157 */ 128 158 public function unregister( string $source_name ) { … … 148 178 * @since 6.5.0 149 179 * 150 * @return arrayThe array of registered sources.180 * @return WP_Block_Bindings_Source[] The array of registered sources. 151 181 */ 152 182 public function get_all_registered() { … … 160 190 * 161 191 * @param string $source_name The name of the source. 162 * @return array|null The registered block bindings source, or `null` if it is not registered.192 * @return WP_Block_Bindings_Source|null The registered block bindings source, or `null` if it is not registered. 163 193 */ 164 194 public function get_registered( string $source_name ) { -
trunk/src/wp-includes/class-wp-block.php
r57561 r57562 271 271 } 272 272 273 $source_callback = $block_binding_source['get_value_callback']; 274 $source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array(); 275 $source_value = call_user_func_array( $source_callback, array( $source_args, $this, $attribute_name ) ); 273 $source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array(); 274 $source_value = $block_binding_source->get_value( $source_args, $this, $attribute_name ); 276 275 277 276 // If the value is not null, process the HTML based on the block and the attribute. -
trunk/src/wp-settings.php
r57548 r57562 333 333 require ABSPATH . WPINC . '/sitemaps/providers/class-wp-sitemaps-taxonomies.php'; 334 334 require ABSPATH . WPINC . '/sitemaps/providers/class-wp-sitemaps-users.php'; 335 require ABSPATH . WPINC . '/class-wp-block-bindings-source.php'; 335 336 require ABSPATH . WPINC . '/class-wp-block-bindings-registry.php'; 336 337 require ABSPATH . WPINC . '/class-wp-block-editor-context.php'; -
trunk/tests/phpunit/tests/block-bindings/register.php
r57526 r57562 12 12 class Tests_Block_Bindings_Register extends WP_UnitTestCase { 13 13 14 const TEST_SOURCE_NAME = 'test/source'; 15 const TEST_SOURCE_PROPERTIES = array( 16 'label' => 'Test source', 17 ); 14 public static $test_source_name = 'test/source'; 15 public static $test_source_properties = array(); 16 17 /** 18 * Set up before each test. 19 * 20 * @since 6.5.0 21 */ 22 public function set_up() { 23 parent::set_up(); 24 25 self::$test_source_properties = array( 26 'label' => 'Test source', 27 'get_value_callback' => function () { 28 return 'test-value'; 29 }, 30 ); 31 } 18 32 19 33 /** … … 40 54 * @covers ::get_all_registered_block_bindings_sources 41 55 * @covers ::get_block_bindings_source 56 * @covers WP_Block_Bindings_Source::__construct 42 57 */ 43 58 public function test_get_all_registered() { 44 59 $source_one_name = 'test/source-one'; 45 $source_one_properties = self:: TEST_SOURCE_PROPERTIES;60 $source_one_properties = self::$test_source_properties; 46 61 register_block_bindings_source( $source_one_name, $source_one_properties ); 47 62 48 63 $source_two_name = 'test/source-two'; 49 $source_two_properties = self:: TEST_SOURCE_PROPERTIES;64 $source_two_properties = self::$test_source_properties; 50 65 register_block_bindings_source( $source_two_name, $source_two_properties ); 51 66 52 67 $source_three_name = 'test/source-three'; 53 $source_three_properties = self:: TEST_SOURCE_PROPERTIES;68 $source_three_properties = self::$test_source_properties; 54 69 register_block_bindings_source( $source_three_name, $source_three_properties ); 55 70 56 71 $expected = array( 57 $source_one_name => array_merge( array( 'name' => $source_one_name ), $source_one_properties ),58 $source_two_name => array_merge( array( 'name' => $source_two_name ), $source_two_properties ),59 $source_three_name => array_merge( array( 'name' => $source_three_name ), $source_three_properties ),72 $source_one_name => new WP_Block_Bindings_Source( $source_one_name, $source_one_properties ), 73 $source_two_name => new WP_Block_Bindings_Source( $source_two_name, $source_two_properties ), 74 $source_three_name => new WP_Block_Bindings_Source( $source_three_name, $source_three_properties ), 60 75 'core/post-meta' => get_block_bindings_source( 'core/post-meta' ), 61 76 'core/pattern-overrides' => get_block_bindings_source( 'core/pattern-overrides' ), … … 73 88 * @covers ::register_block_bindings_source 74 89 * @covers ::unregister_block_bindings_source 90 * @covers WP_Block_Bindings_Source::__construct 75 91 */ 76 92 public function test_unregister_block_source() { 77 register_block_bindings_source( self:: TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES);93 register_block_bindings_source( self::$test_source_name, self::$test_source_properties ); 78 94 79 $result = unregister_block_bindings_source( self:: TEST_SOURCE_NAME);80 $this->assert Same(81 array_merge(82 array( 'name' => self::TEST_SOURCE_NAME ),83 self:: TEST_SOURCE_PROPERTIES95 $result = unregister_block_bindings_source( self::$test_source_name ); 96 $this->assertEquals( 97 new WP_Block_Bindings_Source( 98 self::$test_source_name, 99 self::$test_source_properties 84 100 ), 85 101 $result -
trunk/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php
r57375 r57562 14 14 class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase { 15 15 16 const TEST_SOURCE_NAME = 'test/source'; 17 const TEST_SOURCE_PROPERTIES = array( 18 'label' => 'Test source', 19 ); 16 public static $test_source_name = 'test/source'; 17 public static $test_source_properties = array(); 20 18 21 19 /** … … 36 34 37 35 $this->registry = new WP_Block_Bindings_Registry(); 36 37 self::$test_source_properties = array( 38 'label' => 'Test source', 39 'get_value_callback' => function () { 40 return 'test-value'; 41 }, 42 ); 38 43 } 39 44 … … 59 64 */ 60 65 public function test_register_invalid_non_string_names() { 61 $result = $this->registry->register( 1, self:: TEST_SOURCE_PROPERTIES);66 $result = $this->registry->register( 1, self::$test_source_properties ); 62 67 $this->assertFalse( $result ); 63 68 } … … 73 78 */ 74 79 public function test_register_invalid_names_without_namespace() { 75 $result = $this->registry->register( 'post-meta', self:: TEST_SOURCE_PROPERTIES);80 $result = $this->registry->register( 'post-meta', self::$test_source_properties ); 76 81 $this->assertFalse( $result ); 77 82 } … … 101 106 */ 102 107 public function test_register_invalid_uppercase_characters() { 103 $result = $this->registry->register( 'Core/PostMeta', self::TEST_SOURCE_PROPERTIES ); 108 $result = $this->registry->register( 'Core/PostMeta', self::$test_source_properties ); 109 $this->assertFalse( $result ); 110 } 111 112 /** 113 * Should reject block bindings registration without a label. 114 * 115 * @ticket 60282 116 * 117 * @covers WP_Block_Bindings_Registry::register 118 * 119 * @expectedIncorrectUsage WP_Block_Bindings_Registry::register 120 */ 121 public function test_register_invalid_missing_label() { 122 123 // Remove the label from the properties. 124 unset( self::$test_source_properties['label'] ); 125 126 $result = $this->registry->register( self::$test_source_name, self::$test_source_properties ); 127 $this->assertFalse( $result ); 128 } 129 130 /** 131 * Should reject block bindings registration without a get_value_callback. 132 * 133 * @ticket 60282 134 * 135 * @covers WP_Block_Bindings_Registry::register 136 * 137 * @expectedIncorrectUsage WP_Block_Bindings_Registry::register 138 */ 139 public function test_register_invalid_missing_get_value_callback() { 140 141 // Remove the get_value_callback from the properties. 142 unset( self::$test_source_properties['get_value_callback'] ); 143 144 $result = $this->registry->register( self::$test_source_name, self::$test_source_properties ); 145 $this->assertFalse( $result ); 146 } 147 148 /** 149 * Should reject block bindings registration if `get_value_callback` is not a callable. 150 * 151 * @ticket 60282 152 * 153 * @covers WP_Block_Bindings_Registry::register 154 * 155 * @expectedIncorrectUsage WP_Block_Bindings_Registry::register 156 */ 157 public function test_register_invalid_incorrect_callback_type() { 158 159 self::$test_source_properties['get_value_callback'] = 'not-a-callback'; 160 161 $result = $this->registry->register( self::$test_source_name, self::$test_source_properties ); 104 162 $this->assertFalse( $result ); 105 163 } … … 111 169 * 112 170 * @covers WP_Block_Bindings_Registry::register 171 * @covers WP_Block_Bindings_Source::__construct 113 172 */ 114 173 public function test_register_block_binding_source() { 115 $result = $this->registry->register( self:: TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES);116 $this->assert Same(117 array_merge(118 array( 'name' => self::TEST_SOURCE_NAME ),119 self:: TEST_SOURCE_PROPERTIES174 $result = $this->registry->register( self::$test_source_name, self::$test_source_properties ); 175 $this->assertEquals( 176 new WP_Block_Bindings_Source( 177 self::$test_source_name, 178 self::$test_source_properties 120 179 ), 121 180 $result … … 144 203 * @covers WP_Block_Bindings_Registry::register 145 204 * @covers WP_Block_Bindings_Registry::unregister 205 * WP_Block_Bindings_Source::__construct 146 206 */ 147 207 public function test_unregister_block_source() { 148 $this->registry->register( self:: TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES);149 150 $result = $this->registry->unregister( self:: TEST_SOURCE_NAME);151 $this->assert Same(152 array_merge(153 array( 'name' => self::TEST_SOURCE_NAME ),154 self:: TEST_SOURCE_PROPERTIES208 $this->registry->register( self::$test_source_name, self::$test_source_properties ); 209 210 $result = $this->registry->unregister( self::$test_source_name ); 211 $this->assertEquals( 212 new WP_Block_Bindings_Source( 213 self::$test_source_name, 214 self::$test_source_properties 155 215 ), 156 216 $result … … 165 225 * @covers WP_Block_Bindings_Registry::register 166 226 * @covers WP_Block_Bindings_Registry::get_all_registered 227 * WP_Block_Bindings_Source::__construct 167 228 */ 168 229 public function test_get_all_registered() { 169 230 $source_one_name = 'test/source-one'; 170 $source_one_properties = self:: TEST_SOURCE_PROPERTIES;231 $source_one_properties = self::$test_source_properties; 171 232 $this->registry->register( $source_one_name, $source_one_properties ); 172 233 173 234 $source_two_name = 'test/source-two'; 174 $source_two_properties = self:: TEST_SOURCE_PROPERTIES;235 $source_two_properties = self::$test_source_properties; 175 236 $this->registry->register( $source_two_name, $source_two_properties ); 176 237 177 238 $source_three_name = 'test/source-three'; 178 $source_three_properties = self:: TEST_SOURCE_PROPERTIES;239 $source_three_properties = self::$test_source_properties; 179 240 $this->registry->register( $source_three_name, $source_three_properties ); 180 241 181 242 $expected = array( 182 $source_one_name => array_merge( array( 'name' => $source_one_name ), $source_one_properties ),183 $source_two_name => array_merge( array( 'name' => $source_two_name ), $source_two_properties ),184 $source_three_name => array_merge( array( 'name' => $source_three_name ), $source_three_properties ),243 $source_one_name => new WP_Block_Bindings_Source( $source_one_name, $source_one_properties ), 244 $source_two_name => new WP_Block_Bindings_Source( $source_two_name, $source_two_properties ), 245 $source_three_name => new WP_Block_Bindings_Source( $source_three_name, $source_three_properties ), 185 246 ); 186 247 187 248 $registered = $this->registry->get_all_registered(); 188 $this->assert Same( $expected, $registered );249 $this->assertEquals( $expected, $registered ); 189 250 } 190 251 … … 198 259 */ 199 260 public function test_get_registered_rejects_unknown_source_name() { 200 $this->registry->register( self:: TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES);261 $this->registry->register( self::$test_source_name, self::$test_source_properties ); 201 262 202 263 $source = $this->registry->get_registered( 'test/unknown-source' ); … … 211 272 * @covers WP_Block_Bindings_Registry::register 212 273 * @covers WP_Block_Bindings_Registry::get_registered 274 * @covers WP_Block_Bindings_Source::__construct 213 275 */ 214 276 public function test_get_registered() { 215 277 $source_one_name = 'test/source-one'; 216 $source_one_properties = self:: TEST_SOURCE_PROPERTIES;278 $source_one_properties = self::$test_source_properties; 217 279 $this->registry->register( $source_one_name, $source_one_properties ); 218 280 219 281 $source_two_name = 'test/source-two'; 220 $source_two_properties = self:: TEST_SOURCE_PROPERTIES;282 $source_two_properties = self::$test_source_properties; 221 283 $this->registry->register( $source_two_name, $source_two_properties ); 222 284 223 285 $source_three_name = 'test/source-three'; 224 $source_three_properties = self:: TEST_SOURCE_PROPERTIES;286 $source_three_properties = self::$test_source_properties; 225 287 $this->registry->register( $source_three_name, $source_three_properties ); 226 288 227 $result = $this->registry->get_registered( 'test/source-two' ); 228 $this->assertSame( 229 array_merge( 230 array( 'name' => $source_two_name ), 231 $source_two_properties 232 ), 289 $expected = new WP_Block_Bindings_Source( $source_two_name, $source_two_properties ); 290 $result = $this->registry->get_registered( 'test/source-two' ); 291 292 $this->assertEquals( 293 $expected, 233 294 $result 234 295 ); … … 256 317 */ 257 318 public function test_is_registered_for_known_source() { 258 $this->registry->register( self:: TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES);259 260 $result = $this->registry->is_registered( self:: TEST_SOURCE_NAME);319 $this->registry->register( self::$test_source_name, self::$test_source_properties ); 320 321 $result = $this->registry->is_registered( self::$test_source_name ); 261 322 $this->assertTrue( $result ); 262 323 }
Note: See TracChangeset
for help on using the changeset viewer.