Completed
Pull Request — master (#11085)
by Shiva
08:15
created

WC_Admin_Permalink_Settings::settings_save()   D

Complexity

Conditions 13
Paths 194

Size

Total Lines 48
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 13
eloc 26
c 2
b 1
f 0
nc 194
nop 0
dl 0
loc 48
rs 4.7746

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
		if ( ! is_admin() ) {
167
			return;
168
		}
169
170
		// We need to save the options ourselves; settings api does not trigger save for the permalinks page.
171
		if ( isset( $_POST['permalink_structure'] ) ) {
172
			$permalinks = get_option( 'woocommerce_permalinks' );
173
174
			if ( ! $permalinks ) {
175
				$permalinks = array();
176
			}
177
178
			$permalinks['category_base']    = wc_sanitize_permalink( trim( $_POST['woocommerce_product_category_slug'] ) );
179
			$permalinks['tag_base']         = wc_sanitize_permalink( trim( $_POST['woocommerce_product_tag_slug'] ) );
180
			$permalinks['attribute_base']   = wc_sanitize_permalink( trim( $_POST['woocommerce_product_attribute_slug'] ) );
181
182
			// Product base.
183
			$product_permalink = isset( $_POST['product_permalink'] ) ? wc_clean( $_POST['product_permalink'] ) : '';
184
185
			if ( 'custom' === $product_permalink ) {
186
				if ( isset( $_POST['product_permalink_structure'] ) ) {
187
					$product_permalink = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', trim( $_POST['product_permalink_structure'] ) ) );
188
				} else {
189
					$product_permalink = '/';
190
				}
191
192
				// This is an invalid base structure and breaks pages.
193
				if ( '%product_cat%' == $product_permalink ) {
194
					$product_permalink = '/' . _x( 'product', 'slug', 'woocommerce' ) . '/' . $product_permalink;
195
				}
196
			} elseif ( empty( $product_permalink ) ) {
197
				$product_permalink = false;
198
			}
199
200
			$permalinks['product_base'] = wc_sanitize_permalink( $product_permalink );
201
202
			// Shop base may require verbose page rules if nesting pages.
203
			$shop_page_id   = wc_get_page_id( 'shop' );
204
			$shop_permalink = ( $shop_page_id > 0 && get_post( $shop_page_id ) ) ? get_page_uri( $shop_page_id ) : _x( 'shop', 'default-slug', 'woocommerce' );
205
206
			if ( $shop_page_id && trim( $permalinks['product_base'], '/' ) === $shop_permalink ) {
207
				$permalinks['use_verbose_page_rules'] = true;
208
			}
209
210
			update_option( 'woocommerce_permalinks', $permalinks );
211
		}
212
	}
213
}
214
215
endif;
216
217
return new WC_Admin_Permalink_Settings();
218