{"id":29651272,"url":"https://github.com/arraypress/wp-constant-utils","last_synced_at":"2025-07-29T18:10:11.923Z","repository":{"id":302904468,"uuid":"1013790808","full_name":"arraypress/wp-constant-utils","owner":"arraypress","description":"A lean WordPress library for managing and defining constants safely.","archived":false,"fork":false,"pushed_at":"2025-07-05T21:29:25.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-22T05:06:30.158Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arraypress.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-04T13:20:36.000Z","updated_at":"2025-07-05T21:29:28.000Z","dependencies_parsed_at":"2025-07-04T17:48:22.580Z","dependency_job_id":"85abe144-6c12-4b56-a6d2-55b59a88ae63","html_url":"https://github.com/arraypress/wp-constant-utils","commit_stats":null,"previous_names":["arraypress/wp-constant-utils"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/arraypress/wp-constant-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-constant-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-constant-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-constant-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-constant-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arraypress","download_url":"https://codeload.github.com/arraypress/wp-constant-utils/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-constant-utils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267730574,"owners_count":24135546,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-07-22T05:06:30.028Z","updated_at":"2025-07-29T18:10:11.884Z","avatar_url":"https://github.com/arraypress.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WordPress Constant Utils\n\nA simple WordPress library for safely managing constants in plugins and themes. No more scattered `define()` calls or worrying about overwriting existing constants.\n\n## Installation\n\n```bash\ncomposer require arraypress/wp-constant-utils\n```\n\n## Quick Start\n\n```php\n// Global functions (no imports needed)\nwp_define_constant( 'API_KEY', 'secret-123' );\nwp_setup_plugin_constants( 'MYPLUGIN', __FILE__, '1.0.0' );\n$api_key = wp_get_constant( 'API_KEY', 'default-key' );\n\n// Or use the class directly\nuse ArrayPress\\ConstantUtils\\Constant;\n\nConstant::define( 'API_KEY', 'secret-123' );\nConstant::setup_plugin( 'MYPLUGIN', __FILE__, '1.0.0' );\n$api_key = Constant::get( 'API_KEY', 'default-key' );\n```\n\n## Why Use This?\n\n**Before:**\n```php\n// Scattered throughout your plugin\ndefine( 'MYPLUGIN_VERSION', '1.0.0' );\ndefine( 'MYPLUGIN_PLUGIN_FILE', __FILE__ );\ndefine( 'MYPLUGIN_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );\ndefine( 'MYPLUGIN_PLUGIN_URL', plugin_dir_url( __FILE__ ) );\ndefine( 'MYPLUGIN_DEBUG', WP_DEBUG );\n\n// Oops, accidentally overwrite existing constants\ndefine( 'API_KEY', 'new-value' ); // Might overwrite existing value\n```\n\n**After:**\n```php\n// Clean, safe, and organized (choose your style)\n\n// Global functions (recommended)\nwp_setup_plugin_constants( 'MYPLUGIN', __FILE__, '1.0.0' );\nwp_define_constant( 'API_KEY', 'secret-key' ); // Only defines if not already set\n\n// Or class methods\nConstant::setup_plugin( 'MYPLUGIN', __FILE__, '1.0.0' );\nConstant::define( 'API_KEY', 'secret-key' );\n```\n\n## Essential Methods\n\n### Plugin Setup\n\n**Set up all standard plugin constants:**\n```php\n// Global function (WordPress-style)\nwp_setup_plugin_constants( 'MYPLUGIN', __FILE__, '1.2.3' );\n\n// Or class method\nConstant::setup_plugin( 'MYPLUGIN', __FILE__, '1.2.3' );\n\n// Creates:\n// MYPLUGIN_PLUGIN_VERSION = '1.2.3'\n// MYPLUGIN_PLUGIN_FILE = '/path/to/plugin.php'\n// MYPLUGIN_PLUGIN_DIR = '/path/to/plugin/'\n// MYPLUGIN_PLUGIN_URL = 'https://site.com/wp-content/plugins/myplugin/'\n// MYPLUGIN_PLUGIN_BASE = 'myplugin/myplugin.php'\n```\n\n**Add debug constants:**\n```php\nConstant::setup_debug( 'MYPLUGIN' );\n\n// Creates based on WordPress settings:\n// MYPLUGIN_DEBUG = true/false\n// MYPLUGIN_DEBUG_LOG = true/false\n// MYPLUGIN_SCRIPT_DEBUG = true/false\n```\n\n### Safe Definition\n\n**Define constants safely:**\n```php\n// Global function\nwp_define_constant( 'API_KEY', 'secret-123' ); // Returns true if defined\n\n// Or class method\nConstant::define( 'API_KEY', 'secret-123' );\n\n// Define multiple at once\nConstant::define_multiple( [\n\t'API_URL'     =\u003e 'https://api.example.com',\n\t'API_TIMEOUT' =\u003e 30,\n\t'CACHE_TIME'  =\u003e 3600\n] );\n```\n\n### Smart Retrieval\n\n**Get constants with fallbacks:**\n```php\n// Global function\n$api_key = wp_get_constant( 'API_KEY', 'fallback-key' );\n\n// Or class method\n$api_key = Constant::get( 'API_KEY', 'fallback-key' );\n\n// Get multiple constants\n$config = Constant::get_multiple( [ 'API_KEY', 'API_URL' ], 'default' );\n// Returns: ['API_KEY' =\u003e 'value1', 'API_URL' =\u003e 'value2']\n\n// Get all plugin constants\n$all_constants = Constant::get_all_with_prefix( 'MYPLUGIN_' );\n```\n\n### Environment Configuration\n\n**Different settings per environment:**\n```php\n$config = [\n\t'development' =\u003e [\n\t\t'API_URL'    =\u003e 'https://dev-api.example.com',\n\t\t'DEBUG_MODE' =\u003e true\n\t],\n\t'production'  =\u003e [\n\t\t'API_URL'    =\u003e 'https://api.example.com',\n\t\t'DEBUG_MODE' =\u003e false\n\t]\n];\n\nConstant::setup_environment( 'MYPLUGIN', $config );\n// Automatically uses the right config based on wp_get_environment_type()\n```\n\n### Validation\n\n**Check if constants exist:**\n```php\n// Single constant\nif ( Constant::is_defined( 'API_KEY' ) ) {\n\t// Do something\n}\n\n// Check multiple required constants\n$required = [ 'DB_NAME', 'DB_USER', 'DB_PASSWORD' ];\nif ( Constant::all_defined( $required ) ) {\n\t// All database constants are set\n}\n\n// Check if any caching is configured\n$cache_options = [ 'REDIS_HOST', 'MEMCACHED_HOST' ];\nif ( Constant::any_defined( $cache_options ) ) {\n\t// Some form of caching is available\n}\n```\n\n## Real-World Example\n\n```php\n\u003c?php\n/**\n * Plugin Name: My Awesome Plugin\n * Version: 1.2.3\n */\n\n// Global functions (recommended for simplicity)\nwp_setup_plugin_constants( 'MYAWESOMEPLUGIN', __FILE__, '1.2.3' );\n\n// Or using the class (for advanced usage)\nuse ArrayPress\\ConstantUtils\\Constant;\n\nConstant::setup_debug( 'MYAWESOMEPLUGIN' );\n\n// Environment-specific settings\n$env_config = [\n\t'development' =\u003e [\n\t\t'API_URL'       =\u003e 'https://dev-api.example.com',\n\t\t'CACHE_ENABLED' =\u003e false\n\t],\n\t'production'  =\u003e [\n\t\t'API_URL'       =\u003e 'https://api.example.com',\n\t\t'CACHE_ENABLED' =\u003e true\n\t]\n];\nConstant::setup_environment( 'MYAWESOMEPLUGIN', $env_config );\n\n// Additional plugin settings\nConstant::setup_additional( 'MYAWESOMEPLUGIN', [\n\t'CACHE_TIMEOUT' =\u003e 3600,\n\t'MAX_RETRIES'   =\u003e 3\n] );\n\n// Validate setup\nif ( ! Constant::all_defined( [ 'MYAWESOMEPLUGIN_PLUGIN_VERSION', 'MYAWESOMEPLUGIN_API_URL' ] ) ) {\n\twp_die( 'Plugin setup failed!' );\n}\n\n// Use throughout your plugin (both styles work)\n$api_url    = wp_get_constant( 'MYAWESOMEPLUGIN_API_URL' );\n$is_debug   = Constant::get( 'MYAWESOMEPLUGIN_DEBUG', false );\n$cache_time = wp_get_constant( 'MYAWESOMEPLUGIN_CACHE_TIMEOUT', 1800 );\n```\n\n## Configuration File Pattern\n\n**Create a config file for complex setups:**\n\n```php\n// config/constants.php\nreturn [\n\t'development' =\u003e [\n\t\t'API_URL'       =\u003e 'https://dev-api.example.com',\n\t\t'DEBUG_QUERIES' =\u003e true,\n\t\t'CACHE_ENABLED' =\u003e false,\n\t\t'LOG_LEVEL'     =\u003e 'debug'\n\t],\n\t'staging'     =\u003e [\n\t\t'API_URL'       =\u003e 'https://staging-api.example.com',\n\t\t'DEBUG_QUERIES' =\u003e false,\n\t\t'CACHE_ENABLED' =\u003e true,\n\t\t'LOG_LEVEL'     =\u003e 'info'\n\t],\n\t'production'  =\u003e [\n\t\t'API_URL'       =\u003e 'https://api.example.com',\n\t\t'DEBUG_QUERIES' =\u003e false,\n\t\t'CACHE_ENABLED' =\u003e true,\n\t\t'LOG_LEVEL'     =\u003e 'error'\n\t]\n];\n\n// In your plugin\n$config = include plugin_dir_path( __FILE__ ) . 'config/constants.php';\nConstant::setup_environment( 'MYPLUGIN', $config );\n```\n\n## Global Functions\n\nThe library provides convenient WordPress-style functions:\n\n| Function | Description |\n|----------|-------------|\n| `wp_define_constant($name, $value)` | Safely define a constant |\n| `wp_get_constant($name, $default)` | Get constant with fallback |\n| `wp_setup_plugin_constants($prefix, $file, $version)` | Set up plugin constants |\n\n## All Class Methods\n\n| Method | Description |\n|--------|-------------|\n| `define($name, $value)` | Safely define a constant |\n| `define_multiple($constants)` | Define multiple constants |\n| `get($name, $default)` | Get constant with fallback |\n| `get_multiple($names, $default)` | Get multiple constants |\n| `get_all_with_prefix($prefix)` | Get all constants with prefix |\n| `setup_plugin($prefix, $file, $version)` | Set up plugin constants |\n| `setup_environment($prefix, $config)` | Environment-specific constants |\n| `setup_debug($prefix)` | WordPress debug constants |\n| `setup_additional($prefix, $constants)` | Additional prefixed constants |\n| `is_defined($name)` | Check if constant exists |\n| `is_equal($name, $value)` | Check constant value |\n| `all_defined($names)` | Check if all constants exist |\n| `any_defined($names)` | Check if any constants exist |\n| `export($prefix)` | Export constants for debugging |\n\n## Requirements\n\n- PHP 7.4+\n- WordPress 5.0+\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the GPL-2.0-or-later License.\n\n## Support\n\n- [Documentation](https://github.com/arraypress/wp-constant-utils)\n- [Issue Tracker](https://github.com/arraypress/wp-constant-utils/issues)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farraypress%2Fwp-constant-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farraypress%2Fwp-constant-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farraypress%2Fwp-constant-utils/lists"}