From 973e6d94b8980ae1ce38ff0562599aa1afd8347b Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Wed, 5 Jun 2013 13:30:06 +0200 Subject: [PATCH 01/13] Set up basic helper abstract and get function #3282 --- classes/abstracts/abstract-wc-helper.php | 4 ++++ woocommerce.php | 13 +++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 classes/abstracts/abstract-wc-helper.php diff --git a/classes/abstracts/abstract-wc-helper.php b/classes/abstracts/abstract-wc-helper.php new file mode 100644 index 00000000000..84ff166a3fe --- /dev/null +++ b/classes/abstracts/abstract-wc-helper.php @@ -0,0 +1,4 @@ +helpers[ $id ] ) ) { + $this->helpers[ $id ] = include( 'classes/helpers/class-wc-' . $id . '-helper.php' ); + } + + return $this->helpers[ $id ]; + } /** * Include required admin files. @@ -465,6 +475,9 @@ class Woocommerce { $this->countries = new WC_Countries(); // Countries class $this->integrations = new WC_Integrations(); // Integrations class + // Helpers blank array + $this->helpers = array(); + // Classes/actions loaded for the frontend and for ajax requests if ( ! is_admin() || defined('DOING_AJAX') ) { From 64c75547f31841e42d36a736d04760ce3ec94a32 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Wed, 5 Jun 2013 13:39:27 +0200 Subject: [PATCH 02/13] Abstracted transient helper to class #3282 --- admin/post-types/product.php | 4 +- .../writepanels/writepanel-product_data.php | 4 +- admin/woocommerce-admin-functions.php | 4 +- admin/woocommerce-admin-install.php | 2 +- admin/woocommerce-admin-settings.php | 2 +- admin/woocommerce-admin-status.php | 2 +- classes/abstracts/abstract-wc-product.php | 6 +- classes/class-wc-product-simple.php | 2 +- classes/class-wc-product-variable.php | 8 +- classes/class-wc-product-variation.php | 6 +- classes/helpers/class-wc-transient-helper.php | 65 ++++++++++++++++ woocommerce-ajax.php | 2 +- woocommerce-core-functions.php | 8 +- woocommerce.php | 76 ++++--------------- 14 files changed, 104 insertions(+), 87 deletions(-) create mode 100644 classes/helpers/class-wc-transient-helper.php diff --git a/admin/post-types/product.php b/admin/post-types/product.php index 5605410a722..18d065f0933 100644 --- a/admin/post-types/product.php +++ b/admin/post-types/product.php @@ -759,7 +759,7 @@ function woocommerce_admin_product_quick_edit_save( $post_id, $post ) { } // Clear transient - $woocommerce->clear_product_transients( $post_id ); + $woocommerce->get_helper( 'transient' )->clear_product_transients( $post_id ); } add_action( 'save_post', 'woocommerce_admin_product_quick_edit_save', 10, 2 ); @@ -1156,7 +1156,7 @@ function woocommerce_admin_product_bulk_edit_save( $post_id, $post ) { do_action( 'woocommerce_product_bulk_edit_save', $product ); // Clear transient - $woocommerce->clear_product_transients( $post_id ); + $woocommerce->get_helper( 'transient' )->clear_product_transients( $post_id ); } add_action( 'save_post', 'woocommerce_admin_product_bulk_edit_save', 10, 2 ); diff --git a/admin/post-types/writepanels/writepanel-product_data.php b/admin/post-types/writepanels/writepanel-product_data.php index ed0bce229cc..7039fd87b4f 100644 --- a/admin/post-types/writepanels/writepanel-product_data.php +++ b/admin/post-types/writepanels/writepanel-product_data.php @@ -890,7 +890,7 @@ function woocommerce_process_product_meta( $post_id, $post ) { } // Clear cache/transients - $woocommerce->clear_product_transients( $clear_id ); + $woocommerce->get_helper( 'transient' )->clear_product_transients( $clear_id ); } } } @@ -1021,7 +1021,7 @@ function woocommerce_process_product_meta( $post_id, $post ) { do_action( 'woocommerce_process_product_meta_' . $product_type, $post_id ); // Clear cache/transients - $woocommerce->clear_product_transients( $post_id ); + $woocommerce->get_helper( 'transient' )->clear_product_transients( $post_id ); } add_action('woocommerce_process_product_meta', 'woocommerce_process_product_meta', 1, 2); diff --git a/admin/woocommerce-admin-functions.php b/admin/woocommerce-admin-functions.php index 69834ce37cc..e86950df0ac 100644 --- a/admin/woocommerce-admin-functions.php +++ b/admin/woocommerce-admin-functions.php @@ -105,12 +105,12 @@ function woocommerce_delete_post( $id ) { wp_update_post( $child_post ); } - $woocommerce->clear_product_transients(); + $woocommerce->get_helper( 'transient' )->clear_product_transients(); break; case 'product_variation' : - $woocommerce->clear_product_transients(); + $woocommerce->get_helper( 'transient' )->clear_product_transients(); break; } diff --git a/admin/woocommerce-admin-install.php b/admin/woocommerce-admin-install.php index ff35af62bf0..7632845f298 100644 --- a/admin/woocommerce-admin-install.php +++ b/admin/woocommerce-admin-install.php @@ -87,7 +87,7 @@ function do_install_woocommerce() { } // Clear transient cache - $woocommerce->clear_product_transients(); + $woocommerce->get_helper( 'transient' )->clear_product_transients(); // Recompile LESS styles if they are custom if ( get_option( 'woocommerce_frontend_css' ) == 'yes' ) { diff --git a/admin/woocommerce-admin-settings.php b/admin/woocommerce-admin-settings.php index ca8d69d8eed..aca5ebd44fe 100644 --- a/admin/woocommerce-admin-settings.php +++ b/admin/woocommerce-admin-settings.php @@ -134,7 +134,7 @@ if ( ! function_exists( 'woocommerce_settings' ) ) { } // Clear any unwanted data - $woocommerce->clear_product_transients(); + $woocommerce->get_helper( 'transient' )->clear_product_transients(); delete_transient( 'woocommerce_cache_excluded_uris' ); diff --git a/admin/woocommerce-admin-status.php b/admin/woocommerce-admin-status.php index ddb755462c3..ca6855972cd 100644 --- a/admin/woocommerce-admin-status.php +++ b/admin/woocommerce-admin-status.php @@ -586,7 +586,7 @@ function woocommerce_status_tools() { switch ( $_GET['action'] ) { case "clear_transients" : - $woocommerce->clear_product_transients(); + $woocommerce->get_helper( 'transient' )->clear_product_transients(); echo '

' . __( 'Product Transients Cleared', 'woocommerce' ) . '

