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

refactor: introduce Term helper class to replace static Term helper #1231

Merged
merged 6 commits into from
Jan 9, 2025
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
2 changes: 1 addition & 1 deletion library/Controller/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function getCurrentTermColour()
return false;
}

return \Municipio\Helper\Term::getTermColour($term->term_id, $term->taxonomy);
return \Municipio\Helper\Term::getTermColor($term->term_id, $term->taxonomy);
}

/**
Expand Down
123 changes: 14 additions & 109 deletions library/Helper/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,137 +2,42 @@

namespace Municipio\Helper;

use Municipio\Helper\Term\Term as TermHelper;

/**
* Class Term
*
* @deprecated Use Municipio\Helper\Term\Term instead.
*/
class Term
{
/**
* `getTermColour` returns the colour of a term.
* If no colour is set, it will return the colour of the first ancestor that has a colour set.
*
* @param int|string|WP_Term $term The term to get the colour for. Can be a term object, term ID or term slug.
* @param string $taxonomy The taxonomy of the term. Default is an empty string.
*
* @return false|string A string of the colour of the term in HEX format.
*/
public static function getTermColour($term, string $taxonomy = '')
{
$term = self::getTerm($term, $taxonomy);

if (empty($term)) {
return false;
}

$colour = get_field('colour', $term);
if (is_string($colour) && "" !== $colour && !str_starts_with($colour, '#')) {
$colour = "#{$colour}";
} elseif ("" === $colour || !$colour) {
$colour = self::getAncestorTermColor($term);
}

return apply_filters('Municipio/getTermColour', $colour, $term, $taxonomy);
}

/**
* Gets term color from ancestor term
* @param WP_Term $term The term to get the color for. Can be a term object, term ID or term slug.
*
* @return string|false
*/
private static function getAncestorTermColor(\WP_Term $term)
{
$ancestors = get_ancestors($term->term_id, $term->taxonomy, 'taxonomy');
if (!empty($ancestors)) {
foreach ($ancestors as $ancestorId) {
$color = get_field('colour', 'term_' . $ancestorId);
if ($color) {
return $color;
}
}
}

return false;
}

/**
* Get term based on type.
*
* @param string|int|WP_Term $term The term to get
* @param string $taxonomy The taxonomy of the term. Default is an empty string.
*/
private static function getTerm($term, string $taxonomy = '')
{
if (is_a($term, 'WP_Term')) {
return $term;
}

if (empty($taxonomy)) {
return false;
}

if (is_int($term)) {
return get_term_by('term_id', $term, $taxonomy);
}

if (is_string($term)) {
return get_term_by('slug', $term, $taxonomy);
}

return false;
}

/**
* Alias with American English spelling for getTermColour()
*
* @deprecated Use Municipio\Helper\Term\Term::getTermColor() instead.
*/
public static function getTermColor($term, string $taxonomy = '')
{
return self::getTermColour($term, $taxonomy);
trigger_error('Use Municipio\Helper\Term\Term::getTermColor() instead.', E_USER_DEPRECATED);
$termHelper = new TermHelper(WpService::get(), AcfService::get());
return $termHelper->getTermColor($term, $taxonomy);
}

/**
* Returns the icon for a given term and taxonomy.
*
* @deprecated Use Municipio\Helper\Term\Term::getTermIcon() instead.
*
* @param mixed $term The term to retrieve the icon for. Can be a WP_Term object, ID, or slug.
* @param string $taxonomy The taxonomy of the term. (not needed if $term is a WP_Term object)
*
* @return mixed|array|false The icon of the term and the icon type or false if it can't be found.
*/
public static function getTermIcon($term, string $taxonomy = '')
{
$term = self::getTerm($term, $taxonomy);

if (empty($term)) {
return false;
}

$termIcon = get_field('icon', $term);
$type = !empty($termIcon['type']) ? $termIcon['type'] : false;
if ($type === 'svg' && !empty($termIcon['svg']['ID'])) {
$attachment = wp_get_attachment_image_url($termIcon['svg']['ID'], 'full');
$result = apply_filters(
'Municipio/getTermIconSvg',
[
'src' => $attachment,
'type' => $type,
'description' => $termIcon['svg']['description'],
'alt' => $termIcon['svg']['description']
],
$term
);
} elseif ($type === 'icon' && !empty($termIcon['material_icon'])) {
$result = apply_filters(
'Municipio/getTermIcon',
[
'src' => $termIcon['material_icon'],
'type' => $type
],
$term
);
} else {
$result = false;
}
trigger_error('Use Municipio\Helper\Term\Term::getTermIcon() instead.', E_USER_DEPRECATED);
$termHelper = new TermHelper(WpService::get(), AcfService::get());

return $result;
return $termHelper->getTermIcon($term, $taxonomy);
}
}
14 changes: 14 additions & 0 deletions library/Helper/Term/Contracts/GetTermColor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Municipio\Helper\Term\Contracts;

