-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathbox.php
More file actions
330 lines (239 loc) · 7.98 KB
/
Copy pathbox.php
File metadata and controls
330 lines (239 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* A metabox in wp-admin
*/
class P2P_Box {
private $ctype;
private $args;
private $columns;
private $labels;
private $connected_items;
private static $enqueued_scripts = false;
private static $admin_box_qv = array(
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'post_status' => 'any',
);
function __construct( $args, $columns, $ctype ) {
$this->args = $args;
$this->columns = $columns;
$this->ctype = $ctype;
$this->labels = $this->ctype->get( 'opposite', 'labels' );
}
public function init_scripts() {
if ( self::$enqueued_scripts )
return;
wp_enqueue_style( 'p2p-box', plugins_url( 'box.css', __FILE__ ),
array(), P2P_PLUGIN_VERSION );
wp_register_script( 'mustache', plugins_url( 'mustache.js', __FILE__ ),
array(), '0.7.2', true );
wp_enqueue_script( 'p2p-box', plugins_url( 'box.js', __FILE__ ),
array( 'backbone', 'mustache' ), P2P_PLUGIN_VERSION, true );
wp_localize_script( 'p2p-box', 'P2PAdminL10n', array(
'nonce' => wp_create_nonce( P2P_BOX_NONCE ),
'spinner' => admin_url( 'images/wpspin_light.gif' ),
'deleteConfirmMessage' => __( 'Are you sure you want to delete all connections?', P2P_TEXTDOMAIN ),
) );
self::$enqueued_scripts = true;
add_action( 'admin_footer', array( __CLASS__, 'add_templates' ) );
}
static function add_templates() {
self::add_template( 'tab-list' );
self::add_template( 'table-row' );
}
private static function add_template( $slug ) {
echo html( 'script', array(
'type' => 'text/html',
'id' => "p2p-template-$slug"
), file_get_contents( dirname( __FILE__ ) . "/templates/$slug.html" ) );
}
function render( $item ) {
$extra_qv = array_merge( self::$admin_box_qv, array(
'p2p:context' => 'admin_box',
'p2p:per_page' => -1
) );
$this->connected_items = $this->ctype->get_connected( $item, $extra_qv, 'abstract' )->items;
$data = array(
'attributes' => $this->render_data_attributes(),
'connections' => $this->render_connections_table( $item ),
'create-connections' => $this->render_create_connections( $item ),
'help' => isset( $this->labels->help ) ? $this->labels->help : ''
);
echo P2P_Mustache::render( 'box', $data );
}
protected function render_data_attributes() {
$data_attr = array(
'p2p_type' => $this->ctype->name,
'duplicate_connections' => $this->ctype->duplicate_connections,
'cardinality' => $this->ctype->get( 'opposite', 'cardinality' ),
'direction' => $this->ctype->get_direction()
);
$data_attr_str = array();
foreach ( $data_attr as $key => $value )
$data_attr_str[] = "data-$key='" . $value . "'";
return implode( ' ', $data_attr_str );
}
protected function render_connections_table( $item ) {
$data = array();
if ( empty( $this->connected_items ) )
$data['hide'] = 'style="display:none"';
$tbody = array();
foreach ( $this->connected_items as $item ) {
$tbody[] = $this->connection_row( $item->p2p_id, $item );
}
$data['tbody'] = $tbody;
foreach ( $this->columns as $key => $field ) {
$data['thead'][] = array(
'column' => $key,
'title' => $field->get_title()
);
}
return $data;
}
protected function render_create_connections( $item ) {
$data = array(
'label' => $this->labels->create,
);
if ( 'one' == $this->ctype->get( 'opposite', 'cardinality' ) ) {
if ( !empty( $this->connected_items ) )
$data['hide'] = 'style="display:none"';
}
// Search tab
$tab_content = P2P_Mustache::render( 'tab-search', array(
'placeholder' => $this->labels->search_items,
) );
$data['tabs'][] = array(
'tab-id' => 'search',
'tab-title' => __( 'Search', P2P_TEXTDOMAIN ),
'is-active' => array(true),
'tab-content' => $tab_content
);
// "Create post" tab
if ( $this->can_create_post() ) {
$tab_content = P2P_Mustache::render( 'tab-create-post', array(
'title' => $this->labels->add_new_item
) );
$data['tabs'][] = array(
'tab-id' => 'create-post',
'tab-title' => $this->labels->new_item,
'tab-content' => $tab_content
);
}
$data['show-tab-headers'] = count( $data['tabs'] ) > 1 ? array(true) : false;
return $data;
}
protected function connection_row( $p2p_id, $item, $render = false ) {
$item->title = apply_filters( 'p2p_connected_title', $item->get_title(), $item->get_object(), $this->ctype );
$data = array();
foreach ( $this->columns as $key => $field ) {
$data['columns'][] = array(
'column' => $key,
'content' => $field->render( $p2p_id, $item )
);
}
if ( !$render )
return $data;
return P2P_Mustache::render( 'table-row', $data );
}
protected function candidate_row( $item ) {
$title = apply_filters( 'p2p_candidate_title', $item->get_title(), $item->get_object(), $this->ctype );
$title_data = array_merge( $this->columns['title']->get_data( $item ), array(
'title' => $title,
'item-id' => $item->get_id(),
) );
$data = array();
$data['columns'][] = array(
'column' => 'create',
'content' => P2P_Mustache::render( 'column-create', $title_data )
);
return $data;
}
protected function candidate_rows( $current_post_id, $page = 1, $search = '' ) {
$extra_qv = array_merge( self::$admin_box_qv, array(
'p2p:context' => 'admin_box_candidates',
'p2p:search' => $search,
'p2p:page' => $page,
'p2p:per_page' => 5
) );
$candidate = $this->ctype->get_connectable( $current_post_id, $extra_qv, 'abstract' );
if ( empty( $candidate->items ) ) {
return html( 'div class="p2p-notice"', $this->labels->not_found );
}
$data = array();
foreach ( $candidate->items as $item ) {
$data['rows'][] = $this->candidate_row( $item );
}
if ( $candidate->total_pages > 1 ) {
$data['navigation'] = array(
'current-page' => number_format_i18n( $candidate->current_page ),
'total-pages' => number_format_i18n( $candidate->total_pages ),
'total-pages-raw' => $candidate->total_pages,
'prev-inactive' => ( 1 == $candidate->current_page ) ? 'inactive' : '',
'next-inactive' => ( $candidate->total_pages == $candidate->current_page ) ? 'inactive' : '',
'prev-label' => __( 'previous', P2P_TEXTDOMAIN ),
'next-label' => __( 'next', P2P_TEXTDOMAIN ),
'of-label' => __( 'of', P2P_TEXTDOMAIN ),
);
}
return $data;
}
// Ajax handlers
public function ajax_create_post() {
if ( !$this->can_create_post() )
die( -1 );
$args = array(
'post_title' => $_POST['post_title'],
'post_author' => get_current_user_id(),
'post_type' => $this->ctype->get( 'opposite', 'side' )->first_post_type()
);
$from = absint( $_POST['from'] );
$args = apply_filters( 'p2p_new_post_args', $args, $this->ctype, $from );
$this->safe_connect( wp_insert_post( $args ) );
}
public function ajax_connect() {
$this->safe_connect( $_POST['to'] );
}
private function safe_connect( $to ) {
$from = absint( $_POST['from'] );
$to = absint( $to );
if ( !$from || !$to )
die(-1);
$p2p_id = $this->ctype->connect( $from, $to );
self::maybe_send_error( $p2p_id );
$item = $this->ctype->get( 'opposite','side')->item_recognize( $to );
$out = array(
'row' => $this->connection_row( $p2p_id, $item, true )
);
die( json_encode( $out ) );
}
public function ajax_disconnect() {
p2p_delete_connection( $_POST['p2p_id'] );
$this->refresh_candidates();
}
public function ajax_clear_connections() {
$r = $this->ctype->disconnect( $_POST['from'], 'any' );
self::maybe_send_error( $r );
$this->refresh_candidates();
}
protected static function maybe_send_error( $r ) {
if ( !is_wp_error( $r ) )
return;
$out = array(
'error' => $r->get_error_message()
);
die( json_encode( $out ) );
}
public function ajax_search() {
$this->refresh_candidates();
}
private function refresh_candidates() {
die( json_encode( $this->candidate_rows(
$_REQUEST['from'], $_REQUEST['paged'], $_REQUEST['s'] ) ) );
}
protected function can_create_post() {
if ( !$this->args->can_create_post )
return false;
$side = $this->ctype->get( 'opposite', 'side' );
return $side->can_create_item();
}
}