Issues (1223)

includes/class-lsx-team-scpo-engine.php (2 issues)

1
<?php
2
3
$lsx_team_scporder = new LSX_TEAM_SCPO_Engine();
4
5
/**
6
 * SCPO Engine
7
 *
8
 * @package   LSX Team
9
 * @author    LightSpeed
10
 * @license   GPL3
11
 * @link
12
 * @copyright 2018 LightSpeed
13
 */
14
class LSX_TEAM_SCPO_Engine {
15
16
	public function __construct() {
17
		if ( ! get_option( 'lsx_team_scporder_install' ) )
18
			$this->lsx_team_scporder_install();
19
20
		add_action( 'admin_init', array( $this, 'refresh' ) );
21
		add_action( 'admin_init', array( $this, 'load_script_css' ) );
22
23
		add_action( 'wp_ajax_update-menu-order', array( $this, 'update_menu_order' ) );
24
		add_action( 'wp_ajax_update-menu-order-tags', array( $this, 'update_menu_order_tags' ) );
25
26
		add_action( 'pre_get_posts', array( $this, 'lsx_team_scporder_pre_get_posts' ) );
27
28
		add_filter( 'get_previous_post_where', array( $this, 'lsx_team_scporder_previous_post_where' ) );
29
		add_filter( 'get_previous_post_sort', array( $this, 'lsx_team_scporder_previous_post_sort' ) );
30
		add_filter( 'get_next_post_where', array( $this, 'lsx_team_scporder_next_post_where' ) );
31
		add_filter( 'get_next_post_sort', array( $this, 'lsx_team_scporder_next_post_sort' ) );
32
33
		add_filter( 'get_terms_orderby', array( $this, 'lsx_team_scporder_get_terms_orderby' ), 10, 3 );
34
		add_filter( 'wp_get_object_terms', array( $this, 'lsx_team_scporder_get_object_terms' ), 10, 3 );
35
		add_filter( 'get_terms', array( $this, 'lsx_team_scporder_get_object_terms' ), 10, 3 );
36
	}
37
38
	public function lsx_team_scporder_install() {
39
		global $wpdb;
40
		$result = $wpdb->query( "DESCRIBE $wpdb->terms `lsx_team_term_order`" );
41
42
		if ( ! $result ) {
43
			$result = $wpdb->query( "ALTER TABLE $wpdb->terms ADD `lsx_team_term_order` INT( 4 ) NULL DEFAULT '0'" );
44
		}
45
46
		update_option( 'lsx_team_scporder_install', 1 );
47
	}
48
49
	public function _check_load_script_css() {
50
		$active = false;
51
52
		$objects = $this->get_lsx_team_scporder_options_objects();
53
		$tags = $this->get_lsx_team_scporder_options_tags();
54
55
		if ( empty( $objects ) && empty( $tags ) )
56
			return false;
57
58
		if ( isset( $_GET['orderby'] ) || strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'action=edit' ) || strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'wp-admin/post-new.php' ) )
59
			return false;
60
61
		if ( ! empty( $objects ) ) {
62
			if ( isset( $_GET['post_type'] ) && ! isset( $_GET['taxonomy'] ) && array_key_exists( sanitize_text_field( wp_unslash( $_GET['post_type'] ) ), $objects ) ) { // if page or custom post types.
63
				$active = true;
64
			}
65
			if ( ! isset( $_GET['post_type'] ) && strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'wp-admin/edit.php' ) && array_key_exists( 'post', $objects ) ) { // if post.
66
				$active = true;
67
			}
68
		}
69
70
		if ( ! empty( $tags ) ) {
71
			if ( isset( $_GET['taxonomy'] ) && array_key_exists( sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ), $tags ) ) {
72
				$active = true;
73
			}
74
		}
75
76
		return $active;
77
	}
78
79
	public function load_script_css() {
80
		if ( $this->_check_load_script_css() ) {
81
			wp_enqueue_script( 'scporderjs', LSX_TEAM_URL . 'assets/js/scporder.min.js', array( 'jquery', 'jquery-ui-sortable' ), null, true );
82
83
			$scporderjs_params = array(
84
				'ajax_url' => admin_url( 'admin-ajax.php' ),
85
				'ajax_nonce' => wp_create_nonce( 'scporder' ),
86
			 );
87
88
			wp_localize_script( 'scporderjs', 'scporderjs_params', $scporderjs_params );
89
90
			wp_enqueue_style( 'scporder', LSX_TEAM_URL . 'assets/css/scporder.css', array(), null );
91
		}
92
	}
