return array */ private function get_registered_meta_keys( $object_subtype = '' ) { // Get the object type $object_type = $this->get_meta_type(); // Return the keys return get_registered_meta_keys( $object_type, $object_subtype ); } /** * Maybe update meta values on item update/save. * * @since 1.0.0 * * @param array $meta */ private function save_extra_item_meta( $item_id = 0, $meta = array() ) { // Bail if there is no bulk meta to save $item_id = $this->shape_item_id( $item_id ); if ( empty( $item_id ) || empty( $meta ) ) { return; } // Bail if no meta table exists if ( false === $this->get_meta_table_name() ) { return; } // Only save registered keys $keys = $this->get_registered_meta_keys(); $meta = array_intersect_key( $meta, $keys ); // Bail if no registered meta keys if ( empty( $meta ) ) { return; } // Save or delete meta data foreach ( $meta as $key => $value ) { ! empty( $value ) ? $this->update_item_meta( $item_id, $key, $value ) : $this->delete_item_meta( $item_id, $key ); } } /** * Delete all meta data for an item. * * @since 1.0.0 * * @param int $item_id */ private function delete_all_item_meta( $item_id = 0 ) { // Bail if no meta was returned $item_id = $this->shape_item_id( $item_id ); if ( empty( $item_id ) ) { return; } // Get the meta table name $table = $this->get_meta_table_name(); // Bail if no meta table exists if ( empty( $table ) ) { return; } // Guess the item ID column for the meta table $primary_id = $this->get_primary_column_name(); $item_id_column = $this->apply_prefix( "{$this->item_name}_{$primary_id}" ); // Get meta IDs $query = "SELECT meta_id FROM {$table} WHERE {$item_id_column} = %d"; $prepared = $this->get_db()->prepare( $query, $item_id ); $meta_ids = $this->get_db()->get_col( $prepared ); // Bail if no meta IDs to delete if ( empty( $meta_ids ) ) { return; } // Get the meta type $meta_type = $this->get_meta_type(); // Delete all meta data for this item ID foreach ( $meta_ids as $mid ) { delete_metadata_by_mid( $meta_type, $mid ); } } /** * Get the meta table for this query. * * Forked from WordPress\_get_meta_table() so it can be more accurately * predicted in a future iteration and default to returning false. * * @since 1.0.0 * * @return mixed Table name if exists, False if not */ private function get_meta_table_name() { // Get the meta-type $type = $this->get_meta_type(); // Append "meta" to end of meta-type $table_name = "{$type}meta"; // Variable'ize the database interface, to use inside empty() $db = $this->get_db(); // If not empty, return table name if ( ! empty( $db->{$table_name} ) ) { return $table_name; } // Default return false return false; } /** * Get the meta type for this query. * * This method exists to reduce some duplication for now. Future iterations * will likely use Column::relationships to * * @since 1.1.0 * * @return string */ private function get_meta_type() { return $this->apply_prefix( $this->item_name ); } /** Cache *****************************************************************/ /** * Get cache key from query_vars and query_var_defaults. * * @since 1.0.0 * * @return string */ private function get_cache_key( $group = '' ) { // Slice query vars $slice = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); // Unset `fields` so it does not effect the cache key unset( $slice['fields'] ); // Setup key & last_changed $key = md5( serialize( $slice ) ); $last_changed = $this->get_last_changed_cache( $group ); // Concatenate and return cache key return "get_{$this->item_name_plural}:{$key}:{$last_changed}"; } /** * Get the cache group, or fallback to the primary one. * * @since 1.0.0 * * @param string $group * @return string */ private function get_cache_group( $group = '' ) { // Get the primary column $primary = $this->get_primary_column_name(); // Default return value $retval = $this->cache_group; // Only allow non-primary groups if ( ! empty( $group ) && ( $group !== $primary ) ) { $retval = $group; } // Return the group return $retval; } /** * Get array of which database columns have uniquely cached groups. * * @since 1.0.0 * * @return array */ private function get_cache_groups() { // Return value $cache_groups = array(); // Get the cache groups $groups = $this->get_columns( array( 'cache_key' => true ), 'and', 'name' ); if ( ! empty( $groups ) ) { // Get the primary column name $primary = $this->get_primary_column_name(); // Setup return values foreach ( $groups as $name ) { if ( $primary !== $name ) { $cache_groups[ $name ] = "{$this->cache_group}-by-{$name}"; } else { $cache_groups[ $name ] = $this->cache_group; } } } // Return cache groups array return $cache_groups; } /** * Maybe prime item & item-meta caches by querying 1 time for all un-cached * items. * * Accepts a single ID, or an array of IDs. * * The reason this accepts only IDs is because it gets called immediately * after an item is inserted in the database, but before items have been * "shaped" into proper objects, so object properties may not be set yet. * * @since 1.0.0 * * @param array $item_ids * @param bool $force * * @return bool False if empty */ private function prime_item_caches( $item_ids = array(), $force = false ) { // Bail if no items to cache if ( empty( $item_ids ) ) { return false; } // Accepts single values, so cast to array $item_ids = (array) $item_ids; // Update item caches if ( ! empty( $force ) || ! empty( $this->query_vars['update_item_cache'] ) ) { // Look for non-cached IDs $ids = $this->get_non_cached_ids( $item_ids, $this->cache_group ); // Bail if IDs are cached if ( empty( $ids ) ) { return false; } // Get query parts $table = $this->get_table_name(); $primary = $this->get_primary_column_name(); // Query database $query = "SELECT * FROM {$table} WHERE {$primary} IN (%s)"; $ids = implode( ',', array_map( 'absint', $ids ) ); $prepare = sprintf( $query, $ids ); $results = $this->get_db()->get_results( $prepare ); // Update item caches $this->update_item_cache( $results ); } // Update meta data caches if ( ! empty( $this->query_vars['update_meta_cache'] ) ) { $singular = rtrim( $this->table_name, 's' ); // sic update_meta_cache( $singular, $item_ids ); } } /** * Update the cache for an item. Does not update item-meta cache. * * Accepts a single object, or an array of objects. * * The reason this does not accept ID's is because this gets called * after an item is already updated in the database, so we want to avoid * querying for it again. It's just safer this way. * * @since 1.0.0 * * @param array $items */ private function update_item_cache( $items = array() ) { // Maybe query for single item if ( is_numeric( $items ) ) { $primary = $this->get_primary_column_name(); $items = $this->get_item_raw( $primary, $items ); } // Bail if no items to cache if ( empty( $items ) ) { return false; } // Make sure items are an array (without casting objects to arrays) if ( ! is_array( $items ) ) { $items = array( $items ); } // Get the cache groups $groups = $this->get_cache_groups(); // Loop through all items and cache them foreach ( $items as $item ) { // Skip if item is not an object if ( ! is_object( $item ) ) { continue; } // Loop through groups and set cache if ( ! empty( $groups ) ) { foreach ( $groups as $key => $group ) { $this->cache_set( $item->{$key}, $item, $group ); } } } // Update last changed $this->update_last_changed_cache(); } /** * Clean the cache for an item. Does not clean item-meta. * * Accepts a single object, or an array of objects. * * The reason this does not accept ID's is because this gets called * after an item is already deleted from the database, so it cannot be * queried and may not exist in the cache. It's just safer this way. * * @since 1.0.0 * * @param mixed $items Single object item, or Array of object items * * @return bool */ private function clean_item_cache( $items = array() ) { // Bail if no items to clean if ( empty( $items ) ) { return false; } // Make sure items are an array if ( ! is_array( $items ) ) { $items = array( $items ); } // Get the cache groups $groups = $this->get_cache_groups(); // Loop through all items and clean them foreach ( $items as $item ) { // Skip if item is not an object if ( ! is_object( $item ) ) { continue; } // Loop through groups and delete cache if ( ! empty( $groups ) ) { foreach ( $groups as $key => $group ) { $this->cache_delete( $item->{$key}, $group ); } } } // Update last changed $this->update_last_changed_cache(); } /** * Update the last_changed key for the cache group. * * @since 1.0.0 * * @return string The last time a cache group was changed. */ private function update_last_changed_cache( $group = '' ) { // Fallback to microtime if ( empty( $this->last_changed ) ) { $this->set_last_changed(); } // Set the last changed time for this cache group $this->cache_set( 'last_changed', $this->last_changed, $group ); // Return the last changed time return $this->last_changed; } /** * Get the last_changed key for a cache group. * * @since 1.0.0 * * @param string $group Cache group. Defaults to $this->cache_group * * @return string The last time a cache group was changed. */ private function get_last_changed_cache( $group = '' ) { // Get the last changed cache value $last_changed = $this->cache_get( 'last_changed', $group ); // Maybe update the last changed value if ( false === $last_changed ) { $last_changed = $this->update_last_changed_cache( $group ); } // Return the last changed value for the cache group return $last_changed; } /** * Get array of non-cached item IDs. * * @since 1.0.0 * * @param array $item_ids Array of item IDs * @param string $group Cache group. Defaults to $this->cache_group * * @return array */ private function get_non_cached_ids( $item_ids = array(), $group = '' ) { $retval = array(); // Bail if no item IDs if ( empty( $item_ids ) ) { return $retval; } // Loop through item IDs foreach ( $item_ids as $id ) { $id = $this->shape_item_id( $id ); if ( false === $this->cache_get( $id, $group ) ) { $retval[] = $id; } } // Return array of IDs return $retval; } /** * Add a cache value for a key and group. * * @since 1.0.0 * * @param string $key Cache key. * @param mixed $value Cache value. * @param string $group Cache group. Defaults to $this->cache_group * @param int $expire Expiration. */ private function cache_add( $key = '', $value = '', $group = '', $expire = 0 ) { // Bail if cache invalidation is suspended if ( wp_suspend_cache_addition() ) { return; } // Bail if no cache key if ( empty( $key ) ) { return; } // Get the cache group $group = $this->get_cache_group( $group ); // Add to the cache wp_cache_add( $key, $value, $group, $expire ); } /** * Get a cache value for a key and group. * * @since 1.0.0 * * @param string $key Cache key. * @param string $group Cache group. Defaults to $this->cache_group * @param bool $force */ private function cache_get( $key = '', $group = '', $force = false ) { // Bail if no cache key if ( empty( $key ) ) { return; } // Get the cache group $group = $this->get_cache_group( $group ); // Return from the cache return wp_cache_get( $key, $group, $force ); } /** * Set a cache value for a key and group. * * @since 1.0.0 * * @param string $key Cache key. * @param mixed $value Cache value. * @param string $group Cache group. Defaults to $this->cache_group * @param int $expire Expiration. */ private function cache_set( $key = '', $value = '', $group = '', $expire = 0 ) { // Bail if cache invalidation is suspended if ( wp_suspend_cache_addition() ) { return; } // Bail if no cache key if ( empty( $key ) ) { return; } // Get the cache group $group = $this->get_cache_group( $group ); // Update the cache wp_cache_set( $key, $value, $group, $expire ); } /** * Delete a cache key for a group. * * @since 1.0.0 * * @global bool $_wp_suspend_cache_invalidation * * @param string $key Cache key. * @param string $group Cache group. Defaults to $this->cache_group */ private function cache_delete( $key = '', $group = '' ) { global $_wp_suspend_cache_invalidation; // Bail if cache invalidation is suspended if ( ! empty( $_wp_suspend_cache_invalidation ) ) { return; } // Bail if no cache key if ( empty( $key ) ) { return; } // Get the cache group $group = $this->get_cache_group( $group ); // Delete the cache wp_cache_delete( $key, $group ); } /** * Fetch raw results directly from the database. * * @since 1.0.0 * * @param array $cols Columns for `SELECT`. * @param array $where_cols Where clauses. Each key-value pair in the array * represents a column and a comparison. * @param int $limit Optional. LIMIT value. Default 25. * @param null $offset Optional. OFFSET value. Default null. * @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. * Default OBJECT. * With one of the first three, return an array of * rows indexed from 0 by SQL result row number. * Each row is an associative array (column => value, ...), * a numerically indexed array (0 => value, ...), * or an object. ( ->column = value ), respectively. * With OBJECT_K, return an associative array of * row objects keyed by the value of each row's * first column's value. * * @return array|object|null Database query results. */ public function get_results( $cols = array(), $where_cols = array(), $limit = 25, $offset = null, $output = OBJECT ) { // Bail if no columns have been passed if ( empty( $cols ) ) { return null; } // Fetch all the columns for the table being queried $column_names = $this->get_column_names(); // Ensure valid column names have been passed for the `SELECT` clause foreach ( $cols as $index => $column ) { if ( ! array_key_exists( $column, $column_names ) ) { unset( $cols[ $index ] ); } } // Columns to retrieve $columns = implode( ',', $cols ); // Get the table name $table = $this->get_table_name(); // Setup base query $query = implode( ' ', array( "SELECT", $columns, "FROM {$table} {$this->table_alias}", "WHERE 1=1" ) ); // Ensure valid columns have been passed for the `WHERE` clause if ( ! empty( $where_cols ) ) { // Get keys from where columns $columns = array_keys( $where_cols ); // Loop through columns and unset any invalid names foreach ( $columns as $index => $column ) { if ( ! array_key_exists( $column, $column_names ) ) { unset( $where_cols[ $index ] ); } } // Parse WHERE clauses foreach ( $where_cols as $column => $compare ) { // Basic WHERE clause if ( ! is_array( $compare ) ) { $pattern = $this->get_column_field( array( 'name' => $column ), 'pattern', '%s' ); $statement = " AND {$this->table_alias}.{$column} = {$pattern} "; $query .= $this->get_db()->prepare( $statement, $compare ); // More complex WHERE clause } else { $value = isset( $compare['value'] ) ? $compare['value'] : false; // Skip if a value was not provided if ( false === $value ) { continue; } // Default compare clause to equals $compare_clause = isset( $compare['compare_query'] ) ? trim( strtoupper( $compare['compare_query'] ) ) : '='; // Array (unprepared) if ( is_array( $compare['value'] ) ) { // Default to IN if clause not specified if ( ! in_array( $compare_clause, array( 'IN', 'NOT IN', 'BETWEEN' ), true ) ) { $compare_clause = 'IN'; } // Parse & escape for IN and NOT IN if ( 'IN' === $compare_clause || 'NOT IN' === $compare_clause ) { $value = "('" . implode( "','", $this->get_db()->_escape( $compare['value'] ) ) . "')"; // Parse & escape for BETWEEN } elseif ( is_array( $value ) && 2 === count( $value ) && 'BETWEEN' === $compare_clause ) { $_this = $this->get_db()->_escape( $value[0] ); $_that = $this->get_db()->_escape( $value[1] ); $value = " {$_this} AND {$_that} "; } } // Add WHERE clause $query .= " AND {$this->table_alias}.{$column} {$compare_clause} {$value} "; } } } // Maybe set an offset if ( ! empty( $offset ) ) { $values = explode( ',', $offset ); $values = array_filter( $values, 'intval' ); $offset = implode( ',', $values ); $query .= " OFFSET {$offset} "; } // Maybe set a limit if ( ! empty( $limit ) && ( $limit > 0 ) ) { $limit = intval( $limit ); $query .= " LIMIT {$limit} "; } // Execute query $results = $this->get_db()->get_results( $query, $output ); // Return results return $results; } }