woocommerce/classes/widgets/class-wc-widget-login.php

231 lines
7.7 KiB
PHP
Raw Normal View History

2011-11-07 13:27:36 +00:00
<?php
/**
* Login Widget
*
2012-08-14 17:37:50 +00:00
* @author WooThemes
* @category Widgets
* @package WooCommerce/Widgets
* @version 1.6.4
* @extends WP_Widget
2011-11-07 13:27:36 +00:00
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_Widget_Login extends WP_Widget {
2011-11-07 13:27:36 +00:00
var $woo_widget_cssclass;
var $woo_widget_description;
var $woo_widget_idbase;
var $woo_widget_name;
2012-08-14 17:37:50 +00:00
/**
* constructor
*
* @access public
* @return void
*/
function WC_Widget_Login() {
2011-11-07 13:27:36 +00:00
/* Widget variable settings. */
2012-11-15 17:46:24 +00:00
$this->woo_widget_cssclass = 'woocommerce widget_login';
2012-01-05 11:31:22 +00:00
$this->woo_widget_description = __( 'Display a login area and "My Account" links in the sidebar.', 'woocommerce' );
2011-11-07 13:27:36 +00:00
$this->woo_widget_idbase = 'woocommerce_login';
2012-10-16 09:45:33 +00:00
$this->woo_widget_name = __( 'WooCommerce Login', 'woocommerce' );
2011-11-07 13:27:36 +00:00
/* Widget settings. */
$widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
2011-11-07 13:27:36 +00:00
/* Create the widget. */
$this->WP_Widget('woocommerce_login', $this->woo_widget_name, $widget_ops);
}
2012-08-14 17:37:50 +00:00
/**
* widget function.
*
* @see WP_Widget
* @access public
* @param array $args
* @param array $instance
* @return void
*/
2011-11-07 13:27:36 +00:00
function widget( $args, $instance ) {
global $woocommerce;
2011-11-07 13:27:36 +00:00
extract($args);
// Don't show if on the account page since that has a login
if (is_account_page() && !is_user_logged_in()) return;
2012-11-05 17:22:38 +00:00
$logged_out_title = apply_filters('widget_title', empty( $instance['logged_out_title'] ) ? __('Customer Login', 'woocommerce' ) : $instance['logged_out_title'], $instance, $this->id_base );
$logged_in_title = apply_filters('widget_title', empty( $instance['logged_in_title'] ) ? __( 'Welcome %s', 'woocommerce' ) : $instance['logged_in_title'], $instance, $this->id_base );
2011-11-07 13:27:36 +00:00
echo $before_widget;
2011-11-07 13:27:36 +00:00
if (is_user_logged_in()) {
2011-11-07 13:27:36 +00:00
$user = get_user_by('id', get_current_user_id());
$logged_in_title = apply_filters( 'woocommerce_login_widget_logged_in_title', sprintf( $logged_in_title, ucwords( $user->display_name ) ) );
if ( $logged_in_title )
echo $before_title . $logged_in_title . $after_title;
2011-11-07 13:27:36 +00:00
do_action('woocommerce_login_widget_logged_in_before_links');
2011-11-07 13:27:36 +00:00
$links = apply_filters( 'woocommerce_login_widget_logged_in_links', array(
2012-10-16 09:45:33 +00:00
__( 'My account', 'woocommerce' ) => get_permalink(woocommerce_get_page_id('myaccount')),
__( 'Change my password', 'woocommerce' ) => get_permalink(woocommerce_get_page_id('change_password')),
__( 'Logout', 'woocommerce' ) => wp_logout_url(home_url())
2011-11-07 13:27:36 +00:00
));
2011-11-07 13:27:36 +00:00
if (sizeof($links>0)) :
2011-11-07 13:27:36 +00:00
echo '<ul class="pagenav">';
2011-11-07 13:27:36 +00:00
foreach ($links as $name => $link) :
echo '<li><a href="'.$link.'">'.$name.'</a></li>';
endforeach;
2011-11-07 13:27:36 +00:00
echo '</ul>';
endif;
2011-11-07 13:27:36 +00:00
do_action('woocommerce_login_widget_logged_in_after_links');
2011-11-07 13:27:36 +00:00
} else {
$logged_out_title = apply_filters( 'woocommerce_login_widget_logged_out_title', $logged_out_title );
if ( $logged_out_title )
echo $before_title . $logged_out_title . $after_title;
2011-11-07 13:27:36 +00:00
do_action('woocommerce_login_widget_logged_out_before_form');
2011-11-07 13:27:36 +00:00
global $login_errors;
2011-11-07 13:27:36 +00:00
if ( is_wp_error($login_errors) && $login_errors->get_error_code() ) foreach ($login_errors->get_error_messages() as $error) :
2013-01-03 12:21:26 +00:00
echo '<div class="woocommerce-error">' . esc_html( $error ) . "</div>\n";
2011-11-07 13:27:36 +00:00
break;
endforeach;
2011-11-07 13:27:36 +00:00
// Get redirect URL
2012-10-18 10:33:47 +00:00
$redirect_to = esc_url( apply_filters( 'woocommerce_login_widget_redirect', get_permalink( woocommerce_get_page_id( 'myaccount' ) ) ) );
2011-11-07 13:27:36 +00:00
?>
<form method="post">
2012-10-16 09:45:33 +00:00
<p><label for="user_login"><?php _e( 'Username or email', 'woocommerce' ); ?></label> <input name="log" value="<?php if (isset($_POST['log'])) echo esc_attr(stripslashes($_POST['log'])); ?>" class="input-text" id="user_login" type="text" /></p>
2012-10-16 09:45:33 +00:00
<p><label for="user_pass"><?php _e( 'Password', 'woocommerce' ); ?></label> <input name="pwd" class="input-text" id="user_pass" type="password" /></p>
2012-10-16 09:45:33 +00:00
<p><input type="submit" class="submitbutton" name="wp-submit" id="wp-submit" value="<?php _e( 'Login &rarr;', 'woocommerce' ); ?>" /> <a href="<?php echo wp_lostpassword_url(); ?>"><?php echo __( 'Lost password?', 'woocommerce' ); ?></a></p>
2011-11-07 13:27:36 +00:00
<div>
<input type="hidden" name="redirect_to" class="redirect_to" value="<?php echo $redirect_to; ?>" />
<input type="hidden" name="testcookie" value="1" />
<input type="hidden" name="woocommerce_login" value="1" />
<input type="hidden" name="rememberme" value="forever" />
</div>
2011-11-07 13:27:36 +00:00
<?php do_action('woocommerce_login_widget_logged_out_form'); ?>
2011-11-07 13:27:36 +00:00
</form>
<?php
do_action('woocommerce_login_widget_logged_out_after_form');
2011-12-07 17:46:28 +00:00
$woocommerce->add_inline_js("
// Ajax Login
jQuery('.widget_login form').submit(function(){
2011-12-07 17:46:28 +00:00
var thisform = this;
jQuery(thisform).block({ message: null, overlayCSS: {
backgroundColor: '#fff',
opacity: 0.6
2011-12-07 17:46:28 +00:00
} });
2011-12-07 17:46:28 +00:00
var data = {
action: 'woocommerce_sidebar_login_process',
security: '".wp_create_nonce("woocommerce-sidebar-login-action")."',
user_login: jQuery('input[name=\"log\"]', thisform).val(),
user_password: jQuery('input[name=\"pwd\"]', thisform).val(),
redirect_to: jQuery('.redirect_to:eq(0)', thisform).val()
};
2011-12-07 17:46:28 +00:00
// Ajax action
2012-06-10 14:14:03 +00:00
jQuery.ajax({
2012-08-21 10:12:51 +00:00
url: '" . ( ( is_ssl() || force_ssl_admin() || force_ssl_login() ) ? str_replace( 'http:', 'https:', admin_url( 'admin-ajax.php' ) ) : str_replace( 'https:', 'http:', admin_url( 'admin-ajax.php' ) ) ) . "',
2012-06-10 14:14:03 +00:00
data: data,
type: 'GET',
dataType: 'jsonp',
success: function( result ) {
jQuery('.woocommerce_error').remove();
if (result.success==1) {
window.location = result.redirect;
} else {
jQuery(thisform).prepend('<div class=\"woocommerce_error\">' + result.error + '</div>');
jQuery(thisform).unblock();
}
2011-12-07 17:46:28 +00:00
}
});
2011-12-07 17:46:28 +00:00
return false;
});
");
2011-11-07 13:27:36 +00:00
$links = apply_filters( 'woocommerce_login_widget_logged_out_links', array());
2011-11-07 13:27:36 +00:00
if (sizeof($links>0)) :
2011-11-07 13:27:36 +00:00
echo '<ul class="pagenav">';
2012-10-18 10:33:47 +00:00
foreach ( $links as $name => $link ) {
echo '<li><a href="' . esc_attr( $link ) . '">' . wp_kses_post( $name ) . '</a></li>';
}
2011-11-07 13:27:36 +00:00
echo '</ul>';
endif;
}
2011-11-07 13:27:36 +00:00
echo $after_widget;
}
2012-08-14 17:37:50 +00:00
/**
* update function.
*
* @see WP_Widget->update
* @access public
* @param array $new_instance
* @param array $old_instance
* @return array
*/
2011-11-07 13:27:36 +00:00
function update( $new_instance, $old_instance ) {
$instance['logged_out_title'] = strip_tags(stripslashes($new_instance['logged_out_title']));
$instance['logged_in_title'] = strip_tags(stripslashes($new_instance['logged_in_title']));
return $instance;
}
2012-08-14 17:37:50 +00:00
/**
* form function.
*
* @see WP_Widget->form
* @access public
* @param array $instance
* @return void
*/
2011-11-07 13:27:36 +00:00
function form( $instance ) {
?>
2012-10-16 09:45:33 +00:00
<p><label for="<?php echo $this->get_field_id('logged_out_title'); ?>"><?php _e( 'Logged out title:', 'woocommerce' ) ?></label>
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id('logged_out_title') ); ?>" name="<?php echo esc_attr( $this->get_field_name('logged_out_title') ); ?>" value="<?php if (isset ( $instance['logged_out_title'])) echo esc_attr( $instance['logged_out_title'] ); else echo __( 'Customer Login', 'woocommerce' ); ?>" /></p>
2012-10-16 09:45:33 +00:00
<p><label for="<?php echo $this->get_field_id('logged_in_title'); ?>"><?php _e( 'Logged in title:', 'woocommerce' ) ?></label>
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id('logged_in_title') ); ?>" name="<?php echo esc_attr( $this->get_field_name('logged_in_title') ); ?>" value="<?php if (isset ( $instance['logged_in_title'])) echo esc_attr( $instance['logged_in_title'] ); else echo __( 'Welcome %s', 'woocommerce' ); ?>" /></p>
2011-11-07 13:27:36 +00:00
<?php
}
}