Smilies: Add the `smilies` filter.

This new filter allows the smilies array to be modified with a filter, instead of having to directly access the global.

Props mte90, jorbin.
Fixes #35905.



git-svn-id: https://develop.svn.wordpress.org/trunk@38504 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2016-09-01 23:59:59 +00:00
parent 770a150b42
commit 5b85aa163f
2 changed files with 54 additions and 0 deletions

View File

@ -3342,6 +3342,18 @@ function smilies_init() {
);
}
/**
* Filter all the smilies.
*
* This filter must be added before `smilies_init` is run, as
* it is normally only run once to setup the smilies regex.
*
* @since 4.6.0
*
* @param array $wpsmiliestrans List of the smilies.
*/
$wpsmiliestrans = apply_filters('smilies', $wpsmiliestrans);
if (count($wpsmiliestrans) == 0) {
return;
}

View File

@ -312,4 +312,46 @@ class Tests_Formatting_Smilies extends WP_UnitTestCase {
// standard smilies, use_smilies: OFF
update_option( 'use_smilies', 0 );
}
/**
* Test to ensure smilies can be removed with a filter
*
* @ticket 35905
*/
public function test_smilies_filter_removes_smilies() {
add_filter( 'smilies', array( $this, '_filter_remove_smilies' ) );
smilies_init();
remove_filter( 'smilies', array( $this, '_filter_remove_smilies' ) );
$txt = ':oops: I did it again';
$this->assertEquals( $txt, convert_smilies( $txt ) );
}
/**
* Test to ensure smilies can be added with a filter
*
* @ticket 35905
*/
public function test_smilies_filter_adds_smilies() {
add_filter( 'smilies', array( $this, '_filter_add_smilies' ) );
smilies_init();
remove_filter( 'smilies', array( $this, '_filter_add_smilies' ) );
$txt = 'You played with my <3';
$expected_txt = 'You played with my \xe2\x9d\xa4';
$this->assertEquals( $expected_txt, convert_smilies( $txt ) );
}
public function _filter_remove_smilies( $wpsmiliestrans ) {
unset( $wpsmiliestrans[':oops:'] );
return $wpsmiliestrans;
}
public function _filter_add_smilies( $wpsmiliestrans ) {
$wpsmiliestrans['<3'] = '\xe2\x9d\xa4';
return $wpsmiliestrans;
}
}