interface GetTermColor
{
/**
* Get the color associated with a term.
*
* @param int $termId The term to get the colour for. Can be a term object, term ID or term slug.
* @return string The color associated with the term or false if no color is set.
*/
public function getTermColor(int|string|\WP_Term $term, string $taxonomy = ''): false|string;
}
15 changes: 15 additions & 0 deletions library/Helper/Term/Contracts/GetTermIcon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Municipio\Helper\Term\Contracts;

interface GetTermIcon
{
/**
* Get the icon for a given term.
*
* @param int $termId * @param mixed $term The term to retrieve the icon for. Can be a WP_Term object, ID, or slug.
* @param string $taxonomy The taxonomy of the term. (not needed if $term is a WP_Term object)
* @return array|false The icon of the term and the icon type or false if it can't be found.
*/
public function getTermIcon(int|string|\WP_Term $term, string $taxonomy = ''): array|false;
}
141 changes: 141 additions & 0 deletions library/Helper/Term/Term.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace Municipio\Helper\Term;

use AcfService\Contracts\GetField;
use Municipio\Helper\Term\Contracts\GetTermColor;
use Municipio\Helper\Term\Contracts\GetTermIcon;
use WpService\Contracts\ApplyFilters;
use WpService\Contracts\GetAncestors;
use WpService\Contracts\GetTermBy;
use WpService\Contracts\WpGetAttachmentImageUrl;

/**
* Class Term
*/
class Term implements GetTermColor, GetTermIcon
{
/**
* Constructor.
*/
public function __construct(
private GetTermBy&ApplyFilters&GetAncestors&WpGetAttachmentImageUrl $wpService,
private GetField $acfService
) {
}

/**
* @inheritDoc
*/
public function getTermColor(int|string|\WP_Term $term, string $taxonomy = ''): false|string
{
if (empty($term)) {
return false;
}

$term = $this->getTerm($term, $taxonomy);

if (empty($term)) {
return false;
}

$color = $this->acfService->getField('colour', 'term_' . $term->term_id);

if (is_string($color) && "" !== $color && !str_starts_with($color, '#')) {
$color = "#{$color}";
} elseif ("" === $color || !$color) {
$color = $this->getAncestorTermColor($term);
}

return $this->wpService->applyFilters('Municipio/getTermColour', $color, $term, $taxonomy);
}

/**
* Gets term color from ancestor term
* @param WP_Term $term The term to get the color for. Can be a term object, term ID or term slug.
*
* @return string|false
*/
public function getAncestorTermColor(\WP_Term $term): string|false
{
$ancestors = $this->wpService->getAncestors($term->term_id, $term->taxonomy, 'taxonomy');
if (!empty($ancestors)) {
foreach ($ancestors as $ancestorId) {
$color = $this->acfService->getField('colour', 'term_' . $ancestorId);
if ($color) {
return $color;
}
}
}

return false;
}

/**
* Get term based on type.
*
* @param string|int|WP_Term $term The term to get
* @param string $taxonomy The taxonomy of the term. Default is an empty string.
*/
private function getTerm($term, string $taxonomy = ''): \WP_Term|false
{
if (is_a($term, 'WP_Term')) {
return $term;
}

if (empty($taxonomy)) {
return false;
}

if (is_int($term)) {
return $this->wpService->getTermBy('term_id', $term, $taxonomy, 'OBJECT');
}

if (is_string($term)) {
return $this->wpService->getTermBy('slug', $term, $taxonomy, 'OBJECT');
}

return false;
}

/**
* @inheritDoc
*/
public function getTermIcon(int|string|\WP_Term $term, string $taxonomy = ''): array|false
{
$term = self::getTerm($term, $taxonomy);

if (empty($term)) {
return false;
}

$termIcon = $this->acfService->getField('icon', 'term_' . $term->term_id);
$type = !empty($termIcon['type']) ? $termIcon['type'] : false;
if ($type === 'svg' && !empty($termIcon['svg']['ID'])) {
$attachment = $this->wpService->wpGetAttachmentImageUrl($termIcon['svg']['ID'], 'full');
$result = $this->wpService->applyFilters(
'Municipio/getTermIconSvg',
[
'src' => $attachment,
'type' => $type,
'description' => $termIcon['svg']['description'],
'alt' => $termIcon['svg']['description']
],
$term
);
} elseif ($type === 'icon' && !empty($termIcon['material_icon'])) {
$result = $this->wpService->applyFilters(
'Municipio/getTermIcon',
[
'src' => $termIcon['material_icon'],
'type' => $type
],
$term
);
} else {
$result = false;
}

return $result;
}
}
Loading
Loading