llms.abstract.post.data.php 5.91 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
<?php
/**
 * Defines base methods and properties for querying data about LifterLMS Custom Post Types
 *
 * @package LifterLMS/Abstracts/Classes
 *
 * @since 3.31.0
 * @version 5.9.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * LLMS Post Data abstract class
 *
 * @since 3.31.0
 */
abstract class LLMS_Abstract_Post_Data {

	/**
	 * LLMS Post instance.
	 *
	 * @since 3.31.0
	 * @var LLMS_Post_Model
	 */
	protected $post;

	/**
	 * LLMS Post ID.
	 *
	 * @since 3.31.0
	 * @var int
	 */
	protected $post_id;

	/**
	 * @since 3.31.0
	 * @var array
	 */
	protected $dates = array();

	/**
	 * Constructor.
	 *
	 * @since 3.31.0
	 *
	 * @param int $post_id WP Post ID of the LLMS Post.
	 */
	public function __construct( $post_id ) {

		$this->post_id = $post_id;
		$this->post    = llms_get_post( $this->post_id );

	}

	/**
	 * Retrieve the instance of the LLMS_Post_Model.
	 *
	 * @since 3.31.0
	 *
	 * @return LLMS_Post_Model The instance of the LLMS_Post_Model.
	 */
	public function get_post() {
		return $this->post;
	}

	/**
	 * Retrieve the LLMS_Post_Model ID.
	 *
	 * @since 3.31.0
	 *
	 * @return int The LLMS_Post_Model ID.
	 */
	public function get_post_id() {
		return $this->post_id;
	}

	/**
	 * Allow dates and timestamps to be passed into various data functions.
	 *
	 * @since 3.31.0
	 *
	 * @param  mixed $date A date string or timestamp.
	 * @return int The Unix timestamp of the given date.
	 */
	protected function strtotime( $date ) {
		if ( ! is_numeric( $date ) ) {
			$date = date( 'U', strtotime( $date ) );
		}
		return $date;
	}

	/**
	 * Retrieve a start or end date based on the period.
	 *
	 * @since 3.31.0
	 *
	 * @param  string $period Period [current|previous].
	 * @param  string $date   The date type [start|end].
	 * @return string The start or end date in the format 'Y-m-d H:i:s'.
	 */
	protected function get_date( $period, $date ) {

		return date( 'Y-m-d H:i:s', $this->dates[ $period ][ $date ] );

	}

	/**
	 * Retrieves the selected period from `$_GET` and validates it.
	 *
	 * If there is no period set or it's set to an invalid period, defaults to "today".
	 *
	 * @since 5.9.0
	 *
	 * @return string
	 */
	public function parse_period() {
		$periods = LLMS_Admin_Reporting::get_period_filters();
		$period  = llms_filter_input( INPUT_GET, 'period' );
		if ( ! $period || ! array_key_exists( $period, $periods ) ) {
			$period = 'today';
		}
		return $period;
	}

	/**
	 * Set the dates passed on a date range period
	 *
	 * @since 3.31.0
	 *
	 * @param  string $period Date range period.
	 * @return void
	 */
	public function set_period( $period = 'today' ) {

		$now = current_time( 'timestamp' );

		switch ( $period ) {

			case 'all_time':
				$curr_start = 0;
				$curr_end   = $now;

				$prev_start = 0;
				$prev_end   = $now;
				break;

			case 'last_year':
				$curr_start = strtotime( 'first day of january last year', $now );
				$curr_end   = strtotime( 'last day of december last year', $now );

				$prev_start = strtotime( 'first day of january last year', $curr_start );
				$prev_end   = strtotime( 'last day of december last year', $curr_start );
				break;

			case 'year':
				$curr_start = strtotime( 'first day of january this year', $now );
				$curr_end   = strtotime( 'last day of december this year', $now );

				$prev_start = strtotime( 'first day of january last year', $now );
				$prev_end   = strtotime( 'last day of december last year', $now );
				break;

			case 'last_month':
				$curr_start = strtotime( 'first day of previous month', $now );
				$curr_end   = strtotime( 'last day of previous month', $now );

				$prev_start = strtotime( 'first day of previous month', $curr_start );
				$prev_end   = strtotime( 'last day of previous month', $curr_start );
				break;

			case 'month':
				$curr_start = strtotime( 'first day of this month', $now );
				$curr_end   = strtotime( 'last day of this month', $now );

				$prev_start = strtotime( 'first day of previous month', $now );
				$prev_end   = strtotime( 'last day of previous month', $now );
				break;

			case 'last_week':
				$curr_start = strtotime( 'monday this week', $now - WEEK_IN_SECONDS );
				$curr_end   = $now;

				$prev_start = strtotime( 'monday previous week', $curr_start - WEEK_IN_SECONDS );
				$prev_end   = $curr_start - DAY_IN_SECONDS;
				break;

			case 'week':
				$curr_start = strtotime( 'monday this week', $now );
				$curr_end   = $now;

				$prev_start = strtotime( 'monday previous week', $now );
				$prev_end   = $curr_start - DAY_IN_SECONDS;
				break;

			case 'yesterday':
				$curr_start = $now - DAY_IN_SECONDS;
				$curr_end   = $curr_start;

				$prev_start = $curr_start - DAY_IN_SECONDS;
				$prev_end   = $prev_start;
				break;

			case 'today':
			default:
				$curr_start = $now;
				$curr_end   = $now;

				$prev_start = $now - DAY_IN_SECONDS;
				$prev_end   = $prev_start;

		}// End switch().

		$this->dates = array(
			'current'  => array(
				'start' => strtotime( 'midnight', $curr_start ),
				'end'   => strtotime( 'tomorrow', $curr_end ) - 1,
			),
			'previous' => array(
				'start' => strtotime( 'midnight', $prev_start ),
				'end'   => strtotime( 'tomorrow', $prev_end ) - 1,
			),
		);

	}

	/**
	 * Retrieve recent LLMS_User_Postmeta for the quiz
	 *
	 * @since 3.31.0
	 *
	 * @param array $args {
	 *     Optional. An array of arguments to feed the LLMS_Query_User_Postmeta with.
	 *
	 *     @type int          $per_page The number of posts to query for. Default 10.
	 *     @type array|string $types    Array of strings for the type of events to fetch, or a string to fetch them all. Default 'all'.
	 *                                  @see LLMS_Query_User_Postmeta::parse_args()
	 * }
	 * @return array Array of LLMS_User_Postmetas.
	 */
	public function recent_events( $args = array() ) {

		$query_args = wp_parse_args(
			$args,
			array(
				'per_page' => 10,
				'types'    => 'all',
			)
		);

		$query_args['post_id'] = $this->post_id;

		$query = new LLMS_Query_User_Postmeta( $query_args );

		return $query->get_metas();

	}

}