'; break; diff --git a/classes/abstracts/abstract-wc-product.php b/classes/abstracts/abstract-wc-product.php index 5fb856cf4d4..e878f3c73ec 100644 --- a/classes/abstracts/abstract-wc-product.php +++ b/classes/abstracts/abstract-wc-product.php @@ -168,7 +168,7 @@ class WC_Product { elseif ( $this->backorders_allowed() || $this->get_total_stock() > 0 ) $this->set_stock_status( 'instock' ); - $woocommerce->clear_product_transients( $this->id ); // Clear transient + $woocommerce->get_helper( 'transient' )->clear_product_transients( $this->id ); // Clear transient return $this->get_stock_quantity(); } @@ -193,7 +193,7 @@ class WC_Product { if ( ! $this->backorders_allowed() && $this->get_total_stock() <= 0 ) $this->set_stock_status( 'outofstock' ); - $woocommerce->clear_product_transients( $this->id ); // Clear transient + $woocommerce->get_helper( 'transient' )->clear_product_transients( $this->id ); // Clear transient return $this->get_stock_quantity(); } @@ -218,7 +218,7 @@ class WC_Product { if ( $this->backorders_allowed() || $this->get_total_stock() > 0 ) $this->set_stock_status( 'instock' ); - $woocommerce->clear_product_transients( $this->id ); // Clear transient + $woocommerce->get_helper( 'transient' )->clear_product_transients( $this->id ); // Clear transient return $this->get_stock_quantity(); } diff --git a/classes/class-wc-product-simple.php b/classes/class-wc-product-simple.php index 2190846b6a0..2203984660b 100644 --- a/classes/class-wc-product-simple.php +++ b/classes/class-wc-product-simple.php @@ -70,6 +70,6 @@ class WC_Product_Simple extends WC_Product { } } - $woocommerce->clear_product_transients( $this->id ); + $woocommerce->get_helper( 'transient' )->clear_product_transients( $this->id ); } } \ No newline at end of file diff --git a/classes/class-wc-product-variable.php b/classes/class-wc-product-variable.php index 31f8e8b79fc..3be23e35604 100644 --- a/classes/class-wc-product-variable.php +++ b/classes/class-wc-product-variable.php @@ -87,7 +87,7 @@ class WC_Product_Variable extends WC_Product { elseif ( $this->backorders_allowed() || $this->get_total_stock() > 0 ) $this->set_stock_status( 'instock' ); - $woocommerce->clear_product_transients( $this->id ); // Clear transient + $woocommerce->get_helper( 'transient' )->clear_product_transients( $this->id ); // Clear transient return apply_filters( 'woocommerce_stock_amount', $this->stock ); } @@ -112,7 +112,7 @@ class WC_Product_Variable extends WC_Product { if ( ! $this->backorders_allowed() && $this->get_total_stock() <= 0 ) $this->set_stock_status( 'outofstock' ); - $woocommerce->clear_product_transients( $this->id ); // Clear transient + $woocommerce->get_helper( 'transient' )->clear_product_transients( $this->id ); // Clear transient return apply_filters( 'woocommerce_stock_amount', $this->stock ); } @@ -138,7 +138,7 @@ class WC_Product_Variable extends WC_Product { if ( $this->backorders_allowed() || $this->get_total_stock() > 0 ) $this->set_stock_status( 'instock' ); - $woocommerce->clear_product_transients( $this->id ); // Clear transient + $woocommerce->get_helper( 'transient' )->clear_product_transients( $this->id ); // Clear transient return apply_filters( 'woocommerce_stock_amount', $this->stock ); endif; @@ -506,7 +506,7 @@ class WC_Product_Variable extends WC_Product { update_post_meta( $this->id, '_min_variation_sale_price', $this->min_variation_sale_price ); update_post_meta( $this->id, '_max_variation_sale_price', $this->max_variation_sale_price ); - $woocommerce->clear_product_transients( $this->id ); + $woocommerce->get_helper( 'transient' )->clear_product_transients( $this->id ); } } } \ No newline at end of file diff --git a/classes/class-wc-product-variation.php b/classes/class-wc-product-variation.php index d9e0e93964e..317ec2a7932 100644 --- a/classes/class-wc-product-variation.php +++ b/classes/class-wc-product-variation.php @@ -297,7 +297,7 @@ class WC_Product_Variation extends WC_Product { $this->stock = intval( $amount ); $this->total_stock = intval( $amount ); update_post_meta( $this->variation_id, '_stock', $this->stock ); - $woocommerce->clear_product_transients( $this->id ); // Clear transient + $woocommerce->get_helper( 'transient' )->clear_product_transients( $this->id ); // Clear transient // Check parents out of stock attribute if ( ! $this->is_in_stock() ) { @@ -337,7 +337,7 @@ class WC_Product_Variation extends WC_Product { $this->stock = $this->stock - $by; $this->total_stock = $this->total_stock - $by; update_post_meta( $this->variation_id, '_stock', $this->stock ); - $woocommerce->clear_product_transients( $this->id ); // Clear transient + $woocommerce->get_helper( 'transient' )->clear_product_transients( $this->id ); // Clear transient // Check parents out of stock attribute if ( ! $this->is_in_stock() ) { @@ -375,7 +375,7 @@ class WC_Product_Variation extends WC_Product { $this->stock = $this->stock + $by; $this->total_stock = $this->total_stock + $by; update_post_meta( $this->variation_id, '_stock', $this->stock ); - $woocommerce->clear_product_transients( $this->id ); // Clear transient + $woocommerce->get_helper( 'transient' )->clear_product_transients( $this->id ); // Clear transient // Parents out of stock attribute if ( $this->is_in_stock() ) diff --git a/classes/helpers/class-wc-transient-helper.php b/classes/helpers/class-wc-transient-helper.php new file mode 100644 index 00000000000..ad347970c70 --- /dev/null +++ b/classes/helpers/class-wc-transient-helper.php @@ -0,0 +1,65 @@ +show_errors(); + + // Clear core transients + $transients_to_clear = array( + 'wc_products_onsale', + 'wc_hidden_product_ids', + 'wc_hidden_product_ids_search', + 'wc_attribute_taxonomies', + 'wc_term_counts' + ); + + foreach( $transients_to_clear as $transient ) { + delete_transient( $transient ); + } + + // Clear transients for which we don't have the name + $wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_uf_pid_%') OR `option_name` LIKE ('_transient_timeout_wc_uf_pid_%')" ); + $wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_ln_count_%') OR `option_name` LIKE ('_transient_timeout_wc_ln_count_%')" ); + $wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_ship_%') OR `option_name` LIKE ('_transient_timeout_wc_ship_%')" ); + + // Clear product specific transients + $post_transients_to_clear = array( + 'wc_product_children_ids_', + 'wc_product_total_stock_', + 'wc_average_rating_', + 'wc_rating_count_', + 'woocommerce_product_type_', // No longer used + 'wc_product_type_', // No longer used + ); + + if ( $post_id > 0 ) { + + foreach( $post_transients_to_clear as $transient ) { + delete_transient( $transient . $post_id ); + $wpdb->query( $wpdb->prepare( "DELETE FROM `$wpdb->options` WHERE `option_name` = %s OR `option_name` = %s", '_transient_' . $transient . $post_id, '_transient_timeout_' . $transient . $post_id ) ); + } + + clean_post_cache( $post_id ); + + } else { + + foreach( $post_transients_to_clear as $transient ) { + $wpdb->query( $wpdb->prepare( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE %s OR `option_name` LIKE %s", '_transient_' . $transient . '%', '_transient_timeout_' . $transient . '%' ) ); + } + + } + } +} \ No newline at end of file diff --git a/woocommerce-ajax.php b/woocommerce-ajax.php index f2da9337570..e617cd38d0d 100644 --- a/woocommerce-ajax.php +++ b/woocommerce-ajax.php @@ -744,7 +744,7 @@ function woocommerce_link_all_variations() { break; } - $woocommerce->clear_product_transients( $post_id ); + $woocommerce->get_helper( 'transient' )->clear_product_transients( $post_id ); echo $added; diff --git a/woocommerce-core-functions.php b/woocommerce-core-functions.php index a1deaa0dc4e..dd46d11d85f 100644 --- a/woocommerce-core-functions.php +++ b/woocommerce-core-functions.php @@ -2412,7 +2412,7 @@ function woocommerce_scheduled_sales() { update_post_meta( $product_id, '_sale_price_dates_to', '' ); } - $woocommerce->clear_product_transients( $product_id ); + $woocommerce->get_helper( 'transient' )->clear_product_transients( $product_id ); $parent = wp_get_post_parent_id( $product_id ); @@ -2426,7 +2426,7 @@ function woocommerce_scheduled_sales() { if ( $this_product->is_type( 'simple' ) ) $this_product->grouped_product_sync(); - $woocommerce->clear_product_transients( $parent ); + $woocommerce->get_helper( 'transient' )->clear_product_transients( $parent ); } } } @@ -2453,7 +2453,7 @@ function woocommerce_scheduled_sales() { update_post_meta( $product_id, '_sale_price_dates_from', '' ); update_post_meta( $product_id, '_sale_price_dates_to', '' ); - $woocommerce->clear_product_transients( $product_id ); + $woocommerce->get_helper( 'transient' )->clear_product_transients( $product_id ); $parent = wp_get_post_parent_id( $product_id ); @@ -2467,7 +2467,7 @@ function woocommerce_scheduled_sales() { if ( $this_product->is_type( 'simple' ) ) $this_product->grouped_product_sync(); - $woocommerce->clear_product_transients( $parent ); + $woocommerce->get_helper( 'transient' )->clear_product_transients( $parent ); } } } diff --git a/woocommerce.php b/woocommerce.php index b35cee0d056..f2c8d118b66 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -1430,68 +1430,6 @@ class Woocommerce { return ob_get_clean(); } - /** Transients ************************************************************/ - - /** - * Clear all transients cache for product data. - * - * @access public - * @param int $post_id (default: 0) - * @return void - */ - public function clear_product_transients( $post_id = 0 ) { - global $wpdb; - - $post_id = absint( $post_id ); - - $wpdb->show_errors(); - - // Clear core transients - $transients_to_clear = array( - 'wc_products_onsale', - 'wc_hidden_product_ids', - 'wc_hidden_product_ids_search', - 'wc_attribute_taxonomies', - 'wc_term_counts' - ); - - foreach( $transients_to_clear as $transient ) { - delete_transient( $transient ); - } - - // Clear transients for which we don't have the name - $wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_uf_pid_%') OR `option_name` LIKE ('_transient_timeout_wc_uf_pid_%')" ); - $wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_ln_count_%') OR `option_name` LIKE ('_transient_timeout_wc_ln_count_%')" ); - $wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_ship_%') OR `option_name` LIKE ('_transient_timeout_wc_ship_%')" ); - - // Clear product specific transients - $post_transients_to_clear = array( - 'wc_product_children_ids_', - 'wc_product_total_stock_', - 'wc_average_rating_', - 'wc_rating_count_', - 'woocommerce_product_type_', // No longer used - 'wc_product_type_', // No longer used - ); - - if ( $post_id > 0 ) { - - foreach( $post_transients_to_clear as $transient ) { - delete_transient( $transient . $post_id ); - $wpdb->query( $wpdb->prepare( "DELETE FROM `$wpdb->options` WHERE `option_name` = %s OR `option_name` = %s", '_transient_' . $transient . $post_id, '_transient_timeout_' . $transient . $post_id ) ); - } - - clean_post_cache( $post_id ); - - } else { - - foreach( $post_transients_to_clear as $transient ) { - $wpdb->query( $wpdb->prepare( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE %s OR `option_name` LIKE %s", '_transient_' . $transient . '%', '_transient_timeout_' . $transient . '%' ) ); - } - - } - } - /** Body Classes **********************************************************/ /** @@ -1628,6 +1566,20 @@ class Woocommerce { } } + /** + * Clear all transients cache for product data. + * + * @deprecated 2.1.0 Access via the helpers + * @access public + * @param int $post_id (default: 0) + * @return void + */ + public function clear_product_transients( $post_id = 0 ) { + _deprecated_function( 'Woocommerce->clear_product_transients', '2.1', 'WC_Transient_Helper->clear_product_transients' ); + $helper = $this->get_helper( 'transient' ); + $helper->clear_product_transients( $post_id ); + } + } /** From b3f05bdd66dd9d2aec10184976b96b6257f627c4 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Wed, 5 Jun 2013 13:51:06 +0200 Subject: [PATCH 03/13] Abstracted inline JS helper to class #3282 --- admin/post-types/shop_coupon.php | 2 +- admin/post-types/shop_order.php | 2 +- .../writepanels/writepanel-order_data.php | 2 +- .../writepanel-order_downloads.php | 2 +- .../writepanel-product-type-variable.php | 2 +- classes/abstracts/abstract-wc-email.php | 2 +- .../paypal/class-wc-gateway-paypal.php | 2 +- .../class-wc-inline-javascript-helper.php | 43 +++++++++++++ .../class-wc-google-analytics.php | 2 +- .../widgets/class-wc-widget-layered-nav.php | 2 +- woocommerce.php | 64 ++++++++----------- 11 files changed, 78 insertions(+), 47 deletions(-) create mode 100644 classes/helpers/class-wc-inline-javascript-helper.php diff --git a/admin/post-types/shop_coupon.php b/admin/post-types/shop_coupon.php index 1c9e53c0c7c..7f351de1253 100644 --- a/admin/post-types/shop_coupon.php +++ b/admin/post-types/shop_coupon.php @@ -162,7 +162,7 @@ function woocommerce_restrict_manage_coupons() { add_inline_js( " + $woocommerce->get_helper( 'inline-javascript' )->add_inline_js( " jQuery('select#dropdown_shop_coupon_type, select[name=m]').css('width', '150px').chosen(); " ); } diff --git a/admin/post-types/shop_order.php b/admin/post-types/shop_order.php index 802d57e48c1..c8427312d39 100644 --- a/admin/post-types/shop_order.php +++ b/admin/post-types/shop_order.php @@ -310,7 +310,7 @@ function woocommerce_restrict_manage_orders() { add_inline_js( " + $woocommerce->get_helper( 'inline-javascript' )->add_inline_js( " jQuery('select#dropdown_shop_order_status, select[name=m]').css('width', '150px').chosen(); diff --git a/admin/post-types/writepanels/writepanel-order_data.php b/admin/post-types/writepanels/writepanel-order_data.php index a36ee8a979c..a696a04480d 100644 --- a/admin/post-types/writepanels/writepanel-order_data.php +++ b/admin/post-types/writepanels/writepanel-order_data.php @@ -101,7 +101,7 @@ function woocommerce_order_data_meta_box($post) { add_inline_js( " + $woocommerce->get_helper( 'inline-javascript' )->add_inline_js( " jQuery('select.ajax_chosen_select_customer').ajaxChosen({ method: 'GET', url: '" . admin_url('admin-ajax.php') . "', diff --git a/admin/post-types/writepanels/writepanel-order_downloads.php b/admin/post-types/writepanels/writepanel-order_downloads.php index 0d532279cf7..9c4d0fd5351 100644 --- a/admin/post-types/writepanels/writepanel-order_downloads.php +++ b/admin/post-types/writepanels/writepanel-order_downloads.php @@ -184,7 +184,7 @@ function woocommerce_order_downloads_meta_box() { }); add_inline_js( $javascript ); + $woocommerce->get_helper( 'inline-javascript' )->add_inline_js( $javascript ); } diff --git a/admin/post-types/writepanels/writepanel-product-type-variable.php b/admin/post-types/writepanels/writepanel-product-type-variable.php index 326a185f1b5..89479f09cd7 100755 --- a/admin/post-types/writepanels/writepanel-product-type-variable.php +++ b/admin/post-types/writepanels/writepanel-product-type-variable.php @@ -616,7 +616,7 @@ function variable_product_type_options() { }); add_inline_js( $javascript ); + $woocommerce->get_helper( 'inline-javascript' )->add_inline_js( $javascript ); } add_action('woocommerce_product_write_panels', 'variable_product_type_options'); diff --git a/classes/abstracts/abstract-wc-email.php b/classes/abstracts/abstract-wc-email.php index abf86b122b5..df35d92c13e 100644 --- a/classes/abstracts/abstract-wc-email.php +++ b/classes/abstracts/abstract-wc-email.php @@ -631,7 +631,7 @@ abstract class WC_Email extends WC_Settings_API { ?> add_inline_js(" + $woocommerce->get_helper( 'inline-javascript' )->add_inline_js(" jQuery('select.email_type').change(function(){ var val = jQuery( this ).val(); diff --git a/classes/gateways/paypal/class-wc-gateway-paypal.php b/classes/gateways/paypal/class-wc-gateway-paypal.php index c69c394ee25..85898bc2fe6 100644 --- a/classes/gateways/paypal/class-wc-gateway-paypal.php +++ b/classes/gateways/paypal/class-wc-gateway-paypal.php @@ -422,7 +422,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway { $paypal_args_array[] = ''; } - $woocommerce->add_inline_js( ' + $woocommerce->get_helper( 'inline-javascript' )->add_inline_js( ' $.blockUI({ message: "' . esc_js( __( 'Thank you for your order. We are now redirecting you to PayPal to make payment.', 'woocommerce' ) ) . '", baseZ: 99999, diff --git a/classes/helpers/class-wc-inline-javascript-helper.php b/classes/helpers/class-wc-inline-javascript-helper.php new file mode 100644 index 00000000000..6ae586c771e --- /dev/null +++ b/classes/helpers/class-wc-inline-javascript-helper.php @@ -0,0 +1,43 @@ +_inline_js .= "\n" . $code . "\n"; + } + + /** + * Output any queued inline JS. + * + * @access public + * @return void + */ + public function output_inline_js() { + if ( $this->_inline_js ) { + + echo "\n\n"; + + $this->_inline_js = ''; + } + } +} \ No newline at end of file diff --git a/classes/integrations/google-analytics/class-wc-google-analytics.php b/classes/integrations/google-analytics/class-wc-google-analytics.php index 36241e9db97..9ff9e24081d 100644 --- a/classes/integrations/google-analytics/class-wc-google-analytics.php +++ b/classes/integrations/google-analytics/class-wc-google-analytics.php @@ -311,7 +311,7 @@ class WC_Google_Analytics extends WC_Integration { $parameters = apply_filters( 'woocommerce_ga_event_tracking_parameters', $parameters ); - $woocommerce->add_inline_js(" + $woocommerce->get_helper( 'inline-javascript' )->add_inline_js(" $('" . $selector . "').click(function() { " . sprintf( "_gaq.push(['_trackEvent', %s, %s, %s]);", $parameters['category'], $parameters['action'], $parameters['label'] ) . " }); diff --git a/classes/widgets/class-wc-widget-layered-nav.php b/classes/widgets/class-wc-widget-layered-nav.php index 104df846b70..4e409451a0c 100644 --- a/classes/widgets/class-wc-widget-layered-nav.php +++ b/classes/widgets/class-wc-widget-layered-nav.php @@ -169,7 +169,7 @@ class WC_Widget_Layered_Nav extends WC_Widget { echo ''; - $woocommerce->add_inline_js(" + $woocommerce->get_helper( 'inline-javascript' )->add_inline_js(" jQuery('#dropdown_layered_nav_$taxonomy_filter').change(function(){ diff --git a/woocommerce.php b/woocommerce.php index f2c8d118b66..31a5d7a6a3c 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -1489,44 +1489,6 @@ class Woocommerce { return $classes; } - /** Inline JavaScript Helper **********************************************/ - - /** - * Add some JavaScript inline to be output in the footer. - * - * @access public - * @param string $code - * @return void - */ - public function add_inline_js( $code ) { - $this->_inline_js .= "\n" . $code . "\n"; - } - - /** - * Output any queued inline JS. - * - * @access public - * @return void - */ - public function output_inline_js() { - if ( $this->_inline_js ) { - - echo "\n\n"; - - $this->_inline_js = ''; - } - } - /** Deprecated functions *********************************************************/ /** @@ -1580,6 +1542,32 @@ class Woocommerce { $helper->clear_product_transients( $post_id ); } + /** + * Add some JavaScript inline to be output in the footer. + * + * @deprecated 2.1.0 Access via the helpers + * @access public + * @param string $code + * @return void + */ + public function add_inline_js( $code ) { + _deprecated_function( 'Woocommerce->add_inline_js', '2.1', 'WC_Inline_Javascript_Helper->add_inline_js' ); + $helper = $this->get_helper( 'inline-javascript' ); + $helper->add_inline_js( $code ); + } + + /** + * Output any queued inline JS. + * + * @deprecated 2.1.0 Access via the helpers + * @access public + * @return void + */ + public function output_inline_js() { + _deprecated_function( 'Woocommerce->output_inline_js', '2.1', 'WC_Inline_Javascript_Helper->output_inline_js' ); + $helper = $this->get_helper( 'inline-javascript' ); + $helper->output_inline_js(); + } } /** From 88a7f0f72097dff1c902a3a96748d95fb0698815 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Wed, 5 Jun 2013 15:43:24 +0200 Subject: [PATCH 04/13] Abstracted nonce helper to class #3282 --- classes/class-wc-cart.php | 2 +- classes/class-wc-order.php | 2 +- classes/helpers/class-wc-nonce-helper.php | 31 ++++++++++ templates/cart/cart.php | 2 +- templates/cart/shipping-calculator.php | 2 +- templates/checkout/form-pay.php | 2 +- templates/checkout/review-order.php | 2 +- templates/myaccount/form-change-password.php | 2 +- templates/myaccount/form-edit-address.php | 2 +- templates/myaccount/form-login.php | 4 +- templates/myaccount/form-lost-password.php | 2 +- templates/order/form-tracking.php | 2 +- templates/order/order-details.php | 2 +- templates/shop/form-login.php | 2 +- templates/single-product-reviews.php | 2 +- woocommerce.php | 60 ++++++++++---------- 16 files changed, 77 insertions(+), 44 deletions(-) create mode 100644 classes/helpers/class-wc-nonce-helper.php diff --git a/classes/class-wc-cart.php b/classes/class-wc-cart.php index de3857a254e..a581113edad 100644 --- a/classes/class-wc-cart.php +++ b/classes/class-wc-cart.php @@ -682,7 +682,7 @@ class WC_Cart { global $woocommerce; $cart_page_id = woocommerce_get_page_id('cart'); if ($cart_page_id) - return apply_filters( 'woocommerce_get_remove_url', $woocommerce->nonce_url( 'cart', add_query_arg( 'remove_item', $cart_item_key, get_permalink($cart_page_id) ) ) ); + return apply_filters( 'woocommerce_get_remove_url', $woocommerce->get_helper( 'nonce' )->nonce_url( 'cart', add_query_arg( 'remove_item', $cart_item_key, get_permalink($cart_page_id) ) ) ); } /** diff --git a/classes/class-wc-order.php b/classes/class-wc-order.php index 6c0fca74212..23e04ad7654 100644 --- a/classes/class-wc-order.php +++ b/classes/class-wc-order.php @@ -1147,7 +1147,7 @@ class WC_Order { */ public function get_cancel_order_url() { global $woocommerce; - return apply_filters('woocommerce_get_cancel_order_url', $woocommerce->nonce_url( 'cancel_order', add_query_arg('cancel_order', 'true', add_query_arg('order', $this->order_key, add_query_arg('order_id', $this->id, trailingslashit( home_url() )))))); + return apply_filters('woocommerce_get_cancel_order_url', $woocommerce->get_helper( 'nonce' )->nonce_url( 'cancel_order', add_query_arg('cancel_order', 'true', add_query_arg('order', $this->order_key, add_query_arg('order_id', $this->id, trailingslashit( home_url() )))))); } diff --git a/classes/helpers/class-wc-nonce-helper.php b/classes/helpers/class-wc-nonce-helper.php new file mode 100644 index 00000000000..6ee2a0ccc72 --- /dev/null +++ b/classes/helpers/class-wc-nonce-helper.php @@ -0,0 +1,31 @@ +show_messages(); - nonce_field('cart') ?> + get_helper( 'nonce' )->nonce_field('cart') ?> diff --git a/templates/cart/shipping-calculator.php b/templates/cart/shipping-calculator.php index 759e7b55d62..4421a3977f8 100644 --- a/templates/cart/shipping-calculator.php +++ b/templates/cart/shipping-calculator.php @@ -84,7 +84,7 @@ if ( get_option('woocommerce_enable_shipping_calc')=='no' || ! $woocommerce->car

