Version Description
- option bug fix.
Download this release
Release Info
Developer | Toro_Unit |
Plugin | Custom Post Type Permalinks |
Version | 1.0.4 |
Comparing to | |
See all releases |
Code changes from version 1.0.3 to 1.0.4
- CPTP/Module/Admin.php +55 -61
- CPTP/Module/FlushRules.php +2 -2
- CPTP/Module/GetArchives.php +1 -1
- CPTP/Module/Option.php +40 -24
- CPTP/Module/Permalink.php +46 -48
- CPTP/Module/Setting.php +1 -1
- CPTP/Util.php +1 -3
- custom-post-type-permalinks.php +2 -2
- readme.txt +4 -1
CPTP/Module/Admin.php
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
<?php
|
2 |
|
3 |
|
4 |
-
|
5 |
/**
|
6 |
*
|
7 |
* Admin Page View.
|
@@ -10,7 +9,6 @@
|
|
10 |
* @since 0.9.4
|
11 |
*
|
12 |
* */
|
13 |
-
|
14 |
class CPTP_Module_Admin extends CPTP_Module {
|
15 |
|
16 |
public function add_hook() {
|
@@ -34,35 +32,38 @@ class CPTP_Module_Admin extends CPTP_Module {
|
|
34 |
);
|
35 |
|
36 |
$post_types = CPTP_Util::get_post_types();
|
37 |
-
foreach ( $post_types as $post_type ):
|
38 |
|
39 |
-
|
|
|
|
|
|
|
40 |
$post_type,
|
41 |
array( $this, 'setting_structure_callback_function' ),
|
42 |
'permalink',
|
43 |
'cptp_setting_section',
|
44 |
-
$post_type.'_structure'
|
45 |
);
|
46 |
-
|
47 |
-
|
48 |
-
endforeach;
|
49 |
|
50 |
add_settings_field(
|
51 |
'no_taxonomy_structure',
|
52 |
__( 'Use custom permalink of custom taxonomy archive.', 'cptp' ),
|
53 |
array( $this, 'setting_no_tax_structure_callback_function' ),
|
54 |
'permalink',
|
55 |
-
'cptp_setting_section'
|
|
|
56 |
);
|
57 |
|
58 |
register_setting( 'permalink', 'no_taxonomy_structure' );
|
59 |
|
60 |
add_settings_field(
|
61 |
-
'add_post_type_for_tax
|
62 |
__( 'Add post_type query for custom taxonomy archive.', 'cptp' ),
|
63 |
array( $this, 'add_post_type_for_tax_callback_function' ),
|
64 |
'permalink',
|
65 |
-
'cptp_setting_section'
|
|
|
66 |
);
|
67 |
|
68 |
register_setting( 'permalink', 'no_taxonomy_structure' );
|
@@ -71,24 +72,27 @@ class CPTP_Module_Admin extends CPTP_Module {
|
|
71 |
|
72 |
public function setting_section_callback_function() {
|
73 |
?>
|
74 |
-
|
75 |
-
<?php _e( "The tags you can use is WordPress Structure Tags and '%\"custom_taxonomy_slug\"%'. (e.g. %actors%)", 'cptp' )
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
83 |
}
|
84 |
|
85 |
-
public function setting_structure_callback_function(
|
86 |
|
87 |
-
$post_type
|
88 |
-
$
|
89 |
-
$
|
|
|
90 |
$with_front = $pt_object->rewrite['with_front'];
|
91 |
|
|
|
92 |
$value = CPTP_Util::get_permalink_structure( $post_type );
|
93 |
|
94 |
$disabled = false;
|
@@ -102,31 +106,23 @@ class CPTP_Module_Admin extends CPTP_Module {
|
|
102 |
|
103 |
global $wp_rewrite;
|
104 |
$front = substr( $wp_rewrite->front, 1 );
|
105 |
-
if (
|
106 |
-
$slug = $front
|
107 |
}
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
echo 'with_front: <code>
|
114 |
-
|
115 |
-
echo '</code></p>';
|
116 |
-
|
117 |
}
|
118 |
|
119 |
-
private function disabled_string( $bool ) {
|
120 |
-
if ( $bool ) {
|
121 |
-
return 'disabled';
|
122 |
-
}
|
123 |
-
return '';
|
124 |
-
}
|
125 |
|
126 |
-
public function setting_no_tax_structure_callback_function(){
|
127 |
echo '<input name="no_taxonomy_structure" id="no_taxonomy_structure" type="checkbox" value="1" class="code" ' . checked( false, get_option( 'no_taxonomy_structure' ), false ) . ' /> ';
|
128 |
$txt = __( "If you check,The custom taxonomy's permalinks is <code>%s/post_type/taxonomy/term</code>.", 'cptp' );
|
129 |
-
printf( $txt
|
130 |
}
|
131 |
|
132 |
|
@@ -137,7 +133,6 @@ class CPTP_Module_Admin extends CPTP_Module {
|
|
137 |
}
|
138 |
|
139 |
|
140 |
-
|
141 |
/**
|
142 |
*
|
143 |
* enqueue CSS and JS
|
@@ -150,7 +145,6 @@ class CPTP_Module_Admin extends CPTP_Module {
|
|
150 |
}
|
151 |
|
152 |
|
153 |
-
|
154 |
/**
|
155 |
*
|
156 |
* add js for pointer
|
@@ -160,23 +154,23 @@ class CPTP_Module_Admin extends CPTP_Module {
|
|
160 |
if ( ! is_network_admin() ) {
|
161 |
$dismissed = explode( ',', get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
|
162 |
if ( false === array_search( 'cptp_pointer0871', $dismissed ) ) {
|
163 |
-
$content = __( "<h3>Custom Post Type Permalinks</h3><p>From <a href='options-permalink.php'>Permalinks</a>, set a custom permalink for each post type.</p>"
|
164 |
-
|
165 |
<script type="text/javascript">
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
</script>
|
181 |
<?php
|
182 |
}
|
1 |
<?php
|
2 |
|
3 |
|
|
|
4 |
/**
|
5 |
*
|
6 |
* Admin Page View.
|
9 |
* @since 0.9.4
|
10 |
*
|
11 |
* */
|
|
|
12 |
class CPTP_Module_Admin extends CPTP_Module {
|
13 |
|
14 |
public function add_hook() {
|
32 |
);
|
33 |
|
34 |
$post_types = CPTP_Util::get_post_types();
|
|
|
35 |
|
36 |
+
foreach ( $post_types as $post_type ) {
|
37 |
+
|
38 |
+
add_settings_field(
|
39 |
+
$post_type . '_structure',
|
40 |
$post_type,
|
41 |
array( $this, 'setting_structure_callback_function' ),
|
42 |
'permalink',
|
43 |
'cptp_setting_section',
|
44 |
+
array( 'label_for' => $post_type . '_structure', 'post_type' => $post_type )
|
45 |
);
|
46 |
+
register_setting( 'permalink', $post_type . '_structure' );
|
47 |
+
}
|
|
|
48 |
|
49 |
add_settings_field(
|
50 |
'no_taxonomy_structure',
|
51 |
__( 'Use custom permalink of custom taxonomy archive.', 'cptp' ),
|
52 |
array( $this, 'setting_no_tax_structure_callback_function' ),
|
53 |
'permalink',
|
54 |
+
'cptp_setting_section',
|
55 |
+
array( 'label_for' => 'no_taxonomy_structure' )
|
56 |
);
|
57 |
|
58 |
register_setting( 'permalink', 'no_taxonomy_structure' );
|
59 |
|
60 |
add_settings_field(
|
61 |
+
'add_post_type_for_tax',
|
62 |
__( 'Add post_type query for custom taxonomy archive.', 'cptp' ),
|
63 |
array( $this, 'add_post_type_for_tax_callback_function' ),
|
64 |
'permalink',
|
65 |
+
'cptp_setting_section',
|
66 |
+
array( 'label_for' => 'add_post_type_for_tax' )
|
67 |
);
|
68 |
|
69 |
register_setting( 'permalink', 'no_taxonomy_structure' );
|
72 |
|
73 |
public function setting_section_callback_function() {
|
74 |
?>
|
75 |
+
<p><?php _e( 'Setting permalinks of custom post type.', 'cptp' ); ?><br/>
|
76 |
+
<?php _e( "The tags you can use is WordPress Structure Tags and '%\"custom_taxonomy_slug\"%'. (e.g. %actors%)", 'cptp' ); ?>
|
77 |
+
<br/>
|
78 |
+
<?php _e( "%\"custom_taxonomy_slug\"% is replaced the taxonomy's term.'.", 'cptp' ); ?></p>
|
79 |
+
|
80 |
+
<p><?php _e( "Presence of the trailing '/' is unified into a standard permalink structure setting.", 'cptp' ); ?>
|
81 |
+
<p><?php _e( 'If <code>has_archive</code> is true, add permalinks for custom post type archive.', 'cptp' ); ?>
|
82 |
+
<?php _e( "If you don't entered permalink structure, permalink is configured /%postname%/'.", 'cptp' ); ?>
|
83 |
+
</p>
|
84 |
+
<?php
|
85 |
}
|
86 |
|
87 |
+
public function setting_structure_callback_function( $option ) {
|
88 |
|
89 |
+
$post_type = $option['post_type'];
|
90 |
+
$name = $option['label_for'];
|
91 |
+
$pt_object = get_post_type_object( $post_type );
|
92 |
+
$slug = $pt_object->rewrite['slug'];
|
93 |
$with_front = $pt_object->rewrite['with_front'];
|
94 |
|
95 |
+
|
96 |
$value = CPTP_Util::get_permalink_structure( $post_type );
|
97 |
|
98 |
$disabled = false;
|
106 |
|
107 |
global $wp_rewrite;
|
108 |
$front = substr( $wp_rewrite->front, 1 );
|
109 |
+
if ( $front and $with_front ) {
|
110 |
+
$slug = $front . $slug;
|
111 |
}
|
112 |
+
?>
|
113 |
+
<p>
|
114 |
+
<code><?php echo home_url() . '/' . esc_html( $slug );?></code>
|
115 |
+
<input name="<?php echo esc_attr( $name );?>" id="<?php echo esc_attr( $name );?>" type="text" class="regular-text code " value="<?php echo esc_attr( $value ) ;?>" <?php disabled( $disabled, true, true );?> />
|
116 |
+
</p>
|
117 |
+
<p>has_archive: <code><?php echo $pt_object->has_archive ? 'true' : 'false';?></code> / with_front: <code><?php echo $pt_object->rewrite['with_front'] ? 'true' : 'false';?></code></p>
|
118 |
+
<?php
|
|
|
|
|
119 |
}
|
120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
+
public function setting_no_tax_structure_callback_function() {
|
123 |
echo '<input name="no_taxonomy_structure" id="no_taxonomy_structure" type="checkbox" value="1" class="code" ' . checked( false, get_option( 'no_taxonomy_structure' ), false ) . ' /> ';
|
124 |
$txt = __( "If you check,The custom taxonomy's permalinks is <code>%s/post_type/taxonomy/term</code>.", 'cptp' );
|
125 |
+
printf( $txt, home_url() );
|
126 |
}
|
127 |
|
128 |
|
133 |
}
|
134 |
|
135 |
|
|
|
136 |
/**
|
137 |
*
|
138 |
* enqueue CSS and JS
|
145 |
}
|
146 |
|
147 |
|
|
|
148 |
/**
|
149 |
*
|
150 |
* add js for pointer
|
154 |
if ( ! is_network_admin() ) {
|
155 |
$dismissed = explode( ',', get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
|
156 |
if ( false === array_search( 'cptp_pointer0871', $dismissed ) ) {
|
157 |
+
$content = __( "<h3>Custom Post Type Permalinks</h3><p>From <a href='options-permalink.php'>Permalinks</a>, set a custom permalink for each post type.</p>", 'cptp' );
|
158 |
+
?>
|
159 |
<script type="text/javascript">
|
160 |
+
jQuery(function ($) {
|
161 |
+
|
162 |
+
$("#menu-settings .wp-has-submenu").pointer({
|
163 |
+
content: "<?php echo $content;?>",
|
164 |
+
position: {"edge": "left", "align": "center"},
|
165 |
+
close: function () {
|
166 |
+
$.post('admin-ajax.php', {
|
167 |
+
action: 'dismiss-wp-pointer',
|
168 |
+
pointer: 'cptp_pointer0871'
|
169 |
+
})
|
170 |
+
|
171 |
+
}
|
172 |
+
}).pointer("open");
|
173 |
+
});
|
174 |
</script>
|
175 |
<?php
|
176 |
}
|
CPTP/Module/FlushRules.php
CHANGED
@@ -33,9 +33,9 @@ class CPTP_Module_FlushRules extends CPTP_Module {
|
|
33 |
public function update_rules() {
|
34 |
|
35 |
$post_types = CPTP_Util::get_post_types();
|
36 |
-
foreach ( $post_types as $post_type )
|
37 |
add_action( 'update_option_'.$post_type.'_structure', array( __CLASS__, 'queue_flush_rules' ), 10, 2 );
|
38 |
-
|
39 |
add_action( 'update_option_no_taxonomy_structure', array( __CLASS__, 'queue_flush_rules' ), 10, 2 );
|
40 |
}
|
41 |
|
33 |
public function update_rules() {
|
34 |
|
35 |
$post_types = CPTP_Util::get_post_types();
|
36 |
+
foreach ( $post_types as $post_type ) {
|
37 |
add_action( 'update_option_'.$post_type.'_structure', array( __CLASS__, 'queue_flush_rules' ), 10, 2 );
|
38 |
+
}
|
39 |
add_action( 'update_option_no_taxonomy_structure', array( __CLASS__, 'queue_flush_rules' ), 10, 2 );
|
40 |
}
|
41 |
|
CPTP/Module/GetArchives.php
CHANGED
@@ -108,7 +108,7 @@ class CPTP_Module_GetArchives extends CPTP_Module {
|
|
108 |
}
|
109 |
}
|
110 |
else { // [steve]
|
111 |
-
$c['name'] = ( $c['name']
|
112 |
$link_dir = $post_type->rewrite['slug'].'/'.$c['name'].'/'.$c['termslug'];
|
113 |
}
|
114 |
|
108 |
}
|
109 |
}
|
110 |
else { // [steve]
|
111 |
+
$c['name'] = ( 'category' == $c['name'] && get_option( 'category_base' ) ) ? get_option( 'category_base' ) : $c['name'];
|
112 |
$link_dir = $post_type->rewrite['slug'].'/'.$c['name'].'/'.$c['termslug'];
|
113 |
}
|
114 |
|
CPTP/Module/Option.php
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
<?php
|
2 |
|
3 |
|
4 |
-
|
5 |
/**
|
6 |
*
|
7 |
* Options.
|
@@ -12,7 +11,6 @@
|
|
12 |
* @since 0.9.6
|
13 |
*
|
14 |
* */
|
15 |
-
|
16 |
class CPTP_Module_Option extends CPTP_Module {
|
17 |
|
18 |
public function add_hook() {
|
@@ -22,40 +20,58 @@ class CPTP_Module_Option extends CPTP_Module {
|
|
22 |
|
23 |
public function save_options() {
|
24 |
|
25 |
-
if (
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
-
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
$structure = rtrim( $structure, '/' );
|
41 |
|
42 |
-
|
43 |
-
$structure = $structure.'/';
|
44 |
-
}
|
45 |
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
48 |
}
|
49 |
-
update_option( 'no_taxonomy_structure', ! isset( $_POST['no_taxonomy_structure'] ) );
|
50 |
-
update_option( 'add_post_type_for_tax', isset( $_POST['add_post_type_for_tax'] ) );
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
}
|
54 |
}
|
55 |
|
56 |
public static function uninstall_hook() {
|
57 |
foreach ( CPTP_Util::get_post_types() as $post_type ) {
|
58 |
-
delete_option( $post_type.'_structure' );
|
59 |
}
|
60 |
|
61 |
delete_option( 'no_taxonomy_structure' );
|
1 |
<?php
|
2 |
|
3 |
|
|
|
4 |
/**
|
5 |
*
|
6 |
* Options.
|
11 |
* @since 0.9.6
|
12 |
*
|
13 |
* */
|
|
|
14 |
class CPTP_Module_Option extends CPTP_Module {
|
15 |
|
16 |
public function add_hook() {
|
20 |
|
21 |
public function save_options() {
|
22 |
|
23 |
+
if ( empty( $_POST['submit'] ) ) {
|
24 |
+
return false;
|
25 |
+
}
|
26 |
+
|
27 |
+
if ( empty( $_POST['_wp_http_referer'] ) ) {
|
28 |
+
return false;
|
29 |
+
}
|
30 |
|
31 |
+
if ( false === strpos( $_POST['_wp_http_referer'], 'options-permalink.php' ) ) {
|
32 |
+
return false;
|
33 |
+
}
|
34 |
|
35 |
+
if ( empty ( $_POST['_wpnonce'] ) ) {
|
36 |
+
return false;
|
37 |
+
}
|
38 |
+
$nonce = $_POST['_wpnonce'];
|
39 |
|
40 |
+
if ( ! wp_verify_nonce( $nonce, 'update-permalink' ) ) {
|
41 |
+
return false;
|
42 |
+
}
|
|
|
43 |
|
44 |
+
$post_types = CPTP_Util::get_post_types();
|
|
|
|
|
45 |
|
46 |
+
foreach ( $post_types as $post_type ):
|
47 |
+
|
48 |
+
$structure = trim( esc_attr( $_POST[ $post_type . '_structure' ] ) );#get setting
|
49 |
+
|
50 |
+
#default permalink structure
|
51 |
+
if ( ! $structure ) {
|
52 |
+
$structure = CPTP_DEFAULT_PERMALINK;
|
53 |
}
|
|
|
|
|
54 |
|
55 |
+
$structure = str_replace( '//', '/', '/' . $structure );# first "/"
|
56 |
+
#last "/"
|
57 |
+
$lastString = substr( trim( esc_attr( $_POST['permalink_structure'] ) ), - 1 );
|
58 |
+
$structure = rtrim( $structure, '/' );
|
59 |
+
|
60 |
+
if ( $lastString == '/' ) {
|
61 |
+
$structure = $structure . '/';
|
62 |
+
}
|
63 |
+
|
64 |
+
update_option( $post_type . '_structure', $structure );
|
65 |
+
endforeach;
|
66 |
+
|
67 |
+
update_option( 'no_taxonomy_structure', ! isset( $_POST['no_taxonomy_structure'] ) );
|
68 |
+
update_option( 'add_post_type_for_tax', isset( $_POST['add_post_type_for_tax'] ) );
|
69 |
|
|
|
70 |
}
|
71 |
|
72 |
public static function uninstall_hook() {
|
73 |
foreach ( CPTP_Util::get_post_types() as $post_type ) {
|
74 |
+
delete_option( $post_type . '_structure' );
|
75 |
}
|
76 |
|
77 |
delete_option( 'no_taxonomy_structure' );
|
CPTP/Module/Permalink.php
CHANGED
@@ -10,15 +10,13 @@
|
|
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 |
add_filter( 'post_type_link', array( $this, 'post_type_link' ), 10, 4 );
|
20 |
add_filter( 'term_link', array( $this, 'term_link' ), 10, 3 );
|
21 |
-
add_filter( 'attachment_link', array( $this, 'attachment_link' ), 20
|
22 |
}
|
23 |
|
24 |
|
@@ -38,12 +36,15 @@ class CPTP_Module_Permalink extends CPTP_Module {
|
|
38 |
global /** @var WP_Rewrite $wp_rewrite */
|
39 |
$wp_rewrite;
|
40 |
|
41 |
-
if (
|
42 |
return $post_link;
|
43 |
}
|
44 |
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
if ( $draft_or_pending and ! $leavename ) {
|
48 |
return $post_link;
|
49 |
}
|
@@ -53,49 +54,48 @@ class CPTP_Module_Permalink extends CPTP_Module {
|
|
53 |
$pt_object = get_post_type_object( $post_type );
|
54 |
|
55 |
$permalink = str_replace( '%post_id%', $post->ID, $permalink );
|
56 |
-
$permalink = str_replace( '%'
|
57 |
|
58 |
//親ページが有るとき。
|
59 |
$parentsDirs = '';
|
60 |
if ( $pt_object->hierarchical ) {
|
61 |
-
if ( ! $leavename ){
|
62 |
$postId = $post->ID;
|
63 |
while ( $parent = get_post( $postId )->post_parent ) {
|
64 |
-
$parentsDirs = get_post( $parent )->post_name.'/'
|
65 |
-
$postId
|
66 |
}
|
67 |
}
|
68 |
}
|
69 |
|
70 |
-
$permalink = str_replace( '%'
|
71 |
|
72 |
if ( ! $leavename ) {
|
73 |
-
$permalink = str_replace( '%'
|
74 |
}
|
75 |
|
76 |
//%post_id%/attachment/%attachement_name%;
|
77 |
//画像の編集ページでのリンク
|
78 |
if ( isset( $_GET['post'] ) && $_GET['post'] != $post->ID ) {
|
79 |
$parent_structure = trim( CPTP_Util::get_permalink_structure( $post->post_type ), '/' );
|
80 |
-
$parent_dirs
|
81 |
if ( is_array( $parent_dirs ) ) {
|
82 |
$last_dir = array_pop( $parent_dirs );
|
83 |
-
}
|
84 |
-
else {
|
85 |
$last_dir = $parent_dirs;
|
86 |
}
|
87 |
|
88 |
if ( '%post_id%' == $parent_structure or '%post_id%' == $last_dir ) {
|
89 |
-
$permalink = $permalink.'/attachment/';
|
90 |
}
|
91 |
}
|
92 |
|
93 |
-
$search
|
94 |
$replace = array();
|
95 |
|
96 |
-
$replace_tag = $this->create_taxonomy_replace_tag( $post->ID
|
97 |
-
$search
|
98 |
-
$replace
|
99 |
|
100 |
//from get_permalink.
|
101 |
$category = '';
|
@@ -105,7 +105,7 @@ class CPTP_Module_Permalink extends CPTP_Module {
|
|
105 |
usort( $categories, '_usort_terms_by_ID' ); // order by ID
|
106 |
$category_object = apply_filters( 'post_link_category', $categories[0], $categories, $post );
|
107 |
$category_object = get_term( $category_object, 'category' );
|
108 |
-
$category
|
109 |
if ( $parent = $category_object->parent ) {
|
110 |
$category = get_category_parents( $parent, false, '/', true ) . $category;
|
111 |
}
|
@@ -114,14 +114,14 @@ class CPTP_Module_Permalink extends CPTP_Module {
|
|
114 |
// having to assign it explicitly
|
115 |
if ( empty( $category ) ) {
|
116 |
$default_category = get_term( get_option( 'default_category' ), 'category' );
|
117 |
-
$category
|
118 |
}
|
119 |
}
|
120 |
|
121 |
$author = '';
|
122 |
if ( false !== strpos( $permalink, '%author%' ) ) {
|
123 |
$authordata = get_userdata( $post->post_author );
|
124 |
-
$author
|
125 |
}
|
126 |
|
127 |
$post_date = strtotime( $post->post_date );
|
@@ -149,7 +149,7 @@ class CPTP_Module_Permalink extends CPTP_Module {
|
|
149 |
$permalink
|
150 |
);
|
151 |
$permalink = str_replace( $search, $replace, $permalink );
|
152 |
-
$permalink = rtrim( home_url(), '/' ).'/'.ltrim( $permalink, '/' );
|
153 |
|
154 |
return $permalink;
|
155 |
}
|
@@ -165,7 +165,7 @@ class CPTP_Module_Permalink extends CPTP_Module {
|
|
165 |
* @return array
|
166 |
*/
|
167 |
private function create_taxonomy_replace_tag( $post_id, $permalink ) {
|
168 |
-
$search
|
169 |
$replace = array();
|
170 |
|
171 |
$taxonomies = CPTP_Util::get_taxonomies( true );
|
@@ -175,11 +175,11 @@ class CPTP_Module_Permalink extends CPTP_Module {
|
|
175 |
|
176 |
foreach ( $taxonomies as $taxonomy => $objects ) {
|
177 |
|
178 |
-
if ( false !== strpos( $permalink, '%'
|
179 |
$terms = wp_get_post_terms( $post_id, $taxonomy, array( 'orderby' => 'term_id' ) );
|
180 |
|
181 |
if ( $terms and ! is_wp_error( $terms ) ) {
|
182 |
-
$parents
|
183 |
$newTerms = array();
|
184 |
foreach ( $terms as $key => $term ) {
|
185 |
if ( ! in_array( $term->term_id, $parents ) ) {
|
@@ -188,16 +188,16 @@ class CPTP_Module_Permalink extends CPTP_Module {
|
|
188 |
}
|
189 |
|
190 |
//このブロックだけで良いはず。
|
191 |
-
$term_obj
|
192 |
$term_slug = $term_obj->slug;
|
193 |
|
194 |
if ( isset( $term_obj->parent ) and 0 != $term_obj->parent ) {
|
195 |
-
$term_slug = CPTP_Util::get_taxonomy_parents( $term_obj->parent
|
196 |
}
|
197 |
}
|
198 |
|
199 |
if ( isset( $term_slug ) ) {
|
200 |
-
$search[]
|
201 |
$replace[] = $term_slug;
|
202 |
}
|
203 |
}
|
@@ -230,22 +230,21 @@ class CPTP_Module_Permalink extends CPTP_Module {
|
|
230 |
/** @var WP_Rewrite $wp_rewrite */
|
231 |
global $wp_rewrite;
|
232 |
|
233 |
-
if (
|
234 |
return $link;
|
235 |
}
|
236 |
|
237 |
-
|
238 |
$post = get_post( $postID );
|
239 |
-
if ( ! $post->post_parent ){
|
240 |
return $link;
|
241 |
}
|
242 |
$post_parent = get_post( $post->post_parent );
|
243 |
-
$permalink
|
244 |
-
$post_type
|
245 |
|
246 |
if ( false == $post_type->_builtin ) {
|
247 |
-
if ( strpos( $permalink, '%postname%' ) < strrpos( $permalink, '%post_id%' ) && false === strrpos( $permalink, 'attachment/' )
|
248 |
-
$link = str_replace( $post->post_name, 'attachment/'
|
249 |
}
|
250 |
}
|
251 |
|
@@ -268,12 +267,12 @@ class CPTP_Module_Permalink extends CPTP_Module {
|
|
268 |
/** @var WP_Rewrite $wp_rewrite */
|
269 |
global $wp_rewrite;
|
270 |
|
271 |
-
if (
|
272 |
return $termlink;
|
273 |
}
|
274 |
|
275 |
if ( get_option( 'no_taxonomy_structure' ) ) {
|
276 |
-
return
|
277 |
}
|
278 |
|
279 |
$taxonomy = get_taxonomy( $taxonomy );
|
@@ -289,24 +288,23 @@ class CPTP_Module_Permalink extends CPTP_Module {
|
|
289 |
|
290 |
if ( in_array( get_post_type(), $taxonomy->object_type ) ) {
|
291 |
$post_type = get_post_type();
|
292 |
-
}
|
293 |
-
else {
|
294 |
$post_type = $taxonomy->object_type[0];
|
295 |
}
|
296 |
$post_type_obj = get_post_type_object( $post_type );
|
297 |
-
$slug
|
298 |
-
$with_front
|
299 |
-
$front
|
300 |
-
$termlink
|
301 |
|
302 |
if ( $with_front ) {
|
303 |
-
$slug = $front
|
304 |
}
|
305 |
|
306 |
-
$termlink = str_replace( $wp_home, $wp_home.'/'
|
307 |
|
308 |
if ( ! $taxonomy->rewrite['hierarchical'] ) {
|
309 |
-
$termlink = str_replace( $term->slug.'/', CPTP_Util::get_taxonomy_parents( $term->term_id
|
310 |
}
|
311 |
|
312 |
return $termlink;
|
10 |
* @since 0.9.4
|
11 |
*
|
12 |
* */
|
|
|
|
|
13 |
class CPTP_Module_Permalink extends CPTP_Module {
|
14 |
|
15 |
|
16 |
public function add_hook() {
|
17 |
add_filter( 'post_type_link', array( $this, 'post_type_link' ), 10, 4 );
|
18 |
add_filter( 'term_link', array( $this, 'term_link' ), 10, 3 );
|
19 |
+
add_filter( 'attachment_link', array( $this, 'attachment_link' ), 20, 2 );
|
20 |
}
|
21 |
|
22 |
|
36 |
global /** @var WP_Rewrite $wp_rewrite */
|
37 |
$wp_rewrite;
|
38 |
|
39 |
+
if ( ! $wp_rewrite->permalink_structure ) {
|
40 |
return $post_link;
|
41 |
}
|
42 |
|
43 |
+
$draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array(
|
44 |
+
'draft',
|
45 |
+
'pending',
|
46 |
+
'auto-draft',
|
47 |
+
) );
|
48 |
if ( $draft_or_pending and ! $leavename ) {
|
49 |
return $post_link;
|
50 |
}
|
54 |
$pt_object = get_post_type_object( $post_type );
|
55 |
|
56 |
$permalink = str_replace( '%post_id%', $post->ID, $permalink );
|
57 |
+
$permalink = str_replace( '%' . $post_type . '_slug%', $pt_object->rewrite['slug'], $permalink );
|
58 |
|
59 |
//親ページが有るとき。
|
60 |
$parentsDirs = '';
|
61 |
if ( $pt_object->hierarchical ) {
|
62 |
+
if ( ! $leavename ) {
|
63 |
$postId = $post->ID;
|
64 |
while ( $parent = get_post( $postId )->post_parent ) {
|
65 |
+
$parentsDirs = get_post( $parent )->post_name . '/' . $parentsDirs;
|
66 |
+
$postId = $parent;
|
67 |
}
|
68 |
}
|
69 |
}
|
70 |
|
71 |
+
$permalink = str_replace( '%' . $post_type . '%', $parentsDirs . '%' . $post_type . '%', $permalink );
|
72 |
|
73 |
if ( ! $leavename ) {
|
74 |
+
$permalink = str_replace( '%' . $post_type . '%', $post->post_name, $permalink );
|
75 |
}
|
76 |
|
77 |
//%post_id%/attachment/%attachement_name%;
|
78 |
//画像の編集ページでのリンク
|
79 |
if ( isset( $_GET['post'] ) && $_GET['post'] != $post->ID ) {
|
80 |
$parent_structure = trim( CPTP_Util::get_permalink_structure( $post->post_type ), '/' );
|
81 |
+
$parent_dirs = explode( '/', $parent_structure );
|
82 |
if ( is_array( $parent_dirs ) ) {
|
83 |
$last_dir = array_pop( $parent_dirs );
|
84 |
+
} else {
|
|
|
85 |
$last_dir = $parent_dirs;
|
86 |
}
|
87 |
|
88 |
if ( '%post_id%' == $parent_structure or '%post_id%' == $last_dir ) {
|
89 |
+
$permalink = $permalink . '/attachment/';
|
90 |
}
|
91 |
}
|
92 |
|
93 |
+
$search = array();
|
94 |
$replace = array();
|
95 |
|
96 |
+
$replace_tag = $this->create_taxonomy_replace_tag( $post->ID, $permalink );
|
97 |
+
$search = $search + $replace_tag['search'];
|
98 |
+
$replace = $replace + $replace_tag['replace'];
|
99 |
|
100 |
//from get_permalink.
|
101 |
$category = '';
|
105 |
usort( $categories, '_usort_terms_by_ID' ); // order by ID
|
106 |
$category_object = apply_filters( 'post_link_category', $categories[0], $categories, $post );
|
107 |
$category_object = get_term( $category_object, 'category' );
|
108 |
+
$category = $category_object->slug;
|
109 |
if ( $parent = $category_object->parent ) {
|
110 |
$category = get_category_parents( $parent, false, '/', true ) . $category;
|
111 |
}
|
114 |
// having to assign it explicitly
|
115 |
if ( empty( $category ) ) {
|
116 |
$default_category = get_term( get_option( 'default_category' ), 'category' );
|
117 |
+
$category = is_wp_error( $default_category ) ? '' : $default_category->slug;
|
118 |
}
|
119 |
}
|
120 |
|
121 |
$author = '';
|
122 |
if ( false !== strpos( $permalink, '%author%' ) ) {
|
123 |
$authordata = get_userdata( $post->post_author );
|
124 |
+
$author = $authordata->user_nicename;
|
125 |
}
|
126 |
|
127 |
$post_date = strtotime( $post->post_date );
|
149 |
$permalink
|
150 |
);
|
151 |
$permalink = str_replace( $search, $replace, $permalink );
|
152 |
+
$permalink = rtrim( home_url(), '/' ) . '/' . ltrim( $permalink, '/' );
|
153 |
|
154 |
return $permalink;
|
155 |
}
|
165 |
* @return array
|
166 |
*/
|
167 |
private function create_taxonomy_replace_tag( $post_id, $permalink ) {
|
168 |
+
$search = array();
|
169 |
$replace = array();
|
170 |
|
171 |
$taxonomies = CPTP_Util::get_taxonomies( true );
|
175 |
|
176 |
foreach ( $taxonomies as $taxonomy => $objects ) {
|
177 |
|
178 |
+
if ( false !== strpos( $permalink, '%' . $taxonomy . '%' ) ) {
|
179 |
$terms = wp_get_post_terms( $post_id, $taxonomy, array( 'orderby' => 'term_id' ) );
|
180 |
|
181 |
if ( $terms and ! is_wp_error( $terms ) ) {
|
182 |
+
$parents = array_map( array( __CLASS__, 'get_term_parent' ), $terms ); //親の一覧
|
183 |
$newTerms = array();
|
184 |
foreach ( $terms as $key => $term ) {
|
185 |
if ( ! in_array( $term->term_id, $parents ) ) {
|
188 |
}
|
189 |
|
190 |
//このブロックだけで良いはず。
|
191 |
+
$term_obj = reset( $newTerms ); //最初のOBjectのみを対象。
|
192 |
$term_slug = $term_obj->slug;
|
193 |
|
194 |
if ( isset( $term_obj->parent ) and 0 != $term_obj->parent ) {
|
195 |
+
$term_slug = CPTP_Util::get_taxonomy_parents( $term_obj->parent, $taxonomy, false, '/', true ) . $term_slug;
|
196 |
}
|
197 |
}
|
198 |
|
199 |
if ( isset( $term_slug ) ) {
|
200 |
+
$search[] = '%' . $taxonomy . '%';
|
201 |
$replace[] = $term_slug;
|
202 |
}
|
203 |
}
|
230 |
/** @var WP_Rewrite $wp_rewrite */
|
231 |
global $wp_rewrite;
|
232 |
|
233 |
+
if ( ! $wp_rewrite->permalink_structure ) {
|
234 |
return $link;
|
235 |
}
|
236 |
|
|
|
237 |
$post = get_post( $postID );
|
238 |
+
if ( ! $post->post_parent ) {
|
239 |
return $link;
|
240 |
}
|
241 |
$post_parent = get_post( $post->post_parent );
|
242 |
+
$permalink = CPTP_Util::get_permalink_structure( $post_parent->post_type );
|
243 |
+
$post_type = get_post_type_object( $post_parent->post_type );
|
244 |
|
245 |
if ( false == $post_type->_builtin ) {
|
246 |
+
if ( strpos( $permalink, '%postname%' ) < strrpos( $permalink, '%post_id%' ) && false === strrpos( $permalink, 'attachment/' ) ) {
|
247 |
+
$link = str_replace( $post->post_name, 'attachment/' . $post->post_name, $link );
|
248 |
}
|
249 |
}
|
250 |
|
267 |
/** @var WP_Rewrite $wp_rewrite */
|
268 |
global $wp_rewrite;
|
269 |
|
270 |
+
if ( ! $wp_rewrite->permalink_structure ) {
|
271 |
return $termlink;
|
272 |
}
|
273 |
|
274 |
if ( get_option( 'no_taxonomy_structure' ) ) {
|
275 |
+
return $termlink;
|
276 |
}
|
277 |
|
278 |
$taxonomy = get_taxonomy( $taxonomy );
|
288 |
|
289 |
if ( in_array( get_post_type(), $taxonomy->object_type ) ) {
|
290 |
$post_type = get_post_type();
|
291 |
+
} else {
|
|
|
292 |
$post_type = $taxonomy->object_type[0];
|
293 |
}
|
294 |
$post_type_obj = get_post_type_object( $post_type );
|
295 |
+
$slug = $post_type_obj->rewrite['slug'];
|
296 |
+
$with_front = $post_type_obj->rewrite['with_front'];
|
297 |
+
$front = substr( $wp_rewrite->front, 1 );
|
298 |
+
$termlink = str_replace( $front, '', $termlink );
|
299 |
|
300 |
if ( $with_front ) {
|
301 |
+
$slug = $front . $slug;
|
302 |
}
|
303 |
|
304 |
+
$termlink = str_replace( $wp_home, $wp_home . '/' . $slug, $termlink );
|
305 |
|
306 |
if ( ! $taxonomy->rewrite['hierarchical'] ) {
|
307 |
+
$termlink = str_replace( $term->slug . '/', CPTP_Util::get_taxonomy_parents( $term->term_id, $taxonomy->name, false, '/', true ), $termlink );
|
308 |
}
|
309 |
|
310 |
return $termlink;
|
CPTP/Module/Setting.php
CHANGED
@@ -30,7 +30,7 @@ class CPTP_Module_Setting extends CPTP_Module {
|
|
30 |
if ( false === $version ){
|
31 |
add_option( 'cptp_version', CPTP_VERSION );
|
32 |
}
|
33 |
-
else if (
|
34 |
update_option( 'cptp_version', CPTP_VERSION );
|
35 |
}
|
36 |
}
|
30 |
if ( false === $version ){
|
31 |
add_option( 'cptp_version', CPTP_VERSION );
|
32 |
}
|
33 |
+
else if ( CPTP_VERSION != $version ) {
|
34 |
update_option( 'cptp_version', CPTP_VERSION );
|
35 |
}
|
36 |
}
|
CPTP/Util.php
CHANGED
@@ -52,7 +52,6 @@ class CPTP_Util {
|
|
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 {
|
@@ -76,8 +75,7 @@ class CPTP_Util {
|
|
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 {
|
52 |
$visited[] = $parent->parent;
|
53 |
$chain .= CPTP_Util::get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
|
54 |
}
|
|
|
55 |
if ( $link ) {
|
56 |
$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 );
|
57 |
}else {
|
75 |
$pt_object = $post_type;
|
76 |
}
|
77 |
|
78 |
+
if ( ! empty( $pt_object->cptp_permalink_structure ) ) {
|
|
|
79 |
$structure = $pt_object->cptp_permalink_structure;
|
80 |
}
|
81 |
else {
|
custom-post-type-permalinks.php
CHANGED
@@ -5,7 +5,7 @@ Plugin URI: http://www.torounit.com
|
|
5 |
Description: Add post archives of custom post type and customizable permalinks.
|
6 |
Author: Toro_Unit
|
7 |
Author URI: http://www.torounit.com/plugins/custom-post-type-permalinks/
|
8 |
-
Version: 1.0.
|
9 |
Text Domain: cptp
|
10 |
License: GPL2 or later
|
11 |
Domain Path: /language/
|
@@ -17,7 +17,7 @@ Domain Path: /language/
|
|
17 |
* Custom Post Type Permalinks
|
18 |
*
|
19 |
* @package Custom_Post_Type_Permalinks
|
20 |
-
* @version 1.0.
|
21 |
*
|
22 |
*/
|
23 |
|
5 |
Description: Add post archives of custom post type and customizable permalinks.
|
6 |
Author: Toro_Unit
|
7 |
Author URI: http://www.torounit.com/plugins/custom-post-type-permalinks/
|
8 |
+
Version: 1.0.4
|
9 |
Text Domain: cptp
|
10 |
License: GPL2 or later
|
11 |
Domain Path: /language/
|
17 |
* Custom Post Type Permalinks
|
18 |
*
|
19 |
* @package Custom_Post_Type_Permalinks
|
20 |
+
* @version 1.0.4
|
21 |
*
|
22 |
*/
|
23 |
|
readme.txt
CHANGED
@@ -4,7 +4,7 @@ Donate link: http://www.amazon.co.jp/registry/wishlist/COKSXS25MVQV
|
|
4 |
Tags: permalink,permalinks,custom post type,custom taxonomy,cms
|
5 |
Requires at least: 4.1
|
6 |
Tested up to: 4.2
|
7 |
-
Stable tag: 1.0.
|
8 |
License: GPLv2 or Later
|
9 |
|
10 |
Lets you edit the permalink of custom post type.
|
@@ -61,6 +61,9 @@ That's it. You can access the permalinks setting by going to *Settings -> Permal
|
|
61 |
|
62 |
== Changelog ==
|
63 |
|
|
|
|
|
|
|
64 |
= 1.0.3 =
|
65 |
* add category rule, if only attached category to post type.
|
66 |
|
4 |
Tags: permalink,permalinks,custom post type,custom taxonomy,cms
|
5 |
Requires at least: 4.1
|
6 |
Tested up to: 4.2
|
7 |
+
Stable tag: 1.0.4
|
8 |
License: GPLv2 or Later
|
9 |
|
10 |
Lets you edit the permalink of custom post type.
|
61 |
|
62 |
== Changelog ==
|
63 |
|
64 |
+
= 1.0.4 =
|
65 |
+
* option bug fix.
|
66 |
+
|
67 |
= 1.0.3 =
|
68 |
* add category rule, if only attached category to post type.
|
69 |
|