From 4c5a801f73ee1089b2a80e7f35810cc2e05c23bf Mon Sep 17 00:00:00 2001 From: Ian Tasker Date: Sun, 7 Sep 2014 22:15:05 +0100 Subject: [PATCH 1/5] initial draft of adding buddypress support --- admin/column-bpgroup.php | 46 ++++++++++++ admin/dropdown-bpgroup.php | 37 ++++++++++ admin/field-title-bpgroup.php | 19 +++++ bin/install-wp-tests | 32 +++++++++ core/connection-type-factory.php | 2 +- core/init.php | 1 + core/item-bpgroup.php | 17 +++++ core/query-bpgroup.php | 47 ++++++++++++ core/side-bpgroup.php | 118 +++++++++++++++++++++++++++++++ 9 files changed, 318 insertions(+), 1 deletion(-) create mode 100644 admin/column-bpgroup.php create mode 100644 admin/dropdown-bpgroup.php create mode 100644 admin/field-title-bpgroup.php create mode 100644 bin/install-wp-tests create mode 100644 core/item-bpgroup.php create mode 100644 core/query-bpgroup.php create mode 100644 core/side-bpgroup.php diff --git a/admin/column-bpgroup.php b/admin/column-bpgroup.php new file mode 100644 index 00000000..e06e0bc3 --- /dev/null +++ b/admin/column-bpgroup.php @@ -0,0 +1,46 @@ +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 ); + } +} + diff --git a/admin/dropdown-bpgroup.php b/admin/dropdown-bpgroup.php new file mode 100644 index 00000000..3a3d01b2 --- /dev/null +++ b/admin/dropdown-bpgroup.php @@ -0,0 +1,37 @@ +_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 ) + ) ) + ); + } +} + diff --git a/admin/field-title-bpgroup.php b/admin/field-title-bpgroup.php new file mode 100644 index 00000000..474226dc --- /dev/null +++ b/admin/field-title-bpgroup.php @@ -0,0 +1,19 @@ + $item->get_permalink() + ); + + $group = $item->get_object(); + + if ( $group ) { + $data['status']['text'] = __(ucwords($group->status),'clariner'); + } + + return $data; + } +} + diff --git a/bin/install-wp-tests b/bin/install-wp-tests new file mode 100644 index 00000000..a93a900d --- /dev/null +++ b/bin/install-wp-tests @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +if [ $# -lt 3 ]; then + echo "usage: $0 [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" diff --git a/core/connection-type-factory.php b/core/connection-type-factory.php index bc9a7849..5f6f50e1 100644 --- a/core/connection-type-factory.php +++ b/core/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'; diff --git a/core/init.php b/core/init.php index 488ab749..c42c6e9d 100644 --- a/core/init.php +++ b/core/init.php @@ -10,6 +10,7 @@ P2P_Query_Post::init(); P2P_Query_User::init(); +P2P_Query_Bpgroup::init(); P2P_URL_Query::init(); diff --git a/core/item-bpgroup.php b/core/item-bpgroup.php new file mode 100644 index 00000000..dd00f9f2 --- /dev/null +++ b/core/item-bpgroup.php @@ -0,0 +1,17 @@ +item ); + } + + function get_permalink() { + return get_permalink( $this->item ); + } + + function get_editlink() { + return get_edit_post_link( $this->item ); + } +} + diff --git a/core/query-bpgroup.php b/core/query-bpgroup.php new file mode 100644 index 00000000..35861f39 --- /dev/null +++ b/core/query-bpgroup.php @@ -0,0 +1,47 @@ +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 ]; + } +} + diff --git a/core/side-bpgroup.php b/core/side-bpgroup.php new file mode 100644 index 00000000..e6d73ae9 --- /dev/null +++ b/core/side-bpgroup.php @@ -0,0 +1,118 @@ +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; + } +} + From 259bfcd78d433b7bbfa9e7f44afc654318fac05c Mon Sep 17 00:00:00 2001 From: Ian Tasker Date: Wed, 10 Sep 2014 08:06:29 +0100 Subject: [PATCH 2/5] removed redundant buddypress group files from /core and bin/install-wp-test --- bin/install-wp-tests | 32 ----------- core/item-bpgroup.php | 17 ------ core/query-bpgroup.php | 47 ---------------- core/side-bpgroup.php | 118 ----------------------------------------- 4 files changed, 214 deletions(-) delete mode 100644 bin/install-wp-tests delete mode 100644 core/item-bpgroup.php delete mode 100644 core/query-bpgroup.php delete mode 100644 core/side-bpgroup.php diff --git a/bin/install-wp-tests b/bin/install-wp-tests deleted file mode 100644 index a93a900d..00000000 --- a/bin/install-wp-tests +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash - -if [ $# -lt 3 ]; then - echo "usage: $0 [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" diff --git a/core/item-bpgroup.php b/core/item-bpgroup.php deleted file mode 100644 index dd00f9f2..00000000 --- a/core/item-bpgroup.php +++ /dev/null @@ -1,17 +0,0 @@ -item ); - } - - function get_permalink() { - return get_permalink( $this->item ); - } - - function get_editlink() { - return get_edit_post_link( $this->item ); - } -} - diff --git a/core/query-bpgroup.php b/core/query-bpgroup.php deleted file mode 100644 index 35861f39..00000000 --- a/core/query-bpgroup.php +++ /dev/null @@ -1,47 +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; - - $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 ]; - } -} - diff --git a/core/side-bpgroup.php b/core/side-bpgroup.php deleted file mode 100644 index e6d73ae9..00000000 --- a/core/side-bpgroup.php +++ /dev/null @@ -1,118 +0,0 @@ -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; - } -} - From 1e3d87ecc0a0dc4786a51a39dc107193cb498818 Mon Sep 17 00:00:00 2001 From: Ian Tasker Date: Sat, 20 Sep 2014 17:32:22 +0100 Subject: [PATCH 3/5] added bpgroup query init. removed redundant file. fixed action names. --- admin/column-bpgroup.php | 46 ----------------------------------- admin/dropdown-bpgroup.php | 5 ++-- admin/field-title-bpgroup.php | 2 +- posts-to-posts.php | 1 + 4 files changed, 5 insertions(+), 49 deletions(-) delete mode 100644 admin/column-bpgroup.php diff --git a/admin/column-bpgroup.php b/admin/column-bpgroup.php deleted file mode 100644 index e06e0bc3..00000000 --- a/admin/column-bpgroup.php +++ /dev/null @@ -1,46 +0,0 @@ -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 ); - } -} - diff --git a/admin/dropdown-bpgroup.php b/admin/dropdown-bpgroup.php index d37ad9d1..dac483f6 100644 --- a/admin/dropdown-bpgroup.php +++ b/admin/dropdown-bpgroup.php @@ -5,9 +5,9 @@ class P2P_Dropdown_Bpgroup extends P2P_Dropdown { function __construct( $directed, $title ) { parent::__construct( $directed, $title ); - add_action( 'request', array( __CLASS__, 'massage_query' ), 9 ); + add_action( 'pre_bpgroup_query', array( __CLASS__, 'massage_query' ), 9 ); - add_action( 'restrict_manage_bpgroups', array( $this, 'show_dropdown' ) ); + add_action( 'restrict_manage_bpgroup', array( $this, 'show_dropdown' ) ); } static function massage_query( $query ) { @@ -35,3 +35,4 @@ protected function render_dropdown() { } } + diff --git a/admin/field-title-bpgroup.php b/admin/field-title-bpgroup.php index 9c817a40..2438a005 100644 --- a/admin/field-title-bpgroup.php +++ b/admin/field-title-bpgroup.php @@ -7,7 +7,7 @@ function get_data( $item ) { 'title-attr' => $item->get_permalink() ); - $data['status']['text'] = __(ucwords($item->status),'clariner'); + $data['status']['text'] = __(ucwords($item->status),P2P_TEXTDOMAIN); return $data; } diff --git a/posts-to-posts.php b/posts-to-posts.php index 0f08e77d..5434829e 100644 --- a/posts-to-posts.php +++ b/posts-to-posts.php @@ -25,6 +25,7 @@ function _p2p_load() { P2P_Query_Post::init(); P2P_Query_User::init(); + P2P_Query_Bpgroup::init(); P2P_URL_Query::init(); From 5dca3a8432f8b21d4bcd446ee05c0a5cecdc22ff Mon Sep 17 00:00:00 2001 From: Ian Tasker Date: Sat, 20 Sep 2014 20:36:16 +0100 Subject: [PATCH 4/5] update urls for testing --- composer.json | 2 +- composer.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 5e865dcd..5d6ce646 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "repositories": [ { "type": "vcs", - "url": "https://github.com/scribu/wp-lib-posts-to-posts" + "url": "https://github.com/clariner/wp-lib-posts-to-posts" } ] } diff --git a/composer.lock b/composer.lock index 58e640d2..e78bf6f6 100644 --- a/composer.lock +++ b/composer.lock @@ -146,8 +146,8 @@ "version": "dev-master", "source": { "type": "git", - "url": "https://github.com/scribu/wp-lib-posts-to-posts.git", - "reference": "f6bafd5c4d93af0197f5690574cfe3c073b2f938" + "url": "https://github.com/clariner/wp-lib-posts-to-posts.git", + "reference": "490aa6785e67cc360dd95f4d2b20843de56302bb" }, "dist": { "type": "zip", From 58881ea3d550c1cbce0f578e76f60fc1d802f477 Mon Sep 17 00:00:00 2001 From: Ian Tasker Date: Sat, 20 Sep 2014 20:48:15 +0100 Subject: [PATCH 5/5] removed redundant function call --- posts-to-posts.php | 1 - 1 file changed, 1 deletion(-) diff --git a/posts-to-posts.php b/posts-to-posts.php index 5434829e..0f08e77d 100644 --- a/posts-to-posts.php +++ b/posts-to-posts.php @@ -25,7 +25,6 @@ function _p2p_load() { P2P_Query_Post::init(); P2P_Query_User::init(); - P2P_Query_Bpgroup::init(); P2P_URL_Query::init();