function get_filter_sql($filter_array) { global $nxtdb; if (!empty($filter_array['user_id'])) { $user_sql = BP_Activity_Activity::get_in_operator_sql('a.user_id', $filter_array['user_id']); if (!empty($user_sql)) { $filter_sql[] = $user_sql; } } if (!empty($filter_array['object'])) { $object_sql = BP_Activity_Activity::get_in_operator_sql('a.component', $filter_array['object']); if (!empty($object_sql)) { $filter_sql[] = $object_sql; } } if (!empty($filter_array['action'])) { $action_sql = BP_Activity_Activity::get_in_operator_sql('a.type', $filter_array['action']); if (!empty($action_sql)) { $filter_sql[] = $action_sql; } } if (!empty($filter_array['primary_id'])) { $pid_sql = BP_Activity_Activity::get_in_operator_sql('a.item_id', $filter_array['primary_id']); if (!empty($pid_sql)) { $filter_sql[] = $pid_sql; } } if (!empty($filter_array['secondary_id'])) { $sid_sql = BP_Activity_Activity::get_in_operator_sql('a.secondary_item_id', $filter_array['secondary_id']); if (!empty($sid_sql)) { $filter_sql[] = $sid_sql; } } if (empty($filter_sql)) { return false; } return join(' AND ', $filter_sql); }
/** * Generate WHERE clauses for a first-order clause. * * @since BuddyPress (2.2.0) * @access protected * * @param array $clause Array of arguments belonging to the clause. * @param array $parent_query Parent query to which the clause belongs. * @return array { * @type array $where Array of subclauses for the WHERE statement. * @type array $join Empty array. Not used. * } */ protected function get_sql_for_clause($clause, $parent_query) { global $wpdb; $sql_chunks = array('where' => array(), 'join' => array()); $column = isset($clause['column']) ? $this->validate_column($clause['column']) : ''; $value = isset($clause['value']) ? $clause['value'] : ''; if (empty($column) || !isset($clause['value'])) { return $sql_chunks; } if (isset($clause['compare'])) { $clause['compare'] = strtoupper($clause['compare']); } else { $clause['compare'] = isset($clause['value']) && is_array($clause['value']) ? 'IN' : '='; } // default 'compare' to '=' if no valid operator is found if (!in_array($clause['compare'], array('=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'REGEXP', 'NOT REGEXP', 'RLIKE'))) { $clause['compare'] = '='; } $compare = $clause['compare']; $alias = !empty($this->table_alias) ? "{$this->table_alias}." : ''; // Next, Build the WHERE clause. $where = ''; // value. if (isset($clause['value'])) { if (in_array($compare, array('IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'))) { if (!is_array($value)) { $value = preg_split('/[,\\s]+/', $value); } } // tinyint if (!empty($column) && true === in_array($column, array('hide_sitewide', 'is_spam'))) { $sql_chunks['where'][] = $wpdb->prepare("{$alias}{$column} = %d", $value); } else { switch ($compare) { // IN uses different syntax case 'IN': case 'NOT IN': $in_sql = BP_Activity_Activity::get_in_operator_sql("{$alias}{$column}", $value); // 'NOT IN' operator is as easy as a string replace! if ('NOT IN' === $compare) { $in_sql = str_replace('IN', 'NOT IN', $in_sql); } $sql_chunks['where'][] = $in_sql; break; case 'BETWEEN': case 'NOT BETWEEN': $value = array_slice($value, 0, 2); $where = $wpdb->prepare('%s AND %s', $value); break; case 'LIKE': case 'NOT LIKE': $value = '%' . bp_esc_like($value) . '%'; $where = $wpdb->prepare('%s', $value); break; default: $where = $wpdb->prepare('%s', $value); break; } } if ($where) { $sql_chunks['where'][] = "{$alias}{$column} {$compare} {$where}"; } } /* * Multiple WHERE clauses should be joined in parentheses. */ if (1 < count($sql_chunks['where'])) { $sql_chunks['where'] = array('( ' . implode(' AND ', $sql_chunks['where']) . ' )'); } return $sql_chunks; }
/** * Create filter SQL clauses. * * @since 1.5.0 * * @param array $filter_array { * Fields and values to filter by. * * @type array|string|int $user_id User ID(s). * @type array|string $object Corresponds to the 'component' * column in the database. * @type array|string $action Corresponds to the 'type' column * in the database. * @type array|string|int $primary_id Corresponds to the 'item_id' * column in the database. * @type array|string|int $secondary_id Corresponds to the * 'secondary_item_id' column in the database. * @type int $offset Return only those items with an ID greater * than the offset value. * @type string $since Return only those items that have a * date_recorded value greater than a * given MySQL-formatted date. * } * @return string The filter clause, for use in a SQL query. */ public static function get_filter_sql($filter_array) { $filter_sql = array(); if (!empty($filter_array['user_id'])) { $user_sql = BP_Activity_Activity::get_in_operator_sql('a.user_id', $filter_array['user_id']); if (!empty($user_sql)) { $filter_sql[] = $user_sql; } } if (!empty($filter_array['object'])) { $object_sql = BP_Activity_Activity::get_in_operator_sql('a.component', $filter_array['object']); if (!empty($object_sql)) { $filter_sql[] = $object_sql; } } if (!empty($filter_array['action'])) { $action_sql = BP_Activity_Activity::get_in_operator_sql('a.type', $filter_array['action']); if (!empty($action_sql)) { $filter_sql[] = $action_sql; } } if (!empty($filter_array['primary_id'])) { $pid_sql = BP_Activity_Activity::get_in_operator_sql('a.item_id', $filter_array['primary_id']); if (!empty($pid_sql)) { $filter_sql[] = $pid_sql; } } if (!empty($filter_array['secondary_id'])) { $sid_sql = BP_Activity_Activity::get_in_operator_sql('a.secondary_item_id', $filter_array['secondary_id']); if (!empty($sid_sql)) { $filter_sql[] = $sid_sql; } } if (!empty($filter_array['offset'])) { $sid_sql = absint($filter_array['offset']); $filter_sql[] = "a.id >= {$sid_sql}"; } if (!empty($filter_array['since'])) { // Validate that this is a proper Y-m-d H:i:s date. // Trick: parse to UNIX date then translate back. $translated_date = date('Y-m-d H:i:s', strtotime($filter_array['since'])); if ($translated_date === $filter_array['since']) { $filter_sql[] = "a.date_recorded > '{$translated_date}'"; } } if (empty($filter_sql)) { return false; } return join(' AND ', $filter_sql); }
/** * Create filter SQL clauses. * * @since BuddyPress (1.5.0) * * @param array $filter_array { * Fields and values to filter by. * @type array|string|id $user_id User ID(s). * @type array|string $object Corresponds to the 'component' * column in the database. * @type array|string $action Corresponds to the 'type' column * in the database. * @type array|string|int $primary_id Corresponds to the 'item_id' * column in the database. * @type array|string|int $secondary_id Corresponds to the * 'secondary_item_id' column in the database. * @type int $offset Return only those items with an ID greater * than the offset value. * @type string $since Return only those items that have a * date_recorded value greater than a given MySQL-formatted * date. * } * @return string The filter clause, for use in a SQL query. */ public static function get_filter_sql($filter_array) { $filter_sql = array(); if (!empty($filter_array['user_id'])) { global $wpdb; $user_sql = BP_Activity_Activity::get_in_operator_sql('a.user_id', $filter_array['user_id']); // START Also include @Mentions in User Stream $search_terms = '@' . bp_core_get_username($filter_array['user_id']); $user_sql .= "OR ( a.content LIKE '%%" . $wpdb->esc_like($search_terms) . "%%' )"; // END Also include @Mentions in User Stream if (!empty($user_sql)) { $filter_sql[] = $user_sql; } } if (!empty($filter_array['object'])) { $object_sql = BP_Activity_Activity::get_in_operator_sql('a.component', $filter_array['object']); if (!empty($object_sql)) { $filter_sql[] = $object_sql; } } if (!empty($filter_array['action'])) { $action_sql = BP_Activity_Activity::get_in_operator_sql('a.type', $filter_array['action']); if (!empty($action_sql)) { $filter_sql[] = $action_sql; } } if (!empty($filter_array['primary_id'])) { $pid_sql = BP_Activity_Activity::get_in_operator_sql('a.item_id', $filter_array['primary_id']); if (!empty($pid_sql)) { $filter_sql[] = $pid_sql; } } if (!empty($filter_array['secondary_id'])) { $sid_sql = BP_Activity_Activity::get_in_operator_sql('a.secondary_item_id', $filter_array['secondary_id']); if (!empty($sid_sql)) { $filter_sql[] = $sid_sql; } } if (!empty($filter_array['offset'])) { $sid_sql = absint($filter_array['offset']); $filter_sql[] = "a.id >= {$sid_sql}"; } if (!empty($filter_array['since'])) { // Validate that this is a proper Y-m-d H:i:s date // Trick: parse to UNIX date then translate back $translated_date = date('Y-m-d H:i:s', strtotime($filter_array['since'])); if ($translated_date === $filter_array['since']) { $filter_sql[] = "a.date_recorded > '{$translated_date}'"; } } if (empty($filter_sql)) { return false; } return join(' AND ', $filter_sql); }