Skip to content

Instantly share code, notes, and snippets.

@kurtpayne
Created June 20, 2012 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtpayne/2962496 to your computer and use it in GitHub Desktop.
Save kurtpayne/2962496 to your computer and use it in GitHub Desktop.
wp_constant
<?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");
}
}
@kurtpayne
Copy link
Author

Need to figure out "UNIT_TEST_MODE" and appropriate WP_Error code (or alternate error handling)

@kurtpayne
Copy link
Author

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