Skip to content

Commit

Permalink
Fix campaign categories being overwritten by users from different age…
Browse files Browse the repository at this point in the history
…ncies

Add a function called has_attached_terms_not_in_context to catch if a post has a category attached that does not relate to current Agengy context.

Add a property called run_has_attached_terms_not_in_context to enable this on a taxonomy basis. Currently enabled for Campaign_Category, could also be enabled for other taxonomies that extend Content_Category e.g. News_Category.

Resolves: https://dsdmoj.atlassian.net/jira/software/c/projects/CDPT/boards/1154?selectedIssue=CDPT-1253
  • Loading branch information
EarthlingDavey committed Jan 26, 2024
1 parent a2ab06b commit 0782a7f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ class Campaign_Category extends Content_Category
'assign_terms' => 'assign_campaign_categories',
),
);

protected $run_has_attached_terms_not_in_context = true;

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ public function context_has_terms()
return false;
}

/**
* For the current post, check if any terms in this category are:
* - attached to the post and,
* - do not relate to current Agengy context
*
* @return bool
*/

public function has_attached_terms_not_in_context()
{
global $post;

if (!is_object($post)) return;

$terms = get_the_terms($post->ID, $this->name);
$context = Agency_Context::get_agency_context('term_id');

if (!$terms) return;

foreach ($terms as $term) {
$term_agencies = get_field('term_used_by', $this->name . '_' . $term->term_id);

if (is_array($term_agencies) && !in_array($context, $term_agencies)) {
return true;
}
}

return;
}



/**
* Remove the default category metabox
*/
Expand All @@ -75,11 +107,19 @@ public function remove_default_meta_box()
*/
public function add_custom_category_meta_box()
{
if ($this->context_has_terms()) {
foreach ($this->object_types as $type) {
add_meta_box($this->name, $this->args['labels']['name'], array($this, 'show_custom_category_meta_box'), $type, 'side');
}

if (!$this->context_has_terms()) {
return;
}

if(!empty($this->run_has_attached_terms_not_in_context) && $this->has_attached_terms_not_in_context()) {
return;
}

foreach ($this->object_types as $type) {
add_meta_box($this->name, $this->args['labels']['name'], array($this, 'show_custom_category_meta_box'), $type, 'side');
}

}

/**
Expand Down

0 comments on commit 0782a7f

Please sign in to comment.