1 <?php
  2 /**
  3  * WooCommerce General Settings
  4  *
  5  * @author      WooThemes
  6  * @category    Admin
  7  * @package     WooCommerce/Admin
  8  * @version     2.1.0
  9  */
 10 
 11 if ( ! defined( 'ABSPATH' ) ) {
 12     exit; // Exit if accessed directly
 13 }
 14 
 15 if ( ! class_exists( 'WC_Settings_General' ) ) :
 16 
 17 /**
 18  * WC_Admin_Settings_General
 19  */
 20 class WC_Settings_General extends WC_Settings_Page {
 21 
 22     /**
 23      * Constructor.
 24      */
 25     public function __construct() {
 26         $this->id    = 'general';
 27         $this->label = __( 'General', 'woocommerce' );
 28 
 29         add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
 30         add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
 31         add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
 32 
 33         if ( ( $styles = WC_Frontend_Scripts::get_styles() ) && array_key_exists( 'woocommerce-general', $styles ) ) {
 34             add_action( 'woocommerce_admin_field_frontend_styles', array( $this, 'frontend_styles_setting' ) );
 35         }
 36     }
 37 
 38     /**
 39      * Get settings array
 40      *
 41      * @return array
 42      */
 43     public function get_settings() {
 44         $currency_code_options = get_woocommerce_currencies();
 45 
 46         foreach ( $currency_code_options as $code => $name ) {
 47             $currency_code_options[ $code ] = $name . ' (' . get_woocommerce_currency_symbol( $code ) . ')';
 48         }
 49 
 50         return apply_filters( 'woocommerce_general_settings', array(
 51 
 52             array( 'title' => __( 'General Options', 'woocommerce' ), 'type' => 'title', 'desc' => '', 'id' => 'general_options' ),
 53 
 54             array(
 55                 'title'     => __( 'Base Location', 'woocommerce' ),
 56                 'desc'      => __( 'This is the base location for your business. Tax rates will be based on this country.', 'woocommerce' ),
 57                 'id'        => 'woocommerce_default_country',
 58                 'css'       => 'min-width:350px;',
 59                 'default'   => 'GB',
 60                 'type'      => 'single_select_country',
 61                 'desc_tip'  =>  true,
 62             ),
 63 
 64             array(
 65                 'title' => __( 'Selling Location(s)', 'woocommerce' ),
 66                 'desc'      => __( 'This option lets you limit which countries you are willing to sell to.', 'woocommerce' ),
 67                 'id'        => 'woocommerce_allowed_countries',
 68                 'default'   => 'all',
 69                 'type'      => 'select',
 70                 'class'     => 'chosen_select',
 71                 'css'       => 'min-width: 350px;',
 72                 'desc_tip'  =>  true,
 73                 'options' => array(
 74                     'all'      => __( 'Sell to all countries', 'woocommerce' ),
 75                     'specific' => __( 'Sell to specific countries only', 'woocommerce' )
 76                 )
 77             ),
 78 
 79             array(
 80                 'title' => __( 'Specific Countries', 'woocommerce' ),
 81                 'desc'      => '',
 82                 'id'        => 'woocommerce_specific_allowed_countries',
 83                 'css'       => 'min-width: 350px;',
 84                 'default'   => '',
 85                 'type'      => 'multi_select_countries'
 86             ),
 87 
 88             array(
 89                 'title' => __( 'Store Notice', 'woocommerce' ),
 90                 'desc'      => __( 'Enable site-wide store notice text', 'woocommerce' ),
 91                 'id'        => 'woocommerce_demo_store',
 92                 'default'   => 'no',
 93                 'type'      => 'checkbox'
 94             ),
 95 
 96             array(
 97                 'title' => __( 'Store Notice Text', 'woocommerce' ),
 98                 'desc'      => '',
 99                 'id'        => 'woocommerce_demo_store_notice',
100                 'default'   => __( 'This is a demo store for testing purposes &mdash; no orders shall be fulfilled.', 'woocommerce' ),
101                 'type'      => 'text',
102                 'css'       => 'min-width:300px;',
103                 'autoload'  => false
104             ),
105 
106             array(
107                 'title'   => __( 'API', 'woocommerce' ),
108                 'desc'    => __( 'Enable the REST API', 'woocommerce' ),
109                 'id'      => 'woocommerce_api_enabled',
110                 'type'    => 'checkbox',
111                 'default' => 'yes',
112             ),
113 
114             array( 'type' => 'sectionend', 'id' => 'general_options'),
115 
116             array(  'title' => __( 'Currency Options', 'woocommerce' ), 'type' => 'title', 'desc' => __( 'The following options affect how prices are displayed on the frontend.', 'woocommerce' ), 'id' => 'pricing_options' ),
117 
118             array(
119                 'title'     => __( 'Currency', 'woocommerce' ),
120                 'desc'      => __( 'This controls what currency prices are listed at in the catalog and which currency gateways will take payments in.', 'woocommerce' ),
121                 'id'        => 'woocommerce_currency',
122                 'css'       => 'min-width:350px;',
123                 'default'   => 'GBP',
124                 'type'      => 'select',
125                 'class'     => 'chosen_select',
126                 'desc_tip'  =>  true,
127                 'options'   => $currency_code_options
128             ),
129 
130             array(
131                 'title' => __( 'Currency Position', 'woocommerce' ),
132                 'desc'      => __( 'This controls the position of the currency symbol.', 'woocommerce' ),
133                 'id'        => 'woocommerce_currency_pos',
134                 'css'       => 'min-width:350px;',
135                 'class'     => 'chosen_select',
136                 'default'   => 'left',
137                 'type'      => 'select',
138                 'options' => array(
139                     'left'        => __( 'Left', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . '99.99)',
140                     'right'       => __( 'Right', 'woocommerce' ) . ' (99.99' . get_woocommerce_currency_symbol() . ')',
141                     'left_space'  => __( 'Left with space', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ' 99.99)',
142                     'right_space' => __( 'Right with space', 'woocommerce' ) . ' (99.99 ' . get_woocommerce_currency_symbol() . ')'
143                 ),
144                 'desc_tip'  =>  true,
145             ),
146 
147             array(
148                 'title' => __( 'Thousand Separator', 'woocommerce' ),
149                 'desc'      => __( 'This sets the thousand separator of displayed prices.', 'woocommerce' ),
150                 'id'        => 'woocommerce_price_thousand_sep',
151                 'css'       => 'width:50px;',
152                 'default'   => ',',
153                 'type'      => 'text',
154                 'desc_tip'  =>  true,
155             ),
156 
157             array(
158                 'title' => __( 'Decimal Separator', 'woocommerce' ),
159                 'desc'      => __( 'This sets the decimal separator of displayed prices.', 'woocommerce' ),
160                 'id'        => 'woocommerce_price_decimal_sep',
161                 'css'       => 'width:50px;',
162                 'default'   => '.',
163                 'type'      => 'text',
164                 'desc_tip'  =>  true,
165             ),
166 
167             array(
168                 'title' => __( 'Number of Decimals', 'woocommerce' ),
169                 'desc'      => __( 'This sets the number of decimal points shown in displayed prices.', 'woocommerce' ),
170                 'id'        => 'woocommerce_price_num_decimals',
171                 'css'       => 'width:50px;',
172                 'default'   => '2',
173                 'desc_tip'  =>  true,
174                 'type'      => 'number',
175                 'custom_attributes' => array(
176                     'min'   => 0,
177                     'step'  => 1
178                 )
179             ),
180 
181             array( 'type' => 'sectionend', 'id' => 'pricing_options' ),
182 
183             array(  'title' => __( 'Styles and Scripts', 'woocommerce' ), 'type' => 'title', 'id' => 'script_styling_options' ),
184 
185             array( 'type'       => 'frontend_styles' ),
186 
187             array(
188                 'title' => __( 'Scripts', 'woocommerce' ),
189                 'desc'  => __( 'Enable Lightbox', 'woocommerce' ),
190                 'id'        => 'woocommerce_enable_lightbox',
191                 'default'   => 'yes',
192                 'desc_tip'  => __( 'Include WooCommerce\'s lightbox. Product gallery images will open in a lightbox.', 'woocommerce' ),
193                 'type'      => 'checkbox',
194                 'checkboxgroup'     => 'start'
195             ),
196 
197             array(
198                 'desc'      => __( 'Enable enhanced country select boxes', 'woocommerce' ),
199                 'id'        => 'woocommerce_enable_chosen',
200                 'default'   => 'yes',
201                 'type'      => 'checkbox',
202                 'checkboxgroup'     => 'end',
203                 'desc_tip'  => __( 'This will enable a script allowing the country fields to be searchable.', 'woocommerce' ),
204                 'autoload'  => false
205             ),
206 
207             array( 'type' => 'sectionend', 'id' => 'script_styling_options' ),
208 
209         ) ); // End general settings
210     }
211 
212     /**
213      * Output the frontend styles settings.
214      *
215      * @access public
216      * @return void
217      */
218     public function frontend_styles_setting() {
219         ?><tr valign="top" class="woocommerce_frontend_css_colors">
220             <th scope="row" class="titledesc">
221                 <?php _e( 'Frontend Styles', 'woocommerce' ); ?>
222             </th>
223             <td class="forminp"><?php
224 
225                 $base_file = WC()->plugin_path() . '/assets/css/woocommerce-base.less';
226                 $css_file  = WC()->plugin_path() . '/assets/css/woocommerce.css';
227 
228                 if ( is_writable( $base_file ) && is_writable( $css_file ) ) {
229 
230                     // Get settings
231                     $colors = array_map( 'esc_attr', (array) get_option( 'woocommerce_frontend_css_colors' ) );
232 
233                     // Defaults
234                     if ( empty( $colors['primary'] ) ) {
235                         $colors['primary'] = '#ad74a2';
236                     }
237                     if ( empty( $colors['secondary'] ) ) {
238                         $colors['secondary'] = '#f7f6f7';
239                     }
240                     if ( empty( $colors['highlight'] ) ) {
241                         $colors['highlight'] = '#85ad74';
242                     }
243                     if ( empty( $colors['content_bg'] ) ) {
244                         $colors['content_bg'] = '#ffffff';
245                     }
246                     if ( empty( $colors['subtext'] ) ) {
247                         $colors['subtext'] = '#777777';
248                     }
249 
250                     // Show inputs
251                     $this->color_picker( __( 'Primary', 'woocommerce' ), 'woocommerce_frontend_css_primary', $colors['primary'], __( 'Call to action buttons/price slider/layered nav UI', 'woocommerce' ) );
252                     $this->color_picker( __( 'Secondary', 'woocommerce' ), 'woocommerce_frontend_css_secondary', $colors['secondary'], __( 'Buttons and tabs', 'woocommerce' ) );
253                     $this->color_picker( __( 'Highlight', 'woocommerce' ), 'woocommerce_frontend_css_highlight', $colors['highlight'], __( 'Price labels and Sale Flashes', 'woocommerce' ) );
254                     $this->color_picker( __( 'Content', 'woocommerce' ), 'woocommerce_frontend_css_content_bg', $colors['content_bg'], __( 'Your themes page background - used for tab active states', 'woocommerce' ) );
255                     $this->color_picker( __( 'Subtext', 'woocommerce' ), 'woocommerce_frontend_css_subtext', $colors['subtext'], __( 'Used for certain text and asides - breadcrumbs, small text etc.', 'woocommerce' ) );
256 
257                 } else {
258                     echo '<span class="description">' . __( 'To edit colours <code>woocommerce/assets/css/woocommerce-base.less</code> and <code>woocommerce.css</code> need to be writable. See <a href="http://codex.wordpress.org/Changing_File_Permissions">the Codex</a> for more information.', 'woocommerce' ) . '</span>';
259                 }
260 
261             ?></td>
262         </tr><?php
263     }
264 
265     /**
266      * Output a colour picker input box.
267      *
268      * @access public
269      * @param mixed $name
270      * @param mixed $id
271      * @param mixed $value
272      * @param string $desc (default: '')
273      * @return void
274      */
275     function color_picker( $name, $id, $value, $desc = '' ) {
276         echo '<div class="color_box"><strong><img class="help_tip" data-tip="' . esc_attr( $desc ) . '" src="' . WC()->plugin_url() . '/assets/images/help.png" height="16" width="16" /> ' . esc_html( $name ) . '</strong>
277             <input name="' . esc_attr( $id ). '" id="' . esc_attr( $id ) . '" type="text" value="' . esc_attr( $value ) . '" class="colorpick" /> <div id="colorPickerDiv_' . esc_attr( $id ) . '" class="colorpickdiv"></div>
278         </div>';
279     }
280 
281     /**
282      * Save settings
283      */
284     public function save() {
285         $settings = $this->get_settings();
286 
287         WC_Admin_Settings::save_fields( $settings );
288 
289         if ( isset( $_POST['woocommerce_frontend_css_primary'] ) ) {
290 
291             // Save settings
292             $primary    = ( ! empty( $_POST['woocommerce_frontend_css_primary'] ) ) ? wc_format_hex( $_POST['woocommerce_frontend_css_primary'] ) : '';
293             $secondary  = ( ! empty( $_POST['woocommerce_frontend_css_secondary'] ) ) ? wc_format_hex( $_POST['woocommerce_frontend_css_secondary'] ) : '';
294             $highlight  = ( ! empty( $_POST['woocommerce_frontend_css_highlight'] ) ) ? wc_format_hex( $_POST['woocommerce_frontend_css_highlight'] ) : '';
295             $content_bg = ( ! empty( $_POST['woocommerce_frontend_css_content_bg'] ) ) ? wc_format_hex( $_POST['woocommerce_frontend_css_content_bg'] ) : '';
296             $subtext    = ( ! empty( $_POST['woocommerce_frontend_css_subtext'] ) ) ? wc_format_hex( $_POST['woocommerce_frontend_css_subtext'] ) : '';
297 
298             $colors = array(
299                 'primary'    => $primary,
300                 'secondary'  => $secondary,
301                 'highlight'  => $highlight,
302                 'content_bg' => $content_bg,
303                 'subtext'    => $subtext
304             );
305 
306             // Check the colors.
307             $valid_colors = true;
308             foreach ( $colors as $color ) {
309                 if ( ! preg_match( '/^#[a-f0-9]{6}$/i', $color ) ) {
310                     $valid_colors = false;
311                     WC_Admin_Settings::add_error( sprintf( __( 'Error saving the Frontend Styles, %s is not a valid color, please use only valid colors code.', 'woocommerce' ), $color ) );
312                     break;
313                 }
314             }
315 
316             if ( $valid_colors ) {
317                 $old_colors = get_option( 'woocommerce_frontend_css_colors' );
318                 update_option( 'woocommerce_frontend_css_colors', $colors );
319 
320                 if ( $old_colors != $colors ) {
321                     woocommerce_compile_less_styles();
322                 }
323             }
324         }
325     }
326 
327 }
328 
329 endif;
330 
331 return new WC_Settings_General();
332 
WooCommerce API documentation generated by ApiGen 2.8.0