false;
$error_message = '';
if ( ! wp_verify_nonce( $_POST['nonce'], 'woocommerce-login' ) ) {
$error = true;
/* translators: %s: Error. */
$error_message = sprintf( esc_html__( '%s Sorry, the nonce security check didn’t pass. Please reload the page and try again. You may want to try clearing your browser cache as a last attempt.', 'elementor-pro' ), 'Error:' );
} else {
$info = [
'user_login' => trim( $_POST['username'] ),
'user_password' => trim( $_POST['password'] ),
'remember' => $_POST['remember'],
];
$user_signon = wp_signon( $info, false );
if ( is_wp_error( $user_signon ) ) {
$error = true;
$error_message = $user_signon->get_error_message();
}
}
if ( $error ) {
wc_add_notice(
$error_message,
'error'
);
$response = [
'logged_in' => false,
'message' => wc_print_notices( true ),
];
} else {
$response = [ 'logged_in' => true ];
}
echo wp_json_encode( $response );
wp_die();
}
/**
* Print Woocommerce Shipping Message
*
* Format the shipping messages that will be displayed on the Cart and Checkout Widgets.
* This will add extra classes to those messages so that we can target certain messages
* with certain style controls.
*
* @since 3.5.0
*
* @param string $html the original HTML from WC
* @param string $classes the classes we will surround $html with
* @return string the final formatted HTML that will be rendered
*/
private function print_woocommerce_shipping_message( $html, $classes ) {
return '' . $html . '';
}
/**
* Register Ajax Actions.
*
* Registers ajax action used by the Editor js.
*
* @since 3.5.0
*
* @param Ajax $ajax
*/
public function register_ajax_actions( Ajax $ajax ) {
// `woocommerce_update_page_option` is called in the editor save-show-modal.js.
$ajax->register_ajax_action( 'pro_woocommerce_update_page_option', [ $this, 'update_page_option' ] );
$ajax->register_ajax_action( 'pro_woocommerce_mock_notices', [ $this, 'woocommerce_mock_notices' ] );
}
public function woocommerce_mock_notices( $data ) {
if ( in_array( 'wc_error', $data['notice_elements'], true ) ) {
/* translators: 1: Error notice text, 2: Error notice link. */
$notice_message = sprintf(
'%1$s %2$s',
esc_html__( 'Oops, this is how an error notice would look.', 'elementor-pro' ),
esc_html__( 'Here\'s a link', 'elementor-pro' )
);
wc_add_notice( $notice_message, 'error' );
}
if ( in_array( 'wc_message', $data['notice_elements'], true ) ) {
/* translators: 1: Message notice button, 2: Message notice text, 3: Message notice link. */
$notice_message = sprintf(
'%1$s %2$s %3$s',
esc_html__( 'Button', 'elementor-pro' ),
esc_html__( 'This is what a WooCommerce message notice looks like.', 'elementor-pro' ),
esc_html__( 'Here\'s a link', 'elementor-pro' )
);
wc_add_notice( $notice_message, 'success' );
}
if ( in_array( 'wc_info', $data['notice_elements'], true ) ) {
/* translators: 1: Info notice button, 2: Info notice text. */
$notice_message = sprintf(
'%1$s %2$s',
esc_html__( 'Button', 'elementor-pro' ),
esc_html__( 'This is how WooCommerce provides an info notice.', 'elementor-pro' )
);
wc_add_notice( $notice_message, 'notice' );
}
return '
' . wc_print_notices( true ) . '
';
}
/**
* Update Page Option.
*
* Ajax action can be used to update any WooCommerce option.
*
* @since 3.5.0
*
* @param array $data
*/
public function update_page_option( $data ) {
update_option( $data['option_name'], $data['editor_post_id'] );
}
public function init_site_settings( \Elementor\Core\Kits\Documents\Kit $kit ) {
$kit->register_tab( 'settings-woocommerce', \ElementorPro\Modules\Woocommerce\Settings\Settings_Woocommerce::class );
}
/**
* Add Update Kit Settings Hooks
*
* Add hooks that update the corresponding kit setting when the WooCommerce option is updated.
*/
public function add_update_kit_settings_hooks() {
add_action( 'update_option_woocommerce_cart_page_id', function( $old_value, $value ) {
Plugin::elementor()->kits_manager->update_kit_settings_based_on_option( 'woocommerce_cart_page_id', $value );
}, 10, 2 );
add_action( 'update_option_woocommerce_checkout_page_id', function( $old_value, $value ) {
Plugin::elementor()->kits_manager->update_kit_settings_based_on_option( 'woocommerce_checkout_page_id', $value );
}, 10, 2 );
add_action( 'update_option_woocommerce_myaccount_page_id', function( $old_value, $value ) {
Plugin::elementor()->kits_manager->update_kit_settings_based_on_option( 'woocommerce_myaccount_page_id', $value );
}, 10, 2 );
add_action( 'update_option_woocommerce_terms_page_id', function( $old_value, $value ) {
Plugin::elementor()->kits_manager->update_kit_settings_based_on_option( 'woocommerce_terms_page_id', $value );
}, 10, 2 );
}
/**
* Elementor WC My Account Logout
*
* Programatically log out if $_REQUEST['elementor_wc_logout'] is set.
* The $_REQUEST variables we have generated a custom logout URL for in the My Account menu.
*
* @since 3.5.0
*/
public function elementor_wc_my_account_logout() {
if ( ! empty( $_REQUEST['elementor_wc_logout'] ) && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'customer-logout' ) ) {
wp_logout(); // Log the user out Programatically.
wp_safe_redirect( esc_url( $_REQUEST['elementor_my_account_redirect'] ) ); // Redirect back to the widget page.
exit;
}
}
/**
* Add Localize Data
*
* Makes `woocommercePages` available with the page name and the associated post ID for use with the various
* widgets site settings modal.
*
* @param $settings
* @return array
*/
public function add_localize_data( $settings ) {
$settings['woocommerce']['woocommercePages'] = [
'checkout' => wc_get_page_id( 'checkout' ),
'cart' => wc_get_page_id( 'cart' ),
'myaccount' => wc_get_page_id( 'myaccount' ),
'purchase_summary' => get_option( 'elementor_woocommerce_purchase_summary_page_id' ),
];
return $settings;
}
/**
* Localize Added To Cart On Product Single
*
* WooCommerce doesn't trigger `added_to_cart` event on its products single page which is required for us to
* automatically open our Menu Cart if the settings is chosen. We make the `productAddedToCart` setting
* available that we can use in the Menu Cart js to check if a product has just been added.
*
* @since 3.5.0
*/
public function localize_added_to_cart_on_product_single() {
add_filter( 'elementor_pro/frontend/localize_settings', function ( $settings ) {
$settings['woocommerce']['productAddedToCart'] = true;
return $settings;
} );
}
public function e_notices_body_classes( $classes ) {
foreach ( $this->woocommerce_notices_elements as $notice_element ) {
$classes[] = 'e-' . str_replace( '_', '-', $notice_element ) . '-notice ';
}
return $classes;
}
/**
* Should load WC Notices Styles
*
* Determine if we should load the WooCommerce notices CSS.
* It should only load:
* - When we are in the Editor, regardless if any notices have been activated.
* - If WooCoomerce is active.
* - When we are on the front end, if at least one notice is activated.
*
* It should not load in WP Admin.
*
* @return boolean
*/
private function should_load_wc_notices_styles() {
$woocommerce_active = in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) );
$is_editor = ! empty( $_GET['elementor-preview'] );
// Editor checks.
if ( $woocommerce_active && $is_editor ) {
return true;
}
$kit = Plugin::elementor()->kits_manager->get_active_kit_for_frontend();
$this->woocommerce_notices_elements = is_array( $kit->get_settings_for_display( 'woocommerce_notices_elements' ) ) ? $kit->get_settings_for_display( 'woocommerce_notices_elements' ) : [];
// Front end checks.
if (
0 < count( $this->woocommerce_notices_elements ) // At least one notice has been activated.
&& $woocommerce_active // WooCommerce is active.
&& ( ! is_admin() || $is_editor ) // We are not in WP Admin.
) {
return true;
}
return false;
}
public function get_order_received_endpoint_url( $url, $endpoint, $value ) {
$order_received_endpoint = get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' );
if ( $order_received_endpoint === $endpoint ) {
$woocommerce_purchase_summary_page_id = get_option( 'elementor_woocommerce_purchase_summary_page_id' );
$order = wc_get_order( $value );
if ( $woocommerce_purchase_summary_page_id && $order ) {
$url = trailingslashit( trailingslashit( trailingslashit( get_permalink( $woocommerce_purchase_summary_page_id ) ) . $order_received_endpoint ) . $order->get_id() );
}
}
return $url;
}
public function maybe_define_woocommerce_checkout() {
$woocommerce_purchase_summary_page_id = get_option( 'elementor_woocommerce_purchase_summary_page_id' );
if ( $woocommerce_purchase_summary_page_id && intval( $woocommerce_purchase_summary_page_id ) === get_queried_object_id() ) {
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
define( 'WOOCOMMERCE_CHECKOUT', true );
}
}
}
public function __construct() {
parent::__construct();
add_action( 'elementor/kit/register_tabs', [ $this, 'init_site_settings' ], 1, 40 );
$this->add_update_kit_settings_hooks();
$this->use_mini_cart_template = 'yes' === get_option( 'elementor_' . self::OPTION_NAME_USE_MINI_CART, 'no' );
if ( is_admin() ) {
add_action( 'elementor/admin/after_create_settings/' . Settings::PAGE_ID, [ $this, 'register_admin_fields' ], 15 );
}
add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'maybe_init_cart' ] );
add_action( 'elementor/dynamic_tags/register', [ $this, 'register_tags' ] );
add_action( 'elementor/documents/register', [ $this, 'register_documents' ] );
add_action( 'elementor/theme/register_conditions', [ $this, 'register_conditions' ] );
add_action( 'wp_ajax_elementor_woocommerce_checkout_login_user', [ $this, 'elementor_woocommerce_checkout_login_user' ] );
add_action( 'wp_ajax_nopriv_elementor_woocommerce_checkout_login_user', [ $this, 'elementor_woocommerce_checkout_login_user' ] );
add_filter( 'elementor/theme/need_override_location', [ $this, 'theme_template_include' ], 10, 2 );
add_filter( 'elementor_pro/frontend/localize_settings', [ $this, 'localized_settings_frontend' ] );
// Load our widget Before WooCommerce Ajax. See the variable's PHPDoc for details.
add_action( 'woocommerce_checkout_update_order_review', [ $this, 'load_widget_before_wc_ajax' ] );
// On Editor - Register WooCommerce frontend hooks before the Editor init.
// Priority = 5, in order to allow plugins remove/add their wc hooks on init.
if ( ! empty( $_REQUEST['action'] ) && 'elementor' === $_REQUEST['action'] && is_admin() ) {
add_action( 'init', [ $this, 'register_wc_hooks' ], 5 );
}
// Allow viewing of Checkout page in the Editor with an empty cart.
if (
( ! empty( $_REQUEST['action'] ) && 'elementor' === $_REQUEST['action'] && is_admin() ) // Elementor Editor
|| ! empty( $_REQUEST['elementor-preview'] ) // Elementor Editor Preview
|| ( ! empty( $_REQUEST['action'] ) && 'elementor_ajax' === $_REQUEST['action'] ) // Elementor Editor Preview - Ajax Render Widget
) {
add_filter( 'woocommerce_checkout_redirect_empty_cart', '__return_false', 5 );
}
if ( $this->use_mini_cart_template ) {
add_filter( 'woocommerce_add_to_cart_fragments', [ $this, 'menu_cart_fragments' ] );
add_filter( 'woocommerce_locate_template', [ $this, 'woocommerce_locate_template' ], 10, 3 );
}
add_filter( 'elementor/widgets/wordpress/widget_args', [ $this, 'woocommerce_wordpress_widget_css_class' ], 10, 2 );
add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] );
// Make the Logout redirect go to our my account widget page instead of the set My Account Page.
add_action( 'init', [ $this, 'elementor_wc_my_account_logout' ], 5 );
add_filter( 'elementor_pro/editor/localize_settings', [ $this, 'add_localize_data' ] );
add_action( 'wp', [ $this, 'maybe_define_woocommerce_checkout' ] );
add_filter( 'woocommerce_get_endpoint_url', [ $this, 'get_order_received_endpoint_url' ], 10, 3 );
// Filters for messages on the Shipping calculator
add_filter( 'woocommerce_shipping_may_be_available_html', function ( $html ) {
return $this->print_woocommerce_shipping_message( $html, 'woocommerce-shipping-may-be-available-html e-checkout-message e-cart-content' );
}, 10, 1 );
add_filter( 'woocommerce_shipping_not_enabled_on_cart_html', function ( $html ) {
return $this->print_woocommerce_shipping_message( $html, 'woocommerce-shipping-not_enabled-on-cart-html e-checkout-message e-cart-content' );
}, 10, 1 );
add_filter( 'woocommerce_shipping_estimate_html', function ( $html ) {
return $this->print_woocommerce_shipping_message( $html, 'woocommerce-shipping-estimate-html e-checkout-message e-cart-content' );
}, 10, 1 );
add_filter( 'woocommerce_cart_no_shipping_available_html', function ( $html ) {
return $this->print_woocommerce_shipping_message( $html, 'woocommerce-cart-no-shipping-available-html e-checkout-message e-cart-content' );
}, 10, 1 );
add_filter( 'woocommerce_no_available_payment_methods_message', function ( $html ) {
return $this->print_woocommerce_shipping_message( $html, 'woocommerce-no-available-payment-methods-message e-description' );
}, 10, 1 );
add_filter( 'woocommerce_no_shipping_available_html', function ( $html ) {
return $this->print_woocommerce_shipping_message( $html, 'woocommerce-no-shipping-available-html e-checkout-message' );
}, 10, 1 );
add_action( 'woocommerce_add_to_cart', [ $this, 'localize_added_to_cart_on_product_single' ] );
// WooCommerce Notice Site Settings
if ( $this->should_load_wc_notices_styles() ) {
add_filter( 'body_class', [ $this, 'e_notices_body_classes' ] );
// WooCommerce Notices CSS
wp_enqueue_style(
'e-woocommerce-notices',
ELEMENTOR_PRO_URL . 'assets/css/woocommerce-notices.min.css',
[],
ELEMENTOR_PRO_VERSION
);
}
}
}