Skip to content

Commit

Permalink
Code review fixes. Fixed add to cart issue.
Browse files Browse the repository at this point in the history
Applied latest improved PHPCS rules.
Add to cart was not working when added through wishlist due to wp_send_json function. Added condition to check ajax and normal request.
  • Loading branch information
uday-kokitkar committed Mar 13, 2020
1 parent 871864f commit f2c40be
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 52 deletions.
2 changes: 1 addition & 1 deletion assets/css/admin/wlffwc-admin-main.css

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

2 changes: 1 addition & 1 deletion assets/css/dev/admin/wlffwc-admin-main.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
border-top: solid 1px #ccc;
margin: 10px 0;
}
.#wlffwc_global_settings input {
#wlffwc_global_settings input {
width: 400px;
}
7 changes: 4 additions & 3 deletions assets/css/dev/public/wlffwc-wishlist.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
border-collapse: collapse;
text-align: left;
width: 100%;
font-size: 0.8rem;
/*font-size: 0.8rem;*/
font-size: 12px;
}
#wlffwc-wishlist-wrapper .wlffwc-wishlist-tbl .mobile-th {
background: #eee;
Expand All @@ -27,11 +28,11 @@
max-width: 50px;
}
#wlffwc-wishlist-wrapper .wlffwc-wishlist-tbl .price-increase {
font-size: 0.75rem;
font-size: 10px;
color: #bb5e5e;
}
#wlffwc-wishlist-wrapper .wlffwc-wishlist-tbl .price-decrease {
font-size: 0.75rem;
font-size: 10px;
color: #306f35;
}
#wlffwc-wishlist-wrapper .wlffwc-wishlist-tbl .product-actions .date-added {
Expand Down
2 changes: 1 addition & 1 deletion assets/css/public/wlffwc-wishlist.css

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

23 changes: 14 additions & 9 deletions includes/class-wishlist-feature-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,18 @@ public static function instance() {
* @since 1.0.0
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, esc_html( __( 'Cloning is forbidden.', 'wishlist-feature-for-woocommerce' ) ), '1.0.0' );
if ( function_exists( 'wc_doing_it_wrong' ) ) {
wc_doing_it_wrong( __FUNCTION__, esc_html( __( 'Cloning is forbidden.', 'wishlist-feature-for-woocommerce' ) ), '1.0.0' );
}
}

