Admin.php
12 years ago
FlushRules.php
12 years ago
GetArchives.php
11 years ago
Permalink.php
11 years ago
Rewrite.php
11 years ago
Setting.php
12 years ago
Permalink.php
258 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | /** |
| 5 | * |
| 6 | * CPTP_Permalink |
| 7 | * |
| 8 | * Override Permalinks |
| 9 | * @package Custom_Post_Type_Permalinks |
| 10 | * @since 0.9.4 |
| 11 | * |
| 12 | * */ |
| 13 | |
| 14 | |
| 15 | class CPTP_Module_Permalink extends CPTP_Module { |
| 16 | |
| 17 | |
| 18 | public function add_hook() { |
| 19 | if(get_option( "permalink_structure") != "") { |
| 20 | add_filter( 'post_type_link', array( $this,'post_type_link'), 10, 4 ); |
| 21 | add_filter( 'term_link', array( $this,'term_link'), 10, 3 ); |
| 22 | add_filter( 'attachment_link', array( $this, 'attachment_link'), 20 , 2); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * |
| 28 | * Fix permalinks output. |
| 29 | * |
| 30 | * @param String $post_link |
| 31 | * @param Object $post 投稿� |
| 32 | 報 |
| 33 | * @param String $leavename 記事編集画面でのみ渡される |
| 34 | * |
| 35 | * @version 2.0 |
| 36 | * |
| 37 | */ |
| 38 | public function post_type_link( $post_link, $post, $leavename ) { |
| 39 | |
| 40 | global $wp_rewrite; |
| 41 | |
| 42 | $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); |
| 43 | if( $draft_or_pending and !$leavename ) |
| 44 | return $post_link; |
| 45 | |
| 46 | |
| 47 | |
| 48 | $post_type = $post->post_type; |
| 49 | $permalink = $wp_rewrite->get_extra_permastruct( $post_type ); |
| 50 | |
| 51 | $permalink = str_replace( '%post_id%', $post->ID, $permalink ); |
| 52 | $permalink = str_replace( '%'.$post_type.'_slug%', get_post_type_object( $post_type )->rewrite['slug'], $permalink ); |
| 53 | |
| 54 | |
| 55 | |
| 56 | //親ページが有るとき。 |
| 57 | $parentsDirs = ""; |
| 58 | if( !$leavename ){ |
| 59 | $postId = $post->ID; |
| 60 | while ($parent = get_post($postId)->post_parent) { |
| 61 | $parentsDirs = get_post($parent)->post_name."/".$parentsDirs; |
| 62 | $postId = $parent; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | $permalink = str_replace( '%'.$post_type.'%', $parentsDirs.'%'.$post_type.'%', $permalink ); |
| 67 | |
| 68 | if( !$leavename ){ |
| 69 | $permalink = str_replace( '%'.$post_type.'%', $post->post_name, $permalink ); |
| 70 | } |
| 71 | |
| 72 | //%post_id%/attachment/%attachement_name%; |
| 73 | //画像の編集ページでのリンク |
| 74 | if( isset($_GET["post"]) && $_GET["post"] != $post->ID ) { |
| 75 | $parent_structure = trim(get_option( $post->post_type.'_structure' ), "/"); |
| 76 | $parent_dirs = explode( "/", $parent_structure ); |
| 77 | if(is_array($parent_dirs)) { |
| 78 | $last_dir = array_pop( $parent_dirs ); |
| 79 | } |
| 80 | else { |
| 81 | $last_dir = $parent_dirs; |
| 82 | } |
| 83 | |
| 84 | if( "%post_id%" == $parent_structure or "%post_id%" == $last_dir ) { |
| 85 | $permalink = $permalink."/attachment/"; |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | $search = array(); |
| 90 | $replace = array(); |
| 91 | |
| 92 | $replace_tag = $this->create_taxonomy_replace_tag( $post->ID , $permalink ); |
| 93 | $search = $search + $replace_tag["search"]; |
| 94 | $replace = $replace + $replace_tag["replace"]; |
| 95 | |
| 96 | |
| 97 | $user = get_userdata( $post->post_author ); |
| 98 | if(isset($user->user_nicename)) { |
| 99 | $permalink = str_replace( "%author%", $user->user_nicename, $permalink ); |
| 100 | } |
| 101 | |
| 102 | $post_date = strtotime( $post->post_date ); |
| 103 | $permalink = str_replace(array( |
| 104 | "%year%", |
| 105 | "%monthnum%", |
| 106 | "%day%", |
| 107 | "%hour%", |
| 108 | "%minute%", |
| 109 | "%second%" |
| 110 | ), array( |
| 111 | date("Y",$post_date), |
| 112 | date("m",$post_date), |
| 113 | date("d",$post_date), |
| 114 | date("H",$post_date), |
| 115 | date("i",$post_date), |
| 116 | date("s",$post_date) |
| 117 | ), $permalink ); |
| 118 | $permalink = str_replace($search, $replace, $permalink); |
| 119 | $permalink = rtrim( home_url(),"/")."/".ltrim( $permalink ,"/" ); |
| 120 | |
| 121 | return $permalink; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | /** |
| 127 | * |
| 128 | * create %tax% -> term |
| 129 | * |
| 130 | * */ |
| 131 | private function create_taxonomy_replace_tag( $post_id , $permalink ) { |
| 132 | $search = array(); |
| 133 | $replace = array(); |
| 134 | |
| 135 | $taxonomies = CPTP_Util::get_taxonomies( true ); |
| 136 | |
| 137 | //%taxnomomy% -> parent/child |
| 138 | //運用でケアすべきかも。 |
| 139 | |
| 140 | foreach ( $taxonomies as $taxonomy => $objects ) { |
| 141 | $term = null; |
| 142 | if ( strpos($permalink, "%$taxonomy%") !== false ) { |
| 143 | $terms = wp_get_post_terms( $post_id, $taxonomy, array('orderby' => 'term_id')); |
| 144 | |
| 145 | if ( $terms and count($terms) > 1 ) { |
| 146 | if(reset($terms)->parent == 0){ |
| 147 | |
| 148 | $keys = array_keys($terms); |
| 149 | $term = $terms[$keys[1]]->slug; |
| 150 | if ( $terms[$keys[0]]->term_id == $terms[$keys[1]]->parent ) { |
| 151 | $term = CPTP_Util::get_taxonomy_parents( $terms[$keys[1]]->parent,$taxonomy, false, '/', true ) . $term; |
| 152 | } |
| 153 | }else{ |
| 154 | $keys = array_keys($terms); |
| 155 | $term = $terms[$keys[0]]->slug; |
| 156 | if ( $terms[$keys[1]]->term_id == $terms[$keys[0]]->parent ) { |
| 157 | $term = CPTP_Util::get_taxonomy_parents( $terms[$keys[0]]->parent,$taxonomy, false, '/', true ) . $term; |
| 158 | } |
| 159 | } |
| 160 | }else if( $terms ){ |
| 161 | |
| 162 | $term_obj = array_shift($terms); |
| 163 | $term = $term_obj->slug; |
| 164 | |
| 165 | if(isset($term_obj->parent) and $term_obj->parent != 0) { |
| 166 | $term = CPTP_Util::get_taxonomy_parents( $term_obj->parent,$taxonomy, false, '/', true ) . $term; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if( isset($term) ) { |
| 171 | $search[] = "%$taxonomy%"; |
| 172 | $replace[] = $term; |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | } |
| 177 | return array("search" => $search, "replace" => $replace ); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | |
| 182 | /** |
| 183 | * |
| 184 | * fix attachment output |
| 185 | * |
| 186 | * @version 1.0 |
| 187 | * @since 0.8.2 |
| 188 | * |
| 189 | */ |
| 190 | |
| 191 | public function attachment_link( $link , $postID ) { |
| 192 | $post = get_post( $postID ); |
| 193 | if (!$post->post_parent){ |
| 194 | return $link; |
| 195 | } |
| 196 | $post_parent = get_post( $post->post_parent ); |
| 197 | $permalink = get_option( $post_parent->post_type.'_structure' ); |
| 198 | $post_type = get_post_type_object( $post_parent->post_type ); |
| 199 | |
| 200 | if( $post_type->_builtin == false ) { |
| 201 | if(strpos( $permalink, "%postname%" ) < strrpos( $permalink, "%post_id%" ) && strrpos( $permalink, "attachment/" ) === FALSE ){ |
| 202 | $link = str_replace($post->post_name , "attachment/".$post->post_name, $link); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return $link; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * |
| 211 | * Fix taxonomy link outputs. |
| 212 | * @since 0.6 |
| 213 | * @version 1.0 |
| 214 | * |
| 215 | */ |
| 216 | public function term_link( $termlink, $term, $taxonomy ) { |
| 217 | global $wp_rewrite; |
| 218 | |
| 219 | if( get_option('no_taxonomy_structure') ) { |
| 220 | return $termlink; |
| 221 | } |
| 222 | |
| 223 | $taxonomy = get_taxonomy($taxonomy); |
| 224 | if( $taxonomy->_builtin ) |
| 225 | return $termlink; |
| 226 | |
| 227 | if( empty($taxonomy) ) |
| 228 | return $termlink; |
| 229 | |
| 230 | $wp_home = rtrim( home_url(), '/' ); |
| 231 | |
| 232 | if(in_array(get_post_type(), $taxonomy->object_type)){ |
| 233 | $post_type = get_post_type(); |
| 234 | } |
| 235 | else { |
| 236 | $post_type = $taxonomy->object_type[0]; |
| 237 | } |
| 238 | $post_type_obj = get_post_type_object($post_type); |
| 239 | $slug = $post_type_obj->rewrite['slug']; |
| 240 | $with_front = $post_type_obj->rewrite['with_front']; |
| 241 | $front = substr( $wp_rewrite->front, 1 ); |
| 242 | $termlink = str_replace($front,"",$termlink); |
| 243 | |
| 244 | if( $with_front ) { |
| 245 | $slug = $front.$slug; |
| 246 | } |
| 247 | |
| 248 | $termlink = str_replace( $wp_home, $wp_home."/".$slug, $termlink ); |
| 249 | |
| 250 | if ( ! $taxonomy->rewrite['hierarchical'] ) { |
| 251 | $termlink = str_replace( $term->slug.'/', CPTP_Util::get_taxonomy_parents( $term->term_id,$taxonomy->name, false, '/', true ), $termlink ); |
| 252 | } |
| 253 | |
| 254 | return $termlink; |
| 255 | } |
| 256 | |
| 257 | } |
| 258 |