Skip to content

Buddypress support #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/
composer.lock
.project
195 changes: 195 additions & 0 deletions bp-group-query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

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 );
}

$query = "SELECT * FROM $wpdb->p2p WHERE p2p_type = '".$qv['connected_type']."'";
$connected = $wpdb->get_col($query, 2);

if ( empty($qv['search']) && $connected) {
$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'];
}

}

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


}
?>
2 changes: 1 addition & 1 deletion connection-type-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
21 changes: 21 additions & 0 deletions item-bpgroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

class P2P_Item_Bpgroup extends P2P_Item {

function get_id() {
return $this->item->id;
}

function get_title() {
return $this->item->name;
}

function get_permalink() {
return bp_get_group_permalink($this->item);
}

function get_editlink() {
return bp_get_group_permalink($this->item);
}
}
?>
116 changes: 116 additions & 0 deletions side-bpgroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

class P2P_Side_Bpgroup extends P2P_Side {

protected $item_type = 'P2P_Item_Bpgroup';

function __construct( $query_vars ) {
$this->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_object( $arg ))
return false;

$group = groups_get_group( array( 'group_id' => $arg) );

if ( !is_object( $group ) )
return false;

return $group;
}
}