- nonce_field('cart') ?> + get_helper( 'nonce' )->nonce_field('cart') ?> diff --git a/templates/checkout/form-pay.php b/templates/checkout/form-pay.php index ebe8762ef26..ca97c65383e 100644 --- a/templates/checkout/form-pay.php +++ b/templates/checkout/form-pay.php @@ -83,7 +83,7 @@ global $woocommerce;
- nonce_field('pay')?> + get_helper( 'nonce' )->nonce_field('pay')?>
diff --git a/templates/checkout/review-order.php b/templates/checkout/review-order.php index b36a6281a40..d57f54ad473 100644 --- a/templates/checkout/review-order.php +++ b/templates/checkout/review-order.php @@ -184,7 +184,7 @@ $available_methods = $woocommerce->shipping->get_available_shipping_methods(); - nonce_field('process_checkout')?> + get_helper( 'nonce' )->nonce_field('process_checkout')?> diff --git a/templates/myaccount/form-change-password.php b/templates/myaccount/form-change-password.php index 0d775930322..9869be9d412 100644 --- a/templates/myaccount/form-change-password.php +++ b/templates/myaccount/form-change-password.php @@ -28,7 +28,7 @@ global $woocommerce;

- nonce_field('change_password')?> + get_helper( 'nonce' )->nonce_field('change_password')?> \ No newline at end of file diff --git a/templates/myaccount/form-edit-address.php b/templates/myaccount/form-edit-address.php index bfc438cd1e7..43b50d6cce7 100644 --- a/templates/myaccount/form-edit-address.php +++ b/templates/myaccount/form-edit-address.php @@ -41,7 +41,7 @@ get_currentuserinfo();

