Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
jsarnowski/wp-simple-pay-pro / pro / admin / class-ajax.php
Size: Mime:
<?php
/**
 * Admin: AJAX
 *
 * @package SimplePay\Pro\Admin
 * @copyright Copyright (c) 2020, Sandhills Development, LLC
 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since 3.0.0
 */

namespace SimplePay\Pro\Admin;

use SimplePay\Core\API;
use SimplePay\Core\PaymentForm\PriceOption;
use SimplePay\Pro\Post_Types\Simple_Pay\Edit_Form;
use SimplePay\Pro\License_Management;

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

/**
 * Admin ajax.
 *
 * @since 3.0.0
 */
class Ajax {

	/**
	 * Set up ajax hooks.
	 *
	 * @since 3.0.0
	 */
	public function __construct() {

		add_action( 'wp_ajax_simpay_add_field', array( __CLASS__, 'add_field' ) );

		add_action( 'wp_ajax_simpay_add_price', array( __CLASS__, 'add_price' ) );
		add_action( 'wp_ajax_simpay_add_plan', array( __CLASS__, 'add_plan' ) );

		// License key activation/deactivation.
		add_action( 'wp_ajax_simpay_activate_license', array( __CLASS__, 'activate_license' ) );
		add_action( 'wp_ajax_simpay_deactivate_license', array( __CLASS__, 'deactivate_license' ) );
	}

	/**
	 * Add a new metabox for custom fields settings
	 */
	public static function add_field() {

		// Check the nonce first.
		check_ajax_referer( 'simpay_custom_fields_nonce', 'addFieldNonce' );

		ob_start();

		$type = isset( $_POST['fieldType'] ) ? sanitize_key( strtolower( $_POST['fieldType'] ) ) : '';

		$counter = isset( $_POST['counter'] ) ? intval( $_POST['counter'] ) : 0;
		$uid     = isset( $_POST['nextUid'] ) ? intval( $_POST['nextUid'] ) : $counter;

		// Load new metabox depending on what type was selected.
		if ( ! empty( $type ) ) {
			try {
				global $post;

				$post = isset( $_POST['post_id'] )
					? get_post( absint( $_POST['post_id'] ) )
					: new \stdClass();

				echo Edit_Form\get_custom_field(
					$type,
					$counter,
					array(
						'uid' => $uid,
					)
				);
			} catch ( \Exception $e ) {
				wp_send_json_error(
					array(
						'success' => false,
						'message' => $e,
					)
				);
			}
		} else {
			wp_send_json_error( array( 'success' => false ) );
		}

		ob_end_flush();

		die();
	}

	/**
	 * Handles the AJAX action `simpay_add_plan`.
	 *
	 * @since 4.1.0
	 * @access private
	 */
	public static function add_plan() {
		// Verify nonce.
		$nonce = check_ajax_referer( 'simpay_add_plan_nonce', '_wpnonce', false );

		if ( false === $nonce ) {
			wp_send_json_error(
				array(
					'message' => esc_html__(
						'Unable to add plan. Invalid security token.',
						'simple-pay'
					),
				)
			);
		}

		// Verify form.
		$form_id = isset( $_POST['form_id'] )
			? sanitize_text_field( $_POST['form_id' ] )
			: '';

		$form = simpay_get_form( $form_id );

		if ( false === $form ) {
			wp_send_json_error(
				array(
					'message' => esc_html__(
						'Unable to add plan. Invalid payment form.',
						'simple-pay'
					),
				)
			);
		}

		// Find Plan.
		$plan_id = isset( $_POST['plan_id'] )
			? sanitize_text_field( $_POST['plan_id' ] )
			: '';

		if ( empty( $plan_id ) ) {
			wp_send_json_error(
				array(
					'message' => array(
						'Unable to add plan. Plan ID not found.',
						'simple-pay'
					),
				)
			);
		}

		try {
			$plan = API\Plans\retrieve( $plan_id, $form->get_api_request_args() );

			$price = new PriceOption(
				array(
					'id'          => $plan->id,
					'default'     => false,
					'currency'    => $plan->currency,
					'unit_amount' => $plan->amount,
					'recurring'   => array(
						'interval'          => $plan->interval,
						'interval_count'    => $plan->interval_count,
						'trial_period_days' => $plan->trial_period_days,
					),
				),
				$form
			);

			ob_start();
			Edit_Form\__unstable_price_option( $price, wp_generate_uuid4(), array() );
			$html = ob_get_clean();

			wp_send_json_success( $html );
		} catch ( \Exception $e ) {
			wp_send_json_error(
				array(
					'message' => $e->getMessage(),
				)
			);
		}
	}

