PluginProbe
Friends / 4.2.1
Friends v4.2.1
4.3.2 4.3.1 4.3.0 4.2.2 4.2.1 4.2.0 4.1.0 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.0 2.9.1 All 88 releases
friends / includes / class-friend-tag.php

class-friend-tag.php in Friends 4.2.1, at includes/class-friend-tag.php

215 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Friend Tag Taxonomy
4 *
5 * Handles the friend_tag taxonomy for categorizing and filtering friend posts.
6 *
7 * @package Friends
8 */
9
10 namespace Friends;
11
12 /**
13 * Friend_Tag class for managing the friend_tag taxonomy.
14 */
15 class Friend_Tag {
16 /**
17 * The taxonomy name.
18 */
19 const TAXONOMY = 'friend_tag';
20
21 /**
22 * Prefix for mention tags.
23 */
24 const MENTION_PREFIX = 'mention-';
25
26 /**
27 * Register the friend_tag taxonomy.
28 */
29 public static function register() {
30 $labels = array(
31 'name' => __( 'Friend Tags', 'friends' ),
32 'singular_name' => __( 'Friend Tag', 'friends' ),
33 'search_items' => __( 'Search Friend Tags', 'friends' ),
34 'all_items' => __( 'All Friend Tags', 'friends' ),
35 'edit_item' => __( 'Edit Friend Tag', 'friends' ),
36 'update_item' => __( 'Update Friend Tag', 'friends' ),
37 'add_new_item' => __( 'Add New Friend Tag', 'friends' ),
38 'new_item_name' => __( 'New Friend Tag Name', 'friends' ),
39 'menu_name' => __( 'Friend Tags', 'friends' ),
40 );
41
42 $args = array(
43 'hierarchical' => false,
44 'labels' => $labels,
45 'show_ui' => true,
46 'show_admin_column' => true,
47 'query_var' => true,
48 'rewrite' => array( 'slug' => 'friend-tag' ),
49 'public' => false,
50 'publicly_queryable' => false,
51 'show_in_rest' => true,
52 );
53
54 register_taxonomy( self::TAXONOMY, Friends::CPT, $args );
55 }
56
57 /**
58 * Check if a post has a specific tag.
59 *
60 * @param int $post_id The post ID.
61 * @param string $tag The tag slug to check for.
62 * @return bool True if the post has the tag.
63 */
64 public static function has_tag( $post_id, $tag ) {
65 $terms = wp_get_post_terms( $post_id, self::TAXONOMY, array( 'fields' => 'slugs' ) );
66 if ( is_wp_error( $terms ) ) {
67 return false;
68 }
69 return in_array( $tag, $terms, true );
70 }
71
72 /**
73 * Check if a post has any tag with a given prefix.
74 *
75 * @param int $post_id The post ID.
76 * @param string $prefix The tag prefix to check for.
77 * @return bool True if the post has a tag with the prefix.
78 */
79 public static function has_tag_prefix( $post_id, $prefix ) {
80 $terms = wp_get_post_terms( $post_id, self::TAXONOMY, array( 'fields' => 'slugs' ) );
81 if ( is_wp_error( $terms ) ) {
82 return false;
83 }
84 foreach ( $terms as $slug ) {
85 if ( 0 === strpos( $slug, $prefix ) ) {
86 return true;
87 }
88 }
89 return false;
90 }
91
92 /**
93 * Check if a post has any mention tag.
94 *
95 * @param int $post_id The post ID.
96 * @return bool True if the post has a mention tag.
97 */
98 public static function has_mention( $post_id ) {
99 return self::has_tag_prefix( $post_id, self::MENTION_PREFIX );
100 }
101
102 /**
103 * Get all tags for a post.
104 *
105 * @param int $post_id The post ID.
106 * @param string $fields What fields to return ('slugs', 'names', 'ids', 'all').
107 * @return array Array of tags or empty array on error.
108 */
109 public static function get_tags( $post_id, $fields = 'slugs' ) {
110 $terms = wp_get_post_terms( $post_id, self::TAXONOMY, array( 'fields' => $fields ) );
111 if ( is_wp_error( $terms ) ) {
112 return array();
113 }
114 return $terms;
115 }
116
117 /**
118 * Get all mention tags for a post.
119 *
120 * @param int $post_id The post ID.
121 * @return array Array of mention tag slugs.
122 */
123 public static function get_mention_tags( $post_id ) {
124 $terms = self::get_tags( $post_id, 'slugs' );
125 return array_filter(
126 $terms,
127 function ( $slug ) {
128 return 0 === strpos( $slug, self::MENTION_PREFIX );
129 }
130 );
131 }
132
133 /**
134 * Add a tag to a post.
135 *
136 * @param int $post_id The post ID.
137 * @param string $tag The tag slug to add.
138 * @param bool $append Whether to append to existing tags (default true).
139 * @return array|false|\WP_Error Array of term IDs on success, false or WP_Error on failure.
140 */
141 public static function add_tag( $post_id, $tag, $append = true ) {
142 return wp_set_post_terms( $post_id, array( $tag ), self::TAXONOMY, $append );
143 }
144
145 /**
146 * Add multiple tags to a post.
147 *
148 * @param int $post_id The post ID.
149 * @param array $tags Array of tag slugs to add.
150 * @param bool $append Whether to append to existing tags (default true).
151 * @return array|false|\WP_Error Array of term IDs on success, false or WP_Error on failure.
152 */
153 public static function add_tags( $post_id, $tags, $append = true ) {
154 return wp_set_post_terms( $post_id, $tags, self::TAXONOMY, $append );
155 }
156
157 /**
158 * Add a mention tag for a user.
159 *
160 * @param int $post_id The post ID.
161 * @param string $username The username to create a mention tag for.
162 * @return array|false|\WP_Error Array of term IDs on success, false or WP_Error on failure.
163 */
164 public static function add_mention( $post_id, $username ) {
165 return self::add_tag( $post_id, self::MENTION_PREFIX . $username );
166 }
167
168 /**
169 * Create a mention tag slug for a username.
170 *
171 * @param string $username The username.
172 * @return string The mention tag slug.
173 */
174 public static function mention_tag( $username ) {
175 return self::MENTION_PREFIX . $username;
176 }
177
178 /**
179 * Remove a tag from a post.
180 *
181 * @param int $post_id The post ID.
182 * @param string $tag The tag slug to remove.
183 * @return bool True on success, false on failure.
184 */
185 public static function remove_tag( $post_id, $tag ) {
186 $term = get_term_by( 'slug', $tag, self::TAXONOMY );
187 if ( ! $term ) {
188 return false;
189 }
190 return wp_remove_object_terms( $post_id, $term->term_id, self::TAXONOMY );
191 }
192
193 /**
194 * Clean up orphaned friend tags that have no posts.
195 */
196 public static function cleanup_orphaned() {
197 $terms = get_terms(
198 array(
199 'taxonomy' => self::TAXONOMY,
200 'hide_empty' => false,
201 )
202 );
203
204 if ( is_wp_error( $terms ) ) {
205 return;
206 }
207
208 foreach ( $terms as $term ) {
209 if ( 0 === $term->count ) {
210 wp_delete_term( $term->term_id, self::TAXONOMY );
211 }
212 }
213 }
214 }
215