| 330 | |
| 331 | |
| 332 | // Store passed in shortcode_atts_{$shortcode} args |
| 333 | function _filter_atts( $out, $pairs, $atts ) { |
| 334 | $this->filter_atts_out = $out; |
| 335 | $this->filter_atts_pairs = $pairs; |
| 336 | $this->filter_atts_atts = $atts; |
| 337 | return $out; |
| 338 | } |
| 339 | |
| 340 | // Filter shortcode atts in various ways |
| 341 | function _filter_atts2( $out, $pairs, $atts ) { |
| 342 | // If foo attribute equals "foo1", change it to be default value |
| 343 | if ( isset( $out['foo'] ) && 'foo1' == $out['foo'] ) |
| 344 | $out['foo'] = $pairs['foo']; |
| 345 | |
| 346 | // If baz attribute is set, remove it |
| 347 | if ( isset( $out['baz'] ) ) |
| 348 | unset( $out['baz'] ); |
| 349 | |
| 350 | $this->filter_atts_out = $out; |
| 351 | return $out; |
| 352 | } |
| 353 | |
| 354 | function test_shortcode_atts_filter_passes_original_arguments() { |
| 355 | add_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts' ), 10, 3 ); |
| 356 | |
| 357 | do_shortcode('[bartag foo="foo1" /]'); |
| 358 | $this->assertEquals( array( 'foo' => 'foo1', 'baz' => 'default baz' ), $this->filter_atts_out ); |
| 359 | $this->assertEquals( array( 'foo' => 'no foo', 'baz' => 'default baz' ), $this->filter_atts_pairs ); |
| 360 | $this->assertEquals( array( 'foo' => 'foo1' ), $this->filter_atts_atts ); |
| 361 | |
| 362 | remove_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts' ), 10, 3 ); |
| 363 | } |
| 364 | |
| 365 | function test_shortcode_atts_filtering() { |
| 366 | add_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts2' ), 10, 3 ); |
| 367 | |
| 368 | $out = do_shortcode('[bartag foo="foo1" baz="baz1" /]'); |
| 369 | $this->assertEquals( array( 'foo' => 'no foo' ), $this->filter_atts_out ); |
| 370 | $this->assertEquals( 'foo = no foo', $out ); |
| 371 | |
| 372 | $out = do_shortcode('[bartag foo="foo2" /]'); |
| 373 | $this->assertEquals( 'foo = foo2', $out ); |
| 374 | |
| 375 | remove_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts2' ), 10, 3 ); |
| 376 | } |
| 377 | |