Admin.php
5 years ago
FlushRules.php
8 years ago
GetArchives.php
5 years ago
Option.php
5 years ago
Permalink.php
5 years ago
Rewrite.php
5 years ago
Setting.php
6 years ago
GetArchives.php
169 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Fix: wp_get_archives fix for custom post |
| 4 | * Ex:wp_get_archives('&post_type='.get_query_var( 'post_type' )); |
| 5 | * |
| 6 | * @package Custom_Post_Type_Permalinks |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Get Archives fix. |
| 11 | * |
| 12 | * @since 0.9.4 |
| 13 | * */ |
| 14 | class CPTP_Module_GetArchives extends CPTP_Module { |
| 15 | |
| 16 | /** |
| 17 | * Register hooks. |
| 18 | */ |
| 19 | public function add_hook() { |
| 20 | if ( get_option( 'permalink_structure', '' ) !== '' ) { |
| 21 | add_filter( 'getarchives_join', array( $this, 'getarchives_join' ), 10, 2 ); |
| 22 | add_filter( 'getarchives_where', array( $this, 'getarchives_where' ), 10, 2 ); |
| 23 | add_filter( 'get_archives_link', array( $this, 'get_archives_link' ), 20, 1 ); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Argument in wp_get_archives. |
| 29 | * |
| 30 | * @var array |
| 31 | */ |
| 32 | public $get_archives_where_r; |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Get archive where. |
| 37 | * |
| 38 | * @param string $where SQL where. |
| 39 | * @param array $r Argument in wp_get_archives. |
| 40 | * |
| 41 | * @return mixed|string |
| 42 | */ |
| 43 | public function getarchives_where( $where, $r ) { |
| 44 | $this->get_archives_where_r = $r; |
| 45 | if ( isset( $r['post_type'] ) ) { |
| 46 | if ( ! in_array( $r['post_type'], CPTP_Util::get_post_types(), true ) ) { |
| 47 | return $where; |
| 48 | } |
| 49 | |
| 50 | $post_type = get_post_type_object( $r['post_type'] ); |
| 51 | if ( ! $post_type ) { |
| 52 | return $where; |
| 53 | } |
| 54 | |
| 55 | if ( ! $post_type->has_archive ) { |
| 56 | return $where; |
| 57 | } |
| 58 | |
| 59 | $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where ); |
| 60 | } |
| 61 | |
| 62 | if ( isset( $r['taxonomy'] ) && is_array( $r['taxonomy'] ) ) { |
| 63 | global $wpdb; |
| 64 | $where = $where . " AND $wpdb->term_taxonomy.taxonomy = '" . $r['taxonomy']['name'] . "' AND $wpdb->term_taxonomy.term_id = '" . $r['taxonomy']['termid'] . "'"; |
| 65 | } |
| 66 | |
| 67 | return $where; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get_archive_join |
| 72 | * |
| 73 | * @author Steve |
| 74 | * @since 0.8 |
| 75 | * @version 1.0 |
| 76 | * |
| 77 | * @param string $join SQL JOIN. |
| 78 | * @param array $r Argument in wp_get_archives. |
| 79 | * |
| 80 | * @return string |
| 81 | */ |
| 82 | public function getarchives_join( $join, $r ) { |
| 83 | global $wpdb; |
| 84 | $this->get_archives_where_r = $r; |
| 85 | if ( isset( $r['taxonomy'] ) && is_array( $r['taxonomy'] ) ) { |
| 86 | $join = $join . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)"; |
| 87 | } |
| 88 | |
| 89 | return $join; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /** |
| 94 | * Filter: get_arcihves_link |
| 95 | * |
| 96 | * @version 2.2 03/27/14 |
| 97 | * |
| 98 | * @param string $html archive <li> html. |
| 99 | * |
| 100 | * @return string |
| 101 | */ |
| 102 | public function get_archives_link( $html ) { |
| 103 | global $wp_rewrite; |
| 104 | |
| 105 | if ( ! isset( $this->get_archives_where_r['post_type'] ) ) { |
| 106 | return $html; |
| 107 | } |
| 108 | |
| 109 | if ( ! in_array( $this->get_archives_where_r['post_type'], CPTP_Util::get_post_types(), true ) ) { |
| 110 | return $html; |
| 111 | } |
| 112 | |
| 113 | if ( 'post' === $this->get_archives_where_r['post_type'] ) { |
| 114 | return $html; |
| 115 | } |
| 116 | |
| 117 | $post_type = get_post_type_object( $this->get_archives_where_r['post_type'] ); |
| 118 | if ( ! $post_type ) { |
| 119 | return $html; |
| 120 | } |
| 121 | |
| 122 | if ( ! $post_type->has_archive ) { |
| 123 | return $html; |
| 124 | } |
| 125 | |
| 126 | $c = isset( $this->get_archives_where_r['taxonomy'] ) && is_array( $this->get_archives_where_r['taxonomy'] ) ? $this->get_archives_where_r['taxonomy'] : ''; |
| 127 | $t = $this->get_archives_where_r['post_type']; |
| 128 | |
| 129 | $this->get_archives_where_r['post_type'] = isset( $this->get_archives_where_r['post_type_slug'] ) ? $this->get_archives_where_r['post_type_slug'] : $t; // [steve] [*** bug fixing] |
| 130 | |
| 131 | if ( isset( $this->get_archives_where_r['post_type'] ) && 'postbypost' !== $this->get_archives_where_r['type'] ) { |
| 132 | $blog_url = rtrim( home_url(), '/' ); |
| 133 | |
| 134 | // remove front. |
| 135 | $front = substr( $wp_rewrite->front, 1 ); |
| 136 | $html = str_replace( $front, '', $html ); |
| 137 | |
| 138 | $blog_url = preg_replace( '/https?:\/\//', '', $blog_url ); |
| 139 | $ret_link = str_replace( $blog_url, $blog_url . '/%link_dir%', $html ); |
| 140 | |
| 141 | if ( empty( $c ) ) { |
| 142 | if ( isset( $post_type->rewrite['slug'] ) ) { |
| 143 | $link_dir = $post_type->rewrite['slug']; |
| 144 | } else { |
| 145 | $link_dir = $this->get_archives_where_r['post_type']; |
| 146 | } |
| 147 | } else { |
| 148 | $c['name'] = ( 'category' === $c['name'] && get_option( 'category_base' ) ) ? get_option( 'category_base' ) : $c['name']; |
| 149 | $link_dir = $post_type->rewrite['slug'] . '/' . $c['name'] . '/' . $c['termslug']; |
| 150 | } |
| 151 | |
| 152 | if ( ! strstr( $html, '/date/' ) ) { |
| 153 | $link_dir = $link_dir . CPTP_Util::get_date_front( $post_type ); |
| 154 | } |
| 155 | |
| 156 | if ( $post_type->rewrite['with_front'] ) { |
| 157 | $link_dir = $front . $link_dir; |
| 158 | } |
| 159 | |
| 160 | $ret_link = str_replace( '%link_dir%', $link_dir, $ret_link ); |
| 161 | $ret_link = str_replace( '?post_type=' . $this->get_archives_where_r['post_type'], '', $ret_link ); |
| 162 | } else { |
| 163 | $ret_link = $html; |
| 164 | } |
| 165 | $this->get_archives_where_r['post_type'] = $t; |
| 166 | return $ret_link; |
| 167 | } |
| 168 | } |
| 169 |