From dd70ebcc80f5d1be49d85ce1ab557b1eb863d1b1 Mon Sep 17 00:00:00 2001 From: Oreolek Date: Sat, 1 Jun 2013 13:48:49 +0700 Subject: [PATCH 1/6] sql way --- core/connection-type.php | 233 ++++++++++++++++++++++----------------- 1 file changed, 131 insertions(+), 102 deletions(-) diff --git a/core/connection-type.php b/core/connection-type.php index 1deb846c..db440f66 100644 --- a/core/connection-type.php +++ b/core/connection-type.php @@ -318,7 +318,7 @@ public function get_adjacent( $from, $to, $which ) { } /** - * Get the previous, next and parent items, in an ordered connection type. + * Get the previous, next and parent items * * @param mixed The current item * @@ -337,106 +337,135 @@ public function get_adjacent_items( $item ) { 'next' => false, ); - $r = $this->direction_from_item( $item ); - if ( !$r ) - return false; - - list( $direction, $item ) = $r; - - $connected_series = $this->set_direction( $direction )->get_connected( $item, - array(), 'abstract' )->items; - - if ( empty( $connected_series ) ) - return $r; - - if ( count( $connected_series ) > 1 ) { - trigger_error( 'More than one connected parents found.', E_USER_WARNING ); - } - - $parent = $connected_series[0]; - - $result['parent'] = $parent->get_object(); - $result['previous'] = $this->get_previous( $item->ID, $parent->ID ); - $result['next'] = $this->get_next( $item, $parent ); - - return $result; - } - - /** - * Optimized inner query, after the outer query was executed. - * - * Populates each of the outer querie's $post objects with a 'connected' property, containing a list of connected posts - * - * @param object|array $items WP_Query instance or list of post objects - * @param string|array $extra_qv Additional query vars for the inner query. - * @param string $prop_name The name of the property used to store the list of connected items on each post object. - */ - public function each_connected( $items, $extra_qv = array(), $prop_name = 'connected' ) { - if ( is_a( $items, 'WP_Query' ) ) - $items =& $items->posts; - - if ( empty( $items ) || !is_object( $items[0] ) ) - return; - - $post_types = array_unique( wp_list_pluck( $items, 'post_type' ) ); - - if ( count( $post_types ) > 1 ) { - $extra_qv['post_type'] = 'any'; - } - - $possible_directions = array(); - - foreach ( array( 'from', 'to' ) as $direction ) { - $side = $this->side[ $direction ]; - - if ( 'post' == $side->get_object_type() ) { - foreach ( $post_types as $post_type ) { - if ( $side->recognize_post_type( $post_type ) ) { - $possible_directions[] = $direction; - } - } - } - } - - $direction = _p2p_compress_direction( $possible_directions ); - - if ( !$direction ) - return false; - - $directed = $this->set_direction( $direction ); - - // ignore pagination - foreach ( array( 'showposts', 'posts_per_page', 'posts_per_archive_page' ) as $disabled_qv ) { - if ( isset( $extra_qv[ $disabled_qv ] ) ) { - trigger_error( "Can't use '$disabled_qv' in an inner query", E_USER_WARNING ); - } - } - $extra_qv['nopaging'] = true; - - $q = $directed->get_connected( $items, $extra_qv, 'abstract' ); - - $raw_connected = array(); - foreach ( $q->items as $item ) - $raw_connected[] = $item->get_object(); - - p2p_distribute_connected( $items, $raw_connected, $prop_name ); - } - - public function get_desc() { - $desc = array(); - - foreach ( array( 'from', 'to' ) as $key ) { - $desc[ $key ] = $this->side[ $key ]->get_desc(); - } - - $label = sprintf( '%s %s %s', $desc['from'], $this->strategy->get_arrow(), $desc['to'] ); - - $title = $this->get_field( 'title', 'from' ); - - if ( $title ) - $label .= " ($title)"; - - return $label; - } + $r = $this->direction_from_item( $item ); + if (!$r ) + return; + + list( $direction, $item ) = $r; + + $connected_series = $this->set_direction( $direction )->get_connected( $item, + array(), 'abstract' )->items; + + if ( empty( $connected_series ) ) + return $r; + + if ( count( $connected_series ) > 1 ) { + trigger_error( 'More than one connected parents found.', E_USER_WARNING ); + } + + $parent = $connected_series[0]; + + $result['parent'] = $parent->get_object(); + + /* + if connection is ordered... + $result['previous'] = $this->get_previous( $item->ID, $parent->ID ); + $result['next'] = $this->get_next( $item, $parent ); + */ + + global $wpdb; + // select p2p_from from wp_p2p LEFT JOIN wp_posts ON wp_p2p.p2p_from = wp_posts.id WHERE p2p_type = 'posts_to_pages' AND p2p_to = '2' ORDER BY wp_posts.post_date DESC; + $relatives = $wpdb->get_results('SELECT p2p_from FROM '.$wpdb->p2p.' LEFT JOIN '.$wpdb->posts.' ON '.$wpdb->p2p.'.p2p_from = '.$wpdb->posts.'.id WHERE p2p_type = "' . $this->name . '" AND p2p_to = "' . $result['parent']->ID . '" ORDER BY '.$wpdb->posts.'.post_date DESC'); + + $last2 = FALSE; + $last = FALSE; + $current = FALSE; + $i = 0; + $length = count ($relatives); + while ($i < $length AND $last != $item->ID) + { + if ($last) $last2 = $last; + $last = $current; + $current = $relatives[$i]->p2p_from; + $i++; + } + if ($last == $item->ID) + { + if ($last2) $result['previous'] = get_post($last2); + $result['next'] = get_post($current); + } + if ($current == $item->ID) + { + $result['previous'] = get_post($last); + } + return $result; + } + + /** + * Optimized inner query, after the outer query was executed. + * + * Populates each of the outer querie's $post objects with a 'connected' property, containing a list of connected posts + * + * @param object|array $items WP_Query instance or list of post objects + * @param string|array $extra_qv Additional query vars for the inner query. + * @param string $prop_name The name of the property used to store the list of connected items on each post object. + */ + public function each_connected( $items, $extra_qv = array(), $prop_name = 'connected' ) { + if ( is_a( $items, 'WP_Query' ) ) + $items =& $items->posts; + + if ( empty( $items ) || !is_object( $items[0] ) ) + return; + + $post_types = array_unique( wp_list_pluck( $items, 'post_type' ) ); + + if ( count( $post_types ) > 1 ) { + $extra_qv['post_type'] = 'any'; + } + + $possible_directions = array(); + + foreach ( array( 'from', 'to' ) as $direction ) { + $side = $this->side[ $direction ]; + + if ( 'post' == $side->get_object_type() ) { + foreach ( $post_types as $post_type ) { + if ( $side->recognize_post_type( $post_type ) ) { + $possible_directions[] = $direction; + } + } + } + } + + $direction = _p2p_compress_direction( $possible_directions ); + + if ( !$direction ) + return false; + + $directed = $this->set_direction( $direction ); + + // ignore pagination + foreach ( array( 'showposts', 'posts_per_page', 'posts_per_archive_page' ) as $disabled_qv ) { + if ( isset( $extra_qv[ $disabled_qv ] ) ) { + trigger_error( "Can't use '$disabled_qv' in an inner query", E_USER_WARNING ); + } + } + $extra_qv['nopaging'] = true; + + $q = $directed->get_connected( $items, $extra_qv, 'abstract' ); + + $raw_connected = array(); + foreach ( $q->items as $item ) + $raw_connected[] = $item->get_object(); + + p2p_distribute_connected( $items, $raw_connected, $prop_name ); + } + + public function get_desc() { + $desc = array(); + + foreach ( array( 'from', 'to' ) as $key ) { + $desc[ $key ] = $this->side[ $key ]->get_desc(); + } + + $label = sprintf( '%s %s %s', $desc['from'], $this->strategy->get_arrow(), $desc['to'] ); + + $title = $this->get_field( 'title', 'from' ); + + if ( $title ) + $label .= " ($title)"; + + return $label; + } } From 44bb54bcf4fa7b1db5c1471da71d3146769117c8 Mon Sep 17 00:00:00 2001 From: Oreolek Date: Sat, 1 Jun 2013 14:45:22 +0700 Subject: [PATCH 2/6] slightly better version --- core/connection-type.php | 76 +++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/core/connection-type.php b/core/connection-type.php index db440f66..13377ac4 100644 --- a/core/connection-type.php +++ b/core/connection-type.php @@ -317,6 +317,16 @@ public function get_adjacent( $from, $to, $which ) { return $item->get_object(); } + /** + * Default WP sort - by date DESC + **/ + public static function default_sort($a, $b) + { + if ($a == $b) + return 0; + return (strtotime($a->post_date) > strtotime($b->post_date)) ? +1 : -1; + } + /** * Get the previous, next and parent items * @@ -356,39 +366,47 @@ public function get_adjacent_items( $item ) { $parent = $connected_series[0]; $result['parent'] = $parent->get_object(); - - /* - if connection is ordered... - $result['previous'] = $this->get_previous( $item->ID, $parent->ID ); - $result['next'] = $this->get_next( $item, $parent ); - */ - - global $wpdb; - // select p2p_from from wp_p2p LEFT JOIN wp_posts ON wp_p2p.p2p_from = wp_posts.id WHERE p2p_type = 'posts_to_pages' AND p2p_to = '2' ORDER BY wp_posts.post_date DESC; - $relatives = $wpdb->get_results('SELECT p2p_from FROM '.$wpdb->p2p.' LEFT JOIN '.$wpdb->posts.' ON '.$wpdb->p2p.'.p2p_from = '.$wpdb->posts.'.id WHERE p2p_type = "' . $this->name . '" AND p2p_to = "' . $result['parent']->ID . '" ORDER BY '.$wpdb->posts.'.post_date DESC'); - - $last2 = FALSE; - $last = FALSE; - $current = FALSE; - $i = 0; - $length = count ($relatives); - while ($i < $length AND $last != $item->ID) - { - if ($last) $last2 = $last; - $last = $current; - $current = $relatives[$i]->p2p_from; - $i++; - } - if ($last == $item->ID) + + $directed = $this->set_direction( $direction ); + + $key = $directed->get_orderby_key(); + if ( $key ) { - if ($last2) $result['previous'] = get_post($last2); - $result['next'] = get_post($current); + $result['previous'] = $this->get_previous( $item->ID, $parent->ID ); + $result['next'] = $this->get_next( $item, $parent ); } - if ($current == $item->ID) + else // connection is unsorted { - $result['previous'] = get_post($last); + $relatives = $this->get_connected( $result['parent'], + array(), 'abstract' )->items; + + // sort it by post_date DESC + + usort($relatives, array('P2P_Connection_Type', 'default_sort')); + + $last2 = FALSE; + $last = FALSE; + $current = FALSE; + $i = 0; + $length = count ($relatives); + while ($i < $length AND $last != $item->ID) + { + if ($last) $last2 = $last; + $last = $current; + $current = $relatives[$i]->ID; + $i++; + } + if ($last == $item->ID) + { + if ($last2) $result['previous'] = get_post($last2); + $result['next'] = get_post($current); + } + if ($current == $item->ID) + { + $result['previous'] = get_post($last); + } } - return $result; + return $result; } /** From b1cbe9244e1355322f87e4770b2d721fb6604c61 Mon Sep 17 00:00:00 2001 From: Oreolek Date: Sat, 1 Jun 2013 14:46:52 +0700 Subject: [PATCH 3/6] it's already sorted --- core/connection-type.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/core/connection-type.php b/core/connection-type.php index 13377ac4..c95b57aa 100644 --- a/core/connection-type.php +++ b/core/connection-type.php @@ -317,16 +317,6 @@ public function get_adjacent( $from, $to, $which ) { return $item->get_object(); } - /** - * Default WP sort - by date DESC - **/ - public static function default_sort($a, $b) - { - if ($a == $b) - return 0; - return (strtotime($a->post_date) > strtotime($b->post_date)) ? +1 : -1; - } - /** * Get the previous, next and parent items * @@ -382,8 +372,6 @@ public function get_adjacent_items( $item ) { // sort it by post_date DESC - usort($relatives, array('P2P_Connection_Type', 'default_sort')); - $last2 = FALSE; $last = FALSE; $current = FALSE; From fd9bf807847d9abbd0d2aeb7ccd3c32c6d3a481e Mon Sep 17 00:00:00 2001 From: Oreolek Date: Tue, 11 Jun 2013 09:17:03 +0700 Subject: [PATCH 4/6] Now get_adjacent_items() works on all posts --- core/connection-type.php | 684 +++++++++++++++++++-------------------- 1 file changed, 342 insertions(+), 342 deletions(-) diff --git a/core/connection-type.php b/core/connection-type.php index c95b57aa..a9a757bf 100644 --- a/core/connection-type.php +++ b/core/connection-type.php @@ -11,467 +11,467 @@ class P2P_Connection_Type { protected $title; public function __construct( $args, $sides ) { - $this->side = $sides; + $this->side = $sides; - $this->set_self_connections( $args ); + $this->set_self_connections( $args ); - $this->set_cardinality( _p2p_pluck( $args, 'cardinality' ) ); + $this->set_cardinality( _p2p_pluck( $args, 'cardinality' ) ); - $labels = array(); - foreach ( array( 'from', 'to' ) as $key ) { - $labels[ $key ] = (array) _p2p_pluck( $args, $key . '_labels' ); - } + $labels = array(); + foreach ( array( 'from', 'to' ) as $key ) { + $labels[ $key ] = (array) _p2p_pluck( $args, $key . '_labels' ); + } - $this->labels = $labels; + $this->labels = $labels; - $this->fields = $this->expand_fields( _p2p_pluck( $args, 'fields' ) ); + $this->fields = $this->expand_fields( _p2p_pluck( $args, 'fields' ) ); - foreach ( $args as $key => $value ) { - $this->$key = $value; - } + foreach ( $args as $key => $value ) { + $this->$key = $value; + } } public function get_field( $field, $direction ) { - $value = $this->$field; + $value = $this->$field; - if ( 'title' == $field ) - return $this->expand_title( $value, $direction ); + if ( 'title' == $field ) + return $this->expand_title( $value, $direction ); - if ( 'labels' == $field ) - return $this->expand_labels( $value, $direction ); + if ( 'labels' == $field ) + return $this->expand_labels( $value, $direction ); - if ( false === $direction ) - return $value; + if ( false === $direction ) + return $value; - return $value[ $direction ]; + return $value[ $direction ]; } private function set_self_connections( &$args ) { - $from_side = $this->side['from']; - $to_side = $this->side['to']; + $from_side = $this->side['from']; + $to_side = $this->side['to']; - if ( !$from_side->is_same_type( $to_side ) ) { - $args['self_connections'] = true; - } + if ( !$from_side->is_same_type( $to_side ) ) { + $args['self_connections'] = true; + } } private function expand_fields( $fields ) { - foreach ( $fields as &$field_args ) - { - if ( !is_array( $field_args ) ) - $field_args = array( 'title' => $field_args ); + foreach ( $fields as &$field_args ) + { + if ( !is_array( $field_args ) ) + $field_args = array( 'title' => $field_args ); - if ( !isset( $field_args['type'] ) ) - { - $field_args['type'] = isset( $field_args['values'] ) ? 'select' : 'text'; - } - elseif ( 'checkbox' == $field_args['type'] && !isset( $field_args['values'] ) ) - { - $field_args['values'] = array( true => ' ' ); - } + if ( !isset( $field_args['type'] ) ) + { + $field_args['type'] = isset( $field_args['values'] ) ? 'select' : 'text'; + } + elseif ( 'checkbox' == $field_args['type'] && !isset( $field_args['values'] ) ) + { + $field_args['values'] = array( true => ' ' ); } + } - return $fields; + return $fields; } private function set_cardinality( $cardinality ) { - $parts = explode( '-', $cardinality ); + $parts = explode( '-', $cardinality ); - $this->cardinality['from'] = $parts[0]; - $this->cardinality['to'] = $parts[2]; + $this->cardinality['from'] = $parts[0]; + $this->cardinality['to'] = $parts[2]; - foreach ( $this->cardinality as $key => &$value ) { - if ( 'one' != $value ) - $value = 'many'; - } + foreach ( $this->cardinality as $key => &$value ) { + if ( 'one' != $value ) + $value = 'many'; + } } private function expand_labels( $additional_labels, $key ) { - $labels = clone $this->side[ $key ]->get_labels(); - $labels->create = __( 'Create connections', P2P_TEXTDOMAIN ); + $labels = clone $this->side[ $key ]->get_labels(); + $labels->create = __( 'Create connections', P2P_TEXTDOMAIN ); - foreach ( $additional_labels[ $key ] as $key => $var ) - $labels->$key = $var; + foreach ( $additional_labels[ $key ] as $key => $var ) + $labels->$key = $var; - return $labels; + return $labels; } private function expand_title( $title, $key ) { - if ( $title && !is_array( $title ) ) - return $title; + if ( $title && !is_array( $title ) ) + return $title; - if ( isset( $title[$key] ) ) - return $title[$key]; + if ( isset( $title[$key] ) ) + return $title[$key]; - $other_key = ( 'from' == $key ) ? 'to' : 'from'; + $other_key = ( 'from' == $key ) ? 'to' : 'from'; - return sprintf( - __( 'Connected %s', P2P_TEXTDOMAIN ), - $this->side[ $other_key ]->get_title() - ); + return sprintf( + __( 'Connected %s', P2P_TEXTDOMAIN ), + $this->side[ $other_key ]->get_title() + ); } public function __call( $method, $args ) { - if ( ! method_exists( 'P2P_Directed_Connection_Type', $method ) ) { - trigger_error( "Method '$method' does not exist.", E_USER_ERROR ); - return; - } + if ( ! method_exists( 'P2P_Directed_Connection_Type', $method ) ) { + trigger_error( "Method '$method' does not exist.", E_USER_ERROR ); + return; + } - $r = $this->direction_from_item( $args[0] ); - if ( !$r ) { - trigger_error( sprintf( "Can't determine direction for '%s' type.", $this->name ), E_USER_WARNING ); - return false; - } + $r = $this->direction_from_item( $args[0] ); + if ( !$r ) { + trigger_error( sprintf( "Can't determine direction for '%s' type.", $this->name ), E_USER_WARNING ); + return false; + } - // replace the first argument with the normalized one, to avoid having to do it again - list( $direction, $args[0] ) = $r; + // replace the first argument with the normalized one, to avoid having to do it again + list( $direction, $args[0] ) = $r; - $directed = $this->set_direction( $direction ); + $directed = $this->set_direction( $direction ); - return call_user_func_array( array( $directed, $method ), $args ); + return call_user_func_array( array( $directed, $method ), $args ); } /** - * Set the direction. - * - * @param string $direction Can be 'from', 'to' or 'any'. - * - * @return object P2P_Directed_Connection_Type instance - */ + * Set the direction. + * + * @param string $direction Can be 'from', 'to' or 'any'. + * + * @return object P2P_Directed_Connection_Type instance + */ public function set_direction( $direction, $instantiate = true ) { - if ( !in_array( $direction, array( 'from', 'to', 'any' ) ) ) - return false; + if ( !in_array( $direction, array( 'from', 'to', 'any' ) ) ) + return false; - if ( $instantiate ) { - $class = $this->strategy->get_directed_class(); + if ( $instantiate ) { + $class = $this->strategy->get_directed_class(); - return new $class( $this, $direction ); - } + return new $class( $this, $direction ); + } - return $direction; + return $direction; } /** - * Attempt to guess direction based on a parameter. - * - * @param mixed A post type, object or object id. - * @param bool Whether to return an instance of P2P_Directed_Connection_Type or just the direction - * @param string An object type, such as 'post' or 'user' - * - * @return bool|object|string False on failure, P2P_Directed_Connection_Type instance or direction on success. - */ + * Attempt to guess direction based on a parameter. + * + * @param mixed A post type, object or object id. + * @param bool Whether to return an instance of P2P_Directed_Connection_Type or just the direction + * @param string An object type, such as 'post' or 'user' + * + * @return bool|object|string False on failure, P2P_Directed_Connection_Type instance or direction on success. + */ public function find_direction( $arg, $instantiate = true, $object_type = null ) { - if ( $object_type ) { - $direction = $this->direction_from_object_type( $object_type ); - if ( !$direction ) - return false; + if ( $object_type ) { + $direction = $this->direction_from_object_type( $object_type ); + if ( !$direction ) + return false; - if ( in_array( $direction, array( 'from', 'to' ) ) ) - return $this->set_direction( $direction, $instantiate ); - } + if ( in_array( $direction, array( 'from', 'to' ) ) ) + return $this->set_direction( $direction, $instantiate ); + } - $r = $this->direction_from_item( $arg ); - if ( !$r ) - return false; + $r = $this->direction_from_item( $arg ); + if ( !$r ) + return false; - list( $direction, $item ) = $r; + list( $direction, $item ) = $r; - return $this->set_direction( $direction, $instantiate ); + return $this->set_direction( $direction, $instantiate ); } protected function direction_from_item( $arg ) { - if ( is_array( $arg ) ) - $arg = reset( $arg ); + if ( is_array( $arg ) ) + $arg = reset( $arg ); - foreach ( array( 'from', 'to' ) as $direction ) { - $item = $this->side[ $direction ]->item_recognize( $arg ); + foreach ( array( 'from', 'to' ) as $direction ) { + $item = $this->side[ $direction ]->item_recognize( $arg ); - if ( !$item ) - continue; + if ( !$item ) + continue; - return array( $this->strategy->choose_direction( $direction ), $item ); - } + return array( $this->strategy->choose_direction( $direction ), $item ); + } - return false; + return false; } protected function direction_from_object_type( $current ) { - $from = $this->side['from']->get_object_type(); - $to = $this->side['to']->get_object_type(); + $from = $this->side['from']->get_object_type(); + $to = $this->side['to']->get_object_type(); - if ( $from == $to && $current == $from ) - return 'any'; + if ( $from == $to && $current == $from ) + return 'any'; - if ( $current == $from ) - return 'to'; + if ( $current == $from ) + return 'to'; - if ( $current == $to ) - return 'from'; + if ( $current == $to ) + return 'from'; - return false; + return false; } public function direction_from_types( $object_type, $post_types = null ) { - foreach ( array( 'from', 'to' ) as $direction ) { - if ( !$this->_type_check( $direction, $object_type, $post_types ) ) - continue; + foreach ( array( 'from', 'to' ) as $direction ) { + if ( !$this->_type_check( $direction, $object_type, $post_types ) ) + continue; - return $this->strategy->choose_direction( $direction ); - } + return $this->strategy->choose_direction( $direction ); + } - return false; + return false; } private function _type_check( $direction, $object_type, $post_types ) { - if ( $object_type != $this->side[ $direction ]->get_object_type() ) - return false; + if ( $object_type != $this->side[ $direction ]->get_object_type() ) + return false; - $side = $this->side[ $direction ]; + $side = $this->side[ $direction ]; - if ( !method_exists( $side, 'recognize_post_type' ) ) - return true; + if ( !method_exists( $side, 'recognize_post_type' ) ) + return true; - foreach ( (array) $post_types as $post_type ) { - if ( $side->recognize_post_type( $post_type ) ) { - return true; - } + foreach ( (array) $post_types as $post_type ) { + if ( $side->recognize_post_type( $post_type ) ) { + return true; } + } - return false; + return false; } /** Alias for get_prev() */ public function get_previous( $from, $to ) { - return $this->get_prev( $from, $to ); + return $this->get_prev( $from, $to ); } /** - * Get the previous post in an ordered connection. - * - * @param int The first end of the connection. - * @param int The second end of the connection. - * - * @return bool|object False on failure, post object on success - */ + * Get the previous post in an ordered connection. + * + * @param int The first end of the connection. + * @param int The second end of the connection. + * + * @return bool|object False on failure, post object on success + */ public function get_prev( $from, $to ) { - return $this->get_adjacent( $from, $to, -1 ); + return $this->get_adjacent( $from, $to, -1 ); } /** - * Get the next post in an ordered connection. - * - * @param int The first end of the connection. - * @param int The second end of the connection. - * - * @return bool|object False on failure, post object on success - */ + * Get the next post in an ordered connection. + * + * @param int The first end of the connection. + * @param int The second end of the connection. + * + * @return bool|object False on failure, post object on success + */ public function get_next( $from, $to ) { - return $this->get_adjacent( $from, $to, +1 ); + return $this->get_adjacent( $from, $to, +1 ); } /** - * Get another post in an ordered connection. - * - * @param int The first end of the connection. - * @param int The second end of the connection. - * @param int The position relative to the first parameter - * - * @return bool|object False on failure, post object on success - */ + * Get another post in an ordered connection. + * + * @param int The first end of the connection. + * @param int The second end of the connection. + * @param int The position relative to the first parameter + * + * @return bool|object False on failure, post object on success + */ public function get_adjacent( $from, $to, $which ) { - // The direction needs to be based on the second parameter, - // so that it's consistent with $this->connect( $from, $to ) etc. - $r = $this->direction_from_item( $to ); - if ( !$r ) - return false; + // The direction needs to be based on the second parameter, + // so that it's consistent with $this->connect( $from, $to ) etc. + $r = $this->direction_from_item( $to ); + if ( !$r ) + return false; - list( $direction, $to ) = $r; + list( $direction, $to ) = $r; - $directed = $this->set_direction( $direction ); + $directed = $this->set_direction( $direction ); - $key = $directed->get_orderby_key(); - if ( !$key ) - return false; + $key = $directed->get_orderby_key(); + if ( !$key ) + return false; - $p2p_id = $directed->get_p2p_id( $to, $from ); - if ( !$p2p_id ) - return false; + $p2p_id = $directed->get_p2p_id( $to, $from ); + if ( !$p2p_id ) + return false; - $order = (int) p2p_get_meta( $p2p_id, $key, true ); + $order = (int) p2p_get_meta( $p2p_id, $key, true ); - $adjacent = $directed->get_connected( $to, array( - 'connected_meta' => array( - array( - 'key' => $key, - 'value' => $order + $which - ) - ) - ), 'abstract' ); + $adjacent = $directed->get_connected( $to, array( + 'connected_meta' => array( + array( + 'key' => $key, + 'value' => $order + $which + ) + ) + ), 'abstract' ); - if ( empty( $adjacent->items ) ) - return false; + if ( empty( $adjacent->items ) ) + return false; - $item = reset( $adjacent->items ); + $item = reset( $adjacent->items ); - return $item->get_object(); + return $item->get_object(); } /** - * Get the previous, next and parent items + * Get the previous, next and parent items + * + * @param mixed The current item + * + * @return bool|array False if the connections aren't sortable, + * associative array otherwise: + * array( + * 'parent' => bool|object + * 'previous' => bool|object + * 'next' => bool|object + * ) + */ + public function get_adjacent_items( $item ) { + $result = array( + 'parent' => false, + 'previous' => false, + 'next' => false, + ); + + $r = $this->direction_from_item( $item ); + if (!$r ) + return; + + list( $direction, $item ) = $r; + + $connected_series = $this->set_direction( $direction )->get_connected( $item, + array(), 'abstract' )->items; + + if ( empty( $connected_series ) ) + return $r; + + if ( count( $connected_series ) > 1 ) { + trigger_error( 'More than one connected parents found.', E_USER_WARNING ); + } + + $parent = $connected_series[0]; + + $result['parent'] = $parent->get_object(); + + $directed = $this->set_direction( $direction ); + + $key = $directed->get_orderby_key(); + if ( $key ) + { + $result['previous'] = $this->get_previous( $item->ID, $parent->ID ); + $result['next'] = $this->get_next( $item, $parent ); + } + else // connection is unsorted + { + $relatives = $this->get_connected( $result['parent'], + array( 'p2p:per_page' => -1 ), 'abstract' )->items; + + // sort it by post_date DESC + + $last2 = FALSE; + $last = FALSE; + $current = FALSE; + $i = 0; + $length = count ($relatives); + while ($i < $length AND $last != $item->ID) + { + if ($last) $last2 = $last; + $last = $current; + $current = $relatives[$i]->ID; + $i++; + } + if ($last == $item->ID) + { + if ($last2) $result['previous'] = get_post($last2); + $result['next'] = get_post($current); + } + if ($current == $item->ID) + { + $result['previous'] = get_post($last); + } + } + return $result; + } + + /** + * Optimized inner query, after the outer query was executed. * - * @param mixed The current item + * Populates each of the outer querie's $post objects with a 'connected' property, containing a list of connected posts * - * @return bool|array False if the connections aren't sortable, - * associative array otherwise: - * array( - * 'parent' => bool|object - * 'previous' => bool|object - * 'next' => bool|object - * ) + * @param object|array $items WP_Query instance or list of post objects + * @param string|array $extra_qv Additional query vars for the inner query. + * @param string $prop_name The name of the property used to store the list of connected items on each post object. */ - public function get_adjacent_items( $item ) { - $result = array( - 'parent' => false, - 'previous' => false, - 'next' => false, - ); - - $r = $this->direction_from_item( $item ); - if (!$r ) - return; - - list( $direction, $item ) = $r; - - $connected_series = $this->set_direction( $direction )->get_connected( $item, - array(), 'abstract' )->items; - - if ( empty( $connected_series ) ) - return $r; - - if ( count( $connected_series ) > 1 ) { - trigger_error( 'More than one connected parents found.', E_USER_WARNING ); - } - - $parent = $connected_series[0]; - - $result['parent'] = $parent->get_object(); - - $directed = $this->set_direction( $direction ); - - $key = $directed->get_orderby_key(); - if ( $key ) - { - $result['previous'] = $this->get_previous( $item->ID, $parent->ID ); - $result['next'] = $this->get_next( $item, $parent ); - } - else // connection is unsorted - { - $relatives = $this->get_connected( $result['parent'], - array(), 'abstract' )->items; - - // sort it by post_date DESC - - $last2 = FALSE; - $last = FALSE; - $current = FALSE; - $i = 0; - $length = count ($relatives); - while ($i < $length AND $last != $item->ID) - { - if ($last) $last2 = $last; - $last = $current; - $current = $relatives[$i]->ID; - $i++; - } - if ($last == $item->ID) - { - if ($last2) $result['previous'] = get_post($last2); - $result['next'] = get_post($current); - } - if ($current == $item->ID) - { - $result['previous'] = get_post($last); - } - } - return $result; - } - - /** - * Optimized inner query, after the outer query was executed. - * - * Populates each of the outer querie's $post objects with a 'connected' property, containing a list of connected posts - * - * @param object|array $items WP_Query instance or list of post objects - * @param string|array $extra_qv Additional query vars for the inner query. - * @param string $prop_name The name of the property used to store the list of connected items on each post object. - */ - public function each_connected( $items, $extra_qv = array(), $prop_name = 'connected' ) { - if ( is_a( $items, 'WP_Query' ) ) - $items =& $items->posts; - - if ( empty( $items ) || !is_object( $items[0] ) ) - return; - - $post_types = array_unique( wp_list_pluck( $items, 'post_type' ) ); - - if ( count( $post_types ) > 1 ) { - $extra_qv['post_type'] = 'any'; - } - - $possible_directions = array(); - - foreach ( array( 'from', 'to' ) as $direction ) { - $side = $this->side[ $direction ]; - - if ( 'post' == $side->get_object_type() ) { - foreach ( $post_types as $post_type ) { - if ( $side->recognize_post_type( $post_type ) ) { - $possible_directions[] = $direction; - } - } - } - } - - $direction = _p2p_compress_direction( $possible_directions ); - - if ( !$direction ) - return false; - - $directed = $this->set_direction( $direction ); - - // ignore pagination - foreach ( array( 'showposts', 'posts_per_page', 'posts_per_archive_page' ) as $disabled_qv ) { - if ( isset( $extra_qv[ $disabled_qv ] ) ) { - trigger_error( "Can't use '$disabled_qv' in an inner query", E_USER_WARNING ); - } - } - $extra_qv['nopaging'] = true; - - $q = $directed->get_connected( $items, $extra_qv, 'abstract' ); - - $raw_connected = array(); - foreach ( $q->items as $item ) - $raw_connected[] = $item->get_object(); - - p2p_distribute_connected( $items, $raw_connected, $prop_name ); - } - - public function get_desc() { - $desc = array(); - - foreach ( array( 'from', 'to' ) as $key ) { - $desc[ $key ] = $this->side[ $key ]->get_desc(); - } - - $label = sprintf( '%s %s %s', $desc['from'], $this->strategy->get_arrow(), $desc['to'] ); - - $title = $this->get_field( 'title', 'from' ); - - if ( $title ) - $label .= " ($title)"; - - return $label; - } + public function each_connected( $items, $extra_qv = array(), $prop_name = 'connected' ) { + if ( is_a( $items, 'WP_Query' ) ) + $items =& $items->posts; + + if ( empty( $items ) || !is_object( $items[0] ) ) + return; + + $post_types = array_unique( wp_list_pluck( $items, 'post_type' ) ); + + if ( count( $post_types ) > 1 ) { + $extra_qv['post_type'] = 'any'; + } + + $possible_directions = array(); + + foreach ( array( 'from', 'to' ) as $direction ) { + $side = $this->side[ $direction ]; + + if ( 'post' == $side->get_object_type() ) { + foreach ( $post_types as $post_type ) { + if ( $side->recognize_post_type( $post_type ) ) { + $possible_directions[] = $direction; + } + } + } + } + + $direction = _p2p_compress_direction( $possible_directions ); + + if ( !$direction ) + return false; + + $directed = $this->set_direction( $direction ); + + // ignore pagination + foreach ( array( 'showposts', 'posts_per_page', 'posts_per_archive_page' ) as $disabled_qv ) { + if ( isset( $extra_qv[ $disabled_qv ] ) ) { + trigger_error( "Can't use '$disabled_qv' in an inner query", E_USER_WARNING ); + } + } + $extra_qv['nopaging'] = true; + + $q = $directed->get_connected( $items, $extra_qv, 'abstract' ); + + $raw_connected = array(); + foreach ( $q->items as $item ) + $raw_connected[] = $item->get_object(); + + p2p_distribute_connected( $items, $raw_connected, $prop_name ); + } + + public function get_desc() { + $desc = array(); + + foreach ( array( 'from', 'to' ) as $key ) { + $desc[ $key ] = $this->side[ $key ]->get_desc(); + } + + $label = sprintf( '%s %s %s', $desc['from'], $this->strategy->get_arrow(), $desc['to'] ); + + $title = $this->get_field( 'title', 'from' ); + + if ( $title ) + $label .= " ($title)"; + + return $label; + } } From c45ff3cd6ea731506759ae45abe628aa54e9c2c4 Mon Sep 17 00:00:00 2001 From: Oreolek Date: Tue, 11 Jun 2013 09:29:07 +0700 Subject: [PATCH 5/6] Indentation and spaces --- core/connection-type.php | 484 +++++++++++++++++++-------------------- 1 file changed, 242 insertions(+), 242 deletions(-) diff --git a/core/connection-type.php b/core/connection-type.php index a9a757bf..c304a135 100644 --- a/core/connection-type.php +++ b/core/connection-type.php @@ -11,155 +11,155 @@ class P2P_Connection_Type { protected $title; public function __construct( $args, $sides ) { - $this->side = $sides; + $this->side = $sides; - $this->set_self_connections( $args ); + $this->set_self_connections( $args ); - $this->set_cardinality( _p2p_pluck( $args, 'cardinality' ) ); + $this->set_cardinality( _p2p_pluck( $args, 'cardinality' ) ); - $labels = array(); - foreach ( array( 'from', 'to' ) as $key ) { - $labels[ $key ] = (array) _p2p_pluck( $args, $key . '_labels' ); - } + $labels = array(); + foreach ( array( 'from', 'to' ) as $key ) { + $labels[ $key ] = (array) _p2p_pluck( $args, $key . '_labels' ); + } - $this->labels = $labels; + $this->labels = $labels; - $this->fields = $this->expand_fields( _p2p_pluck( $args, 'fields' ) ); + $this->fields = $this->expand_fields( _p2p_pluck( $args, 'fields' ) ); - foreach ( $args as $key => $value ) { - $this->$key = $value; - } + foreach ( $args as $key => $value ) { + $this->$key = $value; + } } public function get_field( $field, $direction ) { - $value = $this->$field; + $value = $this->$field; - if ( 'title' == $field ) - return $this->expand_title( $value, $direction ); + if ( 'title' == $field ) + return $this->expand_title( $value, $direction ); - if ( 'labels' == $field ) - return $this->expand_labels( $value, $direction ); + if ( 'labels' == $field ) + return $this->expand_labels( $value, $direction ); - if ( false === $direction ) - return $value; + if ( false === $direction ) + return $value; - return $value[ $direction ]; + return $value[ $direction ]; } private function set_self_connections( &$args ) { - $from_side = $this->side['from']; - $to_side = $this->side['to']; + $from_side = $this->side['from']; + $to_side = $this->side['to']; - if ( !$from_side->is_same_type( $to_side ) ) { - $args['self_connections'] = true; - } + if ( !$from_side->is_same_type( $to_side ) ) { + $args['self_connections'] = true; + } } private function expand_fields( $fields ) { - foreach ( $fields as &$field_args ) - { - if ( !is_array( $field_args ) ) - $field_args = array( 'title' => $field_args ); - - if ( !isset( $field_args['type'] ) ) + foreach ( $fields as &$field_args ) { - $field_args['type'] = isset( $field_args['values'] ) ? 'select' : 'text'; - } - elseif ( 'checkbox' == $field_args['type'] && !isset( $field_args['values'] ) ) - { - $field_args['values'] = array( true => ' ' ); + if ( !is_array( $field_args ) ) + $field_args = array( 'title' => $field_args ); + + if ( !isset( $field_args['type'] ) ) + { + $field_args['type'] = isset( $field_args['values'] ) ? 'select' : 'text'; + } + elseif ( 'checkbox' == $field_args['type'] && !isset( $field_args['values'] ) ) + { + $field_args['values'] = array( true => ' ' ); + } } - } - return $fields; + return $fields; } private function set_cardinality( $cardinality ) { - $parts = explode( '-', $cardinality ); + $parts = explode( '-', $cardinality ); - $this->cardinality['from'] = $parts[0]; - $this->cardinality['to'] = $parts[2]; + $this->cardinality['from'] = $parts[0]; + $this->cardinality['to'] = $parts[2]; - foreach ( $this->cardinality as $key => &$value ) { - if ( 'one' != $value ) - $value = 'many'; - } + foreach ( $this->cardinality as $key => &$value ) { + if ( 'one' != $value ) + $value = 'many'; + } } private function expand_labels( $additional_labels, $key ) { - $labels = clone $this->side[ $key ]->get_labels(); - $labels->create = __( 'Create connections', P2P_TEXTDOMAIN ); + $labels = clone $this->side[ $key ]->get_labels(); + $labels->create = __( 'Create connections', P2P_TEXTDOMAIN ); - foreach ( $additional_labels[ $key ] as $key => $var ) - $labels->$key = $var; + foreach ( $additional_labels[ $key ] as $key => $var ) + $labels->$key = $var; - return $labels; + return $labels; } private function expand_title( $title, $key ) { - if ( $title && !is_array( $title ) ) - return $title; + if ( $title && !is_array( $title ) ) + return $title; - if ( isset( $title[$key] ) ) - return $title[$key]; + if ( isset( $title[$key] ) ) + return $title[$key]; - $other_key = ( 'from' == $key ) ? 'to' : 'from'; + $other_key = ( 'from' == $key ) ? 'to' : 'from'; - return sprintf( - __( 'Connected %s', P2P_TEXTDOMAIN ), - $this->side[ $other_key ]->get_title() - ); + return sprintf( + __( 'Connected %s', P2P_TEXTDOMAIN ), + $this->side[ $other_key ]->get_title() + ); } public function __call( $method, $args ) { - if ( ! method_exists( 'P2P_Directed_Connection_Type', $method ) ) { - trigger_error( "Method '$method' does not exist.", E_USER_ERROR ); - return; - } + if ( ! method_exists( 'P2P_Directed_Connection_Type', $method ) ) { + trigger_error( "Method '$method' does not exist.", E_USER_ERROR ); + return; + } - $r = $this->direction_from_item( $args[0] ); - if ( !$r ) { - trigger_error( sprintf( "Can't determine direction for '%s' type.", $this->name ), E_USER_WARNING ); - return false; - } + $r = $this->direction_from_item( $args[0] ); + if ( !$r ) { + trigger_error( sprintf( "Can't determine direction for '%s' type.", $this->name ), E_USER_WARNING ); + return false; + } - // replace the first argument with the normalized one, to avoid having to do it again - list( $direction, $args[0] ) = $r; + // replace the first argument with the normalized one, to avoid having to do it again + list( $direction, $args[0] ) = $r; - $directed = $this->set_direction( $direction ); + $directed = $this->set_direction( $direction ); - return call_user_func_array( array( $directed, $method ), $args ); + return call_user_func_array( array( $directed, $method ), $args ); } /** - * Set the direction. - * - * @param string $direction Can be 'from', 'to' or 'any'. - * - * @return object P2P_Directed_Connection_Type instance - */ + * Set the direction. + * + * @param string $direction Can be 'from', 'to' or 'any'. + * + * @return object P2P_Directed_Connection_Type instance + */ public function set_direction( $direction, $instantiate = true ) { - if ( !in_array( $direction, array( 'from', 'to', 'any' ) ) ) + if ( !in_array( $direction, array( 'from', 'to', 'any' ) ) ) return false; - if ( $instantiate ) { - $class = $this->strategy->get_directed_class(); + if ( $instantiate ) { + $class = $this->strategy->get_directed_class(); - return new $class( $this, $direction ); - } + return new $class( $this, $direction ); + } - return $direction; + return $direction; } /** - * Attempt to guess direction based on a parameter. - * - * @param mixed A post type, object or object id. - * @param bool Whether to return an instance of P2P_Directed_Connection_Type or just the direction - * @param string An object type, such as 'post' or 'user' - * - * @return bool|object|string False on failure, P2P_Directed_Connection_Type instance or direction on success. - */ + * Attempt to guess direction based on a parameter. + * + * @param mixed A post type, object or object id. + * @param bool Whether to return an instance of P2P_Directed_Connection_Type or just the direction + * @param string An object type, such as 'post' or 'user' + * + * @return bool|object|string False on failure, P2P_Directed_Connection_Type instance or direction on success. + */ public function find_direction( $arg, $instantiate = true, $object_type = null ) { if ( $object_type ) { $direction = $this->direction_from_object_type( $object_type ); @@ -168,233 +168,233 @@ public function find_direction( $arg, $instantiate = true, $object_type = null ) if ( in_array( $direction, array( 'from', 'to' ) ) ) return $this->set_direction( $direction, $instantiate ); - } + } - $r = $this->direction_from_item( $arg ); - if ( !$r ) + $r = $this->direction_from_item( $arg ); + if ( !$r ) return false; - list( $direction, $item ) = $r; + list( $direction, $item ) = $r; - return $this->set_direction( $direction, $instantiate ); + return $this->set_direction( $direction, $instantiate ); } protected function direction_from_item( $arg ) { - if ( is_array( $arg ) ) + if ( is_array( $arg ) ) $arg = reset( $arg ); - foreach ( array( 'from', 'to' ) as $direction ) { - $item = $this->side[ $direction ]->item_recognize( $arg ); + foreach ( array( 'from', 'to' ) as $direction ) { + $item = $this->side[ $direction ]->item_recognize( $arg ); - if ( !$item ) - continue; + if ( !$item ) + continue; - return array( $this->strategy->choose_direction( $direction ), $item ); - } + return array( $this->strategy->choose_direction( $direction ), $item ); + } - return false; + return false; } protected function direction_from_object_type( $current ) { - $from = $this->side['from']->get_object_type(); - $to = $this->side['to']->get_object_type(); + $from = $this->side['from']->get_object_type(); + $to = $this->side['to']->get_object_type(); - if ( $from == $to && $current == $from ) - return 'any'; + if ( $from == $to && $current == $from ) + return 'any'; - if ( $current == $from ) - return 'to'; + if ( $current == $from ) + return 'to'; - if ( $current == $to ) - return 'from'; + if ( $current == $to ) + return 'from'; - return false; + return false; } public function direction_from_types( $object_type, $post_types = null ) { - foreach ( array( 'from', 'to' ) as $direction ) { - if ( !$this->_type_check( $direction, $object_type, $post_types ) ) - continue; + foreach ( array( 'from', 'to' ) as $direction ) { + if ( !$this->_type_check( $direction, $object_type, $post_types ) ) + continue; - return $this->strategy->choose_direction( $direction ); - } + return $this->strategy->choose_direction( $direction ); + } - return false; + return false; } private function _type_check( $direction, $object_type, $post_types ) { - if ( $object_type != $this->side[ $direction ]->get_object_type() ) - return false; + if ( $object_type != $this->side[ $direction ]->get_object_type() ) + return false; - $side = $this->side[ $direction ]; + $side = $this->side[ $direction ]; - if ( !method_exists( $side, 'recognize_post_type' ) ) - return true; + if ( !method_exists( $side, 'recognize_post_type' ) ) + return true; - foreach ( (array) $post_types as $post_type ) { - if ( $side->recognize_post_type( $post_type ) ) { - return true; + foreach ( (array) $post_types as $post_type ) { + if ( $side->recognize_post_type( $post_type ) ) { + return true; + } } - } - return false; + return false; } /** Alias for get_prev() */ public function get_previous( $from, $to ) { - return $this->get_prev( $from, $to ); + return $this->get_prev( $from, $to ); } /** - * Get the previous post in an ordered connection. - * - * @param int The first end of the connection. - * @param int The second end of the connection. - * - * @return bool|object False on failure, post object on success - */ + * Get the previous post in an ordered connection. + * + * @param int The first end of the connection. + * @param int The second end of the connection. + * + * @return bool|object False on failure, post object on success + */ public function get_prev( $from, $to ) { - return $this->get_adjacent( $from, $to, -1 ); + return $this->get_adjacent( $from, $to, -1 ); } /** - * Get the next post in an ordered connection. - * - * @param int The first end of the connection. - * @param int The second end of the connection. - * - * @return bool|object False on failure, post object on success - */ + * Get the next post in an ordered connection. + * + * @param int The first end of the connection. + * @param int The second end of the connection. + * + * @return bool|object False on failure, post object on success + */ public function get_next( $from, $to ) { - return $this->get_adjacent( $from, $to, +1 ); + return $this->get_adjacent( $from, $to, +1 ); } /** - * Get another post in an ordered connection. - * - * @param int The first end of the connection. - * @param int The second end of the connection. - * @param int The position relative to the first parameter - * - * @return bool|object False on failure, post object on success - */ + * Get another post in an ordered connection. + * + * @param int The first end of the connection. + * @param int The second end of the connection. + * @param int The position relative to the first parameter + * + * @return bool|object False on failure, post object on success + */ public function get_adjacent( $from, $to, $which ) { - // The direction needs to be based on the second parameter, - // so that it's consistent with $this->connect( $from, $to ) etc. - $r = $this->direction_from_item( $to ); - if ( !$r ) - return false; + // The direction needs to be based on the second parameter, + // so that it's consistent with $this->connect( $from, $to ) etc. + $r = $this->direction_from_item( $to ); + if ( !$r ) + return false; - list( $direction, $to ) = $r; + list( $direction, $to ) = $r; - $directed = $this->set_direction( $direction ); + $directed = $this->set_direction( $direction ); - $key = $directed->get_orderby_key(); - if ( !$key ) - return false; + $key = $directed->get_orderby_key(); + if ( !$key ) + return false; - $p2p_id = $directed->get_p2p_id( $to, $from ); - if ( !$p2p_id ) - return false; + $p2p_id = $directed->get_p2p_id( $to, $from ); + if ( !$p2p_id ) + return false; - $order = (int) p2p_get_meta( $p2p_id, $key, true ); + $order = (int) p2p_get_meta( $p2p_id, $key, true ); - $adjacent = $directed->get_connected( $to, array( - 'connected_meta' => array( - array( - 'key' => $key, - 'value' => $order + $which - ) - ) - ), 'abstract' ); + $adjacent = $directed->get_connected( $to, array( + 'connected_meta' => array( + array( + 'key' => $key, + 'value' => $order + $which + ) + ) + ), 'abstract' ); - if ( empty( $adjacent->items ) ) - return false; + if ( empty( $adjacent->items ) ) + return false; - $item = reset( $adjacent->items ); + $item = reset( $adjacent->items ); - return $item->get_object(); + return $item->get_object(); } /** - * Get the previous, next and parent items - * - * @param mixed The current item - * - * @return bool|array False if the connections aren't sortable, - * associative array otherwise: - * array( - * 'parent' => bool|object - * 'previous' => bool|object - * 'next' => bool|object - * ) - */ + * Get the previous, next and parent items + * + * @param mixed The current item + * + * @return bool|array False if the connections aren't sortable, + * associative array otherwise: + * array( + * 'parent' => bool|object + * 'previous' => bool|object + * 'next' => bool|object + * ) + */ public function get_adjacent_items( $item ) { - $result = array( - 'parent' => false, - 'previous' => false, - 'next' => false, - ); + $result = array( + 'parent' => false, + 'previous' => false, + 'next' => false, + ); $r = $this->direction_from_item( $item ); if (!$r ) - return; + return false; - list( $direction, $item ) = $r; + list( $direction, $item ) = $r; - $connected_series = $this->set_direction( $direction )->get_connected( $item, - array(), 'abstract' )->items; + $connected_series = $this->set_direction( $direction )->get_connected( $item, + array(), 'abstract' )->items; - if ( empty( $connected_series ) ) - return $r; + if ( empty( $connected_series ) ) + return $r; - if ( count( $connected_series ) > 1 ) { - trigger_error( 'More than one connected parents found.', E_USER_WARNING ); - } + if ( count( $connected_series ) > 1 ) { + trigger_error( 'More than one connected parents found.', E_USER_WARNING ); + } + + $parent = $connected_series[0]; - $parent = $connected_series[0]; + $result['parent'] = $parent->get_object(); - $result['parent'] = $parent->get_object(); + $directed = $this->set_direction( $direction ); + + $key = $directed->get_orderby_key(); + if ( $key ) + { + $result['previous'] = $this->get_previous( $item->ID, $parent->ID ); + $result['next'] = $this->get_next( $item, $parent ); + } + else // connection is unsorted + { + $relatives = $this->get_connected( $result['parent'], + array( 'p2p:per_page' => -1 ), 'abstract' )->items; - $directed = $this->set_direction( $direction ); + // sort it by post_date DESC - $key = $directed->get_orderby_key(); - if ( $key ) + $last2 = FALSE; + $last = FALSE; + $current = FALSE; + $i = 0; + $length = count ($relatives); + while ($i < $length AND $last != $item->ID) { - $result['previous'] = $this->get_previous( $item->ID, $parent->ID ); - $result['next'] = $this->get_next( $item, $parent ); + if ($last) $last2 = $last; + $last = $current; + $current = $relatives[$i]->ID; + $i++; } - else // connection is unsorted + if ($last == $item->ID) { - $relatives = $this->get_connected( $result['parent'], - array( 'p2p:per_page' => -1 ), 'abstract' )->items; - - // sort it by post_date DESC - - $last2 = FALSE; - $last = FALSE; - $current = FALSE; - $i = 0; - $length = count ($relatives); - while ($i < $length AND $last != $item->ID) - { - if ($last) $last2 = $last; - $last = $current; - $current = $relatives[$i]->ID; - $i++; - } - if ($last == $item->ID) - { - if ($last2) $result['previous'] = get_post($last2); - $result['next'] = get_post($current); - } - if ($current == $item->ID) - { - $result['previous'] = get_post($last); - } + if ($last2) $result['previous'] = get_post($last2); + $result['next'] = get_post($current); } - return $result; + if ($current == $item->ID) + { + $result['previous'] = get_post($last); + } + } + return $result; } /** From cf5d6893a1649e16ecc75baa1f8d0b93e4b18f14 Mon Sep 17 00:00:00 2001 From: Oreolek Date: Tue, 11 Jun 2013 09:33:05 +0700 Subject: [PATCH 6/6] Indentation and spaces, part II --- core/connection-type.php | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/core/connection-type.php b/core/connection-type.php index c304a135..8b0df2d9 100644 --- a/core/connection-type.php +++ b/core/connection-type.php @@ -59,15 +59,15 @@ private function expand_fields( $fields ) { foreach ( $fields as &$field_args ) { if ( !is_array( $field_args ) ) - $field_args = array( 'title' => $field_args ); + $field_args = array( 'title' => $field_args ); if ( !isset( $field_args['type'] ) ) { - $field_args['type'] = isset( $field_args['values'] ) ? 'select' : 'text'; + $field_args['type'] = isset( $field_args['values'] ) ? 'select' : 'text'; } elseif ( 'checkbox' == $field_args['type'] && !isset( $field_args['values'] ) ) { - $field_args['values'] = array( true => ' ' ); + $field_args['values'] = array( true => ' ' ); } } @@ -82,7 +82,7 @@ private function set_cardinality( $cardinality ) { foreach ( $this->cardinality as $key => &$value ) { if ( 'one' != $value ) - $value = 'many'; + $value = 'many'; } } @@ -140,7 +140,7 @@ public function __call( $method, $args ) { */ public function set_direction( $direction, $instantiate = true ) { if ( !in_array( $direction, array( 'from', 'to', 'any' ) ) ) - return false; + return false; if ( $instantiate ) { $class = $this->strategy->get_directed_class(); @@ -161,18 +161,18 @@ public function set_direction( $direction, $instantiate = true ) { * @return bool|object|string False on failure, P2P_Directed_Connection_Type instance or direction on success. */ public function find_direction( $arg, $instantiate = true, $object_type = null ) { - if ( $object_type ) { - $direction = $this->direction_from_object_type( $object_type ); - if ( !$direction ) - return false; + if ( $object_type ) { + $direction = $this->direction_from_object_type( $object_type ); + if ( !$direction ) + return false; - if ( in_array( $direction, array( 'from', 'to' ) ) ) - return $this->set_direction( $direction, $instantiate ); + if ( in_array( $direction, array( 'from', 'to' ) ) ) + return $this->set_direction( $direction, $instantiate ); } $r = $this->direction_from_item( $arg ); if ( !$r ) - return false; + return false; list( $direction, $item ) = $r; @@ -181,7 +181,7 @@ public function find_direction( $arg, $instantiate = true, $object_type = null ) protected function direction_from_item( $arg ) { if ( is_array( $arg ) ) - $arg = reset( $arg ); + $arg = reset( $arg ); foreach ( array( 'from', 'to' ) as $direction ) { $item = $this->side[ $direction ]->item_recognize( $arg ); @@ -214,7 +214,7 @@ protected function direction_from_object_type( $current ) { public function direction_from_types( $object_type, $post_types = null ) { foreach ( array( 'from', 'to' ) as $direction ) { if ( !$this->_type_check( $direction, $object_type, $post_types ) ) - continue; + continue; return $this->strategy->choose_direction( $direction ); } @@ -302,10 +302,10 @@ public function get_adjacent( $from, $to, $which ) { $adjacent = $directed->get_connected( $to, array( 'connected_meta' => array( - array( - 'key' => $key, - 'value' => $order + $which - ) + array( + 'key' => $key, + 'value' => $order + $which + ) ) ), 'abstract' ); @@ -338,13 +338,13 @@ public function get_adjacent_items( $item ) { ); $r = $this->direction_from_item( $item ); - if (!$r ) + if ( !$r ) return false; list( $direction, $item ) = $r; $connected_series = $this->set_direction( $direction )->get_connected( $item, - array(), 'abstract' )->items; + array(), 'abstract' )->items; if ( empty( $connected_series ) ) return $r;