- nonce_field('edit_address') ?> + get_helper( 'nonce' )->nonce_field('edit_address') ?>

diff --git a/templates/myaccount/form-login.php b/templates/myaccount/form-login.php index 838dffd5606..6b5b67b06ea 100644 --- a/templates/myaccount/form-login.php +++ b/templates/myaccount/form-login.php @@ -36,7 +36,7 @@ global $woocommerce; ?>

- nonce_field('login', 'login') ?> + get_helper( 'nonce' )->nonce_field('login', 'login') ?> - nonce_field('register', 'register') ?> + get_helper( 'nonce' )->nonce_field('register', 'register') ?>

diff --git a/templates/myaccount/form-lost-password.php b/templates/myaccount/form-lost-password.php index 567784502d0..39f769e741f 100644 --- a/templates/myaccount/form-lost-password.php +++ b/templates/myaccount/form-lost-password.php @@ -43,6 +43,6 @@ global $woocommerce, $post;

- nonce_field( $args['form'] ); ?> + get_helper( 'nonce' )->nonce_field( $args['form'] ); ?> \ No newline at end of file diff --git a/templates/order/form-tracking.php b/templates/order/form-tracking.php index 39a884fbe0b..19cd7e493c6 100644 --- a/templates/order/form-tracking.php +++ b/templates/order/form-tracking.php @@ -21,6 +21,6 @@ global $woocommerce, $post;

- nonce_field('order_tracking') ?> + get_helper( 'nonce' )->nonce_field('order_tracking') ?> \ No newline at end of file diff --git a/templates/order/order-details.php b/templates/order/order-details.php index 7c64a70d43c..199e354b69b 100755 --- a/templates/order/order-details.php +++ b/templates/order/order-details.php @@ -87,7 +87,7 @@ $order = new WC_Order( $order_id ); status=='completed' ) : ?>

- +

diff --git a/templates/shop/form-login.php b/templates/shop/form-login.php index 1d377331978..064238ade1f 100644 --- a/templates/shop/form-login.php +++ b/templates/shop/form-login.php @@ -27,7 +27,7 @@ if (is_user_logged_in()) return;

- nonce_field('login', 'login') ?> + get_helper( 'nonce' )->nonce_field('login', 'login') ?> diff --git a/templates/single-product-reviews.php b/templates/single-product-reviews.php index 36c103e1e00..f0711bee253 100644 --- a/templates/single-product-reviews.php +++ b/templates/single-product-reviews.php @@ -104,7 +104,7 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly } - $comment_form['comment_field'] .= '

' . $woocommerce->nonce_field('comment_rating', true, false); + $comment_form['comment_field'] .= '

