LLMS_Analytics_Widget::set_order_data_query( $args = array() )


Description Description


Source Source

File: includes/abstracts/abstract.llms.analytics.widget.php

	protected function set_order_data_query( $args = array() ) {

		extract( wp_parse_args( $args, array(

			'select' => array( '*' ),
			'date_range' => true, // whether or not to add a "where" for the posted date range
			'date_field' => 'post_date',
			'query_function' => 'get_results', // query function to pass to $wpdb->query()
			'output_type' => OBJECT,
			'joins' => array(), // array of JOIN statements
			'statuses' => array(), // array of order statuses to query
			'wheres' => array(), // array of "WHERE" statements
			'order' => 'ASC',
			'orderby' => '',

		) ) );

		$this->query_function = $query_function;
		$this->query_vars = array();
		$this->output_type = $output_type;

		global $wpdb;

		// setup student join & where clauses
		$students = $this->get_posted_students();
		$students_join = '';
		$students_where = '';
		if ( $students ) {
			$students_join = "JOIN {$wpdb->postmeta} AS m1 ON orders.ID = m1.post_id";
			$students_where .= "AND m1.meta_key = '_llms_user_id'";
			$students_where .= ' AND m1.meta_value IN ( ' . implode( ', ', $students ) . ' )';
		}

		// setup post (product) joins & where clauses
		$posts = $this->get_posted_posts();
		$products_join = '';
		$products_where = '';
		if ( $posts ) {
			$products_join = "JOIN {$wpdb->postmeta} AS m2 ON orders.ID = m2.post_id";
			$products_where .= "AND m2.meta_key = '_llms_product_id'";
			$products_where .= ' AND m2.meta_value IN ( ' . implode( ', ', $posts ) . ' )';
		}

		$order_dates = '';
		if ( $date_range ) {
			$dates = $this->get_posted_dates();
			$order_dates = "AND orders.{$date_field} BETWEEN CAST( %s AS DATETIME ) AND CAST( %s AS DATETIME )";
			$this->query_vars[] = $this->format_date( $dates['start'], 'start' );
			$this->query_vars[] = $this->format_date( $dates['end'], 'end' );
		}

		// setup post status conditions in the where clause
		$post_statuses = '';
		if ( $statuses ) {
			$post_statuses .= ' AND ( ';
			foreach ( $statuses as $i => $status ) {
				if ( $i > 0 ) {
					$post_statuses .= ' OR ';
				}
				$post_statuses .= 'post_status = %s';
				$this->query_vars[] = $status;
			}
			$post_statuses .= ' )';
		}

		// setup the select clause
		$select_clause = '';
		foreach ( $select as $i => $s ) {
			if ( $i > 0 ) {
				$select_clause .= ', ';
			}
			$select_clause .= $s;
		}

		$joins_clause = '';
		foreach ( $joins as $join ) {
			$joins_clause .= $join . "\r\n";
		}

		$wheres_clause = '';
		foreach ( $wheres as $where ) {
			$wheres_clause .= $where . "\r\n";
		}

		$order_clause = '';
		if ( $order && $orderby ) {
			$order_clause = 'ORDER BY ' . $orderby . ' ' . $order;
		}

		$this->query = "SELECT {$select_clause}
						FROM {$wpdb->posts} AS orders
						{$students_join}
						{$products_join}
						{$joins_clause}
						WHERE orders.post_type = 'llms_order'
							{$order_dates}
							{$post_statuses}
							{$students_where}
							{$products_where}
							{$wheres_clause}
						{$order_clause}
						;";

	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.





Permalink: