New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fancy Notifications Module (take 2) #2038
Changes from 2 commits
8546a06
ac4ac0e
ba7be18
0d7a3a9
5948eb4
a5d336e
175e73f
0fd7e14
75f1637
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
/** | ||
* WP eCommerce Fancy Notifications CSS | ||
*/ | ||
|
||
#fancy_notification { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
background: #fff; | ||
border: 4px solid #ccc; | ||
display: none; | ||
height: auto; | ||
z-index: 9; | ||
} | ||
|
||
#fancy_notification #loading_animation { | ||
display: none; | ||
} | ||
|
||
#fancy_notification #fancy_notification_content { | ||
display: none; | ||
width: 300px; | ||
height: auto; | ||
padding: 8px; | ||
text-align: left; | ||
margin: 0 !important; | ||
} | ||
|
||
#fancy_notification #fancy_notification_content span { | ||
margin: 0 0 6px 0; | ||
display: block; | ||
font-weight: normal; | ||
} | ||
|
||
#fancy_notification #fancy_notification_content a { | ||
display: block; | ||
float: left; | ||
margin-right: 6px; | ||
margin-bottom: 3px; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
/** | ||
* WP eCommerce Fancy Notifications | ||
*/ | ||
|
||
add_action( 'wp_enqueue_scripts', array( 'WPSC_Fancy_Notifications', 'enqueue_styles' ) ); | ||
add_action( 'wp_enqueue_scripts', array( 'WPSC_Fancy_Notifications', 'enqueue_scripts' ) ); | ||
add_action( 'wpsc_add_to_cart_button_form_begin', array( 'WPSC_Fancy_Notifications', 'add_fancy_notifications' ) ); | ||
add_action( 'wpsc_theme_footer', array( 'WPSC_Fancy_Notifications', 'fancy_notifications' ) ); | ||
add_filter( 'wpsc_add_to_cart_json_response', array( 'WPSC_Fancy_Notifications', 'wpsc_add_to_cart_json_response' ) ); | ||
|
||
/** | ||
* WP eCommerce Fancy Notifications Class | ||
*/ | ||
class WPSC_Fancy_Notifications { | ||
|
||
/** | ||
* Fancy Notifications | ||
* | ||
* Container HTML for fancy notifications. | ||
* | ||
* @param boolean $return Return output. | ||
* @return string Output. | ||
*/ | ||
public static function fancy_notifications( $return = false ) { | ||
|
||
static $already_output = false; | ||
|
||
if ( $already_output ) { | ||
return ''; | ||
} | ||
|
||
$output = ''; | ||
if ( 1 == get_option( 'fancy_notifications' ) ) { | ||
$output .= '<div id="fancy_notification">'; | ||
$output .= ' <div id="loading_animation">'; | ||
$output .= ' <img id="fancy_notificationimage" title="' . esc_attr__( 'Loading', 'wpsc' ) . '" alt="' . esc_attr__( 'Loading', 'wpsc' ) . '" src="' . esc_url( wpsc_loading_animation_url() ) . '" />' . esc_html__( 'Updating', 'wpsc' ) . '...'; | ||
$output .= ' </div>'; | ||
$output .= ' <div id="fancy_notification_content"></div>'; | ||
$output .= '</div>'; | ||
} | ||
|
||
$already_output = true; | ||
|
||
if ( $return ) { | ||
return $output; | ||
} | ||
echo $output; | ||
|
||
} | ||
|
||
/** | ||
* Fancy Notification Content | ||
* | ||
* @param array $cart_messages Cart message. | ||
* @return string Fancy notification content. | ||
*/ | ||
public static function fancy_notification_content( $cart_messages ) { | ||
|
||
$siteurl = get_option( 'siteurl' ); | ||
|
||
$output = ''; | ||
foreach ( (array)$cart_messages as $cart_message ) { | ||
$output .= '<span>' . $cart_message . '</span><br />'; | ||
} | ||
$output .= sprintf( '<a href="%s" class="go_to_checkout">%s</a>', esc_url( get_option( 'shopping_cart_url' ) ), esc_html__( 'Go to Checkout', 'wpsc' ) ); | ||
$output .= sprintf( '<a href="#" onclick="jQuery( \'#fancy_notification\' ).css( \'display\', \'none\' ); return false;" class="continue_shopping">%s</a>', esc_html__( 'Continue Shopping', 'wpsc' ) ); | ||
|
||
return $output; | ||
|
||
} | ||
|
||
/** | ||
* Add To Cart JSON Response | ||
* | ||
* Adds 'fancy_notification' content to JSON response. | ||
* | ||
* @param array $json_response JSON response. | ||
* @return array Updated JSON response. | ||
*/ | ||
public static function wpsc_add_to_cart_json_response( $json_response ) { | ||
|
||
if ( is_numeric( $json_response['product_id'] ) && 1 == get_option( 'fancy_notifications' ) ) { | ||
$json_response['fancy_notification'] = str_replace( array( "\n", "\r" ), array( '\n', '\r' ), self::fancy_notification_content( $json_response['cart_messages'] ) ); | ||
} | ||
|
||
return $json_response; | ||
|
||
} | ||
|
||
/** | ||
* Add Fancy Notifications | ||
*/ | ||
public static function add_fancy_notifications() { | ||
|
||
add_action( 'wp_footer', array( 'WPSC_Fancy_Notifications', 'fancy_notifications' ) ); | ||
|
||
} | ||
|
||
/** | ||
* Enqueue Styles | ||
*/ | ||
public static function enqueue_styles() { | ||
|
||
wp_enqueue_style( 'wpsc-fancy-notifications', self::plugin_url() . '/css/fancy-notifications.css', false, '1.0' ); | ||
|
||
} | ||
|
||
/** | ||
* Enqueue Scripts | ||
*/ | ||
public static function enqueue_scripts() { | ||
|
||
wp_enqueue_script( 'wpsc-fancy-notifications', self::plugin_url() . '/js/fancy-notifications.js', array( 'jquery' ), '1.0' ); | ||
|
||
} | ||
|
||
/** | ||
* Plugin URL | ||
* | ||
* @return string URL for fancy notifications directory. | ||
*/ | ||
public static function plugin_url() { | ||
|
||
return plugins_url( '', __FILE__ ); | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
/** | ||
* WP eCommerce Fancy Notifications JS | ||
*/ | ||
|
||
var WPEC_Fancy_Notifications; | ||
|
||
( function( $ ) { | ||
|
||
WPEC_Fancy_Notifications = { | ||
|
||
/** | ||
* Move Fancy Notification element to end of HTML body. | ||
*/ | ||
appendToBody : function() { | ||
|
||
$( '#fancy_notification' ).appendTo( 'body' ); | ||
|
||
}, | ||
|
||
/** | ||
* Fancy Notification: Show | ||
*/ | ||
wpscAddToCart : function( e ) { | ||
|
||
$( 'div.wpsc_loading_animation' ).css( 'visibility', 'hidden' ); | ||
WPEC_Fancy_Notifications.fancy_notification( e.form ); | ||
|
||
}, | ||
|
||
/** | ||
* Fancy Notification: Hide | ||
*/ | ||
wpscAddedToCart : function( e ) { | ||
|
||
if ( ( e.response ) ) { | ||
if ( e.response.hasOwnProperty( 'fancy_notification' ) && e.response.fancy_notification ) { | ||
if ( $( '#fancy_notification_content' ) ) { | ||
$( '#fancy_notification_content' ).html( e.response.fancy_notification ); | ||
$( '#loading_animation').css( 'display', 'none' ); | ||
$( '#fancy_notification_content' ).css( 'display', 'block' ); | ||
} | ||
} | ||
$( document ).trigger( { type : 'wpsc_fancy_notification', response : e.response } ); | ||
} | ||
|
||
if ( $( '#fancy_notification' ).length > 0 ) { | ||
$( '#loading_animation' ).css( 'display', 'none' ); | ||
} | ||
|
||
}, | ||
|
||
/** | ||
* Fancy Notification | ||
* | ||
* @param object parent_form Form element. | ||
*/ | ||
fancy_notification : function( parent_form ) { | ||
|
||
var fancyNotificationEl = $( '#fancy_notification' ); | ||
|
||
if ( typeof( WPSC_SHOW_FANCY_NOTIFICATION ) == 'undefined' ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Getting this from Scrutinizer: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. Should be === Also 'typeof' should not be implemented as a function I think. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof |
||
WPSC_SHOW_FANCY_NOTIFICATION = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also from Scrutinizer: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the purpose of that JS variable is that if you define it and set it to false it will disable fancy notifications. Again, kept for backwards compatibility. If we defined it somewhere I don't think we could guarantee that someone may not have defined it before us? Haven't checked if the admin setting is filterable at all. If not it probably should be and we could try to deprecate this use case? |
||
} | ||
|
||
if ( ( WPSC_SHOW_FANCY_NOTIFICATION === true ) && ( fancyNotificationEl !== null ) ) { | ||
fancyNotificationEl.css( { | ||
display : 'block', | ||
position : 'fixed', | ||
left : ( $( window ).width() - fancyNotificationEl.outerWidth() ) / 2, | ||
top : ( $( window ).height() - fancyNotificationEl.outerHeight() ) / 2 | ||
} ); | ||
$( '#loading_animation' ).css( 'display', 'block' ); | ||
$( '#fancy_notification_content' ).css( 'display', 'none' ); | ||
} | ||
|
||
} | ||
|
||
}; | ||
|
||
/** | ||
* Event Handlers | ||
*/ | ||
$( document ).on( 'ready', WPEC_Fancy_Notifications.appendToBody ); | ||
$( document ).on( 'wpscAddToCart', WPEC_Fancy_Notifications.wpscAddToCart ); | ||
$( document ).on( 'wpscAddedToCart', WPEC_Fancy_Notifications.wpscAddedToCart ); | ||
|
||
} ) ( jQuery ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,9 +185,9 @@ function wpsc_add_to_cart() { | |
|
||
$json_response = $json_response + $output; | ||
|
||
if ( is_numeric( $product_id ) && 1 == get_option( 'fancy_notifications' ) ) { | ||
$json_response['fancy_notification'] = str_replace( array( "\n", "\r" ), array( '\n', '\r' ), fancy_notification_content( $cart_messages ) ); | ||
} | ||
//if ( is_numeric( $product_id ) && 1 == get_option( 'fancy_notifications' ) ) { | ||
// $json_response['fancy_notification'] = str_replace( array( "\n", "\r" ), array( '\n', '\r' ), fancy_notification_content( $cart_messages ) ); | ||
//} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @benhuson Is there any reason we're maintaining commented-out code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. Oops. |
||
|
||
$json_response = apply_filters( 'wpsc_add_to_cart_json_response', $json_response ); | ||
|
||
|
@@ -241,13 +241,16 @@ function wpsc_add_to_cart_button( $product_id, $return = false ) { | |
} | ||
} | ||
|
||
/* 19-02-09 | ||
* add to cart shortcode function used for shortcodes calls the function in | ||
/** | ||
* Add to cart shortcode function used for shortcodes calls the function in | ||
* product_display_functions.php | ||
* | ||
* @since 19-02-2009 | ||
* | ||
* Note: Really old legacy shortcode support for add to cart buttons. | ||
* This isn't a proper WordPress shortcode! | ||
*/ | ||
|
||
function add_to_cart_shortcode( $content = '' ) { | ||
static $fancy_notification_output = false; | ||
if ( ! in_the_loop() ) | ||
return $content; | ||
|
||
|
@@ -257,11 +260,6 @@ function add_to_cart_shortcode( $content = '' ) { | |
$output = wpsc_add_to_cart_button( $product_id, true ); | ||
$content = str_replace( $original_string, $output, $content ); | ||
} | ||
|
||
if ( ! $fancy_notification_output ) { | ||
$content .= wpsc_fancy_notifications( true ); | ||
$fancy_notification_output = true; | ||
} | ||
} | ||
return $content; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benhuson Can we use site_url() here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually $siteurl is not used so can be removed.