Conditions | 5 |
Paths | 4 |
Total Lines | 66 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
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.