* @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; } }