	/**
	 * Handles the AJAX action `simpay_add_price`.
	 *
	 * @since 4.1.0
	 * @access private
	 */
	public static function add_price() {
		$nonce = check_ajax_referer( 'simpay_add_price_nonce', '_wpnonce', false );

		if ( false === $nonce ) {
			wp_send_json_error(
				array(
					'message' => esc_html__(
						'Unable to add price. Invalid security token.',
						'simple-pay'
					),
				)
			);
		}

		// Verify form.
		$form_id = isset( $_POST['form_id'] )
			? sanitize_text_field( $_POST['form_id' ] )
			: '';

		$form = simpay_get_form( $form_id );

		if ( false === $form ) {
			wp_send_json_error(
				array(
					'message' => esc_html__(
						'Unable to add price. Invalid payment form.',
						'simple-pay'
					),
				)
			);
		}

		$currency = strtolower( simpay_get_setting( 'currency', 'USD' ) );

		$price = new PriceOption(
			array(
				'unit_amount' => simpay_get_currency_minimum( $currency ),
				'currency'    => $currency,
				'default'     => false,
				'can_recur'   => false,
			),
			$form
		);

		// Provides a way to circumvent a lack of `id` or `unit_amount_min`
		// before the PriceOption has a chance to be saved.
		$price->__unstable_unsaved = true;

		ob_start();
		Edit_Form\__unstable_price_option( $price, wp_generate_uuid4(), array() );
		$html = ob_get_clean();

		wp_send_json_success( $html );
	}

	/**
	 * Activate a plugin license.
	 *
	 * @since 3.5.0
	 */
	public static function activate_license() {
		$unknown_error = array(
			'message' => esc_html__( 'An unknown error has occured. Please try again.', 'simple-pay' ),
		);

		if ( ! wp_verify_nonce( $_POST['nonce'], 'simpay-manage-license' ) ) {
			return wp_send_json_error( $unknown_error );
		}

		$license      = sanitize_text_field( $_POST['license'] );
		$license_data = License_Management\activate_license( $license );

		// Error talking to the API.
		if ( ! $license_data ) {
			return wp_send_json_error( $unknown_error );
		}

		$feedback = License_Management\get_license_feedback( $license_data->license );
		$message  = License_Management\maybe_add_expiration_to_feedback( $license_data, $feedback );

		if ( 'valid' === $license_data->license ) {
			return wp_send_json_success(
				array(
					'message'      => $message,
					'license_data' => $license_data,
				)
			);
		} else {
			return wp_send_json_error(
				array(
					'message'      => $message,
					'license_data' => $license_data,
				)
			);
		}
	}

	/**
	 * Deactivate a plugin license.
	 *
	 * @since 3.5.0
	 */
	public static function deactivate_license() {
		$unknown_error = array(
			'message' => esc_html__( 'An unknown error has occured. Please try again.', 'simple-pay' ),
		);

		if ( ! wp_verify_nonce( $_POST['nonce'], 'simpay-manage-license' ) ) {
			return wp_send_json_error( $unknown_error );
		}

		$license      = sanitize_text_field( $_POST['license'] );
		$license_data = License_Management\deactivate_license( $license );

		// Error talking to the API.
		if ( ! $license_data ) {
			return wp_send_json_error( $unknown_error );
		}

		$feedback = License_Management\get_license_feedback( $license_data->license );
		$message  = License_Management\maybe_add_expiration_to_feedback( $license_data, $feedback );

		if ( 'deactivated' === $license_data->license ) {
			return wp_send_json_success(
				array(
					'message'      => $message,
					'license_data' => $license_data,
				)
			);
		} else {
			return wp_send_json_error(
				array(
					'message'      => $message,
					'license_data' => $license_data,
				)
			);
		}
	}

}