llms.functions.user.postmeta.php 7.78 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
<?php
/**
 * CRUD LifterLMS User Postmeta Data
 *
 * All functions are pluggable.
 *
 * @package LifterLMS/Functions
 *
 * @since 3.21.0
 * @version 3.36.3
 */

defined( 'ABSPATH' ) || exit;

/**
 * CRUD LifterLMS User Postmeta Data.
 *
 * @since 3.21.0
 * @since 3.33.0 Added `llms_bulk_delete_user_postmeta`.
 *               Also now `llms_delete_user_postmeta` returns true only if at least one existing user postmeta has been successfully deleted.
 * @since 3.36.3 Fix doc and indentation.
 */
if ( ! function_exists( 'llms_delete_user_postmeta' ) ) :
	/**
	 * Delete user postmeta data.
	 *
	 * @since 3.21.0
	 * @since 3.33.0 Returns true only if at least one existing user postmeta has been successfully deleted.
	 *
	 * @param int    $user_id    WP User ID.
	 * @param int    $post_id    WP Post ID.
	 * @param string $meta_key   Optional. Meta key for lookup, if not supplied, all matching items will be removed. Default null.
	 * @param mixed  $meta_value Optional. Meta value for lookup, if not supplied, all matching items will be removed. Default null.
	 *
	 * @return bool False if no postmetas has been deleted either because they do not exist or because of an error during the
	 *              actual row deletion from the db. True if at least one existing user postmeta has been successfully deleted.
	 */
	function llms_delete_user_postmeta( $user_id, $post_id, $meta_key = null, $meta_value = null ) {

		$ret = false;

		$existing = _llms_query_user_postmeta( $user_id, $post_id, $meta_key, maybe_unserialize( $meta_value ) );
		if ( $existing ) {
			foreach ( $existing as $obj ) {
				$item = new LLMS_User_Postmeta( $obj->meta_id, false );
				if ( ! $item->delete() ) {
					$ret = $ret || false;
				} else {
					$ret = true;
				}
			}
		}

		return $ret;

	}
endif;

if ( ! function_exists( 'llms_bulk_delete_user_postmeta' ) ) :
	/**
	 * Bulk remove user postmeta data.
	 *
	 * @since 3.33.0
	 *
	 * @param int   $user_id WP User ID.
	 * @param int   $post_id WP Post ID.
	 * @param array $data    Optional. Associative array of meta keys => meta values to delete.
	 *                       If not meta values supplied, all matching items will be removed. Default empty array.
	 * @return array|boolean On error returns an associative array of the submitted keys, each item will be true for success or false for error.
	 *                       On success returns true.
	 */
	function llms_bulk_delete_user_postmeta( $user_id, $post_id, $data = array() ) {

		$res = array_fill_keys( array_keys( $data ), null );
		$err = false;

		if ( ! empty( $data ) ) {
			foreach ( $data as $key => $value ) {
				$delete      = llms_delete_user_postmeta( $user_id, $post_id, $key, $value );
				$res[ $key ] = $delete;
				if ( ! $delete ) {
					$err = true;
				}
			}
		} else {
			$res = llms_delete_user_postmeta( $user_id, $post_id );
			$err = ! $res;
		}

		return $err ? $res : true;

	}
endif;

if ( ! function_exists( 'llms_get_user_postmeta' ) ) :
	/**
	 * Get user postmeta data or dates by user, post, and key.
	 *
	 * @since 3.21.0
	 *
	 * @param int    $user_id  WP User ID.
	 * @param int    $post_id  WP Post ID.
	 * @param string $meta_key Optional. Meta key, if not supplied returns associative array of all metadata found for the given user / post. Default null.
	 * @param bool   $single   Optional. If true, returns only the data. Default true.
	 * @param string $return   Optional. Determine if the meta value or updated date should be returned [meta_value,updated_date]. Default 'meta_value'.
	 * @return mixed
	 */
	function llms_get_user_postmeta( $user_id, $post_id, $meta_key = null, $single = true, $return = 'meta_value' ) {

		$single = is_null( $meta_key ) ? false : $single;

		$res = array();

		$metas = _llms_query_user_postmeta( $user_id, $post_id, $meta_key );
		if ( count( $metas ) ) {
			foreach ( $metas as $meta ) {
				if ( $meta_key ) {
					$res[ $meta_key ][] = maybe_unserialize( $meta->$return );
				} else {
					$res[ $meta->meta_key ][] = maybe_unserialize( $meta->$return );
				}
			}
		}

		if ( $single ) {
			return count( $res ) ? $res[ $meta_key ][0] : '';
		} elseif ( $meta_key ) {
			return count( $res ) ? $res[ $meta_key ] : array();
		}

		return $res;

	}
