Permalink
Browse files

Fancy Notifications Module (take 2) (#2038)

* Add Fancy Notifications module.

* Use Fancy Notifications module.

* Remove unused $siteurl from fancy_notification_content().

* Remove commented-out code in wpsc_add_to_cart().

* Streamline code where `wpsc_add_to_cart_json_response` is used.

* Deprecate `parent_form` in WPEC_Fancy_Notifications.fancy_notification().

* Fix use of JavaScript `typeof` and use === in WPEC_Fancy_Notifications.fancy_notification().

* Remove `parent_form` in WPEC_Fancy_Notifications.fancy_notification().

* Add Fancy Noifications module @since documentation.
  • Loading branch information...
1 parent 1d6059a commit ca99f845686483b0d70bd595b3c733bc88ca45bb @benhuson benhuson committed with JustinSainton May 20, 2016
@@ -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;
}
@@ -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;
+}
@@ -0,0 +1,144 @@
+<?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
+ *
+ * @since 4.0
+ */
+class WPSC_Fancy_Notifications {
+
+ /**
+ * Fancy Notifications
+ *
+ * Container HTML for fancy notifications.
+ *
+ * @since 4.0
+ *
+ * @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
+ *
+ * @since 4.0
+ *
+ * @param array $cart_messages Cart message.
+ * @return string Fancy notification content.
+ */
+ public static function fancy_notification_content( $cart_messages ) {
+
+ $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.
+ *
+ * @since 4.0
+ *
+ * @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
+ *
+ * @since 4.0
+ */
+ public static function add_fancy_notifications() {
+
+ add_action( 'wp_footer', array( 'WPSC_Fancy_Notifications', 'fancy_notifications' ) );
+
+ }
+
+ /**
+ * Enqueue Styles
+ *
+ * @since 4.0
+ */
+ public static function enqueue_styles() {
+
+ wp_enqueue_style( 'wpsc-fancy-notifications', self::plugin_url() . '/css/fancy-notifications.css', false, '1.0' );
+
+ }
+
+ /**
+ * Enqueue Scripts
+ *
+ * @since 4.0
+ */
+ public static function enqueue_scripts() {
+
+ wp_enqueue_script( 'wpsc-fancy-notifications', self::plugin_url() . '/js/fancy-notifications.js', array( 'jquery' ), '1.0' );
+
+ }
+
+ /**
+ * Plugin URL
+ *
+ * @since 4.0
+ *
+ * @return string URL for fancy notifications directory.
+ */
+ public static function plugin_url() {
+
+ return plugins_url( '', __FILE__ );
+
+ }
+
+}
@@ -0,0 +1,96 @@
+
+/**
+ * WP eCommerce Fancy Notifications JS
+ *
+ * @since 4.0
+ */
+
+var WPEC_Fancy_Notifications;
+
+( function( $ ) {
+
+ WPEC_Fancy_Notifications = {
+
+ /**
+ * Move Fancy Notification element to end of HTML body.
+ *
+ * @since 4.0
+ */
+ appendToBody : function() {
+
+ $( '#fancy_notification' ).appendTo( 'body' );
+
+ },
+
+ /**
+ * Fancy Notification: Show
+ *
+ * @since 4.0
+ */
+ wpscAddToCart : function( e ) {
+
+ $( 'div.wpsc_loading_animation' ).css( 'visibility', 'hidden' );
+ WPEC_Fancy_Notifications.fancy_notification();
+
+ },
+
+ /**
+ * Fancy Notification: Hide
+ *
+ * @since 4.0
+ */
+ 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
+ *
+ * @since 4.0
+ */
+ fancy_notification : function() {
+
+ var fancyNotificationEl = $( '#fancy_notification' );
+
+ if ( typeof WPSC_SHOW_FANCY_NOTIFICATION === 'undefined' ) {
+ WPSC_SHOW_FANCY_NOTIFICATION = true;
+ }
+
+ 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 );
@@ -183,13 +183,7 @@ function wpsc_add_to_cart() {
$output = _wpsc_ajax_get_cart( false, $cart_messages );
- $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 ) );
- }
-
- $json_response = apply_filters( 'wpsc_add_to_cart_json_response', $json_response );
+ $json_response = apply_filters( 'wpsc_add_to_cart_json_response', $json_response + $output );
die( json_encode( $json_response ) );
}
@@ -241,13 +235,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 +254,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;
}
@@ -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 );
@@ -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
Oops, something went wrong.

0 comments on commit ca99f84

Please sign in to comment.