| 1 |
<?php |
| 2 |
/** |
| 3 |
* Product module |
| 4 |
* |
| 5 |
* @package |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Modules\Admin\Product; |
| 8 |
|
| 9 |
use WPFunnels\Admin\Module\Wpfnl_Admin_Module; |
| 10 |
use WPFunnels\Traits\SingletonTrait; |
| 11 |
use WPFunnels\Wpfnl_functions; |
| 12 |
use \WC_Subscriptions_Product; |
| 13 |
use Wpfnl_Type_Factory; |
| 14 |
class Module extends Wpfnl_Admin_Module |
| 15 |
{ |
| 16 |
use SingletonTrait; |
| 17 |
|
| 18 |
protected $products = []; |
| 19 |
|
| 20 |
public function get_view() |
| 21 |
{ |
| 22 |
require_once WPFNL_DIR . '/admin/modules/product/views/view.php'; |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
public function set_products($products) |
| 27 |
{ |
| 28 |
if (is_plugin_active('woocommerce/woocommerce.php')) { |
| 29 |
if ($products) { |
| 30 |
foreach ($products as $saved_product) { |
| 31 |
$product = wc_get_product($saved_product['id']); |
| 32 |
if (is_object($product)) { |
| 33 |
$product_name = $product->get_name(); |
| 34 |
$this->products[] = [ |
| 35 |
'id' => $saved_product['id'], |
| 36 |
'name' => $product_name, |
| 37 |
'quantity' => $saved_product['quantity'], |
| 38 |
]; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
public function get_products() |
| 46 |
{ |
| 47 |
return $this->products; |
| 48 |
} |
| 49 |
|
| 50 |
public function init_ajax() |
| 51 |
{ |
| 52 |
add_action('wp_ajax_wpfnl_product_search', [ $this, 'fetch_products' ]); |
| 53 |
add_action('wp_ajax_wpfnl_search_coupon', [ $this, 'fetch_coupons' ]); |
| 54 |
add_action('wp_ajax_wpfnl_product_search_gbf', [ $this, 'fetch_products_gbf' ]); |
| 55 |
add_action('wp_ajax_wpfnl_product_search_for_gbf_type', [ $this, 'fetch_specific_products_gbf' ]); |
| 56 |
add_action('wp_ajax_wpfnl_product_search_by_category_and_name', [ $this, 'wpfnl_product_search_by_category_and_name' ]); |
| 57 |
|
| 58 |
wp_ajax_helper()->handle('global-funnel-add-upsell-product') |
| 59 |
->with_callback([ $this, 'global_funnel_add_upsell_product' ]) |
| 60 |
->with_validation($this->get_validation_data()); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Fetch product from WC data store |
| 65 |
* |
| 66 |
* @throws \Exception |
| 67 |
* @since 1.0.0 |
| 68 |
*/ |
| 69 |
public function fetch_products() |
| 70 |
{ |
| 71 |
check_ajax_referer('wpfnl-admin', 'security'); |
| 72 |
if (isset($_GET['term'])) { |
| 73 |
$term = (string) esc_attr( wp_unslash($_GET['term']) ); |
| 74 |
} |
| 75 |
|
| 76 |
$term = isset($term) ? trim($term) : ''; |
| 77 |
$limit = isset($_GET['limit']) ? absint($_GET['limit']) : 0; |
| 78 |
|
| 79 |
// If no search term but a limit is set, return recent products |
| 80 |
if (empty($term) && $limit > 0) { |
| 81 |
$products = []; |
| 82 |
if ($_GET['isLms'] == 'true') { |
| 83 |
$_class = 'lms'; |
| 84 |
} else { |
| 85 |
$_class = 'wc'; |
| 86 |
} |
| 87 |
|
| 88 |
if ($_class === 'wc' && Wpfnl_functions::is_wc_active()) { |
| 89 |
$args = [ |
| 90 |
'status' => 'publish', |
| 91 |
'limit' => $limit, |
| 92 |
'orderby' => 'date', |
| 93 |
'order' => 'DESC', |
| 94 |
'stock_status' => 'instock', |
| 95 |
]; |
| 96 |
$wc_products = wc_get_products($args); |
| 97 |
|
| 98 |
foreach ($wc_products as $product_object) { |
| 99 |
if ($product_object && (($product_object->managing_stock() && $product_object->get_stock_quantity() > 0) || (!$product_object->managing_stock() && $product_object->get_stock_status() !== 'outofstock'))) { |
| 100 |
$formatted_name = $product_object->get_name(); |
| 101 |
|
| 102 |
if ($product_object->get_type() == 'variable' || $product_object->get_type() == 'variable-subscription') { |
| 103 |
$parent_id = $product_object->get_id(); |
| 104 |
$products[$parent_id] = [ |
| 105 |
'name' => $formatted_name, |
| 106 |
'price' => $product_object->get_variation_regular_price('min') ? $product_object->get_variation_regular_price('min') : $product_object->get_regular_price(), |
| 107 |
'sale_price' => $product_object->get_variation_sale_price('min') ? $product_object->get_variation_sale_price('min') : $product_object->get_price(), |
| 108 |
]; |
| 109 |
} else { |
| 110 |
$products[$product_object->get_id()] = [ |
| 111 |
'name' => rawurldecode($formatted_name), |
| 112 |
'price' => $product_object->get_regular_price(), |
| 113 |
'sale_price' => $product_object->get_sale_price(), |
| 114 |
]; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
wp_send_json($products); |
| 121 |
} |
| 122 |
|
| 123 |
if (empty($term)) { |
| 124 |
wp_die(); |
| 125 |
} |
| 126 |
$products = []; |
| 127 |
if( $_GET['isLms'] == 'true' ){ |
| 128 |
$_class = 'lms'; |
| 129 |
}else{ |
| 130 |
$_class = 'wc'; |
| 131 |
} |
| 132 |
$class_object = Wpfnl_Type_Factory::build($_class); |
| 133 |
if( $class_object ){ |
| 134 |
$products = $class_object->retrieve_item( $term ); |
| 135 |
} |
| 136 |
wp_send_json($products); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
/** |
| 141 |
* Fetch Coupons from WooCommerce or LMS data store via AJAX. |
| 142 |
* |
| 143 |
* This function handles the AJAX request to fetch coupons from either the WooCommerce (wc) |
| 144 |
* or Learning Management System (LMS) data store based on the provided term. It performs security checks, |
| 145 |
* processes the term, retrieves relevant coupons, and sends back the JSON response with the coupon data. |
| 146 |
* |
| 147 |
* @since 2.8.6 |
| 148 |
*/ |
| 149 |
public function fetch_coupons() |
| 150 |
{ |
| 151 |
// Verify the security nonce. |
| 152 |
check_ajax_referer('wpfnl-admin', 'security'); |
| 153 |
|
| 154 |
// Check if the 'term' parameter is set in the request. |
| 155 |
if (!isset($_GET['term'])) { |
| 156 |
wp_die(); |
| 157 |
} |
| 158 |
|
| 159 |
// Sanitize and process the term parameter. |
| 160 |
$term = (string) esc_attr(wp_unslash($_GET['term'])); |
| 161 |
|
| 162 |
// Check if the term is empty. |
| 163 |
if (empty($term)) { |
| 164 |
wp_die(); |
| 165 |
} |
| 166 |
|
| 167 |
$coupons = []; |
| 168 |
|
| 169 |
// Determine the class based on the 'isLms' parameter. |
| 170 |
if (sanitize_text_field($_GET['isLms']) == 'true') { |
| 171 |
$_class = 'lms'; |
| 172 |
} else { |
| 173 |
$_class = 'wc'; |
| 174 |
} |
| 175 |
|
| 176 |
// Build the appropriate class object based on the determined class. |
| 177 |
$class_object = Wpfnl_Type_Factory::build($_class); |
| 178 |
|
| 179 |
// If the class object is not available, terminate. |
| 180 |
if (!$class_object) { |
| 181 |
wp_die(); |
| 182 |
} |
| 183 |
|
| 184 |
// Retrieve coupons using the appropriate class object. |
| 185 |
$coupons = $class_object->retrieve_coupon($term); |
| 186 |
|
| 187 |
// Send JSON response with the fetched coupons. |
| 188 |
wp_send_json($coupons); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
/** |
| 193 |
* Fetch product from WC data store |
| 194 |
* |
| 195 |
* @throws \Exception |
| 196 |
* @since 1.0.0 |
| 197 |
*/ |
| 198 |
public function fetch_products_gbf() |
| 199 |
{ |
| 200 |
check_ajax_referer('wpfnl-admin', 'security'); |
| 201 |
if (isset($_GET['term'])) { |
| 202 |
$term = (string) esc_attr( wp_unslash($_GET['term']) ); |
| 203 |
} |
| 204 |
if (empty($term)) { |
| 205 |
wp_die(); |
| 206 |
} |
| 207 |
|
| 208 |
$products = []; |
| 209 |
$data_store = \WC_Data_Store::load('product'); |
| 210 |
$ids = $data_store->search_products($term, '', false, false, 10); |
| 211 |
|
| 212 |
$product_objects = array_filter(array_map('wc_get_product', $ids), 'wc_products_array_filter_readable'); |
| 213 |
if( is_array($product_objects) ){ |
| 214 |
foreach ($product_objects as $product_object) { |
| 215 |
|
| 216 |
if( $product_object && (( $product_object->managing_stock() && $product_object->get_stock_quantity() > 0 ) || ( !$product_object->managing_stock() && $product_object->get_stock_status() !== 'outofstock' )) ){ |
| 217 |
$formatted_name = $product_object->get_name(); |
| 218 |
if($product_object->get_type() == 'variable' || $product_object->get_type() == 'variable-subscription') { |
| 219 |
if( is_plugin_active( 'woocommerce-subscriptions/woocommerce-subscriptions.php' ) ){ |
| 220 |
$signUpFee = \WC_Subscriptions_Product::get_sign_up_fee( $product_object ); |
| 221 |
} |
| 222 |
$variations = $product_object->get_available_variations(); |
| 223 |
$isPro = Wpfnl_functions::is_wpfnl_pro_activated(); |
| 224 |
if($isPro){ |
| 225 |
$parent_id = $product_object->get_id(); |
| 226 |
$products[] = [ |
| 227 |
'name' => $formatted_name, |
| 228 |
'id' => $parent_id, |
| 229 |
'regular_price' => $product_object->get_regular_price(), |
| 230 |
'sale_price' => $product_object->get_sale_price(), |
| 231 |
]; |
| 232 |
} |
| 233 |
if( is_array($variations ) ){ |
| 234 |
foreach ($variations as $variation) { |
| 235 |
$variation_product = wc_get_product($variation['variation_id']); |
| 236 |
if( $variation_product ){ |
| 237 |
if( ( $variation_product->managing_stock() && $variation_product->get_stock_quantity() > 0 ) || ( !$variation_product->managing_stock() && $variation_product->get_stock_status() !== 'outofstock' ) ){ |
| 238 |
$products[] = [ |
| 239 |
|
| 240 |
'name' => Wpfnl_functions::get_formated_product_name( $variation_product ), |
| 241 |
'id' => $variation['variation_id'], |
| 242 |
'regular_price' => $variation['display_price'], |
| 243 |
'sale_price' => $variation['display_regular_price'], |
| 244 |
]; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
}else { |
| 251 |
$products[] = [ |
| 252 |
'name' => rawurldecode($formatted_name), |
| 253 |
'id' => $product_object->get_id(), |
| 254 |
'regular_price' => $product_object->get_regular_price(), |
| 255 |
'sale_price' => $product_object->get_sale_price(), |
| 256 |
]; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
wp_send_json($products); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Fetch specific product from WC data store for gbf type |
| 267 |
* |
| 268 |
* @return [type] |
| 269 |
*/ |
| 270 |
public function fetch_specific_products_gbf() |
| 271 |
{ |
| 272 |
check_ajax_referer('wpfnl-admin', 'security'); |
| 273 |
if (isset($_GET['term'])) { |
| 274 |
$term = (string) esc_attr( wp_unslash($_GET['term']) ); |
| 275 |
} |
| 276 |
if (empty($term)) { |
| 277 |
wp_die(); |
| 278 |
} |
| 279 |
|
| 280 |
$products = []; |
| 281 |
$data_store = \WC_Data_Store::load('product'); |
| 282 |
$ids = $data_store->search_products($term, '', false, false, 10); |
| 283 |
|
| 284 |
$product_objects = array_filter(array_map('wc_get_product', $ids), 'wc_products_array_filter_readable'); |
| 285 |
if( $product_objects ){ |
| 286 |
foreach ($product_objects as $product_object) { |
| 287 |
if( ( $product_object->managing_stock() && $product_object->get_stock_quantity() > 0 ) || ( !$product_object->managing_stock() && $product_object->get_stock_status() !== 'outofstock' ) ){ |
| 288 |
$formatted_name = $product_object->get_name(); |
| 289 |
if($product_object->get_type() == 'variable' || $product_object->get_type() == 'variable-subscription') { |
| 290 |
if( is_plugin_active( 'woocommerce-subscriptions/woocommerce-subscriptions.php' ) ){ |
| 291 |
$signUpFee = \WC_Subscriptions_Product::get_sign_up_fee( $product_object ); |
| 292 |
} |
| 293 |
$variations = $product_object->get_available_variations(); |
| 294 |
$parent_id = $product_object->get_id(); |
| 295 |
$products[] = [ |
| 296 |
'name' => $formatted_name, |
| 297 |
'id' => $parent_id, |
| 298 |
'regular_price' => $product_object->get_regular_price(), |
| 299 |
'sale_price' => $product_object->get_sale_price(), |
| 300 |
]; |
| 301 |
if( !empty($variations) ){ |
| 302 |
foreach ($variations as $variation) { |
| 303 |
$variation_product = wc_get_product($variation['variation_id']); |
| 304 |
if( $variation_product ){ |
| 305 |
if( ( $variation_product->managing_stock() && $variation_product->get_stock_quantity() > 0 ) || ( !$variation_product->managing_stock() && $variation_product->get_stock_status() !== 'outofstock' ) ){ |
| 306 |
$products[] = [ |
| 307 |
|
| 308 |
'name' => Wpfnl_functions::get_formated_product_name( $variation_product ), |
| 309 |
'id' => $variation['variation_id'], |
| 310 |
'regular_price' => $variation['display_price'], |
| 311 |
'sale_price' => $variation['display_regular_price'], |
| 312 |
]; |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
}else { |
| 319 |
$products[] = [ |
| 320 |
'name' => rawurldecode($formatted_name), |
| 321 |
'id' => $product_object->get_id(), |
| 322 |
'regular_price' => $product_object->get_regular_price(), |
| 323 |
'sale_price' => $product_object->get_sale_price(), |
| 324 |
]; |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
wp_send_json($products); |
| 330 |
} |
| 331 |
|
| 332 |
public function get_name() |
| 333 |
{ |
| 334 |
return 'product'; |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Wpfnl_product_search_by_category_and_name |
| 339 |
*/ |
| 340 |
public function wpfnl_product_search_by_category_and_name(){ |
| 341 |
check_ajax_referer('wpfnl-admin', 'security'); |
| 342 |
$category_id = $_GET['category']; |
| 343 |
$product_name = $_GET['term']; |
| 344 |
$args = array( |
| 345 |
'post_status' => 'publish', |
| 346 |
'posts_per_page' => -1, |
| 347 |
'post_type' => ['product','product_variation'], |
| 348 |
's' => $product_name, |
| 349 |
); |
| 350 |
$products = get_posts($args); |
| 351 |
if(empty($products)){ |
| 352 |
$data = [ |
| 353 |
'status' => 'success', |
| 354 |
'data' => 'Product not found', |
| 355 |
]; |
| 356 |
}else{ |
| 357 |
foreach($products as $key => $product){ |
| 358 |
$_product = wc_get_product($product->ID); |
| 359 |
if( $_product ){ |
| 360 |
$products[$key]->price = $_product->get_price(); |
| 361 |
} |
| 362 |
} |
| 363 |
$data = [ |
| 364 |
'status' => 'success', |
| 365 |
'data' => $products, |
| 366 |
]; |
| 367 |
} |
| 368 |
wp_send_json($data); |
| 369 |
} |
| 370 |
} |
| 371 |
|