EDD


Source

File: classes/EDD.php

class EDD {

	protected $post_types;

	public function __construct() {

		if ( ! dozent_edd_exists() ) {
			return;
		}

		$this->post_types = apply_filters( 'edd_download_metabox_post_types', array( 'download' ) );


		add_action( 'add_meta_boxes', [ $this, 'register_meta_box' ] );

		foreach ( $this->post_types as $post_type ) {
			add_action( "save_post_" . $post_type, [ $this, 'attach_courses_to_product' ] );
		}

		add_filter( 'dozent_course_price', [ $this, 'filter_course_price' ], 10, 2 );
		add_action( 'dozent_course_type_closed_gate', [ $this, 'dozent_course_gate' ] );

		add_action( 'edd_complete_purchase', [ $this, 'edd_complete_purchase' ], 10, 3 );
		add_filter( 'dozent_course_new_enrol_status', [ $this, 'new_enrol_status' ], 10, 4 );

		/**
		 * EDD Earning status change
		 * Useful to get back money from the instructor if any payment got refund or disput
		 */

		add_action( 'edd_update_payment_status', [ $this, 'edd_update_payment_status' ], 10, 3 );

	}

	public function register_meta_box() {
		foreach ( $this->post_types as $post_type ) {

			add_meta_box( 'dozent-attach-courses', __( 'Add Courses', 'dozent' ), [ $this, 'add_courses' ], $post_type,
				'normal', 'high' );

		}
	}

	public function add_courses() {

		include DOZENT_ABSPATH . 'integration/edd/views/courses-data-panel.php';
	}

	public function attach_courses_to_product( $product_ID ) {

		//Attach or detach course to product only from data panel
		$data_source = dozent_input_text( 'dozent_courses_data_panel' );
		if ( $data_source !== 'edd' ) {
			return;
		}

		$course_ids = (array) dozent_input_array_field( 'dozent_attach_course_ids_to_product' );
		$course_ids = array_filter( $course_ids );

		$attached_course_ids = dozent_get_attached_course_ids_to_product( $product_ID );

		if ( dozent_count( $attached_course_ids ) ) {
			$removed_course_ids = array_diff( $attached_course_ids, $course_ids );

			foreach ( $removed_course_ids as $removed_course_id ) {
				delete_post_meta( $removed_course_id, '_attached_product_id' );
				delete_post_meta( $removed_course_id, '_product_by' );
			}
		}

		if ( dozent_count( $course_ids ) ) {
			foreach ( $course_ids as $course_id ) {
				update_post_meta( $course_id, '_attached_product_id', $product_ID );
				update_post_meta( $course_id, '_product_by', 'EDD' );
			}
		}
	}


	public function filter_course_price( $price, $course ) {
		$course = dozent_get_course( $course );

		if ( $course->is_closed() ) {
			$course_id = $course->get_id();

			$product_id = dozent_get_attached_product_id_by_course( $course_id );

			$price = edd_price( $product_id, false );
		}

		return $price;
	}


	/**
	 * When user click Take this course button from course single page.
	 * make sure user will be redirect to WooCommerce product landing page.
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @param $course
	 */

	public function dozent_course_gate( $course ) {
		$course                = dozent_get_course( $course );
		$attached_product_type = dozent_attached_product_type_to_course( $course );

		if ( $attached_product_type === 'EDD' ) {
			$product_id = dozent_get_attached_product_id_by_course( $course->get_id() );
			dozent_redirect( get_the_permalink( $product_id ) );
		}
	}


	/**
	 * Runs **when** a purchase is marked as "complete".
	 *
	 * And immediately enrol customer as student.
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @param  int  $payment_id  Payment ID.
	 * @param  \EDD_Payment  $payment  EDD_Payment object containing all payment data.
	 * @param  \EDD_Customer  $customer  EDD_Customer object containing all customer data.
	 */

	public function edd_complete_purchase( $payment_id, $payment, $customer ) {
		$enable_earning = (bool) dozent_get_option( 'enable_dozent_earning' );

		$cart_details = $payment->cart_details;

		if ( is_array( $cart_details ) ) {
			foreach ( $cart_details as $cart_index => $download ) {
				$course_ids = dozent_get_attached_course_ids_to_product( $download['id'] );

				if ( dozent_count( $course_ids ) ) {
					foreach ( $course_ids as $course_id ) {
						dozent_enrol_course( $course_id, $customer->user_id, $payment_id );
					}

					//Add Earning Data
					if ( $enable_earning ) {
						$this->add_earning_data( $course_ids, $download['id'], $payment_id );
					}
				}

			}
		}
	}

