Skip to content

Commit

Permalink
(refactor): processed feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike van den Hoek committed Jul 15, 2024
1 parent abc337d commit 984259c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 29 deletions.
34 changes: 17 additions & 17 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,104 +1,104 @@
# CHANGELOG

## [2.0.0]
## 2.0.0

### Added

- Register REST endpoints.
- 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

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions src/Leges/Repositories/LegesRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace OWC\PDC\Leges\Repositories;

use OWC\PDC\Leges\RestAPI\Repositories\LegesRepository as LegesRepositoryAPI;
use WP_Query;

class LegesRepository extends LegesRepositoryAPI
{
/**
* Posttype definition.
*/
protected string $posttype = 'pdc-leges';

public function all(): array
{
$args = array_merge($this->queryArgs, [
'post_type' => [$this->posttype],
'post_status' => 'publish',
]);

$this->query = new WP_Query($args);

return $this->getQuery()->posts;
}
}
10 changes: 5 additions & 5 deletions src/Leges/RestAPI/Controllers/LegesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
Expand All @@ -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,
]);
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Leges/RestAPI/Repositories/LegesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class LegesRepository extends AbstractRepository
{
/**
* Posttype definition
* Posttype definition.
*/
protected string $posttype = 'pdc-leges';

Expand Down
13 changes: 9 additions & 4 deletions src/Leges/WPCron/Events/UpdateLegesPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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',
Expand All @@ -54,7 +54,7 @@ protected function getLeges(): array
],
]);

return $query->posts ?: [];
return $repository->all();
}

/**
Expand Down Expand Up @@ -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));

Expand Down
3 changes: 1 addition & 2 deletions src/Leges/WPCron/WPCronServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand Down

0 comments on commit 984259c

Please sign in to comment.