From 0992e58b5d4835af54085f8a439d2bd026ac0a8e Mon Sep 17 00:00:00 2001 From: Ian Tasker Date: Wed, 10 Sep 2014 01:09:03 +0100 Subject: [PATCH 1/7] first draft of adding buddypress support. --- item-bpgroup.php | 17 +++ query-bpgroup.php | 48 ++++++++ side-bpgroup.php | 289 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 354 insertions(+) create mode 100644 item-bpgroup.php create mode 100644 query-bpgroup.php create mode 100644 side-bpgroup.php diff --git a/item-bpgroup.php b/item-bpgroup.php new file mode 100644 index 0000000..c8d1f20 --- /dev/null +++ b/item-bpgroup.php @@ -0,0 +1,17 @@ +item->name; + } + + function get_permalink() { + return bp_get_group_permalink($this->item); + } + + function get_editlink() { + return bp_get_group_permalink($this->item); + } +} +?> \ No newline at end of file diff --git a/query-bpgroup.php b/query-bpgroup.php new file mode 100644 index 0000000..db55b29 --- /dev/null +++ b/query-bpgroup.php @@ -0,0 +1,48 @@ +query_vars, 'bpgroup' ); + + if ( is_wp_error( $r ) ) { + $query->_p2p_error = $r; + + $query->query_where = " AND 1=0"; + return; + } + + if ( null === $r ) + return; + + list( $p2p_q, $query->query_vars ) = $r; + + $map = array( + 'fields' => 'query_fields', + 'join' => 'query_from', + 'where' => 'query_where', + 'orderby' => 'query_orderby', + ); + + $clauses = array(); + + foreach ( $map as $clause => $key ) + $clauses[$clause] = $query->$key; + + $table = $bp->groups->table_name; + $clauses = $p2p_q->alter_clauses( $clauses, "$table.id" ); + + if ( 0 !== strpos( $clauses['orderby'], 'ORDER BY ' ) ) + $clauses['orderby'] = 'ORDER BY ' . $clauses['orderby']; + + foreach ( $map as $clause => $key ) + $query->$key = $clauses[ $clause ]; + } +} +?> diff --git a/side-bpgroup.php b/side-bpgroup.php new file mode 100644 index 0000000..b04e553 --- /dev/null +++ b/side-bpgroup.php @@ -0,0 +1,289 @@ +query_vars = $query_vars; + } + + function get_object_type() { + return 'bpgroup'; + } + + function get_desc() { + return __( 'Buddypress Group', P2P_TEXTDOMAIN ); + } + + function get_title() { + return $this->get_desc(); + } + + function get_labels() { + return (object) array( + 'singular_name' => __( 'Buddypress Group', P2P_TEXTDOMAIN ), + 'search_items' => __( 'Search Buddypress Groups', P2P_TEXTDOMAIN ), + 'not_found' => __( 'No Buddypress Groups found.', P2P_TEXTDOMAIN ), + ); + } + + function can_edit_connections() { + return true; + } + + function can_create_item() { + return false; + } + + function translate_qv( $qv ) { + if ( isset( $qv['p2p:include'] ) ) + $qv['include'] = _p2p_pluck( $qv, 'p2p:include' ); + + if ( isset( $qv['p2p:exclude'] ) ) + $qv['exclude'] = _p2p_pluck( $qv, 'p2p:exclude' ); + + if ( isset( $qv['p2p:search'] ) && $qv['p2p:search'] ) + $qv['search'] = '*' . _p2p_pluck( $qv, 'p2p:search' ) . '*'; + + if ( isset( $qv['p2p:page'] ) && $qv['p2p:page'] > 0 ) { + if ( isset( $qv['p2p:per_page'] ) && $qv['p2p:per_page'] > 0 ) { + $qv['number'] = $qv['p2p:per_page']; + $qv['offset'] = $qv['p2p:per_page'] * ( $qv['p2p:page'] - 1 ); + } + } + + return $qv; + } + + function do_query( $args ) { + return new P2P_BP_Group_Query($args); + } + + function capture_query( $args ) { + + $args['count_total'] = false; + + $uq = new P2P_BP_Group_Query; + $uq->_p2p_capture = true; // needed by P2P_URL_Query + + // see http://core.trac.wordpress.org/ticket/21119 + $uq->query_vars = wp_parse_args( $args, array( + 'include' => array(), + 'exclude' => array(), + 'search' => '', + ) ); + + $uq->prepare_query(); + + return "SELECT $uq->query_fields $uq->query_from $uq->query_where $uq->query_orderby $uq->query_limit"; + //return 'SELECT * FROM '.$wpdb->prefix.'bp_groups bpg JOIN bp_groups_members bpgm ON bpg.id = bpgm.group_id WHERE bpgm.user_id IN ( '. $post->author .')'; + } + + function get_list( $query ) { + $list = new P2P_List( $query->get_results(), $this->item_type ); + + $qv = $query->query_vars; + + if ( isset( $qv['p2p:page'] ) ) { + $list->current_page = $qv['p2p:page']; + $list->total_pages = ceil( $query->get_total() / $qv['p2p:per_page'] ); + } + + return $list; +} + + function is_indeterminate( $side ) { + return true; + } + + function get_base_qv( $q ) { + return array_merge( $this->query_vars, $q ); + } + + protected function recognize( $arg ) { + if ( is_a( $arg, 'BP_Groups_Group' ) ) + return $arg; + + return false; + } +} + +class P2P_BP_Group_Query { + + /** + * Query vars, after parsing + * + * @since 3.5.0 + * @access public + * @var array + */ + var $query_vars = array(); + + /** + * List of found user ids + * + * @since 3.1.0 + * @access private + * @var array + */ + var $results; + + /** + * Total number of found users for the current query + * + * @since 3.1.0 + * @access private + * @var int + */ + var $total_bp_groups = 0; + + // SQL clauses + var $query_fields; + var $query_from; + var $query_where; + var $query_orderby; + var $query_limit; + + /** + * PHP5 constructor. + * + * @since 3.1.0 + * + * @param string|array $args Optional. The query variables. + * @return P2P_BP_Group_Query + */ + function __construct( $query = null ) { + if ( ! empty( $query ) ) { + $this->prepare_query( $query ); + $this->query(); + } + } + + /** + * Prepare the query variables. + * + * @since 3.1.0 + * + * @param string|array $args Optional. The query variables. + */ + function prepare_query( $query = array() ) { + global $wpdb,$bp; + + $table = $bp->groups->table_name; + if ( empty( $this->query_vars ) || ! empty( $query ) ) { + $this->query_limit = null; + $this->query_vars = wp_parse_args( $query, array( + 'include' => array(), + 'exclude' => array(), + 'search' => '', + ) ); + } + + $qv =& $this->query_vars; + + $this->query_fields = "$table.id, $table.name, $table.slug"; + $this->query_from = "FROM $table"; + $this->query_where = "WHERE 1=1"; + $this->query_orderby = "ORDER BY name ASC"; + + // limit + if ( isset( $qv['number'] ) && $qv['number'] ) { + if ( $qv['offset'] ) + $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']); + else + $this->query_limit = $wpdb->prepare("LIMIT %d", $qv['number']); + } + + $search = ''; + if ( isset( $qv['search'] ) ) + $search = trim( $qv['search'] ); + + if ( $search ) { + $leading_wild = ( ltrim($search, '*') != $search ); + $trailing_wild = ( rtrim($search, '*') != $search ); + if ( $leading_wild && $trailing_wild ) + $wild = 'both'; + elseif ( $leading_wild ) + $wild = 'leading'; + elseif ( $trailing_wild ) + $wild = 'trailing'; + else + $wild = false; + if ( $wild ) + $search = trim($search, '*'); + + $search_columns = array('name'); + + $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild ); + } + + if ( ! empty( $qv['include'] ) ) { + $ids = implode( ',', wp_parse_id_list( $qv['include'] ) ); + $this->query_where .= " AND $table.id IN ($ids)"; + } elseif ( ! empty( $qv['exclude'] ) ) { + $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); + $this->query_where .= " AND $table.id NOT IN ($ids)"; + } + } + + /** + * Execute the query, with the current variables. + * + * @since 3.1.0 + * + * @global wpdb $wpdb WordPress database object for queries. + */ + function query() { + global $wpdb; + + $qv =& $this->query_vars; + + $query = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; + + $this->results = $wpdb->get_results( $query ); + + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) + $this->total_bp_groups = $wpdb->get_var( 'SELECT FOUND_ROWS()' ); + + if ( !$this->results ) + return; + } + + function get( $query_var ) { + if ( isset( $this->query_vars[$query_var] ) ) + return $this->query_vars[$query_var]; + + return null; + } + + function set( $query_var, $value ) { + $this->query_vars[$query_var] = $value; + } + + function get_search_sql( $string, $cols, $wild = false ) { + $string = esc_sql( $string ); + + $searches = array(); + $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : ''; + $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : ''; + foreach ( $cols as $col ) { + if ( 'ID' == $col ) + $searches[] = "$col = '$string'"; + else + $searches[] = "$col LIKE '$leading_wild" . like_escape($string) . "$trailing_wild'"; + } + + return ' AND (' . implode(' OR ', $searches) . ')'; + } + + function get_results() { + return $this->results; + } + + function get_total() { + return $this->total_bp_groups; + } +} + + From 694f398e7f4502ee1e58af49b2d2490c045fa425 Mon Sep 17 00:00:00 2001 From: Ian Tasker Date: Wed, 10 Sep 2014 01:11:18 +0100 Subject: [PATCH 2/7] add buddypress group status to query results so it can be show in dropdown list like post status. --- side-bpgroup.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/side-bpgroup.php b/side-bpgroup.php index b04e553..51d0914 100644 --- a/side-bpgroup.php +++ b/side-bpgroup.php @@ -182,7 +182,7 @@ function prepare_query( $query = array() ) { $qv =& $this->query_vars; - $this->query_fields = "$table.id, $table.name, $table.slug"; + $this->query_fields = "$table.id, $table.name, $table.status, $table.slug"; $this->query_from = "FROM $table"; $this->query_where = "WHERE 1=1"; $this->query_orderby = "ORDER BY name ASC"; From 7b65f85e0fc944b34c9c6a104b88a7147ff02c20 Mon Sep 17 00:00:00 2001 From: Ian Tasker Date: Wed, 10 Sep 2014 08:17:33 +0100 Subject: [PATCH 3/7] update hooks in query-bpgroup.php moved P2P_BP_Group_Query to separate file bp-group-query.php fixed query object type check in side-bpgroup.php --- bg-group-query.php | 178 ++++++++++++++++++++++++++++++++++++++++++++ query-bpgroup.php | 7 +- side-bpgroup.php | 181 +-------------------------------------------- 3 files changed, 183 insertions(+), 183 deletions(-) create mode 100644 bg-group-query.php diff --git a/bg-group-query.php b/bg-group-query.php new file mode 100644 index 0000000..7838d3d --- /dev/null +++ b/bg-group-query.php @@ -0,0 +1,178 @@ +prepare_query( $query ); + $this->query(); + } + } + + /** + * Prepare the query variables. + * + * @since 3.1.0 + * + * @param string|array $args Optional. The query variables. + */ + function prepare_query( $query = array() ) { + global $wpdb,$bp; + + $table = $bp->groups->table_name; + if ( empty( $this->query_vars ) || ! empty( $query ) ) { + $this->query_limit = null; + $this->query_vars = wp_parse_args( $query, array( + 'include' => array(), + 'exclude' => array(), + 'search' => '', + ) ); + } + + $qv =& $this->query_vars; + + $this->query_fields = "$table.id, $table.name, $table.status, $table.slug"; + $this->query_from = "FROM $table"; + $this->query_where = "WHERE 1=1"; + $this->query_orderby = "ORDER BY name ASC"; + + // limit + if ( isset( $qv['number'] ) && $qv['number'] ) { + if ( $qv['offset'] ) + $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']); + else + $this->query_limit = $wpdb->prepare("LIMIT %d", $qv['number']); + } + + $search = ''; + if ( isset( $qv['search'] ) ) + $search = trim( $qv['search'] ); + + if ( $search ) { + $leading_wild = ( ltrim($search, '*') != $search ); + $trailing_wild = ( rtrim($search, '*') != $search ); + if ( $leading_wild && $trailing_wild ) + $wild = 'both'; + elseif ( $leading_wild ) + $wild = 'leading'; + elseif ( $trailing_wild ) + $wild = 'trailing'; + else + $wild = false; + if ( $wild ) + $search = trim($search, '*'); + + $search_columns = array('name'); + + $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild ); + } + + if ( ! empty( $qv['include'] ) ) { + $ids = implode( ',', wp_parse_id_list( $qv['include'] ) ); + $this->query_where .= " AND $table.id IN ($ids)"; + } elseif ( ! empty( $qv['exclude'] ) ) { + $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); + $this->query_where .= " AND $table.id NOT IN ($ids)"; + } + } + + /** + * Execute the query, with the current variables. + * + * @since 3.1.0 + * + * @global wpdb $wpdb WordPress database object for queries. + */ + function query() { + global $wpdb; + + $qv =& $this->query_vars; + + $query = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; + + $this->results = $wpdb->get_results( $query ); + + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) + $this->total_bp_groups = $wpdb->get_var( 'SELECT FOUND_ROWS()' ); + + if ( !$this->results ) + return; + } + + function get( $query_var ) { + if ( isset( $this->query_vars[$query_var] ) ) + return $this->query_vars[$query_var]; + + return null; + } + + function set( $query_var, $value ) { + $this->query_vars[$query_var] = $value; + } + + function get_search_sql( $string, $cols, $wild = false ) { + $string = esc_sql( $string ); + + $searches = array(); + $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : ''; + $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : ''; + foreach ( $cols as $col ) { + if ( 'ID' == $col ) + $searches[] = "$col = '$string'"; + else + $searches[] = "$col LIKE '$leading_wild" . like_escape($string) . "$trailing_wild'"; + } + + return ' AND (' . implode(' OR ', $searches) . ')'; + } + + function get_results() { + return $this->results; + } + + function get_total() { + return $this->total_bp_groups; + } +} diff --git a/query-bpgroup.php b/query-bpgroup.php index db55b29..89a0e43 100644 --- a/query-bpgroup.php +++ b/query-bpgroup.php @@ -3,10 +3,10 @@ class P2P_Query_Bpgroup { static function init() { - add_action( 'pre_user_query', array( __CLASS__, 'pre_user_query' ), 20 ); + add_action( 'pre_bpgroup_query', array( __CLASS__, 'pre_bpgroup_query' ), 20 ); } - static function pre_user_query( $query ) { + static function pre_bpgroup_query( $query ) { global $wpdb,$bp; $r = P2P_Query::create_from_qv( $query->query_vars, 'bpgroup' ); @@ -35,7 +35,7 @@ static function pre_user_query( $query ) { foreach ( $map as $clause => $key ) $clauses[$clause] = $query->$key; - $table = $bp->groups->table_name; + $table = $bp->groups->table_name; $clauses = $p2p_q->alter_clauses( $clauses, "$table.id" ); if ( 0 !== strpos( $clauses['orderby'], 'ORDER BY ' ) ) @@ -45,4 +45,5 @@ static function pre_user_query( $query ) { $query->$key = $clauses[ $clause ]; } } + ?> diff --git a/side-bpgroup.php b/side-bpgroup.php index 51d0914..b924a88 100644 --- a/side-bpgroup.php +++ b/side-bpgroup.php @@ -102,188 +102,9 @@ function get_base_qv( $q ) { } protected function recognize( $arg ) { - if ( is_a( $arg, 'BP_Groups_Group' ) ) + if ( is_a( $arg, 'P2P_BP_Group_Query' ) ) return $arg; return false; } } - -class P2P_BP_Group_Query { - - /** - * Query vars, after parsing - * - * @since 3.5.0 - * @access public - * @var array - */ - var $query_vars = array(); - - /** - * List of found user ids - * - * @since 3.1.0 - * @access private - * @var array - */ - var $results; - - /** - * Total number of found users for the current query - * - * @since 3.1.0 - * @access private - * @var int - */ - var $total_bp_groups = 0; - - // SQL clauses - var $query_fields; - var $query_from; - var $query_where; - var $query_orderby; - var $query_limit; - - /** - * PHP5 constructor. - * - * @since 3.1.0 - * - * @param string|array $args Optional. The query variables. - * @return P2P_BP_Group_Query - */ - function __construct( $query = null ) { - if ( ! empty( $query ) ) { - $this->prepare_query( $query ); - $this->query(); - } - } - - /** - * Prepare the query variables. - * - * @since 3.1.0 - * - * @param string|array $args Optional. The query variables. - */ - function prepare_query( $query = array() ) { - global $wpdb,$bp; - - $table = $bp->groups->table_name; - if ( empty( $this->query_vars ) || ! empty( $query ) ) { - $this->query_limit = null; - $this->query_vars = wp_parse_args( $query, array( - 'include' => array(), - 'exclude' => array(), - 'search' => '', - ) ); - } - - $qv =& $this->query_vars; - - $this->query_fields = "$table.id, $table.name, $table.status, $table.slug"; - $this->query_from = "FROM $table"; - $this->query_where = "WHERE 1=1"; - $this->query_orderby = "ORDER BY name ASC"; - - // limit - if ( isset( $qv['number'] ) && $qv['number'] ) { - if ( $qv['offset'] ) - $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']); - else - $this->query_limit = $wpdb->prepare("LIMIT %d", $qv['number']); - } - - $search = ''; - if ( isset( $qv['search'] ) ) - $search = trim( $qv['search'] ); - - if ( $search ) { - $leading_wild = ( ltrim($search, '*') != $search ); - $trailing_wild = ( rtrim($search, '*') != $search ); - if ( $leading_wild && $trailing_wild ) - $wild = 'both'; - elseif ( $leading_wild ) - $wild = 'leading'; - elseif ( $trailing_wild ) - $wild = 'trailing'; - else - $wild = false; - if ( $wild ) - $search = trim($search, '*'); - - $search_columns = array('name'); - - $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild ); - } - - if ( ! empty( $qv['include'] ) ) { - $ids = implode( ',', wp_parse_id_list( $qv['include'] ) ); - $this->query_where .= " AND $table.id IN ($ids)"; - } elseif ( ! empty( $qv['exclude'] ) ) { - $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); - $this->query_where .= " AND $table.id NOT IN ($ids)"; - } - } - - /** - * Execute the query, with the current variables. - * - * @since 3.1.0 - * - * @global wpdb $wpdb WordPress database object for queries. - */ - function query() { - global $wpdb; - - $qv =& $this->query_vars; - - $query = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; - - $this->results = $wpdb->get_results( $query ); - - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) - $this->total_bp_groups = $wpdb->get_var( 'SELECT FOUND_ROWS()' ); - - if ( !$this->results ) - return; - } - - function get( $query_var ) { - if ( isset( $this->query_vars[$query_var] ) ) - return $this->query_vars[$query_var]; - - return null; - } - - function set( $query_var, $value ) { - $this->query_vars[$query_var] = $value; - } - - function get_search_sql( $string, $cols, $wild = false ) { - $string = esc_sql( $string ); - - $searches = array(); - $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : ''; - $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : ''; - foreach ( $cols as $col ) { - if ( 'ID' == $col ) - $searches[] = "$col = '$string'"; - else - $searches[] = "$col LIKE '$leading_wild" . like_escape($string) . "$trailing_wild'"; - } - - return ' AND (' . implode(' OR ', $searches) . ')'; - } - - function get_results() { - return $this->results; - } - - function get_total() { - return $this->total_bp_groups; - } -} - - From f4607ba722f9cb8b92578ce27c21e5afe5a0830d Mon Sep 17 00:00:00 2001 From: Ian Tasker Date: Thu, 11 Sep 2014 23:51:41 +0100 Subject: [PATCH 4/7] correct buddypress group query file name and add bpgroup as a known type like user and attachment. --- bg-group-query.php => bp-group-query.php | 0 connection-type-factory.php | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename bg-group-query.php => bp-group-query.php (100%) diff --git a/bg-group-query.php b/bp-group-query.php similarity index 100% rename from bg-group-query.php rename to bp-group-query.php diff --git a/connection-type-factory.php b/connection-type-factory.php index bc9a784..51f066a 100644 --- a/connection-type-factory.php +++ b/connection-type-factory.php @@ -54,7 +54,7 @@ public static function register( $args ) { private static function create_side( &$args, $direction ) { $object = _p2p_pluck( $args, $direction ); - if ( in_array( $object, array( 'user', 'attachment' ) ) ) + if ( in_array( $object, array( 'user', 'attachment', 'bpgroup') ) ) $object_type = $object; else $object_type = 'post'; From 409d932c73491da6f41cabc59720b7f58bc828ed Mon Sep 17 00:00:00 2001 From: Ian Tasker Date: Tue, 16 Sep 2014 01:32:41 +0100 Subject: [PATCH 5/7] resolved issue with connections not being created. --- bp-group-query.php | 15 ++++++++++----- item-bpgroup.php | 4 ++++ query-bpgroup.php | 11 +++++++++++ side-bpgroup.php | 12 +++++++++--- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/bp-group-query.php b/bp-group-query.php index 7838d3d..4747f8a 100644 --- a/bp-group-query.php +++ b/bp-group-query.php @@ -62,6 +62,7 @@ function prepare_query( $query = array() ) { global $wpdb,$bp; $table = $bp->groups->table_name; + if ( empty( $this->query_vars ) || ! empty( $query ) ) { $this->query_limit = null; $this->query_vars = wp_parse_args( $query, array( @@ -89,7 +90,7 @@ function prepare_query( $query = array() ) { $search = ''; if ( isset( $qv['search'] ) ) $search = trim( $qv['search'] ); - + if ( $search ) { $leading_wild = ( ltrim($search, '*') != $search ); $trailing_wild = ( rtrim($search, '*') != $search ); @@ -109,13 +110,16 @@ function prepare_query( $query = array() ) { $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild ); } - if ( ! empty( $qv['include'] ) ) { + if( ! empty( $qv['include'] ) ) { $ids = implode( ',', wp_parse_id_list( $qv['include'] ) ); - $this->query_where .= " AND $table.id IN ($ids)"; + $this->query_where .= " AND $table.id IN ($ids)"; } elseif ( ! empty( $qv['exclude'] ) ) { $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); $this->query_where .= " AND $table.id NOT IN ($ids)"; + } elseif( empty( $qv['include']) && empty( $qv['exclude'] ) && empty($qv['search'] )) { + $this->query_where .= " AND 1=0 "; } + do_action_ref_array( 'pre_bpgroup_query', array( &$this ) ); } /** @@ -131,7 +135,7 @@ function query() { $qv =& $this->query_vars; $query = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; - + $this->results = $wpdb->get_results( $query ); if ( isset( $qv['count_total'] ) && $qv['count_total'] ) @@ -159,7 +163,7 @@ function get_search_sql( $string, $cols, $wild = false ) { $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : ''; $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : ''; foreach ( $cols as $col ) { - if ( 'ID' == $col ) + if ( 'id' == $col ) $searches[] = "$col = '$string'"; else $searches[] = "$col LIKE '$leading_wild" . like_escape($string) . "$trailing_wild'"; @@ -176,3 +180,4 @@ function get_total() { return $this->total_bp_groups; } } +?> \ No newline at end of file diff --git a/item-bpgroup.php b/item-bpgroup.php index c8d1f20..1f16055 100644 --- a/item-bpgroup.php +++ b/item-bpgroup.php @@ -2,6 +2,10 @@ class P2P_Item_Bpgroup extends P2P_Item { + function get_id() { + return $this->item->id; + } + function get_title() { return $this->item->name; } diff --git a/query-bpgroup.php b/query-bpgroup.php index 89a0e43..69c0eab 100644 --- a/query-bpgroup.php +++ b/query-bpgroup.php @@ -44,6 +44,17 @@ static function pre_bpgroup_query( $query ) { foreach ( $map as $clause => $key ) $query->$key = $clauses[ $clause ]; } + + /** + * Pre-populates the p2p meta cache to decrease the number of queries. + */ + static function cache_p2p_meta( $the_posts, $wp_query ) { + if ( isset( $wp_query->_p2p_query ) && !empty( $the_posts ) ) + update_meta_cache( 'p2p', wp_list_pluck( $the_posts, 'p2p_id' ) ); + + return $the_posts; + } + } ?> diff --git a/side-bpgroup.php b/side-bpgroup.php index b924a88..abcb793 100644 --- a/side-bpgroup.php +++ b/side-bpgroup.php @@ -102,9 +102,15 @@ function get_base_qv( $q ) { } protected function recognize( $arg ) { - if ( is_a( $arg, 'P2P_BP_Group_Query' ) ) - return $arg; + + if ( is_object( $arg )) + return false; - return false; + $group = groups_get_group( array( 'group_id' => $arg) ); + + if ( !is_object( $group ) ) + return false; + + return $group; } } From 490aa6785e67cc360dd95f4d2b20843de56302bb Mon Sep 17 00:00:00 2001 From: Ian Tasker Date: Sat, 20 Sep 2014 17:35:01 +0100 Subject: [PATCH 6/7] removed redundant file. updated query sql. --- .gitignore | 1 + bp-group-query.php | 20 +++++++++------- query-bpgroup.php | 60 ---------------------------------------------- 3 files changed, 13 insertions(+), 68 deletions(-) delete mode 100644 query-bpgroup.php diff --git a/.gitignore b/.gitignore index d1502b0..416985d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor/ composer.lock +.project diff --git a/bp-group-query.php b/bp-group-query.php index 4747f8a..0d227ca 100644 --- a/bp-group-query.php +++ b/bp-group-query.php @@ -109,17 +109,19 @@ function prepare_query( $query = array() ) { $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild ); } - - if( ! empty( $qv['include'] ) ) { - $ids = implode( ',', wp_parse_id_list( $qv['include'] ) ); - $this->query_where .= " AND $table.id IN ($ids)"; - } elseif ( ! empty( $qv['exclude'] ) ) { - $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); + + $query = "SELECT * FROM $wpdb->p2p WHERE p2p_type = '".$qv['connected_type']."'"; + $connected = $wpdb->get_col($query, 2); + + if ( empty($qv['search']) && $connected) { + $ids = implode( ',', $connected ); + $this->query_where .= " AND $table.id IN ($ids)"; + } elseif ($connected) { + $ids = implode( ',', $connected ); $this->query_where .= " AND $table.id NOT IN ($ids)"; - } elseif( empty( $qv['include']) && empty( $qv['exclude'] ) && empty($qv['search'] )) { + } else { $this->query_where .= " AND 1=0 "; } - do_action_ref_array( 'pre_bpgroup_query', array( &$this ) ); } /** @@ -179,5 +181,7 @@ function get_results() { function get_total() { return $this->total_bp_groups; } + + } ?> \ No newline at end of file diff --git a/query-bpgroup.php b/query-bpgroup.php deleted file mode 100644 index 69c0eab..0000000 --- a/query-bpgroup.php +++ /dev/null @@ -1,60 +0,0 @@ -query_vars, 'bpgroup' ); - - if ( is_wp_error( $r ) ) { - $query->_p2p_error = $r; - - $query->query_where = " AND 1=0"; - return; - } - - if ( null === $r ) - return; - - list( $p2p_q, $query->query_vars ) = $r; - - $map = array( - 'fields' => 'query_fields', - 'join' => 'query_from', - 'where' => 'query_where', - 'orderby' => 'query_orderby', - ); - - $clauses = array(); - - foreach ( $map as $clause => $key ) - $clauses[$clause] = $query->$key; - - $table = $bp->groups->table_name; - $clauses = $p2p_q->alter_clauses( $clauses, "$table.id" ); - - if ( 0 !== strpos( $clauses['orderby'], 'ORDER BY ' ) ) - $clauses['orderby'] = 'ORDER BY ' . $clauses['orderby']; - - foreach ( $map as $clause => $key ) - $query->$key = $clauses[ $clause ]; - } - - /** - * Pre-populates the p2p meta cache to decrease the number of queries. - */ - static function cache_p2p_meta( $the_posts, $wp_query ) { - if ( isset( $wp_query->_p2p_query ) && !empty( $the_posts ) ) - update_meta_cache( 'p2p', wp_list_pluck( $the_posts, 'p2p_id' ) ); - - return $the_posts; - } - -} - -?> From 0450d59d3147118b0c01e41809da935d65d49391 Mon Sep 17 00:00:00 2001 From: Ian Tasker Date: Tue, 30 Sep 2014 01:09:12 +0100 Subject: [PATCH 7/7] resolved issue with malformed javascript and added support for filtering by buddypress group hierarchy --- bp-group-query.php | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/bp-group-query.php b/bp-group-query.php index 0d227ca..142f550 100644 --- a/bp-group-query.php +++ b/bp-group-query.php @@ -75,7 +75,7 @@ function prepare_query( $query = array() ) { $qv =& $this->query_vars; $this->query_fields = "$table.id, $table.name, $table.status, $table.slug"; - $this->query_from = "FROM $table"; + $this->query_from = "FROM $table "; $this->query_where = "WHERE 1=1"; $this->query_orderby = "ORDER BY name ASC"; @@ -114,14 +114,22 @@ function prepare_query( $query = array() ) { $connected = $wpdb->get_col($query, 2); if ( empty($qv['search']) && $connected) { - $ids = implode( ',', $connected ); - $this->query_where .= " AND $table.id IN ($ids)"; - } elseif ($connected) { - $ids = implode( ',', $connected ); - $this->query_where .= " AND $table.id NOT IN ($ids)"; - } else { - $this->query_where .= " AND 1=0 "; - } + $this->query_fields .= ", $wpdb->p2p.*"; + $this->query_from .= " INNER JOIN $wpdb->p2p ON $table.id = $wpdb->p2p.p2p_to AND $wpdb->p2p.p2p_type = '".$qv['connected_type']."'"; + + $ids = implode( ',', $connected ); + $this->query_where .= " AND $table.id IN ($ids)"; + } elseif ($connected) { + $ids = implode( ',', $connected ); + $this->query_where .= " AND $table.id NOT IN ($ids)"; + } elseif ( empty($qv['search'])){ + $this->query_where .= " AND 1=0 "; + } + + if(!empty($qv['parent'])) { + $this->query_where .= " AND $table.parent_id = ".$qv['parent']; + } + } /**