| 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 |
// wp_set_object_terms() inserts one term_relationships row per entry without |
| 155 |
// deduplicating, so a duplicate slug here triggers a duplicate key DB error. |
| 156 |
return wp_set_post_terms( $post_id, array_unique( $tags ), self::TAXONOMY, $append ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Add a mention tag for a user. |
| 161 |
* |
| 162 |
* @param int $post_id The post ID. |
| 163 |
* @param string $username The username to create a mention tag for. |
| 164 |
* @return array|false|\WP_Error Array of term IDs on success, false or WP_Error on failure. |
| 165 |
*/ |
| 166 |
public static function add_mention( $post_id, $username ) { |
| 167 |
return self::add_tag( $post_id, self::MENTION_PREFIX . $username ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Create a mention tag slug for a username. |
| 172 |
* |
| 173 |
* @param string $username The username. |
| 174 |
* @return string The mention tag slug. |
| 175 |
*/ |
| 176 |
public static function mention_tag( $username ) { |
| 177 |
return self::MENTION_PREFIX . $username; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Remove a tag from a post. |
| 182 |
* |
| 183 |
* @param int $post_id The post ID. |
| 184 |
* @param string $tag The tag slug to remove. |
| 185 |
* @return bool True on success, false on failure. |
| 186 |
*/ |
| 187 |
public static function remove_tag( $post_id, $tag ) { |
| 188 |
$term = get_term_by( 'slug', $tag, self::TAXONOMY ); |
| 189 |
if ( ! $term ) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
return wp_remove_object_terms( $post_id, $term->term_id, self::TAXONOMY ); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Clean up orphaned friend tags that have no posts. |
| 197 |
*/ |
| 198 |
public static function cleanup_orphaned() { |
| 199 |
$terms = get_terms( |
| 200 |
array( |
| 201 |
'taxonomy' => self::TAXONOMY, |
| 202 |
'hide_empty' => false, |
| 203 |
) |
| 204 |
); |
| 205 |
|
| 206 |
if ( is_wp_error( $terms ) ) { |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
foreach ( $terms as $term ) { |
| 211 |
if ( 0 === $term->count ) { |
| 212 |
wp_delete_term( $term->term_id, self::TAXONOMY ); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|