Last active
December 11, 2016 05:24
Star
You must be signed in to star a gist
Return class for magic WordPress filter callback methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This class is designed to return a basic value by parsing the dynamic | |
* static method name you call to determine its type and value. | |
* | |
* It's useful for filtering values in WordPress quickly without having | |
* to create a callback function. Think of this as a continuation of the | |
* callback helpers already in WordPress: __return_true(), et al. | |
* | |
* EXAMPLES | |
* add_filter( 'twentyseventeen_front_page_sections', 'Returner::int__4' ); | |
* add_filter( 'site_icon_sizes', 'Returner::array__256__128__64' ); | |
* add_filter( 'post_class', 'Returner::featured' ); | |
* | |
* @author Frankie Jarrett | |
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 | |
*/ | |
class Returner { | |
/** | |
* Magic value returner method. | |
* | |
* @access public | |
* @static | |
* | |
* @param string $name | |
* @param array $args (unused) | |
* | |
* @return mixed | |
*/ | |
public static function __callStatic( $name, $args ) { | |
/** | |
* Return an integer. | |
* | |
* Returner::int__6() ==> 6 | |
*/ | |
if ( 0 === strpos( $name, 'int__' ) ) { | |
return (int) self::value( $name ); | |
} | |
/** | |
* Return a negative integer. | |
* | |
* Returner::nint__6() ==> -6 | |
*/ | |
if ( 0 === strpos( $name, 'nint__' ) ) { | |
return (int) self::value( $name ) * -1; | |
} | |
/** | |
* Return a floating point number. | |
* | |
* Returner::float__1_234() ==> 1.234 | |
*/ | |
if ( 0 === strpos( $name, 'float__' ) ) { | |
return (float) str_replace( '_', '.', self::value( $name ) ); | |
} | |
/** | |
* Return a negative floating point number. | |
* | |
* Returner::nfloat__1_234() ==> -1.234 | |
*/ | |
if ( 0 === strpos( $name, 'nfloat__' ) ) { | |
return (float) str_replace( '_', '.', self::value( $name ) ) * -1; | |
} | |
/** | |
* Return a one-dimensional indexed array. | |
* | |
* Returner::array__foo__bar_baz() ==> [ 'foo', 'bar baz' ] | |
* Returner::array__1__foo__3() ==> [ 1, 'foo', 3 ] | |
* Returner::array__foo__FALSE__bar ==> [ 'foo', false, 'baz' ] | |
*/ | |
if ( 0 === strpos( $name, 'array__' ) ) { | |
$array = []; | |
foreach ( explode( '__', self::value( $name ) ) as $value ) { | |
if ( 'TRUE' === $value ) { | |
$array[] = true; | |
continue; | |
} | |
if ( 'FALSE' === $value ) { | |
$array[] = false; | |
continue; | |
} | |
$value = trim( str_replace( '_', ' ', $value ) ); | |
$array[] = is_numeric( $value ) ? (int) $value : $value; | |
} | |
return $array; | |
} | |
/** | |
* Return a string. | |
* | |
* Returner::foo_bar_baz() ==> 'foo bar baz' | |
*/ | |
return trim( str_replace( '_', ' ', $name ) ); | |
} | |
/** | |
* Return the value portion of a method name. | |
* | |
* @access public | |
* @static | |
* | |
* @param string $name | |
* | |
* @return string | |
*/ | |
public static function value( $name ) { | |
return preg_replace( '/^[a-z]+__/', '', $name ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment