How to use Meta class

Best Cucumber Common Library code snippet using Meta

meta.php

Source:meta.php Github

copy

Full Screen

1<?php2/**3 * Metadata API4 *5 * Functions for retrieving and manipulating metadata of various WordPress object types. Metadata6 * for an object is a represented by a simple key-value pair. Objects may contain multiple7 * metadata entries that share the same key and differ only in their value.8 *9 * @package WordPress10 * @subpackage Meta11 * @since 2.9.012 */13/**14 * Add metadata for the specified object.15 *16 * @since 2.9.017 * @uses $wpdb WordPress database object for queries.18 * @uses do_action() Calls 'added_{$meta_type}_meta' with meta_id of added metadata entry,19 * object ID, meta key, and meta value20 *21 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)22 * @param int $object_id ID of the object metadata is for23 * @param string $meta_key Metadata key24 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.25 * @param bool $unique Optional, default is false. Whether the specified metadata key should be26 * unique for the object. If true, and the object already has a value for the specified27 * metadata key, no change will be made28 * @return int|bool The meta ID on successful update, false on failure.29 */30function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) {31 if ( !$meta_type || !$meta_key )32 return false;33 if ( !$object_id = absint($object_id) )34 return false;35 if ( ! $table = _get_meta_table($meta_type) )36 return false;37 global $wpdb;38 $column = sanitize_key($meta_type . '_id');39 // expected_slashed ($meta_key)40 $meta_key = wp_unslash($meta_key);41 $meta_value = wp_unslash($meta_value);42 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );43 $check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );44 if ( null !== $check )45 return $check;46 if ( $unique && $wpdb->get_var( $wpdb->prepare(47 "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",48 $meta_key, $object_id ) ) )49 return false;50 $_meta_value = $meta_value;51 $meta_value = maybe_serialize( $meta_value );52 do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );53 $result = $wpdb->insert( $table, array(54 $column => $object_id,55 'meta_key' => $meta_key,56 'meta_value' => $meta_value57 ) );58 if ( ! $result )59 return false;60 $mid = (int) $wpdb->insert_id;61 wp_cache_delete($object_id, $meta_type . '_meta');62 do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );63 return $mid;64}65/**66 * Update metadata for the specified object. If no value already exists for the specified object67 * ID and metadata key, the metadata will be added.68 *69 * @since 2.9.070 * @uses $wpdb WordPress database object for queries.71 * @uses do_action() Calls 'update_{$meta_type}_meta' before updating metadata with meta_id of72 * metadata entry to update, object ID, meta key, and meta value73 * @uses do_action() Calls 'updated_{$meta_type}_meta' after updating metadata with meta_id of74 * updated metadata entry, object ID, meta key, and meta value75 *76 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)77 * @param int $object_id ID of the object metadata is for78 * @param string $meta_key Metadata key79 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.80 * @param mixed $prev_value Optional. If specified, only update existing metadata entries with81 * the specified value. Otherwise, update all entries.82 * @return bool True on successful update, false on failure.83 */84function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') {85 if ( !$meta_type || !$meta_key )86 return false;87 if ( !$object_id = absint($object_id) )88 return false;89 if ( ! $table = _get_meta_table($meta_type) )90 return false;91 global $wpdb;92 $column = sanitize_key($meta_type . '_id');93 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';94 // expected_slashed ($meta_key)95 $meta_key = wp_unslash($meta_key);96 $passed_value = $meta_value;97 $meta_value = wp_unslash($meta_value);98 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );99 $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );100 if ( null !== $check )101 return (bool) $check;102 // Compare existing value to new value if no prev value given and the key exists only once.103 if ( empty($prev_value) ) {104 $old_value = get_metadata($meta_type, $object_id, $meta_key);105 if ( count($old_value) == 1 ) {106 if ( $old_value[0] === $meta_value )107 return false;108 }109 }110 if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )111 return add_metadata($meta_type, $object_id, $meta_key, $passed_value);112 $_meta_value = $meta_value;113 $meta_value = maybe_serialize( $meta_value );114 $data = compact( 'meta_value' );115 $where = array( $column => $object_id, 'meta_key' => $meta_key );116 if ( !empty( $prev_value ) ) {117 $prev_value = maybe_serialize($prev_value);118 $where['meta_value'] = $prev_value;119 }120 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );121 if ( 'post' == $meta_type )122 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );123 $result = $wpdb->update( $table, $data, $where );124 if ( ! $result )125 return false;126 wp_cache_delete($object_id, $meta_type . '_meta');127 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );128 if ( 'post' == $meta_type )129 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );130 return true;131}132/**133 * Delete metadata for the specified object.134 *135 * @since 2.9.0136 * @uses $wpdb WordPress database object for queries.137 * @uses do_action() Calls 'deleted_{$meta_type}_meta' after deleting with meta_id of138 * deleted metadata entries, object ID, meta key, and meta value139 *140 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)141 * @param int $object_id ID of the object metadata is for142 * @param string $meta_key Metadata key143 * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete metadata entries144 * with this value. Otherwise, delete all entries with the specified meta_key.145 * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries146 * for all objects, ignoring the specified object_id. Otherwise, only delete matching147 * metadata entries for the specified object_id.148 * @return bool True on successful delete, false on failure.149 */150function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false) {151 if ( !$meta_type || !$meta_key )152 return false;153 if ( (!$object_id = absint($object_id)) && !$delete_all )154 return false;155 if ( ! $table = _get_meta_table($meta_type) )156 return false;157 global $wpdb;158 $type_column = sanitize_key($meta_type . '_id');159 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';160 // expected_slashed ($meta_key)161 $meta_key = wp_unslash($meta_key);162 $meta_value = wp_unslash($meta_value);163 $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );164 if ( null !== $check )165 return (bool) $check;166 $_meta_value = $meta_value;167 $meta_value = maybe_serialize( $meta_value );168 $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );169 if ( !$delete_all )170 $query .= $wpdb->prepare(" AND $type_column = %d", $object_id );171 if ( $meta_value )172 $query .= $wpdb->prepare(" AND meta_value = %s", $meta_value );173 $meta_ids = $wpdb->get_col( $query );174 if ( !count( $meta_ids ) )175 return false;176 if ( $delete_all )177 $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) );178 do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );179 // Old-style action.180 if ( 'post' == $meta_type )181 do_action( 'delete_postmeta', $meta_ids );182 $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )";183 $count = $wpdb->query($query);184 if ( !$count )185 return false;186 if ( $delete_all ) {187 foreach ( (array) $object_ids as $o_id ) {188 wp_cache_delete($o_id, $meta_type . '_meta');189 }190 } else {191 wp_cache_delete($object_id, $meta_type . '_meta');192 }193 do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );194 // Old-style action.195 if ( 'post' == $meta_type )196 do_action( 'deleted_postmeta', $meta_ids );197 return true;198}199/**200 * Retrieve metadata for the specified object.201 *202 * @since 2.9.0203 *204 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)205 * @param int $object_id ID of the object metadata is for206 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for207 * the specified object.208 * @param bool $single Optional, default is false. If true, return only the first value of the209 * specified meta_key. This parameter has no effect if meta_key is not specified.210 * @return string|array Single metadata value, or array of values211 */212function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {213 if ( !$meta_type )214 return false;215 if ( !$object_id = absint($object_id) )216 return false;217 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );218 if ( null !== $check ) {219 if ( $single && is_array( $check ) )220 return $check[0];221 else222 return $check;223 }224 $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');225 if ( !$meta_cache ) {226 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );227 $meta_cache = $meta_cache[$object_id];228 }229 if ( !$meta_key )230 return $meta_cache;231 if ( isset($meta_cache[$meta_key]) ) {232 if ( $single )233 return maybe_unserialize( $meta_cache[$meta_key][0] );234 else235 return array_map('maybe_unserialize', $meta_cache[$meta_key]);236 }237 if ($single)238 return '';239 else240 return array();241}242/**243 * Determine if a meta key is set for a given object244 *245 * @since 3.3.0246 *247 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)248 * @param int $object_id ID of the object metadata is for249 * @param string $meta_key Metadata key.250 * @return boolean true of the key is set, false if not.251 */252function metadata_exists( $meta_type, $object_id, $meta_key ) {253 if ( ! $meta_type )254 return false;255 if ( ! $object_id = absint( $object_id ) )256 return false;257 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true );258 if ( null !== $check )259 return true;260 $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );261 if ( !$meta_cache ) {262 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );263 $meta_cache = $meta_cache[$object_id];264 }265 if ( isset( $meta_cache[ $meta_key ] ) )266 return true;267 return false;268}269/**270 * Get meta data by meta ID271 *272 * @since 3.3.0273 *274 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)275 * @param int $meta_id ID for a specific meta row276 * @return object Meta object or false.277 */278function get_metadata_by_mid( $meta_type, $meta_id ) {279 global $wpdb;280 if ( ! $meta_type )281 return false;282 if ( !$meta_id = absint( $meta_id ) )283 return false;284 if ( ! $table = _get_meta_table($meta_type) )285 return false;286 $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';287 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );288 if ( empty( $meta ) )289 return false;290 if ( isset( $meta->meta_value ) )291 $meta->meta_value = maybe_unserialize( $meta->meta_value );292 return $meta;293}294/**295 * Update meta data by meta ID296 *297 * @since 3.3.0298 *299 * @uses get_metadata_by_mid() Calls get_metadata_by_mid() to fetch the meta key, value300 * and object_id of the given meta_id.301 *302 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)303 * @param int $meta_id ID for a specific meta row304 * @param string $meta_value Metadata value305 * @param string $meta_key Optional, you can provide a meta key to update it306 * @return bool True on successful update, false on failure.307 */308function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {309 global $wpdb;310 // Make sure everything is valid.311 if ( ! $meta_type )312 return false;313 if ( ! $meta_id = absint( $meta_id ) )314 return false;315 if ( ! $table = _get_meta_table( $meta_type ) )316 return false;317 $column = sanitize_key($meta_type . '_id');318 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';319 // Fetch the meta and go on if it's found.320 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {321 $original_key = $meta->meta_key;322 $original_value = $meta->meta_value;323 $object_id = $meta->{$column};324 // If a new meta_key (last parameter) was specified, change the meta key,325 // otherwise use the original key in the update statement.326 if ( false === $meta_key ) {327 $meta_key = $original_key;328 } elseif ( ! is_string( $meta_key ) ) {329 return false;330 }331 // Sanitize the meta332 $_meta_value = $meta_value;333 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );334 $meta_value = maybe_serialize( $meta_value );335 // Format the data query arguments.336 $data = array(337 'meta_key' => $meta_key,338 'meta_value' => $meta_value339 );340 // Format the where query arguments.341 $where = array();342 $where[$id_column] = $meta_id;343 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );344 if ( 'post' == $meta_type )345 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );346 // Run the update query, all fields in $data are %s, $where is a %d.347 $result = $wpdb->update( $table, $data, $where, '%s', '%d' );348 if ( ! $result )349 return false;350 // Clear the caches.351 wp_cache_delete($object_id, $meta_type . '_meta');352 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );353 if ( 'post' == $meta_type )354 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );355 return true;356 }357 // And if the meta was not found.358 return false;359}360/**361 * Delete meta data by meta ID362 *363 * @since 3.3.0364 *365 * @uses get_metadata_by_mid() Calls get_metadata_by_mid() to fetch the meta key, value366 * and object_id of the given meta_id.367 *368 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)369 * @param int $meta_id ID for a specific meta row370 * @return bool True on successful delete, false on failure.371 */372function delete_metadata_by_mid( $meta_type, $meta_id ) {373 global $wpdb;374 // Make sure everything is valid.375 if ( ! $meta_type )376 return false;377 if ( ! $meta_id = absint( $meta_id ) )378 return false;379 if ( ! $table = _get_meta_table( $meta_type ) )380 return false;381 // object and id columns382 $column = sanitize_key($meta_type . '_id');383 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';384 // Fetch the meta and go on if it's found.385 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {386 $object_id = $meta->{$column};387 do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );388 // Old-style action.389 if ( 'post' == $meta_type || 'comment' == $meta_type )390 do_action( "delete_{$meta_type}meta", $meta_id );391 // Run the query, will return true if deleted, false otherwise392 $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) );393 // Clear the caches.394 wp_cache_delete($object_id, $meta_type . '_meta');395 do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );396 // Old-style action.397 if ( 'post' == $meta_type || 'comment' == $meta_type )398 do_action( "deleted_{$meta_type}meta", $meta_id );399 return $result;400 }401 // Meta id was not found.402 return false;403}404/**405 * Update the metadata cache for the specified objects.406 *407 * @since 2.9.0408 * @uses $wpdb WordPress database object for queries.409 *410 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)411 * @param int|array $object_ids array or comma delimited list of object IDs to update cache for412 * @return mixed Metadata cache for the specified objects, or false on failure.413 */414function update_meta_cache($meta_type, $object_ids) {415 if ( empty( $meta_type ) || empty( $object_ids ) )416 return false;417 if ( ! $table = _get_meta_table($meta_type) )418 return false;419 $column = sanitize_key($meta_type . '_id');420 global $wpdb;421 if ( !is_array($object_ids) ) {422 $object_ids = preg_replace('|[^0-9,]|', '', $object_ids);423 $object_ids = explode(',', $object_ids);424 }425 $object_ids = array_map('intval', $object_ids);426 $cache_key = $meta_type . '_meta';427 $ids = array();428 $cache = array();429 foreach ( $object_ids as $id ) {430 $cached_object = wp_cache_get( $id, $cache_key );431 if ( false === $cached_object )432 $ids[] = $id;433 else434 $cache[$id] = $cached_object;435 }436 if ( empty( $ids ) )437 return $cache;438 // Get meta info439 $id_list = join( ',', $ids );440 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';441 $meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A );442 if ( !empty($meta_list) ) {443 foreach ( $meta_list as $metarow) {444 $mpid = intval($metarow[$column]);445 $mkey = $metarow['meta_key'];446 $mval = $metarow['meta_value'];447 // Force subkeys to be array type:448 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )449 $cache[$mpid] = array();450 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )451 $cache[$mpid][$mkey] = array();452 // Add a value to the current pid/key:453 $cache[$mpid][$mkey][] = $mval;454 }455 }456 foreach ( $ids as $id ) {457 if ( ! isset($cache[$id]) )458 $cache[$id] = array();459 wp_cache_add( $id, $cache[$id], $cache_key );460 }461 return $cache;462}463/**464 * Given a meta query, generates SQL clauses to be appended to a main query465 *466 * @since 3.2.0467 *468 * @see WP_Meta_Query469 *470 * @param array $meta_query A meta query471 * @param string $type Type of meta472 * @param string $primary_table473 * @param string $primary_id_column474 * @param object $context (optional) The main query object475 * @return array( 'join' => $join_sql, 'where' => $where_sql )476 */477function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) {478 $meta_query_obj = new WP_Meta_Query( $meta_query );479 return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );480}481/**482 * Container class for a multiple metadata query483 *484 * @since 3.2.0485 */486class WP_Meta_Query {487 /**488 * List of metadata queries. A single query is an associative array:489 * - 'key' string The meta key490 * - 'value' string|array The meta value491 * - 'compare' (optional) string How to compare the key to the value.492 * Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN',493 * 'BETWEEN', 'NOT BETWEEN', 'REGEXP', 'NOT REGEXP', 'RLIKE'.494 * Default: '='495 * - 'type' string (optional) The type of the value.496 * Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.497 * Default: 'CHAR'498 *499 * @since 3.2.0500 * @access public501 * @var array502 */503 public $queries = array();504 /**505 * The relation between the queries. Can be one of 'AND' or 'OR'.506 *507 * @since 3.2.0508 * @access public509 * @var string510 */511 public $relation;512 /**513 * Constructor514 *515 * @param array $meta_query (optional) A meta query516 */517 function __construct( $meta_query = false ) {518 if ( !$meta_query )519 return;520 if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {521 $this->relation = 'OR';522 } else {523 $this->relation = 'AND';524 }525 $this->queries = array();526 foreach ( $meta_query as $key => $query ) {527 if ( ! is_array( $query ) )528 continue;529 $this->queries[] = $query;530 }531 }532 /**533 * Constructs a meta query based on 'meta_*' query vars534 *535 * @since 3.2.0536 * @access public537 *538 * @param array $qv The query variables539 */540 function parse_query_vars( $qv ) {541 $meta_query = array();542 // Simple query needs to be first for orderby=meta_value to work correctly543 foreach ( array( 'key', 'compare', 'type' ) as $key ) {544 if ( !empty( $qv[ "meta_$key" ] ) )545 $meta_query[0][ $key ] = $qv[ "meta_$key" ];546 }547 // WP_Query sets 'meta_value' = '' by default548 if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] && ( ! is_array( $qv[ 'meta_value' ] ) || $qv[ 'meta_value' ] ) )549 $meta_query[0]['value'] = $qv[ 'meta_value' ];550 if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {551 $meta_query = array_merge( $meta_query, $qv['meta_query'] );552 }553 $this->__construct( $meta_query );554 }555 /**556 * Given a meta type, return the appropriate alias if applicable557 *558 * @since 3.7.0559 *560 * @param string $type MySQL type to cast meta_value561 * @return string MySQL type562 */563 function get_cast_for_type( $type = '' ) {564 if ( empty( $type ) )565 return 'CHAR';566 $meta_type = strtoupper( $type );567 if ( ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $meta_type ) )568 return 'CHAR';569 if ( 'NUMERIC' == $meta_type )570 $meta_type = 'SIGNED';571 return $meta_type;572 }573 /**574 * Generates SQL clauses to be appended to a main query.575 *576 * @since 3.2.0577 * @access public578 *579 * @param string $type Type of meta580 * @param string $primary_table581 * @param string $primary_id_column582 * @param object $context (optional) The main query object583 * @return array( 'join' => $join_sql, 'where' => $where_sql )584 */585 function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {586 global $wpdb;587 if ( ! $meta_table = _get_meta_table( $type ) )588 return false;589 $meta_id_column = sanitize_key( $type . '_id' );590 $join = array();591 $where = array();592 $key_only_queries = array();593 $queries = array();594 // Split out the queries with empty arrays as value595 foreach ( $this->queries as $k => $q ) {596 if ( isset( $q['value'] ) && is_array( $q['value'] ) && empty( $q['value'] ) ) {597 $key_only_queries[$k] = $q;598 unset( $this->queries[$k] );599 }600 }601 // Split out the meta_key only queries (we can only do this for OR)602 if ( 'OR' == $this->relation ) {603 foreach ( $this->queries as $k => $q ) {604 if ( ! array_key_exists( 'value', $q ) && ! empty( $q['key'] ) )605 $key_only_queries[$k] = $q;606 else607 $queries[$k] = $q;608 }609 } else {610 $queries = $this->queries;611 }612 // Specify all the meta_key only queries in one go613 if ( $key_only_queries ) {614 $join[] = "INNER JOIN $meta_table ON $primary_table.$primary_id_column = $meta_table.$meta_id_column";615 foreach ( $key_only_queries as $key => $q )616 $where["key-only-$key"] = $wpdb->prepare( "$meta_table.meta_key = %s", trim( $q['key'] ) );617 }618 foreach ( $queries as $k => $q ) {619 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';620 $meta_type = $this->get_cast_for_type( isset( $q['type'] ) ? $q['type'] : '' );621 if ( array_key_exists( 'value', $q ) && is_null( $q['value'] ) )622 $q['value'] = '';623 $meta_value = isset( $q['value'] ) ? $q['value'] : null;624 if ( isset( $q['compare'] ) )625 $meta_compare = strtoupper( $q['compare'] );626 else627 $meta_compare = is_array( $meta_value ) ? 'IN' : '=';628 if ( ! in_array( $meta_compare, array(629 '=', '!=', '>', '>=', '<', '<=',630 'LIKE', 'NOT LIKE',631 'IN', 'NOT IN',632 'BETWEEN', 'NOT BETWEEN',633 'NOT EXISTS',634 'REGEXP', 'NOT REGEXP', 'RLIKE'635 ) ) )636 $meta_compare = '=';637 $i = count( $join );638 $alias = $i ? 'mt' . $i : $meta_table;639 if ( 'NOT EXISTS' == $meta_compare ) {640 $join[$i] = "LEFT JOIN $meta_table";641 $join[$i] .= $i ? " AS $alias" : '';642 $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column AND $alias.meta_key = '$meta_key')";643 $where[$k] = ' ' . $alias . '.' . $meta_id_column . ' IS NULL';644 continue;645 }646 $join[$i] = "INNER JOIN $meta_table";647 $join[$i] .= $i ? " AS $alias" : '';648 $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";649 $where[$k] = '';650 if ( !empty( $meta_key ) )651 $where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key );652 if ( is_null( $meta_value ) ) {653 if ( empty( $where[$k] ) )654 unset( $join[$i] );655 continue;656 }657 if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {658 if ( ! is_array( $meta_value ) )659 $meta_value = preg_split( '/[,\s]+/', $meta_value );660 if ( empty( $meta_value ) ) {661 unset( $join[$i] );662 continue;663 }664 } else {665 $meta_value = trim( $meta_value );666 }667 if ( 'IN' == substr( $meta_compare, -2) ) {668 $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';669 } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {670 $meta_value = array_slice( $meta_value, 0, 2 );671 $meta_compare_string = '%s AND %s';672 } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {673 $meta_value = '%' . like_escape( $meta_value ) . '%';674 $meta_compare_string = '%s';675 } else {676 $meta_compare_string = '%s';677 }678 if ( ! empty( $where[$k] ) )679 $where[$k] .= ' AND ';680 $where[$k] = ' (' . $where[$k] . $wpdb->prepare( "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string})", $meta_value );681 }682 $where = array_filter( $where );683 if ( empty( $where ) )684 $where = '';685 else686 $where = ' AND (' . implode( "\n{$this->relation} ", $where ) . ' )';687 $join = implode( "\n", $join );688 if ( ! empty( $join ) )689 $join = ' ' . $join;690 return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) );691 }692}693/**694 * Retrieve the name of the metadata table for the specified object type.695 *696 * @since 2.9.0697 * @uses $wpdb WordPress database object for queries.698 *699 * @param string $type Type of object to get metadata table for (e.g., comment, post, or user)700 * @return mixed Metadata table name, or false if no metadata table exists701 */702function _get_meta_table($type) {703 global $wpdb;704 $table_name = $type . 'meta';705 if ( empty($wpdb->$table_name) )706 return false;707 return $wpdb->$table_name;708}709/**710 * Determine whether a meta key is protected711 *712 * @since 3.1.3713 *714 * @param string $meta_key Meta key715 * @return bool True if the key is protected, false otherwise.716 */717function is_protected_meta( $meta_key, $meta_type = null ) {718 $protected = ( '_' == $meta_key[0] );719 return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );720}721/**722 * Sanitize meta value723 *724 * @since 3.1.3725 *726 * @param string $meta_key Meta key727 * @param mixed $meta_value Meta value to sanitize728 * @param string $meta_type Type of meta729 * @return mixed Sanitized $meta_value730 */731function sanitize_meta( $meta_key, $meta_value, $meta_type ) {732 return apply_filters( "sanitize_{$meta_type}_meta_{$meta_key}", $meta_value, $meta_key, $meta_type );733}734/**735 * Register meta key736 *737 * @since 3.3.0738 *739 * @param string $meta_type Type of meta740 * @param string $meta_key Meta key741 * @param string|array $sanitize_callback A function or method to call when sanitizing the value of $meta_key.742 * @param string|array $auth_callback Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks.743 * @param array $args Arguments744 */745function register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null ) {746 if ( is_callable( $sanitize_callback ) )747 add_filter( "sanitize_{$meta_type}_meta_{$meta_key}", $sanitize_callback, 10, 3 );748 if ( empty( $auth_callback ) ) {749 if ( is_protected_meta( $meta_key, $meta_type ) )750 $auth_callback = '__return_false';751 else752 $auth_callback = '__return_true';753 }754 if ( is_callable( $auth_callback ) )...

Full Screen

Full Screen

Meta

Using AI Code Generation

copy

Full Screen

1$meta = new Meta();2$meta->setMeta("title", "My Title");3$meta->setMeta("description", "My Description");4$meta->setMeta("keywords", "My Keywords");5$meta->setMeta("robots", "index, follow");6$meta->setMeta("author", "My Name");7$meta->setMeta("viewport", "width=device-width, initial-scale=1.0");8$meta->setMeta("revisit-after", "30 days");9$meta->setMeta("og:title", "My Title");10$meta->setMeta("og:description", "My Description");11$meta->setMeta("og:site_name", "Example Site");12$meta->setMeta("og:type", "website");13$meta->setMeta("og:locale", "en_US");14$meta->setMeta("og:locale:alternate", "en_GB");15$meta->setMeta("og:locale:alternate", "fr_FR");16$meta->setMeta("twitter:card", "summary");17$meta->setMeta("twitter:site", "@ExampleSite");18$meta->setMeta("twitter:title", "My Title");19$meta->setMeta("twitter:description", "My Description");20$meta->setMeta("twitter:creator", "@ExampleAuthor");21$link = new Link();

Full Screen

Full Screen

Meta

Using AI Code Generation

copy

Full Screen

1require_once("Meta.php");2$meta = new Meta();3$meta->getMetaData();4$meta->displayMetaData();5$meta->displayMetaDataInTable();6$meta->displayMetaDataInTableWithBorder();7$meta->displayMetaDataInTableWithBorderAndCaption();8$meta->displayMetaDataInTableWithBorderCaptionAndHeader();9$meta->displayMetaDataInTableWithBorderCaptionHeaderAndFooter();10$meta->displayMetaDataInTableWithBorderCaptionHeaderFooterAndBackgroundColor();11$meta->displayMetaDataInTableWithBorderCaptionHeaderFooterBackgroundColorAndWidth();12$meta->displayMetaDataInTableWithBorderCaptionHeaderFooterBackgroundColorWidthAndHeight();13$meta->displayMetaDataInTableWithBorderCaptionHeaderFooterBackgroundColorWidthHeightAndFont();14$meta->displayMetaDataInTableWithBorderCaptionHeaderFooterBackgroundColorWidthHeightFontAndFontSize();

Full Screen

Full Screen

Meta

Using AI Code Generation

copy

Full Screen

1$meta = new Meta();2$meta->readFile('features/2.feature');3print_r($meta->getFeature());4print_r($meta->getScenario());5print_r($meta->getGiven());6print_r($meta->getWhen());7print_r($meta->getThen());8$meta->writeFile('features/2.feature');9$meta = new Meta();10$meta->readFile('features/2.feature');11print_r($meta->getFeature());12print_r($meta->getScenario());13print_r($meta->getGiven());14print_r($meta->getWhen());15print_r($meta->getThen());16$meta->writeFile('features/2.feature');17$meta = new Meta();18$meta->readFile('features/2.feature');19print_r($meta->getFeature());20print_r($meta->getScenario());21print_r($meta->getGiven());22print_r($meta->getWhen());23print_r($meta->getThen());24$meta->writeFile('features/2.feature');25$meta = new Meta();26$meta->readFile('features/2.feature');27print_r($meta->getFeature());28print_r($meta->getScenario());29print_r($meta->getGiven());30print_r($meta->getWhen());31print_r($meta->getThen());32$meta->writeFile('features/2.feature');

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Cucumber Common Library automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful