Skip to content

initial draft of adding buddypress support #454

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 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
46 changes: 46 additions & 0 deletions admin/column-bpgroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

class P2P_Column_Bpgroup extends P2P_Column {

function __construct( $directed ) {
parent::__construct( $directed );

add_action( 'pre_user_query', array( __CLASS__, 'user_query' ), 9 );

add_filter( 'manage_users_custom_column', array( $this, 'display_column' ), 10, 3 );
}

protected function get_items() {
global $wp_list_table;

return $wp_list_table->items;
}

// Add the query vars to the global user query (on the user admin screen)
static function user_query( $query ) {
if ( isset( $query->_p2p_capture ) )
return;

// Don't overwrite existing P2P query
if ( isset( $query->query_vars['connected_type'] ) )
return;

_p2p_append( $query->query_vars, wp_array_slice_assoc( $_GET,
P2P_URL_Query::get_custom_qv() ) );
}

function get_admin_link( $item ) {
$args = array(
'connected_type' => $this->ctype->name,
'connected_direction' => $this->ctype->flip_direction()->get_direction(),
'connected_items' => $item->get_id(),
);

return add_query_arg( $args, admin_url( 'users.php' ) );
}

function display_column( $content, $column, $item_id ) {
return $content . parent::render_column( $column, $item_id );
}
}

37 changes: 37 additions & 0 deletions admin/dropdown-bpgroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

class P2P_Dropdown_Bpgroup extends P2P_Dropdown_Post {

function __construct( $directed, $title ) {
parent::__construct( $directed, $title );

add_action( 'pre_user_query', array( __CLASS__, 'massage_query' ), 9 );

add_action( 'restrict_manage_users', array( $this, 'show_dropdown' ) );
}

static function massage_query( $query ) {
if ( isset( $query->_p2p_capture ) )
return;

// Don't overwrite existing P2P query
if ( isset( $query->query_vars['connected_type'] ) )
return;

_p2p_append( $query->query_vars, self::get_qv() );
}

protected function render_dropdown() {
return html( 'div', array(
'style' => 'float: right; margin-left: 16px'
),
parent::render_dropdown(),
html( 'input', array(
'type' => 'submit',
'class' => 'button',
'value' => __( 'Filter', P2P_TEXTDOMAIN )
) )
);
}
}

19 changes: 19 additions & 0 deletions admin/field-title-bpgroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class P2P_Field_Title_Bpgroup extends P2P_Field_Title {

function get_data( $item ) {
$data = array(
'title-attr' => $item->get_permalink()
);

$group = $item->get_object();

if ( $group ) {
$data['status']['text'] = __(ucwords($group->status),'clariner');
}

return $data;
}
}

32 changes: 32 additions & 0 deletions bin/install-wp-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

if [ $# -lt 3 ]; then
echo "usage: $0 <db-name> <db-user> <db-pass> [wp-version]"
exit 1
fi

DB_NAME=$1
DB_USER=$2
DB_PASS=$3
WP_VERSION=${4-master}

set -ex

# set up a WP install
WP_CORE_DIR=/tmp/wordpress/
mkdir -p $WP_CORE_DIR
wget -nv -O /tmp/wordpress.tar.gz https://github.com/WordPress/WordPress/tarball/$WP_VERSION
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR

# set up testing suite
svn co --ignore-externals --quiet http://unit-tests.svn.wordpress.org/trunk/ $WP_TESTS_DIR

cd $WP_TESTS_DIR
cp wp-tests-config-sample.php wp-tests-config.php
sed -i "s:dirname( __FILE__ ) . '/wordpress/':'$WP_CORE_DIR':" wp-tests-config.php
sed -i "s/yourdbnamehere/$DB_NAME/" wp-tests-config.php
sed -i "s/yourusernamehere/$DB_USER/" wp-tests-config.php
sed -i "s/yourpasswordhere/$DB_PASS/" wp-tests-config.php

# create database
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2P already has a bin/install-wp-tests.sh file, so this file is a duplicate and should be removed.

2 changes: 1 addition & 1 deletion core/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
1 change: 1 addition & 0 deletions core/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

P2P_Query_Post::init();
P2P_Query_User::init();
P2P_Query_Bpgroup::init();

P2P_URL_Query::init();

Expand Down
17 changes: 17 additions & 0 deletions core/item-bpgroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

class P2P_Item_Bpgroup extends P2P_Item {

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

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

function get_editlink() {
return get_edit_post_link( $this->item );
}
}

47 changes: 47 additions & 0 deletions core/query-bpgroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

class P2P_Query_Bpgroup {

static function init() {
add_action( 'pre_user_query', array( __CLASS__, 'pre_user_query' ), 20 );
}

static function pre_user_query( $query ) {
global $wpdb;

$r = P2P_Query::create_from_qv( $query->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;

$clauses = $p2p_q->alter_clauses( $clauses, "$wpdb->bp_groups.ID" );

if ( 0 !== strpos( $clauses['orderby'], 'ORDER BY ' ) )
$clauses['orderby'] = 'ORDER BY ' . $clauses['orderby'];

foreach ( $map as $clause => $key )
$query->$key = $clauses[ $clause ];
}
}

118 changes: 118 additions & 0 deletions core/side-bpgroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?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; //current_user_can( 'list_users' );
}

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 BP_Groups_Template($args);
}

function capture_query( $args ) {
$args['count_total'] = false;

$uq = new BP_Groups_Group;
$uq->_p2p_capture = true; // needed by P2P_URL_Query

$r = wp_parse_args( $args, $defaults );

$groups = BP_Groups_Group::get( array(
'type' => $r['type'],
'user_id' => $r['user_id'],
'include' => $r['include'],
'exclude' => $r['exclude'],
'search_terms' => $r['search_terms'],
'meta_query' => $r['meta_query'],
'show_hidden' => $r['show_hidden'],
'per_page' => $r['per_page'],
'page' => $r['page'],
'populate_extras' => $r['populate_extras'],
'update_meta_cache' => $r['update_meta_cache'],
'order' => $r['order'],
'orderby' => $r['orderby'],
) );

return $groups;
}

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