public/wordpress/wp-content/plugins/wp-courseware/includes/controllers/subscriptions.php
/**
	 * Process Subscription.
	 *
	 * @since 4.3.0
	 *
	 * @param array $data The subscription post data.
	 *
	 * @return bool True on successful save, False on failure
	 */
Process Subscription.

@param array $data The subscription post data.

@return bool True on successful save, False on failure
Process Subscription . @param array $data The subscription post data . @return bool True on successful save False on failure
public function process_subscription( $data = array() ) {
		if ( empty( $data ) ) {
			return false;
		}
		if ( ! ( $subscription_id = wpcw_array_var( $data, 'subscription_id' ) ) ) {
			return false;
		}
		$subscription = new Subscription( absint( $subscription_id ) );
		if ( ! $subscription->get_id() ) {
			$subscription->create();
		}
		foreach ( $subscription->get_data( true ) as $key => $value ) {
			if ( ! isset( $data[ $key ] ) ) {
				continue;
			}
			$dont_set_prop = false;
			switch ( $key ) {
				case 'created' :
					if ( ! ( $created = wpcw_array_var( $data, 'created' ) ) ) {
						$value = current_time( 'timestamp', true );
					} else {
						$created_hour   = wpcw_array_var( $data, 'created_hour' );
						$created_minute = wpcw_array_var( $data, 'created_minute' );
						$created_second = wpcw_array_var( $data, 'created_second' );
						if ( $created && $created_hour && $created_minute && $created_second ) {
							$value = gmdate( 'Y-m-d H:i:s', strtotime( $created . ' ' . (int) $created_hour . ':' . (int) $created_minute . ':' . (int) $created_second ) );
						}
					}
					break;
				case 'expiration' :
					if ( ! ( $expiration = wpcw_array_var( $data, 'expiration' ) ) ) {
						$value = current_time( 'timestamp', true );
					} else {
						$expiration_hour   = wpcw_array_var( $data, 'expiration_hour' );
						$expiration_minute = wpcw_array_var( $data, 'expiration_minute' );
						$expiration_second = wpcw_array_var( $data, 'expiration_second' );
						if ( $expiration && $expiration_hour && $expiration_minute && $expiration_second ) {
							$value = gmdate( 'Y-m-d H:i:s', strtotime( $expiration . ' ' . (int) $expiration_hour . ':' . (int) $expiration_minute . ':' . (int) $expiration_second ) );
						}
					}
					break;
				case 'order_id':
					$value = absint( $data[ $key ] );
					if ( absint( $subscription->get_order_id() ) !== absint( $value ) ) {
						$order = new Order( $value );
						if ( $order->get_order_id() ) {
							$order->update_meta( 'subscription_id', $value, $subscription->get_order_id() );
						}
					}
					break;
				case 'course_id':
					$value = absint( $data[ $key ] );
					break;
				case 'student_id' :
					$value = absint( $data[ $key ] );
					break;
				case 'status' :
					$subscription->set_status( wpcw_clean( $data[ $key ] ) );
					$dont_set_prop = true;
					break;
				default:
					$value = wpcw_clean( $data[ $key ] );
					break;
			}
			if ( ! $dont_set_prop ) {
				$subscription->set_prop( $key, $value );
			}
		}
		if ( ! $subscription->get_student_id() ) {
			wp_die( esc_html__( 'Student is required.', 'wp-courseware' ) );
		}
		if ( ! $subscription->get_course_id() ) {
			wp_die( esc_html__( 'Course is required.', 'wp-courseware' ) );
		}
		if ( ! $subscription->get_method() ) {
			wp_die( esc_html__( 'Payment Method is required.', 'wp-courseware' ) );
		}
		if ( ( $order_action = wpcw_array_var( $data, 'order_action' ) ) && 'new' === $order_action ) {
			$order = new Order();
			$order->create();
			$course  = $subscription->get_course();
			$student = $subscription->get_student();
			$order->insert_order_items( array(
				array(
					'id'     => $course->get_course_id(),
					'title'  => $course->get_course_title(),
					'qty'    => 1,
					'amount' => $course->get_payments_price(),
				),
			) );
			$subscription->set_prop( 'course_title', $course->get_course_title() );
			if ( $subscription->get_student_id() ) {
				$order->set_prop( 'student_id', $student->get_ID() );
				$order->set_prop( 'student_name', $student->get_full_name() );
				$order->set_prop( 'student_email', $student->get_email() );
			}
			if ( $subscription->get_transaction_id() ) {
				$order->set_prop( 'transaction_id', $subscription->get_transaction_id() );
			}
			if ( $subscription->get_method() ) {
				$order->set_prop( 'payment_method', $subscription->get_method() );
			}
			if ( 'completed' !== $order->get_order_status() ) {
				$order->set_order_status( 'completed' );
			}
			$order->update_totals();
			$order->save();
			$order->add_meta( 'subscription_id', $subscription->get_id(), true );
			$subscription->set_prop( 'order_id', $order->get_order_id() );
		}
		return $subscription->save();
	}
