From b9a8f2c2047da0b47922772ddc7609245d4d801a Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 31 Dec 2012 13:03:56 +0000 Subject: [PATCH] Combined some functions #1765 --- woocommerce-functions.php | 67 ++++++++++++++++++----- woocommerce-hooks.php | 4 +- woocommerce.php | 112 ++++++++------------------------------ 3 files changed, 78 insertions(+), 105 deletions(-) diff --git a/woocommerce-functions.php b/woocommerce-functions.php index d76eab684fb..10f5eda8351 100644 --- a/woocommerce-functions.php +++ b/woocommerce-functions.php @@ -16,41 +16,78 @@ * @access public * @return void */ -function woocommerce_redirects() { +function woocommerce_template_redirect() { global $woocommerce, $wp_query; // When default permalinks are enabled, redirect shop page to post type archive url - if ( isset($_GET['page_id']) && $_GET['page_id'] > 0 && get_option( 'permalink_structure' )=="" && $_GET['page_id'] == woocommerce_get_page_id('shop') ) : + if ( ! empty( $_GET['page_id'] ) && get_option( 'permalink_structure' ) == "" && $_GET['page_id'] == woocommerce_get_page_id( 'shop' ) ) { wp_safe_redirect( get_post_type_archive_link('product') ); exit; - endif; + } // When on the checkout with an empty cart, redirect to cart page - if (is_page(woocommerce_get_page_id('checkout')) && sizeof($woocommerce->cart->get_cart())==0) : - wp_redirect(get_permalink(woocommerce_get_page_id('cart'))); + elseif ( is_page( woocommerce_get_page_id( 'checkout' ) ) && sizeof( $woocommerce->cart->get_cart() ) == 0 ) { + wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) ); exit; - endif; + } // When on pay page with no query string, redirect to checkout - if (is_page(woocommerce_get_page_id('pay')) && !isset($_GET['order'])) : - wp_redirect(get_permalink(woocommerce_get_page_id('checkout'))); + elseif ( is_page( woocommerce_get_page_id( 'pay' ) ) && ! isset( $_GET['order'] ) ) { + wp_redirect( get_permalink( woocommerce_get_page_id( 'checkout' ) ) ); exit; - endif; + } // My account page redirects (logged out) - if (!is_user_logged_in() && ( is_page(woocommerce_get_page_id('edit_address')) || is_page(woocommerce_get_page_id('view_order')) || is_page(woocommerce_get_page_id('change_password')) )) : - wp_redirect(get_permalink(woocommerce_get_page_id('myaccount'))); + elseif ( ! is_user_logged_in() && ( is_page( woocommerce_get_page_id( 'edit_address' ) ) || is_page( woocommerce_get_page_id( 'view_order' ) ) || is_page( woocommerce_get_page_id( 'change_password' ) ) ) ) { + wp_redirect( get_permalink( woocommerce_get_page_id( 'myaccount' ) ) ); exit; - endif; + } // Redirect to the product page if we have a single product - if (is_search() && is_post_type_archive('product') && get_option('woocommerce_redirect_on_single_search_result')=='yes') { - if ($wp_query->post_count==1) { + elseif ( is_search() && is_post_type_archive( 'product' ) && get_option( 'woocommerce_redirect_on_single_search_result' ) == 'yes' ) { + if ( $wp_query->post_count == 1 ) { $product = get_product( $wp_query->post ); - if ($product->is_visible()) wp_safe_redirect( get_permalink($product->id), 302 ); + if ( $product->is_visible() ) + wp_safe_redirect( get_permalink( $product->id ), 302 ); exit; } } + + // Force SSL + elseif ( get_option('woocommerce_force_ssl_checkout') == 'yes' && ! is_ssl() ) { + + if ( is_checkout() ) { + wp_safe_redirect( str_replace('http:', 'https:', get_permalink( woocommerce_get_page_id( 'checkout' ) ) ), 301 ); + exit; + } elseif ( is_account_page() || apply_filters( 'woocommerce_force_ssl_checkout', false ) ) { + if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) { + wp_safe_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ) ); + exit; + } else { + wp_safe_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); + exit; + } + } + + } + + // Break out of SSL if we leave the checkout/my accounts (anywhere but thanks) + elseif ( get_option('woocommerce_force_ssl_checkout') == 'yes' && get_option('woocommerce_unforce_ssl_checkout') == 'yes' && is_ssl() && $_SERVER['REQUEST_URI'] && ! is_checkout() && ! is_page( woocommerce_get_page_id('thanks') ) && ! is_ajax() && ! is_account_page() && apply_filters( 'woocommerce_unforce_ssl_checkout', true ) ) { + + if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) { + wp_safe_redirect( preg_replace( '|^https://|', 'http://', $_SERVER['REQUEST_URI'] ) ); + exit; + } else { + wp_safe_redirect( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); + exit; + } + + } + + // Buffer the checkout page + elseif ( is_checkout() ) { + ob_start(); + } } diff --git a/woocommerce-hooks.php b/woocommerce-hooks.php index 7ffcec063ab..5186540e861 100644 --- a/woocommerce-hooks.php +++ b/woocommerce-hooks.php @@ -209,11 +209,11 @@ if ( ! is_admin() || defined('DOING_AJAX') ) { /** * Shop Page Handling and Support * - * @see woocommerce_redirects() + * @see woocommerce_template_redirect() * @see woocommerce_nav_menu_item_classes() * @see woocommerce_list_pages() */ -add_action( 'template_redirect', 'woocommerce_redirects' ); +add_action( 'template_redirect', 'woocommerce_template_redirect' ); add_filter( 'wp_nav_menu_objects', 'woocommerce_nav_menu_item_classes', 2, 20 ); add_filter( 'wp_list_pages', 'woocommerce_list_pages' ); diff --git a/woocommerce.php b/woocommerce.php index ed459b4e7a6..7f1ab652088 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -337,13 +337,18 @@ class Woocommerce { add_filter( 'template_include', array( $this, 'template_loader' ) ); add_filter( 'comments_template', array( $this, 'comments_template_loader' ) ); add_filter( 'wp_redirect', array( $this, 'redirect' ), 1, 2 ); - add_action( 'template_redirect', array( $this, 'buffer_checkout' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts' ) ); add_action( 'wp_print_scripts', array( $this, 'check_jquery' ), 25 ); add_action( 'wp_head', array( $this, 'generator' ) ); add_action( 'wp_head', array( $this, 'wp_head' ) ); add_filter( 'body_class', array( $this, 'output_body_class' ) ); add_action( 'wp_footer', array( $this, '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_siteurl', 'option_homeurl', 'option_home', 'option_url', 'option_wpurl', 'option_stylesheet_url', 'option_template_url', 'script_loader_src', 'style_loader_src', 'template_directory_uri', 'stylesheet_directory_uri', 'site_url' ); + + foreach ( $filters as $filter ) + add_filter( $filter, array( $this, 'force_ssl' ) ); } // Actions @@ -356,16 +361,6 @@ class Woocommerce { foreach ( $email_actions as $action ) add_action( $action, array( $this, 'send_transactional_email') ); - // Actions for SSL - if ( ! is_admin() || defined('DOING_AJAX') ) { - add_action( 'template_redirect', array( $this, 'ssl_redirect' ) ); - - $filters = array( 'post_thumbnail_html', 'widget_text', 'wp_get_attachment_url', 'wp_get_attachment_image_attributes', 'wp_get_attachment_url', 'option_siteurl', 'option_homeurl', 'option_home', 'option_url', 'option_wpurl', 'option_stylesheet_url', 'option_template_url', 'script_loader_src', 'style_loader_src', 'template_directory_uri', 'stylesheet_directory_uri', 'site_url' ); - - foreach ( $filters as $filter ) - add_filter( $filter, array( $this, 'force_ssl') ); - } - // Register globals for WC environment $this->register_globals(); @@ -375,9 +370,6 @@ class Woocommerce { // Init Images sizes $this->init_image_sizes(); - // Init styles - if ( ! is_admin() ) $this->init_styles(); - // Trigger API requests $this->api_requests(); @@ -416,10 +408,8 @@ class Woocommerce { // Load admin specific MO files if ( is_admin() ) { - if ( file_exists( WP_LANG_DIR . "/woocommerce/woocommerce-admin-$locale.mo" ) ) - load_textdomain( 'woocommerce', WP_LANG_DIR . "/woocommerce/woocommerce-admin-$locale.mo" ); - else - load_textdomain( 'woocommerce', $this->plugin_path() . "/i18n/languages/woocommerce-admin-$locale.mo" ); + load_textdomain( 'woocommerce', WP_LANG_DIR . "/woocommerce/woocommerce-admin-$locale.mo" ); + load_textdomain( 'woocommerce', $this->plugin_path() . "/i18n/languages/woocommerce-admin-$locale.mo" ); } load_plugin_textdomain( 'woocommerce', false, dirname( plugin_basename( __FILE__ ) ) . "/i18n/languages/$formal" ); @@ -489,26 +479,16 @@ class Woocommerce { * @return string */ public function comments_template_loader( $template ) { - if( get_post_type() !== 'product' ) return $template; + if ( get_post_type() !== 'product' ) + return $template; - if (file_exists( STYLESHEETPATH . '/' . $this->template_url . 'single-product-reviews.php' )) + if ( file_exists( STYLESHEETPATH . '/' . $this->template_url . 'single-product-reviews.php' )) return STYLESHEETPATH . '/' . $this->template_url . 'single-product-reviews.php'; else return $this->plugin_path() . '/templates/single-product-reviews.php'; } - /** - * Output buffering on the checkout allows gateways to do header redirects. - * - * @access public - * @return void - */ - public function buffer_checkout() { - if ( is_checkout() ) ob_start(); - } - - /** * Register WC environment globals. * @@ -571,44 +551,6 @@ class Woocommerce { } - /** - * Redirect to https if Force SSL is enabled. - * - * @access public - * @return void - */ - public function ssl_redirect() { - if ( get_option('woocommerce_force_ssl_checkout') == 'no' ) return; - - if ( ! is_ssl() ) { - if ( is_checkout() ) { - wp_safe_redirect( str_replace('http:', 'https:', get_permalink( woocommerce_get_page_id( 'checkout' ) ) ), 301 ); - exit; - } elseif ( is_account_page() || apply_filters( 'woocommerce_force_ssl_checkout', false ) ) { - if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) { - wp_safe_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ) ); - exit; - } else { - wp_safe_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); - exit; - } - exit; - } - } else { - // Break out of SSL if we leave the checkout/my accounts (anywhere but thanks) - if ( get_option('woocommerce_unforce_ssl_checkout') == 'yes' && $_SERVER['REQUEST_URI'] && ! is_checkout() && ! is_page( woocommerce_get_page_id('thanks') ) && ! is_ajax() && ! is_account_page() && apply_filters( 'woocommerce_unforce_ssl_checkout', true ) ) { - if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) { - wp_safe_redirect( preg_replace( '|^https://|', 'http://', $_SERVER['REQUEST_URI'] ) ); - exit; - } else { - wp_safe_redirect( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); - exit; - } - } - } - } - - /** * Output generator to aid debugging. * @@ -1029,23 +971,6 @@ class Woocommerce { } - /** - * Init frontend CSS. - * - * @access public - * @return void - */ - public function init_styles() { - - // Optional front end css - if ( ( defined('WOOCOMMERCE_USE_CSS') && WOOCOMMERCE_USE_CSS ) || ( ! defined('WOOCOMMERCE_USE_CSS') && get_option('woocommerce_frontend_css') == 'yes') ) { - $css = file_exists( get_stylesheet_directory() . '/woocommerce/style.css' ) ? get_stylesheet_directory_uri() . '/woocommerce/style.css' : $this->plugin_url() . '/assets/css/woocommerce.css'; - - wp_enqueue_style( 'woocommerce_frontend_styles', $css ); - } - } - - /** * Register/queue frontend scripts. * @@ -1121,6 +1046,16 @@ class Woocommerce { $woocommerce_params['locale'] = json_encode( $this->countries->get_country_locale() ); wp_localize_script( 'woocommerce', 'woocommerce_params', apply_filters( 'woocommerce_params', $woocommerce_params ) ); + + // CSS Styles + if ( ! defined( 'WOOCOMMERCE_USE_CSS' ) ) + define( 'WOOCOMMERCE_USE_CSS', get_option( 'woocommerce_frontend_css' ) == 'yes' ? true : false ); + + if ( WOOCOMMERCE_USE_CSS ) { + $css = file_exists( get_stylesheet_directory() . '/woocommerce/style.css' ) ? get_stylesheet_directory_uri() . '/woocommerce/style.css' : $this->plugin_url() . '/assets/css/woocommerce.css'; + + wp_enqueue_style( 'woocommerce_frontend_styles', $css ); + } } /** @@ -1671,7 +1606,8 @@ class Woocommerce { * @return void */ public function nocache() { - if ( ! defined('DONOTCACHEPAGE') ) define("DONOTCACHEPAGE", "true"); // WP Super Cache constant + if ( ! defined('DONOTCACHEPAGE') ) + define("DONOTCACHEPAGE", "true"); // WP Super Cache constant } @@ -1684,7 +1620,7 @@ class Woocommerce { */ public function cart_has_contents_cookie( $set ) { if ( ! headers_sent() ) { - if ($set) + if ( $set ) setcookie( "woocommerce_items_in_cart", "1", 0, COOKIEPATH, COOKIE_DOMAIN, false ); else setcookie( "woocommerce_items_in_cart", "0", time() - 3600, COOKIEPATH, COOKIE_DOMAIN, false );