93
94
	public function refresh() {
95
		global $wpdb;
96
		$objects = $this->get_lsx_team_scporder_options_objects();
97
		$tags = $this->get_lsx_team_scporder_options_tags();
98
99
		if ( ! empty( $objects ) ) {
100
			foreach ( $objects as $object => $object_data ) {
101
				$result = $wpdb->get_results( $wpdb->prepare( "
102
					SELECT count( * ) as cnt, max( menu_order ) as max, min( menu_order ) as min
103
					FROM $wpdb->posts
104
					WHERE post_type = '%s' AND post_status IN ( 'publish', 'pending', 'draft', 'private', 'future' )
105
				", $object ) );
106
107
				if ( 0 == $result[0]->cnt || $result[0]->cnt == $result[0]->max )
108
					continue;
109
110
				$results = $wpdb->get_results( $wpdb->prepare( "
111
					SELECT ID
112
					FROM $wpdb->posts
113
					WHERE post_type = '%s' AND post_status IN ( 'publish', 'pending', 'draft', 'private', 'future' )
114
					ORDER BY menu_order ASC
115
				", $object ) );
116
117
				foreach ( $results as $key => $result ) {
118
					$wpdb->update( $wpdb->posts,
119
						array(
120
							'menu_order' => $key + 1,
121
						),
122
						array(
123
							'ID' => $result->ID,
124
						)
125
					);
126
				}
127
			}
128
		}
129
130
		if ( ! empty( $tags ) ) {
131
			foreach ( $tags as $taxonomy => $taxonomy_data ) {
132
				$result = $wpdb->get_results( $wpdb->prepare( "
133
					SELECT count( * ) as cnt, max( lsx_team_term_order ) as max, min( lsx_team_term_order ) as min
134
					FROM $wpdb->terms AS terms
135
					INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
136
					WHERE term_taxonomy.taxonomy = '%s'
137
				", $taxonomy ) );
138
139
				if ( 0 == $result[0]->cnt || $result[0]->cnt == $result[0]->max )
140
					continue;
141
142
				$results = $wpdb->get_results( $wpdb->prepare( "
143
					SELECT terms.term_id
144
					FROM $wpdb->terms AS terms
145
					INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
146
					WHERE term_taxonomy.taxonomy = '%s'
147
					ORDER BY lsx_team_term_order ASC
148
				", $taxonomy ) );
149
150
				foreach ( $results as $key => $result ) {
151
					$wpdb->update(
152
						$wpdb->terms,
153
						array(
154
							'lsx_team_term_order' => $key + 1,
155
						),
156
						array(
157
							'term_id' => $result->term_id,
158
						)
159
					);
160
				}
161
			}
162
		}
163
	}
164
165
	public function update_menu_order() {
166
		check_ajax_referer( 'scporder', 'security' );
167
168
		global $wpdb;
169
170
		parse_str( sanitize_text_field( wp_unslash( $_POST['order'] ) ), $data );
171
172
		if ( ! is_array( $data ) )
173
			return false;
174
175
		$id_arr = array();
176
177
		foreach ( $data as $key => $values ) {
178
			foreach ( $values as $position => $id ) {
179
				$id_arr[] = $id;
180
			}
181
		}
182
183
		$menu_order_arr = array();
184
185
		foreach ( $id_arr as $key => $id ) {
186
			$results = $wpdb->get_results( "SELECT menu_order FROM $wpdb->posts WHERE ID = " . intval( $id ) );
187
			foreach ( $results as $result ) {
188
				$menu_order_arr[] = $result->menu_order;
189
			}
190
		}
191
192
		sort( $menu_order_arr );
193
194
		foreach ( $data as $key => $values ) {
195
			foreach ( $values as $position => $id ) {
196
				$wpdb->update( $wpdb->posts,
197
					array(
198
						'menu_order' => $menu_order_arr[ $position ],
199
					),
200
					array(
201
						'ID' => intval( $id ),
202
					)
203
				);
204
			}
205
		}
206
	}
207
208
	public function update_menu_order_tags() {
209
		check_ajax_referer( 'scporder', 'security' );
210
211
		global $wpdb;
212
213
		parse_str( sanitize_text_field( wp_unslash( $_POST['order'] ) ), $data );
214
215
		if ( ! is_array( $data ) )
216
			return false;
217
218
		$id_arr = array();
219
220
		foreach ( $data as $key => $values ) {
221
			foreach ( $values as $position => $id ) {
222
				$id_arr[] = $id;
223
			}
224
		}
225
226
		$menu_order_arr = array();
227
228
		foreach ( $id_arr as $key => $id ) {
229
			$results = $wpdb->get_results( "
230
				SELECT lsx_team_term_order 
231
				FROM $wpdb->terms 
232
				WHERE term_id = " . intval( $id ) );
233
			foreach ( $results as $result ) {
234
				$menu_order_arr[] = $result->lsx_team_term_order;
235
			}
236
		}
237
238
		sort( $menu_order_arr );
239
240
		foreach ( $data as $key => $values ) {
241
			foreach ( $values as $position => $id ) {
242
				$wpdb->update( $wpdb->terms,
243
					array(
244
						'lsx_team_term_order' => $menu_order_arr[ $position ],
245
					),
246
					array(
247
						'term_id' => intval( $id ),
248
					)
249
				);
250
			}
251
		}
252
	}
253
254
	public function lsx_team_scporder_previous_post_where( $where ) {
255
		global $post;
256
		$objects = $this->get_lsx_team_scporder_options_objects();
257
258
		if ( empty( $objects ) )
259
			return $where;
260
261
		if ( isset( $post->post_type ) && array_key_exists( $post->post_type, $objects ) ) {
262
			$current_menu_order = $post->menu_order;
263
			$where = "WHERE p.menu_order > '" . $current_menu_order . "' AND p.post_type = '" . $post->post_type . "' AND p.post_status = 'publish'";
264
		}
265
266
		return $where;
267
	}
268
269
	public function lsx_team_scporder_previous_post_sort( $orderby ) {
270
		global $post;
271
		$objects = $this->get_lsx_team_scporder_options_objects();
272
273
		if ( empty( $objects ) )
274
			return $orderby;
275
276
		if ( isset( $post->post_type ) && array_key_exists( $post->post_type, $objects ) ) {
277
			$orderby = 'ORDER BY p.menu_order ASC LIMIT 1';
278
		}
279
280
		return $orderby;
281
	}
282
283
	public function lsx_team_scporder_next_post_where( $where ) {
284
		global $post;
285
		$objects = $this->get_lsx_team_scporder_options_objects();
286
287
		if ( empty( $objects ) )
288
			return $where;
289
290
		if ( isset( $post->post_type ) && array_key_exists( $post->post_type, $objects ) ) {
291
			$current_menu_order = $post->menu_order;
292
			$where = "WHERE p.menu_order < '" . $current_menu_order . "' AND p.post_type = '" . $post->post_type . "' AND p.post_status = 'publish'";
293
		}
294
295
		return $where;
296
	}
297
298
	public function lsx_team_scporder_next_post_sort( $orderby ) {
299
		global $post;
300
		$objects = $this->get_lsx_team_scporder_options_objects();
301
302
		if ( empty( $objects ) )
303
			return $orderby;
304
305
		if ( isset( $post->post_type ) && array_key_exists( $post->post_type, $objects ) ) {
306
			$orderby = 'ORDER BY p.menu_order DESC LIMIT 1';
307
		}
308
309
		return $orderby;
310
	}
311
312
	public function lsx_team_scporder_pre_get_posts( $wp_query ) {
313
		$objects = $this->get_lsx_team_scporder_options_objects();
314
315
		if ( empty( $objects ) )
316
			return false;
317
318
		if ( is_admin() ) {
319
			if ( isset( $wp_query->query['post_type'] ) && ! isset( $_GET['orderby'] ) ) {
320
				if ( array_key_exists( $wp_query->query['post_type'], $objects ) ) {
321
					$wp_query->set( 'orderby', 'menu_order' );
322
					$wp_query->set( 'order', 'ASC' );
323
				}
324
			}
325
		} else {
326
			$active = false;
327
328
			if ( isset( $wp_query->query['post_type'] ) ) {
329
				if ( ! is_array( $wp_query->query['post_type'] ) ) {
330
					if ( array_key_exists( $wp_query->query['post_type'], $objects ) ) {
331
						$active = true;
332
					}
333
				}
334
			} else {
335
				if ( array_key_exists( 'post', $objects ) ) {
336
					$active = true;
337
				}
338
			}
339
340
			if ( ! $active )
341
				return false;
342
343
			if ( isset( $wp_query->query['suppress_filters'] ) ) {
344
				if ( $wp_query->get( 'orderby' ) == 'date' )
345
					$wp_query->set( 'orderby', 'menu_order' );
346
				if ( $wp_query->get( 'order' ) == 'DESC' )
347
					$wp_query->set( 'order', 'ASC' );
348
			} else {
349
				if ( ! $wp_query->get( 'orderby' ) )
350
					$wp_query->set( 'orderby', 'menu_order' );
351
				if ( ! $wp_query->get( 'order' ) )
352
					$wp_query->set( 'order', 'ASC' );
353
			}
354
		}
355
	}
356
357
	public function lsx_team_scporder_get_terms_orderby( $orderby, $args ) {
358
		if ( is_admin() )
359
			return $orderby;
360
361
		$tags = $this->get_lsx_team_scporder_options_tags();
362
363
		if ( ! isset( $args['taxonomy'] ) )
364
			return $orderby;
365
366
		$taxonomy = $args['taxonomy'];
367
		if ( is_array( $taxonomy ) && count( $taxonomy ) == 1 )
368
			$taxonomy = $taxonomy[0];
369
		if ( ! array_key_exists( $taxonomy[0], $tags ) )
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
370
			return $orderby;
371
372
		$orderby = 't.lsx_team_term_order';
373
		return $orderby;
374
	}
375
376
	public function lsx_team_scporder_get_object_terms( $terms ) {
377
		$tags = $this->get_lsx_team_scporder_options_tags();
378
379
		if ( is_admin() && isset( $_GET['orderby'] ) )
380
			return $terms;
381
382
		foreach ( $terms as $key => $term ) {
383
			if ( is_object( $term ) && isset( $term->taxonomy ) ) {
384
				$taxonomy = $term->taxonomy;
385
				if ( ! array_key_exists( $taxonomy, $tags ) )
386
					return $terms;
387
			} else {
388
				return $terms;
389
			}
390
		}
391
392
		usort( $terms, array( $this, 'taxcmp' ) );
393
		return $terms;
394
	}
395
396
	public function taxcmp( $a, $b ) {
397
		if ( $a->lsx_team_term_order == $b->lsx_team_term_order )
398
			return 0;
399
400
		return ( $a->lsx_team_term_order < $b->lsx_team_term_order ) ? -1 : 1;
401
	}
402
403
	public function get_lsx_team_scporder_options_objects() {
404
		return array(
405
			'team' => esc_html_x( 'Team Member', 'post type singular name', 'lsx-team' ),
406
		);
407
	}
408
409
	public function get_lsx_team_scporder_options_tags() {
410
		return array(
411
			'team_role' => esc_html_x( 'Role', 'taxonomy singular name', 'lsx-team' ),
412
		);
413
	}
414
415
}
416
417
/**
418
 * SCP Order Uninstall hook
419
 */
420
register_uninstall_hook( __FILE__, 'lsx_team_scporder_uninstall' );
421
422
function lsx_team_scporder_uninstall() {
423
	global $wpdb;
424
425
	if ( function_exists( 'is_multisite' ) && is_multisite() ) {
426
		$curr_blog = $wpdb->blogid;
427
		$blogids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
428
429
		foreach ( $blogids as $blog_id ) {
430
			switch_to_blog( $blog_id );
431
			lsx_team_scporder_uninstall_db();
432
		}
433
434
		switch_to_blog( $curr_blog );
435
	} else {
436
		lsx_team_scporder_uninstall_db();
437
	}
438
}
439
440
function lsx_team_scporder_uninstall_db() {
441
	global $wpdb;
442
	$result = $wpdb->query( "DESCRIBE $wpdb->terms `lsx_team_term_order`" );
443
444
	if ( $result ) {
445
		$result = $wpdb->query( "ALTER TABLE $wpdb->terms DROP `lsx_team_term_order`" );
446
	}
447
448
	delete_option( 'lsx_team_scporder_install' );
449
}
450