Skip to content

Commit

Permalink
Merge pull request #240 from addonify/origin/diwakar
Browse files Browse the repository at this point in the history
Updated: API url updated to new version and deprecated function get_p…
  • Loading branch information
h1dd3nsn1p3r authored Mar 30, 2023
2 parents 6327111 + 06d72e4 commit 4f10e4e
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 22 deletions.
8 changes: 4 additions & 4 deletions includes/class-addonify-wishlist-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function register_rest_endpoints() {
);

register_rest_route( // Update options.
$this->rest_namespace,
$this->rest_namespace_v2,
'/update_options',
array(
'methods' => \WP_REST_Server::CREATABLE,
Expand All @@ -89,7 +89,7 @@ public function register_rest_endpoints() {
);

register_rest_route( // Reset options.
$this->rest_namespace,
$this->rest_namespace_v2,
'/reset_options',
array(
'methods' => \WP_REST_Server::CREATABLE,
Expand All @@ -99,7 +99,7 @@ public function register_rest_endpoints() {
);

register_rest_route( // Export options.
$this->rest_namespace,
$this->rest_namespace_v2,
'/export_options',
array(
'methods' => \WP_REST_Server::READABLE,
Expand All @@ -109,7 +109,7 @@ public function register_rest_endpoints() {
);

register_rest_route( // Import options.
$this->rest_namespace,
$this->rest_namespace_v2,
'/import_options',
array(
'methods' => \WP_REST_Server::CREATABLE,
Expand Down
2 changes: 2 additions & 0 deletions includes/class-addonify-wishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ private function load_dependencies() {
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/db/class-wishlist.php';

require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-adfy-wishlist.php';

/**
* The class responsible for orchestrating the actions and filters of the
* core plugin.
Expand Down
99 changes: 97 additions & 2 deletions includes/class-adfy-wishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,26 @@ class Adfy_Wishlist {
*/
public $wishlist_items_count;

/**
* Default Public wishlist.
*
* @since 1.1.0
* @access private
* @var string $default_wishlist
*/
private $default_wishlist = 'Default Wishlist';

/**
* Stores this Object instance.
*
* @var array
*/
private static $instance;

/**
* Class constructor.
*/
public function __construct() {
private function __construct() {
global $wishlist;
$this->wishlist = $wishlist;

Expand All @@ -50,6 +66,13 @@ public function __construct() {
$this->wishlist_items_count = $this->get_wishlist_count();
}

public static function get_instance() {
if ( ! ( is_array( self::$instance ) && array_key_exists( 'instance', self::$instance ) ) ) {
self::$instance['instance'] = new Adfy_Wishlist();
}
return self::$instance['instance'];
}

/**
* Returns wishlist.
*/
Expand Down Expand Up @@ -99,8 +122,12 @@ public function get_wishlist_count() {
* @param int $product_id Product ID.
* @return boolean True if saved, false otherwise.
*/
public function save_in_wishlist( $wishlist_id, $product_id ) {
public function add_to_wishlist( $wishlist_id = false, $product_id ) {

if ( $wishlist_id === false ) {
$wishlist_id = $this->get_default_wishlist_id();
}

$return_boolean = false;

do_action( 'addonify_wishlist_before_adding_to_wishlist' );
Expand Down Expand Up @@ -128,4 +155,72 @@ public function save_in_wishlist( $wishlist_id, $product_id ) {
return $return_boolean;
}

/**
* Get default wishlist id.
*
* @return int
*/
private function get_default_wishlist_id() {
foreach ( $this->wishlist_items as $index => $item ) {
if ( $item['name'] === $this->default_wishlist ) {
return $index;
}
}
return false;
}

/**
* Remove product from the wishlist if product is already in the wishlist.
*
* @since 1.0.0
* @param int $product_id Product ID.
* @param int $parent_wishlist_id Wishlist ID.
* @return boolean true if removed successfully otherwise false.
*/
public function remove_from_wishlist( $product_id, $parent_wishlist_id = false ) {
if ( $parent_wishlist_id ) {
if ( array_key_exists( $parent_wishlist_id, $this->wishlist_items ) ) {
if ( array_key_exists( 'product_ids', $this->wishlist_items[ $parent_wishlist_id ] ) && in_array( (int) $product_id, $this->wishlist_items[ $parent_wishlist_id ]['product_ids'], true ) ) {
$where = array(
'parent_wishlist_id' => $parent_wishlist_id,
'product_id' => $product_id,
'user_id' => get_current_user_id(),
'site_url' => get_bloginfo( 'url' ),
);
$this->wishlist->delete_where( $where );
unset( $this->wishlist_items[ $parent_wishlist_id ]['product_ids'][ array_search( (int) $product_id, $this->wishlist_items[ $parent_wishlist_id ]['product_ids'], true ) ] );
$this->wishlist_items_count = $this->get_wishlist_count();
return true;
}
}
} else {
foreach ( $this->wishlist_items as $index => $item ) {
if ( array_key_exists( 'product_ids', $item ) && in_array( (int) $product_id, $item['product_ids'], true ) ) {
$where = array(
'parent_wishlist_id' => $index,
'product_id' => $product_id,
'user_id' => get_current_user_id(),
'site_url' => get_bloginfo( 'url' ),
);
$this->wishlist->delete_where( $where );
unset( $this->wishlist_items[ $index ]['product_ids'][ array_search( (int) $product_id, $this->wishlist_items[ $index ]['product_ids'], true ) ] );
$this->wishlist_items_count = $this->get_wishlist_count();
return true;
}
}
}
return false;
}

}

global $adfy_wishlist;
add_action(
'plugins_loaded',
function () {
global $adfy_wishlist, $wishlist;
if ( $wishlist->check_wishlist_table_exists() ) {
$adfy_wishlist = Adfy_Wishlist::get_instance();
}
}
);
24 changes: 24 additions & 0 deletions includes/setting-functions/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ function addonify_wishlist_get_pages() {
}
}

if ( ! function_exists( 'addonify_wishlist_get_shop_page_title' ) ) {
/**
* Get the list of pages.
*
* @since 1.0.0
* @return array $page_list
*/
function addonify_wishlist_get_shop_page_title() {

$pages = get_pages();

if ( ! empty( $pages ) ) {

foreach ( $pages as $page ) {

if ( $page->post_title === 'Shop' ) {
return $page->post_title;
}
}
}

return '';
}
}

if ( ! function_exists( 'addonify_wishlist_get_sidebar_icons' ) ) {
/**
Expand Down
9 changes: 1 addition & 8 deletions includes/setting-functions/settings-v2.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@
*/
function addonify_wishlist_settings_defaults( $setting_id = '' ) {

$shop = get_page_by_title( 'Shop' );
if ( null !== $shop ) {
$page = 'Shop';
} else {
$page = '';
}

$defaults = apply_filters(
'addonify_wishlist_setting_defaults',
array(
Expand Down Expand Up @@ -80,7 +73,7 @@ function addonify_wishlist_settings_defaults( $setting_id = '' ) {
'undo_notice_timeout' => 5,
'icon_position' => 'left',
'show_empty_wishlist_navigation_link' => true,
'empty_wishlist_navigation_link' => $page,
'empty_wishlist_navigation_link' => addonify_wishlist_get_shop_page_title(),
'empty_wishlist_navigation_link_label' => __( 'Go to Shop', 'addonify-wishlist' ),
'login_required_message' => __( 'Please login before adding item to Wishlist', 'addonify-wishlist' ),
'could_not_add_to_wishlist_error_description' => __( 'Something went wrong. <br>{product_name} was not added to wishlist. Please refresh page and try again.', 'addonify-wishlist' ),
Expand Down
9 changes: 1 addition & 8 deletions includes/setting-functions/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@
*/
function addonify_wishlist_settings_defaults( $setting_id = '' ) {

$shop = get_page_by_title( 'Shop' );
if ( null !== $shop ) {
$page = 'Shop';
} else {
$page = '';
}

$defaults = apply_filters(
'addonify_wishlist_setting_defaults',
array(
Expand Down Expand Up @@ -81,7 +74,7 @@ function addonify_wishlist_settings_defaults( $setting_id = '' ) {
'undo_notice_timeout' => 5,
'icon_position' => 'left',
'show_empty_wishlist_navigation_link' => true,
'empty_wishlist_navigation_link' => $page,
'empty_wishlist_navigation_link' => addonify_wishlist_get_shop_page_title(),
'empty_wishlist_navigation_link_label' => __( 'Go to Shop', 'addonify-wishlist' ),
'login_required_message' => __( 'Please login before adding item to Wishlist', 'addonify-wishlist' ),
'could_not_add_to_wishlist_error_description' => __( 'Something went wrong. <br>{product_name} was not added to wishlist. Please refresh page and try again.', 'addonify-wishlist' ),
Expand Down

0 comments on commit 4f10e4e

Please sign in to comment.