[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BuddyPress Activity Classes 4 * 5 * @package BuddyPress 6 * @subpackage ActivityQuery 7 * @since 2.2.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * Class for generating the WHERE SQL clause for advanced activity fetching. 15 * 16 * This is notably used in {@link BP_Activity_Activity::get()} with the 17 * 'filter_query' parameter. 18 * 19 * @since 2.2.0 20 */ 21 class BP_Activity_Query extends BP_Recursive_Query { 22 /** 23 * Array of activity queries. 24 * 25 * See {@see BP_Activity_Query::__construct()} for information on query arguments. 26 * 27 * @since 2.2.0 28 * @var array 29 */ 30 public $queries = array(); 31 32 /** 33 * Table alias. 34 * 35 * @since 2.2.0 36 * @var string 37 */ 38 public $table_alias = ''; 39 40 /** 41 * Supported DB columns. 42 * 43 * See the 'wp_bp_activity' DB table schema. 44 * 45 * @since 2.2.0 46 * @var array 47 */ 48 public $db_columns = array( 49 'id', 'user_id', 'component', 'type', 'action', 'content', 'primary_link', 50 'item_id', 'secondary_item_id', 'hide_sitewide', 'is_spam', 51 ); 52 53 /** 54 * Constructor. 55 * 56 * @since 2.2.0 57 * 58 * @param array $query { 59 * Array of query clauses. 60 * @type array { 61 * @type string $column Required. The column to query against. Basically, any DB column in the main 62 * 'wp_bp_activity' table. 63 * @type string $value Required. Value to filter by. 64 * @type string $compare Optional. The comparison operator. Default '='. 65 * Accepts '=', '!=', '>', '>=', '<', '<=', 'IN', 'NOT IN', 'LIKE', 66 * 'NOT LIKE', BETWEEN', 'NOT BETWEEN', 'REGEXP', 'NOT REGEXP', 'RLIKE'. 67 * @type string $relation Optional. The boolean relationship between the activity queries. 68 * Accepts 'OR', 'AND'. Default 'AND'. 69 * @type array { 70 * Optional. Another fully-formed activity query. See parameters above. 71 * } 72 * } 73 * } 74 */ 75 public function __construct( $query = array() ) { 76 if ( ! is_array( $query ) ) { 77 return; 78 } 79 80 $this->queries = $this->sanitize_query( $query ); 81 } 82 83 /** 84 * Generates WHERE SQL clause to be appended to a main query. 85 * 86 * @since 2.2.0 87 * 88 * @param string $alias An existing table alias that is compatible with the current query clause. 89 * Default: 'a'. BP_Activity_Activity::get() uses 'a', so we default to that. 90 * @return string SQL fragment to append to the main WHERE clause. 91 */ 92 public function get_sql( $alias = 'a' ) { 93 if ( ! empty( $alias ) ) { 94 $this->table_alias = sanitize_title( $alias ); 95 } 96 97 $sql = $this->get_sql_clauses(); 98 99 // We only need the 'where' clause. 100 // 101 // Also trim trailing "AND" clause from parent BP_Recursive_Query class 102 // since it's not necessary for our needs. 103 return preg_replace( '/^\sAND/', '', $sql['where'] ); 104 } 105 106 /** 107 * Generate WHERE clauses for a first-order clause. 108 * 109 * @since 2.2.0 110 * 111 * @param array $clause Array of arguments belonging to the clause. 112 * @param array $parent_query Parent query to which the clause belongs. 113 * @return array { 114 * @type array $where Array of subclauses for the WHERE statement. 115 * @type array $join Empty array. Not used. 116 * } 117 */ 118 protected function get_sql_for_clause( $clause, $parent_query ) { 119 global $wpdb; 120 121 $sql_chunks = array( 122 'where' => array(), 123 'join' => array(), 124 ); 125 126 $column = isset( $clause['column'] ) ? $this->validate_column( $clause['column'] ) : ''; 127 $value = isset( $clause['value'] ) ? $clause['value'] : ''; 128 if ( empty( $column ) || ! isset( $clause['value'] ) ) { 129 return $sql_chunks; 130 } 131 132 if ( isset( $clause['compare'] ) ) { 133 $clause['compare'] = strtoupper( $clause['compare'] ); 134 } else { 135 $clause['compare'] = isset( $clause['value'] ) && is_array( $clause['value'] ) ? 'IN' : '='; 136 } 137 138 // Default 'compare' to '=' if no valid operator is found. 139 if ( ! in_array( $clause['compare'], array( 140 '=', '!=', '>', '>=', '<', '<=', 141 'LIKE', 'NOT LIKE', 142 'IN', 'NOT IN', 143 'BETWEEN', 'NOT BETWEEN', 144 'REGEXP', 'NOT REGEXP', 'RLIKE' 145 ) ) ) { 146 $clause['compare'] = '='; 147 } 148 149 $compare = $clause['compare']; 150 151 $alias = ! empty( $this->table_alias ) ? "{$this->table_alias}." : ''; 152 153 // Next, Build the WHERE clause. 154 $where = ''; 155 156 // Value. 157 if ( isset( $clause['value'] ) ) { 158 if ( in_array( $compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { 159 if ( ! is_array( $value ) ) { 160 $value = preg_split( '/[,\s]+/', $value ); 161 } 162 } 163 164 // Tinyint. 165 if ( ! empty( $column ) && true === in_array( $column, array( 'hide_sitewide', 'is_spam' ) ) ) { 166 $sql_chunks['where'][] = $wpdb->prepare( "{$alias}{$column} = %d", $value ); 167 168 } else { 169 switch ( $compare ) { 170 // IN uses different syntax. 171 case 'IN' : 172 case 'NOT IN' : 173 $in_sql = BP_Activity_Activity::get_in_operator_sql( "{$alias}{$column}", $value ); 174 175 // 'NOT IN' operator is as easy as a string replace! 176 if ( 'NOT IN' === $compare ) { 177 $in_sql = str_replace( 'IN', 'NOT IN', $in_sql ); 178 } 179 180 $sql_chunks['where'][] = $in_sql; 181 break; 182 183 case 'BETWEEN' : 184 case 'NOT BETWEEN' : 185 $value = array_slice( $value, 0, 2 ); 186 $where = $wpdb->prepare( '%s AND %s', $value ); 187 break; 188 189 case 'LIKE' : 190 case 'NOT LIKE' : 191 $value = '%' . bp_esc_like( $value ) . '%'; 192 $where = $wpdb->prepare( '%s', $value ); 193 break; 194 195 default : 196 $where = $wpdb->prepare( '%s', $value ); 197 break; 198 199 } 200 } 201 202 if ( $where ) { 203 $sql_chunks['where'][] = "{$alias}{$column} {$compare} {$where}"; 204 } 205 } 206 207 /* 208 * Multiple WHERE clauses should be joined in parentheses. 209 */ 210 if ( 1 < count( $sql_chunks['where'] ) ) { 211 $sql_chunks['where'] = array( '( ' . implode( ' AND ', $sql_chunks['where'] ) . ' )' ); 212 } 213 214 return $sql_chunks; 215 } 216 217 /** 218 * Determine whether a clause is first-order. 219 * 220 * @since 2.2.0 221 * 222 * @param array $query Clause to check. 223 * @return bool 224 */ 225 protected function is_first_order_clause( $query ) { 226 return isset( $query['column'] ) || isset( $query['value'] ); 227 } 228 229 /** 230 * Validates a column name parameter. 231 * 232 * Column names are checked against a list of known tables. 233 * See {@link BP_Activity_Query::db_tables}. 234 * 235 * @since 2.2.0 236 * 237 * @param string $column The user-supplied column name. 238 * @return string A validated column name value. 239 */ 240 public function validate_column( $column = '' ) { 241 if ( in_array( $column, $this->db_columns ) ) { 242 return $column; 243 } else { 244 return ''; 245 } 246 } 247 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:57 2024 | Cross-referenced by PHPXref 0.7.1 |