BP_Activity_Query

Class for generating the WHERE SQL clause for advanced activity fetching.

Defined (1)

The class is defined in the following location(s).

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