class-llms-event.php 3.56 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
<?php
/**
 * LifterLMS Event Model
 *
 * @package LifterLMS/Models/Classes
 *
 * @since 3.36.0
 * @version 4.3.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * LifterLMS Event Model
 *
 * @since 3.36.0
 * @since 4.3.0 Added record `$type` property definition.
 */
class LLMS_Event extends LLMS_Abstract_Database_Store {

	/**
	 * Array of table column name => format
	 *
	 * @var  array
	 */
	protected $columns = array(
		'date'         => '%s',
		'actor_id'     => '%d',
		'object_type'  => '%s',
		'object_id'    => '%d',
		'event_type'   => '%s',
		'event_action' => '%s',
		'meta'         => '%s',
	);

	/**
	 * Created date key name.
	 *
	 * @var string
	 */
	protected $date_created = 'date';

	/**
	 * Updated date not supported.
	 *
	 * @var null
	 */
	protected $date_updated = null;

	/**
	 * Database Table Name
	 *
	 * @var  string
	 */
	protected $table = 'events';

	/**
	 * The record type
	 *
	 * @var string
	 */
	protected $type = 'event';

	/**
	 * Constructor
	 *
	 * @since 3.36.0
	 *
	 * @param int  $id Event ID.
	 * @param bool $hydrate If true, hydrates the object on instantiation if an ID is supplied.
	 */
	public function __construct( $id = null, $hydrate = false ) {

		$this->id = $id;
		if ( $this->id && $hydrate ) {
			$this->hydrate();
		}

		// Adds created and updated dates on instantiation.
		parent::__construct();

	}

	/**
	 * Delete meta data
	 *
	 * @since 3.36.0
	 *
	 * @param string $key Meta key, if omitted deletes *all* metadata.
	 * @param bool   $save If true, saves updated metadata to the database.
	 * @return LLMS_Event
	 */
	public function delete_meta( $key = null, $save = false ) {

		if ( ! $key ) {
			return $this->set_unencoded_metas( array(), $save );
		}

		$all = $this->get_meta( null, false );
		unset( $all[ $key ] );
		return $this->set_unencoded_metas( $all, $save );

	}

	/**
	 * Retrieve metadata.
	 *
	 * @since 3.36.0
	 *
	 * @param string $key Metadata key, if omitted returns an associative array of all metadata as key=>val pairs.
	 * @param bool   $cache If true, uses cached data when available.
	 * @return mixed
	 */
	public function get_meta( $key = null, $cache = true ) {

		$all = $this->get( 'meta', $cache );
		$all = empty( $all ) ? array() : json_decode( $all, true );

		if ( ! $key ) {
			return $all;
		}

		return isset( $all[ $key ] ) ? $all[ $key ] : null;

	}

	/**
	 * Update/Add a single meta item.
	 *
	 * @since 3.36.0
	 *
	 * @param string $key Meta key.
	 * @param mixed  $val Meta value.
	 * @param bool   $save If true, saves the updated metadata to the database.
	 * @return LLMS_Event
	 */
	public function set_meta( $key, $val, $save = false ) {

		$all         = $this->get_meta();
		$all[ $key ] = $val;
		return $this->set_unencoded_metas( $all, $save );

	}

	/**
	 * Update/Add multiple metas.
	 *
	 * @since 3.36.0
	 *
	 * @param array $metas Associative array of metadata to update/add as key=>val pairs.
	 * @param bool  $save If true, saves the updated metadata to the database.
	 * @return LLMS_Event
	 */
	public function set_metas( $metas, $save = false ) {

		foreach ( $metas as $key => $val ) {
			$this->set_meta( $key, $val );
		}

		if ( $save ) {
			$this->save();
		}

		return $this;

	}

	/**
	 * Encode the array of metadata before setting it to the object.
	 *
	 * @since 3.36.0
	 *
	 * @param array $metas Associative array of metadata to update/add as key=>val pairs.
	 * @param bool  $save If true, saves the updated metadata to the database.
	 * @return LLMS_Event
	 */
	protected function set_unencoded_metas( $metas, $save = false ) {
		return $this->set( 'meta', wp_json_encode( $metas ), $save );
	}

}