{"id":29651283,"url":"https://github.com/arraypress/wp-database-utils","last_synced_at":"2025-09-15T09:43:17.183Z","repository":{"id":303120545,"uuid":"1012763106","full_name":"arraypress/wp-database-utils","owner":"arraypress","description":"A lean WordPress library for essential database operations and query building.","archived":false,"fork":false,"pushed_at":"2025-07-13T09:56:17.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-13T11:40:33.064Z","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-02T21:00:39.000Z","updated_at":"2025-07-13T09:56:20.000Z","dependencies_parsed_at":"2025-07-05T20:44:45.847Z","dependency_job_id":"0420c862-e95b-47c0-942a-153fc3b98ed6","html_url":"https://github.com/arraypress/wp-database-utils","commit_stats":null,"previous_names":["arraypress/wp-database-utils"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/arraypress/wp-database-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-database-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-database-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-database-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-database-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arraypress","download_url":"https://codeload.github.com/arraypress/wp-database-utils/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-database-utils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266430668,"owners_count":23927169,"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-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"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:31.062Z","updated_at":"2025-07-22T05:06:31.646Z","avatar_url":"https://github.com/arraypress.png","language":"PHP","readme":"# WordPress Database Utilities\n\nA lean WordPress library for essential database operations and query building. Provides clean APIs for common database tasks that developers use every day.\n\n## Features\n\n* 🔍 **Existence Checks**: Check if tables, columns, rows, and values exist\n* 🛡️ **Safe Query Building**: LIKE patterns, placeholders, and prepared statements\n* 📋 **Query Components**: WHERE, ORDER BY, LIMIT, and other clause builders\n* 🗄️ **Table Information**: Get table names, prefixes, and metadata tables\n* 📅 **Date Range Helpers**: Prevent code duplication for date range queries\n* 🎯 **WordPress Integration**: Built specifically for WordPress database patterns\n\n## Requirements\n\n* PHP 7.4 or later\n* WordPress 5.0 or later\n\n## Installation\n\n```bash\ncomposer require arraypress/wp-database-utils\n```\n\n## Basic Usage\n\n### Database Information \u0026 Existence Checks\n\n```php\nuse ArrayPress\\DatabaseUtils\\Database;\n\n// Check if things exist\n$table_exists  = Database::table_exists( 'custom_table' );\n$column_exists = Database::column_exists( 'posts', 'custom_field' );\n$row_exists    = Database::row_exists( 'posts', 'ID', 123 );\n$value_exists  = Database::value_exists( 'users', 'email', 'user@example.com' );\n\n// Get table information\n$posts_table    = Database::get_table( 'post' ); // Returns wp_posts\n$postmeta_table = Database::get_meta_table( 'post' ); // Returns wp_postmeta\n$prefix         = Database::get_prefix(); // Returns wp_\n$charset        = Database::get_charset_collate();\n```\n\n### Query Building \u0026 Patterns\n\n```php\nuse ArrayPress\\DatabaseUtils\\Builder;\n\n// LIKE patterns (automatically escaped)\n$prefix_pattern   = Builder::like_pattern( 'prefix', 'prefix' ); // \"prefix%\"\n$suffix_pattern   = Builder::like_pattern( 'suffix', 'suffix' ); // \"%suffix\"\n$contains_pattern = Builder::like_pattern( 'word', 'substring' ); // \"%word%\"\n\n// Placeholders for prepared statements\n$placeholders     = Builder::placeholders( [ 'a', 'b', 'c' ] ); // \"%s, %s, %s\"\n$int_placeholders = Builder::placeholders( [ 1, 2, 3 ], '%d' ); // \"%d, %d, %d\"\n\n// IN/NOT IN clauses\n$in_clause     = Builder::in_clause( 'post_id', [ 1, 2, 3 ] ); // \"post_id IN (%s, %s, %s)\"\n$not_in_clause = Builder::in_clause( 'post_id', [ 1, 2, 3 ], true ); // \"post_id NOT IN (%s, %s, %s)\"\n\n// LIKE clauses\n$like_clause = Builder::like_clause( 'post_title', 'search term' ); // \"post_title LIKE %search term%\"\n$prefix_like = Builder::like_clause( 'post_name', 'hello', 'prefix' ); // \"post_name LIKE hello%\"\n\n// Range conditions\n$between_clause = Builder::between_clause( 'post_date', '2024-01-01', '2024-12-31' );\n\n// Flexible conditions with type safety\n$condition  = Builder::condition( 'price', 100, '\u003e', 'float' );\n$null_check = Builder::condition( 'meta_value', null, '!=' ); // \"meta_value IS NOT NULL\"\n```\n\n### Query Component Building\n\n```php\n// WHERE clauses\n$conditions = [\n\tBuilder::condition( 'post_status', 'publish' ),\n\tBuilder::condition( 'post_type', 'post' ),\n\tBuilder::like_clause( 'post_title', 'search term' )\n];\n$where      = Builder::where_clause( $conditions ); // \"WHERE post_status = 'publish' AND ...\"\n$where_or   = Builder::where_clause( $conditions, 'OR' );\n\n// ORDER BY clauses\n$order_by = Builder::order_by_clause( [ 'post_date' =\u003e 'DESC', 'post_title' =\u003e 'ASC' ] );\n\n// LIMIT clauses\n$limit        = Builder::limit_clause( 10 ); // \"LIMIT 10\"\n$limit_offset = Builder::limit_clause( 10, 20 ); // \"LIMIT 20, 10\"\n\n// GROUP BY clauses\n$group_by = Builder::group_by_clause( [ 'post_author', 'post_type' ] );\n```\n\n### Advanced Parameter Handling\n\n```php\n// Safe IN clause with parameter building (prevents SQL injection)\n$params = [];\n$safe_in = Builder::safe_in_clause( 'post_id', [ 1, 2, 3 ], $params, false, '%d' );\n// $params now contains [1, 2, 3]\n\n// Placeholders with automatic parameter collection\n$params = [];\n$placeholders = Builder::placeholders_with_params( $values, $params );\n```\n\n### Date Range Queries (Perfect for Reports \u0026 Analytics)\n\n```php\n// Build date range conditions with parameters\n$params = [];\n$conditions = Builder::date_range_conditions( \n    'order_date', \n    '2024-01-01', \n    '2024-12-31', \n    $params \n);\n// Returns: [\"order_date \u003e= %s\", \"order_date \u003c= %s\"]\n// $params contains: ['2024-01-01', '2024-12-31']\n\n// Build complete date range clause\n$params = [];\n$where_clause = Builder::date_range_clause( \n    'order_date', \n    '2024-01-01', \n    '2024-12-31', \n    $params, \n    ' AND ' \n);\n// Returns: \" AND order_date \u003e= %s AND order_date \u003c= %s\"\n\n// Perfect for analytics queries\n$start_date = '2024-01-01';\n$end_date = '2024-12-31';\n$params = [ 'publish' ];\n\n$date_clause = Builder::date_range_clause( 'post_date', $start_date, $end_date, $params );\n\n$sql = \"SELECT COUNT(*) FROM {$wpdb-\u003eposts} WHERE post_status = %s {$date_clause}\";\n$count = $wpdb-\u003eget_var( $wpdb-\u003eprepare( $sql, $params ) );\n```\n\n### Real-world Examples\n\n```php\n// Check if a custom table exists before querying\nif ( Database::table_exists( 'products' ) ) {\n    $exists = Database::row_exists( 'products', 'sku', 'PRODUCT-123' );\n}\n\n// Build a search query safely\n$search_term = 'wordpress';\n$conditions  = [\n    Builder::condition( 'post_status', 'publish' ),\n    Builder::in_clause( 'post_type', [ 'post', 'page' ] ),\n    Builder::like_clause( 'post_title', $search_term, 'substring' )\n];\n\n$where = Builder::where_clause( $conditions );\n$order = Builder::order_by_clause( [ 'post_date' =\u003e 'DESC' ] );\n$limit = Builder::limit_clause( 20 );\n\n$sql = \"SELECT * FROM {$wpdb-\u003eposts} {$where} {$order} {$limit}\";\n\n// Date range analytics (common in EDD, WooCommerce, etc.)\n$params = [];\n$date_conditions = Builder::date_range_conditions( \n    'order_date', \n    $_GET['start_date'] ?? null, \n    $_GET['end_date'] ?? null, \n    $params \n);\n\nif ( ! empty( $date_conditions ) ) {\n    $where = 'WHERE ' . implode( ' AND ', $date_conditions );\n    $sql = \"SELECT SUM(total) FROM orders {$where}\";\n    $total = $wpdb-\u003eget_var( $wpdb-\u003eprepare( $sql, $params ) );\n}\n```\n\n## Key Features\n\n- **Safe by Default**: All methods use proper escaping and prepared statements\n- **WordPress Optimized**: Built specifically for WordPress database patterns\n- **Lean \u0026 Focused**: Only essential utilities that save time and prevent errors\n- **Date Range Helpers**: Prevent code duplication in analytics and reporting\n- **Parameter Safety**: Automatic parameter handling to prevent SQL injection\n- **Component Based**: Build queries piece by piece with reusable components\n\n## What's Not Included (By Design)\n\nThis library intentionally **does not** include:\n- Complex query builders (use WP_Query or custom SQL instead)\n- ORM functionality (WordPress has enough abstraction layers)\n- Basic utilities that are one-liners (`$wpdb-\u003einsert_id`, etc.)\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-database-utils)\n- [Issue Tracker](https://github.com/arraypress/wp-database-utils/issues)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farraypress%2Fwp-database-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farraypress%2Fwp-database-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farraypress%2Fwp-database-utils/lists"}