Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix campaign categories being overwritten by different agencies #465

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading