Posts 2 Posts - Version 0.6

Version Description

  • added p2p_each_connected()
  • fixed p2p_is_connected()
  • made p2p_get_connected() return p2p_ids even with `$direction
Download this release

Release Info

Developer scribu
Plugin Icon wp plugin Posts 2 Posts
Version 0.6
Comparing to
See all releases

Code changes from version 0.5.1 to 0.6

Files changed (5) hide show
  1. api.php +132 -49
  2. posts-to-posts.php +6 -6
  3. readme.txt +9 -2
  4. tests.php +216 -0
  5. ui/ui.js +15 -10
api.php CHANGED
@@ -62,21 +62,24 @@ function p2p_disconnect( $from, $to, $data = array() ) {
62
  * Get a list of connected posts
63
  *
64
  * @param int $post_id One end of the connection
65
- * @param string $direction The direction of the connection. Can be 'to', 'from' or 'both'
66
  * @param array $data additional data about the connection to filter against
67
  *
68
  * @return array( p2p_id => post_id )
69
  */
70
- function p2p_get_connected( $post_id, $direction = 'both', $data = array() ) {
71
- if ( in_array( $direction, array( 'to', 'from' ) ) ) {
72
- $ids = P2P_Connections::get( $post_id, $direction, $data );
73
- } else {
74
- $to = P2P_Connections::get( $post_id, 'to', $data );
75
  $from = P2P_Connections::get( $post_id, 'from', $data );
76
- $ids = array_merge( $to, array_diff( $from, $to ) );
 
 
 
 
 
 
77
  }
78
 
79
- return $ids;
80
  }
81
 
82
  /**
@@ -105,70 +108,121 @@ function p2p_delete_connection( $p2p_id ) {
105
  return P2P_Connections::delete( $p2p_id );
106
  }
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  // Allows you to write query_posts( array( 'connected' => 123 ) );
109
  class P2P_Query {
 
 
 
 
 
110
 
111
  function init() {
112
- new scbQueryManipulation( array( __CLASS__, 'query' ), false );
 
113
  }
114
 
115
  function query( $clauses, $wp_query ) {
116
  global $wpdb;
117
 
118
- $map = array(
119
- 'connected' => 'both',
120
- 'connected_to' => 'to',
121
- 'connected_from' => 'from',
122
- );
123
 
124
- foreach ( $map as $qv => $direction ) {
125
- $search = $wp_query->get( $qv );
126
- if ( !empty($search) )
127
- break;
128
- }
129
-
130
- if ( empty( $search ) )
131
  return $clauses;
132
 
133
- $clauses['fields'] .= ", $wpdb->p2p.p2p_id";
134
 
135
- $groupby = "{$wpdb->posts}.ID";
136
- if ( false === strpos( $clauses['groupby'], $groupby ) ) {
137
- if ( empty( $clauses['groupby'] ) )
138
- $clauses['groupby'] = $groupby;
139
- else
140
- $clauses['groupby'] .= ",$groupby";
 
 
141
  }
142
 
143
  switch ( $direction ) {
144
  case 'from':
145
- $clauses['join'] .= " INNER JOIN $wpdb->p2p ON ($wpdb->posts.ID = $wpdb->p2p.p2p_to)";
 
 
 
146
  break;
147
  case 'to':
148
- $clauses['join'] .= " INNER JOIN $wpdb->p2p ON ($wpdb->posts.ID = $wpdb->p2p.p2p_from)";
 
 
 
149
  break;
150
- case 'both':
151
- $clauses['join'] .= " INNER JOIN $wpdb->p2p ON ($wpdb->posts.ID = $wpdb->p2p.p2p_to OR $wpdb->posts.ID = $wpdb->p2p.p2p_from)";
 
 
 
 
 
 
 
152
  break;
153
  }
154
 
155
- if ( 'any' != $search ) {
156
- $search = absint( $search );
157
-
158
- switch ( $direction ) {
159
- case 'from':
160
- $clauses['where'] .= " AND $wpdb->p2p.p2p_from = $search";
161
- break;
162
- case 'to':
163
- $clauses['where'] .= " AND $wpdb->p2p.p2p_to = $search";
164
- break;
165
- case 'both':
166
- $clauses['where'] .= " AND ($wpdb->p2p.p2p_to = $search OR $wpdb->p2p.p2p_from = $search)";
167
- break;
168
- }
169
- }
170
-
171
- $connected_meta = $wp_query->get('connected_meta');
172
  if ( !empty( $connected_meta ) ) {
173
  $meta_clauses = _p2p_meta_sql_helper( $connected_meta );
174
  foreach ( $meta_clauses as $key => $value ) {
@@ -178,5 +232,34 @@ class P2P_Query {
178
 
179
  return $clauses;
180
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  }
182
 
62
  * Get a list of connected posts
63
  *
64
  * @param int $post_id One end of the connection
65
+ * @param string $direction The direction of the connection. Can be 'to', 'from' or 'any'
66
  * @param array $data additional data about the connection to filter against
67
  *
68
  * @return array( p2p_id => post_id )
69
  */
70
+ function p2p_get_connected( $post_id, $direction = 'any', $data = array() ) {
71
+ if ( 'any' == $direction ) {
 
 
 
72
  $from = P2P_Connections::get( $post_id, 'from', $data );
73
+ $to = P2P_Connections::get( $post_id, 'to', $data );
74
+
75
+ foreach ( $to as $p2p_id => $post_id ) {
76
+ $from[ $p2p_id ] = $post_id;
77
+ }
78
+
79
+ return $from;
80
  }
81
 
82
+ return P2P_Connections::get( $post_id, $direction, $data );
83
  }
84
 
85
  /**
108
  return P2P_Connections::delete( $p2p_id );
109
  }
110
 
111
+ /**
112
+ * Optimized inner query, after the outer query was executed.
113
+ *
114
+ * Populates each of the outer querie's $post objects with a property containing a list of connected posts
115
+ *
116
+ * @param string $direction The direction of the connection. Can be 'to', 'from' or 'any'
117
+ * @param string $prop_name The property name; will be prefixed with 'connected_'
118
+ * @param string|array $args The query vars for the inner query
119
+ * @param object $query (optional) The outer query. Defaults to the global $wp_query
120
+ */
121
+ function p2p_each_connected( $direction, $prop_name, $search, $query = null ) {
122
+ if ( is_null( $query ) )
123
+ $query = $GLOBALS['wp_query'];
124
+
125
+ if ( empty( $query->posts ) )
126
+ return;
127
+
128
+ if ( empty( $prop_name ) )
129
+ $prop_name = 'connected';
130
+ else
131
+ $prop_name = 'connected_' . $prop_name;
132
+
133
+ // re-index by ID
134
+ $posts = array();
135
+ foreach ( $query->posts as $post ) {
136
+ $post->$prop_name = array();
137
+ $posts[ $post->ID ] = $post;
138
+ }
139
+
140
+ // ignore other 'connected' query vars for the inner query
141
+ foreach ( array_keys( P2P_Query::$qv_map ) as $qv )
142
+ unset( $search[ $qv ] );
143
+
144
+ if ( 'any' == $direction )
145
+ $key = 'connected';
146
+ else
147
+ $key = 'connected_' . $direction;
148
+
149
+ $search[ $key ] = array_keys( $posts );
150
+ $search[ 'suppress_filters' ] = false;
151
+
152
+ foreach ( get_posts( $search ) as $inner_post ) {
153
+ if ( $inner_post->ID == $inner_post->p2p_from )
154
+ $outer_post_id = $inner_post->p2p_to;
155
+ elseif ( $inner_post->ID == $inner_post->p2p_to )
156
+ $outer_post_id = $inner_post->p2p_from;
157
+ else
158
+ throw new Exception( 'Corrupted data.' );
159
+
160
+ if ( $outer_post_id == $inner_post->ID )
161
+ throw new Exception( 'Post connected to itself.' );
162
+
163
+ array_push( $posts[ $outer_post_id ]->$prop_name, $inner_post );
164
+ }
165
+ }
166
+
167
  // Allows you to write query_posts( array( 'connected' => 123 ) );
168
  class P2P_Query {
169
+ static $qv_map = array(
170
+ 'connected' => 'any',
171
+ 'connected_to' => 'to',
172
+ 'connected_from' => 'from',
173
+ );
174
 
175
  function init() {
176
+ new scbQueryManipulation( array( __CLASS__, 'query' ), false ); // 'posts_clauses'
177
+ add_filter( 'the_posts', array( __CLASS__, 'the_posts' ), 11, 2 );
178
  }
179
 
180
  function query( $clauses, $wp_query ) {
181
  global $wpdb;
182
 
183
+ $found = self::find_qv( $wp_query );
 
 
 
 
184
 
185
+ if ( !$found )
 
 
 
 
 
 
186
  return $clauses;
187
 
188
+ list( $search, $key, $direction ) = $found;
189
 
190
+ $clauses['fields'] .= ", $wpdb->p2p.*";
191
+
192
+ $clauses['join'] .= " INNER JOIN $wpdb->p2p";
193
+
194
+ if ( 'any' == $search ) {
195
+ $search = false;
196
+ } else {
197
+ $search = implode( ',', array_map( 'absint', (array) $search ) );
198
  }
199
 
200
  switch ( $direction ) {
201
  case 'from':
202
+ $clauses['where'] .= " AND $wpdb->posts.ID = $wpdb->p2p.p2p_to";
203
+ if ( $search ) {
204
+ $clauses['where'] .= " AND $wpdb->p2p.p2p_from IN ($search)";
205
+ }
206
  break;
207
  case 'to':
208
+ $clauses['where'] .= " AND $wpdb->posts.ID = $wpdb->p2p.p2p_from";
209
+ if ( $search ) {
210
+ $clauses['where'] .= " AND $wpdb->p2p.p2p_to IN ($search)";
211
+ }
212
  break;
213
+ case 'any':
214
+ if ( $search ) {
215
+ $clauses['where'] .= " AND (
216
+ ($wpdb->posts.ID = $wpdb->p2p.p2p_to AND $wpdb->p2p.p2p_from IN ($search) ) OR
217
+ ($wpdb->posts.ID = $wpdb->p2p.p2p_from AND $wpdb->p2p.p2p_to IN ($search) )
218
+ )";
219
+ } else {
220
+ $clauses['where'] .= " AND ($wpdb->posts.ID = $wpdb->p2p.p2p_to OR $wpdb->posts.ID = $wpdb->p2p.p2p_from)";
221
+ }
222
  break;
223
  }
224
 
225
+ $connected_meta = $wp_query->get( 'connected_meta' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  if ( !empty( $connected_meta ) ) {
227
  $meta_clauses = _p2p_meta_sql_helper( $connected_meta );
228
  foreach ( $meta_clauses as $key => $value ) {
232
 
233
  return $clauses;
234
  }
235
+
236
+ function the_posts( $the_posts, $wp_query ) {
237
+ if ( empty( $the_posts ) )
238
+ return $the_posts;
239
+
240
+ $found = self::find_qv( $wp_query, 'each_' );
241
+
242
+ if ( !$found )
243
+ return $the_posts;
244
+
245
+ list( $search, $key, $direction ) = $found;
246
+
247
+ p2p_each_connected( $direction, $key, $search, $wp_query );
248
+
249
+ return $the_posts;
250
+ }
251
+
252
+ private function find_qv( $wp_query, $prefix = '' ) {
253
+ foreach ( self::$qv_map as $qv => $direction ) {
254
+ $search = $wp_query->get( $prefix . $qv );
255
+ if ( !empty( $search ) )
256
+ break;
257
+ }
258
+
259
+ if ( empty( $search ) )
260
+ return false;
261
+
262
+ return array( $search, $qv, $direction );
263
+ }
264
  }
265
 
posts-to-posts.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
  /*
3
  Plugin Name: Posts 2 Posts
4
- Version: 0.5.1
5
  Plugin Author: scribu
6
  Description: Create many-to-many relationships between all types of posts
7
  Author URI: http://scribu.net/
@@ -31,16 +31,16 @@ require dirname( __FILE__ ) . '/scb/load.php';
31
  function _p2p_init() {
32
  load_plugin_textdomain( 'posts-to-posts', '', dirname( plugin_basename( __FILE__ ) ) . '/lang' );
33
 
34
- require dirname( __FILE__ ) . '/storage.php';
35
- require dirname( __FILE__ ) . '/api.php';
36
- require dirname( __FILE__ ) . '/ui/ui.php';
37
- require dirname( __FILE__ ) . '/ui/boxes.php';
38
 
39
  P2P_Connections::init( __FILE__ );
40
  P2P_Query::init();
41
  P2P_Connection_Types::init();
42
  P2P_Box_Multiple::init();
43
-
44
  P2P_Migrate::init();
45
  }
46
  scb_init( '_p2p_init' );
1
  <?php
2
  /*
3
  Plugin Name: Posts 2 Posts
4
+ Version: 0.6
5
  Plugin Author: scribu
6
  Description: Create many-to-many relationships between all types of posts
7
  Author URI: http://scribu.net/
31
  function _p2p_init() {
32
  load_plugin_textdomain( 'posts-to-posts', '', dirname( plugin_basename( __FILE__ ) ) . '/lang' );
33
 
34
+ require_once dirname( __FILE__ ) . '/storage.php';
35
+ require_once dirname( __FILE__ ) . '/api.php';
36
+ require_once dirname( __FILE__ ) . '/ui/ui.php';
37
+ require_once dirname( __FILE__ ) . '/ui/boxes.php';
38
 
39
  P2P_Connections::init( __FILE__ );
40
  P2P_Query::init();
41
  P2P_Connection_Types::init();
42
  P2P_Box_Multiple::init();
43
+
44
  P2P_Migrate::init();
45
  }
46
  scb_init( '_p2p_init' );
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: scribu
3
  Donate link: http://scribu.net/paypal
4
  Tags: cms, custom post types, relationships, many-to-many
5
  Requires at least: 3.0
6
- Tested up to: 3.1-alpha
7
- Stable tag: 0.5.1
8
 
9
  Create connections between posts
10
 
@@ -49,6 +49,13 @@ Make sure your host is running PHP 5. The only foolproof way to do this is to ad
49
 
50
  == Changelog ==
51
 
 
 
 
 
 
 
 
52
  = 0.5.1 =
53
  * fixed fatal error on Menus screen
54
 
3
  Donate link: http://scribu.net/paypal
4
  Tags: cms, custom post types, relationships, many-to-many
5
  Requires at least: 3.0
6
+ Tested up to: 3.1
7
+ Stable tag: 0.6
8
 
9
  Create connections between posts
10
 
49
 
50
  == Changelog ==
51
 
52
+ = 0.6 =
53
+ * added p2p_each_connected()
54
+ * fixed p2p_is_connected()
55
+ * made p2p_get_connected() return p2p_ids even with `$direction = 'any'`
56
+ * made compatible with [Proper Network Activation](http://wordpress.org/extend/plugins/proper-network-activation)
57
+ * [more info](http://scribu.net/wordpress/posts-to-posts/version-0-6.html)
58
+
59
  = 0.5.1 =
60
  * fixed fatal error on Menus screen
61
 
tests.php ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // Automated testing suite for the Posts 2 Posts plugin
4
+
5
+ class P2P_Test {
6
+
7
+ function init() {
8
+ if ( !function_exists('p2p_register_connection_type') )
9
+ return;
10
+
11
+ add_action('init', array(__CLASS__, '_init'));
12
+ # add_action('admin_init', array(__CLASS__, 'setup'));
13
+ add_action('load-index.php', array(__CLASS__, 'test'));
14
+ # add_action('load-index.php', array(__CLASS__, 'debug'));
15
+ }
16
+
17
+ function _init() {
18
+ register_post_type('actor', array('label' => 'Actors', 'public' => true));
19
+ register_post_type('movie', array('label' => 'Movies', 'public' => true));
20
+
21
+ # p2p_register_connection_type('actor', 'actor', true);
22
+ p2p_register_connection_type('actor', 'movie', true);
23
+ p2p_register_connection_type('actor', 'actor', true);
24
+ }
25
+
26
+ function setup() {
27
+ global $wpdb;
28
+
29
+ $wpdb->query("DELETE FROM $wpdb->posts WHERE post_type IN ('actor', 'movie')");
30
+
31
+ $movie_ids = $actor_ids = array();
32
+
33
+ for ( $i=0; $i<20; $i++ ) {
34
+ $actor_ids[] = wp_insert_post(array(
35
+ 'post_type' => 'actor',
36
+ 'post_title' => "Actor $i",
37
+ 'post_status' => 'publish'
38
+ ));
39
+
40
+ $movie_ids[] = wp_insert_post(array(
41
+ 'post_type' => 'movie',
42
+ 'post_title' => "Movie $i",
43
+ 'post_status' => 'publish'
44
+ ));
45
+ }
46
+ }
47
+
48
+ function test() {
49
+ global $wpdb;
50
+
51
+ $wpdb->query("TRUNCATE $wpdb->p2p");
52
+ $wpdb->query("TRUNCATE $wpdb->p2pmeta");
53
+
54
+ assert_options(ASSERT_ACTIVE, 1);
55
+ assert_options(ASSERT_WARNING, 0);
56
+ assert_options(ASSERT_QUIET_EVAL, 1);
57
+
58
+ $failed = false;
59
+
60
+ assert_options(ASSERT_CALLBACK, function ($file, $line, $code) use ( &$failed ) {
61
+ $failed = true;
62
+
63
+ echo "<hr>Assertion Failed (line $line):<br />
64
+ <code>$code</code><br /><hr />";
65
+
66
+ add_action('admin_notices', array(__CLASS__, 'debug'));
67
+ });
68
+
69
+ $actor_ids = get_posts( array(
70
+ 'fields' => 'ids',
71
+ 'post_type' => 'actor',
72
+ 'post_status' => 'any',
73
+ 'orderby' => 'post_title',
74
+ 'order' => 'asc',
75
+ 'nopaging' => true
76
+ ) );
77
+
78
+ $movie_ids = get_posts( array(
79
+ 'fields' => 'ids',
80
+ 'post_type' => 'movie',
81
+ 'post_status' => 'any',
82
+ 'orderby' => 'post_title',
83
+ 'order' => 'asc',
84
+ 'nopaging' => true
85
+ ) );
86
+
87
+ // basic API correctness
88
+ p2p_connect( array_slice( $actor_ids, 0, 5 ), array_slice( $movie_ids, 0, 3 ) );
89
+ p2p_connect( $movie_ids[0], $actor_ids[10] );
90
+
91
+ $result = array_values( p2p_get_connected( $actor_ids[0], 'from' ) );
92
+ sort( $result );
93
+ $expected = array_slice( $movie_ids, 0, 3 );
94
+ sort($expected);
95
+ assert( '$expected == $result' );
96
+
97
+ $result = array_values( p2p_get_connected( $movie_ids[0], 'any' ) );
98
+ sort( $result );
99
+ $expected = array( $actor_ids[0], $actor_ids[10] );
100
+ sort( $expected );
101
+ assert( '$expected == $result' );
102
+
103
+ assert( 'true == p2p_is_connected( $actor_ids[0], $movie_ids[0] )' );
104
+ assert( 'false == p2p_is_connected( $actor_ids[0], $movie_ids[10] )' );
105
+
106
+ // 'actor' => 'actor'
107
+ $posts = get_posts( array(
108
+ 'connected' => $actor_ids[0],
109
+ 'post_type' => 'actor',
110
+ 'post_status' => 'any',
111
+ 'suppress_filters' => false,
112
+ 'fields' => 'ids',
113
+ ) );
114
+ assert( 'array() == $posts' );
115
+
116
+ // compare p2p_get_connected() to 'connected' => 123
117
+ p2p_connect( $actor_ids[2], $actor_ids[10] );
118
+ p2p_connect( $actor_ids[10], $actor_ids[2] );
119
+
120
+ $raw = p2p_get_connected($actor_ids[2], 'any');
121
+
122
+ $posts = get_posts( array(
123
+ 'connected' => $actor_ids[2],
124
+ 'post_type' => 'actor',
125
+ 'post_status' => 'any',
126
+ 'suppress_filters' => false,
127
+ 'cache_results' => false,
128
+ ) );
129
+
130
+ $r = array();
131
+ foreach ( $posts as $post ) {
132
+ $r[ $post->p2p_id ] = $post->ID;
133
+ }
134
+
135
+ assert( 'array_intersect_assoc($r, $raw) == $r' );
136
+
137
+ # // test 'each_*' query vars
138
+ # $posts = get_posts( array(
139
+ # 'post_type' => 'actor',
140
+ # 'post_status' => 'any',
141
+ # 'nopaging' => true,
142
+ # 'each_connected' => array(
143
+ # 'post_type' => 'movie',
144
+ # 'post_status' => 'any',
145
+ # 'nopaging' => true,
146
+ # ),
147
+ # 'suppress_filters' => false
148
+ # ) );
149
+
150
+ # self::walk( $posts );
151
+
152
+ # // test 'each_*' query vars
153
+ # $posts = get_posts( array(
154
+ # 'post_type' => 'actor',
155
+ # 'post_status' => 'any',
156
+ # 'nopaging' => true,
157
+ # 'each_connected' => array(
158
+ # 'post_type' => 'actor',
159
+ # 'post_status' => 'any',
160
+ # 'nopaging' => true,
161
+ # 'each_connected' => array(
162
+ # 'post_type' => 'actor',
163
+ # 'post_status' => 'any',
164
+ # 'nopaging' => true,
165
+ # ),
166
+ # ),
167
+ # 'suppress_filters' => false
168
+ # ) );
169
+
170
+ # self::walk( $posts );
171
+
172
+ # // test p2p_each_connected()
173
+ # $query = new WP_Query( array(
174
+ # 'post_type' => 'actor',
175
+ # 'post_status' => 'any',
176
+ # 'nopaging' => true,
177
+ # ) );
178
+
179
+ # p2p_each_connected( 'any', 'movies', array( 'post_type' => 'movie' ), $query );
180
+
181
+ # self::walk( $query->posts, 'movies' );
182
+
183
+ if ( $failed )
184
+ self::debug();
185
+ }
186
+
187
+ private function walk( $posts, $key = '', $level = 0 ) {
188
+ if ( 0 == $level )
189
+ echo '<pre>';
190
+
191
+ foreach ( $posts as $post ) {
192
+ echo str_repeat( "\t", $level ) . "$post->ID: $post->post_title\n";
193
+ self::walk( (array) @$post->{"connected_$key"}, $key, $level+1 );
194
+ }
195
+
196
+ if ( 0 == $level )
197
+ echo '</pre>';
198
+ }
199
+
200
+ function debug() {
201
+ global $wpdb;
202
+
203
+ $rows = $wpdb->get_results("SELECT * FROM $wpdb->p2p");
204
+
205
+ foreach ( $rows as $row ) {
206
+ echo html_link( get_edit_post_link( $row->p2p_from ), $row->p2p_from ) . ' -> ';
207
+ echo html_link( get_edit_post_link( $row->p2p_to ), $row->p2p_to );
208
+ echo '<br>';
209
+ }
210
+
211
+ die;
212
+ }
213
+ }
214
+
215
+ add_action( 'plugins_loaded', array('P2P_Test', 'init'), 11 );
216
+
ui/ui.js CHANGED
@@ -1,6 +1,6 @@
1
  jQuery(document).ready(function($) {
2
  $('.p2p_results').delegate('a', 'click', function() {
3
- var $self = $(this);
4
  $metabox = $self.parents('.p2p_metabox'),
5
  $list = $metabox.find('.p2p_connected'),
6
  post_id = $self.attr('name');
@@ -20,28 +20,33 @@ jQuery(document).ready(function($) {
20
  return false;
21
  });
22
 
23
- var delayed = undefined;
 
 
 
 
 
24
 
25
- $('.p2p_search :text').keyup(function() {
26
 
27
- if ( delayed != undefined )
28
  clearTimeout(delayed);
 
29
 
30
- var $self = $(this);
31
  $metabox = $self.parents('.p2p_metabox'),
32
  $results = $metabox.find('.p2p_results'),
33
- post_type = $self.attr('name').replace('p2p_search_', ''),
34
- old_value = '',
35
  $spinner = $metabox.find('.waiting');
36
 
37
- var delayed = setTimeout(function() {
38
  if ( !$self.val().length ) {
39
  $results.html('');
40
  return;
41
  }
42
 
43
- if ( $self.val() == old_value )
44
  return;
 
45
  old_value = $self.val();
46
 
47
  $spinner.show();
@@ -51,7 +56,7 @@ jQuery(document).ready(function($) {
51
  q: $self.val(),
52
  box_id: $metabox.attr('id').replace('p2p-box-', ''),
53
  reversed: +$metabox.hasClass('reversed')
54
- }
55
 
56
  $.getJSON(ajaxurl, data, function(data) {
57
  $spinner.hide();
1
  jQuery(document).ready(function($) {
2
  $('.p2p_results').delegate('a', 'click', function() {
3
+ var $self = $(this),
4
  $metabox = $self.parents('.p2p_metabox'),
5
  $list = $metabox.find('.p2p_connected'),
6
  post_id = $self.attr('name');
20
  return false;
21
  });
22
 
23
+ $('.p2p_search :text').keypress(function (ev) {
24
+ if ( 13 === ev.keyCode )
25
+ return false;
26
+ });
27
+
28
+ var delayed, old_value = '';
29
 
30
+ $('.p2p_search :text').keyup(function (ev) {
31
 
32
+ if ( undefined !== delayed ) {
33
  clearTimeout(delayed);
34
+ }
35
 
36
+ var $self = $(this),
37
  $metabox = $self.parents('.p2p_metabox'),
38
  $results = $metabox.find('.p2p_results'),
 
 
39
  $spinner = $metabox.find('.waiting');
40
 
41
+ delayed = setTimeout(function() {
42
  if ( !$self.val().length ) {
43
  $results.html('');
44
  return;
45
  }
46
 
47
+ if ( $self.val() === old_value ) {
48
  return;
49
+ }
50
  old_value = $self.val();
51
 
52
  $spinner.show();
56
  q: $self.val(),
57
  box_id: $metabox.attr('id').replace('p2p-box-', ''),
58
  reversed: +$metabox.hasClass('reversed')
59
+ };
60
 
61
  $.getJSON(ajaxurl, data, function(data) {
62
  $spinner.hide();