{"id":31241625,"url":"https://github.com/arraypress/wp-cookie-utils","last_synced_at":"2025-09-23T00:12:01.378Z","repository":{"id":312920776,"uuid":"1013330634","full_name":"arraypress/wp-cookie-utils","owner":"arraypress","description":"A lean PHP library for WordPress cookie management with secure defaults, multisite support, type casting, and essential cookie operations.","archived":false,"fork":false,"pushed_at":"2025-07-05T21:51:38.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-02T21:19:35.865Z","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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-07-03T18:06:50.000Z","updated_at":"2025-07-05T21:51:41.000Z","dependencies_parsed_at":"2025-09-02T21:29:43.181Z","dependency_job_id":null,"html_url":"https://github.com/arraypress/wp-cookie-utils","commit_stats":null,"previous_names":["arraypress/wp-cookie-utils"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/arraypress/wp-cookie-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-cookie-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-cookie-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-cookie-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-cookie-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arraypress","download_url":"https://codeload.github.com/arraypress/wp-cookie-utils/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-cookie-utils/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276493432,"owners_count":25652176,"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-09-22T02:00:08.972Z","response_time":79,"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-09-23T00:11:58.279Z","updated_at":"2025-09-23T00:12:01.373Z","avatar_url":"https://github.com/arraypress.png","language":"PHP","readme":"# WordPress Cookie Utilities\n\nA lean PHP library for managing cookies in WordPress, providing secure cookie handling, multisite support, type casting, and essential cookie operations.\n\n## Features\n\n- 🔒 **Secure by Default**: Built with security best practices\n- 🌐 **Multisite Support**: Handle cookies across WordPress multisite networks\n- 🎯 **Type Casting**: Get cookie values with automatic type conversion\n- ⏱️ **Time Utilities**: Convenient methods for setting cookie expiration times\n- 🔄 **Multiple Operations**: Set and manage multiple cookies at once\n- 📦 **JSON Support**: Built-in JSON encoding and decoding\n- ⚡ **WordPress Integration**: Seamless integration with WordPress functions\n- 🔄 **Cookie Refresh**: Update expiration times without changing values\n- ✅ **Size Validation**: Automatic cookie size limit checking\n- 🎛️ **SameSite Control**: Full control over SameSite cookie attributes\n\n## Requirements\n\n- PHP 7.4 or later\n- WordPress 5.0 or later\n\n## Installation\n\nInstall via Composer:\n\n```bash\ncomposer require arraypress/wp-cookie-utils\n```\n\n## Basic Usage\n\n### Setting Cookies\n\n```php\nuse ArrayPress\\CookieUtils\\Cookie;\n\n// Set a basic cookie\nCookie::set( 'my_cookie', 'cookie_value' );\n\n// Set a cookie with expiration time\nCookie::set( 'my_cookie', 'value', Cookie::hours( 2 ) );  // Expires in 2 hours\n\n// Set a secure cookie with WordPress defaults\nCookie::set_secure( 'my_cookie', 'cookie_value' );\n```\n\n### Time Utilities\n\n```php\n// Individual time methods\nCookie::set( 'seconds_cookie', 'value', Cookie::seconds( 30 ) );  // 30 seconds\nCookie::set( 'minutes_cookie', 'value', Cookie::minutes( 15 ) );  // 15 minutes\nCookie::set( 'hours_cookie', 'value', Cookie::hours( 2 ) );      // 2 hours\nCookie::set( 'days_cookie', 'value', Cookie::days( 7 ) );        // 7 days\nCookie::set( 'weeks_cookie', 'value', Cookie::weeks( 2 ) );      // 2 weeks\nCookie::set( 'months_cookie', 'value', Cookie::months( 6 ) );    // 6 months\nCookie::set( 'years_cookie', 'value', Cookie::years( 1 ) );      // 1 year\n\n// Unified time method\nCookie::set( 'flexible_cookie', 'value', Cookie::from_now( 3, 'hours' ) );\nCookie::set( 'another_cookie', 'value', Cookie::from_now( 14, 'days' ) );\n```\n\n### Getting Cookie Values\n\n```php\n// Get a cookie value\n$value = Cookie::get( 'my_cookie' );\n\n// Get with default value if cookie doesn't exist\n$value = Cookie::get( 'my_cookie', 'default_value' );\n\n// Check if a cookie exists\nif ( Cookie::exists( 'my_cookie' ) ) {\n\t// Cookie exists\n}\n```\n\n### Type Casting\n\n```php\n// Get cookie values with automatic type casting\n$count   = Cookie::get_cast( 'item_count', 'int', 0 );\n$enabled = Cookie::get_cast( 'feature_enabled', 'bool', false );\n$tags    = Cookie::get_cast( 'selected_tags', 'array', [] );\n$price   = Cookie::get_cast( 'product_price', 'float', 0.0 );\n$name    = Cookie::get_cast( 'user_name', 'string', '' );\n\n// Supported types: 'int', 'integer', 'float', 'double', 'bool', 'boolean', 'array', 'string'\n```\n\n### Working with JSON Data\n\n```php\n// Set a cookie with JSON data\n$data = [ 'key' =\u003e 'value', 'array' =\u003e [ 1, 2, 3 ] ];\nCookie::set_json( 'json_cookie', $data );\n\n// Get and decode JSON data\n$data = Cookie::get_json( 'json_cookie', [] );\n```\n\n### Multisite Support\n\n```php\n// Set a cookie for the current site in multisite\nCookie::set_multisite( 'site_cookie', 'site_value', false ); // Site-specific\n\n// Set a network-wide cookie\nCookie::set_multisite( 'network_cookie', 'network_value', true ); // Network-wide\n```\n\n### Multiple Cookie Operations\n\n```php\n// Set multiple cookies at once\n$cookies = [\n\t'cookie1' =\u003e 'value1',\n\t'cookie2' =\u003e 'value2',\n\t'cookie3' =\u003e 'value3'\n];\nCookie::set_multiple( $cookies, Cookie::days( 1 ) );\n\n// Delete multiple cookies\n$names = [ 'cookie1', 'cookie2', 'cookie3' ];\nCookie::delete_multiple( $names );\n```\n\n### Cookie Management\n\n```php\n// Refresh a cookie's expiration without changing its value\nCookie::refresh( 'session_token', Cookie::hours( 1 ) );\n\n// Check if a cookie value is within size limits\nif ( Cookie::is_valid_size( $large_value ) ) {\n\tCookie::set( 'large_cookie', $large_value );\n}\n\n// Delete a specific cookie\nCookie::delete( 'unwanted_cookie' );\n\n// Get all cookies\n$all_cookies = Cookie::get_all();\n```\n\n## Advanced Options\n\n### Custom Cookie Options\n\n```php\n$options = [\n    'expire'   =\u003e Cookie::days( 7 ),       // 7 days\n    'path'     =\u003e '/custom/path',\n    'domain'   =\u003e 'example.com',\n    'secure'   =\u003e true,\n    'httponly' =\u003e true,\n    'samesite' =\u003e Cookie::SAMESITE_LAX     // LAX, STRICT, or NONE\n];\n\nCookie::set_secure( 'custom_cookie', 'value', $options );\n```\n\n### SameSite Attribute Options\n\n```php\n// Strict (default) - Most secure, blocks all cross-site requests\nCookie::set_secure( 'csrf_token', $token, [\n    'samesite' =\u003e Cookie::SAMESITE_STRICT\n] );\n\n// Lax - Allows some cross-site requests (good for auth cookies)\nCookie::set_secure( 'auth_token', $token, [\n    'samesite' =\u003e Cookie::SAMESITE_LAX\n] );\n\n// None - Allows all cross-site requests (requires Secure flag)\nCookie::set_secure( 'widget_state', $state, [\n    'samesite' =\u003e Cookie::SAMESITE_NONE,\n    'secure'   =\u003e true  // Required with NONE\n] );\n```\n\n## Common Use Cases\n\n### Authentication Cookies\n\n```php\n// Set login cookie with appropriate settings\nCookie::set_secure( 'user_session', $session_token, [\n    'expire'   =\u003e Cookie::hours( 24 ),\n    'samesite' =\u003e Cookie::SAMESITE_LAX,\n    'httponly' =\u003e true,\n    'secure'   =\u003e true\n] );\n```\n\n### User Preferences\n\n```php\n// Store user preferences as JSON\n$preferences = [\n    'theme' =\u003e 'dark',\n    'language' =\u003e 'en',\n    'notifications' =\u003e true\n];\nCookie::set_json( 'user_prefs', $preferences, [\n    'expire' =\u003e Cookie::months( 6 )\n] );\n\n// Retrieve preferences with type casting\n$theme = Cookie::get_cast( 'selected_theme', 'string', 'light' );\n$notifications = Cookie::get_cast( 'notifications_enabled', 'bool', true );\n```\n\n### Shopping Cart State\n\n```php\n// Store cart items\n$cart_items = [ 'item1', 'item2', 'item3' ];\nCookie::set_json( 'cart_items', $cart_items );\n\n// Get cart count\n$cart_count = Cookie::get_cast( 'cart_count', 'int', 0 );\n\n// Refresh cart session\nCookie::refresh( 'cart_session', Cookie::hours( 2 ) );\n```\n\n## Security Features\n\n- Automatic value sanitization using `wp_kses`\n- RFC-compliant cookie name validation\n- SameSite attribute enforcement (Strict by default)\n- HTTPOnly flag enabled by default\n- Secure flag enabled by default for HTTPS\n- Automatic size validation (4096 byte limit)\n- Graceful error handling without logging\n\n## Best Practices\n\n1. **Use secure defaults**: Always prefer `Cookie::set_secure()` over `Cookie::set()`\n2. **Set appropriate expiration**: Don't use excessively long expiration times\n3. **Use type casting**: Leverage `get_cast()` for consistent data types\n4. **Choose correct SameSite**: Use `LAX` for auth, `STRICT` for security, `NONE` for embeds\n5. **Validate before setting**: Use `is_valid_size()` for large values\n6. **Clean up**: Delete cookies when no longer needed\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-cookie-utils)\n- [Issue Tracker](https://github.com/arraypress/wp-cookie-utils/issues)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farraypress%2Fwp-cookie-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farraypress%2Fwp-cookie-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farraypress%2Fwp-cookie-utils/lists"}