1 <?php
  2 /**
  3  * WooCommerce Admin Functions
  4  *
  5  * @author      WooThemes
  6  * @category    Core
  7  * @package     WooCommerce/Admin/Functions
  8  * @version     2.1.0
  9  */
 10 
 11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 12 
 13 /**
 14  * Get all WooCommerce screen ids
 15  *
 16  * @return array
 17  */
 18 function wc_get_screen_ids() {
 19     $wc_screen_id = sanitize_title( __( 'WooCommerce', 'woocommerce' ) );
 20 
 21     return apply_filters( 'woocommerce_screen_ids', array(
 22         'toplevel_page_' . $wc_screen_id,
 23         $wc_screen_id . '_page_wc-reports',
 24         $wc_screen_id . '_page_wc-settings',
 25         $wc_screen_id . '_page_wc-status',
 26         $wc_screen_id . '_page_wc-addons',
 27         'product_page_product_attributes',
 28         'edit-shop_order',
 29         'shop_order',
 30         'edit-product',
 31         'product',
 32         'edit-shop_coupon',
 33         'shop_coupon',
 34         'edit-product_cat',
 35         'edit-product_tag',
 36         'edit-product_shipping_class'
 37     ) );
 38 }
 39 
 40 /**
 41  * Create a page and store the ID in an option.
 42  *
 43  * @access public
 44  * @param mixed $slug Slug for the new page
 45  * @param mixed $option Option name to store the page's ID
 46  * @param string $page_title (default: '') Title for the new page
 47  * @param string $page_content (default: '') Content for the new page
 48  * @param int $post_parent (default: 0) Parent for the new page
 49  * @return int page ID
 50  */
 51 function wc_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) {
 52     global $wpdb;
 53 
 54     $option_value = get_option( $option );
 55 
 56     if ( $option_value > 0 && get_post( $option_value ) )
 57         return -1;
 58 
 59     $page_found = null;
 60 
 61     if ( strlen( $page_content ) > 0 ) {
 62         // Search for an existing page with the specified page content (typically a shortcode)
 63         $page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . $wpdb->posts . " WHERE post_type='page' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
 64     } else {
 65         // Search for an existing page with the specified page slug
 66         $page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . $wpdb->posts . " WHERE post_type='page' AND post_name = %s LIMIT 1;", $slug ) );
 67     }
 68 
 69     if ( $page_found ) {
 70         if ( ! $option_value )
 71             update_option( $option, $page_found );
 72         
 73         return $page_found;
 74     }
 75 
 76     $page_data = array(
 77         'post_status'       => 'publish',
 78         'post_type'         => 'page',
 79         'post_author'       => 1,
 80         'post_name'         => $slug,
 81         'post_title'        => $page_title,
 82         'post_content'      => $page_content,
 83         'post_parent'       => $post_parent,
 84         'comment_status'    => 'closed'
 85     );
 86     $page_id = wp_insert_post( $page_data );
 87 
 88     if ( $option )
 89         update_option( $option, $page_id );
 90 
 91     return $page_id;
 92 }
 93 
 94 /**
 95  * Output admin fields.
 96  *
 97  * Loops though the woocommerce options array and outputs each field.
 98  *
 99  * @param array $options Opens array to output
100  */
101 function woocommerce_admin_fields( $options ) {
102     if ( ! class_exists( 'WC_Admin_Settings' ) )
103         include 'class-wc-admin-settings.php';
104 
105     WC_Admin_Settings::output_fields( $options );
106 }
107 
108 /**
109  * Update all settings which are passed.
110  *
111  * @access public
112  * @param array $options
113  * @return void
114  */
115 function woocommerce_update_options( $options ) {
116     if ( ! class_exists( 'WC_Admin_Settings' ) )
117         include 'class-wc-admin-settings.php';
118 
119     WC_Admin_Settings::save_fields( $options );
120 }
121 
122 /**
123  * Get a setting from the settings API.
124  *
125  * @param mixed $option
126  * @return string
127  */
128 function woocommerce_settings_get_option( $option_name, $default = '' ) {
129     if ( ! class_exists( 'WC_Admin_Settings' ) )
130         include 'class-wc-admin-settings.php';
131 
132     return WC_Admin_Settings::get_option( $option_name, $default );
133 }
134 
135 /**
136  * Generate CSS from the less file when changing colours.
137  *
138  * @access public
139  * @return void
140  */
141 function woocommerce_compile_less_styles() {
142     global $woocommerce;
143 
144     $colors         = array_map( 'esc_attr', (array) get_option( 'woocommerce_frontend_css_colors' ) );
145     $base_file      = WC()->plugin_path() . '/assets/css/woocommerce-base.less';
146     $less_file      = WC()->plugin_path() . '/assets/css/woocommerce.less';
147     $css_file       = WC()->plugin_path() . '/assets/css/woocommerce.css';
148 
149     // Write less file
150     if ( is_writable( $base_file ) && is_writable( $css_file ) ) {
151 
152         // Colours changed - recompile less
153         if ( ! class_exists( 'lessc' ) )
154             include_once( WC()->plugin_path() . '/includes/libraries/class-lessc.php' );
155         if ( ! class_exists( 'cssmin' ) )
156             include_once( WC()->plugin_path() . '/includes/libraries/class-cssmin.php' );
157 
158         try {
159             // Set default if colours not set
160             if ( ! $colors['primary'] ) $colors['primary'] = '#ad74a2';
161             if ( ! $colors['secondary'] ) $colors['secondary'] = '#f7f6f7';
162             if ( ! $colors['highlight'] ) $colors['highlight'] = '#85ad74';
163             if ( ! $colors['content_bg'] ) $colors['content_bg'] = '#ffffff';
164             if ( ! $colors['subtext'] ) $colors['subtext'] = '#777777';
165 
166             // Write new color to base file
167             $color_rules = "
168 @primary:       " . $colors['primary'] . ";
169 @primarytext:   " . wc_light_or_dark( $colors['primary'], 'desaturate(darken(@primary,50%),18%)', 'desaturate(lighten(@primary,50%),18%)' ) . ";
170 
171 @secondary:     " . $colors['secondary'] . ";
172 @secondarytext: " . wc_light_or_dark( $colors['secondary'], 'desaturate(darken(@secondary,60%),18%)', 'desaturate(lighten(@secondary,60%),18%)' ) . ";
173 
174 @highlight:     " . $colors['highlight'] . ";
175 @highlightext:  " . wc_light_or_dark( $colors['highlight'], 'desaturate(darken(@highlight,60%),18%)', 'desaturate(lighten(@highlight,60%),18%)' ) . ";
176 
177 @contentbg:     " . $colors['content_bg'] . ";
178 
179 @subtext:       " . $colors['subtext'] . ";
180             ";
181 
182             file_put_contents( $base_file, $color_rules );
183 
184             $less         = new lessc;
185             $compiled_css = $less->compileFile( $less_file );
186             $compiled_css = CssMin::minify( $compiled_css );
187 
188             if ( $compiled_css )
189                 file_put_contents( $css_file, $compiled_css );
190 
191         } catch ( exception $ex ) {
192             wp_die( __( 'Could not compile woocommerce.less:', 'woocommerce' ) . ' ' . $ex->getMessage() );
193         }
194     }
195 }
196 
WooCommerce API documentation generated by ApiGen 2.8.0