llms-trait-sales-page.php 3.84 KB
Newer Older
cyrille's avatar
cyrille committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
<?php
/**
 * LifterLMS Sales Page trait
 *
 * @package LifterLMS/Traits
 *
 * @since 5.3.0
 * @version 5.3.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * LifterLMS Sales Page trait.
 *
 * **This trait should only be used by classes that extend from the {@see LLMS_Post_Model} class.**
 * **Classes that use this trait must call {@see LLMS_Trait_Sales_Page::construct_sales_page()} in their constructor.**
 *
 * @since 5.3.0
 *
 * @property int    $sales_page_content_page_id WP Post ID of the WP page to redirect to when $sales_page_content_type is 'page'.
 * @property string $sales_page_content_type    Sales page behavior [none,content,page,url].
 * @property string $sales_page_content_url     Redirect URL for a sales page, when $sales_page_content_type is 'url'.
 */
trait LLMS_Trait_Sales_Page {
	/**
	 * @inheritdoc
	 */
	abstract protected function add_properties( $props = array() );

	/**
	 * Setup properties used by this trait.
	 *
	 * **Must be called by the constructor of the class that uses this trait.**
	 *
	 * @since 5.3.0
	 */
	protected function construct_sales_page() {

		$this->add_properties(
			array(
				'sales_page_content_page_id' => 'absint',
				'sales_page_content_type'    => 'string',
				'sales_page_content_url'     => 'string',
			)
		);
	}

	/**
	 * @inheritdoc
	 */
	abstract public function get( $key, $raw = false );

	/**
	 * Get the URL to a WP page or custom URL when sales page redirection is enabled.
	 *
	 * **The class that uses this trait must have the {@see LLMS_Post_Model::$model_post_type} property.**
	 *
	 * @since 3.20.0
	 * @since 5.3.0 Check for an empty  URL or ID.
	 *              Refactored from `LLMS_Course` and `LLMS_Membership`.
	 *
	 * @return string
	 */
	public function get_sales_page_url() {

		$type = $this->get( 'sales_page_content_type' );
		switch ( $type ) {
			case 'page':
				$url = get_permalink( $this->get( 'sales_page_content_page_id' ) );
				break;
			case 'url':
				$url = $this->get( 'sales_page_content_url' );
				break;
			default:
				$url = get_permalink( $this->get( 'id' ) );
		}

		/**
		 * Filters the model's sales page URL
		 *
		 * The dynamic portion of the hook name, $this->model_post_type,
		 * refers to the model's post type, e.g. 'course' or 'membership'.
		 *
		 * @since Unknown
		 *
		 * @param string          $url    Sales page URL.
		 * @param LLMS_Post_Model $object The LLMS_Course or LLMS_Membership object.
		 * @param string          $type   The model's $sales_page_content_type property.
		 */
		$url = apply_filters( "llms_{$this->model_post_type}_get_sales_page_url", $url, $this, $type );

		return $url;
	}

	/**
	 * Determine if sales page redirection is enabled.
	 *
	 * **The class that uses this trait must have the {@see LLMS_Post_Model::$model_post_type} property.**
	 *
	 * @since 5.3.0 Refactored from `LLMS_Course` and `LLMS_Membership`.
	 *
	 * @return boolean
	 */
	public function has_sales_page_redirect() {

		$type = $this->get( 'sales_page_content_type' );
		switch ( $type ) {
			case 'page':
				$has_redirect = (bool) $this->get( 'sales_page_content_page_id' );
				break;
			case 'url':
				$has_redirect = (bool) $this->get( 'sales_page_content_url' );
				break;
			default:
				$has_redirect = false;
		}

		/**
		 * Filters whether or not the model has a sales page redirect.
		 *
		 * The dynamic portion of the hook name, $this->model_post_type,
		 * refers to the model's post type, e.g. 'course' or 'membership'.
		 *
		 * @since Unknown
		 *
		 * @param boolean         $has_redirect Whether or not the model has a sales page redirect.
		 * @param LLMS_Post_Model $object       The LLMS_Course or LLMS_Membership object.
		 * @param string          $type         The model's $sales_page_content_type property.
		 */
		$has_redirect = apply_filters(
			"llms_{$this->model_post_type}_has_sales_page_redirect",
			$has_redirect,
			$this,
			$type
		);

		return $has_redirect;
	}
}