| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Tag Groups |
| 5 |
* |
| 6 |
* @package Tag Groups |
| 7 |
* @author Christoph Amthor |
| 8 |
* @copyright 2018 Christoph Amthor (@ Chatty Mango, chattymango.com) |
| 9 |
* @license GPL-3.0+ |
| 10 |
* @since 0.38 |
| 11 |
* |
| 12 |
*/ |
| 13 |
|
| 14 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps, Squiz.Scope.MethodScope.Missing, PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 15 |
if (!class_exists('TagGroups_Taxonomy')) { |
| 16 |
class TagGroups_Taxonomy |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Constructor |
| 20 |
* |
| 21 |
* |
| 22 |
*/ |
| 23 |
public function __construct() |
| 24 |
{ |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Removes taxonomy names that are not registered with this WP site as public |
| 29 |
* |
| 30 |
* |
| 31 |
* @param array $taxonomy_slugs |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public static function remove_invalid($taxonomy_slugs) |
| 35 |
{ |
| 36 |
if (!is_array($taxonomy_slugs)) { |
| 37 |
$taxonomy_slugs = array( $taxonomy_slugs ); |
| 38 |
} |
| 39 |
$valid_taxonomy_slugs = self::get_public_taxonomies(); |
| 40 |
return array_values(array_intersect($taxonomy_slugs, $valid_taxonomy_slugs)); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Returns taxonomy names that are enabled in the options |
| 45 |
* |
| 46 |
* |
| 47 |
* @param array|string $intersect_taxonomy_slugs Optional array of taxonomy names that needs to be intersected |
| 48 |
* @return array |
| 49 |
*/ |
| 50 |
public static function get_enabled_taxonomies($intersect_taxonomy_slugs = null) |
| 51 |
{ |
| 52 |
if (!empty($intersect_taxonomy_slugs) && !is_array($intersect_taxonomy_slugs)) { |
| 53 |
$intersect_taxonomy_slugs = array( $intersect_taxonomy_slugs ); |
| 54 |
} |
| 55 |
$tag_group_taxonomies = TagGroups_Options::get_option('tag_group_taxonomy', array( 'post_tag' )); |
| 56 |
$valid_taxonomy_slugs = self::get_public_taxonomies(); |
| 57 |
|
| 58 |
if (empty($intersect_taxonomy_slugs)) { |
| 59 |
return array_values(array_intersect($tag_group_taxonomies, $valid_taxonomy_slugs)); |
| 60 |
} else { |
| 61 |
return array_values(array_intersect($tag_group_taxonomies, $valid_taxonomy_slugs, $intersect_taxonomy_slugs)); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns taxonomy names that are enabled in the options for the metabox |
| 67 |
* |
| 68 |
* |
| 69 |
* @param array $intersect_taxonomy_slugs Optional array of taxonomy names that needs to be intersected |
| 70 |
* @return array |
| 71 |
*/ |
| 72 |
public static function get_taxonomies_for_metabox($intersect_taxonomy_slugs = null) |
| 73 |
{ |
| 74 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 75 |
$tag_group_taxonomies = TagGroups_Options::get_option('tag_group_taxonomy', array( 'post_tag' )); |
| 76 |
$tag_group_meta_box_taxonomies = TagGroups_Options::get_option('tag_group_meta_box_taxonomy', array()); |
| 77 |
$valid_taxonomy_slugs = self::get_public_taxonomies(); |
| 78 |
|
| 79 |
if (empty($intersect_taxonomy_slugs)) { |
| 80 |
return array_values(array_intersect($tag_group_taxonomies, $valid_taxonomy_slugs, $tag_group_meta_box_taxonomies)); |
| 81 |
} |
| 82 |
|
| 83 |
return array_values(array_intersect($tag_group_taxonomies, $valid_taxonomy_slugs, $tag_group_meta_box_taxonomies, $intersect_taxonomy_slugs)); |
| 84 |
} |
| 85 |
|
| 86 |
return array(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Returns taxonomy names that are registered with this WP site |
| 91 |
* |
| 92 |
* |
| 93 |
* @param void |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
public static function get_public_taxonomies() |
| 97 |
{ |
| 98 |
return get_taxonomies(array( |
| 99 |
'public' => true, |
| 100 |
), 'names'); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Retrieves post types from taxonomies |
| 105 |
* |
| 106 |
* @param array|string $taxonomy_slugs |
| 107 |
* @return array |
| 108 |
*/ |
| 109 |
static function post_types_from_taxonomies($taxonomy_slugs = array()) |
| 110 |
{ |
| 111 |
if (!is_array($taxonomy_slugs)) { |
| 112 |
$taxonomy_slugs = array( $taxonomy_slugs ); |
| 113 |
} |
| 114 |
$taxonomy_slugs = array_values($taxonomy_slugs); |
| 115 |
$post_types = array(); |
| 116 |
foreach ($taxonomy_slugs as $taxonomy) { |
| 117 |
$post_type_a = array(); |
| 118 |
$taxonomy_o = get_taxonomy($taxonomy); |
| 119 |
/** |
| 120 |
* The return value of get_taxonomy can be false |
| 121 |
*/ |
| 122 |
if (!empty($taxonomy_o)) { |
| 123 |
$post_type_a = $taxonomy_o->object_type; |
| 124 |
} |
| 125 |
if (!empty($post_type_a)) { |
| 126 |
foreach ($post_type_a as $post_type) { |
| 127 |
if (!in_array($post_type, $post_types)) { |
| 128 |
$post_types[] = $post_type; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
return $post_types; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Gets the taxonomy name for a given slug |
| 138 |
* |
| 139 |
* |
| 140 |
* @param string $taxonomy_slug |
| 141 |
* @return string name |
| 142 |
*/ |
| 143 |
public static function get_name_from_slug($taxonomy_slug) |
| 144 |
{ |
| 145 |
$taxonomy = get_taxonomy($taxonomy_slug); |
| 146 |
|
| 147 |
if (is_object($taxonomy) && is_object($taxonomy->labels)) { |
| 148 |
return $taxonomy->labels->name; |
| 149 |
} else { |
| 150 |
return $taxonomy_slug; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Wrapper for backwards compatibility |
| 156 |
* |
| 157 |
* @deprecated |
| 158 |
* @param array $intersect_taxonomy_slugs Optional array of taxonomy names that needs to be intersected |
| 159 |
* @return array |
| 160 |
*/ |
| 161 |
public static function get_metabox($intersect_taxonomy_slugs = null) |
| 162 |
{ |
| 163 |
TagGroups_Error::deprecated(); |
| 164 |
return self::get_taxonomies_for_metabox($intersect_taxonomy_slugs); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns the URL to a tag groups admin page |
| 169 |
* |
| 170 |
* |
| 171 |
* @param string $taxonomy |
| 172 |
* @return string |
| 173 |
*/ |
| 174 |
public static function get_tag_group_admin_url($taxonomy) |
| 175 |
{ |
| 176 |
$post_type = TagGroups_Utilities::get_first_element(self::post_types_from_taxonomies($taxonomy)); |
| 177 |
|
| 178 |
if ('post' == $post_type) { |
| 179 |
$rel_url = 'edit.php?page=tag-groups_' . $post_type; |
| 180 |
} else { |
| 181 |
$rel_url = 'edit.php?post_type=' . $post_type . '&page=tag-groups_' . $post_type; |
| 182 |
} |
| 183 |
|
| 184 |
return admin_url($rel_url); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Update the list of enabled taxonomies and trigger required actions |
| 189 |
* |
| 190 |
* @param array $taxonomies |
| 191 |
* @return void |
| 192 |
*/ |
| 193 |
public static function update_enabled($taxonomies) |
| 194 |
{ |
| 195 |
|
| 196 |
if (TagGroups_Options::update_option('tag_group_taxonomy', $taxonomies)) { |
| 197 |
// trigger actions |
| 198 |
do_action('tag_groups_taxonomies_saved', $taxonomies); |
| 199 |
if ( |
| 200 |
TagGroups_Utilities::is_premium_plan() |
| 201 |
&& (!defined('TAG_GROUPS_DISABLE_CACHE_REBUILD') || TAG_GROUPS_DISABLE_CACHE_REBUILD) |
| 202 |
) { |
| 203 |
TagGroups_Cron::schedule_in_secs(2, 'tag_groups_run_post_migration'); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|