Util.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Utilty Class |
| 6 | * |
| 7 | * @package Custom_Post_Type_Permalinks |
| 8 | * @since 0.9.4 |
| 9 | * |
| 10 | * */ |
| 11 | |
| 12 | class CPTP_Util { |
| 13 | |
| 14 | private function __construct() { |
| 15 | } |
| 16 | |
| 17 | public static function get_post_types() { |
| 18 | return get_post_types( array( '_builtin' => false, 'publicly_queryable' => true, 'show_ui' => true ) ); |
| 19 | } |
| 20 | |
| 21 | public static function get_taxonomies( $objects = false ) { |
| 22 | if ( $objects ) { |
| 23 | $output = 'objects'; |
| 24 | } |
| 25 | else { |
| 26 | $output = 'names'; |
| 27 | } |
| 28 | return get_taxonomies( array( 'show_ui' => true, '_builtin' => false ), $output ); |
| 29 | } |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * |
| 34 | * Get Custom Taxonomies parents. |
| 35 | * @version 1.0 |
| 36 | * |
| 37 | */ |
| 38 | public static function get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) { |
| 39 | $chain = ''; |
| 40 | $parent = get_term( $id, $taxonomy ); |
| 41 | if ( is_wp_error( $parent ) ) { |
| 42 | return $parent; |
| 43 | } |
| 44 | |
| 45 | if ( $nicename ){ |
| 46 | $name = $parent->slug; |
| 47 | }else { |
| 48 | $name = $parent->name; |
| 49 | } |
| 50 | |
| 51 | if ( $parent->parent && ( $parent->parent != $parent->term_id ) && ! in_array( $parent->parent, $visited ) ) { |
| 52 | $visited[] = $parent->parent; |
| 53 | $chain .= CPTP_Util::get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited ); |
| 54 | } |
| 55 | |
| 56 | if ( $link ) { |
| 57 | $chain .= '<a href="' . get_term_link( $parent->term_id, $taxonomy ) . '" title="' . esc_attr( sprintf( __( 'View all posts in %s' ), $parent->name ) ) . '">'.esc_html( $name ).'</a>' .esc_html( $separator ); |
| 58 | }else { |
| 59 | $chain .= $name.$separator; |
| 60 | } |
| 61 | return $chain; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get permalink structure. |
| 66 | * |
| 67 | * @since 0.9.6 |
| 68 | * @param string|object $post_type post type name. / object post type object. |
| 69 | * @return string post type structure. |
| 70 | */ |
| 71 | public static function get_permalink_structure( $post_type ) { |
| 72 | if ( is_string( $post_type ) ) { |
| 73 | $pt_object = get_post_type_object( $post_type ); |
| 74 | } |
| 75 | else { |
| 76 | $pt_object = $post_type; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | if ( !empty( $pt_object->cptp_permalink_structure ) ) { |
| 81 | $structure = $pt_object->cptp_permalink_structure; |
| 82 | } |
| 83 | else { |
| 84 | |
| 85 | $structure = get_option( $pt_object->name.'_structure' ); |
| 86 | } |
| 87 | |
| 88 | return apply_filters( 'CPTP_'.$pt_object->name.'_structure', $structure ); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * Get permalink structure front for date archive. |
| 94 | * |
| 95 | * @since 1.0.0 |
| 96 | * @param string $post_type post type name. |
| 97 | * @return string |
| 98 | */ |
| 99 | public static function get_date_front( $post_type ) { |
| 100 | $structure = CPTP_Util::get_permalink_structure( $post_type ); |
| 101 | |
| 102 | $front = ''; |
| 103 | |
| 104 | preg_match_all( '/%.+?%/', $structure, $tokens ); |
| 105 | $tok_index = 1; |
| 106 | foreach ( (array) $tokens[0] as $token ) { |
| 107 | if ( '%post_id%' == $token && ($tok_index <= 3) ) { |
| 108 | $front = '/date'; |
| 109 | break; |
| 110 | } |
| 111 | $tok_index++; |
| 112 | } |
| 113 | |
| 114 | return $front; |
| 115 | |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | } |
| 121 |