Skip to content

Commit

Permalink
Merge f99d0de into 248963b
Browse files Browse the repository at this point in the history
  • Loading branch information
bfocht committed Feb 12, 2019
2 parents 248963b + f99d0de commit 3780620
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 74 deletions.
26 changes: 8 additions & 18 deletions assets/js/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@
} ).length;
},

addItemApi: function( data, isc, success, error ) {
addItemApi: function( data, success, error ) {
var param = '&cart=' + JSON.stringify( data );
if ( isc ) {
param += '&isc='+isc;
}
var settings = {
type: 'GET',
url: rstore.urls.cart_api + param,
Expand All @@ -55,25 +52,20 @@
$.ajax( settings ).done( success ).fail( error );
},

addItem: function( id, qty, redirect, isc, $form ) {
addItem: function( id, qty, $form ) {
var data = { items: [ {
id: id,
quantity: ( qty > 0 ) ? qty : 1, // Must be greater than 0
} ] };

cart.addItemApi( data, isc, function( response ) {
cart.addItemApi( data, function( response ) {
if ( response.error ) {
return cart.addItemError( $form, response );
}

cart.updateCount( response );

if ( response.nextStepUrl ) {
if ( redirect ) {
window.location.href = response.nextStepUrl;
return;
}

$form.find( '.rstore-cart' ).find( 'a' ).attr( 'href', response.nextStepUrl );
}

Expand Down Expand Up @@ -110,21 +102,19 @@
var $this = $( this ),
$form = $this.closest( '.rstore-add-to-cart-form' ),
id = $this.attr( 'data-id' ),
qty = parseInt( $this.attr( 'data-quantity' ), 10 ),
redirect = ( $this.attr( 'data-redirect' ) === 'true' ),
isc = $this.attr( 'data-isc' );
qty = parseInt( $this.attr( 'data-quantity' ), 10 );

e.preventDefault();
if ( $this.attr( 'data-loading' ) ) {
return false;
}

$this.attr( 'data-loading', 'true' );

$form.find( '.rstore-message' ).empty();
$form.find( '.rstore-loading' ).removeClass( 'rstore-loading-hidden' );

cart.addItem( id, qty, redirect, isc, $form );
if ( id ) {
e.preventDefault();
cart.addItem( id, qty, $form );
}
},

addItemSuccess: function( $form ) {
Expand Down
2 changes: 1 addition & 1 deletion assets/js/store.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 16 additions & 14 deletions includes/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ public function __construct() {
*/
$this->max_retries = (int) apply_filters( 'rstore_api_max_retries', $this->max_retries );

$this->urls['api'] = sprintf( 'https://www.%s/api/v1/', $this->tld );
$this->urls['cart'] = sprintf( 'https://cart.%s/', $this->tld );
$this->urls['www'] = sprintf( 'https://www.%s/', $this->tld );
$this->urls['sso'] = sprintf( 'https://sso.%s/', $this->tld );
$this->urls['account'] = sprintf( 'https://account.%s/', $this->tld );
$this->urls['gui'] = sprintf( 'https://gui.%s/pcjson/standardheaderfooter', $this->tld );
$this->urls['api'] = sprintf( 'https://www.%s/api/v1/', $this->tld );
$this->urls['cart_api'] = sprintf( 'https://www.%s/api/v1/cart/{pl_id}', $this->tld );
$this->urls['cart'] = sprintf( 'https://cart.%s/', $this->tld );
$this->urls['www'] = sprintf( 'https://www.%s/', $this->tld );
$this->urls['sso'] = sprintf( 'https://sso.%s/', $this->tld );
$this->urls['account'] = sprintf( 'https://account.%s/', $this->tld );
$this->urls['gui'] = sprintf( 'https://gui.%s/pcjson/standardheaderfooter', $this->tld );

}

Expand All @@ -89,21 +90,21 @@ public function __construct() {
*
* @since 0.2.0
*
* @param string $url The original URL.
* @param string $url The original URL.
* @param array $args (optional) Additional query arguments.
* @param string $url_key (optional) Url Key to use for bulding url.
*
* @return string
*/
public function add_query_args( $url ) {

$args = [];
public function add_query_args( $url, $args = [], $url_key = '' ) {

if ( rstore_is_setup() ) {

$args['plid'] = (int) rstore_get_option( 'pl_id' );

}

$args = (array) apply_filters( 'rstore_api_query_args', $args );
$args = (array) apply_filters( 'rstore_api_query_args', $args, $url_key, $url );

return esc_url_raw( add_query_arg( $args, $url ) );

Expand All @@ -114,12 +115,13 @@ public function add_query_args( $url ) {
*
* @since 0.2.0
*
* @param string $url_key (optional) Url Key to use for bulding url.
* @param string $url_key (optional) Url Key to use for bulding url.
* @param string $endpoint (optional) API endpoint to override the request with.
* @param array $args (optional) Additional query arguments.
*
* @return string
*/
public function url( $url_key, $endpoint = '' ) {
public function url( $url_key, $endpoint = '', $args = [] ) {

if ( ! array_key_exists( $url_key, $this->urls ) ) {
return $this->url( 'www', $endpoint );
Expand All @@ -143,7 +145,7 @@ public function url( $url_key, $endpoint = '' ) {

}

return $this->add_query_args( trailingslashit( $url ) );
return $this->add_query_args( trailingslashit( $url ), $args, $url_key );

}

Expand Down
2 changes: 1 addition & 1 deletion includes/class-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function wp_enqueue_scripts() {
$data = [
'pl_id' => (int) rstore_get_option( 'pl_id' ),
'urls' => [
'cart_api' => esc_url_raw( rstore()->api->url( 'api', 'cart/{pl_id}' ) ),
'cart_api' => esc_url_raw( rstore()->api->url( 'cart_api' ) ),
'gui' => rstore()->api->url( 'gui' ),
],
'cookies' => [
Expand Down
76 changes: 50 additions & 26 deletions includes/functions/template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ function rstore_price( $post = null ) {
* @since 0.2.0
*
* @param int|WP_Post|null $post Product WP_Post instance.
* @param bool $echo (optional) Echo the text.
* @param string $button_label (optional) Text to display in the button.
* @param string $text_cart (optional) Text to display in the cart link.
* @param bool $redirect (optional) Redirect to cart after adding item.
*
* @return string|null
*/
function rstore_add_to_cart_form( $post, $button_label = null, $text_cart = null, $redirect = null ) {
function rstore_add_to_cart_form( $post, $echo = false, $button_label = null, $text_cart = null, $redirect = true ) {

$post = get_post( $post );

Expand All @@ -102,47 +103,66 @@ function rstore_add_to_cart_form( $post, $button_label = null, $text_cart = null
'quantity' => 1, // @TODO Future release.
];

if ( ! isset( $redirect ) ) {
if ( empty( $button_label ) ) {

$redirect = ! ( (bool) rstore_get_product_meta( $post->ID, 'skip_cart_redirect' ) );
$button_label = rstore_get_product_meta( $post->ID, 'add_to_cart_button_label' );

if ( empty( $button_label ) ) {

$button_label = esc_html__( 'Add to cart', 'reseller-store' );

}
}

$data['redirect'] = $redirect ? 'true' : 'false';
if ( $redirect ) {

if ( ! isset( $button_label ) ) {
$args['redirect'] = true;

$button_label = rstore_get_product_meta( $post->ID, 'add_to_cart_button_label' );
$cart_url = esc_url_raw( rstore()->api->url( 'api', 'cart/{pl_id}', $args ) );

if ( ! isset( $button_label ) ) {
$button_label = esc_html__( 'Add to cart', 'reseller-store' );
}
}
$items = json_encode( [ $data ] );

$cart_form = sprintf(
'<form class="rstore-add-to-cart-form" method="POST" action="%s" ><input type="hidden" name="items" value=\'%s\' /><button class="rstore-add-to-cart button btn btn-primary" type="submit">%s</button><div class="rstore-loading rstore-loading-hidden"></div></form>',
$cart_url,
$items,
esc_html( $button_label )
);

} else {

if ( ! isset( $text_cart ) ) {
if ( empty( $text_cart ) ) {

$text_cart = rstore_get_product_meta( $post->ID, 'cart_link_text' );
$text_cart = rstore_get_product_meta( $post->ID, 'cart_link_text' );

if ( ! isset( $text_cart ) ) {
if ( empty( $text_cart ) ) {

$text_cart = esc_html__( 'Continue to cart', 'reseller-store' );
$text_cart = esc_html__( 'Continue to cart', 'reseller-store' );

}
}

$cart_link = sprintf(
'<span class="dashicons dashicons-yes rstore-success"></span><a href="%s" rel="nofollow">%s</a>',
esc_url_raw( rstore()->api->url( 'cart' ), 'https' ),
esc_html( $text_cart )
);

$button = rstore_add_to_cart_button( $data, $button_label );

$cart_form = sprintf(
'<div class="rstore-add-to-cart-form">%s<div class="rstore-loading rstore-loading-hidden"></div><div class="rstore-cart rstore-cart-hidden">%s</div><div class="rstore-message rstore-message-hidden"></div></div>',
$button,
$cart_link
);

}

$cart_link = sprintf(
'<span class="dashicons dashicons-yes rstore-success"></span><a href="%s" rel="nofollow">%s</a>',
esc_url_raw( rstore()->api->url( 'cart' ), 'https' ),
esc_html( $text_cart )
);
if ( $echo ) {

$button = rstore_add_to_cart_button( $data, $button_label, $redirect );
echo $cart_form; // xss ok.

$cart_form = sprintf(
'<div class="rstore-add-to-cart-form">%s<div class="rstore-loading rstore-loading-hidden"></div><div class="rstore-cart rstore-cart-hidden">%s</div><div class="rstore-message rstore-message-hidden"></div></div>',
$button,
$cart_link
);
}

return $cart_form;

Expand Down Expand Up @@ -178,7 +198,9 @@ function rstore_append_add_to_cart_form( $content ) {
if ( \Reseller_Store\Post_Type::SLUG === $post->post_type && ! is_feed() && ! $is_rest_request ) {

$content .= rstore_price( $post->ID );
$content .= rstore_add_to_cart_form( $post->ID );

$redirect = ! ( (bool) rstore_get_product_meta( $post->ID, 'skip_cart_redirect' ) );
$content .= rstore_add_to_cart_form( $post->ID, false, null, null, $redirect );

}

Expand All @@ -201,7 +223,9 @@ function rstore_add_to_cart_button( $cart_vars, $button_label ) {
$cart_vars = apply_filters( 'rstore_cart_options', $cart_vars );

if ( ! is_array( $cart_vars ) ) {

return;

}

$output = '<div><button class="rstore-add-to-cart button btn btn-primary"';
Expand Down
4 changes: 2 additions & 2 deletions includes/widgets/class-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function widget( $args, $instance ) {

if ( ! empty( $data['button_label'] ) ) {

$content .= rstore_add_to_cart_form( $post_id, $data['button_label'], $data['text_cart'], $data['redirect'] ); // xss ok.
$content .= rstore_add_to_cart_form( $post_id, false, $data['button_label'], $data['text_cart'], $data['redirect'] ); // xss ok.

}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ public function widget( $args, $instance ) {

if ( ! empty( $data['button_label'] ) ) {

$content .= rstore_add_to_cart_form( $post_id, $data['button_label'], $data['text_cart'], $data['redirect'] ); // xss ok.
$content .= rstore_add_to_cart_form( $post_id, false, $data['button_label'], $data['text_cart'], $data['redirect'] );

}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
],
"scripts": {
"version": "grunt version && git add -A .",
"lint": "eslint --ext .js ./.dev/src/ ./assets/js",
"lint": "eslint --ext .js ./.dev/src/ ./assets/js --fix",
"prebuild": "webpack",
"build": "grunt build"
},
Expand Down

0 comments on commit 3780620

Please sign in to comment.