diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fe15e7..9eb6e15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # CHANGELOG -## [2.0.0] +## 2.0.0 ### Added @@ -8,97 +8,97 @@ - Replaced commands with WP Cron Events, reducing the need for server cron job configuration. - Updated `README.md`. -## [1.2.5] +## 1.2.5 ### Changed - No changes. Corrects version and git tag which were out of sync. -## [1.2.4] +## 1.2.4 ### Changed - No changes. Corrects version and git tag which were out of sync. -## [1.2.3] +## 1.2.3 ### Fixed - Avoid exiting with an error code when there is nothing to update. -## [1.2.2] +## 1.2.2 ### Added - Added search box to find leges items by ID. -## [1.2.1] +## 1.2.1 ### Fixed - Missing PHP extension when running GH workflows. -## [1.2.0] +## 1.2.0 ### Added - Updates can now be provided through the Admin interface. -## [1.1.4] +## 1.1.4 ### Changed - Replaced Composer plugin dependency check with runtime check. -## [1.1.3] +## 1.1.3 ### Fixed - Validate `datActive` in `dateIsNow` inside Shortcode class. -## [1.1.2] +## 1.1.2 ### Changed - Update dependencies and reference pdc-base plugin from BitBucket to GitHub. -## [1.1.1] +## 1.1.1 ### Fixed - Fix new lege price date comparison. -## [1.1.0] +## 1.1.0 ### Added - Update lege prices via WP_CLI command. -## [1.0.4] +## 1.0.4 ### Changed - Composer dependency on pdc-base plug-in updated from `^2.0.0` to `^3.0.0`. -## [1.0.3] +## 1.0.3 ### Fixed - WordPress 5.3 class and style changes caused issues with quickedit functionality. -## [1.0.2] +## 1.0.2 ### Added - Adding of admin column. -## [1.0.1] +## 1.0.1 ### Fixed - Check if required file for `is_plugin_active` is already loaded, otherwise load it. Props @Jasper Heidebrink. -## [1.0.0] +## 1.0.0 ### Added diff --git a/README.md b/README.md index 689a2e1..54ddce0 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ To create an optimized and zipped build, run the `composer run package` command. ### Commands Since version 2.0.0, the commands have been replaced by WP Cron Events. This change requires less configuration on your server by eliminating the need to add server cron jobs. Just activate the plugin and you're all set. +Remember to remove any previously configured cron jobs from your web server, as they have been deprecated since version 2.0.0. ### WP Cron Events diff --git a/src/Leges/Repositories/LegesRepository.php b/src/Leges/Repositories/LegesRepository.php new file mode 100644 index 0000000..42df100 --- /dev/null +++ b/src/Leges/Repositories/LegesRepository.php @@ -0,0 +1,26 @@ +queryArgs, [ + 'post_type' => [$this->posttype], + 'post_status' => 'publish', + ]); + + $this->query = new WP_Query($args); + + return $this->getQuery()->posts; + } +} diff --git a/src/Leges/RestAPI/Controllers/LegesController.php b/src/Leges/RestAPI/Controllers/LegesController.php index b353712..ab00d6a 100644 --- a/src/Leges/RestAPI/Controllers/LegesController.php +++ b/src/Leges/RestAPI/Controllers/LegesController.php @@ -35,9 +35,9 @@ public function getLeges(WP_REST_Request $request): WP_REST_Response */ public function getLege(WP_REST_Request $request) { - $id = $request->get_param('id'); + $id = $this->handleRequestParam($request, 'id', 'int', 0); - if (empty($id) || ! is_numeric($id)) { + if (empty($id)) { return new WP_Error('bad_request', 'Invalid ID', [ 'status' => 400, ]); @@ -59,9 +59,9 @@ public function getLege(WP_REST_Request $request) */ public function getLegeBySlug(WP_REST_Request $request) { - $slug = $request->get_param('slug'); + $slug = $this->handleRequestParam($request, 'slug', 'string', ''); - if (empty($slug) || ! is_string($slug)) { + if (strlen($slug) === 0) { return new WP_Error('bad_request', 'Invalid slug', [ 'status' => 400, ]); @@ -85,7 +85,7 @@ protected function addPaginator(array $data, WP_Query $query): array { $perPage = $query->get('posts_per_page'); $page = $query->get('paged'); - $page = 0 == $page || -1 == $perPage ? 1 : $page; // If $perPage = -1, $page should be 1. + $page = $page = (0 == $page || -1 == $perPage) ? 1 : $page; // If $perPage = -1, $page should be 1. return array_merge([ 'data' => $data, diff --git a/src/Leges/RestAPI/Repositories/LegesRepository.php b/src/Leges/RestAPI/Repositories/LegesRepository.php index 18eb46a..47fb260 100644 --- a/src/Leges/RestAPI/Repositories/LegesRepository.php +++ b/src/Leges/RestAPI/Repositories/LegesRepository.php @@ -8,7 +8,7 @@ class LegesRepository extends AbstractRepository { /** - * Posttype definition + * Posttype definition. */ protected string $posttype = 'pdc-leges'; diff --git a/src/Leges/WPCron/Events/UpdateLegesPrices.php b/src/Leges/WPCron/Events/UpdateLegesPrices.php index 48de14c..4482753 100644 --- a/src/Leges/WPCron/Events/UpdateLegesPrices.php +++ b/src/Leges/WPCron/Events/UpdateLegesPrices.php @@ -3,9 +3,9 @@ namespace OWC\PDC\Leges\WPCron\Events; use DateTime; +use OWC\PDC\Leges\Repositories\LegesRepository; use OWC\PDC\Leges\WPCron\Contracts\AbstractEvent; use WP_Post; -use WP_Query; class UpdateLegesPrices extends AbstractEvent { @@ -36,8 +36,8 @@ protected function execute(): void */ protected function getLeges(): array { - $query = new WP_Query([ - 'post_type' => 'pdc-leges', + $repository = new LegesRepository(); + $repository->addQueryArguments([ 'posts_per_page' => -1, 'meta_query' => [ 'relation' => 'AND', @@ -54,7 +54,7 @@ protected function getLeges(): array ], ]); - return $query->posts ?: []; + return $repository->all(); } /** @@ -109,6 +109,11 @@ protected function updatePostMeta(WP_Post $lege): void $updated = update_post_meta($lege->ID, self::META_PRICE, $newPrice); + /** + * Check if the previous and new prices are the same. + * If they are, updating will return false, but this is not a reason to stop the current iteration. + * If they are not the same, something else went wrong, so stop the current iteration. + */ if (! $updated && $currentPrice !== $newPrice) { $this->logError(sprintf('could not update lege [%s].', $lege->post_title)); diff --git a/src/Leges/WPCron/WPCronServiceProvider.php b/src/Leges/WPCron/WPCronServiceProvider.php index 104a66a..4a4363b 100644 --- a/src/Leges/WPCron/WPCronServiceProvider.php +++ b/src/Leges/WPCron/WPCronServiceProvider.php @@ -3,7 +3,6 @@ namespace OWC\PDC\Leges\WPCron; use DateTime; -use DateTimeZone; use OWC\PDC\Base\Foundation\ServiceProvider; use OWC\PDC\Leges\WPCron\Events\UpdateLegesPrices; @@ -20,7 +19,7 @@ public function register(): void protected function timeToExecute(): int { - $currentDateTime = new DateTime('now', new DateTimeZone(wp_timezone_string())); + $currentDateTime = new DateTime('now', wp_timezone()); $tomorrowDateTime = $currentDateTime->modify('+1 day'); $tomorrowDateTime->setTime(6, 0, 0);