/**
* Unserializing instances of this class is forbidden.
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, esc_html( __( 'Unserializing instances of this class is forbidden.', 'wishlist-feature-for-woocommerce' ) ), '1.0.0' );
if ( function_exists( 'wc_doing_it_wrong' ) ) {
wc_doing_it_wrong( __FUNCTION__, esc_html( __( 'Unserializing instances of this class is forbidden.', 'wishlist-feature-for-woocommerce' ) ), '1.0.0' );
}
}

/**
Expand Down Expand Up @@ -156,7 +160,7 @@ public function init() {
return;
}

/*
/**
|
| Classes loaded for the dashboard only.
|
Expand All @@ -165,14 +169,14 @@ public function init() {
$this->admin_init();
}

/*
/**
|
| Classes loaded for the frontend and for ajax requests.
|
*/
if ( $this->is_request( 'frontend' ) ) {

/*
/**
|
| Classes loaded for Shortcodes.
|
Expand Down Expand Up @@ -227,6 +231,7 @@ public function build_dependencies_notice() {
<h3>
<?php
printf(
/* translators: Plugin name, Minimum PHP version, Current PHP version, Plugin name */
__(
'The <strong>%1$s</strong> requires PHP version %2$s or higher. Because you are using an unsupported version of PHP (%3$s), the <strong>%4$s</strong> plugin will not initialize. Please contact your hosting company to upgrade to PHP.'
),
Expand All @@ -239,15 +244,15 @@ public function build_dependencies_notice() {
</h3>
</div>
<?php
} else if ( ! defined( 'WC_PLUGIN_FILE' ) ) {
} elseif ( ! defined( 'WC_PLUGIN_FILE' ) ) {
// Show notice if WooCommerce is not active.
?>
<div class="notice notice-error">
<p>
<?php
$install_wc_url = admin_url( 'plugin-install.php?s=woocommerce&tab=search' );

printf(
/* translators: plugin name, Installation URL. */
__(
'The <strong>%1$s</strong> requires WooCommerce to be activated ! <a href="%2$s">Install / Activate WooCommerce</a>'
),
Expand Down Expand Up @@ -325,8 +330,8 @@ public function is_rest_api_request() {
return false;
}

$r_prefix = trailingslashit( rest_get_url_prefix() );
$is_rest_api_request = ( false !== strpos( $_SERVER['REQUEST_URI'], $r_prefix ) );
$r_prefix = trailingslashit( rest_get_url_prefix() );
$is_rest_api_request = ( false !== strpos( $_SERVER['REQUEST_URI'], $r_prefix ) );

return $is_rest_api_request;
}
Expand Down
52 changes: 29 additions & 23 deletions includes/class-wlffwc-wishlist-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ public function localize_data() {
return apply_filters(
'wlffwc_localize_wishlist',
array(
'ajax_url' => admin_url( 'admin-ajax.php', 'relative' ),
'ajax_loader' => WLFFWC_PLUGIN_URL . 'assets/images/ajax-loader.svg',
'labels' => array(
'ajax_url' => admin_url( 'admin-ajax.php', 'relative' ),
'ajax_loader' => WLFFWC_PLUGIN_URL . 'assets/images/ajax-loader.svg',
'labels' => array(
'added_to_cart_message' => sprintf( '<div class="woocommerce-message" role="alert"><a href="%s" class="button wc-forward">%s</a> %s</div>', esc_url( wc_get_cart_url() ), __( 'View cart', 'wishlist-feature-for-woocommerce' ), __( 'A product has been added to your cart.', 'wishlist-feature-for-woocommerce' ) ),
),
'wlffwc_add_nonce' => wp_create_nonce( 'wlffwc-add-ajax-' . get_current_user_id() ),
'wlffwc_add_nonce' => wp_create_nonce( 'wlffwc-add-ajax-' . get_current_user_id() ),
'wlffwc_remove_nonce' => wp_create_nonce( 'wlffwc-remove-ajax-' . get_current_user_id() ),
'actions' => array(
'add_to_wishlist_action' => 'wlffwc_add_to_wishlist',
'actions' => array(
'add_to_wishlist_action' => 'wlffwc_add_to_wishlist',
'remove_from_wishlist_action' => 'wlffwc_remove_from_wishlist',
),
'plugin_settings' => array(
'browse_url' => get_permalink( $plugin_settings['wlffwc_wishlist_page'] ),
'plugin_settings' => array(
'browse_url' => get_permalink( $plugin_settings['wlffwc_wishlist_page'] ),
'wishlist_alt_string' => $plugin_settings['wlffwc_wishlist_text'],
),
)
Expand All @@ -116,7 +116,7 @@ public function localize_data() {
* @since 1.0.0
* @return int Wishlist ID. Returns an existing one or newly created.
*/
private function create_wishlist( int $user_id = 0 ) {
private function create_wishlist( $user_id = 0 ) {

if ( 0 === $user_id ) {
$user_id = get_current_user_id();
Expand Down Expand Up @@ -148,7 +148,7 @@ private function create_wishlist( int $user_id = 0 ) {

$default_data['wishlist_name'] = ( ! empty( $default_data['wishlist_name'] ) ) ? strip_tags( $default_data['wishlist_name'] ) : __( 'My wishlist', 'wishlist-feature-for-woocommerce' );

$default_data['privacy'] = ( in_array( $default_data['privacy'], array( 0, 1 ) ) ) ? $default_data['privacy'] : 0;
$default_data['privacy'] = ( in_array( $default_data['privacy'], array( 0, 1 ), true ) ) ? $default_data['privacy'] : 0;

if ( $error_in_data ) {
return new WP_Error( 'wlffwc', __( 'Wishlist data is not valid.', 'wishlist-feature-for-woocommerce' ) );
Expand Down Expand Up @@ -194,7 +194,7 @@ private function create_wishlist( int $user_id = 0 ) {
*/
public function add( $atts = array() ) {

$row_id = 0;
$row_id = 0;
$ajax_response = array(
'success' => false,
'message' => __( 'Something went wrong. Please refresh a page and try again.', 'wishlist-feature-for-woocommerce' ),
Expand Down Expand Up @@ -249,6 +249,7 @@ public function add( $atts = array() ) {

$ajax_response['success'] = true;
$ajax_response['message'] = sprintf(
/* translators: Plugin settings page, wishlist text */
__(
'A product is successfully added! <a href="%1$s">Browse %2$s</a>'
),
Expand Down Expand Up @@ -276,7 +277,7 @@ public function add( $atts = array() ) {
*
* @return int Row ID.
*/
private function add_product_to_wishlist( WC_Product $product, int $wishlist_id ) {
private function add_product_to_wishlist( WC_Product $product, $wishlist_id ) {

global $wpdb;

Expand Down Expand Up @@ -305,13 +306,14 @@ private function add_product_to_wishlist( WC_Product $product, int $wishlist_id
/**
* Remove an entry from the wishlist.
*
* @param array $atts Array of parameters; when not passed, parameters will be retrieved from $_REQUEST.
* @param array $atts Array of parameters; when not passed, parameters will be retrieved from $_REQUEST.
* @param boolean $is_ajax To check if this is the ajax request. Default is true.
*
* @since 1.0.0
* @return boolean True if removed.
*/
public function remove( $atts = array() ) {
$row_id = 0;
public function remove( $atts = array(), $is_ajax = true ) {
$row_id = 0;
$ajax_response = array(
'success' => false,
'message' => __( 'Something went wrong. Please refresh a page and try again.', 'wishlist-feature-for-woocommerce' ),
Expand All @@ -330,7 +332,11 @@ public function remove( $atts = array() ) {
}

if ( empty( $atts ) ) {
wp_send_json_error();
if ( $is_ajax ) {
wp_send_json_error();
} else {
return $ajax_response;
}
}

if ( ! get_current_user_id() ) {
Expand Down Expand Up @@ -362,7 +368,7 @@ public function remove( $atts = array() ) {
}

// Response to ajax request.
if ( ! empty( $_POST ) ) {
if ( $is_ajax ) {
wp_send_json(
$ajax_response
);
Expand All @@ -378,13 +384,13 @@ public function remove( $atts = array() ) {
* @param int $wishlist_id Wishlist to be updated.
* @return boolean True on success.
*/
private function remove_product_from_wishlist( int $product_id, int $wishlist_id ) {
private function remove_product_from_wishlist( $product_id, $wishlist_id ) {
global $wpdb;

$res = $wpdb->delete(
$wpdb->prefix . 'wlffwc_list_details',
array(
'product_id' => $product_id,
'product_id' => $product_id,
'wishlist_id' => $wishlist_id,
)
);
Expand All @@ -408,8 +414,8 @@ public function remove_from_wishlist_after_add_to_cart() {
$product_id = sanitize_text_field( wp_unslash( $_REQUEST['wlffwc_remove_after_adding_cart'] ) );

// To confirm that we are going to remove the product that is added in the cart.
if ( isset( $_REQUEST['product_id'] ) && $product_id == $_REQUEST['product_id'] ) {
$this->remove( array( 'product_id' => $product_id ) );
if ( isset( $_REQUEST['product_id'] ) && $product_id === $_REQUEST['product_id'] ) {
$this->remove( array( 'product_id' => $product_id ), false );
}
}
}
Expand All @@ -422,7 +428,7 @@ public function remove_from_wishlist_after_add_to_cart() {
* @param int $user_id User ID.
* @return int Wishlist ID from the database, empty if not found.
*/
public function get_wishlist_id( int $user_id = 0 ) {
public function get_wishlist_id( $user_id = 0 ) {
if ( 0 === $user_id ) {
return 0;
}
Expand Down Expand Up @@ -451,7 +457,7 @@ public function get_wishlist_id( int $user_id = 0 ) {
*
* @return bool
*/
public function is_product_in_wishlist( int $product_id, int $user_id, int $wishlist_id = 0 ) {
public function is_product_in_wishlist( $product_id, $user_id, $wishlist_id = 0 ) {

$response = false;

Expand Down
26 changes: 17 additions & 9 deletions includes/templates/admin/wlffwc-global-settings.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
</th>
<td class="forminp forminp-text">
<input
name = "wlffwc_wishlist_text"
id = "wlffwc_wishlist_text"
type = "text"
value = "<?php echo esc_attr( wp_unslash( $global_settings['wlffwc_wishlist_text'] ) ); ?>"
class = ""
placeholder = "<?php echo esc_attr( wp_unslash( $default_settings['wlffwc_wishlist_text'] ) ); ?>"
/>
name = "wlffwc_wishlist_text"
id = "wlffwc_wishlist_text"
type = "text"
value = "<?php echo esc_attr( wp_unslash( $global_settings['wlffwc_wishlist_text'] ) ); ?>"
class = ""
placeholder = "<?php echo esc_attr( wp_unslash( $default_settings['wlffwc_wishlist_text'] ) ); ?>"
/>
<p class="description"><?php esc_html_e( 'A "wishlist" text. Changing this text will take effect on frontend only.', 'wishlist-feature-for-woocommerce' ); ?></p>
</td>
</tr>
Expand All @@ -49,10 +49,18 @@ class = ""
<?php
foreach ( $wp_pages as $w_page ) {
$sel = '';
if ( $w_page->ID == $global_settings['wlffwc_wishlist_page'] ) {
if ( $w_page->ID === $global_settings['wlffwc_wishlist_page'] ) {
$sel = ' selected="selected"';
}
echo '<option value="' . esc_attr( $w_page->ID ) . '"' . $sel . '>' . esc_html( $w_page->post_title ) . '</option>';
echo wp_kses(
'<option value="' . esc_attr( $w_page->ID ) . '"' . $sel . '>' . esc_html( $w_page->post_title ) . '</option>',
array(
'option' => array(
'value' => array(),
'selected' => array(),
),
)
);
}
?>
</select>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
defined( 'ABSPATH' ) || exit;
?>
<div class="wlffwc-add-to-wishlist-wrap">
<a href="<?php echo esc_url( add_query_arg( 'wlffwc_add_to_wishlist', $product->get_id() ) ); ?>" rel="nofollow" data-product_id="<?php echo $product->get_id(); ?>" data-product_type="<?php echo $product->get_type(); ?>" class="add-to-wishlist-link">
<a href="<?php echo esc_url( add_query_arg( 'wlffwc_add_to_wishlist', $product->get_id() ) ); ?>" rel="nofollow" data-product_id="<?php echo esc_attr( $product->get_id() ); ?>" data-product_type="<?php echo esc_attr( $product->get_type() ); ?>" class="add-to-wishlist-link">
<span><?php echo esc_attr( $link_label ); ?></span>
</a>
</div>
Loading

0 comments on commit f2c40be

Please sign in to comment.