diff --git a/CHANGELOG.md b/CHANGELOG.md index f1d7a35..8d31838 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +## v2.2.0 - 2024.03.28 + +### Added + +- Add utility functions to test the current environment ([#31](https://github.com/studiometa/wp-toolkit/pull/31)) +- Add a `FacetsManager` to easily filter content with `pre_get_posts` ([#32](https://github.com/studiometa/wp-toolkit/pull/32)) +- Add a `facets_get('key')` Twig function to easily get the value of a facets filter from the query string ([#32](https://github.com/studiometa/wp-toolkit/pull/32)) + +### Fixed + +- Fix dependency conflict ([#32](https://github.com/studiometa/wp-toolkit/pull/32)) + ## v2.1.0 - 2024.03.12 ### Added diff --git a/composer.json b/composer.json index 73f5073..3285ed5 100644 --- a/composer.json +++ b/composer.json @@ -6,20 +6,22 @@ "require": { "php": "^8.1", "monolog/monolog": "^2.9|^3.0", - "psr/log": "^1.1", + "psr/log": "^1.1|^2.0|^3.0", "sentry/sentry": "^4.6", "studiometa/webpack-config": "^5.0", "symfony/http-foundation": "^6.4|^7.0", "symfony/yaml": "^6.4|^7.0", + "timber/timber": "^1.0|^2.0", "wecodemore/wordpress-early-hook": "^1.2" }, "require-dev": { - "squizlabs/php_codesniffer": "^3.4", "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", - "szepeviktor/phpstan-wordpress": "^1.1", "php-stubs/wordpress-stubs": "^5.5", "phpunit/phpunit": "^9.0", - "yoast/phpunit-polyfills": "^1.0" + "squizlabs/php_codesniffer": "^3.4", + "szepeviktor/phpstan-wordpress": "^1.1", + "wp-phpunit/wp-phpunit": "^6.4", + "yoast/phpunit-polyfills": "^2.0" }, "autoload": { "psr-4": { @@ -40,14 +42,15 @@ "@fix:style" ], "fix:style": "phpcbf", - "test": "phpunit" + "test": "XDEBUG_MODE=coverage phpunit" }, "minimum-stability": "dev", "prefer-stable": true, "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true, - "phpstan/extension-installer": true + "phpstan/extension-installer": true, + "composer/installers": true }, "sort-packages": true } diff --git a/composer.lock b/composer.lock index 379e41f..fa33d45 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "83e8d763f067b8b9f1332f4fb1a9bcd4", + "content-hash": "dbac66e2af21cca739607377fdf3b4a3", "packages": [ { "name": "anahkiasen/html-object", @@ -52,6 +52,151 @@ }, "time": "2017-05-31T07:52:45+00:00" }, + { + "name": "composer/installers", + "version": "v2.2.0", + "source": { + "type": "git", + "url": "https://github.com/composer/installers.git", + "reference": "c29dc4b93137acb82734f672c37e029dfbd95b35" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/composer/installers/zipball/c29dc4b93137acb82734f672c37e029dfbd95b35", + "reference": "c29dc4b93137acb82734f672c37e029dfbd95b35", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^1.0 || ^2.0", + "php": "^7.2 || ^8.0" + }, + "require-dev": { + "composer/composer": "1.6.* || ^2.0", + "composer/semver": "^1 || ^3", + "phpstan/phpstan": "^0.12.55", + "phpstan/phpstan-phpunit": "^0.12.16", + "symfony/phpunit-bridge": "^5.3", + "symfony/process": "^5" + }, + "type": "composer-plugin", + "extra": { + "class": "Composer\\Installers\\Plugin", + "branch-alias": { + "dev-main": "2.x-dev" + }, + "plugin-modifies-install-path": true + }, + "autoload": { + "psr-4": { + "Composer\\Installers\\": "src/Composer/Installers" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Kyle Robinson Young", + "email": "kyle@dontkry.com", + "homepage": "https://github.com/shama" + } + ], + "description": "A multi-framework Composer library installer", + "homepage": "https://composer.github.io/installers/", + "keywords": [ + "Dolibarr", + "Eliasis", + "Hurad", + "ImageCMS", + "Kanboard", + "Lan Management System", + "MODX Evo", + "MantisBT", + "Mautic", + "Maya", + "OXID", + "Plentymarkets", + "Porto", + "RadPHP", + "SMF", + "Starbug", + "Thelia", + "Whmcs", + "WolfCMS", + "agl", + "annotatecms", + "attogram", + "bitrix", + "cakephp", + "chef", + "cockpit", + "codeigniter", + "concrete5", + "croogo", + "dokuwiki", + "drupal", + "eZ Platform", + "elgg", + "expressionengine", + "fuelphp", + "grav", + "installer", + "itop", + "known", + "kohana", + "laravel", + "lavalite", + "lithium", + "magento", + "majima", + "mako", + "matomo", + "mediawiki", + "miaoxing", + "modulework", + "modx", + "moodle", + "osclass", + "pantheon", + "phpbb", + "piwik", + "ppi", + "processwire", + "puppet", + "pxcms", + "reindex", + "roundcube", + "shopware", + "silverstripe", + "sydes", + "sylius", + "tastyigniter", + "wordpress", + "yawik", + "zend", + "zikula" + ], + "support": { + "issues": "https://github.com/composer/installers/issues", + "source": "https://github.com/composer/installers/tree/v2.2.0" + }, + "funding": [ + { + "url": "https://packagist.com", + "type": "custom" + }, + { + "url": "https://github.com/composer", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/composer/composer", + "type": "tidelift" + } + ], + "time": "2022-08-20T06:45:11+00:00" + }, { "name": "guzzlehttp/psr7", "version": "2.6.2", @@ -229,42 +374,41 @@ }, { "name": "monolog/monolog", - "version": "2.9.1", + "version": "3.5.0", "source": { "type": "git", "url": "https://github.com/Seldaek/monolog.git", - "reference": "f259e2b15fb95494c83f52d3caad003bbf5ffaa1" + "reference": "c915e2634718dbc8a4a15c61b0e62e7a44e14448" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Seldaek/monolog/zipball/f259e2b15fb95494c83f52d3caad003bbf5ffaa1", - "reference": "f259e2b15fb95494c83f52d3caad003bbf5ffaa1", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/c915e2634718dbc8a4a15c61b0e62e7a44e14448", + "reference": "c915e2634718dbc8a4a15c61b0e62e7a44e14448", "shasum": "" }, "require": { - "php": ">=7.2", - "psr/log": "^1.0.1 || ^2.0 || ^3.0" + "php": ">=8.1", + "psr/log": "^2.0 || ^3.0" }, "provide": { - "psr/log-implementation": "1.0.0 || 2.0.0 || 3.0.0" + "psr/log-implementation": "3.0.0" }, "require-dev": { - "aws/aws-sdk-php": "^2.4.9 || ^3.0", + "aws/aws-sdk-php": "^3.0", "doctrine/couchdb": "~1.0@dev", "elasticsearch/elasticsearch": "^7 || ^8", "ext-json": "*", - "graylog2/gelf-php": "^1.4.2 || ^2@dev", - "guzzlehttp/guzzle": "^7.4", + "graylog2/gelf-php": "^1.4.2 || ^2.0", + "guzzlehttp/guzzle": "^7.4.5", "guzzlehttp/psr7": "^2.2", "mongodb/mongodb": "^1.8", "php-amqplib/php-amqplib": "~2.4 || ^3", - "phpspec/prophecy": "^1.15", - "phpstan/phpstan": "^0.12.91", - "phpunit/phpunit": "^8.5.14", - "predis/predis": "^1.1 || ^2.0", - "rollbar/rollbar": "^1.3 || ^2 || ^3", + "phpstan/phpstan": "^1.9", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-strict-rules": "^1.4", + "phpunit/phpunit": "^10.1", + "predis/predis": "^1.1 || ^2", "ruflin/elastica": "^7", - "swiftmailer/swiftmailer": "^5.3|^6.0", "symfony/mailer": "^5.4 || ^6", "symfony/mime": "^5.4 || ^6" }, @@ -287,7 +431,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "2.x-dev" + "dev-main": "3.x-dev" } }, "autoload": { @@ -315,7 +459,7 @@ ], "support": { "issues": "https://github.com/Seldaek/monolog/issues", - "source": "https://github.com/Seldaek/monolog/tree/2.9.1" + "source": "https://github.com/Seldaek/monolog/tree/3.5.0" }, "funding": [ { @@ -327,7 +471,7 @@ "type": "tidelift" } ], - "time": "2023-02-06T13:44:46+00:00" + "time": "2023-10-27T15:32:31+00:00" }, { "name": "psr/http-factory", @@ -439,30 +583,30 @@ }, { "name": "psr/log", - "version": "1.1.4", + "version": "3.0.0", "source": { "type": "git", "url": "https://github.com/php-fig/log.git", - "reference": "d49695b909c3b7628b6289db5479a1c204601f11" + "reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/log/zipball/d49695b909c3b7628b6289db5479a1c204601f11", - "reference": "d49695b909c3b7628b6289db5479a1c204601f11", + "url": "https://api.github.com/repos/php-fig/log/zipball/fe5ea303b0887d5caefd3d431c3e61ad47037001", + "reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001", "shasum": "" }, "require": { - "php": ">=5.3.0" + "php": ">=8.0.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.1.x-dev" + "dev-master": "3.x-dev" } }, "autoload": { "psr-4": { - "Psr\\Log\\": "Psr/Log/" + "Psr\\Log\\": "src" } }, "notification-url": "https://packagist.org/downloads/", @@ -483,9 +627,9 @@ "psr-3" ], "support": { - "source": "https://github.com/php-fig/log/tree/1.1.4" + "source": "https://github.com/php-fig/log/tree/3.0.0" }, - "time": "2021-05-03T11:20:27+00:00" + "time": "2021-07-14T16:46:02+00:00" }, { "name": "ralouphie/getallheaders", @@ -1403,8 +1547,164 @@ "issues": "https://github.com/tighten/collect/issues", "source": "https://github.com/tighten/collect/tree/v8.83.27" }, + "abandoned": "illuminate/collections", "time": "2023-01-13T18:05:42+00:00" }, + { + "name": "timber/timber", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/timber/timber.git", + "reference": "ee467c650e51d042a04d9107f35239cbc566d4d7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/timber/timber/zipball/ee467c650e51d042a04d9107f35239cbc566d4d7", + "reference": "ee467c650e51d042a04d9107f35239cbc566d4d7", + "shasum": "" + }, + "require": { + "composer/installers": "^1.0 || ^2.0", + "php": "^7.4 || ^8.0", + "symfony/polyfill-php80": "^1.27", + "twig/twig": "^2.15.3 || ^3.0" + }, + "require-dev": { + "ergebnis/composer-normalize": "^2.28", + "php-parallel-lint/php-parallel-lint": "^1.3", + "php-stubs/acf-pro-stubs": "^5.12", + "php-stubs/wp-cli-stubs": "^2.0", + "phpro/grumphp": "^1.12", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan": "^1.7", + "squizlabs/php_codesniffer": "^3.0", + "symplify/easy-coding-standard": "^12.0", + "szepeviktor/phpstan-wordpress": "^1.1", + "twig/cache-extra": "^3.3", + "wpackagist-plugin/advanced-custom-fields": "^5.0 || ^6.0", + "wpackagist-plugin/co-authors-plus": "^3.3", + "yoast/wp-test-utils": "^1.0" + }, + "suggest": { + "php-coveralls/php-coveralls": "^2.0 for code coverage", + "twig/cache-extra": "For using the cache tag in Twig" + }, + "type": "library", + "autoload": { + "psr-4": { + "Timber\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jared Novack", + "email": "jared@upstatement.com", + "homepage": "http://upstatement.com" + }, + { + "name": "Connor J. Burton", + "email": "connorjburton@gmail.com", + "homepage": "http://connorburton.com" + } + ], + "description": "Plugin to write WordPress themes w Object-Oriented Code and the Twig Template Engine", + "homepage": "http://timber.upstatement.com", + "keywords": [ + "templating", + "themes", + "timber", + "twig" + ], + "support": { + "docs": "https://timber.github.io/docs/", + "issues": "https://github.com/timber/timber/issues", + "source": "https://github.com/timber/timber" + }, + "funding": [ + { + "url": "https://opencollective.com/timber", + "type": "open_collective" + } + ], + "time": "2023-11-09T08:30:32+00:00" + }, + { + "name": "twig/twig", + "version": "v3.8.0", + "source": { + "type": "git", + "url": "https://github.com/twigphp/Twig.git", + "reference": "9d15f0ac07f44dc4217883ec6ae02fd555c6f71d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/9d15f0ac07f44dc4217883ec6ae02fd555c6f71d", + "reference": "9d15f0ac07f44dc4217883ec6ae02fd555c6f71d", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/polyfill-ctype": "^1.8", + "symfony/polyfill-mbstring": "^1.3", + "symfony/polyfill-php80": "^1.22" + }, + "require-dev": { + "psr/container": "^1.0|^2.0", + "symfony/phpunit-bridge": "^5.4.9|^6.3|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Twig\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + }, + { + "name": "Twig Team", + "role": "Contributors" + }, + { + "name": "Armin Ronacher", + "email": "armin.ronacher@active-4.com", + "role": "Project Founder" + } + ], + "description": "Twig, the flexible, fast, and secure template language for PHP", + "homepage": "https://twig.symfony.com", + "keywords": [ + "templating" + ], + "support": { + "issues": "https://github.com/twigphp/Twig/issues", + "source": "https://github.com/twigphp/Twig/tree/v3.8.0" + }, + "funding": [ + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/twig/twig", + "type": "tidelift" + } + ], + "time": "2023-11-21T18:54:41+00:00" + }, { "name": "wecodemore/wordpress-early-hook", "version": "1.2.0", @@ -1896,16 +2196,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.60", + "version": "1.10.65", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "95dcea7d6c628a3f2f56d091d8a0219485a86bbe" + "reference": "3c657d057a0b7ecae19cb12db446bbc99d8839c6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/95dcea7d6c628a3f2f56d091d8a0219485a86bbe", - "reference": "95dcea7d6c628a3f2f56d091d8a0219485a86bbe", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/3c657d057a0b7ecae19cb12db446bbc99d8839c6", + "reference": "3c657d057a0b7ecae19cb12db446bbc99d8839c6", "shasum": "" }, "require": { @@ -1954,7 +2254,7 @@ "type": "tidelift" } ], - "time": "2024-03-07T13:30:19+00:00" + "time": "2024-03-23T10:30:26+00:00" }, { "name": "phpunit/php-code-coverage", @@ -2277,16 +2577,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.17", + "version": "9.6.18", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "1a156980d78a6666721b7e8e8502fe210b587fcd" + "reference": "32c2c2d6580b1d8ab3c10b1e9e4dc263cc69bb04" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/1a156980d78a6666721b7e8e8502fe210b587fcd", - "reference": "1a156980d78a6666721b7e8e8502fe210b587fcd", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/32c2c2d6580b1d8ab3c10b1e9e4dc263cc69bb04", + "reference": "32c2c2d6580b1d8ab3c10b1e9e4dc263cc69bb04", "shasum": "" }, "require": { @@ -2360,7 +2660,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.17" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.18" }, "funding": [ { @@ -2376,7 +2676,7 @@ "type": "tidelift" } ], - "time": "2024-02-23T13:14:51+00:00" + "time": "2024-03-21T12:07:32+00:00" }, { "name": "sebastian/cli-parser", @@ -3180,16 +3480,16 @@ }, { "name": "sebastian/resource-operations", - "version": "3.0.3", + "version": "3.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/resource-operations.git", - "reference": "0f4443cb3a1d92ce809899753bc0d5d5a8dd19a8" + "reference": "05d5692a7993ecccd56a03e40cd7e5b09b1d404e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/0f4443cb3a1d92ce809899753bc0d5d5a8dd19a8", - "reference": "0f4443cb3a1d92ce809899753bc0d5d5a8dd19a8", + "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/05d5692a7993ecccd56a03e40cd7e5b09b1d404e", + "reference": "05d5692a7993ecccd56a03e40cd7e5b09b1d404e", "shasum": "" }, "require": { @@ -3201,7 +3501,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.0-dev" + "dev-main": "3.0-dev" } }, "autoload": { @@ -3222,8 +3522,7 @@ "description": "Provides a list of PHP built-in functions that operate on resources", "homepage": "https://www.github.com/sebastianbergmann/resource-operations", "support": { - "issues": "https://github.com/sebastianbergmann/resource-operations/issues", - "source": "https://github.com/sebastianbergmann/resource-operations/tree/3.0.3" + "source": "https://github.com/sebastianbergmann/resource-operations/tree/3.0.4" }, "funding": [ { @@ -3231,7 +3530,7 @@ "type": "github" } ], - "time": "2020-09-28T06:45:17+00:00" + "time": "2024-03-14T16:00:52+00:00" }, { "name": "sebastian/type", @@ -3500,16 +3799,16 @@ }, { "name": "szepeviktor/phpstan-wordpress", - "version": "v1.3.3", + "version": "v1.3.4", "source": { "type": "git", "url": "https://github.com/szepeviktor/phpstan-wordpress.git", - "reference": "f7ff091331bc00c5688fe4ce0c4d51d06fa61553" + "reference": "891d0767855a32c886a439efae090408cc1fa156" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/szepeviktor/phpstan-wordpress/zipball/f7ff091331bc00c5688fe4ce0c4d51d06fa61553", - "reference": "f7ff091331bc00c5688fe4ce0c4d51d06fa61553", + "url": "https://api.github.com/repos/szepeviktor/phpstan-wordpress/zipball/891d0767855a32c886a439efae090408cc1fa156", + "reference": "891d0767855a32c886a439efae090408cc1fa156", "shasum": "" }, "require": { @@ -3556,9 +3855,9 @@ ], "support": { "issues": "https://github.com/szepeviktor/phpstan-wordpress/issues", - "source": "https://github.com/szepeviktor/phpstan-wordpress/tree/v1.3.3" + "source": "https://github.com/szepeviktor/phpstan-wordpress/tree/v1.3.4" }, - "time": "2024-02-26T13:55:50+00:00" + "time": "2024-03-21T16:32:59+00:00" }, { "name": "theseer/tokenizer", @@ -3610,23 +3909,71 @@ ], "time": "2024-03-03T12:36:25+00:00" }, + { + "name": "wp-phpunit/wp-phpunit", + "version": "6.4.2", + "source": { + "type": "git", + "url": "https://github.com/wp-phpunit/wp-phpunit.git", + "reference": "aa3c8f5d1b7efc295fd2b37c7264d2356a8c1099" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/wp-phpunit/wp-phpunit/zipball/aa3c8f5d1b7efc295fd2b37c7264d2356a8c1099", + "reference": "aa3c8f5d1b7efc295fd2b37c7264d2356a8c1099", + "shasum": "" + }, + "type": "library", + "autoload": { + "files": [ + "__loaded.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-2.0-or-later" + ], + "authors": [ + { + "name": "Evan Mattson", + "email": "me@aaemnnost.tv" + }, + { + "name": "WordPress Community", + "homepage": "https://wordpress.org/about/" + } + ], + "description": "WordPress core PHPUnit library", + "homepage": "https://github.com/wp-phpunit", + "keywords": [ + "phpunit", + "test", + "wordpress" + ], + "support": { + "docs": "https://github.com/wp-phpunit/docs", + "issues": "https://github.com/wp-phpunit/issues", + "source": "https://github.com/wp-phpunit/wp-phpunit" + }, + "time": "2023-12-07T00:50:08+00:00" + }, { "name": "yoast/phpunit-polyfills", - "version": "1.1.0", + "version": "2.0.0", "source": { "type": "git", "url": "https://github.com/Yoast/PHPUnit-Polyfills.git", - "reference": "224e4a1329c03d8bad520e3fc4ec980034a4b212" + "reference": "c758753e8f9dac251fed396a73c8305af3f17922" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Yoast/PHPUnit-Polyfills/zipball/224e4a1329c03d8bad520e3fc4ec980034a4b212", - "reference": "224e4a1329c03d8bad520e3fc4ec980034a4b212", + "url": "https://api.github.com/repos/Yoast/PHPUnit-Polyfills/zipball/c758753e8f9dac251fed396a73c8305af3f17922", + "reference": "c758753e8f9dac251fed396a73c8305af3f17922", "shasum": "" }, "require": { - "php": ">=5.4", - "phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0" + "php": ">=5.6", + "phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0" }, "require-dev": { "yoast/yoastcs": "^2.3.0" @@ -3668,7 +4015,7 @@ "issues": "https://github.com/Yoast/PHPUnit-Polyfills/issues", "source": "https://github.com/Yoast/PHPUnit-Polyfills" }, - "time": "2023-08-19T14:25:08+00:00" + "time": "2023-06-06T20:28:24+00:00" } ], "aliases": [], diff --git a/src/Helpers/Env.php b/src/Helpers/Env.php index 5f92efe..785506e 100644 --- a/src/Helpers/Env.php +++ b/src/Helpers/Env.php @@ -19,4 +19,67 @@ public static function get(string $key): string // so we use `getenv` as a fallback to try and get the value. return $env[ $key ] ?? (string) getenv($key); } + + /** + * Get the current APP_ENV configuration. + * + * @return string + */ + private static function get_app_env(): string + { + return strtolower(self::get('APP_ENV')); + } + + /** + * Test if the current environment is production. + * Both `production` and `prod` values are tested on the `APP_ENV` or + * `WP_ENV` environment variable or the `WP_ENV` constant. + * + * @return bool + */ + public static function is_prod(): bool + { + $env = self::get_app_env(); + return $env === 'production' || $env === 'prod'; + } + + /** + * Test if the current environment is preprod. + * + * @return bool + */ + public static function is_preprod(): bool + { + return self::get_app_env() === 'preprod'; + } + + /** + * Test if the current environment is local. + * + * @return bool + */ + public static function is_local(): bool + { + return self::get_app_env() === 'local'; + } + + /** + * Test if the current environment is staging. + * + * @return bool + */ + public static function is_staging(): bool + { + return self::get_app_env() === 'staging'; + } + + /** + * Test if the current environment is development. + * + * @return bool + */ + public static function is_development(): bool + { + return self::get_app_env() === 'development'; + } } diff --git a/src/Managers/FacetsManager.php b/src/Managers/FacetsManager.php new file mode 100644 index 0000000..db4e90c --- /dev/null +++ b/src/Managers/FacetsManager.php @@ -0,0 +1,88 @@ +get('facets'); + $this->facets = is_array($facets) ? $facets : null; + } + + /** + * {@inheritdoc} + */ + public function run() + { + add_filter('timber/twig', array( $this, 'add_twig_helpers' )); + + if (is_array($this->facets)) { + add_action('pre_get_posts', array( $this, 'add_facets_to_query' ), $this->pre_get_posts_priority); + } + } + + /** + * Add facets defined as get or post parameters from the request to the main WP Query. + * For the list of available parameters, see the documentation for `WP_Query` linked below. + * + * @see https://developer.wordpress.org/reference/classes/wp_query/#parameters + * + * @param WP_Query $query The query for the current request. + * @return void + */ + public function add_facets_to_query(WP_Query &$query): void + { + if (!is_array($this->facets)) { + return; + } + + foreach ($this->facets as $query_var => $value) { + $query->query_vars[ $query_var ] = $value; + } + } + + /** + * Add Twig helpers to get parameters values. + * + * @param Environment $twig The Twig environment. + * @return Environment + */ + public function add_twig_helpers(Environment $twig): Environment + { + $twig->addFunction( + new TwigFunction( + 'facets_get', + fn(string $parameter) => $this->facets[ $parameter ] ?? null, + ) + ); + + return $twig; + } +} diff --git a/src/helpers.php b/src/helpers.php index 72c6bd4..dfbcbaf 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -22,6 +22,46 @@ function env(string $key): string return Env::get($key); } +/** + * Test if the current environment is prod. + */ +function env_is_prod(): bool +{ + return Env::is_prod(); +} + +/** + * Test if the current environment is preprod. + */ +function env_is_preprod(): bool +{ + return Env::is_preprod(); +} + +/** + * Test if the current environment is local. + */ +function env_is_local(): bool +{ + return Env::is_local(); +} + +/** + * Test if the current environment is staging. + */ +function env_is_staging(): bool +{ + return Env::is_staging(); +} + +/** + * Test if the current environment is development. + */ +function env_is_development(): bool +{ + return Env::is_development(); +} + /** * Get a Request instance from the symfony/http-foundation package. * diff --git a/tests/Helpers/EnvTest.php b/tests/Helpers/EnvTest.php index a986ecc..1916bd4 100644 --- a/tests/Helpers/EnvTest.php +++ b/tests/Helpers/EnvTest.php @@ -2,14 +2,14 @@ namespace Studiometa\WPToolkitTest; -use WP_UnitTestCase; -use Studiometa\WPToolkit\Helpers\Env as EnvClass; +use PHPUnit\Framework\TestCase; +use Studiometa\WPToolkit\Helpers\Env; use function Studiometa\WPToolkit\env; /** * EnvTest test case. */ -class EnvTest extends WP_UnitTestCase +class EnvTest extends TestCase { /** @@ -20,6 +20,33 @@ class EnvTest extends WP_UnitTestCase public function test_type_of_request_function_helper() { $this->assertTrue(is_string(env('missing'))); - $this->assertTrue(is_string(EnvClass::get('missing'))); + $this->assertTrue(is_string(Env::get('missing'))); + } + + /** + * Test the `env_is_...` functions and methods. + * + * @return void + */ + public function test_env_is_functions() + { + $mapping = [ + 'is_local' => 'local', + 'is_prod' => 'production', + 'is_prod' => 'prod', + 'is_preprod' => 'preprod', + 'is_development' => 'development', + 'is_staging' => 'staging', + ]; + + foreach ($mapping as $name => $value) { + $fn = sprintf('\Studiometa\WPToolkit\env_%s', $name); + $_ENV['APP_ENV'] = strtolower($value); + $this->assertTrue($fn()); + $this->assertTrue(Env::$name()); + $_ENV['APP_ENV'] = strtoupper($value); + $this->assertTrue($fn()); + $this->assertTrue(Env::$name()); + } } } diff --git a/tests/Managers/FacetsManagerTest.php b/tests/Managers/FacetsManagerTest.php new file mode 100644 index 0000000..195811b --- /dev/null +++ b/tests/Managers/FacetsManagerTest.php @@ -0,0 +1,54 @@ +query->set('facets', ['cat' => 1]); + $manager = new FacetsManager(); + $manager->run(); + + $this->go_to('/?facets[cat]=1'); + global $wp_query; + + $this->assertTrue($wp_query instanceof WP_Query); + $this->assertTrue($wp_query->query_vars['cat'] === 1); + } + + public function test_it_should_not_add_query_vars_to_wp_query() + { + request()->query->remove('facets'); + (new FacetsManager())->run(); + $this->go_to('/?facets[cat]=1'); + global $wp_query; + + $this->assertTrue($wp_query instanceof WP_Query); + $this->assertFalse($wp_query->query_vars['cat'] === 1); + } + + public function test_it_should_add_a_twig_helper_function() + { + request()->query->set('facets', ['cat' => 2]); + $loader = new ArrayLoader(); + $twig = new Environment($loader); + $manager = new FacetsManager(); + $twig = $manager->add_twig_helpers($twig); + $function = $twig->getFunction('facets_get'); + $this->assertTrue($function instanceof TwigFunction); + $this->assertTrue($function->getCallable()('cat') === 2); + } +}