[ Avaa Bypassed ]




Upload:

Command:

hmhc3928@3.145.66.195: ~ $
<?php
/**
 * Manage Cart
 *
 * @package Tutor\Ecommerce
 * @author Themeum
 * @link https://themeum.com
 * @since 3.0.0
 */

namespace Tutor\Ecommerce;

use Tutor\Helpers\HttpHelper;
use TUTOR\Input;
use Tutor\Models\CartModel;
use Tutor\Traits\JsonResponse;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * CartController class
 *
 * @since 3.0.0
 */
class CartController {

	/**
	 * Page slug for cart page
	 *
	 * @since 3.0.0
	 *
	 * @var string
	 */
	public const PAGE_SLUG = 'cart';

	/**
	 * Page slug for cart page
	 *
	 * @since 3.0.0
	 *
	 * @var string
	 */
	public const PAGE_ID_OPTION_NAME = 'tutor_cart_page_id';

	/**
	 * Cart model
	 *
	 * @since 3.0.0
	 *
	 * @var CartModel
	 */
	private $model;

	/**
	 * Trait for sending JSON response
	 */
	use JsonResponse;

	/**
	 * Constructor.
	 *
	 * Initializes the Cart class, sets the page title, and optionally registers
	 * hooks for handling AJAX requests related to cart data, bulk actions, cart updates,
	 * and cart deletions.
	 *
	 * @param bool $register_hooks Whether to register hooks for handling requests. Default is true.
	 *
	 * @since 3.0.0
	 *
	 * @return void
	 */
	public function __construct( $register_hooks = true ) {
		$this->model = new CartModel();

		if ( $register_hooks ) {
			/**
			 * Handle AJAX request for adding course to cart
			 *
			 * @since 3.0.0
			 */
			add_action( 'wp_ajax_tutor_add_course_to_cart', array( $this, 'add_course_to_cart' ) );

			/**
			 * Handle AJAX request for deleting course from cart
			 *
			 * @since 3.0.0
			 */
			add_action( 'wp_ajax_tutor_delete_course_from_cart', array( $this, 'delete_course_from_cart' ) );
		}
	}

	/**
	 * Create cart page
	 *
	 * @since 3.0.0
	 *
	 * @return void
	 */
	public static function create_cart_page() {
		$page_id = self::get_page_id();
		if ( ! $page_id ) {
			$args = array(
				'post_title'   => ucfirst( self::PAGE_SLUG ),
				'post_content' => '',
				'post_type'    => 'page',
				'post_status'  => 'publish',
			);

			$page_id = wp_insert_post( $args );
			tutor_utils()->update_option( self::PAGE_ID_OPTION_NAME, $page_id );
		}
	}

	/**
	 * Get cart page url
	 *
	 * @since 3.0.0
	 *
	 * @return string
	 */
	public static function get_page_url() {
		return get_post_permalink( self::get_page_id() );
	}

	/**
	 * Get cart page ID
	 *
	 * @since 3.0.0
	 *
	 * @return string
	 */
	public static function get_page_id() {
		return (int) tutor_utils()->get_option( self::PAGE_ID_OPTION_NAME );
	}

	/**
	 * Get cart items
	 *
	 * @since 3.0.0
	 *
	 * @return array
	 */
	public function get_cart_items() {
		$user_id = tutils()->get_user_id();
		return $this->model->get_cart_items( $user_id );
	}

	/**
	 * Get cart count.
	 *
	 * @since 3.0.0
	 *
	 * @param int $user_id logged in user_id.
	 *
	 * @return int
	 */
	public function get_user_cart_item_count( $user_id = 0 ) {
		if ( ! $user_id ) {
			$user_id = tutils()->get_user_id();
		}
		$cart_items = $this->model->get_cart_items( $user_id );
		$cart_count = $cart_items['courses']['total_count'];
		return $cart_count;
	}

	/**
	 * Add course to cart
	 *
	 * @since 3.0.0
	 *
	 * @return void JSON response
	 */
	public function add_course_to_cart() {
		if ( ! tutor_utils()->is_nonce_verified() ) {
			$this->json_response(
				tutor_utils()->error_message( 'nonce' ),
				null,
				HttpHelper::STATUS_BAD_REQUEST
			);
		}

		$user_id   = tutils()->get_user_id();
		$course_id = Input::post( 'course_id', 0, Input::TYPE_INT );

		if ( ! $course_id ) {
			$this->json_response(
				__( 'Invalid course id.', 'tutor' ),
				null,
				HttpHelper::STATUS_BAD_REQUEST
			);
		}

		// Check if the course already exists in the cart or not.
		$is_course_in_user_cart = $this->model->is_course_in_user_cart( $user_id, $course_id );
		if ( $is_course_in_user_cart ) {
			$this->json_response(
				__( 'The course is already in the cart.', 'tutor' ),
				null,
				HttpHelper::STATUS_BAD_REQUEST
			);
		}

		$response = $this->model->add_course_to_cart( $user_id, $course_id );

		if ( $response ) {
			$this->json_response(
				__( 'The course was added to the cart successfully.', 'tutor' ),
				array(
					'cart_page_url' => self::get_page_url(),
					'cart_count'    => self::get_user_cart_item_count( $user_id ),
				),
				HttpHelper::STATUS_CREATED
			);
		} else {
			$this->json_response(
				__( 'Failed to add to cart.', 'tutor' ),
				null,
				HttpHelper::STATUS_BAD_REQUEST
			);
		}
	}

	/**
	 * Delete course from cart
	 *
	 * @since 3.0.0
	 *
	 * @return void JSON response
	 */
	public function delete_course_from_cart() {
		if ( ! tutor_utils()->is_nonce_verified() ) {
			$this->json_response(
				tutor_utils()->error_message( 'nonce' ),
				null,
				HttpHelper::STATUS_BAD_REQUEST
			);
		}

		$user_id   = tutils()->get_user_id();
		$course_id = Input::post( 'course_id', 0, Input::TYPE_INT );

		if ( ! $course_id ) {
			$this->json_response(
				__( 'Invalid course id.', 'tutor' ),
				null,
				HttpHelper::STATUS_BAD_REQUEST
			);
		}

		$response = $this->model->delete_course_from_cart( $user_id, $course_id );

		if ( $response ) {
			ob_start();
			tutor_load_template( 'ecommerce.cart' );
			$cart_template = ob_get_clean();
			$data          = array(
				'cart_template' => $cart_template,
				'cart_count'    => self::get_user_cart_item_count( $user_id ),
			);
			$this->json_response(
				__( 'The course was removed successfully.', 'tutor' ),
				$data,
				HttpHelper::STATUS_OK
			);
		} else {
			$this->json_response(
				__( 'Course remove failed.', 'tutor' ),
				null,
				HttpHelper::STATUS_BAD_REQUEST
			);
		}
	}
}

Filemanager

Name Type Size Permission Actions
PaymentGateways Folder 0755
AdminMenu.php File 2.34 KB 0644
BillingController.php File 5.57 KB 0644
CartController.php File 5.46 KB 0644
CheckoutController.php File 26.97 KB 0644
CouponController.php File 21.48 KB 0644
Ecommerce.php File 5.12 KB 0644
EmailController.php File 24.96 KB 0644
HooksHandler.php File 11.75 KB 0644
OptionKeys.php File 1.77 KB 0644
OrderActivitiesController.php File 4.57 KB 0644
OrderController.php File 33.34 KB 0644
PaymentHandler.php File 4.25 KB 0644
Settings.php File 12.7 KB 0644
Tax.php File 6.35 KB 0644
currency.php File 27.51 KB 0644