1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Adds settings to the permalinks admin settings page |
4
|
|
|
* |
5
|
|
|
* @class WC_Admin_Permalink_Settings |
6
|
|
|
* @author WooThemes |
7
|
|
|
* @category Admin |
8
|
|
|
* @package WooCommerce/Admin |
9
|
|
|
* @version 2.3.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
13
|
|
|
exit; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
if ( ! class_exists( 'WC_Admin_Permalink_Settings' ) ) : |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* WC_Admin_Permalink_Settings Class. |
20
|
|
|
*/ |
21
|
|
|
class WC_Admin_Permalink_Settings { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Hook in tabs. |
25
|
|
|
*/ |
26
|
|
|
public function __construct() { |
27
|
|
|
$this->settings_init(); |
28
|
|
|
$this->settings_save(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Init our settings. |
33
|
|
|
*/ |
34
|
|
|
public function settings_init() { |
35
|
|
|
// Add a section to the permalinks page |
36
|
|
|
add_settings_section( 'woocommerce-permalink', __( 'Product Permalinks', 'woocommerce' ), array( $this, 'settings' ), 'permalink' ); |
37
|
|
|
|
38
|
|
|
// Add our settings |
39
|
|
|
add_settings_field( |
40
|
|
|
'woocommerce_product_category_slug', // id |
41
|
|
|
__( 'Product category base', 'woocommerce' ), // setting title |
42
|
|
|
array( $this, 'product_category_slug_input' ), // display callback |
43
|
|
|
'permalink', // settings page |
44
|
|
|
'optional' // settings section |
45
|
|
|
); |
46
|
|
|
add_settings_field( |
47
|
|
|
'woocommerce_product_tag_slug', // id |
48
|
|
|
__( 'Product tag base', 'woocommerce' ), // setting title |
49
|
|
|
array( $this, 'product_tag_slug_input' ), // display callback |
50
|
|
|
'permalink', // settings page |
51
|
|
|
'optional' // settings section |
52
|
|
|
); |
53
|
|
|
add_settings_field( |
54
|
|
|
'woocommerce_product_attribute_slug', // id |
55
|
|
|
__( 'Product attribute base', 'woocommerce' ), // setting title |
56
|
|
|
array( $this, 'product_attribute_slug_input' ), // display callback |
57
|
|
|
'permalink', // settings page |
58
|
|
|
'optional' // settings section |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Show a slug input box. |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function product_category_slug_input() { |
|
|
|
|
66
|
|
|
$permalinks = get_option( 'woocommerce_permalinks' ); |
67
|
|
|
?> |
68
|
|
|
<input name="woocommerce_product_category_slug" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['category_base'] ) ) echo esc_attr( $permalinks['category_base'] ); ?>" placeholder="<?php echo esc_attr_x('product-category', 'slug', 'woocommerce') ?>" /> |
69
|
|
|
<?php |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Show a slug input box. |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function product_tag_slug_input() { |
|
|
|
|
76
|
|
|
$permalinks = get_option( 'woocommerce_permalinks' ); |
77
|
|
|
?> |
78
|
|
|
<input name="woocommerce_product_tag_slug" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['tag_base'] ) ) echo esc_attr( $permalinks['tag_base'] ); ?>" placeholder="<?php echo esc_attr_x('product-tag', 'slug', 'woocommerce') ?>" /> |
79
|
|
|
<?php |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Show a slug input box. |
84
|
|
|
*/ |
85
|
|
|
public function product_attribute_slug_input() { |
86
|
|
|
$permalinks = get_option( 'woocommerce_permalinks' ); |
87
|
|
|
?> |
88
|
|
|
<input name="woocommerce_product_attribute_slug" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['attribute_base'] ) ) echo esc_attr( $permalinks['attribute_base'] ); ?>" /><code>/attribute-name/attribute/</code> |
89
|
|
|
<?php |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Show the settings. |
94
|
|
|
*/ |
95
|
|
|
public function settings() { |
96
|
|
|
echo wpautop( __( 'These settings control the permalinks used specifically for products.', 'woocommerce' ) ); |
97
|
|
|
|
98
|
|
|
$permalinks = get_option( 'woocommerce_permalinks' ); |
99
|
|
|
$product_permalink = isset( $permalinks['product_base'] ) ? $permalinks['product_base'] : ''; |
100
|
|
|
|
101
|
|
|
// Get shop page |
102
|
|
|
$shop_page_id = wc_get_page_id( 'shop' ); |
103
|
|
|
$base_slug = urldecode( ( $shop_page_id > 0 && get_post( $shop_page_id ) ) ? get_page_uri( $shop_page_id ) : _x( 'shop', 'default-slug', 'woocommerce' ) ); |
104
|
|
|
$product_base = _x( 'product', 'default-slug', 'woocommerce' ); |
105
|
|
|
|
106
|
|
|
$structures = array( |
107
|
|
|
0 => '', |
108
|
|
|
1 => '/' . trailingslashit( $base_slug ), |
109
|
|
|
2 => '/' . trailingslashit( $base_slug ) . trailingslashit( '%product_cat%' ) |
110
|
|
|
); |
111
|
|
|
?> |
112
|
|
|
<table class="form-table wc-permalink-structure"> |
113
|
|
|
<tbody> |
114
|
|
|
<tr> |
115
|
|
|
<th><label><input name="product_permalink" type="radio" value="<?php echo esc_attr( $structures[0] ); ?>" class="wctog" <?php checked( $structures[0], $product_permalink ); ?> /> <?php _e( 'Default', 'woocommerce' ); ?></label></th> |
116
|
|
|
<td><code class="default-example"><?php echo esc_html( home_url() ); ?>/?product=sample-product</code> <code class="non-default-example"><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $product_base ); ?>/sample-product/</code></td> |
117
|
|
|
</tr> |
118
|
|
|
<?php if ( $shop_page_id ) : ?> |
119
|
|
|
<tr> |
120
|
|
|
<th><label><input name="product_permalink" type="radio" value="<?php echo esc_attr( $structures[1] ); ?>" class="wctog" <?php checked( $structures[1], $product_permalink ); ?> /> <?php _e( 'Shop base', 'woocommerce' ); ?></label></th> |
121
|
|
|
<td><code><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $base_slug ); ?>/sample-product/</code></td> |
122
|
|
|
</tr> |
123
|
|
|
<tr> |
124
|
|
|
<th><label><input name="product_permalink" type="radio" value="<?php echo esc_attr( $structures[2] ); ?>" class="wctog" <?php checked( $structures[2], $product_permalink ); ?> /> <?php _e( 'Shop base with category', 'woocommerce' ); ?></label></th> |
125
|
|
|
<td><code><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $base_slug ); ?>/product-category/sample-product/</code></td> |
126
|
|
|
</tr> |
127
|
|
|
<?php endif; ?> |
128
|
|
|
<tr> |
129
|
|
|
<th><label><input name="product_permalink" id="woocommerce_custom_selection" type="radio" value="custom" class="tog" <?php checked( in_array( $product_permalink, $structures ), false ); ?> /> |
130
|
|
|
<?php _e( 'Custom Base', 'woocommerce' ); ?></label></th> |
131
|
|
|
<td> |
132
|
|
|
<input name="product_permalink_structure" id="woocommerce_permalink_structure" type="text" value="<?php echo esc_attr( $product_permalink ); ?>" class="regular-text code"> <span class="description"><?php _e( 'Enter a custom base to use. A base <strong>must</strong> be set or WordPress will use default instead.', 'woocommerce' ); ?></span> |
133
|
|
|
</td> |
134
|
|
|
</tr> |
135
|
|
|
</tbody> |
136
|
|
|
</table> |
137
|
|
|
<script type="text/javascript"> |
138
|
|
|
jQuery( function() { |
139
|
|
|
jQuery('input.wctog').change(function() { |
140
|
|
|
jQuery('#woocommerce_permalink_structure').val( jQuery( this ).val() ); |
141
|
|
|
}); |
142
|
|
|
jQuery('.permalink-structure input').change(function() { |
143
|
|
|
jQuery('.wc-permalink-structure').find('code.non-default-example, code.default-example').hide(); |
144
|
|
|
if ( jQuery(this).val() ) { |
145
|
|
|
jQuery('.wc-permalink-structure code.non-default-example').show(); |
146
|
|
|
jQuery('.wc-permalink-structure input').removeAttr('disabled'); |
147
|
|
|
} else { |
148
|
|
|
jQuery('.wc-permalink-structure code.default-example').show(); |
149
|
|
|
jQuery('.wc-permalink-structure input:eq(0)').click(); |
150
|
|
|
jQuery('.wc-permalink-structure input').attr('disabled', 'disabled'); |
151
|
|
|
} |
152
|
|
|
}); |
153
|
|
|
jQuery('.permalink-structure input:checked').change(); |
154
|
|
|
jQuery('#woocommerce_permalink_structure').focus( function(){ |
155
|
|
|
jQuery('#woocommerce_custom_selection').click(); |
156
|
|
|
} ); |
157
|
|
|
} ); |
158
|
|
|
</script> |
159
|
|
|
<?php |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Save the settings. |
164
|
|
|
*/ |
165
|
|
|
public function settings_save() { |
166
|
|
|
|
167
|
|
|
if ( ! is_admin() ) { |
168
|
|
|
return; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// We need to save the options ourselves; settings api does not trigger save for the permalinks page. |
172
|
|
|
if ( isset( $_POST['permalink_structure'] ) ) { |
173
|
|
|
// Cat and tag bases. |
174
|
|
|
$woocommerce_product_category_slug = wc_clean( $_POST['woocommerce_product_category_slug'] ); |
175
|
|
|
$woocommerce_product_tag_slug = wc_clean( $_POST['woocommerce_product_tag_slug'] ); |
176
|
|
|
$woocommerce_product_attribute_slug = wc_clean( $_POST['woocommerce_product_attribute_slug'] ); |
177
|
|
|
$permalinks = get_option( 'woocommerce_permalinks' ); |
178
|
|
|
|
179
|
|
|
if ( ! $permalinks ) { |
180
|
|
|
$permalinks = array(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$permalinks['category_base'] = untrailingslashit( $woocommerce_product_category_slug ); |
184
|
|
|
$permalinks['tag_base'] = untrailingslashit( $woocommerce_product_tag_slug ); |
185
|
|
|
$permalinks['attribute_base'] = untrailingslashit( $woocommerce_product_attribute_slug ); |
186
|
|
|
|
187
|
|
|
// Product base. |
188
|
|
|
$product_permalink = isset( $_POST['product_permalink'] ) ? wc_clean( $_POST['product_permalink'] ) : ''; |
189
|
|
|
|
190
|
|
|
if ( 'custom' === $product_permalink ) { |
191
|
|
|
// Get permalink without slashes. |
192
|
|
|
$product_permalink = isset( $_POST['product_permalink_structure'] ) ? trim( wc_clean( $_POST['product_permalink_structure'] ), '/' ) : ''; |
193
|
|
|
|
194
|
|
|
// This is an invalid base structure and breaks pages. |
195
|
|
|
if ( '%product_cat%' == $product_permalink ) { |
196
|
|
|
$product_permalink = _x( 'product', 'slug', 'woocommerce' ) . '/' . $product_permalink; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// Prepending slash. |
200
|
|
|
$product_permalink = '/' . $product_permalink; |
201
|
|
|
} elseif ( empty( $product_permalink ) ) { |
202
|
|
|
$product_permalink = false; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$permalinks['product_base'] = untrailingslashit( $product_permalink ); |
206
|
|
|
|
207
|
|
|
// Shop base may require verbose page rules if nesting pages. |
208
|
|
|
$shop_page_id = wc_get_page_id( 'shop' ); |
209
|
|
|
$shop_permalink = ( $shop_page_id > 0 && get_post( $shop_page_id ) ) ? get_page_uri( $shop_page_id ) : _x( 'shop', 'default-slug', 'woocommerce' ); |
210
|
|
|
|
211
|
|
|
if ( $shop_page_id && trim( $permalinks['product_base'], '/' ) === $shop_permalink ) { |
212
|
|
|
$permalinks['use_verbose_page_rules'] = true; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
update_option( 'woocommerce_permalinks', $permalinks ); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
endif; |
221
|
|
|
|
222
|
|
|
return new WC_Admin_Permalink_Settings(); |
223
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.