endif;

if ( ! function_exists( 'llms_update_user_postmeta' ) ) :
	/**
	 * Update user postmeta data.
	 *
	 * @since 3.21.0
	 *
	 * @param int    $user_id    WP User ID.
	 * @param int    $post_id    WP Post ID.
	 * @param string $meta_key   Meta key.
	 * @param mixed  $meta_value Meta value (don't serialize serializable values).
	 * @param bool   $unique     Optional. If true, updates existing value (if it exists).
	 *                           If false, will add a new record (allowing multiple records with the same key to exist).
	 *                           Deafult true.
	 * @return bool
	 */
	function llms_update_user_postmeta( $user_id, $post_id, $meta_key, $meta_value, $unique = true ) {

		$item = false;

		// if unique is true, make an update to the existing item (if it exists).
		if ( $unique ) {

			// locate the item.
			$existing = _llms_query_user_postmeta( $user_id, $post_id, $meta_key );
			if ( $existing ) {

				// load it and make sure it exists.
				$item = new LLMS_User_Postmeta( $existing[0]->meta_id, false );
				if ( ! $item->exists() ) {
					$item = false;
				}
			}
		}

		if ( ! $item ) {
			$item = new LLMS_User_Postmeta();
		}

		// setup the data we want to store.
		$updated_date = llms_current_time( 'mysql' );
		$meta_value   = maybe_serialize( $meta_value );
		$item->setup( compact( 'user_id', 'post_id', 'meta_key', 'meta_value', 'updated_date' ) );
		return $item->save();

	}
endif;

if ( ! function_exists( 'llms_bulk_update_user_postmeta' ) ) :
	/**
	 * Update bulk update user postmeta data.
	 *
	 * @since 3.21.0
	 *
	 * @param int   $user_id WP User ID.
	 * @param int   $post_id WP Post ID.
	 * @param array $data    Optional. Associative array of meta keys => meta values to update.
	 *                       Default empty array.
	 * @param bool  $unique  Optional. If true, updates existing value (if it exists).
	 *                       If false, will add a new record (allowing multiple records with the same key to exist).
	 *                       Deafult true.
	 * @return array|true On error returns an associative array of the submitted keys, each item will be true for success or false for error.
	 *                    On success returns true.
	 */
	function llms_bulk_update_user_postmeta( $user_id, $post_id, $data = array(), $unique = true ) {

		$res = array_fill_keys( array_keys( $data ), null );
		$err = false;
		foreach ( $data as $key => $val ) {
			$update      = llms_update_user_postmeta( $user_id, $post_id, $key, $val, $unique );
			$res[ $key ] = $update;
			if ( ! $update ) {
				$err = true;
			}
		}

		return $err ? $res : true;

	}
endif;

if ( ! function_exists( '_llms_query_user_postmeta' ) ) :
	/**
	 * Query user postmeta data.
	 * This function is marked for internal use only.
	 *
	 * @since 3.21.0
	 *
	 * @access private
	 *
	 * @param int    $user_id WP User ID.
	 * @param int    $post_id WP Post ID.
	 * @param string $meta_key   Optional. Meta key. Default null.
	 * @param string $meta_value Optional. Meta value. Default null.
	 * @return array
	 */
	function _llms_query_user_postmeta( $user_id, $post_id, $meta_key = null, $meta_value = null ) {

		global $wpdb;

		$key = $meta_key ? $wpdb->prepare( 'AND meta_key = %s', $meta_key ) : '';
		$val = $meta_value ? $wpdb->prepare( 'AND meta_value = %s', $meta_value ) : '';

		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$res = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta
				 WHERE user_id = %d AND post_id = %d {$key} {$val} ORDER BY updated_date DESC",
				$user_id,
				$post_id
			)
		);
		// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

		return $res;

	}
endif;