' . $woocommerce->get_helper( 'nonce' )->nonce_field('comment_rating', true, false); comment_form( apply_filters( 'woocommerce_product_review_comment_form_args', $comment_form ) ); diff --git a/woocommerce.php b/woocommerce.php index 31a5d7a6a3c..47fd3f63e40 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -1342,35 +1342,6 @@ class Woocommerce { if ( isset( $types[$type] ) ) return $types[$type]; } - /** Nonces ****************************************************************/ - - /** - * Return a nonce field. - * - * @access public - * @param mixed $action - * @param bool $referer (default: true) - * @param bool $echo (default: true) - * @return void - */ - public function nonce_field( $action, $referer = true , $echo = true ) { - return wp_nonce_field('woocommerce-' . $action, '_n', $referer, $echo ); - } - - - /** - * Return a url with a nonce appended. - * - * @access public - * @param mixed $action - * @param string $url (default: '') - * @return string - */ - public function nonce_url( $action, $url = '' ) { - return add_query_arg( '_n', wp_create_nonce( 'woocommerce-' . $action ), $url ); - } - - /** * Check a nonce and sets woocommerce error in case it is invalid. * @@ -1568,6 +1539,37 @@ class Woocommerce { $helper = $this->get_helper( 'inline-javascript' ); $helper->output_inline_js(); } + + /** + * Return a nonce field. + * + * @deprecated 2.1.0 Access via the helpers + * @access public + * @param mixed $action + * @param bool $referer (default: true) + * @param bool $echo (default: true) + * @return void + */ + public function nonce_field( $action, $referer = true , $echo = true ) { + _deprecated_function( 'Woocommerce->nonce_field', '2.1', 'WC_Nonce_Helper->nonce_field' ); + $helper = $this->get_helper( 'nonce' ); + return $helper->nonce_field( $action, $referer = true , $echo = true ); + } + + /** + * Return a url with a nonce appended. + * + * @deprecated 2.1.0 Access via the helpers + * @access public + * @param mixed $action + * @param string $url (default: '') + * @return string + */ + public function nonce_url( $action, $url = '' ) { + _deprecated_function( 'Woocommerce->nonce_url', '2.1', 'WC_Nonce_Helper->nonce_url' ); + $helper = $this->get_helper( 'nonce' ); + return $helper->nonce_url( $action, $url = '' ); + } } /** From 05bfac8c16c08b6d0a4fca080fd014c3042ecf69 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Wed, 5 Jun 2013 15:49:48 +0200 Subject: [PATCH 05/13] Abstracted shortcode helper to class #3282 --- classes/class-wc-shortcodes.php | 16 +++--- classes/helpers/class-wc-shortcode-helper.php | 34 ++++++++++++ .../class-wc-shortcode-change-password.php | 2 +- .../class-wc-shortcode-checkout.php | 2 +- .../class-wc-shortcode-edit-address.php | 2 +- .../class-wc-shortcode-lost-password.php | 2 +- .../class-wc-shortcode-my-account.php | 2 +- .../class-wc-shortcode-order-tracking.php | 2 +- .../class-wc-shortcode-view-order.php | 2 +- woocommerce.php | 54 ++++++++----------- 10 files changed, 72 insertions(+), 46 deletions(-) create mode 100644 classes/helpers/class-wc-shortcode-helper.php diff --git a/classes/class-wc-shortcodes.php b/classes/class-wc-shortcodes.php index a2a62597c4a..b3c00ceab54 100644 --- a/classes/class-wc-shortcodes.php +++ b/classes/class-wc-shortcodes.php @@ -49,7 +49,7 @@ class WC_Shortcodes { */ public function cart( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( 'WC_Shortcode_Cart', 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( 'WC_Shortcode_Cart', 'output' ), $atts ); } /** @@ -61,7 +61,7 @@ class WC_Shortcodes { */ public function checkout( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( 'WC_Shortcode_Checkout', 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( 'WC_Shortcode_Checkout', 'output' ), $atts ); } /** @@ -73,7 +73,7 @@ class WC_Shortcodes { */ public function order_tracking( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( 'WC_Shortcode_Order_Tracking', 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( 'WC_Shortcode_Order_Tracking', 'output' ), $atts ); } /** @@ -85,7 +85,7 @@ class WC_Shortcodes { */ public function my_account( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( 'WC_Shortcode_My_Account', 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( 'WC_Shortcode_My_Account', 'output' ), $atts ); } /** @@ -97,7 +97,7 @@ class WC_Shortcodes { */ public function edit_address( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( 'WC_Shortcode_Edit_Address', 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( 'WC_Shortcode_Edit_Address', 'output' ), $atts ); } @@ -110,7 +110,7 @@ class WC_Shortcodes { */ public function change_password( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( 'WC_Shortcode_Change_Password', 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( 'WC_Shortcode_Change_Password', 'output' ), $atts ); } /** @@ -122,7 +122,7 @@ class WC_Shortcodes { */ public function lost_password( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( 'WC_Shortcode_Lost_Password', 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( 'WC_Shortcode_Lost_Password', 'output' ), $atts ); } /** @@ -134,7 +134,7 @@ class WC_Shortcodes { */ public function view_order( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( 'WC_Shortcode_View_Order', 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( 'WC_Shortcode_View_Order', 'output' ), $atts ); } /** diff --git a/classes/helpers/class-wc-shortcode-helper.php b/classes/helpers/class-wc-shortcode-helper.php new file mode 100644 index 00000000000..840601136ef --- /dev/null +++ b/classes/helpers/class-wc-shortcode-helper.php @@ -0,0 +1,34 @@ + 'woocommerce', + 'before' => null, + 'after' => null + ) + ){ + ob_start(); + + $before = empty( $wrapper['before'] ) ? '
' : $wrapper['before']; + $after = empty( $wrapper['after'] ) ? '
' : $wrapper['after']; + + echo $before; + call_user_func( $function, $atts ); + echo $after; + + return ob_get_clean(); + } +} \ No newline at end of file diff --git a/classes/shortcodes/class-wc-shortcode-change-password.php b/classes/shortcodes/class-wc-shortcode-change-password.php index d30412652cd..2aef8a7c391 100644 --- a/classes/shortcodes/class-wc-shortcode-change-password.php +++ b/classes/shortcodes/class-wc-shortcode-change-password.php @@ -18,7 +18,7 @@ class WC_Shortcode_Change_Password { */ public static function get( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); } /** diff --git a/classes/shortcodes/class-wc-shortcode-checkout.php b/classes/shortcodes/class-wc-shortcode-checkout.php index a0023e82a5c..b4d45c65850 100644 --- a/classes/shortcodes/class-wc-shortcode-checkout.php +++ b/classes/shortcodes/class-wc-shortcode-checkout.php @@ -21,7 +21,7 @@ class WC_Shortcode_Checkout { */ public static function get( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); } /** diff --git a/classes/shortcodes/class-wc-shortcode-edit-address.php b/classes/shortcodes/class-wc-shortcode-edit-address.php index 7290a75b5b2..12d8627cf44 100644 --- a/classes/shortcodes/class-wc-shortcode-edit-address.php +++ b/classes/shortcodes/class-wc-shortcode-edit-address.php @@ -23,7 +23,7 @@ class WC_Shortcode_Edit_Address { */ public static function get( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); } /** diff --git a/classes/shortcodes/class-wc-shortcode-lost-password.php b/classes/shortcodes/class-wc-shortcode-lost-password.php index f96e4227b1a..ca56751901b 100644 --- a/classes/shortcodes/class-wc-shortcode-lost-password.php +++ b/classes/shortcodes/class-wc-shortcode-lost-password.php @@ -21,7 +21,7 @@ class WC_Shortcode_Lost_Password { */ public static function get( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); } /** diff --git a/classes/shortcodes/class-wc-shortcode-my-account.php b/classes/shortcodes/class-wc-shortcode-my-account.php index 1f26b2de4ba..279b3b596a6 100644 --- a/classes/shortcodes/class-wc-shortcode-my-account.php +++ b/classes/shortcodes/class-wc-shortcode-my-account.php @@ -21,7 +21,7 @@ class WC_Shortcode_My_Account { */ public static function get( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); } /** diff --git a/classes/shortcodes/class-wc-shortcode-order-tracking.php b/classes/shortcodes/class-wc-shortcode-order-tracking.php index 46830ee8bab..a6c6ae74fbc 100644 --- a/classes/shortcodes/class-wc-shortcode-order-tracking.php +++ b/classes/shortcodes/class-wc-shortcode-order-tracking.php @@ -21,7 +21,7 @@ class WC_Shortcode_Order_Tracking { */ public static function get( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); } /** diff --git a/classes/shortcodes/class-wc-shortcode-view-order.php b/classes/shortcodes/class-wc-shortcode-view-order.php index e838f36d374..78abf333da2 100644 --- a/classes/shortcodes/class-wc-shortcode-view-order.php +++ b/classes/shortcodes/class-wc-shortcode-view-order.php @@ -19,7 +19,7 @@ class WC_Shortcode_View_Order { */ public static function get( $atts ) { global $woocommerce; - return $woocommerce->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); + return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); } /** diff --git a/woocommerce.php b/woocommerce.php index 47fd3f63e40..59a580edeca 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -1370,37 +1370,6 @@ class Woocommerce { return false; } - /** Shortcode Helpers *********************************************************/ - - /** - * Shortcode Wrapper - * - * @access public - * @param mixed $function - * @param array $atts (default: array()) - * @return string - */ - public function shortcode_wrapper( - $function, - $atts = array(), - $wrapper = array( - 'class' => 'woocommerce', - 'before' => null, - 'after' => null - ) - ){ - ob_start(); - - $before = empty( $wrapper['before'] ) ? '
' : $wrapper['before']; - $after = empty( $wrapper['after'] ) ? '
' : $wrapper['after']; - - echo $before; - call_user_func( $function, $atts ); - echo $after; - - return ob_get_clean(); - } - /** Body Classes **********************************************************/ /** @@ -1570,6 +1539,29 @@ class Woocommerce { $helper = $this->get_helper( 'nonce' ); return $helper->nonce_url( $action, $url = '' ); } + + /** + * Shortcode Wrapper + * + * @deprecated 2.1.0 Access via the helpers + * @access public + * @param mixed $function + * @param array $atts (default: array()) + * @return string + */ + public function shortcode_wrapper( + $function, + $atts = array(), + $wrapper = array( + 'class' => 'woocommerce', + 'before' => null, + 'after' => null + ) + ) { + _deprecated_function( 'Woocommerce->shortcode_wrapper', '2.1', 'WC_Shortcode_Helper->shortcode_wrapper' ); + $helper = $this->get_helper( 'shortcode' ); + return $helper->shortcode_wrapper( $function, $atts, $wrapper ); + } } /** From e2b850bddb910091bf64f5684b4d5f6b5f6bf6b5 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Wed, 5 Jun 2013 15:54:13 +0200 Subject: [PATCH 06/13] Added verify_nonce method to nonce helper #3282 --- classes/class-wc-checkout.php | 2 +- classes/helpers/class-wc-nonce-helper.php | 28 +++++++++++ .../shortcodes/class-wc-shortcode-cart.php | 2 +- .../class-wc-shortcode-lost-password.php | 4 +- .../class-wc-shortcode-order-tracking.php | 2 +- woocommerce-functions.php | 20 ++++---- woocommerce.php | 47 ++++++++----------- 7 files changed, 62 insertions(+), 43 deletions(-) diff --git a/classes/class-wc-checkout.php b/classes/class-wc-checkout.php index f02390eba74..98a7cef8da2 100644 --- a/classes/class-wc-checkout.php +++ b/classes/class-wc-checkout.php @@ -374,7 +374,7 @@ class WC_Checkout { public function process_checkout() { global $wpdb, $woocommerce, $current_user; - $woocommerce->verify_nonce( 'process_checkout' ); + $woocommerce->get_helper( 'nonce' )->verify_nonce( 'process_checkout' ); if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) define( 'WOOCOMMERCE_CHECKOUT', true ); diff --git a/classes/helpers/class-wc-nonce-helper.php b/classes/helpers/class-wc-nonce-helper.php index 6ee2a0ccc72..be5e990f198 100644 --- a/classes/helpers/class-wc-nonce-helper.php +++ b/classes/helpers/class-wc-nonce-helper.php @@ -28,4 +28,32 @@ class WC_Nonce_Helper extends WC_Helper { public function nonce_url( $action, $url = '' ) { return add_query_arg( '_n', wp_create_nonce( 'woocommerce-' . $action ), $url ); } + + /** + * Check a nonce and sets woocommerce error in case it is invalid. + * + * To fail silently, set the error_message to an empty string + * + * @access public + * @param string $name the nonce name + * @param string $action then nonce action + * @param string $method the http request method _POST, _GET or _REQUEST + * @param string $error_message custom error message, or false for default message, or an empty string to fail silently + * @return bool + */ + public function verify_nonce( $action, $method='_POST', $error_message = false ) { + + $name = '_n'; + $action = 'woocommerce-' . $action; + + if ( $error_message === false ) $error_message = __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ); + + if ( ! in_array( $method, array( '_GET', '_POST', '_REQUEST' ) ) ) $method = '_POST'; + + if ( isset($_REQUEST[$name] ) && wp_verify_nonce( $_REQUEST[$name], $action ) ) return true; + + if ( $error_message ) $this->add_error( $error_message ); + + return false; + } } \ No newline at end of file diff --git a/classes/shortcodes/class-wc-shortcode-cart.php b/classes/shortcodes/class-wc-shortcode-cart.php index 5147ce0b801..74c36705b0f 100644 --- a/classes/shortcodes/class-wc-shortcode-cart.php +++ b/classes/shortcodes/class-wc-shortcode-cart.php @@ -38,7 +38,7 @@ class WC_Shortcode_Cart { $woocommerce->cart->remove_coupons( $_GET['remove_discounts'] ); // Update Shipping - } elseif ( ! empty( $_POST['calc_shipping'] ) && $woocommerce->verify_nonce('cart') ) { + } elseif ( ! empty( $_POST['calc_shipping'] ) && $woocommerce->get_helper( 'nonce' )->verify_nonce('cart') ) { $validation = $woocommerce->validation(); diff --git a/classes/shortcodes/class-wc-shortcode-lost-password.php b/classes/shortcodes/class-wc-shortcode-lost-password.php index ca56751901b..d6d3b2317a0 100644 --- a/classes/shortcodes/class-wc-shortcode-lost-password.php +++ b/classes/shortcodes/class-wc-shortcode-lost-password.php @@ -42,7 +42,7 @@ class WC_Shortcode_Lost_Password { // process lost password form if( isset( $_POST['user_login'] ) ) { - $woocommerce->verify_nonce( 'lost_password' ); + $woocommerce->get_helper( 'nonce' )->verify_nonce( 'lost_password' ); self::retrieve_password(); } @@ -72,7 +72,7 @@ class WC_Shortcode_Lost_Password { $args['key'] = esc_attr( $_POST['reset_key'] ); $args['login'] = esc_attr( $_POST['reset_login'] ); - $woocommerce->verify_nonce( 'reset_password' ); + $woocommerce->get_helper( 'nonce' )->verify_nonce( 'reset_password' ); if( empty( $_POST['password_1'] ) || empty( $_POST['password_2'] ) ) { $woocommerce->add_error( __( 'Please enter your password.', 'woocommerce' ) ); diff --git a/classes/shortcodes/class-wc-shortcode-order-tracking.php b/classes/shortcodes/class-wc-shortcode-order-tracking.php index a6c6ae74fbc..0f6c82fea91 100644 --- a/classes/shortcodes/class-wc-shortcode-order-tracking.php +++ b/classes/shortcodes/class-wc-shortcode-order-tracking.php @@ -41,7 +41,7 @@ class WC_Shortcode_Order_Tracking { if ( ! empty( $_REQUEST['orderid'] ) ) { - $woocommerce->verify_nonce( 'order_tracking' ); + $woocommerce->get_helper( 'nonce' )->verify_nonce( 'order_tracking' ); $order_id = empty( $_REQUEST['orderid'] ) ? 0 : esc_attr( $_REQUEST['orderid'] ); $order_email = empty( $_REQUEST['order_email'] ) ? '' : esc_attr( $_REQUEST['order_email']) ; diff --git a/woocommerce-functions.php b/woocommerce-functions.php index d81e7685ca7..a389b497247 100644 --- a/woocommerce-functions.php +++ b/woocommerce-functions.php @@ -213,7 +213,7 @@ function woocommerce_update_cart_action() { global $woocommerce; // Remove from cart - if ( isset($_GET['remove_item']) && $_GET['remove_item'] && $woocommerce->verify_nonce('cart', '_GET')) { + if ( isset($_GET['remove_item']) && $_GET['remove_item'] && $woocommerce->get_helper( 'nonce' )->verify_nonce('cart', '_GET')) { $woocommerce->cart->set_quantity( $_GET['remove_item'], 0 ); @@ -224,7 +224,7 @@ function woocommerce_update_cart_action() { exit; // Update Cart - } elseif ( ( ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) && $woocommerce->verify_nonce('cart')) { + } elseif ( ( ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) && $woocommerce->get_helper( 'nonce' )->verify_nonce('cart')) { $cart_totals = isset( $_POST['cart'] ) ? $_POST['cart'] : ''; @@ -569,7 +569,7 @@ function woocommerce_checkout_action() { function woocommerce_pay_action() { global $woocommerce, $wp; - if ( isset( $_POST['woocommerce_pay'] ) && $woocommerce->verify_nonce( 'pay' ) ) { + if ( isset( $_POST['woocommerce_pay'] ) && $woocommerce->get_helper( 'nonce' )->verify_nonce( 'pay' ) ) { ob_start(); @@ -646,7 +646,7 @@ function woocommerce_process_login() { if ( ! empty( $_POST['login'] ) ) { - $woocommerce->verify_nonce( 'login' ); + $woocommerce->get_helper( 'nonce' )->verify_nonce( 'login' ); try { $creds = array(); @@ -705,7 +705,7 @@ function woocommerce_process_registration() { if ( ! empty( $_POST['register'] ) ) { - $woocommerce->verify_nonce( 'register' ); + $woocommerce->get_helper( 'nonce' )->verify_nonce( 'register' ); // Get fields $user_email = isset( $_POST['email'] ) ? trim( $_POST['email'] ) : ''; @@ -822,7 +822,7 @@ function woocommerce_order_again() { if ( ! isset( $_GET['order_again'] ) || ! is_user_logged_in() || get_option('woocommerce_allow_customers_to_reorder') == 'no' ) return; // Nonce security check - if ( ! $woocommerce->verify_nonce( 'order_again', '_GET' ) ) return; + if ( ! $woocommerce->get_helper( 'nonce' )->verify_nonce( 'order_again', '_GET' ) ) return; // Clear current cart $woocommerce->cart->empty_cart(); @@ -883,7 +883,7 @@ function woocommerce_cancel_order() { $order = new WC_Order( $order_id ); - if ( $order->id == $order_id && $order->order_key == $order_key && in_array( $order->status, array( 'pending', 'failed' ) ) && $woocommerce->verify_nonce( 'cancel_order', '_GET' ) ) : + if ( $order->id == $order_id && $order->order_key == $order_key && in_array( $order->status, array( 'pending', 'failed' ) ) && $woocommerce->get_helper( 'nonce' )->verify_nonce( 'cancel_order', '_GET' ) ) : // Cancel the order + restore stock $order->cancel_order( __('Order cancelled by customer.', 'woocommerce' ) ); @@ -1257,7 +1257,7 @@ function woocommerce_check_comment_rating( $comment_data ) { global $woocommerce; // If posting a comment (not trackback etc) and not logged in - if ( isset( $_POST['rating'] ) && ! $woocommerce->verify_nonce('comment_rating') ) + if ( isset( $_POST['rating'] ) && ! $woocommerce->get_helper( 'nonce' )->verify_nonce('comment_rating') ) wp_die( __( 'You have taken too long. Please go back and refresh the page.', 'woocommerce' ) ); elseif ( isset( $_POST['rating'] ) && empty( $_POST['rating'] ) && $comment_data['comment_type'] == '' && get_option('woocommerce_review_rating_required') == 'yes' ) { @@ -1535,7 +1535,7 @@ function woocommerce_save_password() { if ( empty( $_POST[ 'action' ] ) || ( 'change_password' !== $_POST[ 'action' ] ) ) return; - $woocommerce->verify_nonce( 'change_password' ); + $woocommerce->get_helper( 'nonce' )->verify_nonce( 'change_password' ); $update = true; $errors = new WP_Error(); @@ -1595,7 +1595,7 @@ function woocommerce_save_address() { if ( empty( $_POST[ 'action' ] ) || ( 'edit_address' !== $_POST[ 'action' ] ) ) return; - $woocommerce->verify_nonce( 'edit_address' ); + $woocommerce->get_helper( 'nonce' )->verify_nonce( 'edit_address' ); $validation = $woocommerce->validation(); diff --git a/woocommerce.php b/woocommerce.php index 59a580edeca..32edd534baf 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -1342,34 +1342,6 @@ class Woocommerce { if ( isset( $types[$type] ) ) return $types[$type]; } - /** - * Check a nonce and sets woocommerce error in case it is invalid. - * - * To fail silently, set the error_message to an empty string - * - * @access public - * @param string $name the nonce name - * @param string $action then nonce action - * @param string $method the http request method _POST, _GET or _REQUEST - * @param string $error_message custom error message, or false for default message, or an empty string to fail silently - * @return bool - */ - public function verify_nonce( $action, $method='_POST', $error_message = false ) { - - $name = '_n'; - $action = 'woocommerce-' . $action; - - if ( $error_message === false ) $error_message = __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ); - - if ( ! in_array( $method, array( '_GET', '_POST', '_REQUEST' ) ) ) $method = '_POST'; - - if ( isset($_REQUEST[$name] ) && wp_verify_nonce( $_REQUEST[$name], $action ) ) return true; - - if ( $error_message ) $this->add_error( $error_message ); - - return false; - } - /** Body Classes **********************************************************/ /** @@ -1540,6 +1512,25 @@ class Woocommerce { return $helper->nonce_url( $action, $url = '' ); } + /** + * Check a nonce and sets woocommerce error in case it is invalid. + * + * To fail silently, set the error_message to an empty string + * + * @deprecated 2.1.0 Access via the helpers + * @access public + * @param string $name the nonce name + * @param string $action then nonce action + * @param string $method the http request method _POST, _GET or _REQUEST + * @param string $error_message custom error message, or false for default message, or an empty string to fail silently + * @return bool + */ + public function verify_nonce( $action, $method='_POST', $error_message = false ) { + _deprecated_function( 'Woocommerce->verify_nonce', '2.1', 'WC_Nonce_Helper->verify_nonce' ); + $helper = $this->get_helper( 'nonce' ); + return $helper->verify_nonce( $action, $method, $error_message ); + } + /** * Shortcode Wrapper * From 4b2edd8616045648b10095b1f83487fc6416fd0c Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Wed, 5 Jun 2013 15:57:09 +0200 Subject: [PATCH 07/13] Move actions for inline js to helpers #3282 --- woocommerce.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/woocommerce.php b/woocommerce.php index 32edd534baf..4449e65385a 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -505,7 +505,7 @@ class Woocommerce { add_action( 'wp_head', array( $this, 'wp_head' ) ); add_filter( 'body_class', array( $this, 'output_body_class' ) ); add_filter( 'post_class', array( $this, 'post_class' ), 20, 3 ); - add_action( 'wp_footer', array( $this, 'output_inline_js' ), 25 ); + add_action( 'wp_footer', array( $this->get_helper( 'inline-javascript' ), 'output_inline_js' ), 25 ); // HTTPS urls with SSL on $filters = array( 'post_thumbnail_html', 'widget_text', 'wp_get_attachment_url', 'wp_get_attachment_image_attributes', 'wp_get_attachment_url', 'option_stylesheet_url', 'option_template_url', 'script_loader_src', 'style_loader_src', 'template_directory_uri', 'stylesheet_directory_uri', 'site_url' ); @@ -516,7 +516,7 @@ class Woocommerce { // Actions add_action( 'the_post', array( $this, 'setup_product_data' ) ); - add_action( 'admin_footer', array( $this, 'output_inline_js' ), 25 ); + add_action( 'admin_footer', array( $this->get_helper( 'inline-javascript' ), 'output_inline_js' ), 25 ); // Email Actions $email_actions = array( 'woocommerce_low_stock', 'woocommerce_no_stock', 'woocommerce_product_on_backorder', 'woocommerce_order_status_pending_to_processing', 'woocommerce_order_status_pending_to_completed', 'woocommerce_order_status_pending_to_on-hold', 'woocommerce_order_status_failed_to_processing', 'woocommerce_order_status_failed_to_completed', 'woocommerce_order_status_completed', 'woocommerce_new_customer_note' ); From 74fcf1a1d4382e554cd0f81ff15cc02912d601c9 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Wed, 5 Jun 2013 16:14:25 +0200 Subject: [PATCH 08/13] Abstracted attribute helper to class #3282 --- .../writepanels/variation-admin-html.php | 2 +- .../writepanel-product-type-variable.php | 2 +- .../writepanels/writepanel-product_data.php | 6 +- admin/woocommerce-admin-attributes.php | 16 +- admin/woocommerce-admin-dashboard.php | 2 +- classes/class-wc-cart.php | 4 +- classes/class-wc-order-item-meta.php | 4 +- classes/class-wc-post-types.php | 4 +- classes/helpers/class-wc-attribute-helper.php | 96 +++++++++++ .../widgets/class-wc-widget-layered-nav.php | 8 +- .../single-product/add-to-cart/variable.php | 4 +- .../single-product/product-attributes.php | 2 +- woocommerce-core-functions.php | 4 +- woocommerce-functions.php | 4 +- woocommerce.php | 160 ++++++++---------- 15 files changed, 194 insertions(+), 124 deletions(-) create mode 100644 classes/helpers/class-wc-attribute-helper.php diff --git a/admin/post-types/writepanels/variation-admin-html.php b/admin/post-types/writepanels/variation-admin-html.php index 09275816d65..2474b52fcd5 100644 --- a/admin/post-types/writepanels/variation-admin-html.php +++ b/admin/post-types/writepanels/variation-admin-html.php @@ -17,7 +17,7 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly $variation_selected_value = isset( $variation_data[ 'attribute_' . sanitize_title( $attribute['name'] ) ][0] ) ? $variation_data[ 'attribute_' . sanitize_title( $attribute['name'] ) ][0] : ''; // Name will be something like attribute_pa_color - echo ''; // Get terms for attribute taxonomy or value if its a custom attribute if ( $attribute['is_taxonomy'] ) { diff --git a/admin/post-types/writepanels/writepanel-product-type-variable.php b/admin/post-types/writepanels/writepanel-product-type-variable.php index 89479f09cd7..5337a8cb58a 100755 --- a/admin/post-types/writepanels/writepanel-product-type-variable.php +++ b/admin/post-types/writepanels/writepanel-product-type-variable.php @@ -211,7 +211,7 @@ function variable_product_type_options() { $variation_selected_value = isset( $default_attributes[ sanitize_title( $attribute['name'] ) ] ) ? $default_attributes[ sanitize_title( $attribute['name'] ) ] : ''; // Name will be something like attribute_pa_color - echo ''; // Get terms for attribute taxonomy or value if its a custom attribute if ( $attribute['is_taxonomy'] ) { diff --git a/admin/post-types/writepanels/writepanel-product_data.php b/admin/post-types/writepanels/writepanel-product_data.php index 7039fd87b4f..276e483cc22 100644 --- a/admin/post-types/writepanels/writepanel-product_data.php +++ b/admin/post-types/writepanels/writepanel-product_data.php @@ -333,7 +333,7 @@ function woocommerce_product_data_box() { get_attribute_taxonomies(); + $attribute_taxonomies = $woocommerce->get_helper( 'attribute' )->get_attribute_taxonomies(); // Product attributes - taxonomies and custom, ordered, with visibility and variation attributes set $attributes = maybe_unserialize( get_post_meta( $thepostid, '_product_attributes', true ) ); @@ -345,7 +345,7 @@ function woocommerce_product_data_box() { foreach ( $attribute_taxonomies as $tax ) { $i++; // Get name of taxonomy we're now outputting (pa_xxx) - $attribute_taxonomy_name = $woocommerce->attribute_taxonomy_name( $tax->attribute_name ); + $attribute_taxonomy_name = $woocommerce->get_helper( 'attribute' )->attribute_taxonomy_name( $tax->attribute_name ); // Ensure it exists if ( ! taxonomy_exists( $attribute_taxonomy_name ) ) @@ -504,7 +504,7 @@ function woocommerce_product_data_box() { attribute_taxonomy_name( $tax->attribute_name ); + $attribute_taxonomy_name = $woocommerce->get_helper( 'attribute' )->attribute_taxonomy_name( $tax->attribute_name ); $label = $tax->attribute_label ? $tax->attribute_label : $tax->attribute_name; echo ''; } diff --git a/admin/woocommerce-admin-attributes.php b/admin/woocommerce-admin-attributes.php index f7a10a073cd..66b8b8cf559 100644 --- a/admin/woocommerce-admin-attributes.php +++ b/admin/woocommerce-admin-attributes.php @@ -80,7 +80,7 @@ function woocommerce_attributes() { } elseif ( in_array( $attribute_name, $reserved_terms ) ) { $error = sprintf( __( 'Slug ā€œ%sā€ is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) ); } else { - $taxonomy_exists = taxonomy_exists( $woocommerce->attribute_taxonomy_name( $attribute_name ) ); + $taxonomy_exists = taxonomy_exists( $woocommerce->get_helper( 'attribute' )->attribute_taxonomy_name( $attribute_name ) ); if ( 'add' === $action && $taxonomy_exists ) { $error = sprintf( __( 'Slug ā€œ%sā€ is already in use. Change it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) ); @@ -130,7 +130,7 @@ function woocommerce_attributes() { // Update taxonomies in the wp term taxonomy table $wpdb->update( $wpdb->term_taxonomy, - array( 'taxonomy' => $woocommerce->attribute_taxonomy_name( $attribute_name ) ), + array( 'taxonomy' => $woocommerce->get_helper( 'attribute' )->attribute_taxonomy_name( $attribute_name ) ), array( 'taxonomy' => 'pa_' . $old_attribute_name ) ); @@ -174,7 +174,7 @@ function woocommerce_attributes() { if ( $attribute_name && $wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = $attribute_id" ) ) { - $taxonomy = $woocommerce->attribute_taxonomy_name( $attribute_name ); + $taxonomy = $woocommerce->get_helper( 'attribute' )->attribute_taxonomy_name( $attribute_name ); if ( taxonomy_exists( $taxonomy ) ) { $terms = get_terms( $taxonomy, 'orderby=name&hide_empty=0' ); @@ -313,12 +313,12 @@ function woocommerce_add_attribute() { get_attribute_taxonomies(); + $attribute_taxonomies = $woocommerce->get_helper( 'attribute' )->get_attribute_taxonomies(); if ( $attribute_taxonomies ) : foreach ($attribute_taxonomies as $tax) : ?> - attribute_label ); ?> + attribute_label ); ?>
|
@@ -338,9 +338,9 @@ function woocommerce_add_attribute() { } ?> attribute_taxonomy_name($tax->attribute_name))) : + if (taxonomy_exists($woocommerce->get_helper( 'attribute' )->attribute_taxonomy_name($tax->attribute_name))) : $terms_array = array(); - $terms = get_terms( $woocommerce->attribute_taxonomy_name($tax->attribute_name), 'orderby=name&hide_empty=0' ); + $terms = get_terms( $woocommerce->get_helper( 'attribute' )->attribute_taxonomy_name($tax->attribute_name), 'orderby=name&hide_empty=0' ); if ($terms) : foreach ($terms as $term) : $terms_array[] = $term->name; @@ -353,7 +353,7 @@ function woocommerce_add_attribute() { echo ''; endif; ?> - + get_attribute_taxonomies() ); + $product_attr_count = count( $woocommerce->get_helper( 'attribute' )->get_attribute_taxonomies() ); $pending_count = get_term_by( 'slug', 'pending', 'shop_order_status' )->count; $completed_count = get_term_by( 'slug', 'completed', 'shop_order_status' )->count; diff --git a/classes/class-wc-cart.php b/classes/class-wc-cart.php index a581113edad..8b86b9a9571 100644 --- a/classes/class-wc-cart.php +++ b/classes/class-wc-cart.php @@ -576,9 +576,9 @@ class WC_Cart { } if ( $flat ) - $variation_list[] = $woocommerce->attribute_label( str_replace( 'attribute_', '', $name ) ) . ': ' . $value; + $variation_list[] = $woocommerce->get_helper( 'attribute' )->attribute_label( str_replace( 'attribute_', '', $name ) ) . ': ' . $value; else - $variation_list[] = '
' . $woocommerce->attribute_label( str_replace( 'attribute_', '', $name ) ) . ':
' . $value . '
'; + $variation_list[] = '
' . $woocommerce->get_helper( 'attribute' )->attribute_label( str_replace( 'attribute_', '', $name ) ) . ':
' . $value . '
'; } diff --git a/classes/class-wc-order-item-meta.php b/classes/class-wc-order-item-meta.php index 96e37788782..91506cb9c42 100644 --- a/classes/class-wc-order-item-meta.php +++ b/classes/class-wc-order-item-meta.php @@ -61,9 +61,9 @@ class WC_Order_Item_Meta { } if ( $flat ) - $meta_list[] = esc_attr( $woocommerce->attribute_label( str_replace( 'attribute_', '', $meta_key ) ) . ': ' . apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ) ); + $meta_list[] = esc_attr( $woocommerce->get_helper( 'attribute' )->attribute_label( str_replace( 'attribute_', '', $meta_key ) ) . ': ' . apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ) ); else - $meta_list[] = '
' . wp_kses_post( $woocommerce->attribute_label( str_replace( 'attribute_', '', $meta_key ) ) ) . ':
' . wp_kses_post( apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ) ) . '
'; + $meta_list[] = '
' . wp_kses_post( $woocommerce->get_helper( 'attribute' )->attribute_label( str_replace( 'attribute_', '', $meta_key ) ) ) . ':
' . wp_kses_post( apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ) ) . '
'; } } diff --git a/classes/class-wc-post-types.php b/classes/class-wc-post-types.php index fb363e4fa6d..a53e4d74f7e 100644 --- a/classes/class-wc-post-types.php +++ b/classes/class-wc-post-types.php @@ -168,9 +168,9 @@ class WC_Post_types { $wc_product_attributes = array(); - if ( $attribute_taxonomies = $woocommerce->get_attribute_taxonomies() ) { + if ( $attribute_taxonomies = $woocommerce->get_helper( 'attribute' )->get_attribute_taxonomies() ) { foreach ( $attribute_taxonomies as $tax ) { - if ( $name = $woocommerce->attribute_taxonomy_name( $tax->attribute_name ) ) { + if ( $name = $woocommerce->get_helper( 'attribute' )->attribute_taxonomy_name( $tax->attribute_name ) ) { $label = ! empty( $tax->attribute_label ) ? $tax->attribute_label : $tax->attribute_name; diff --git a/classes/helpers/class-wc-attribute-helper.php b/classes/helpers/class-wc-attribute-helper.php new file mode 100644 index 00000000000..e23984a9ee4 --- /dev/null +++ b/classes/helpers/class-wc-attribute-helper.php @@ -0,0 +1,96 @@ +get_results( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies" ); + + set_transient( $transient_name, $attribute_taxonomies ); + } + + return apply_filters( 'woocommerce_attribute_taxonomies', $attribute_taxonomies ); + } + + /** + * Get a product attributes name. + * + * @access public + * @param mixed $name + * @return string + */ + public function attribute_taxonomy_name( $name ) { + return 'pa_' . woocommerce_sanitize_taxonomy_name( $name ); + } + + /** + * Get a product attributes label. + * + * @access public + * @param mixed $name + * @return string + */ + public function attribute_label( $name ) { + global $wpdb; + + if ( taxonomy_is_product_attribute( $name ) ) { + $name = woocommerce_sanitize_taxonomy_name( str_replace( 'pa_', '', $name ) ); + + $label = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_label FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) ); + + if ( ! $label ) + $label = ucfirst( $name ); + } else { + $label = $name; + } + + return apply_filters( 'woocommerce_attribute_label', $label, $name ); + } + + /** + * Get a product attributes orderby setting. + * + * @access public + * @param mixed $name + * @return string + */ + public function attribute_orderby( $name ) { + global $wpdb; + + $name = str_replace( 'pa_', '', sanitize_title( $name ) ); + + $orderby = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_orderby FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) ); + + return apply_filters( 'woocommerce_attribute_orderby', $orderby, $name ); + } + + /** + * Get an array of product attribute taxonomies. + * + * @access public + * @return array + */ + public function get_attribute_taxonomy_names() { + $taxonomy_names = array(); + $attribute_taxonomies = $this->get_attribute_taxonomies(); + if ( $attribute_taxonomies ) { + foreach ( $attribute_taxonomies as $tax ) { + $taxonomy_names[] = $this->attribute_taxonomy_name( $tax->attribute_name ); + } + } + return $taxonomy_names; + } +} \ No newline at end of file diff --git a/classes/widgets/class-wc-widget-layered-nav.php b/classes/widgets/class-wc-widget-layered-nav.php index 4e409451a0c..56155795571 100644 --- a/classes/widgets/class-wc-widget-layered-nav.php +++ b/classes/widgets/class-wc-widget-layered-nav.php @@ -25,10 +25,10 @@ class WC_Widget_Layered_Nav extends WC_Widget { $this->widget_name = __( 'WooCommerce Layered Nav', 'woocommerce' ); $attribute_array = array(); - $attribute_taxonomies = $woocommerce->get_attribute_taxonomies(); + $attribute_taxonomies = $woocommerce->get_helper( 'attribute' )->get_attribute_taxonomies(); if ( $attribute_taxonomies ) foreach ( $attribute_taxonomies as $tax ) - if ( taxonomy_exists( $woocommerce->attribute_taxonomy_name( $tax->attribute_name ) ) ) + if ( taxonomy_exists( $woocommerce->get_helper( 'attribute' )->attribute_taxonomy_name( $tax->attribute_name ) ) ) $attribute_array[ $tax->attribute_name ] = $tax->attribute_name; $this->settings = array( @@ -86,7 +86,7 @@ class WC_Widget_Layered_Nav extends WC_Widget { $current_tax = $_attributes_array && is_tax( $_attributes_array ) ? get_queried_object()->taxonomy : ''; $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); - $taxonomy = $woocommerce->attribute_taxonomy_name($instance['attribute']); + $taxonomy = $woocommerce->get_helper( 'attribute' )->attribute_taxonomy_name($instance['attribute']); $query_type = isset( $instance['query_type'] ) ? $instance['query_type'] : 'and'; $display_type = isset( $instance['display_type'] ) ? $instance['display_type'] : 'list'; @@ -123,7 +123,7 @@ class WC_Widget_Layered_Nav extends WC_Widget { echo ' attribute_orderby( $name ); + $orderby = $woocommerce->get_helper( 'attribute' )->attribute_orderby( $name ); switch ( $orderby ) { case 'name' : diff --git a/templates/single-product/product-attributes.php b/templates/single-product/product-attributes.php index d5199abe1d7..7323b5f77ef 100644 --- a/templates/single-product/product-attributes.php +++ b/templates/single-product/product-attributes.php @@ -49,7 +49,7 @@ if ( empty( $attributes ) && ( ! $product->enable_dimensions_display() || ( ! $p ?> - attribute_label( $attribute['name'] ); ?> + get_helper( 'attribute' )->attribute_label( $attribute['name'] ); ?> attribute_label(str_replace('attribute_', '', $name)).': '.$value; + $variation_list[] = $woocommerce->get_helper( 'attribute' )->attribute_label(str_replace('attribute_', '', $name)).': '.$value; else - $variation_list[] = '
'.$woocommerce->attribute_label(str_replace('attribute_', '', $name)).':
'.$value.'
'; + $variation_list[] = '
'.$woocommerce->get_helper( 'attribute' )->attribute_label(str_replace('attribute_', '', $name)).':
'.$value.'
'; } if ( $flat ) diff --git a/woocommerce-functions.php b/woocommerce-functions.php index a389b497247..ad459e90ad5 100644 --- a/woocommerce-functions.php +++ b/woocommerce-functions.php @@ -1329,12 +1329,12 @@ function woocommerce_layered_nav_init( ) { $_chosen_attributes = $_attributes_array = array(); - $attribute_taxonomies = $woocommerce->get_attribute_taxonomies(); + $attribute_taxonomies = $woocommerce->get_helper( 'attribute' )->get_attribute_taxonomies(); if ( $attribute_taxonomies ) { foreach ( $attribute_taxonomies as $tax ) { $attribute = sanitize_title( $tax->attribute_name ); - $taxonomy = $woocommerce->attribute_taxonomy_name( $attribute ); + $taxonomy = $woocommerce->get_helper( 'attribute' )->attribute_taxonomy_name( $attribute ); // create an array of product attribute taxonomies $_attributes_array[] = $taxonomy; diff --git a/woocommerce.php b/woocommerce.php index 4449e65385a..a0fe1f8e501 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -1216,99 +1216,6 @@ class Woocommerce { return apply_filters( 'woocommerce_redirect', $location ); } - /** Attribute Helpers ****************************************************************/ - - /** - * Get attribute taxonomies. - * - * @access public - * @return object - */ - public function get_attribute_taxonomies() { - - $transient_name = 'wc_attribute_taxonomies'; - - if ( false === ( $attribute_taxonomies = get_transient( $transient_name ) ) ) { - - global $wpdb; - - $attribute_taxonomies = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies" ); - - set_transient( $transient_name, $attribute_taxonomies ); - } - - return apply_filters( 'woocommerce_attribute_taxonomies', $attribute_taxonomies ); - } - - /** - * Get a product attributes name. - * - * @access public - * @param mixed $name - * @return string - */ - public function attribute_taxonomy_name( $name ) { - return 'pa_' . woocommerce_sanitize_taxonomy_name( $name ); - } - - /** - * Get a product attributes label. - * - * @access public - * @param mixed $name - * @return string - */ - public function attribute_label( $name ) { - global $wpdb; - - if ( taxonomy_is_product_attribute( $name ) ) { - $name = woocommerce_sanitize_taxonomy_name( str_replace( 'pa_', '', $name ) ); - - $label = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_label FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) ); - - if ( ! $label ) - $label = ucfirst( $name ); - } else { - $label = $name; - } - - return apply_filters( 'woocommerce_attribute_label', $label, $name ); - } - - /** - * Get a product attributes orderby setting. - * - * @access public - * @param mixed $name - * @return string - */ - public function attribute_orderby( $name ) { - global $wpdb; - - $name = str_replace( 'pa_', '', sanitize_title( $name ) ); - - $orderby = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_orderby FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) ); - - return apply_filters( 'woocommerce_attribute_orderby', $orderby, $name ); - } - - /** - * Get an array of product attribute taxonomies. - * - * @access public - * @return array - */ - public function get_attribute_taxonomy_names() { - $taxonomy_names = array(); - $attribute_taxonomies = $this->get_attribute_taxonomies(); - if ( $attribute_taxonomies ) { - foreach ( $attribute_taxonomies as $tax ) { - $taxonomy_names[] = $this->attribute_taxonomy_name( $tax->attribute_name ); - } - } - return $taxonomy_names; - } - /** Coupon Helpers ********************************************************/ /** @@ -1553,6 +1460,73 @@ class Woocommerce { $helper = $this->get_helper( 'shortcode' ); return $helper->shortcode_wrapper( $function, $atts, $wrapper ); } + + /** + * Get attribute taxonomies. + * + * @deprecated 2.1.0 Access via the helpers + * @access public + * @return object + */ + public function get_attribute_taxonomies() { + _deprecated_function( 'Woocommerce->get_attribute_taxonomies', '2.1', 'WC_Attribute_Helper->get_attribute_taxonomies' ); + $helper = $this->get_helper( 'attribute' ); + return $helper->get_attribute_taxonomies(); + } + + /** + * Get a product attributes name. + * + * @deprecated 2.1.0 Access via the helpers + * @access public + * @param mixed $name + * @return string + */ + public function attribute_taxonomy_name( $name ) { + _deprecated_function( 'Woocommerce->attribute_taxonomy_name', '2.1', 'WC_Attribute_Helper->attribute_taxonomy_name' ); + $helper = $this->get_helper( 'attribute' ); + return $helper->attribute_taxonomy_name( $name ); + } + + /** + * Get a product attributes label. + * + * @deprecated 2.1.0 Access via the helpers + * @access public + * @param mixed $name + * @return string + */ + public function attribute_label( $name ) { + _deprecated_function( 'Woocommerce->attribute_label', '2.1', 'WC_Attribute_Helper->attribute_label' ); + $helper = $this->get_helper( 'attribute' ); + return $helper->attribute_label( $name ); + } + + /** + * Get a product attributes orderby setting. + * + * @deprecated 2.1.0 Access via the helpers + * @access public + * @param mixed $name + * @return string + */ + public function attribute_orderby( $name ) { + _deprecated_function( 'Woocommerce->attribute_orderby', '2.1', 'WC_Attribute_Helper->attribute_orderby' ); + $helper = $this->get_helper( 'attribute' ); + return $helper->attribute_orderby( $name ); + } + + /** + * Get an array of product attribute taxonomies. + * + * @access public + * @return array + */ + public function get_attribute_taxonomy_names() { + _deprecated_function( 'Woocommerce->get_attribute_taxonomy_names', '2.1', 'WC_Attribute_Helper->get_attribute_taxonomy_names' ); + $helper = $this->get_helper( 'attribute' ); + return $helper->get_attribute_taxonomy_names(); + } } /** From a43e204a8cda3634dc34256cc3a06f96044367a0 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Wed, 5 Jun 2013 16:20:56 +0200 Subject: [PATCH 09/13] Abstracted coupon helper to class #3282 --- admin/post-types/shop_coupon.php | 4 +- .../writepanels/writepanel-coupon_data.php | 2 +- classes/helpers/class-wc-coupon-helper.php | 34 +++++++++++ woocommerce.php | 59 ++++++++----------- 4 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 classes/helpers/class-wc-coupon-helper.php diff --git a/admin/post-types/shop_coupon.php b/admin/post-types/shop_coupon.php index 7f351de1253..c707413b704 100644 --- a/admin/post-types/shop_coupon.php +++ b/admin/post-types/shop_coupon.php @@ -85,7 +85,7 @@ function woocommerce_custom_coupon_columns( $column ) { break; case "type" : - echo esc_html( $woocommerce->get_coupon_discount_type( get_post_meta( $post->ID, 'discount_type', true ) ) ); + echo esc_html( $woocommerce->get_helper( 'coupon' )->get_coupon_discount_type( get_post_meta( $post->ID, 'discount_type', true ) ) ); break; case "amount" : echo esc_html( get_post_meta( $post->ID, 'coupon_amount', true ) ); @@ -148,7 +148,7 @@ function woocommerce_restrict_manage_coupons() {