diff --git a/type-factory.php b/connection-type-factory.php similarity index 100% rename from type-factory.php rename to connection-type-factory.php diff --git a/type.php b/connection-type.php similarity index 100% rename from type.php rename to connection-type.php diff --git a/directed-type.php b/directed-connection-type.php similarity index 100% rename from directed-type.php rename to directed-connection-type.php diff --git a/indeterminate-type.php b/indeterminate-directed-connection-type.php similarity index 100% rename from indeterminate-type.php rename to indeterminate-directed-connection-type.php diff --git a/item-any.php b/item-any.php new file mode 100644 index 0000000..b77eda1 --- /dev/null +++ b/item-any.php @@ -0,0 +1,19 @@ +item->ID ) ) + return wp_get_attachment_image( $this->item->ID, 'thumbnail', false ); + + return get_the_title( $this->item ); + } +} + diff --git a/item-post.php b/item-post.php new file mode 100644 index 0000000..4e94fcc --- /dev/null +++ b/item-post.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/item-user.php b/item-user.php new file mode 100644 index 0000000..da5b54a --- /dev/null +++ b/item-user.php @@ -0,0 +1,41 @@ +item->display_name; + } + + function get_permalink() { + return get_author_posts_url( $this->item->ID ); + } + + function get_editlink() { + return get_edit_user_link( $this->item->ID ); + } +} + + +// WP < 3.5 +if ( !function_exists( 'get_edit_user_link' ) ) : +function get_edit_user_link( $user_id = null ) { + if ( ! $user_id ) + $user_id = get_current_user_id(); + + if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) ) + return ''; + + $user = new WP_User( $user_id ); + + if ( ! $user->exists() ) + return ''; + + if ( get_current_user_id() == $user->ID ) + $link = get_edit_profile_url( $user->ID ); + else + $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) ); + + return apply_filters( 'get_edit_user_link', $link, $user->ID ); +} +endif; + diff --git a/item.php b/item.php index 7bad18f..16d15fc 100644 --- a/item.php +++ b/item.php @@ -32,88 +32,3 @@ abstract function get_permalink(); abstract function get_title(); } - -class P2P_Item_Any extends P2P_Item { - - function __construct() {} - - function get_permalink() {} - - function get_title() {} - - function get_object() { - return 'any'; - } - - function get_id() { - return false; - } -} - - -class P2P_Item_Post 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 ); - } -} - - -class P2P_Item_Attachment extends P2P_Item_Post { - - function get_title() { - if( wp_attachment_is_image( $this->item->ID ) ) - return wp_get_attachment_image( $this->item->ID, 'thumbnail', false ); - - return get_the_title( $this->item ); - } -} - - -class P2P_Item_User extends P2P_Item { - - function get_title() { - return $this->item->display_name; - } - - function get_permalink() { - return get_author_posts_url( $this->item->ID ); - } - - function get_editlink() { - return get_edit_user_link( $this->item->ID ); - } -} - - -// WP < 3.5 -if ( !function_exists( 'get_edit_user_link' ) ) : -function get_edit_user_link( $user_id = null ) { - if ( ! $user_id ) - $user_id = get_current_user_id(); - - if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) ) - return ''; - - $user = new WP_User( $user_id ); - - if ( ! $user->exists() ) - return ''; - - if ( get_current_user_id() == $user->ID ) - $link = get_edit_profile_url( $user->ID ); - else - $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) ); - - return apply_filters( 'get_edit_user_link', $link, $user->ID ); -} -endif; - diff --git a/query-post.php b/query-post.php index dbf08bd..6502c41 100644 --- a/query-post.php +++ b/query-post.php @@ -1,6 +1,6 @@ '', + 'mode' => 'ul', + ), $attr ); + + return _p2p_get_list( array( + 'ctype' => $attr['type'], + 'method' => $method, + 'item' => $post, + 'mode' => $attr['mode'], + 'context' => 'shortcode' + ) ); + } +} + diff --git a/side-attachment.php b/side-attachment.php new file mode 100644 index 0000000..1b3bc23 --- /dev/null +++ b/side-attachment.php @@ -0,0 +1,23 @@ +query_vars = $query_vars; + + $this->query_vars['post_type'] = array( 'attachment' ); + } + + function can_create_item() { + return false; + } + + function get_base_qv( $q ) { + return array_merge( parent::get_base_qv( $q ), array( + 'post_status' => 'inherit' + ) ); + } +} + diff --git a/side-post.php b/side-post.php new file mode 100644 index 0000000..b2b9c09 --- /dev/null +++ b/side-post.php @@ -0,0 +1,138 @@ +query_vars = $query_vars; + } + + public function get_object_type() { + return 'post'; + } + + public function first_post_type() { + return $this->query_vars['post_type'][0]; + } + + private function get_ptype() { + return get_post_type_object( $this->first_post_type() ); + } + + function get_base_qv( $q ) { + if ( isset( $q['post_type'] ) && 'any' != $q['post_type'] ) { + $common = array_intersect( $this->query_vars['post_type'], (array) $q['post_type'] ); + + if ( !$common ) + unset( $q['post_type'] ); + } + + return array_merge( $this->query_vars, $q, array( + 'suppress_filters' => false, + 'ignore_sticky_posts' => true, + ) ); + } + + function get_desc() { + return implode( ', ', array_map( array( $this, 'post_type_label' ), $this->query_vars['post_type'] ) ); + } + + private function post_type_label( $post_type ) { + $cpt = get_post_type_object( $post_type ); + return $cpt ? $cpt->label : $post_type; + } + + function get_title() { + return $this->get_ptype()->labels->name; + } + + function get_labels() { + return $this->get_ptype()->labels; + } + + function can_edit_connections() { + return current_user_can( $this->get_ptype()->cap->edit_posts ); + } + + function can_create_item() { + if ( count( $this->query_vars['post_type'] ) > 1 ) + return false; + + if ( count( $this->query_vars ) > 1 ) + return false; + + return true; + } + + function translate_qv( $qv ) { + $map = array( + 'include' => 'post__in', + 'exclude' => 'post__not_in', + 'search' => 's', + 'page' => 'paged', + 'per_page' => 'posts_per_page' + ); + + foreach ( $map as $old => $new ) + if ( isset( $qv["p2p:$old"] ) ) + $qv[$new] = _p2p_pluck( $qv, "p2p:$old" ); + + return $qv; + } + + function do_query( $args ) { + return new WP_Query( $args ); + } + + function capture_query( $args ) { + $q = new WP_Query; + $q->_p2p_capture = true; + + $q->query( $args ); + + return $q->_p2p_sql; + } + + function get_list( $wp_query ) { + $list = new P2P_List( $wp_query->posts, $this->item_type ); + + $list->current_page = max( 1, $wp_query->get('paged') ); + $list->total_pages = $wp_query->max_num_pages; + + return $list; + } + + function is_indeterminate( $side ) { + $common = array_intersect( + $this->query_vars['post_type'], + $side->query_vars['post_type'] + ); + + return !empty( $common ); + } + + protected function recognize( $arg ) { + if ( is_object( $arg ) && !isset( $arg->post_type ) ) + return false; + + $post = get_post( $arg ); + + if ( !is_object( $post ) ) + return false; + + if ( !$this->recognize_post_type( $post->post_type ) ) + return false; + + return $post; + } + + public function recognize_post_type( $post_type ) { + if ( !post_type_exists( $post_type ) ) + return false; + + return in_array( $post_type, $this->query_vars['post_type'] ); + } +} + + diff --git a/side-user.php b/side-user.php new file mode 100644 index 0000000..6d69fcd --- /dev/null +++ b/side-user.php @@ -0,0 +1,122 @@ +query_vars = $query_vars; + } + + function get_object_type() { + return 'user'; + } + + function get_desc() { + return __( 'Users', P2P_TEXTDOMAIN ); + } + + function get_title() { + return $this->get_desc(); + } + + function get_labels() { + return (object) array( + 'singular_name' => __( 'User', P2P_TEXTDOMAIN ), + 'search_items' => __( 'Search Users', P2P_TEXTDOMAIN ), + 'not_found' => __( 'No users found.', P2P_TEXTDOMAIN ), + ); + } + + function can_edit_connections() { + return 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 WP_User_Query( $args ); + } + + function capture_query( $args ) { + $args['count_total'] = false; + + $uq = new WP_User_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( + 'blog_id' => $GLOBALS['blog_id'], + 'role' => '', + 'meta_key' => '', + 'meta_value' => '', + 'meta_compare' => '', + 'include' => array(), + 'exclude' => array(), + 'search' => '', + 'search_columns' => array(), + 'orderby' => 'login', + 'order' => 'ASC', + 'offset' => '', + 'number' => '', + 'count_total' => true, + 'fields' => 'all', + 'who' => '' + ) ); + + $uq->prepare_query(); + + return "SELECT $uq->query_fields $uq->query_from $uq->query_where $uq->query_orderby $uq->query_limit"; + } + + 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, 'WP_User' ) ) + return $arg; + + return get_user_by( 'id', $arg ); + } +} + diff --git a/side.php b/side.php index ef3430b..7804153 100644 --- a/side.php +++ b/side.php @@ -52,282 +52,3 @@ function item_recognize( $arg ) { abstract protected function recognize( $arg ); } - -class P2P_Side_Post extends P2P_Side { - - protected $item_type = 'P2P_Item_Post'; - - function __construct( $query_vars ) { - $this->query_vars = $query_vars; - } - - public function get_object_type() { - return 'post'; - } - - public function first_post_type() { - return $this->query_vars['post_type'][0]; - } - - private function get_ptype() { - return get_post_type_object( $this->first_post_type() ); - } - - function get_base_qv( $q ) { - if ( isset( $q['post_type'] ) && 'any' != $q['post_type'] ) { - $common = array_intersect( $this->query_vars['post_type'], (array) $q['post_type'] ); - - if ( !$common ) - unset( $q['post_type'] ); - } - - return array_merge( $this->query_vars, $q, array( - 'suppress_filters' => false, - 'ignore_sticky_posts' => true, - ) ); - } - - function get_desc() { - return implode( ', ', array_map( array( $this, 'post_type_label' ), $this->query_vars['post_type'] ) ); - } - - private function post_type_label( $post_type ) { - $cpt = get_post_type_object( $post_type ); - return $cpt ? $cpt->label : $post_type; - } - - function get_title() { - return $this->get_ptype()->labels->name; - } - - function get_labels() { - return $this->get_ptype()->labels; - } - - function can_edit_connections() { - return current_user_can( $this->get_ptype()->cap->edit_posts ); - } - - function can_create_item() { - if ( count( $this->query_vars['post_type'] ) > 1 ) - return false; - - if ( count( $this->query_vars ) > 1 ) - return false; - - return true; - } - - function translate_qv( $qv ) { - $map = array( - 'include' => 'post__in', - 'exclude' => 'post__not_in', - 'search' => 's', - 'page' => 'paged', - 'per_page' => 'posts_per_page' - ); - - foreach ( $map as $old => $new ) - if ( isset( $qv["p2p:$old"] ) ) - $qv[$new] = _p2p_pluck( $qv, "p2p:$old" ); - - return $qv; - } - - function do_query( $args ) { - return new WP_Query( $args ); - } - - function capture_query( $args ) { - $q = new WP_Query; - $q->_p2p_capture = true; - - $q->query( $args ); - - return $q->_p2p_sql; - } - - function get_list( $wp_query ) { - $list = new P2P_List( $wp_query->posts, $this->item_type ); - - $list->current_page = max( 1, $wp_query->get('paged') ); - $list->total_pages = $wp_query->max_num_pages; - - return $list; - } - - function is_indeterminate( $side ) { - $common = array_intersect( - $this->query_vars['post_type'], - $side->query_vars['post_type'] - ); - - return !empty( $common ); - } - - protected function recognize( $arg ) { - if ( is_object( $arg ) && !isset( $arg->post_type ) ) - return false; - - $post = get_post( $arg ); - - if ( !is_object( $post ) ) - return false; - - if ( !$this->recognize_post_type( $post->post_type ) ) - return false; - - return $post; - } - - public function recognize_post_type( $post_type ) { - if ( !post_type_exists( $post_type ) ) - return false; - - return in_array( $post_type, $this->query_vars['post_type'] ); - } -} - - -class P2P_Side_Attachment extends P2P_Side_Post { - - protected $item_type = 'P2P_Item_Attachment'; - - function __construct( $query_vars ) { - $this->query_vars = $query_vars; - - $this->query_vars['post_type'] = array( 'attachment' ); - } - - function can_create_item() { - return false; - } - - function get_base_qv( $q ) { - return array_merge( parent::get_base_qv( $q ), array( - 'post_status' => 'inherit' - ) ); - } -} - - -class P2P_Side_User extends P2P_Side { - - protected $item_type = 'P2P_Item_User'; - - function __construct( $query_vars ) { - $this->query_vars = $query_vars; - } - - function get_object_type() { - return 'user'; - } - - function get_desc() { - return __( 'Users', P2P_TEXTDOMAIN ); - } - - function get_title() { - return $this->get_desc(); - } - - function get_labels() { - return (object) array( - 'singular_name' => __( 'User', P2P_TEXTDOMAIN ), - 'search_items' => __( 'Search Users', P2P_TEXTDOMAIN ), - 'not_found' => __( 'No users found.', P2P_TEXTDOMAIN ), - ); - } - - function can_edit_connections() { - return 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 WP_User_Query( $args ); - } - - function capture_query( $args ) { - $args['count_total'] = false; - - $uq = new WP_User_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( - 'blog_id' => $GLOBALS['blog_id'], - 'role' => '', - 'meta_key' => '', - 'meta_value' => '', - 'meta_compare' => '', - 'include' => array(), - 'exclude' => array(), - 'search' => '', - 'search_columns' => array(), - 'orderby' => 'login', - 'order' => 'ASC', - 'offset' => '', - 'number' => '', - 'count_total' => true, - 'fields' => 'all', - 'who' => '' - ) ); - - $uq->prepare_query(); - - return "SELECT $uq->query_fields $uq->query_from $uq->query_where $uq->query_orderby $uq->query_limit"; - } - - 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, 'WP_User' ) ) - return $arg; - - return get_user_by( 'id', $arg ); - } -} - diff --git a/storage.php b/storage.php index 677c17d..cede3e8 100644 --- a/storage.php +++ b/storage.php @@ -60,5 +60,3 @@ static function deleted_object( $object_id ) { } } -P2P_Storage::init(); - diff --git a/util.php b/util.php index dadd4e0..7f0df91 100644 --- a/util.php +++ b/util.php @@ -133,3 +133,52 @@ function _p2p_get_other_id( $item ) { trigger_error( "Corrupted data for item $inner_item->ID", E_USER_WARNING ); } +/** @internal */ +function _p2p_get_list( $args ) { + extract( $args ); + + $ctype = p2p_type( $ctype ); + if ( !$ctype ) { + trigger_error( sprintf( "Unregistered connection type '%s'.", $ctype ), E_USER_WARNING ); + return ''; + } + + $directed = $ctype->find_direction( $item ); + if ( !$directed ) + return ''; + + $extra_qv = array( + 'p2p:per_page' => -1, + 'p2p:context' => $context + ); + + $connected = $directed->$method( $item, $extra_qv, 'abstract' ); + + switch ( $mode ) { + case 'inline': + $args = array( + 'separator' => ', ' + ); + break; + + case 'ol': + $args = array( + 'before_list' => '