Created
June 20, 2012 22:01
-
-
Save kurtpayne/2962496 to your computer and use it in GitHub Desktop.
wp_constant
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 | |
$wp_constant_mgr = new class WP_Constants_Manager(); | |
function wp_constant( $name, $value ) { | |
global $wp_constant_mgr; | |
if ( UNIT_TEST_MODE ) | |
$wp_constant_mgr->set( $name, $value ); | |
else | |
define( $name, $value ); | |
} | |
function wp_defined( $name ) { | |
global $wp_constant_mgr; | |
if ( UNIT_TEST_MODE ) | |
return $wp_constant_mgr->get( $name ); | |
else | |
return defined( $name ); | |
} | |
final class WP_Constants_Manager { | |
private $_constants = array(); | |
public function get( $name ) { | |
return $this->_constants[ $name ] ?: false; | |
} | |
public function unset( $name ) { | |
unset $this->_constants[ $name ]; | |
} | |
public function set( $name, $value ) { | |
if ( !isset( $this->_constants[ $name ] ) ) | |
$this->_constants[ $name ] = $value; | |
else | |
return WP_Error(0, "Constant '$name' already defined"); | |
} | |
} |
This doesn't account for pure constant usage (e.g. as a variable) either.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need to figure out "UNIT_TEST_MODE" and appropriate WP_Error code (or alternate error handling)