-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "When Taxonomy is updated" to notifications #1759
- Loading branch information
1 parent
c6f621c
commit 61bef7f
Showing
6 changed files
with
280 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
lib/Notifications/Workflow/Step/Event/Filter/Taxonomies.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
/** | ||
* @package PublishPress\Notifications | ||
* @author PublishPress <help@publishpress.com> | ||
* @copyright Copyright (c) 2022 PublishPress. All rights reserved. | ||
* @license GPLv2 or later | ||
* @since 1.0.0 | ||
*/ | ||
|
||
namespace PublishPress\Notifications\Workflow\Step\Event\Filter; | ||
|
||
use PP_Notifications; | ||
use PublishPress\Notifications\Workflow\Step\Event\Post_TaxonomyUpdate; | ||
|
||
class Taxonomies extends Base implements Filter_Interface | ||
{ | ||
const META_KEY_TAXONOMIES_FROM = '_psppno_taxonomiesfrom'; | ||
|
||
/** | ||
* Function to render and returnt the HTML markup for the | ||
* Field in the form. | ||
* | ||
* @return string | ||
*/ | ||
public function render() | ||
{ | ||
echo $this->get_service('view')->render( | ||
'workflow_filter_multiple_select', | ||
[ | ||
'name' => esc_attr("publishpress_notif[{$this->step_name}_filters][taxonomies]"), | ||
'id' => esc_attr("publishpress_notif_{$this->step_name}_filters_taxonomies"), | ||
'options' => $this->get_options(), | ||
'labels' => [ | ||
'label' => esc_html__('Taxonomies', 'publishpress'), | ||
], | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Returns a list of taxonomies in the options format | ||
* | ||
* @return array | ||
*/ | ||
protected function get_options() | ||
{ | ||
|
||
$excluded_taxonomies = []; | ||
if (class_exists('\PP_Notifications')) { | ||
$blacklisted_taxonomies = PP_Notifications::getOption('blacklisted_taxonomies'); | ||
if (!empty($blacklisted_taxonomies)) { | ||
$excluded_taxonomies = array_filter(explode(',', $blacklisted_taxonomies)); | ||
} | ||
} | ||
|
||
$taxonomies = get_taxonomies([], 'objects', 'and'); | ||
$metadata = (array)$this->get_metadata(static::META_KEY_TAXONOMIES_FROM); | ||
$options = []; | ||
|
||
foreach ($taxonomies as $tax) { | ||
if (empty($tax->labels->name) || in_array($tax->labels->name, $excluded_taxonomies)) { | ||
continue; | ||
} | ||
$options[] = [ | ||
'value' => esc_attr($tax->name), | ||
'label' => esc_html($tax->labels->name. ' ('.$tax->name.')'), | ||
'selected' => in_array($tax->name, $metadata), | ||
]; | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* Function to save the metadata from the metabox | ||
* | ||
* @param int $id | ||
* @param WP_Post $post | ||
*/ | ||
public function save_metabox_data($id, $post) | ||
{ | ||
if (!isset($_POST['publishpress_notif']["{$this->step_name}_filters"]['taxonomies'])) { | ||
$values = []; | ||
} else { | ||
$values = array_map( | ||
'sanitize_key', | ||
(array)$_POST['publishpress_notif']["{$this->step_name}_filters"]['taxonomies'] | ||
); | ||
} | ||
|
||
$this->update_metadata_array($id, static::META_KEY_TAXONOMIES_FROM, $values); | ||
} | ||
|
||
/** | ||
* Filters and returns the arguments for the query which locates | ||
* workflows that should be executed. | ||
* | ||
* @param array $query_args | ||
* @param array $event_args | ||
* | ||
* @return array | ||
*/ | ||
public function get_run_workflow_query_args($query_args, $event_args) | ||
{ | ||
// Taxonomy | ||
$query_args['meta_query'][] = [ | ||
[ | ||
'key' => static::META_KEY_TAXONOMIES_FROM, | ||
'value' => $event_args['params']['taxonomy'], | ||
'compare' => '=', | ||
], | ||
]; | ||
|
||
return parent::get_run_workflow_query_args($query_args, $event_args); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
lib/Notifications/Workflow/Step/Event/Post_TaxonomyUpdate.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
/** | ||
* @package PublishPress\Notifications | ||
* @author PublishPress <help@publishpress.com> | ||
* @copyright Copyright (c) 2022 PublishPress. All rights reserved. | ||
* @license GPLv2 or later | ||
* @since 1.0.0 | ||
*/ | ||
|
||
namespace PublishPress\Notifications\Workflow\Step\Event; | ||
|
||
use PublishPress\Notifications\Workflow\Step\Event\Filter; | ||
|
||
class Post_TaxonomyUpdate extends Base | ||
{ | ||
const META_KEY_SELECTED = '_psppno_evttaxonomyupdate'; | ||
|
||
const META_VALUE_SELECTED = 'taxonomy_update'; | ||
|
||
const EVENT_NAME = 'taxonomy_update'; | ||
|
||
/** | ||
* The constructorPost_TaxonomyUpdate' | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->name = static::META_VALUE_SELECTED; | ||
$this->label = __('When taxonomy is updated', 'publishpress'); | ||
|
||
parent::__construct(); | ||
|
||
// Add filter to return the metakey representing if it is selected or not | ||
add_filter('psppno_events_metakeys', [$this, 'filter_events_metakeys']); | ||
add_filter('publishpress_notifications_workflow_events', [$this, 'filter_workflow_actions']); | ||
add_filter('publishpress_notifications_event_label', [$this, 'filter_event_label'], 10, 2); | ||
} | ||
|
||
/** | ||
* Filters and returns the arguments for the query which locates | ||
* workflows that should be executed. | ||
* | ||
* @param array $query_args | ||
* @param array $event_args | ||
* | ||
* @return array | ||
*/ | ||
public function filter_running_workflow_query_args($query_args, $event_args) | ||
{ | ||
if ($this->should_ignore_event_on_query($event_args)) { | ||
return $query_args; | ||
} | ||
|
||
if (static::EVENT_NAME === $event_args['event']) { | ||
$query_args['meta_query'][] = [ | ||
'key' => static::META_KEY_SELECTED, | ||
'value' => 1, | ||
'type' => 'BOOL', | ||
'compare' => '=', | ||
]; | ||
|
||
// Check the filters | ||
$filters = $this->get_filters(); | ||
|
||
foreach ($filters as $filter) { | ||
$query_args = $filter->get_run_workflow_query_args($query_args, $event_args); | ||
} | ||
} | ||
|
||
return $query_args; | ||
} | ||
|
||
/** | ||
* Method to return a list of fields to display in the filter area | ||
* | ||
* @param array | ||
* | ||
* @return array | ||
*/ | ||
protected function get_filters($filters = []) | ||
{ | ||
if (!empty($this->cache_filters)) { | ||
return $this->cache_filters; | ||
} | ||
|
||
$step_name = $this->attr_prefix . '_' . $this->name; | ||
|
||
$filters[] = new Filter\Taxonomies($step_name); | ||
|
||
return parent::get_filters($filters); | ||
} | ||
|
||
public function filter_workflow_actions($actions) | ||
{ | ||
if (!is_array($actions) || empty($actions)) { | ||
$actions = []; | ||
} | ||
|
||
$actions[] = static::EVENT_NAME; | ||
|
||
return $actions; | ||
} | ||
|
||
/** | ||
* @param string $label | ||
* @param string $event | ||
* @return string|void | ||
*/ | ||
public function filter_event_label($label, $event) | ||
{ | ||
if ($event === static::EVENT_NAME) { | ||
$label = $this->label; | ||
} | ||
|
||
return $label; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters