Skip to content

Commit

Permalink
(feat): use 'rest_after_insert_pdc-item' filter in addition to existi…
Browse files Browse the repository at this point in the history
…ng 'updated_post_meta' filter
  • Loading branch information
Mike van den Hoek authored and mvdhoek1 committed Apr 28, 2023
1 parent 7d2f886 commit e30e16f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## Version [3.7.3] (2023-04-28)

### Feat

- Use 'rest_after_insert_pdc-item' filter in addition to existing 'updated_post_meta' filter.

## Version [3.7.2] (2023-04-11)

Expand Down
2 changes: 1 addition & 1 deletion pdc-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Yard | PDC Base
* Plugin URI: https://www.openwebconcept.nl/
* Description: Acts as foundation for other PDC related content plugins. This plugin implements actions to allow for other plugins to add and/or change Custom Posttypes, Metaboxes, Taxonomies, en Posts 2 posts relations.
* Version: 3.7.2
* Version: 3.7.3
* Author: Yard | Digital Agency
* Author URI: https://www.yard.nl/
* License: GPL-3.0
Expand Down
2 changes: 1 addition & 1 deletion src/Base/Foundation/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Plugin
*
* @var string
*/
const VERSION = '3.7.2';
const VERSION = '3.7.3';

/**
* Path to the root of the plugin.
Expand Down
50 changes: 42 additions & 8 deletions src/Base/Metabox/Handlers/UPLResourceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,70 @@
namespace OWC\PDC\Base\Metabox\Handlers;

use OWC\PDC\Base\Support\Traits\RequestUPL;
use WP_Post;
use WP_REST_Request;

class UPLResourceHandler
{
use RequestUPL;

/**
* Update resourceURL meta based on uplName meta.
*
* @param mixed $metaValue
*/
public function handleUpdatedMeta(int $metaId, int $postID, string $metaKey, $metaValue): void
public function handleUpdatedMetaClassicEditor(int $metaId, int $postID, string $metaKey, $metaValue): void
{
if (!$this->objectIsPDC($postID) || $metaKey !== '_owc_pdc_upl_naam') {
if (! $this->objectIsPDC($postID) || $metaKey !== '_owc_pdc_upl_naam') {
return;
}

$uplName = get_post_meta($postID, '_owc_pdc_upl_naam', true);
$uplName = \get_post_meta($postID, '_owc_pdc_upl_naam', true);

if (empty($uplName)) {
return;
}

$options = $this->getOptionsUPL();
$options = $this->getOptionsUPL();
$resourceURL = $this->getResourceURL($options, $uplName);

if (empty($resourceURL)) {
return;
}

update_post_meta($postID, '_owc_pdc_upl_resource', $resourceURL);
$oldResourceURL = \get_post_meta($postID, '_owc_pdc_upl_resource', true);

if ($oldResourceURL === $resourceURL) {
return;
}

\update_post_meta($postID, '_owc_pdc_upl_resource', $resourceURL);
}

public function handleUpdatedMetaGutenbergEditor(WP_Post $post, WP_REST_Request $request, bool $creating): void
{
if (! $this->objectIsPDC($post->ID)) {
return;
}

$uplName = \get_post_meta($post->ID, '_owc_pdc_upl_naam', true);

if (empty($uplName)) {
return;
}

$options = $this->getOptionsUPL();
$resourceURL = $this->getResourceURL($options, $uplName);

if (empty($resourceURL)) {
return;
}

$oldResourceURL = \get_post_meta($post->ID, '_owc_pdc_upl_resource', true);

if ($oldResourceURL === $resourceURL) {
return;
}

\update_post_meta($post->ID, '_owc_pdc_upl_resource', $resourceURL);
}

/**
Expand Down Expand Up @@ -61,7 +95,7 @@ protected function getURLFromMatch(array $matches): string

public function objectIsPDC(int $postID): bool
{
$post = get_post($postID);
$post = \get_post($postID);

if ($post->post_type !== 'pdc-item') {
return false;
Expand Down
16 changes: 8 additions & 8 deletions src/Base/Metabox/MetaboxServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace OWC\PDC\Base\Metabox;

use OWC\PDC\Base\Support\Traits\RequestUPL;
use OWC\PDC\Base\Metabox\Handlers\UPLNameHandler;
use OWC\PDC\Base\Metabox\Handlers\UPLResourceHandler;
use OWC\PDC\Base\Support\Traits\RequestUPL;

class MetaboxServiceProvider extends MetaboxBaseServiceProvider
{
use RequestUPL;

/**
* Register the hooks.
*
* @return void
*/
public function register()
{
$this->plugin->loader->addFilter('rwmb_meta_boxes', $this, 'registerMetaboxes', 10, 1);
$this->plugin->loader->addAction('updated_post_meta', new UPLResourceHandler(), 'handleUpdatedMeta', 10, 4);
$this->plugin->loader->addAction('updated_post_meta', new UPLResourceHandler(), 'handleUpdatedMetaClassicEditor', 10, 4);
$this->plugin->loader->addAction('rest_after_insert_pdc-item', new UPLResourceHandler(), 'handleUpdatedMetaGutenbergEditor', 10, 3);

}

public function registerMetaboxes(array $rwmbMetaboxes): array
Expand All @@ -34,9 +34,9 @@ public function registerMetaboxes(array $rwmbMetaboxes): array
$configMetaboxes = array_merge($configMetaboxes, $this->plugin->config->get('escape_element_metabox'));
}

if ($this->plugin->settings->useShowOn()) {
$configMetaboxes = $this->getShowOnMetabox($configMetaboxes);
}
if ($this->plugin->settings->useShowOn()) {
$configMetaboxes = $this->getShowOnMetabox($configMetaboxes);
}

$metaboxes = [];

Expand All @@ -57,7 +57,7 @@ private function addOptionsUPL(array $configMetaboxes): array
return $configMetaboxes;
}

protected function getShowOnMetabox(array $configMetaboxes): array
protected function getShowOnMetabox(array $configMetaboxes): array
{
return array_merge($configMetaboxes, $this->plugin->config->get('show_on_metabox'));
}
Expand Down

0 comments on commit e30e16f

Please sign in to comment.