-
Notifications
You must be signed in to change notification settings - Fork 24
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
clariner
wants to merge
7
commits into
scribu:master
Choose a base branch
from
clariner:buddypress-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0992e58
first draft of adding buddypress support.
clariner 694f398
add buddypress group status to query results so it can be show in dro…
clariner 7b65f85
update hooks in query-bpgroup.php
clariner f4607ba
correct buddypress group query file name and add bpgroup as a known t…
clariner 409d932
resolved issue with connections not being created.
clariner 490aa67
removed redundant file.
clariner 0450d59
resolved issue with malformed javascript and added support for filter…
clariner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 $this->item->name; | ||
| } | ||
|
|
||
| function get_permalink() { | ||
| return bp_get_group_permalink($this->item); | ||
| } | ||
|
|
||
| function get_editlink() { | ||
| return bp_get_group_permalink($this->item); | ||
| } | ||
| } | ||
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?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,$bp; | ||
|
|
||
| $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; | ||
|
|
||
| $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 ]; | ||
| } | ||
| } | ||
| ?> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,289 @@ | ||
| <?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_a( $arg, 'BP_Groups_Group' ) ) | ||
| return $arg; | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| class P2P_BP_Group_Query { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should move this class to a separate file (call it |
||
|
|
||
| /** | ||
| * 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; | ||
| } | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just leaving a note here that
pre_user_queryis not the right hook to use.P2P_Query_Useralready takes care of extracting P2P args from WP_User_Query objects.