Skip to content
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

Merged
5 changes: 5 additions & 0 deletions wp-shopping-cart.php
Expand Up @@ -87,6 +87,11 @@ public function _register_core_components( $components ) {
WPSC_FILE_PATH . '/wpsc-components/marketplace-core-v1/marketplace-core-v1.php'
);

$components['fancy-notifications']['fancy-notifications-v1'] = array(
'title' => __( 'Fancy Notifications v1', 'wpsc' ),
'includes' => WPSC_FILE_PATH . '/wpsc-components/fancy-notifications/fancy-notifications.php'
);

return $components;
}

Expand Down
41 changes: 41 additions & 0 deletions wpsc-components/fancy-notifications/css/fancy-notifications.css
@@ -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;
}
130 changes: 130 additions & 0 deletions wpsc-components/fancy-notifications/fancy-notifications.php
@@ -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' );
Copy link
Member

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?

Copy link
Member Author

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.


$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__ );

}

}
88 changes: 88 additions & 0 deletions wpsc-components/fancy-notifications/js/fancy-notifications.js
@@ -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' ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting this from Scrutinizer: === was expected, but instead == was given.

Copy link
Member Author

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also from Scrutinizer: WPSC_SHOW_FANCY_NOTIFICATION does not seem to be defined.

Copy link
Member Author

Choose a reason for hiding this comment

The 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 );
22 changes: 10 additions & 12 deletions wpsc-components/theme-engine-v1/helpers/ajax.php
Expand Up @@ -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 ) );
//}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benhuson Is there any reason we're maintaining commented-out code?

Copy link
Member Author

Choose a reason for hiding this comment

The 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 );

Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
41 changes: 0 additions & 41 deletions wpsc-components/theme-engine-v1/helpers/product.php
@@ -1,6 +1,5 @@
<?php
add_action( 'save_post' , 'wpsc_refresh_page_urls', 10, 2 );
add_action( 'wpsc_theme_footer', 'wpsc_fancy_notifications' );

if ( get_option( 'wpsc_replace_page_title' ) == 1 ) {
add_filter( 'wp_title', 'wpsc_replace_wp_title', 10, 2 );
Expand Down Expand Up @@ -774,46 +773,6 @@ function wpsc_loading_animation_url() {
return apply_filters( 'wpsc_loading_animation_url', WPSC_CORE_THEME_URL . 'wpsc-images/indicator.gif' );
}

function fancy_notifications() {
return wpsc_fancy_notifications( true );
}
function wpsc_fancy_notifications( $return = false ) {
static $already_output = false;

if ( $already_output )
return '';

$output = "";
if ( get_option( 'fancy_notifications' ) == 1 ) {
$output = "";
$output .= "<div id='fancy_notification'>\n\r";
$output .= " <div id='loading_animation'>\n\r";
$output .= '<img id="fancy_notificationimage" title="' . esc_attr__( 'Loading', 'wp-e-commerce' ) . '" alt="' . esc_attr__( 'Loading', 'wp-e-commerce' ) . '" src="' . wpsc_loading_animation_url() . '" />' . __( 'Updating', 'wp-e-commerce' ) . "...\n\r";
$output .= " </div>\n\r";
$output .= " <div id='fancy_notification_content'>\n\r";
$output .= " </div>\n\r";
$output .= "</div>\n\r";
}

$already_output = true;

if ( $return )
return $output;
else
echo $output;
}

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 .= "<a href='" . get_option( 'shopping_cart_url' ) . "' class='go_to_checkout'>" . __( 'Go to Checkout', 'wp-e-commerce' ) . "</a>";
$output .= "<a href='#' onclick='jQuery(\"#fancy_notification\").css(\"display\", \"none\"); return false;' class='continue_shopping'>" . __( 'Continue Shopping', 'wp-e-commerce' ) . "</a>";
return $output;
}

/*
* wpsc product url function, gets the URL of a product,
* Deprecated, all parameters past the first unused. use get_permalink
Expand Down