PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.4.0
AlphaListing v4.4.0
trunk 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0
alphalisting / src / Shortcode / QueryParts / TermsCommon.php
alphalisting / src / Shortcode / QueryParts Last commit date
Alphabet.php 1 month ago BackToTop.php 1 month ago ColumnGap.php 1 month ago ColumnWidth.php 1 month ago Columns.php 1 month ago ExcludePosts.php 1 month ago ExcludeTerms.php 1 month ago HideEmptyTerms.php 1 month ago HideEmpty_Deprecated.php 1 month ago InstanceId.php 1 month ago ParentPost.php 1 month ago ParentTermCommon.php 1 month ago ParentTermId.php 1 month ago ParentTermSlugOrId.php 1 month ago PostType.php 1 month ago PostsTerms.php 1 month ago SymbolsFirst.php 1 month ago Taxonomy.php 1 month ago TermsCommon.php 1 month ago TermsTerms.php 1 month ago
TermsCommon.php
112 lines
1 <?php
2 /**
3 * Taxonomy Terms Query Part.
4 *
5 * @package alphalisting
6 */
7
8 declare(strict_types=1);
9
10 namespace eslin87\AlphaListing\Shortcode\QueryParts;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 use \eslin87\AlphaListing\Shortcode\Extension;
17 use \eslin87\AlphaListing\Strings;
18
19 /**
20 * Taxonomy Terms Query Part extension common implementation
21 */
22 abstract class TermsCommon extends Extension {
23 /**
24 * The attribute for this Query Part.
25 *
26 * @since 4.0.0
27 * @var string
28 */
29 public $attribute_name = 'terms';
30
31 /**
32 * Get the configured terms.
33 *
34 * @since 4.0.0
35 * @param string $value The shortcode attribute value.
36 * @param array $taxonomies The configured taxonomies.
37 * @return array<string> The terms.
38 */
39 public function get_terms( $value, array $taxonomies ): array {
40 $terms = Strings::maybe_mb_split( ',', $value );
41 $terms = array_map( 'trim', $terms );
42 $terms = array_filter( $terms );
43 $terms = array_unique( $terms );
44 $terms = array_map(
45 function ( string $term ) use ( $taxonomies ) : int {
46 if ( is_numeric( $term ) ) {
47 return intval( $term );
48 } else {
49 foreach ( $taxonomies as $taxonomy ) {
50 $negate = false;
51 $first = substr( $term, 0, 1 );
52 if ( '!' === $first ) {
53 $term = substr( $term, 1 );
54 $negate = true;
55 }
56 $term_obj = get_term_by( 'slug', $term, $taxonomy );
57 if ( false !== $term_obj ) {
58 if ( true === $negate ) {
59 return -$term_obj->term_id;
60 }
61 return $term_obj->term_id;
62 }
63 }
64 }
65 return 0;
66 },
67 $terms
68 );
69 return array_unique( $terms );
70 }
71
72 /**
73 * Get the configured terms for exclusion.
74 *
75 * @since 4.0.0
76 * @param array $terms The terms.
77 * @return array<string> The terms for exclusion.
78 */
79 public function get_exclude_terms( array $terms ): array {
80 $terms = array_filter(
81 $terms,
82 function( int $value ): bool {
83 return 0 > $value;
84 }
85 );
86 $terms = array_map(
87 function( $term ) {
88 return -$term;
89 },
90 $terms
91 );
92 return array_values( array_unique( $terms ) );
93 }
94
95 /**
96 * Get the configured terms for inclusion.
97 *
98 * @since 4.0.0
99 * @param array $terms The terms.
100 * @return array<string> The terms for inclusion.
101 */
102 public function get_include_terms( array $terms ): array {
103 $terms = array_filter(
104 $terms,
105 function( int $value ): bool {
106 return 0 < $value;
107 }
108 );
109 return array_values( array_unique( $terms ) );
110 }
111 }
112