permalink-manager-admin-functions.php
9 years ago
permalink-manager-helper-functions.php
9 years ago
permalink-manager-post-uri-functions.php
9 years ago
permalink-manager-uri-actions.php
9 years ago
permalink-manager-helper-functions.php
124 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) { |
| 31 | global $permalink_manager_options; |
| 32 | |
| 33 | if($permalink_manager_options['miscellaneous']['yoast_primary_term'] == 1 && class_exists('WPSEO_Primary_Term')) { |
| 34 | $primary_term = new WPSEO_Primary_Term($taxonomy, $post_id); |
| 35 | $primary_term = get_term($primary_term->get_primary_term()); |
| 36 | return (!is_wp_error($primary_term)) ? $primary_term->slug : ""; |
| 37 | } else { |
| 38 | return ''; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get post_types array |
| 44 | */ |
| 45 | static function get_post_types_array($format = null, $cpt = null) { |
| 46 | $post_types = apply_filters('permalink-manager-post-types', get_post_types( array('public' => true), 'objects')); |
| 47 | |
| 48 | $post_types_array = array(); |
| 49 | if($format == 'full') { |
| 50 | foreach ( $post_types as $post_type ) { |
| 51 | $post_types_array[$post_type->name] = array('label' => $post_type->labels->name, 'name' => $post_type->name); |
| 52 | } |
| 53 | } else { |
| 54 | foreach ( $post_types as $post_type ) { |
| 55 | $post_types_array[$post_type->name] = $post_type->labels->name; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return (empty($cpt)) ? $post_types_array : $post_types_array[$cpt]; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get permastruct |
| 64 | */ |
| 65 | static function get_default_permastruct($post_type = 'page', $remove_post_tag = false) { |
| 66 | global $wp_rewrite; |
| 67 | |
| 68 | // Get default permastruct |
| 69 | if($post_type == 'page') { |
| 70 | $permastruct = $wp_rewrite->get_page_permastruct(); |
| 71 | } else if($post_type == 'post') { |
| 72 | $permastruct = get_option('permalink_structure'); |
| 73 | } else { |
| 74 | $permastruct = $wp_rewrite->get_extra_permastruct($post_type); |
| 75 | } |
| 76 | |
| 77 | return ($remove_post_tag) ? trim(str_replace(array("%postname%", "%pagename%", "%{$post_type}%"), "", $permastruct), "/") : $permastruct; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Structure Tags & Rewrite functions |
| 82 | */ |
| 83 | static function get_all_structure_tags($code = true, $seperator = ', ', $hide_slug_tags = true) { |
| 84 | global $wp_rewrite; |
| 85 | |
| 86 | $tags = $wp_rewrite->rewritecode; |
| 87 | $output = ""; |
| 88 | $last_tag_index = count($tags); |
| 89 | $i = 1; |
| 90 | |
| 91 | // Hide slug tags |
| 92 | if($hide_slug_tags) { |
| 93 | $post_types = Permalink_Manager_Helper_Functions::get_post_types_array(); |
| 94 | foreach($post_types as $post_type => $post_type_name) { |
| 95 | $post_type_tag = Permalink_Manager_Helper_Functions::get_post_tag($post_type); |
| 96 | // Find key with post type tag from rewritecode |
| 97 | $key = array_search($post_type_tag, $tags); |
| 98 | if($key) { unset($tags[$key]); } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | foreach($tags as $tag) { |
| 103 | $sep = ($last_tag_index == $i) ? "" : $seperator; |
| 104 | $output .= ($code) ? "<code>{$tag}</code>{$sep}" : "{$tag}{$sep}"; |
| 105 | $i++; |
| 106 | } |
| 107 | |
| 108 | return $output; |
| 109 | } |
| 110 | |
| 111 | static function get_post_tag($post_type) { |
| 112 | // Get the post type (with fix for posts & pages) |
| 113 | if($post_type == 'page') { |
| 114 | $post_type_tag = '%pagename%'; |
| 115 | } else if ($post_type == 'post') { |
| 116 | $post_type_tag = '%postname%'; |
| 117 | } else { |
| 118 | $post_type_tag = "%{$post_type}%"; |
| 119 | } |
| 120 | return $post_type_tag; |
| 121 | } |
| 122 | |
| 123 | } |
| 124 |