public function process_subscription ( $ data = array ( ) ) { if ( empty ( $ data ) ) { return false ; } if ( ! ( $ subscription_id = wpcw_array_var ( $ data , 'subscription_id' ) ) ) { return false ; } $ subscription = new Subscription ( absint ( $ subscription_id ) ) ; if ( ! $ subscription -> get_id ( ) ) { $ subscription -> create ( ) ; } foreach ( $ subscription -> get_data ( true ) as $ key => $ value ) { if ( ! isset ( $ data [ $ key ] ) ) { continue ; } $ dont_set_prop = false ; switch ( $ key ) { case 'created' : if ( ! ( $ created = wpcw_array_var ( $ data , 'created' ) ) ) { $ value = current_time ( 'timestamp' , true ) ; } else { $ created_hour = wpcw_array_var ( $ data , 'created_hour' ) ; $ created_minute = wpcw_array_var ( $ data , 'created_minute' ) ; $ created_second = wpcw_array_var ( $ data , 'created_second' ) ; if ( $ created && $ created_hour && $ created_minute && $ created_second ) { $ value = gmdate ( 'Y-m-d H:i:s' , strtotime ( $ created . ' ' . ( int ) $ created_hour . ':' . ( int ) $ created_minute . ':' . ( int ) $ created_second ) ) ; } } break ; case 'expiration' : if ( ! ( $ expiration = wpcw_array_var ( $ data , 'expiration' ) ) ) { $ value = current_time ( 'timestamp' , true ) ; } else { $ expiration_hour = wpcw_array_var ( $ data , 'expiration_hour' ) ; $ expiration_minute = wpcw_array_var ( $ data , 'expiration_minute' ) ; $ expiration_second = wpcw_array_var ( $ data , 'expiration_second' ) ; if ( $ expiration && $ expiration_hour && $ expiration_minute && $ expiration_second ) { $ value = gmdate ( 'Y-m-d H:i:s' , strtotime ( $ expiration . ' ' . ( int ) $ expiration_hour . ':' . ( int ) $ expiration_minute . ':' . ( int ) $ expiration_second ) ) ; } } break ; case 'order_id' : $ value = absint ( $ data [ $ key ] ) ; if ( absint ( $ subscription -> get_order_id ( ) ) !== absint ( $ value ) ) { $ order = new Order ( $ value ) ; if ( $ order -> get_order_id ( ) ) { $ order -> update_meta ( 'subscription_id' , $ value , $ subscription -> get_order_id ( ) ) ; } } break ; case 'course_id' : $ value = absint ( $ data [ $ key ] ) ; break ; case 'student_id' : $ value = absint ( $ data [ $ key ] ) ; break ; case 'status' : $ subscription -> set_status ( wpcw_clean ( $ data [ $ key ] ) ) ; $ dont_set_prop = true ; break ; default : $ value = wpcw_clean ( $ data [ $ key ] ) ; break ; } if ( ! $ dont_set_prop ) { $ subscription -> set_prop ( $ key , $ value ) ; } } if ( ! $ subscription -> get_student_id ( ) ) { wp_die ( esc_html__ ( 'Student is required.' , 'wp-courseware' ) ) ; } if ( ! $ subscription -> get_course_id ( ) ) { wp_die ( esc_html__ ( 'Course is required.' , 'wp-courseware' ) ) ; } if ( ! $ subscription -> get_method ( ) ) { wp_die ( esc_html__ ( 'Payment Method is required.' , 'wp-courseware' ) ) ; } if ( ( $ order_action = wpcw_array_var ( $ data , 'order_action' ) ) && 'new' === $ order_action ) { $ order = new Order ( ) ; $ order -> create ( ) ; $ course = $ subscription -> get_course ( ) ; $ student = $ subscription -> get_student ( ) ; $ order -> insert_order_items ( array ( array ( 'id' => $ course -> get_course_id ( ) , 'title' => $ course -> get_course_title ( ) , 'qty' => 1 , 'amount' => $ course -> get_payments_price ( ) , ) , ) ) ; $ subscription -> set_prop ( 'course_title' , $ course -> get_course_title ( ) ) ; if ( $ subscription -> get_student_id ( ) ) { $ order -> set_prop ( 'student_id' , $ student -> get_ID ( ) ) ; $ order -> set_prop ( 'student_name' , $ student -> get_full_name ( ) ) ; $ order -> set_prop ( 'student_email' , $ student -> get_email ( ) ) ; } if ( $ subscription -> get_transaction_id ( ) ) { $ order -> set_prop ( 'transaction_id' , $ subscription -> get_transaction_id ( ) ) ; } if ( $ subscription -> get_method ( ) ) { $ order -> set_prop ( 'payment_method' , $ subscription -> get_method ( ) ) ; } if ( 'completed' !== $ order -> get_order_status ( ) ) { $ order -> set_order_status ( 'completed' ) ; } $ order -> update_totals ( ) ; $ order -> save ( ) ; $ order -> add_meta ( 'subscription_id' , $ subscription -> get_id ( ) , true ) ; $ subscription -> set_prop ( 'order_id' , $ order -> get_order_id ( ) ) ; } return $ subscription -> save ( ) ; }
Process Subscription.
Process Subscription .
// Subscription Id. // Setup Subscription. // Create subscription if doesn't exist. // Sanitize & Save Subscription Data. // Specific Sanitization // Set Subscription Property. // Check for Student Id. // Check for Course Id. // Check for Payment Method. // Add new order. // Create Order. // Get Course. // Add Order Items. // Set Course Id. // Set Student Id. // Set Transaction Id. // Set Payment Method. // Set Order Completed. // Update Totals & Save. // Set Subscription Id to Order. // Set Order Id for Subscription. // Save Subscription.