	public function add_earning_data( $course_ids, $product_id, $order_id ) {
		global $wpdb;

		$total_price = edd_get_download_price( $product_id );
		$payment     = new \EDD_Payment( $order_id );

		$payment_status = 'pending';
		if ( $payment ) {
			$payment_status = $payment->status;

			if ( $payment->status === 'publish' ) {
				$payment_status = 'completed';
			}
		}

		if ( dozent_count( $course_ids ) ) {

			$total_courses    = count( $course_ids );
			$per_course_price = $total_price;

			/**
			 * Divide Split course price into all courses
			 * so each attached course will get the earning based on equal amount
			 */

			if ( $per_course_price > 0 && $total_courses > 1 ) {
				$per_course_price = $total_price / $total_courses;
			}

			$enable_fees_deducting = (bool) dozent_get_option( 'dozent_earning_fees.enable_fees_deducting' );
			$fees_name             = dozent_get_option( 'dozent_earning_fees.fees_name' );
			$deduct_fees_rate      = dozent_get_option( 'dozent_earning_fees.fees_amount' );
			$fees_type             = dozent_get_option( 'dozent_earning_fees.fees_type' );

			$fees_deduct_data = array();

			if ( $enable_fees_deducting ) {
				$fees_amount = $deduct_fees_rate;

				if ( $deduct_fees_rate > 0 ) {
					if ( $fees_type === 'percent' ) {
						$fees_amount = ( $total_price * $deduct_fees_rate ) / 100;
					}

					if ( $fees_amount > 0 && $total_courses > 1 ) {
						$fees_amount = $fees_amount / $total_courses;
					}

					//If fis amount fixed, it will deduct direct $fees_amount

					$per_course_price = $per_course_price - $fees_amount;
				}

				$fees_deduct_data = array(
					'deduct_fees_rate'   => $deduct_fees_rate,
					'deduct_fees_amount' => $fees_amount,
					'deduct_fees_name'   => $fees_name,
					'deduct_fees_type'   => $fees_type,
				);
			}

			$instructor_rate = dozent_get_option( 'earning_instructor_commission' );
			$admin_rate      = dozent_get_option( 'earning_admin_commission' );

			$instructor_amount = 0;
			if ( $instructor_rate > 0 ) {
				$instructor_amount = ( $per_course_price * $instructor_rate ) / 100;
			}

			$admin_amount = 0;
			if ( $admin_rate > 0 ) {
				$admin_amount = ( $per_course_price * $admin_rate ) / 100;
			}

			foreach ( $course_ids as $course_id ) {
				$user_id = $wpdb->get_var( "SELECT post_author FROM {$wpdb->posts} WHERE ID = {$course_id} " );

				$earning_data = array(
					'user_id'                   => $user_id,
					'course_id'                 => $course_id,
					'order_id'                  => $order_id,
					'order_status'              => $payment_status,
					'total_courses_under_order' => $total_courses,
					'course_price_total'        => $total_price,
					'per_course_price'          => $per_course_price,
					'instructor_amount'         => $instructor_amount,
					'instructor_rate'           => $instructor_rate,
					'admin_amount'              => $admin_amount,
					'admin_rate'                => $admin_rate,
					'commission_type'           => 'percent',
					'process_by'                => 'EDD',
					'created_at'                => date( 'Y-m-d H:i:s', dozent_time() ),
				);

				$earning_data['raw_data'] = json_encode( $earning_data );

				$earning_data = apply_filters( 'dozent_new_earning_data', array_merge( $earning_data, $fees_deduct_data ) );

				$wpdb->insert( $wpdb->prefix . 'dozent_earnings', $earning_data );
			}
		}

	}

	/**
	 * Set new enrolment status from EDD
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @param $status
	 * @param $course
	 * @param $order_id
	 * @param $expired_at
	 *
	 * @return string
	 */

	public function new_enrol_status( $status, $course, $payment_id, $expired_at ) {
		$payment = new \EDD_Payment( $payment_id );

		if ( $payment ) {
			$status = $payment->status;

			if ( $payment->status === 'publish' ) {
				$status = 'active';
			}
		}

		return $status;
	}


	public function edd_update_payment_status( $payment_id, $new_status, $old_status ) {
		if ( $new_status === 'publish' ) {
			$new_status = 'completed';
		}

		global $wpdb;

		$wpdb->update( $wpdb->dozent_earnings, [ 'order_status' => $new_status ], [ 'order_id' => $payment_id ] );
	}


}

Methods