Simple Custom Post Order - Version 2.5.5

Version Description

Download this release

Release Info

Developer giucu91
Plugin Icon wp plugin Simple Custom Post Order
Version 2.5.5
Comparing to
See all releases

Code changes from version 2.5.4 to 2.5.5

Files changed (3) hide show
  1. readme.txt +5 -1
  2. settings.php +407 -390
  3. simple-custom-post-order.php +599 -562
readme.txt CHANGED
@@ -4,7 +4,7 @@ Tags: custom post order, post order, js post order, page order, posts order, cat
4
  Requires at least: 4.6
5
  Requires PHP: 5.6
6
  Tested up to: 5.7
7
- Stable tag: 2.5.4
8
  License: GPLv3 or later
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
@@ -36,6 +36,10 @@ An answer to that question.
36
 
37
  == Changelog ==
38
 
 
 
 
 
39
  = Version 2.5.4 - 05.03.2021 =
40
  Changed: Improved Performance ( https://github.com/ColorlibHQ/simple-custom-post-order/issues/105 )
41
 
4
  Requires at least: 4.6
5
  Requires PHP: 5.6
6
  Tested up to: 5.7
7
+ Stable tag: 2.5.5
8
  License: GPLv3 or later
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
36
 
37
  == Changelog ==
38
 
39
+ = Version 2.5.5 - 11.05.2021 =
40
+ Changed : Code Cleaning
41
+ Changed : Allow custom orderby in Block Preview ( https://github.com/ColorlibHQ/simple-custom-post-order/issues/98 )
42
+
43
  = Version 2.5.4 - 05.03.2021 =
44
  Changed: Improved Performance ( https://github.com/ColorlibHQ/simple-custom-post-order/issues/105 )
45
 
settings.php CHANGED
@@ -1,402 +1,419 @@
1
  <?php
2
- $scporder_options = get_option('scporder_options');
3
- $scporder_objects = isset($scporder_options['objects']) ? $scporder_options['objects'] : array();
4
- $scporder_tags = isset($scporder_options['tags']) ? $scporder_options['tags'] : array();
5
 
6
- $advanced_view = isset($scporder_options['show_advanced_view']) ? $scporder_options['show_advanced_view'] : '';
7
  ?>
8
  <style>
9
 
10
- .epsilon-toggle {
11
- position: relative;
12
- display:inline-block;
13
- user-select: none;
14
- }
15
-
16
- .epsilon-toggle__items {
17
- box-sizing: border-box;
18
- }
19
-
20
- .epsilon-toggle__items > * {
21
- box-sizing: inherit;
22
- }
23
-
24
- .epsilon-toggle__input[type=checkbox] {
25
- border-radius: 2px;
26
- border: 2px solid #6c7781;
27
- margin-right: 12px;
28
- transition: none;
29
- height: 100%;
30
- left: 0;
31
- top: 0;
32
- margin: 0;
33
- padding: 0;
34
- opacity: 0;
35
- position: absolute;
36
- width: 100%;
37
- z-index: 1;
38
- }
39
-
40
- .epsilon-toggle__track {
41
- background-color: #fff;
42
- border: 2px solid #6c7781;
43
- border-radius: 9px;
44
- display: inline-block;
45
- height: 18px;
46
- width: 36px;
47
- vertical-align: top;
48
- transition: background .2s ease;
49
- }
50
-
51
- .epsilon-toggle__thumb {
52
- background-color: #6c7781;
53
- border: 5px solid #6c7781;
54
- border-radius: 50%;
55
- display: block;
56
- height: 10px;
57
- width: 10px;
58
- position: absolute;
59
- left: 4px;
60
- top: 4px;
61
- transition: transform .2s ease;
62
- }
63
-
64
- .epsilon-toggle__off {
65
- position: absolute;
66
- right: 6px;
67
- top: 6px;
68
- color: #6c7781;
69
- fill: currentColor;
70
- }
71
-
72
- .epsilon-toggle__on {
73
- position: absolute;
74
- top: 6px;
75
- left: 8px;
76
- border: 1px solid #fff;
77
- outline: 1px solid transparent;
78
- outline-offset: -1px;
79
- display: none;
80
- }
81
-
82
-
83
- .epsilon-toggle__input[type=checkbox]:checked + .epsilon-toggle__items .epsilon-toggle__track {
84
- background-color: #11a0d2;
85
- border: 9px solid transparent;
86
- }
87
-
88
- .epsilon-toggle__input[type=checkbox]:checked + .epsilon-toggle__items .epsilon-toggle__thumb {
89
- background-color: #fff;
90
- border-width: 0;
91
- transform: translateX(18px);
92
- }
93
-
94
- .epsilon-toggle__input[type=checkbox]:checked + .epsilon-toggle__items .epsilon-toggle__off {
95
- display: none;
96
- }
97
-
98
- .epsilon-toggle__input[type=checkbox]:checked + .epsilon-toggle__items .epsilon-toggle__on {
99
- display: inline-block;
100
- }
101
-
102
- .scpo-reset-response {
103
- margin-left:15px;
104
- color:#0085ba;
105
- }
106
- </style>
107
  <div class="wrap">
108
- <h2><?php _e('Simple Custom Post Order Settings', 'simple-custom-post-order'); ?></h2>
109
- <?php if (isset($_GET['msg'])) : ?>
110
- <div id="message" class="updated below-h2">
111
- <?php if ($_GET['msg'] == 'update') : ?>
112
- <p><?php _e('Settings Updated.','simple-custom-post-order'); ?></p>
113
- <?php endif; ?>
114
- </div>
115
- <?php endif; ?>
116
-
117
- <form method="post">
118
-
119
- <?php if (function_exists('wp_nonce_field')) wp_nonce_field('nonce_scporder'); ?>
120
-
121
- <div id="scporder_select_objects">
122
-
123
- <table class="form-table">
124
- <tbody>
125
- <tr valign="top">
126
- <th scope="row"><?php _e('Check to Sort Post Types', 'simple-custom-post-order') ?></th>
127
- <td>
128
-
129
- <label>
130
- <div class="epsilon-toggle">
131
- <input id="scporder_allcheck_objects" class="epsilon-toggle__input" type="checkbox">
132
- <div class="epsilon-toggle__items">
133
- <span class="epsilon-toggle__track"></span>
134
- <span class="epsilon-toggle__thumb"></span>
135
- <svg class="epsilon-toggle__off" width="6" height="6" aria-hidden="true"
136
- role="img" focusable="false" viewBox="0 0 6 6">
137
- <path d="M3 1.5c.8 0 1.5.7 1.5 1.5S3.8 4.5 3 4.5 1.5 3.8 1.5 3 2.2 1.5 3 1.5M3 0C1.3 0 0 1.3 0 3s1.3 3 3 3 3-1.3 3-3-1.3-3-3-3z"></path>
138
- </svg>
139
- <svg class="epsilon-toggle__on" width="2" height="6" aria-hidden="true"
140
- role="img" focusable="false" viewBox="0 0 2 6">
141
- <path d="M0 0h2v6H0z"></path>
142
- </svg>
143
- </div>
144
- </div>
145
- &nbsp;<?php _e('Check All', 'simple-custom-post-order') ?></label><br>
146
- <?php
147
- $post_types_args = apply_filters('scpo_post_types_args', array(
148
- 'show_ui' => true,
149
- 'show_in_menu' => true,
150
- ),$scporder_options);
151
-
152
- $post_types = get_post_types($post_types_args, 'objects');
153
-
154
- foreach ($post_types as $post_type) {
155
- if ($post_type->name == 'attachment')
156
- continue;
157
- ?>
158
- <label>
159
- <div class="epsilon-toggle">
160
- <input class="epsilon-toggle__input" type="checkbox"
161
- name="objects[]" value="<?php echo $post_type->name; ?>" <?php
162
- if (isset($scporder_objects) && is_array($scporder_objects)) {
163
- if (in_array($post_type->name, $scporder_objects)) {
164
- echo 'checked="checked"';
165
- }
166
- }
167
- ?>>
168
- <div class="epsilon-toggle__items">
169
- <span class="epsilon-toggle__track"></span>
170
- <span class="epsilon-toggle__thumb"></span>
171
- <svg class="epsilon-toggle__off" width="6" height="6" aria-hidden="true"
172
- role="img" focusable="false" viewBox="0 0 6 6">
173
- <path d="M3 1.5c.8 0 1.5.7 1.5 1.5S3.8 4.5 3 4.5 1.5 3.8 1.5 3 2.2 1.5 3 1.5M3 0C1.3 0 0 1.3 0 3s1.3 3 3 3 3-1.3 3-3-1.3-3-3-3z"></path>
174
- </svg>
175
- <svg class="epsilon-toggle__on" width="2" height="6" aria-hidden="true"
176
- role="img" focusable="false" viewBox="0 0 2 6">
177
- <path d="M0 0h2v6H0z"></path>
178
- </svg>
179
- </div>
180
- </div>
181
- &nbsp;<?php echo $post_type->label; ?></label><br>
182
- <?php
183
- }
184
- ?>
185
- </td>
186
- </tr>
187
- </tbody>
188
- </table>
189
-
190
- </div>
191
-
192
-
193
- <div id="scporder_select_tags">
194
- <table class="form-table">
195
- <tbody>
196
- <tr valign="top">
197
- <th scope="row"><?php _e('Check to Sort Taxonomies', 'simple-custom-post-order') ?></th>
198
- <td>
199
-
200
- <label>
201
- <div class="epsilon-toggle">
202
- <input id="scporder_allcheck_tags" class="epsilon-toggle__input" type="checkbox">
203
- <div class="epsilon-toggle__items">
204
- <span class="epsilon-toggle__track"></span>
205
- <span class="epsilon-toggle__thumb"></span>
206
- <svg class="epsilon-toggle__off" width="6" height="6" aria-hidden="true"
207
- role="img" focusable="false" viewBox="0 0 6 6">
208
- <path d="M3 1.5c.8 0 1.5.7 1.5 1.5S3.8 4.5 3 4.5 1.5 3.8 1.5 3 2.2 1.5 3 1.5M3 0C1.3 0 0 1.3 0 3s1.3 3 3 3 3-1.3 3-3-1.3-3-3-3z"></path>
209
- </svg>
210
- <svg class="epsilon-toggle__on" width="2" height="6" aria-hidden="true"
211
- role="img" focusable="false" viewBox="0 0 2 6">
212
- <path d="M0 0h2v6H0z"></path>
213
- </svg>
214
- </div>
215
- </div>
216
- &nbsp;<?php _e('Check All', 'simple-custom-post-order') ?></label><br>
217
-
218
- <?php
219
- $taxonomies = get_taxonomies(array(
220
- 'show_ui' => true,
221
- ), 'objects');
222
-
223
- foreach ($taxonomies as $taxonomy) {
224
- if ($taxonomy->name == 'post_format')
225
- continue;
226
- ?>
227
- <label>
228
- <div class="epsilon-toggle">
229
- <input class="epsilon-toggle__input" type="checkbox"
230
- name="tags[]" value="<?php echo $taxonomy->name; ?>" <?php
231
- if (isset($scporder_tags) && is_array($scporder_tags)) {
232
- if (in_array($taxonomy->name, $scporder_tags)) {
233
- echo 'checked="checked"';
234
- }
235
- }
236
- ?>>
237
- <div class="epsilon-toggle__items">
238
- <span class="epsilon-toggle__track"></span>
239
- <span class="epsilon-toggle__thumb"></span>
240
- <svg class="epsilon-toggle__off" width="6" height="6" aria-hidden="true"
241
- role="img" focusable="false" viewBox="0 0 6 6">
242
- <path d="M3 1.5c.8 0 1.5.7 1.5 1.5S3.8 4.5 3 4.5 1.5 3.8 1.5 3 2.2 1.5 3 1.5M3 0C1.3 0 0 1.3 0 3s1.3 3 3 3 3-1.3 3-3-1.3-3-3-3z"></path>
243
- </svg>
244
- <svg class="epsilon-toggle__on" width="2" height="6" aria-hidden="true"
245
- role="img" focusable="false" viewBox="0 0 2 6">
246
- <path d="M0 0h2v6H0z"></path>
247
- </svg>
248
- </div>
249
- </div>
250
- &nbsp;<?php echo $taxonomy->label ?></label><br>
251
- <?php
252
- }
253
- ?>
254
- </td>
255
- </tr>
256
- </tbody>
257
- </table>
258
-
259
- </div>
260
- <div id="scporder_advanved_view">
261
- <table class="form-table">
262
- <tbody>
263
- <tr valign="top">
264
- <th scope="row"><?php _e('Check to see advanced view of Post Types ', 'simple-custom-post-order') ?></th>
265
- <td>
266
- <label>
267
- <div class="epsilon-toggle">
268
- <input class="epsilon-toggle__input" type="checkbox"
269
- name="show_advanced_view" value="1" <?php checked( '1', $advanced_view, 'checked="checked"' ); ?>>
270
- <div class="epsilon-toggle__items">
271
- <span class="epsilon-toggle__track"></span>
272
- <span class="epsilon-toggle__thumb"></span>
273
- <svg class="epsilon-toggle__off" width="6" height="6" aria-hidden="true"
274
- role="img" focusable="false" viewBox="0 0 6 6">
275
- <path d="M3 1.5c.8 0 1.5.7 1.5 1.5S3.8 4.5 3 4.5 1.5 3.8 1.5 3 2.2 1.5 3 1.5M3 0C1.3 0 0 1.3 0 3s1.3 3 3 3 3-1.3 3-3-1.3-3-3-3z"></path>
276
- </svg>
277
- <svg class="epsilon-toggle__on" width="2" height="6" aria-hidden="true"
278
- role="img" focusable="false" viewBox="0 0 2 6">
279
- <path d="M0 0h2v6H0z"></path>
280
- </svg>
281
- </div>
282
- </div>
283
- <?php echo __('Show advanced view of Post Types','simple-custom-post-order'); ?>
284
- </label><br>
285
- <p class="description"><?php _e('NOTICE: This is for advanced users only.','simple-custom-post-order'); ?></p>
286
- <!--@todo : @giucu please look into below description. -->
287
- <p class="description"><?php _e('Check to include other custom post types. You will need to update your settings after enabling this option.','simple-custom-post-order'); ?></p>
288
-
289
- </td>
290
- </tr>
291
- </tbody>
292
- </table>
293
- </div>
294
-
295
- <p class="submit">
296
- <input type="submit" class="button-primary" name="scporder_submit" value="<?php _e('Update', 'simple-custom-post-order'); ?>">
297
- </p>
298
-
299
- </form>
300
- <div class="scpo-reset-order">
301
- <h1>Want to reset the order of the posts?</h1>
302
- <div id="scpo_reset_select_objects">
303
- <table class="form-table">
304
- <tbody>
305
- <tr valign="top">
306
- <th scope="row"><?php _e('Check to reset order of Post Types', 'simple-custom-post-order') ?></th>
307
- <td>
308
- <?php
309
- foreach ($post_types as $post_type) {
310
- if ($post_type->name == 'attachment')
311
- continue;
312
- ?>
313
- <label>
314
- <div class="epsilon-toggle">
315
- <input class="epsilon-toggle__input" type="checkbox"
316
- name="<?php echo $post_type->name; ?>" value="">
317
- <div class="epsilon-toggle__items">
318
- <span class="epsilon-toggle__track"></span>
319
- <span class="epsilon-toggle__thumb"></span>
320
- <svg class="epsilon-toggle__off" width="6" height="6" aria-hidden="true"
321
- role="img" focusable="false" viewBox="0 0 6 6">
322
- <path d="M3 1.5c.8 0 1.5.7 1.5 1.5S3.8 4.5 3 4.5 1.5 3.8 1.5 3 2.2 1.5 3 1.5M3 0C1.3 0 0 1.3 0 3s1.3 3 3 3 3-1.3 3-3-1.3-3-3-3z"></path>
323
- </svg>
324
- <svg class="epsilon-toggle__on" width="2" height="6" aria-hidden="true"
325
- role="img" focusable="false" viewBox="0 0 2 6">
326
- <path d="M0 0h2v6H0z"></path>
327
- </svg>
328
- </div>
329
- </div>
330
- &nbsp;<?php echo $post_type->label; ?></label><br>
331
- <?php
332
- }
333
- ?>
334
- </td>
335
- </tr>
336
- </tbody>
337
- </table>
338
-
339
- </div>
340
- <div>
341
- <a id="reset-scp-order" class="button button-primary" href="#">Reset order</a>
342
- <span class="scpo-reset-response"></span>
343
- </div>
344
- </div>
345
-
346
- <h3>Like this simple plugin?</h3>
347
- <p>Make sure to <a href="https://wordpress.org/support/plugin/simple-custom-post-order/reviews/?filter=5"><strong>rate it</strong></a> and visit us at <a href="https://colorlib.com/wp/"><strong>Colorlib.com</strong></a></p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348
 
349
 
350
  </div>
351
 
352
  <script>
353
- (function ($) {
354
-
355
- $("#scporder_allcheck_objects").on('click', function () {
356
- var items = $("#scporder_select_objects input");
357
- if ($(this).is(':checked'))
358
- $(items).prop('checked', true);
359
- else
360
- $(items).prop('checked', false);
361
- });
362
-
363
- $("#scporder_allcheck_tags").on('click', function () {
364
- var items = $("#scporder_select_tags input");
365
- if ($(this).is(':checked'))
366
- $(items).prop('checked', true);
367
- else
368
- $(items).prop('checked', false);
369
- });
370
-
371
- // Reset order function
372
- $('#reset-scp-order').click(function (e) {
373
-
374
- e.preventDefault();
375
- var btn = $(this),
376
- item_input = $(this).parents('.scpo-reset-order').find('input:checked'),
377
- items = [],
378
- data = {
379
- action: 'scpo_reset_order',
380
- scpo_security: '<?php echo wp_create_nonce("scpo-reset-order"); ?>'
381
- };
382
-
383
- if (item_input.length > 0) {
384
- item_input.each(function (i, item) {
385
- items.push(item.name);
386
- });
387
-
388
- data['items'] = items;
389
-
390
- $.post("<?php echo admin_url('admin-ajax.php'); ?>", data, function (response) {
391
- if (response) {
392
- btn.next('.scpo-reset-response').text(response);
393
- setTimeout(function(){
394
- location.reload(true);
395
- },1500);
396
- }
397
- });
398
- }
399
- });
400
-
401
- })(jQuery)
402
  </script>
1
  <?php
2
+ $scporder_options = get_option( 'scporder_options' );
3
+ $scporder_objects = isset( $scporder_options['objects'] ) ? $scporder_options['objects'] : array();
4
+ $scporder_tags = isset( $scporder_options['tags'] ) ? $scporder_options['tags'] : array();
5
 
6
+ $advanced_view = isset( $scporder_options['show_advanced_view'] ) ? $scporder_options['show_advanced_view'] : '';
7
  ?>
8
  <style>
9
 
10
+ .epsilon-toggle {
11
+ position: relative;
12
+ display:inline-block;
13
+ user-select: none;
14
+ }
15
+
16
+ .epsilon-toggle__items {
17
+ box-sizing: border-box;
18
+ }
19
+
20
+ .epsilon-toggle__items > * {
21
+ box-sizing: inherit;
22
+ }
23
+
24
+ .epsilon-toggle__input[type=checkbox] {
25
+ border-radius: 2px;
26
+ border: 2px solid #6c7781;
27
+ margin-right: 12px;
28
+ transition: none;
29
+ height: 100%;
30
+ left: 0;
31
+ top: 0;
32
+ margin: 0;
33
+ padding: 0;
34
+ opacity: 0;
35
+ position: absolute;
36
+ width: 100%;
37
+ z-index: 1;
38
+ }
39
+
40
+ .epsilon-toggle__track {
41
+ background-color: #fff;
42
+ border: 2px solid #6c7781;
43
+ border-radius: 9px;
44
+ display: inline-block;
45
+ height: 18px;
46
+ width: 36px;
47
+ vertical-align: top;
48
+ transition: background .2s ease;
49
+ }
50
+
51
+ .epsilon-toggle__thumb {
52
+ background-color: #6c7781;
53
+ border: 5px solid #6c7781;
54
+ border-radius: 50%;
55
+ display: block;
56
+ height: 10px;
57
+ width: 10px;
58
+ position: absolute;
59
+ left: 4px;
60
+ top: 4px;
61
+ transition: transform .2s ease;
62
+ }
63
+
64
+ .epsilon-toggle__off {
65
+ position: absolute;
66
+ right: 6px;
67
+ top: 6px;
68
+ color: #6c7781;
69
+ fill: currentColor;
70
+ }
71
+
72
+ .epsilon-toggle__on {
73
+ position: absolute;
74
+ top: 6px;
75
+ left: 8px;
76
+ border: 1px solid #fff;
77
+ outline: 1px solid transparent;
78
+ outline-offset: -1px;
79
+ display: none;
80
+ }
81
+
82
+
83
+ .epsilon-toggle__input[type=checkbox]:checked + .epsilon-toggle__items .epsilon-toggle__track {
84
+ background-color: #11a0d2;
85
+ border: 9px solid transparent;
86
+ }
87
+
88
+ .epsilon-toggle__input[type=checkbox]:checked + .epsilon-toggle__items .epsilon-toggle__thumb {
89
+ background-color: #fff;
90
+ border-width: 0;
91
+ transform: translateX(18px);
92
+ }
93
+
94
+ .epsilon-toggle__input[type=checkbox]:checked + .epsilon-toggle__items .epsilon-toggle__off {
95
+ display: none;
96
+ }
97
+
98
+ .epsilon-toggle__input[type=checkbox]:checked + .epsilon-toggle__items .epsilon-toggle__on {
99
+ display: inline-block;
100
+ }
101
+
102
+ .scpo-reset-response {
103
+ margin-left:15px;
104
+ color:#0085ba;
105
+ }
106
+ </style>
107
  <div class="wrap">
108
+ <h2><?php _e( 'Simple Custom Post Order Settings', 'simple-custom-post-order' ); ?></h2>
109
+ <?php if ( isset( $_GET['msg'] ) ) : ?>
110
+ <div id="message" class="updated below-h2">
111
+ <?php if ( $_GET['msg'] == 'update' ) : ?>
112
+ <p><?php _e( 'Settings Updated.', 'simple-custom-post-order' ); ?></p>
113
+ <?php endif; ?>
114
+ </div>
115
+ <?php endif; ?>
116
+
117
+ <form method="post">
118
+
119
+ <?php
120
+ if ( function_exists( 'wp_nonce_field' ) ) {
121
+ wp_nonce_field( 'nonce_scporder' );}
122
+ ?>
123
+
124
+ <div id="scporder_select_objects">
125
+
126
+ <table class="form-table">
127
+ <tbody>
128
+ <tr valign="top">
129
+ <th scope="row"><?php _e( 'Check to Sort Post Types', 'simple-custom-post-order' ); ?></th>
130
+ <td>
131
+
132
+ <label>
133
+ <div class="epsilon-toggle">
134
+ <input id="scporder_allcheck_objects" class="epsilon-toggle__input" type="checkbox">
135
+ <div class="epsilon-toggle__items">
136
+ <span class="epsilon-toggle__track"></span>
137
+ <span class="epsilon-toggle__thumb"></span>
138
+ <svg class="epsilon-toggle__off" width="6" height="6" aria-hidden="true"
139
+ role="img" focusable="false" viewBox="0 0 6 6">
140
+ <path d="M3 1.5c.8 0 1.5.7 1.5 1.5S3.8 4.5 3 4.5 1.5 3.8 1.5 3 2.2 1.5 3 1.5M3 0C1.3 0 0 1.3 0 3s1.3 3 3 3 3-1.3 3-3-1.3-3-3-3z"></path>
141
+ </svg>
142
+ <svg class="epsilon-toggle__on" width="2" height="6" aria-hidden="true"
143
+ role="img" focusable="false" viewBox="0 0 2 6">
144
+ <path d="M0 0h2v6H0z"></path>
145
+ </svg>
146
+ </div>
147
+ </div>
148
+ &nbsp;<?php _e( 'Check All', 'simple-custom-post-order' ); ?></label><br>
149
+ <?php
150
+ $post_types_args = apply_filters(
151
+ 'scpo_post_types_args',
152
+ array(
153
+ 'show_ui' => true,
154
+ 'show_in_menu' => true,
155
+ ),
156
+ $scporder_options
157
+ );
158
+
159
+ $post_types = get_post_types( $post_types_args, 'objects' );
160
+
161
+ foreach ( $post_types as $post_type ) {
162
+ if ( $post_type->name == 'attachment' ) {
163
+ continue;
164
+ }
165
+ ?>
166
+ <label>
167
+ <div class="epsilon-toggle">
168
+ <input class="epsilon-toggle__input" type="checkbox"
169
+ name="objects[]" value="<?php echo $post_type->name; ?>"
170
+ <?php
171
+ if ( isset( $scporder_objects ) && is_array( $scporder_objects ) ) {
172
+ if ( in_array( $post_type->name, $scporder_objects ) ) {
173
+ echo 'checked="checked"';
174
+ }
175
+ }
176
+ ?>
177
+ >
178
+ <div class="epsilon-toggle__items">
179
+ <span class="epsilon-toggle__track"></span>
180
+ <span class="epsilon-toggle__thumb"></span>
181
+ <svg class="epsilon-toggle__off" width="6" height="6" aria-hidden="true"
182
+ role="img" focusable="false" viewBox="0 0 6 6">
183
+ <path d="M3 1.5c.8 0 1.5.7 1.5 1.5S3.8 4.5 3 4.5 1.5 3.8 1.5 3 2.2 1.5 3 1.5M3 0C1.3 0 0 1.3 0 3s1.3 3 3 3 3-1.3 3-3-1.3-3-3-3z"></path>
184
+ </svg>
185
+ <svg class="epsilon-toggle__on" width="2" height="6" aria-hidden="true"
186
+ role="img" focusable="false" viewBox="0 0 2 6">
187
+ <path d="M0 0h2v6H0z"></path>
188
+ </svg>
189
+ </div>
190
+ </div>
191
+ &nbsp;<?php echo $post_type->label; ?></label><br>
192
+ <?php
193
+ }
194
+ ?>
195
+ </td>
196
+ </tr>
197
+ </tbody>
198
+ </table>
199
+
200
+ </div>
201
+
202
+
203
+ <div id="scporder_select_tags">
204
+ <table class="form-table">
205
+ <tbody>
206
+ <tr valign="top">
207
+ <th scope="row"><?php _e( 'Check to Sort Taxonomies', 'simple-custom-post-order' ); ?></th>
208
+ <td>
209
+
210
+ <label>
211
+ <div class="epsilon-toggle">
212
+ <input id="scporder_allcheck_tags" class="epsilon-toggle__input" type="checkbox">
213
+ <div class="epsilon-toggle__items">
214
+ <span class="epsilon-toggle__track"></span>
215
+ <span class="epsilon-toggle__thumb"></span>
216
+ <svg class="epsilon-toggle__off" width="6" height="6" aria-hidden="true"
217
+ role="img" focusable="false" viewBox="0 0 6 6">
218
+ <path d="M3 1.5c.8 0 1.5.7 1.5 1.5S3.8 4.5 3 4.5 1.5 3.8 1.5 3 2.2 1.5 3 1.5M3 0C1.3 0 0 1.3 0 3s1.3 3 3 3 3-1.3 3-3-1.3-3-3-3z"></path>
219
+ </svg>
220
+ <svg class="epsilon-toggle__on" width="2" height="6" aria-hidden="true"
221
+ role="img" focusable="false" viewBox="0 0 2 6">
222
+ <path d="M0 0h2v6H0z"></path>
223
+ </svg>
224
+ </div>
225
+ </div>
226
+ &nbsp;<?php _e( 'Check All', 'simple-custom-post-order' ); ?></label><br>
227
+
228
+ <?php
229
+ $taxonomies = get_taxonomies(
230
+ array(
231
+ 'show_ui' => true,
232
+ ),
233
+ 'objects'
234
+ );
235
+
236
+ foreach ( $taxonomies as $taxonomy ) {
237
+ if ( $taxonomy->name == 'post_format' ) {
238
+ continue;
239
+ }
240
+ ?>
241
+ <label>
242
+ <div class="epsilon-toggle">
243
+ <input class="epsilon-toggle__input" type="checkbox"
244
+ name="tags[]" value="<?php echo $taxonomy->name; ?>"
245
+ <?php
246
+ if ( isset( $scporder_tags ) && is_array( $scporder_tags ) ) {
247
+ if ( in_array( $taxonomy->name, $scporder_tags ) ) {
248
+ echo 'checked="checked"';
249
+ }
250
+ }
251
+ ?>
252
+ >
253
+ <div class="epsilon-toggle__items">
254
+ <span class="epsilon-toggle__track"></span>
255
+ <span class="epsilon-toggle__thumb"></span>
256
+ <svg class="epsilon-toggle__off" width="6" height="6" aria-hidden="true"
257
+ role="img" focusable="false" viewBox="0 0 6 6">
258
+ <path d="M3 1.5c.8 0 1.5.7 1.5 1.5S3.8 4.5 3 4.5 1.5 3.8 1.5 3 2.2 1.5 3 1.5M3 0C1.3 0 0 1.3 0 3s1.3 3 3 3 3-1.3 3-3-1.3-3-3-3z"></path>
259
+ </svg>
260
+ <svg class="epsilon-toggle__on" width="2" height="6" aria-hidden="true"
261
+ role="img" focusable="false" viewBox="0 0 2 6">
262
+ <path d="M0 0h2v6H0z"></path>
263
+ </svg>
264
+ </div>
265
+ </div>
266
+ &nbsp;<?php echo $taxonomy->label; ?></label><br>
267
+ <?php
268
+ }
269
+ ?>
270
+ </td>
271
+ </tr>
272
+ </tbody>
273
+ </table>
274
+
275
+ </div>
276
+ <div id="scporder_advanved_view">
277
+ <table class="form-table">
278
+ <tbody>
279
+ <tr valign="top">
280
+ <th scope="row"><?php _e( 'Check to see advanced view of Post Types ', 'simple-custom-post-order' ); ?></th>
281
+ <td>
282
+ <label>
283
+ <div class="epsilon-toggle">
284
+ <input class="epsilon-toggle__input" type="checkbox"
285
+ name="show_advanced_view" value="1" <?php checked( '1', $advanced_view, 'checked="checked"' ); ?>>
286
+ <div class="epsilon-toggle__items">
287
+ <span class="epsilon-toggle__track"></span>
288
+ <span class="epsilon-toggle__thumb"></span>
289
+ <svg class="epsilon-toggle__off" width="6" height="6" aria-hidden="true"
290
+ role="img" focusable="false" viewBox="0 0 6 6">
291
+ <path d="M3 1.5c.8 0 1.5.7 1.5 1.5S3.8 4.5 3 4.5 1.5 3.8 1.5 3 2.2 1.5 3 1.5M3 0C1.3 0 0 1.3 0 3s1.3 3 3 3 3-1.3 3-3-1.3-3-3-3z"></path>
292
+ </svg>
293
+ <svg class="epsilon-toggle__on" width="2" height="6" aria-hidden="true"
294
+ role="img" focusable="false" viewBox="0 0 2 6">
295
+ <path d="M0 0h2v6H0z"></path>
296
+ </svg>
297
+ </div>
298
+ </div>
299
+ <?php echo __( 'Show advanced view of Post Types', 'simple-custom-post-order' ); ?>
300
+ </label><br>
301
+ <p class="description"><?php _e( 'NOTICE: This is for advanced users only.', 'simple-custom-post-order' ); ?></p>
302
+ <!--@todo : @giucu please look into below description. -->
303
+ <p class="description"><?php _e( 'Check to include other custom post types. You will need to update your settings after enabling this option.', 'simple-custom-post-order' ); ?></p>
304
+
305
+ </td>
306
+ </tr>
307
+ </tbody>
308
+ </table>
309
+ </div>
310
+
311
+ <p class="submit">
312
+ <input type="submit" class="button-primary" name="scporder_submit" value="<?php _e( 'Update', 'simple-custom-post-order' ); ?>">
313
+ </p>
314
+
315
+ </form>
316
+ <div class="scpo-reset-order">
317
+ <h1>Want to reset the order of the posts?</h1>
318
+ <div id="scpo_reset_select_objects">
319
+ <table class="form-table">
320
+ <tbody>
321
+ <tr valign="top">
322
+ <th scope="row"><?php _e( 'Check to reset order of Post Types', 'simple-custom-post-order' ); ?></th>
323
+ <td>
324
+ <?php
325
+ foreach ( $post_types as $post_type ) {
326
+ if ( $post_type->name == 'attachment' ) {
327
+ continue;
328
+ }
329
+ ?>
330
+ <label>
331
+ <div class="epsilon-toggle">
332
+ <input class="epsilon-toggle__input" type="checkbox"
333
+ name="<?php echo $post_type->name; ?>" value="">
334
+ <div class="epsilon-toggle__items">
335
+ <span class="epsilon-toggle__track"></span>
336
+ <span class="epsilon-toggle__thumb"></span>
337
+ <svg class="epsilon-toggle__off" width="6" height="6" aria-hidden="true"
338
+ role="img" focusable="false" viewBox="0 0 6 6">
339
+ <path d="M3 1.5c.8 0 1.5.7 1.5 1.5S3.8 4.5 3 4.5 1.5 3.8 1.5 3 2.2 1.5 3 1.5M3 0C1.3 0 0 1.3 0 3s1.3 3 3 3 3-1.3 3-3-1.3-3-3-3z"></path>
340
+ </svg>
341
+ <svg class="epsilon-toggle__on" width="2" height="6" aria-hidden="true"
342
+ role="img" focusable="false" viewBox="0 0 2 6">
343
+ <path d="M0 0h2v6H0z"></path>
344
+ </svg>
345
+ </div>
346
+ </div>
347
+ &nbsp;<?php echo $post_type->label; ?></label><br>
348
+ <?php
349
+ }
350
+ ?>
351
+ </td>
352
+ </tr>
353
+ </tbody>
354
+ </table>
355
+
356
+ </div>
357
+ <div>
358
+ <a id="reset-scp-order" class="button button-primary" href="#">Reset order</a>
359
+ <span class="scpo-reset-response"></span>
360
+ </div>
361
+ </div>
362
+
363
+ <h3>Like this simple plugin?</h3>
364
+ <p>Make sure to <a href="https://wordpress.org/support/plugin/simple-custom-post-order/reviews/?filter=5"><strong>rate it</strong></a> and visit us at <a href="https://colorlib.com/wp/"><strong>Colorlib.com</strong></a></p>
365
 
366
 
367
  </div>
368
 
369
  <script>
370
+ (function ($) {
371
+
372
+ $("#scporder_allcheck_objects").on('click', function () {
373
+ var items = $("#scporder_select_objects input");
374
+ if ($(this).is(':checked'))
375
+ $(items).prop('checked', true);
376
+ else
377
+ $(items).prop('checked', false);
378
+ });
379
+
380
+ $("#scporder_allcheck_tags").on('click', function () {
381
+ var items = $("#scporder_select_tags input");
382
+ if ($(this).is(':checked'))
383
+ $(items).prop('checked', true);
384
+ else
385
+ $(items).prop('checked', false);
386
+ });
387
+
388
+ // Reset order function
389
+ $('#reset-scp-order').click(function (e) {
390
+
391
+ e.preventDefault();
392
+ var btn = $(this),
393
+ item_input = $(this).parents('.scpo-reset-order').find('input:checked'),
394
+ items = [],
395
+ data = {
396
+ action: 'scpo_reset_order',
397
+ scpo_security: '<?php echo wp_create_nonce( 'scpo-reset-order' ); ?>'
398
+ };
399
+
400
+ if (item_input.length > 0) {
401
+ item_input.each(function (i, item) {
402
+ items.push(item.name);
403
+ });
404
+
405
+ data['items'] = items;
406
+
407
+ $.post("<?php echo admin_url( 'admin-ajax.php' ); ?>", data, function (response) {
408
+ if (response) {
409
+ btn.next('.scpo-reset-response').text(response);
410
+ setTimeout(function(){
411
+ location.reload(true);
412
+ },1500);
413
+ }
414
+ });
415
+ }
416
+ });
417
+
418
+ })(jQuery)
419
  </script>
simple-custom-post-order.php CHANGED
@@ -1,266 +1,271 @@
1
  <?php
2
  /**
3
- * Plugin Name: Simple Custom Post Order
4
- * Plugin URI: https://wordpress.org/plugins-wp/simple-custom-post-order/
5
- * Description: Order Items (Posts, Pages, and Custom Post Types) using a Drag and Drop Sortable JavaScript.
6
- * Version: 2.5.4
7
- * Author: Colorlib
8
- * Author URI: https://colorlib.com/
9
- * Tested up to: 5.7
10
- * Requires: 4.6 or higher
11
- * License: GPLv3 or later
12
- * License URI: http://www.gnu.org/licenses/gpl-3.0.html
13
- * Requires PHP: 5.6
14
- * Text Domain: simple-custom-post-order
15
- * Domain Path: /languages
16
- *
17
- * Copyright 2013-2017 Sameer Humagain im@hsameer.com.np
18
- * Copyright 2017-2019 Colorlib support@colorlib.com
19
- *
20
- * SVN commit with ownership change: https://plugins.trac.wordpress.org/changeset/1590135/simple-custom-post-order
21
- *
22
- * This program is free software; you can redistribute it and/or modify
23
- * it under the terms of the GNU General Public License, version 3, as
24
- * published by the Free Software Foundation.
25
- *
26
- * This program is distributed in the hope that it will be useful,
27
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29
- * GNU General Public License for more details.
30
- *
31
- * You should have received a copy of the GNU General Public License
32
- * along with this program; if not, write to the Free Software
33
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34
- */
35
-
36
-
37
- define('SCPORDER_URL', plugins_url('', __FILE__));
38
- define('SCPORDER_DIR', plugin_dir_path(__FILE__));
39
- define('SCPORDER_VERSION', '2.5.4');
40
 
41
  $scporder = new SCPO_Engine();
42
 
43
  class SCPO_Engine {
44
 
45
- function __construct() {
46
- if (!get_option('scporder_install'))
47
- $this->scporder_install();
 
48
 
49
- $this->load_dependencies();
50
 
51
- add_action('admin_menu', array($this, 'admin_menu'));
52
 
53
- add_action('admin_init', array( $this, 'refresh' ) );
54
 
 
 
55
 
56
- add_action('admin_init', array($this, 'update_options'));
57
- add_action('admin_init', array($this, 'load_script_css'));
58
 
59
- add_action('wp_ajax_update-menu-order', array($this, 'update_menu_order'));
60
- add_action('wp_ajax_update-menu-order-tags', array($this, 'update_menu_order_tags'));
61
 
62
- add_action('pre_get_posts', array($this, 'scporder_pre_get_posts'));
 
 
 
63
 
64
- add_filter('get_previous_post_where', array($this, 'scporder_previous_post_where'));
65
- add_filter('get_previous_post_sort', array($this, 'scporder_previous_post_sort'));
66
- add_filter('get_next_post_where', array($this, 'scporder_next_post_where'));
67
- add_filter('get_next_post_sort', array($this, 'scporder_next_post_sort'));
68
 
69
- add_filter('get_terms_orderby', array($this, 'scporder_get_terms_orderby'), 10, 3);
70
- add_filter('wp_get_object_terms', array($this, 'scporder_get_object_terms'), 10, 3);
71
- add_filter('get_terms', array($this, 'scporder_get_object_terms'), 10, 3);
72
-
73
- add_action( 'admin_notices', array( $this, 'scporder_notice_not_checked' ) );
74
- add_action( 'wp_ajax_scporder_dismiss_notices', array( $this, 'dismiss_notices' ) );
75
 
76
- add_action( 'plugins_loaded', array( $this, 'load_scpo_textdomain' ) );
77
 
78
- add_filter('scpo_post_types_args',array($this,'scpo_filter_post_types'),10,2);
79
 
80
- add_action('wp_ajax_scpo_reset_order', array($this, 'scpo_ajax_reset_order'));
81
- }
82
 
83
- private function load_dependencies() {
84
- include SCPORDER_DIR . 'class-simple-review.php';
85
- }
86
 
87
- public function scpo_filter_post_types($args,$options){
88
 
89
- if(isset($options['show_advanced_view']) && '1' == $options['show_advanced_view'] ){
90
- unset($args['show_in_menu']);
91
- }
92
 
93
- return $args;
94
- }
95
 
96
- public function load_scpo_textdomain(){
97
 
98
- load_plugin_textdomain( 'simple-custom-post-order', false, basename( dirname( __FILE__ ) ) . '/languages/' );
99
- }
100
 
101
- public function dismiss_notices() {
102
 
103
- if ( ! check_admin_referer( 'scporder_dismiss_notice', 'scporder_nonce' ) ) {
104
- wp_die( 'nok' );
105
- }
106
 
107
- update_option( 'scporder_notice', '1' );
108
 
109
- wp_die( 'ok' );
110
 
111
- }
112
 
113
- public function scporder_notice_not_checked() {
114
 
115
- $settings = $this->get_scporder_options_objects();
116
- if ( ! empty( $settings ) ){
117
- return;
118
- }
119
 
120
- $screen = get_current_screen();
121
 
122
- if ( 'settings_page_scporder-settings' == $screen->id ) {
123
- return;
124
- }
125
 
126
- $dismessed = get_option( 'scporder_notice', false );
127
 
128
- if ( $dismessed ) {
129
- return;
130
- }
131
 
132
- ?>
133
- <div class="notice scpo-notice" id="scpo-notice">
134
- <img src="<?php echo esc_url( plugins_url( 'assets/logo.jpg', __FILE__ ) ); ?>" width="80">
135
 
136
- <h1><?php esc_html_e( 'Simple Custom Post Order', 'simple-custom-post-order' ); ?></h1>
137
 
138
- <p><?php esc_html_e( 'Thank you for installing our awesome plugin, in order to enable it you need to go to the settings page and select which custom post or taxonomy you want to order.', 'simple-custom-post-order' ); ?></p>
139
 
140
- <p><a href="<?php echo admin_url( 'options-general.php?page=scporder-settings' ) ?>" class="button button-primary button-hero"><?php esc_html_e( 'Get started !', 'simple-custom-post-order' ); ?></a></p>
141
- <button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'simple-custom-post-order' ); ?></span></button>
142
- </div>
143
 
144
- <style>
145
- .scpo-notice {
146
- background: #e9eff3;
147
- border: 10px solid #fff;
148
- color: #608299;
149
- padding: 30px;
150
- text-align: center;
151
- position: relative;
152
- }
153
- </style>
154
- <script>
155
- jQuery(document).ready(function(){
156
- jQuery( '#scpo-notice .notice-dismiss' ).click(function( evt ){
157
- evt.preventDefault();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
- var ajaxData = {
160
- 'action' : 'scporder_dismiss_notices',
161
- 'scporder_nonce' : '<?php echo wp_create_nonce( 'scporder_dismiss_notice' ) ?>'
162
- }
 
 
 
 
 
163
 
164
- jQuery.ajax({
165
- url: "<?php echo admin_url('admin-ajax.php'); ?>",
166
- method: "POST",
167
- data: ajaxData,
168
- dataType: "html"
169
- }).done(function(){
170
- jQuery("#scpo-notice").hide();
171
- });
172
 
173
- });
174
- })
175
- </script>
176
- <?php
177
- }
178
 
179
- public function scporder_install() {
180
- global $wpdb;
181
- $result = $wpdb->query("DESCRIBE $wpdb->terms `term_order`");
182
- if (!$result) {
183
- $query = "ALTER TABLE $wpdb->terms ADD `term_order` INT( 4 ) NULL DEFAULT '0'";
184
- $result = $wpdb->query($query);
185
- }
186
- update_option('scporder_install', 1);
187
- }
188
-
189
- public function admin_menu() {
190
- add_options_page(__('SCPOrder', 'simple-custom-post-order'), __('SCPOrder', 'simple-custom-post-order'), 'manage_options', 'scporder-settings', array($this, 'admin_page'));
191
- }
192
-
193
- public function admin_page() {
194
- require SCPORDER_DIR . 'settings.php';
195
- }
196
-
197
- public function _check_load_script_css() {
198
-
199
- $active = false;
200
-
201
- $objects = $this->get_scporder_options_objects();
202
- $tags = $this->get_scporder_options_tags();
203
-
204
- if (empty($objects) && empty($tags))
205
- return false;
206
-
207
- if (isset($_GET['orderby']) || strstr($_SERVER['REQUEST_URI'], 'action=edit') || strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php'))
208
- return false;
209
-
210
- if (!empty($objects)) {
211
- if (isset($_GET['post_type']) && !isset($_GET['taxonomy']) && in_array($_GET['post_type'], $objects)) { // if page or custom post types
212
- $active = true;
213
- }
214
- if (!isset($_GET['post_type']) && strstr($_SERVER['REQUEST_URI'], 'wp-admin/edit.php') && in_array('post', $objects)) { // if post
215
- $active = true;
216
- }
217
- }
218
-
219
- if (!empty($tags)) {
220
- if (isset($_GET['taxonomy']) && in_array($_GET['taxonomy'], $tags)) {
221
- $active = true;
222
- }
223
- }
224
-
225
- return $active;
226
- }
227
-
228
- public function load_script_css() {
229
- if ($this->_check_load_script_css()) {
230
- wp_enqueue_script('jquery');
231
- wp_enqueue_script('jquery-ui-sortable');
232
- wp_enqueue_script('scporderjs', SCPORDER_URL . '/assets/scporder.min.js', array('jquery'), SCPORDER_VERSION, true);
233
- add_action( 'admin_print_styles', array( $this, 'print_scpo_style' ) );
234
 
235
- }
236
- }
237
 
238
- public function refresh() {
 
239
 
 
 
 
240
 
241
- if ( scporder_doing_ajax() ) {
242
- return;
243
- }
244
 
245
- global $wpdb;
246
- $objects = $this->get_scporder_options_objects();
247
- $tags = $this->get_scporder_options_tags();
 
 
 
 
 
248
 
249
- if (!empty($objects)) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
 
251
- foreach ($objects as $object) {
252
- $result = $wpdb->get_results("
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
254
  FROM $wpdb->posts
255
  WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
256
- ");
 
257
 
258
- if ($result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max)
259
- continue;
 
260
 
261
- // Here's the optimization
262
- $wpdb->query("SET @row_number = 0;");
263
- $wpdb->query("UPDATE $wpdb->posts as pt JOIN (
 
264
 
265
  SELECT ID, (@row_number:=@row_number + 1) AS `rank`
266
  FROM $wpdb->posts
@@ -268,404 +273,436 @@ class SCPO_Engine {
268
  ORDER BY menu_order ASC
269
  ) as pt2
270
  ON pt.id = pt2.id
271
- SET pt.menu_order = pt2.`rank`;");
 
272
 
 
 
273
 
274
- }
275
- }
276
-
277
- if (!empty($tags)) {
278
- foreach ($tags as $taxonomy) {
279
- $result = $wpdb->get_results("
280
  SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min
281
  FROM $wpdb->terms AS terms
282
  INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
283
  WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
284
- ");
285
- if ($result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max)
286
- continue;
287
-
288
- $results = $wpdb->get_results("
 
 
 
289
  SELECT terms.term_id
290
  FROM $wpdb->terms AS terms
291
  INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
292
  WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
293
  ORDER BY term_order ASC
294
- ");
295
- foreach ($results as $key => $result) {
296
- $wpdb->update($wpdb->terms, array('term_order' => $key + 1), array('term_id' => $result->term_id));
297
- }
298
- }
299
- }
300
- }
301
-
302
- public function update_menu_order() {
303
- global $wpdb;
304
 
305
- parse_str($_POST['order'], $data);
 
306
 
307
- if (!is_array($data))
308
- return false;
309
 
310
- $id_arr = array();
311
- foreach ($data as $key => $values) {
312
- foreach ($values as $position => $id) {
313
- $id_arr[] = $id;
314
- }
315
- }
316
 
317
- $menu_order_arr = array();
318
- foreach ($id_arr as $key => $id) {
319
- $results = $wpdb->get_results("SELECT menu_order FROM $wpdb->posts WHERE ID = " . intval($id));
320
- foreach ($results as $result) {
321
- $menu_order_arr[] = $result->menu_order;
322
- }
323
- }
324
 
325
- sort($menu_order_arr);
 
 
 
 
 
 
326
 
327
- foreach ($data as $key => $values) {
328
- foreach ($values as $position => $id) {
329
- $wpdb->update($wpdb->posts, array('menu_order' => $menu_order_arr[$position]), array('ID' => intval($id)));
330
- }
331
- }
332
 
333
- do_action('scp_update_menu_order');
 
 
 
 
334
 
335
- }
336
 
337
- public function update_menu_order_tags() {
338
- global $wpdb;
339
 
340
- parse_str($_POST['order'], $data);
 
341
 
342
- if (!is_array($data))
343
- return false;
344
 
345
- $id_arr = array();
346
- foreach ($data as $key => $values) {
347
- foreach ($values as $position => $id) {
348
- $id_arr[] = $id;
349
- }
350
- }
351
 
352
- $menu_order_arr = array();
353
- foreach ($id_arr as $key => $id) {
354
- $results = $wpdb->get_results("SELECT term_order FROM $wpdb->terms WHERE term_id = " . intval($id));
355
- foreach ($results as $result) {
356
- $menu_order_arr[] = $result->term_order;
357
- }
358
- }
359
- sort($menu_order_arr);
360
 
361
- foreach ($data as $key => $values) {
362
- foreach ($values as $position => $id) {
363
- $wpdb->update($wpdb->terms, array('term_order' => $menu_order_arr[$position]), array('term_id' => intval($id)));
364
- }
365
- }
 
 
 
366
 
367
- do_action('scp_update_menu_order_tags');
 
 
 
 
368
 
369
- }
370
 
371
- public function update_options() {
372
- global $wpdb;
373
 
374
- if (!isset($_POST['scporder_submit']))
375
- return false;
376
 
377
- check_admin_referer('nonce_scporder');
 
 
378
 
379
- $input_options = array();
380
- $input_options['objects'] = isset($_POST['objects']) ? $_POST['objects'] : '';
381
- $input_options['tags'] = isset($_POST['tags']) ? $_POST['tags'] : '';
382
- $input_options['show_advanced_view'] = isset($_POST['show_advanced_view']) ? $_POST['show_advanced_view'] : '';
383
 
 
 
 
 
384
 
385
- update_option('scporder_options', $input_options);
386
 
387
- $objects = $this->get_scporder_options_objects();
388
- $tags = $this->get_scporder_options_tags();
389
 
390
- if (!empty($objects)) {
391
- foreach ($objects as $object) {
392
- $result = $wpdb->get_results("
 
393
  SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
394
  FROM $wpdb->posts
395
  WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
396
- ");
397
- if ($result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max)
398
- continue;
399
-
400
- if ($object == 'page') {
401
- $results = $wpdb->get_results("
 
 
 
402
  SELECT ID
403
  FROM $wpdb->posts
404
  WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
405
  ORDER BY post_title ASC
406
- ");
407
- } else {
408
- $results = $wpdb->get_results("
 
 
409
  SELECT ID
410
  FROM $wpdb->posts
411
  WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
412
  ORDER BY post_date DESC
413
- ");
414
- }
415
- foreach ($results as $key => $result) {
416
- $wpdb->update($wpdb->posts, array('menu_order' => $key + 1), array('ID' => $result->ID));
417
- }
418
- }
419
- }
420
-
421
- if (!empty($tags)) {
422
- foreach ($tags as $taxonomy) {
423
- $result = $wpdb->get_results("
 
 
424
  SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min
425
  FROM $wpdb->terms AS terms
426
  INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
427
  WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
428
- ");
429
- if ($result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max)
430
- continue;
431
-
432
- $results = $wpdb->get_results("
 
 
 
433
  SELECT terms.term_id
434
  FROM $wpdb->terms AS terms
435
  INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
436
  WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
437
  ORDER BY name ASC
438
- ");
439
- foreach ($results as $key => $result) {
440
- $wpdb->update($wpdb->terms, array('term_order' => $key + 1), array('term_id' => $result->term_id));
441
- }
442
- }
443
- }
444
-
445
- wp_redirect('admin.php?page=scporder-settings&msg=update');
446
- }
447
-
448
- public function scporder_previous_post_where($where) {
449
- global $post;
450
-
451
- $objects = $this->get_scporder_options_objects();
452
- if (empty($objects))
453
- return $where;
454
-
455
- if (isset($post->post_type) && in_array($post->post_type, $objects)) {
456
- $where = preg_replace("/p.post_date < \'[0-9\-\s\:]+\'/i", "p.menu_order > '" . $post->menu_order . "'", $where);
457
- }
458
- return $where;
459
- }
460
-
461
- public function scporder_previous_post_sort($orderby) {
462
- global $post;
463
-
464
- $objects = $this->get_scporder_options_objects();
465
- if (empty($objects))
466
- return $orderby;
467
-
468
- if (isset($post->post_type) && in_array($post->post_type, $objects)) {
469
- $orderby = 'ORDER BY p.menu_order ASC LIMIT 1';
470
- }
471
- return $orderby;
472
- }
473
-
474
- public function scporder_next_post_where($where) {
475
- global $post;
476
-
477
- $objects = $this->get_scporder_options_objects();
478
- if (empty($objects))
479
- return $where;
480
-
481
- if (isset($post->post_type) && in_array($post->post_type, $objects)) {
482
- $where = preg_replace("/p.post_date > \'[0-9\-\s\:]+\'/i", "p.menu_order < '" . $post->menu_order . "'", $where);
483
- }
484
- return $where;
485
- }
486
-
487
- public function scporder_next_post_sort($orderby) {
488
- global $post;
489
-
490
- $objects = $this->get_scporder_options_objects();
491
- if (empty($objects))
492
- return $orderby;
493
-
494
- if (isset($post->post_type) && in_array($post->post_type, $objects)) {
495
- $orderby = 'ORDER BY p.menu_order DESC LIMIT 1';
496
- }
497
- return $orderby;
498
- }
499
-
500
- public function scporder_pre_get_posts($wp_query) {
501
- $objects = $this->get_scporder_options_objects();
502
-
503
-
504
- if (empty($objects))
505
- return false;
506
- if (is_admin() && ! wp_doing_ajax()) {
507
-
508
- if (isset($wp_query->query['post_type']) && !isset($_GET['orderby'])) {
509
- if (in_array($wp_query->query['post_type'], $objects)) {
510
- if (!$wp_query->get('orderby'))
511
- $wp_query->set('orderby', 'menu_order');
512
- if (!$wp_query->get('order'))
513
- $wp_query->set('order', 'ASC');
514
- }
515
- }
516
- } else {
517
-
518
- $active = false;
519
-
520
- if (isset($wp_query->query['post_type'])) {
521
- if (!is_array($wp_query->query['post_type'])) {
522
- if (in_array($wp_query->query['post_type'], $objects)) {
523
- $active = true;
524
- }
525
- }
526
- } else {
527
- if (in_array('post', $objects)) {
528
- $active = true;
529
- }
530
- }
531
-
532
- if (!$active)
533
- return false;
534
-
535
- if (isset($wp_query->query['suppress_filters'])) {
536
- if ($wp_query->get('orderby') == 'date')
537
- $wp_query->set('orderby', 'menu_order');
538
- if ($wp_query->get('order') == 'DESC')
539
- $wp_query->set('order', 'ASC');
540
- } else {
541
- if (!$wp_query->get('orderby'))
542
- $wp_query->set('orderby', 'menu_order');
543
- if (!$wp_query->get('order'))
544
- $wp_query->set('order', 'ASC');
545
- }
546
- }
547
- }
548
-
549
- public function scporder_get_terms_orderby($orderby, $args) {
550
-
551
- if (is_admin() && ! wp_doing_ajax())
552
- return $orderby;
553
-
554
- $tags = $this->get_scporder_options_tags();
555
-
556
- if (!isset($args['taxonomy']))
557
- return $orderby;
558
-
559
-
560
- if(is_array($args['taxonomy'])){
561
- if(isset($args['taxonomy'][0])){
562
- $taxonomy = $args['taxonomy'][0];
563
- } else {
564
- $taxonomy = false;
565
- }
566
-
567
- } else {
568
- $taxonomy = $args['taxonomy'];
569
- }
570
-
571
-
572
- if (!in_array($taxonomy, $tags))
573
- return $orderby;
574
-
575
- $orderby = 't.term_order';
576
- return $orderby;
577
- }
578
-
579
- public function scporder_get_object_terms($terms) {
580
- $tags = $this->get_scporder_options_tags();
581
-
582
-
583
- if (is_admin() && ! wp_doing_ajax() && isset($_GET['orderby']))
584
- return $terms;
585
-
586
- foreach ($terms as $key => $term) {
587
- if (is_object($term) && isset($term->taxonomy)) {
588
- $taxonomy = $term->taxonomy;
589
- if (!in_array($taxonomy, $tags))
590
- return $terms;
591
- } else {
592
- return $terms;
593
- }
594
- }
595
-
596
- usort($terms, array($this, 'taxcmp'));
597
- return $terms;
598
- }
599
-
600
- public function taxcmp($a, $b) {
601
- if ($a->term_order == $b->term_order)
602
- return 0;
603
- return ( $a->term_order < $b->term_order ) ? -1 : 1;
604
- }
605
-
606
- public function get_scporder_options_objects() {
607
- $scporder_options = get_option('scporder_options') ? get_option('scporder_options') : array();
608
- $objects = isset($scporder_options['objects']) && is_array($scporder_options['objects']) ? $scporder_options['objects'] : array();
609
- return $objects;
610
- }
611
-
612
- public function get_scporder_options_tags() {
613
- $scporder_options = get_option('scporder_options') ? get_option('scporder_options') : array();
614
- $tags = isset($scporder_options['tags']) && is_array($scporder_options['tags']) ? $scporder_options['tags'] : array();
615
- return $tags;
616
- }
617
-
618
-
619
- /**
620
- * SCPO reset order for post types/taxonomies
621
- */
622
- public function scpo_ajax_reset_order() {
623
-
624
- global $wpdb;
625
- if ('scpo_reset_order' == $_POST['action']) {
626
- check_ajax_referer('scpo-reset-order', 'scpo_security');
627
- $items = $_POST['items'];
628
 
629
- $count = 0;
630
- $in_list = "(";
631
- foreach ($items as $item) {
632
 
633
- if ($count != 0) {
634
- $in_list .= ',';
635
- }
636
- $in_list .= '\'' . $item . '\'';
637
- $count++;
638
- }
639
- $in_list .= ")";
640
 
641
- $prep_posts_query = "UPDATE $wpdb->posts SET `menu_order` = 0 WHERE `post_type` IN $in_list";
 
 
 
642
 
643
- $result = $wpdb->query($prep_posts_query);
 
 
 
 
644
 
645
- $scpo_options = get_option( 'scporder_options' );
 
646
 
647
- if ( !false == $scpo_options ) {
 
 
 
648
 
649
- $scpo_options['objects'] = array_diff( $scpo_options['objects'], $items );
650
- update_option( 'scporder_options', $scpo_options );
651
- }
 
 
652
 
653
- if ($result) {
654
- echo 'Items have been reset';
655
- } else {
656
- echo false;
657
- }
658
 
659
- wp_die();
660
- }
661
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662
 
663
  /**
664
  * Print inline admin style
665
  *
666
  * @since 2.5.4
667
  */
668
- public function print_scpo_style(){
669
  ?>
670
  <style>
671
  .ui-sortable tr:hover {
@@ -686,47 +723,47 @@ class SCPO_Engine {
686
 
687
  }
688
 
689
- function scporder_doing_ajax(){
690
 
691
- if ( function_exists( 'wp_doing_ajax' ) ) {
692
- return wp_doing_ajax();
693
- }
694
 
695
- if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
696
- return true;
697
- }
698
 
699
- return false;
700
 
701
  }
702
 
703
  /**
704
  * SCP Order Uninstall hook
705
  */
706
- register_uninstall_hook(__FILE__, 'scporder_uninstall');
707
 
708
  function scporder_uninstall() {
709
- global $wpdb;
710
- if (function_exists('is_multisite') && is_multisite()) {
711
- $curr_blog = $wpdb->blogid;
712
- $blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
713
- foreach ($blogids as $blog_id) {
714
- switch_to_blog($blog_id);
715
- scporder_uninstall_db();
716
- }
717
- switch_to_blog($curr_blog);
718
- } else {
719
- scporder_uninstall_db();
720
- }
721
  }
722
 
723
  function scporder_uninstall_db() {
724
- global $wpdb;
725
- $result = $wpdb->query("DESCRIBE $wpdb->terms `term_order`");
726
- if ($result) {
727
- $query = "ALTER TABLE $wpdb->terms DROP `term_order`";
728
- $result = $wpdb->query($query);
729
- }
730
- delete_option('scporder_install');
731
  }
732
 
1
  <?php
2
  /**
3
+ * Plugin Name: Simple Custom Post Order
4
+ * Plugin URI: https://wordpress.org/plugins-wp/simple-custom-post-order/
5
+ * Description: Order Items (Posts, Pages, and Custom Post Types) using a Drag and Drop Sortable JavaScript.
6
+ * Version: 2.5.5
7
+ * Author: Colorlib
8
+ * Author URI: https://colorlib.com/
9
+ * Tested up to: 5.6
10
+ * Requires: 4.6 or higher
11
+ * License: GPLv3 or later
12
+ * License URI: http://www.gnu.org/licenses/gpl-3.0.html
13
+ * Requires PHP: 5.6
14
+ * Text Domain: simple-custom-post-order
15
+ * Domain Path: /languages
16
+ *
17
+ * Copyright 2013-2017 Sameer Humagain im@hsameer.com.np
18
+ * Copyright 2017-2019 Colorlib support@colorlib.com
19
+ *
20
+ * SVN commit with ownership change: https://plugins.trac.wordpress.org/changeset/1590135/simple-custom-post-order
21
+ *
22
+ * This program is free software; you can redistribute it and/or modify
23
+ * it under the terms of the GNU General Public License, version 3, as
24
+ * published by the Free Software Foundation.
25
+ *
26
+ * This program is distributed in the hope that it will be useful,
27
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29
+ * GNU General Public License for more details.
30
+ *
31
+ * You should have received a copy of the GNU General Public License
32
+ * along with this program; if not, write to the Free Software
33
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34
+ */
35
+
36
+
37
+ define( 'SCPORDER_URL', plugins_url( '', __FILE__ ) );
38
+ define( 'SCPORDER_DIR', plugin_dir_path( __FILE__ ) );
39
+ define( 'SCPORDER_VERSION', '2.5.5' );
40
 
41
  $scporder = new SCPO_Engine();
42
 
43
  class SCPO_Engine {
44
 
45
+ function __construct() {
46
+ if ( ! get_option( 'scporder_install' ) ) {
47
+ $this->scporder_install();
48
+ }
49
 
50
+ $this->load_dependencies();
51
 
52
+ add_action( 'admin_menu', array( $this, 'admin_menu' ) );
53
 
54
+ add_action( 'admin_init', array( $this, 'refresh' ) );
55
 
56
+ add_action( 'admin_init', array( $this, 'update_options' ) );
57
+ add_action( 'admin_init', array( $this, 'load_script_css' ) );
58
 
59
+ add_action( 'wp_ajax_update-menu-order', array( $this, 'update_menu_order' ) );
60
+ add_action( 'wp_ajax_update-menu-order-tags', array( $this, 'update_menu_order_tags' ) );
61
 
62
+ add_action( 'pre_get_posts', array( $this, 'scporder_pre_get_posts' ) );
 
63
 
64
+ add_filter( 'get_previous_post_where', array( $this, 'scporder_previous_post_where' ) );
65
+ add_filter( 'get_previous_post_sort', array( $this, 'scporder_previous_post_sort' ) );
66
+ add_filter( 'get_next_post_where', array( $this, 'scporder_next_post_where' ) );
67
+ add_filter( 'get_next_post_sort', array( $this, 'scporder_next_post_sort' ) );
68
 
69
+ add_filter( 'get_terms_orderby', array( $this, 'scporder_get_terms_orderby' ), 10, 3 );
70
+ add_filter( 'wp_get_object_terms', array( $this, 'scporder_get_object_terms' ), 10, 3 );
71
+ add_filter( 'get_terms', array( $this, 'scporder_get_object_terms' ), 10, 3 );
 
72
 
73
+ add_action( 'admin_notices', array( $this, 'scporder_notice_not_checked' ) );
74
+ add_action( 'wp_ajax_scporder_dismiss_notices', array( $this, 'dismiss_notices' ) );
 
 
 
 
75
 
76
+ add_action( 'plugins_loaded', array( $this, 'load_scpo_textdomain' ) );
77
 
78
+ add_filter( 'scpo_post_types_args', array( $this, 'scpo_filter_post_types' ), 10, 2 );
79
 
80
+ add_action( 'wp_ajax_scpo_reset_order', array( $this, 'scpo_ajax_reset_order' ) );
81
+ }
82
 
83
+ private function load_dependencies() {
84
+ include SCPORDER_DIR . 'class-simple-review.php';
85
+ }
86
 
87
+ public function scpo_filter_post_types( $args, $options ) {
88
 
89
+ if ( isset( $options['show_advanced_view'] ) && '1' == $options['show_advanced_view'] ) {
90
+ unset( $args['show_in_menu'] );
91
+ }
92
 
93
+ return $args;
94
+ }
95
 
96
+ public function load_scpo_textdomain() {
97
 
98
+ load_plugin_textdomain( 'simple-custom-post-order', false, basename( dirname( __FILE__ ) ) . '/languages/' );
99
+ }
100
 
101
+ public function dismiss_notices() {
102
 
103
+ if ( ! check_admin_referer( 'scporder_dismiss_notice', 'scporder_nonce' ) ) {
104
+ wp_die( 'nok' );
105
+ }
106
 
107
+ update_option( 'scporder_notice', '1' );
108
 
109
+ wp_die( 'ok' );
110
 
111
+ }
112
 
113
+ public function scporder_notice_not_checked() {
114
 
115
+ $settings = $this->get_scporder_options_objects();
116
+ if ( ! empty( $settings ) ) {
117
+ return;
118
+ }
119
 
120
+ $screen = get_current_screen();
121
 
122
+ if ( 'settings_page_scporder-settings' == $screen->id ) {
123
+ return;
124
+ }
125
 
126
+ $dismessed = get_option( 'scporder_notice', false );
127
 
128
+ if ( $dismessed ) {
129
+ return;
130
+ }
131
 
132
+ ?>
133
+ <div class="notice scpo-notice" id="scpo-notice">
134
+ <img src="<?php echo esc_url( plugins_url( 'assets/logo.jpg', __FILE__ ) ); ?>" width="80">
135
 
136
+ <h1><?php esc_html_e( 'Simple Custom Post Order', 'simple-custom-post-order' ); ?></h1>
137
 
138
+ <p><?php esc_html_e( 'Thank you for installing our awesome plugin, in order to enable it you need to go to the settings page and select which custom post or taxonomy you want to order.', 'simple-custom-post-order' ); ?></p>
139
 
140
+ <p><a href="<?php echo admin_url( 'options-general.php?page=scporder-settings' ); ?>" class="button button-primary button-hero"><?php esc_html_e( 'Get started !', 'simple-custom-post-order' ); ?></a></p>
141
+ <button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'simple-custom-post-order' ); ?></span></button>
142
+ </div>
143
 
144
+ <style>
145
+ .scpo-notice {
146
+ background: #e9eff3;
147
+ border: 10px solid #fff;
148
+ color: #608299;
149
+ padding: 30px;
150
+ text-align: center;
151
+ position: relative;
152
+ }
153
+ </style>
154
+ <script>
155
+ jQuery(document).ready(function(){
156
+ jQuery( '#scpo-notice .notice-dismiss' ).click(function( evt ){
157
+ evt.preventDefault();
158
+
159
+ var ajaxData = {
160
+ 'action' : 'scporder_dismiss_notices',
161
+ 'scporder_nonce' : '<?php echo wp_create_nonce( 'scporder_dismiss_notice' ); ?>'
162
+ }
163
+
164
+ jQuery.ajax({
165
+ url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
166
+ method: "POST",
167
+ data: ajaxData,
168
+ dataType: "html"
169
+ }).done(function(){
170
+ jQuery("#scpo-notice").hide();
171
+ });
172
+
173
+ });
174
+ })
175
+ </script>
176
+ <?php
177
+ }
178
 
179
+ public function scporder_install() {
180
+ global $wpdb;
181
+ $result = $wpdb->query( "DESCRIBE $wpdb->terms `term_order`" );
182
+ if ( ! $result ) {
183
+ $query = "ALTER TABLE $wpdb->terms ADD `term_order` INT( 4 ) NULL DEFAULT '0'";
184
+ $result = $wpdb->query( $query );
185
+ }
186
+ update_option( 'scporder_install', 1 );
187
+ }
188
 
189
+ public function admin_menu() {
190
+ add_options_page( __( 'SCPOrder', 'simple-custom-post-order' ), __( 'SCPOrder', 'simple-custom-post-order' ), 'manage_options', 'scporder-settings', array( $this, 'admin_page' ) );
191
+ }
 
 
 
 
 
192
 
193
+ public function admin_page() {
194
+ require SCPORDER_DIR . 'settings.php';
195
+ }
 
 
196
 
197
+ public function _check_load_script_css() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
+ $active = false;
 
200
 
201
+ $objects = $this->get_scporder_options_objects();
202
+ $tags = $this->get_scporder_options_tags();
203
 
204
+ if ( empty( $objects ) && empty( $tags ) ) {
205
+ return false;
206
+ }
207
 
208
+ if ( isset( $_GET['orderby'] ) || strstr( $_SERVER['REQUEST_URI'], 'action=edit' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post-new.php' ) ) {
209
+ return false;
210
+ }
211
 
212
+ if ( ! empty( $objects ) ) {
213
+ if ( isset( $_GET['post_type'] ) && ! isset( $_GET['taxonomy'] ) && in_array( $_GET['post_type'], $objects ) ) { // if page or custom post types
214
+ $active = true;
215
+ }
216
+ if ( ! isset( $_GET['post_type'] ) && strstr( $_SERVER['REQUEST_URI'], 'wp-admin/edit.php' ) && in_array( 'post', $objects ) ) { // if post
217
+ $active = true;
218
+ }
219
+ }
220
 
221
+ if ( ! empty( $tags ) ) {
222
+ if ( isset( $_GET['taxonomy'] ) && in_array( $_GET['taxonomy'], $tags ) ) {
223
+ $active = true;
224
+ }
225
+ }
226
+
227
+ return $active;
228
+ }
229
+
230
+ public function load_script_css() {
231
+ if ( $this->_check_load_script_css() ) {
232
+ wp_enqueue_script( 'jquery' );
233
+ wp_enqueue_script( 'jquery-ui-sortable' );
234
+ wp_enqueue_script( 'scporderjs', SCPORDER_URL . '/assets/scporder.min.js', array( 'jquery' ), SCPORDER_VERSION, true );
235
+ add_action( 'admin_print_styles', array( $this, 'print_scpo_style' ) );
236
+
237
+ }
238
+ }
239
 
240
+ public function refresh() {
241
+
242
+ if ( scporder_doing_ajax() ) {
243
+ return;
244
+ }
245
+
246
+ global $wpdb;
247
+ $objects = $this->get_scporder_options_objects();
248
+ $tags = $this->get_scporder_options_tags();
249
+
250
+ if ( ! empty( $objects ) ) {
251
+
252
+ foreach ( $objects as $object ) {
253
+ $result = $wpdb->get_results(
254
+ "
255
  SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
256
  FROM $wpdb->posts
257
  WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
258
+ "
259
+ );
260
 
261
+ if ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max ) {
262
+ continue;
263
+ }
264
 
265
+ // Here's the optimization
266
+ $wpdb->query( 'SET @row_number = 0;' );
267
+ $wpdb->query(
268
+ "UPDATE $wpdb->posts as pt JOIN (
269
 
270
  SELECT ID, (@row_number:=@row_number + 1) AS `rank`
271
  FROM $wpdb->posts
273
  ORDER BY menu_order ASC
274
  ) as pt2
275
  ON pt.id = pt2.id
276
+ SET pt.menu_order = pt2.`rank`;"
277
+ );
278
 
279
+ }
280
+ }
281
 
282
+ if ( ! empty( $tags ) ) {
283
+ foreach ( $tags as $taxonomy ) {
284
+ $result = $wpdb->get_results(
285
+ "
 
 
286
  SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min
287
  FROM $wpdb->terms AS terms
288
  INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
289
  WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
290
+ "
291
+ );
292
+ if ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max ) {
293
+ continue;
294
+ }
295
+
296
+ $results = $wpdb->get_results(
297
+ "
298
  SELECT terms.term_id
299
  FROM $wpdb->terms AS terms
300
  INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
301
  WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
302
  ORDER BY term_order ASC
303
+ "
304
+ );
305
+ foreach ( $results as $key => $result ) {
306
+ $wpdb->update( $wpdb->terms, array( 'term_order' => $key + 1 ), array( 'term_id' => $result->term_id ) );
307
+ }
308
+ }
309
+ }
310
+ }
 
 
311
 
312
+ public function update_menu_order() {
313
+ global $wpdb;
314
 
315
+ parse_str( $_POST['order'], $data );
 
316
 
317
+ if ( ! is_array( $data ) ) {
318
+ return false;
319
+ }
 
 
 
320
 
321
+ $id_arr = array();
322
+ foreach ( $data as $key => $values ) {
323
+ foreach ( $values as $position => $id ) {
324
+ $id_arr[] = $id;
325
+ }
326
+ }
 
327
 
328
+ $menu_order_arr = array();
329
+ foreach ( $id_arr as $key => $id ) {
330
+ $results = $wpdb->get_results( "SELECT menu_order FROM $wpdb->posts WHERE ID = " . intval( $id ) );
331
+ foreach ( $results as $result ) {
332
+ $menu_order_arr[] = $result->menu_order;
333
+ }
334
+ }
335
 
336
+ sort( $menu_order_arr );
 
 
 
 
337
 
338
+ foreach ( $data as $key => $values ) {
339
+ foreach ( $values as $position => $id ) {
340
+ $wpdb->update( $wpdb->posts, array( 'menu_order' => $menu_order_arr[ $position ] ), array( 'ID' => intval( $id ) ) );
341
+ }
342
+ }
343
 
344
+ do_action( 'scp_update_menu_order' );
345
 
346
+ }
 
347
 
348
+ public function update_menu_order_tags() {
349
+ global $wpdb;
350
 
351
+ parse_str( $_POST['order'], $data );
 
352
 
353
+ if ( ! is_array( $data ) ) {
354
+ return false;
355
+ }
 
 
 
356
 
357
+ $id_arr = array();
358
+ foreach ( $data as $key => $values ) {
359
+ foreach ( $values as $position => $id ) {
360
+ $id_arr[] = $id;
361
+ }
362
+ }
 
 
363
 
364
+ $menu_order_arr = array();
365
+ foreach ( $id_arr as $key => $id ) {
366
+ $results = $wpdb->get_results( "SELECT term_order FROM $wpdb->terms WHERE term_id = " . intval( $id ) );
367
+ foreach ( $results as $result ) {
368
+ $menu_order_arr[] = $result->term_order;
369
+ }
370
+ }
371
+ sort( $menu_order_arr );
372
 
373
+ foreach ( $data as $key => $values ) {
374
+ foreach ( $values as $position => $id ) {
375
+ $wpdb->update( $wpdb->terms, array( 'term_order' => $menu_order_arr[ $position ] ), array( 'term_id' => intval( $id ) ) );
376
+ }
377
+ }
378
 
379
+ do_action( 'scp_update_menu_order_tags' );
380
 
381
+ }
 
382
 
383
+ public function update_options() {
384
+ global $wpdb;
385
 
386
+ if ( ! isset( $_POST['scporder_submit'] ) ) {
387
+ return false;
388
+ }
389
 
390
+ check_admin_referer( 'nonce_scporder' );
 
 
 
391
 
392
+ $input_options = array();
393
+ $input_options['objects'] = isset( $_POST['objects'] ) ? $_POST['objects'] : '';
394
+ $input_options['tags'] = isset( $_POST['tags'] ) ? $_POST['tags'] : '';
395
+ $input_options['show_advanced_view'] = isset( $_POST['show_advanced_view'] ) ? $_POST['show_advanced_view'] : '';
396
 
397
+ update_option( 'scporder_options', $input_options );
398
 
399
+ $objects = $this->get_scporder_options_objects();
400
+ $tags = $this->get_scporder_options_tags();
401
 
402
+ if ( ! empty( $objects ) ) {
403
+ foreach ( $objects as $object ) {
404
+ $result = $wpdb->get_results(
405
+ "
406
  SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
407
  FROM $wpdb->posts
408
  WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
409
+ "
410
+ );
411
+ if ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max ) {
412
+ continue;
413
+ }
414
+
415
+ if ( $object == 'page' ) {
416
+ $results = $wpdb->get_results(
417
+ "
418
  SELECT ID
419
  FROM $wpdb->posts
420
  WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
421
  ORDER BY post_title ASC
422
+ "
423
+ );
424
+ } else {
425
+ $results = $wpdb->get_results(
426
+ "
427
  SELECT ID
428
  FROM $wpdb->posts
429
  WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
430
  ORDER BY post_date DESC
431
+ "
432
+ );
433
+ }
434
+ foreach ( $results as $key => $result ) {
435
+ $wpdb->update( $wpdb->posts, array( 'menu_order' => $key + 1 ), array( 'ID' => $result->ID ) );
436
+ }
437
+ }
438
+ }
439
+
440
+ if ( ! empty( $tags ) ) {
441
+ foreach ( $tags as $taxonomy ) {
442
+ $result = $wpdb->get_results(
443
+ "
444
  SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min
445
  FROM $wpdb->terms AS terms
446
  INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
447
  WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
448
+ "
449
+ );
450
+ if ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max ) {
451
+ continue;
452
+ }
453
+
454
+ $results = $wpdb->get_results(
455
+ "
456
  SELECT terms.term_id
457
  FROM $wpdb->terms AS terms
458
  INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
459
  WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
460
  ORDER BY name ASC
461
+ "
462
+ );
463
+ foreach ( $results as $key => $result ) {
464
+ $wpdb->update( $wpdb->terms, array( 'term_order' => $key + 1 ), array( 'term_id' => $result->term_id ) );
465
+ }
466
+ }
467
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468
 
469
+ wp_redirect( 'admin.php?page=scporder-settings&msg=update' );
470
+ }
 
471
 
472
+ public function scporder_previous_post_where( $where ) {
473
+ global $post;
 
 
 
 
 
474
 
475
+ $objects = $this->get_scporder_options_objects();
476
+ if ( empty( $objects ) ) {
477
+ return $where;
478
+ }
479
 
480
+ if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
481
+ $where = preg_replace( "/p.post_date < \'[0-9\-\s\:]+\'/i", "p.menu_order > '" . $post->menu_order . "'", $where );
482
+ }
483
+ return $where;
484
+ }
485
 
486
+ public function scporder_previous_post_sort( $orderby ) {
487
+ global $post;
488
 
489
+ $objects = $this->get_scporder_options_objects();
490
+ if ( empty( $objects ) ) {
491
+ return $orderby;
492
+ }
493
 
494
+ if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
495
+ $orderby = 'ORDER BY p.menu_order ASC LIMIT 1';
496
+ }
497
+ return $orderby;
498
+ }
499
 
500
+ public function scporder_next_post_where( $where ) {
501
+ global $post;
 
 
 
502
 
503
+ $objects = $this->get_scporder_options_objects();
504
+ if ( empty( $objects ) ) {
505
+ return $where;
506
+ }
507
+
508
+ if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
509
+ $where = preg_replace( "/p.post_date > \'[0-9\-\s\:]+\'/i", "p.menu_order < '" . $post->menu_order . "'", $where );
510
+ }
511
+ return $where;
512
+ }
513
+
514
+ public function scporder_next_post_sort( $orderby ) {
515
+ global $post;
516
+
517
+ $objects = $this->get_scporder_options_objects();
518
+ if ( empty( $objects ) ) {
519
+ return $orderby;
520
+ }
521
+
522
+ if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
523
+ $orderby = 'ORDER BY p.menu_order DESC LIMIT 1';
524
+ }
525
+ return $orderby;
526
+ }
527
+
528
+ public function scporder_pre_get_posts( $wp_query ) {
529
+ $objects = $this->get_scporder_options_objects();
530
+
531
+ if ( empty( $objects ) ) {
532
+ return false;
533
+ }
534
+ if ( is_admin() && ! wp_doing_ajax() ) {
535
+
536
+ if ( isset( $wp_query->query['post_type'] ) && ! isset( $wp_query->query['orderby'] ) ) {
537
+ if ( in_array( $wp_query->query['post_type'], $objects ) ) {
538
+ if ( ! $wp_query->get( 'orderby' ) ) {
539
+ $wp_query->set( 'orderby', 'menu_order' );
540
+ }
541
+ if ( ! $wp_query->get( 'order' ) ) {
542
+ $wp_query->set( 'order', 'ASC' );
543
+ }
544
+ }
545
+ }
546
+ } else {
547
+
548
+ $active = false;
549
+
550
+ if ( isset( $wp_query->query['post_type'] ) ) {
551
+ if ( ! is_array( $wp_query->query['post_type'] ) ) {
552
+ if ( in_array( $wp_query->query['post_type'], $objects ) ) {
553
+ $active = true;
554
+ }
555
+ }
556
+ } else {
557
+ if ( in_array( 'post', $objects ) ) {
558
+ $active = true;
559
+ }
560
+ }
561
+
562
+ if ( ! $active ) {
563
+ return false;
564
+ }
565
+
566
+ if ( isset( $wp_query->query['suppress_filters'] ) ) {
567
+ if ( $wp_query->get( 'orderby' ) == 'date' ) {
568
+ $wp_query->set( 'orderby', 'menu_order' );
569
+ }
570
+ if ( $wp_query->get( 'order' ) == 'DESC' ) {
571
+ $wp_query->set( 'order', 'ASC' );
572
+ }
573
+ } else {
574
+ if ( ! $wp_query->get( 'orderby' ) ) {
575
+ $wp_query->set( 'orderby', 'menu_order' );
576
+ }
577
+ if ( ! $wp_query->get( 'order' ) ) {
578
+ $wp_query->set( 'order', 'ASC' );
579
+ }
580
+ }
581
+ }
582
+ }
583
+
584
+ public function scporder_get_terms_orderby( $orderby, $args ) {
585
+
586
+ if ( is_admin() && ! wp_doing_ajax() ) {
587
+ return $orderby;
588
+ }
589
+
590
+ $tags = $this->get_scporder_options_tags();
591
+
592
+ if ( ! isset( $args['taxonomy'] ) ) {
593
+ return $orderby;
594
+ }
595
+
596
+ if ( is_array( $args['taxonomy'] ) ) {
597
+ if ( isset( $args['taxonomy'][0] ) ) {
598
+ $taxonomy = $args['taxonomy'][0];
599
+ } else {
600
+ $taxonomy = false;
601
+ }
602
+ } else {
603
+ $taxonomy = $args['taxonomy'];
604
+ }
605
+
606
+ if ( ! in_array( $taxonomy, $tags ) ) {
607
+ return $orderby;
608
+ }
609
+
610
+ $orderby = 't.term_order';
611
+ return $orderby;
612
+ }
613
+
614
+ public function scporder_get_object_terms( $terms ) {
615
+ $tags = $this->get_scporder_options_tags();
616
+
617
+ if ( is_admin() && ! wp_doing_ajax() && isset( $_GET['orderby'] ) ) {
618
+ return $terms;
619
+ }
620
+
621
+ foreach ( $terms as $key => $term ) {
622
+ if ( is_object( $term ) && isset( $term->taxonomy ) ) {
623
+ $taxonomy = $term->taxonomy;
624
+ if ( ! in_array( $taxonomy, $tags ) ) {
625
+ return $terms;
626
+ }
627
+ } else {
628
+ return $terms;
629
+ }
630
+ }
631
+
632
+ usort( $terms, array( $this, 'taxcmp' ) );
633
+ return $terms;
634
+ }
635
+
636
+ public function taxcmp( $a, $b ) {
637
+ if ( $a->term_order == $b->term_order ) {
638
+ return 0;
639
+ }
640
+ return ( $a->term_order < $b->term_order ) ? -1 : 1;
641
+ }
642
+
643
+ public function get_scporder_options_objects() {
644
+ $scporder_options = get_option( 'scporder_options' ) ? get_option( 'scporder_options' ) : array();
645
+ $objects = isset( $scporder_options['objects'] ) && is_array( $scporder_options['objects'] ) ? $scporder_options['objects'] : array();
646
+ return $objects;
647
+ }
648
+
649
+ public function get_scporder_options_tags() {
650
+ $scporder_options = get_option( 'scporder_options' ) ? get_option( 'scporder_options' ) : array();
651
+ $tags = isset( $scporder_options['tags'] ) && is_array( $scporder_options['tags'] ) ? $scporder_options['tags'] : array();
652
+ return $tags;
653
+ }
654
+
655
+
656
+ /**
657
+ * SCPO reset order for post types/taxonomies
658
+ */
659
+ public function scpo_ajax_reset_order() {
660
+
661
+ global $wpdb;
662
+ if ( 'scpo_reset_order' == $_POST['action'] ) {
663
+ check_ajax_referer( 'scpo-reset-order', 'scpo_security' );
664
+ $items = $_POST['items'];
665
+
666
+ $count = 0;
667
+ $in_list = '(';
668
+ foreach ( $items as $item ) {
669
+
670
+ if ( $count != 0 ) {
671
+ $in_list .= ',';
672
+ }
673
+ $in_list .= '\'' . $item . '\'';
674
+ $count++;
675
+ }
676
+ $in_list .= ')';
677
+
678
+ $prep_posts_query = "UPDATE $wpdb->posts SET `menu_order` = 0 WHERE `post_type` IN $in_list";
679
+
680
+ $result = $wpdb->query( $prep_posts_query );
681
+
682
+ $scpo_options = get_option( 'scporder_options' );
683
+
684
+ if ( ! false == $scpo_options ) {
685
+
686
+ $scpo_options['objects'] = array_diff( $scpo_options['objects'], $items );
687
+ update_option( 'scporder_options', $scpo_options );
688
+ }
689
+
690
+ if ( $result ) {
691
+ echo 'Items have been reset';
692
+ } else {
693
+ echo false;
694
+ }
695
+
696
+ wp_die();
697
+ }
698
+ }
699
 
700
  /**
701
  * Print inline admin style
702
  *
703
  * @since 2.5.4
704
  */
705
+ public function print_scpo_style() {
706
  ?>
707
  <style>
708
  .ui-sortable tr:hover {
723
 
724
  }
725
 
726
+ function scporder_doing_ajax() {
727
 
728
+ if ( function_exists( 'wp_doing_ajax' ) ) {
729
+ return wp_doing_ajax();
730
+ }
731
 
732
+ if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
733
+ return true;
734
+ }
735
 
736
+ return false;
737
 
738
  }
739
 
740
  /**
741
  * SCP Order Uninstall hook
742
  */
743
+ register_uninstall_hook( __FILE__, 'scporder_uninstall' );
744
 
745
  function scporder_uninstall() {
746
+ global $wpdb;
747
+ if ( function_exists( 'is_multisite' ) && is_multisite() ) {
748
+ $curr_blog = $wpdb->blogid;
749
+ $blogids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
750
+ foreach ( $blogids as $blog_id ) {
751
+ switch_to_blog( $blog_id );
752
+ scporder_uninstall_db();
753
+ }
754
+ switch_to_blog( $curr_blog );
755
+ } else {
756
+ scporder_uninstall_db();
757
+ }
758
  }
759
 
760
  function scporder_uninstall_db() {
761
+ global $wpdb;
762
+ $result = $wpdb->query( "DESCRIBE $wpdb->terms `term_order`" );
763
+ if ( $result ) {
764
+ $query = "ALTER TABLE $wpdb->terms DROP `term_order`";
765
+ $result = $wpdb->query( $query );
766
+ }
767
+ delete_option( 'scporder_install' );
768
  }
769