permalink-manager-actions.php
8 years ago
permalink-manager-admin-functions.php
8 years ago
permalink-manager-core-functions.php
8 years ago
permalink-manager-helper-functions.php
8 years ago
permalink-manager-third-parties.php
8 years ago
permalink-manager-uri-functions-post.php
8 years ago
permalink-manager-helper-functions.php
364 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Additional functions used in classes and another subclasses |
| 5 | */ |
| 6 | class Permalink_Manager_Helper_Functions extends Permalink_Manager_Class { |
| 7 | |
| 8 | public function __construct() { } |
| 9 | |
| 10 | /** |
| 11 | * Support for multidimensional arrays - array_map() |
| 12 | */ |
| 13 | static function multidimensional_array_map($function, $input) { |
| 14 | $output = array(); |
| 15 | |
| 16 | if(is_array($input)) { |
| 17 | foreach ($input as $key => $val) { |
| 18 | $output[$key] = (is_array($val) ? self::multidimensional_array_map($function, $val) : $function($val)); |
| 19 | } |
| 20 | } else { |
| 21 | $output = $function($input); |
| 22 | } |
| 23 | |
| 24 | return $output; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Get primary term (by Yoast SEO) |
| 29 | */ |
| 30 | static function get_primary_term($post_id, $taxonomy, $slug_only = true) { |
| 31 | global $permalink_manager_options; |
| 32 | |
| 33 | $primary_term_enabled = apply_filters('permalink-manager-primary-term', true); |
| 34 | |
| 35 | if($primary_term_enabled && class_exists('WPSEO_Primary_Term')) { |
| 36 | $primary_term = new WPSEO_Primary_Term($taxonomy, $post_id); |
| 37 | $primary_term = get_term($primary_term->get_primary_term()); |
| 38 | |
| 39 | if(!is_wp_error($primary_term)) { |
| 40 | return ($slug_only) ? $primary_term->slug : $primary_term; |
| 41 | } |
| 42 | } |
| 43 | return ''; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Allow to disable post types and taxonomies |
| 48 | */ |
| 49 | static function get_disabled_post_types() { |
| 50 | global $permalink_manager_options; |
| 51 | |
| 52 | $disabled_post_types = (!empty($permalink_manager_options['general']['partial_disable']['post_types'])) ? (array) $permalink_manager_options['general']['partial_disable']['post_types'] : array(); |
| 53 | return apply_filters('permalink-manager-disabled-post-types', $disabled_post_types); |
| 54 | } |
| 55 | |
| 56 | static function get_disabled_taxonomies() { |
| 57 | global $permalink_manager_options; |
| 58 | |
| 59 | $disabled_taxonomies = (!empty($permalink_manager_options['general']['partial_disable']['taxonomies'])) ? (array) $permalink_manager_options['general']['partial_disable']['taxonomies'] : array(); |
| 60 | return apply_filters('permalink-manager-disabled-taxonomies', $disabled_taxonomies); |
| 61 | } |
| 62 | |
| 63 | static public function is_disabled($content_name, $content_type = 'post_type') { |
| 64 | $out = false; |
| 65 | |
| 66 | if($content_type == 'post_type') { |
| 67 | $disabled_post_types = self::get_disabled_post_types(); |
| 68 | $out = (is_array($disabled_post_types) && in_array($content_name, $disabled_post_types)) ? true : false; |
| 69 | } else { |
| 70 | $disabled_taxonomies = self::get_disabled_taxonomies(); |
| 71 | $out = (is_array($disabled_taxonomies) && in_array($content_name, $disabled_taxonomies)) ? true : false; |
| 72 | } |
| 73 | |
| 74 | return $out; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get post_types array |
| 79 | */ |
| 80 | static function get_post_types_array($format = null, $cpt = null, $all = false) { |
| 81 | $post_types = get_post_types(array('public' => true), 'objects'); |
| 82 | $disabled_post_types = self::get_disabled_post_types(); |
| 83 | |
| 84 | $post_types_array = array(); |
| 85 | if($format == 'full') { |
| 86 | foreach ( $post_types as $post_type ) { |
| 87 | $post_types_array[$post_type->name] = array('label' => $post_type->labels->name, 'name' => $post_type->name); |
| 88 | } |
| 89 | } else { |
| 90 | foreach ( $post_types as $post_type ) { |
| 91 | $post_types_array[$post_type->name] = $post_type->labels->name; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Disable post types |
| 96 | if(!$all && is_array($disabled_post_types)) { |
| 97 | foreach($disabled_post_types as $post_type) { |
| 98 | if(!empty($post_types_array[$post_type])) { unset($post_types_array[$post_type]); } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return (empty($cpt)) ? $post_types_array : $post_types_array[$cpt]; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get array with all taxonomies |
| 107 | */ |
| 108 | static function get_taxonomies_array($format = null, $tax = null, $prefix = false, $all = false) { |
| 109 | $taxonomies = get_taxonomies(array('public' => true, 'rewrite' => true), 'objects'); |
| 110 | $disabled_taxonomies = self::get_disabled_taxonomies(); |
| 111 | |
| 112 | $taxonomies_array = array(); |
| 113 | |
| 114 | foreach($taxonomies as $taxonomy) { |
| 115 | $key = ($prefix) ? "tax-{$taxonomy->name}" : $taxonomy->name; |
| 116 | if($format == 'full') { |
| 117 | $taxonomies_array[$taxonomy->name] = array('label' => $taxonomy->labels->name, 'name' => $taxonomy->name); |
| 118 | } else { |
| 119 | $taxonomies_array[$key] = $taxonomy->labels->name; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Disable taxonomies |
| 124 | if(!$all && is_array($disabled_taxonomies)) { |
| 125 | foreach($disabled_taxonomies as $taxonomy) { |
| 126 | if(!empty($taxonomies_array[$taxonomy])) { unset($taxonomies_array[$taxonomy]); } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return (empty($tax)) ? $taxonomies_array : $taxonomies_array[$tax]; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get permastruct |
| 135 | */ |
| 136 | static function get_default_permastruct($post_type = 'page', $remove_post_tag = false) { |
| 137 | global $wp_rewrite; |
| 138 | |
| 139 | // Get default permastruct |
| 140 | if($post_type == 'page') { |
| 141 | $permastruct = $wp_rewrite->get_page_permastruct(); |
| 142 | } else if($post_type == 'post') { |
| 143 | $permastruct = get_option('permalink_structure'); |
| 144 | } else { |
| 145 | $permastruct = $wp_rewrite->get_extra_permastruct($post_type); |
| 146 | } |
| 147 | |
| 148 | return ($remove_post_tag) ? trim(str_replace(array("%postname%", "%pagename%", "%{$post_type}%"), "", $permastruct), "/") : $permastruct; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get all endpoints |
| 153 | */ |
| 154 | static function get_endpoints() { |
| 155 | global $wp_rewrite; |
| 156 | |
| 157 | // Start with default endpoints |
| 158 | $endpoints = "page|feed|embed|attachment|track|filter"; |
| 159 | |
| 160 | if(!empty($wp_rewrite->endpoints)) { |
| 161 | foreach($wp_rewrite->endpoints as $endpoint) { |
| 162 | $endpoints .= "|{$endpoint[1]}"; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return apply_filters("permalink-manager-endpoints", str_replace("/", "\/", $endpoints)); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Remove post tag from permastructure |
| 171 | */ |
| 172 | static function remove_post_tag($permastruct) { |
| 173 | $post_types = self::get_post_types_array('full'); |
| 174 | |
| 175 | // Get all post tags |
| 176 | $post_tags = array("%postname%", "%pagename%"); |
| 177 | foreach($post_types as $post_type) { |
| 178 | $post_tags[] = "%{$post_type['name']}%"; |
| 179 | } |
| 180 | |
| 181 | $permastruct = str_replace($post_tags, "", $permastruct); |
| 182 | return trim($permastruct, "/"); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Structure Tags & Rewrite functions |
| 187 | */ |
| 188 | static function get_all_structure_tags($code = true, $seperator = ', ', $hide_slug_tags = true) { |
| 189 | global $wp_rewrite; |
| 190 | |
| 191 | $tags = $wp_rewrite->rewritecode; |
| 192 | |
| 193 | // Hide slug tags |
| 194 | if($hide_slug_tags) { |
| 195 | $post_types = Permalink_Manager_Helper_Functions::get_post_types_array(); |
| 196 | foreach($post_types as $post_type => $post_type_name) { |
| 197 | $post_type_tag = Permalink_Manager_Helper_Functions::get_post_tag($post_type); |
| 198 | // Find key with post type tag from rewritecode |
| 199 | $key = array_search($post_type_tag, $tags); |
| 200 | if($key) { unset($tags[$key]); } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | foreach($tags as &$tag) { |
| 205 | $tag = ($code) ? "<code>{$tag}</code>" : "{$tag}"; |
| 206 | } |
| 207 | $output = implode($seperator, $tags); |
| 208 | |
| 209 | return "<span class=\"structure-tags-list\">{$output}</span>"; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Get endpoint used to mark the postname or its equivalent for custom post types and pages in permastructures |
| 214 | */ |
| 215 | static function get_post_tag($post_type) { |
| 216 | // Get the post type (with fix for posts & pages) |
| 217 | if($post_type == 'page') { |
| 218 | $post_type_tag = '%pagename%'; |
| 219 | } else if ($post_type == 'post') { |
| 220 | $post_type_tag = '%postname%'; |
| 221 | } else { |
| 222 | $post_type_tag = "%{$post_type}%"; |
| 223 | } |
| 224 | return $post_type_tag; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Find taxonomy name using "term_id" |
| 229 | */ |
| 230 | static function get_tax_name($term, $term_by = 'id') { |
| 231 | $term_object = get_term_by($term_by, $term); |
| 232 | return (isset($term_object->taxonomy)) ? $term_object->taxonomy : ''; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Get default language (WPML & Polylang) |
| 237 | */ |
| 238 | static function get_language() { |
| 239 | global $sitepress; |
| 240 | $def_lang = ''; |
| 241 | |
| 242 | if(function_exists('pll_default_language')) { |
| 243 | $def_lang = pll_default_language('slug'); |
| 244 | } else if(is_object($sitepress)) { |
| 245 | $def_lang = $sitepress->get_default_language(); |
| 246 | } |
| 247 | |
| 248 | return $def_lang; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Sanitize multidimensional array |
| 253 | */ |
| 254 | static function sanitize_array($data = array()) { |
| 255 | if (!is_array($data) || !count($data)) { |
| 256 | return array(); |
| 257 | } |
| 258 | |
| 259 | foreach ($data as $k => $v) { |
| 260 | if (!is_array($v) && !is_object($v)) { |
| 261 | $data[$k] = htmlspecialchars(trim($v)); |
| 262 | } |
| 263 | if (is_array($v)) { |
| 264 | $data[$k] = self::sanitize_array($v); |
| 265 | } |
| 266 | } |
| 267 | return $data; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Encode URI and keep slashes |
| 272 | */ |
| 273 | static function encode_uri($uri) { |
| 274 | return str_replace("%2F", "/", urlencode($uri)); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Slugify function |
| 279 | */ |
| 280 | public static function sanitize_title($str) { |
| 281 | // Trim slashes & whitespaces |
| 282 | $clean = trim($str, " /"); |
| 283 | |
| 284 | // Remove accents |
| 285 | $clean = remove_accents($clean); |
| 286 | |
| 287 | // $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $clean); |
| 288 | $clean = preg_replace("/[^\p{L}a-zA-Z0-9\/_\.|+ -]/u", '', $clean); |
| 289 | $clean = strtolower(trim($clean, '-')); |
| 290 | $clean = preg_replace("/[_|+ -]+/", "-", $clean); |
| 291 | |
| 292 | return $clean; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Force custom slugs |
| 297 | */ |
| 298 | public static function force_custom_slugs($slug, $object, $flat = false) { |
| 299 | global $permalink_manager_options; |
| 300 | |
| 301 | if(!empty($permalink_manager_options['general']['force_custom_slugs'])) { |
| 302 | $old_slug = basename($slug); |
| 303 | $new_slug = (!empty($object->name)) ? sanitize_title($object->name) : sanitize_title($object->post_title); |
| 304 | |
| 305 | $slug = ($old_slug != $new_slug) ? str_replace($old_slug, $new_slug, $slug) : $slug; |
| 306 | } |
| 307 | |
| 308 | if($flat) { |
| 309 | $slug = preg_replace("/([^\/]+)(.*)/", "$1", $slug); |
| 310 | } |
| 311 | |
| 312 | return $slug; |
| 313 | } |
| 314 | |
| 315 | public static function element_exists($element_id) { |
| 316 | global $wpdb; |
| 317 | |
| 318 | if(strpos($element_id, 'tax-') !== false) { |
| 319 | $term_id = intval(preg_replace("/[^0-9]/", "", $element_id)); |
| 320 | $element_exists = $wpdb->get_var( "SELECT * FROM {$wpdb->prefix}terms WHERE term_id = {$term_id}" ); |
| 321 | } else { |
| 322 | $element_exists = $wpdb->get_var( "SELECT * FROM {$wpdb->prefix}posts WHERE ID = {$element_id} AND post_status NOT IN ('auto-draft', 'trash') AND post_type != 'nav_menu_item'" ); |
| 323 | } |
| 324 | |
| 325 | return (!empty($element_exists)) ? $element_exists : false; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Detect duplicates |
| 330 | */ |
| 331 | public static function get_all_duplicates() { |
| 332 | global $permalink_manager_uris, $permalink_manager_redirects, $permalink_manager_options, $wpdb; |
| 333 | |
| 334 | // Make sure that both variables are arrays |
| 335 | $all_uris = (is_array($permalink_manager_uris)) ? $permalink_manager_uris : array(); |
| 336 | $permalink_manager_redirects = (is_array($permalink_manager_redirects)) ? $permalink_manager_redirects : array(); |
| 337 | |
| 338 | // Convert redirects list, so it can be merged with $permalink_manager_uris |
| 339 | foreach($permalink_manager_redirects as $element_id => $redirects) { |
| 340 | if(is_array($redirects)) { |
| 341 | foreach($redirects as $index => $uri) { |
| 342 | $all_uris["redirect-{$index}_{$element_id}"] = $uri; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Count duplicates |
| 348 | $duplicates_removed = 0; |
| 349 | $duplicates_groups = array(); |
| 350 | $duplicates_list = array_count_values($all_uris); |
| 351 | $duplicates_list = array_filter($duplicates_list, function ($x) { return $x >= 2; }); |
| 352 | |
| 353 | // Assign keys to duplicates (group them) |
| 354 | if(count($duplicates_list) > 0) { |
| 355 | foreach($duplicates_list as $duplicated_uri => $count) { |
| 356 | $duplicates_groups[$duplicated_uri] = array_keys($all_uris, $duplicated_uri); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | return $duplicates_groups; |
| 361 | } |
| 362 | |
| 363 | } |
| 364 |