Simple Custom Post Order - Version 2.4.0

Version Description

Download this release

Release Info

Developer machothemes
Plugin Icon wp plugin Simple Custom Post Order
Version 2.4.0
Comparing to
See all releases

Code changes from version 2.3.9 to 2.4.0

assets/scporder.css CHANGED
@@ -1,10 +1,10 @@
1
- .ui-sortable tr:hover {
2
- cursor: move;
3
- }
4
- .ui-sortable tr.alternate {
5
- background-color: #F9F9F9;
6
- }
7
- .ui-sortable tr.ui-sortable-helper {
8
- background-color: #F9F9F9;
9
- border-top: 1px solid #DFDFDF;
10
- }
1
+ .ui-sortable tr:hover {
2
+ cursor: move;
3
+ }
4
+ .ui-sortable tr.alternate {
5
+ background-color: #F9F9F9;
6
+ }
7
+ .ui-sortable tr.ui-sortable-helper {
8
+ background-color: #F9F9F9;
9
+ border-top: 1px solid #DFDFDF;
10
+ }
assets/scporder.js CHANGED
@@ -1,31 +1,31 @@
1
- (function($){
2
- $('table.posts #the-list, table.pages #the-list').sortable({
3
- 'items': 'tr',
4
- 'axis': 'y',
5
- 'helper': fixHelper,
6
- 'update' : function(e, ui) {
7
- $.post( ajaxurl, {
8
- action: 'update-menu-order',
9
- order: $('#the-list').sortable('serialize'),
10
- });
11
- }
12
- });
13
- $('table.tags #the-list').sortable({
14
- 'items': 'tr',
15
- 'axis': 'y',
16
- 'helper': fixHelper,
17
- 'update' : function(e, ui) {
18
- $.post( ajaxurl, {
19
- action: 'update-menu-order-tags',
20
- order: $('#the-list').sortable('serialize'),
21
- });
22
- }
23
- });
24
- var fixHelper = function(e, ui) {
25
- ui.children().children().each(function() {
26
- $(this).width($(this).width());
27
- });
28
- return ui;
29
- };
30
-
31
- })(jQuery)
1
+ (function($){
2
+ $('table.posts #the-list, table.pages #the-list').sortable({
3
+ 'items': 'tr',
4
+ 'axis': 'y',
5
+ 'helper': fixHelper,
6
+ 'update' : function(e, ui) {
7
+ $.post( ajaxurl, {
8
+ action: 'update-menu-order',
9
+ order: $('#the-list').sortable('serialize'),
10
+ });
11
+ }
12
+ });
13
+ $('table.tags #the-list').sortable({
14
+ 'items': 'tr',
15
+ 'axis': 'y',
16
+ 'helper': fixHelper,
17
+ 'update' : function(e, ui) {
18
+ $.post( ajaxurl, {
19
+ action: 'update-menu-order-tags',
20
+ order: $('#the-list').sortable('serialize'),
21
+ });
22
+ }
23
+ });
24
+ var fixHelper = function(e, ui) {
25
+ ui.children().children().each(function() {
26
+ $(this).width($(this).width());
27
+ });
28
+ return ui;
29
+ };
30
+
31
+ })(jQuery)
assets/taxonomy_order.js CHANGED
@@ -1,54 +1,54 @@
1
- jQuery( document ).ready( function( $ )
2
- {
3
- // if it is taxonomy page
4
- if( adminpage == 'edit-tags-php' )
5
- {
6
- // make table rows sortable
7
- $( '.wp-list-table.tags tbody' ).sortable({
8
- items: 'tr:not(.inline-edit-row)',
9
- cursor: 'move',
10
- axis: 'y',
11
- containment: 'table.widefat',
12
- scrollSensitivity: 40,
13
- stop: function( event, ui ) {
14
- // array for the ids and positions
15
- var rows = new Array([]);
16
-
17
- // show "activity" with spinner
18
- hsam_taxonomy_sort_activity_spinner( true );
19
-
20
- $( '.wp-list-table.tags tbody tr:not(.inline-edit-row)' ).each( function( i, e ) {
21
- var rowID = parseInt( $( e ).attr( 'id' ).substr( 4 ) );
22
- rows[i] = rowID;
23
- } );
24
-
25
- // post rows for sorting
26
- $.post( ajaxurl, { 'rows' : rows, 'action' : 'get_inline_boxes' }, function(response) {
27
- // stop activity spinner
28
- hsam_taxonomy_sort_activity_spinner( false );
29
- });
30
- }
31
- });
32
- }
33
-
34
- /**
35
- * Adds, shows or hides the activity status spinner
36
-
37
- */
38
- function hsam_taxonomy_sort_activity_spinner( status ) {
39
- var actions_table = $( '.tablenav .actions' );
40
-
41
- if( actions_table.find( '.spinner' ).length === 0 && status === true ) {
42
- // add spinner
43
- actions_table.prepend( '<div class="spinner" style="display: inline;" />' );
44
- }
45
- else if ( status === true ) {
46
- // show spinner
47
- actions_table.find( '.spinner' ).show();
48
- }
49
- else {
50
- // hide spinner
51
- actions_table.find( '.spinner' ).hide();
52
- }
53
- }
54
  } );
1
+ jQuery( document ).ready( function( $ )
2
+ {
3
+ // if it is taxonomy page
4
+ if( adminpage == 'edit-tags-php' )
5
+ {
6
+ // make table rows sortable
7
+ $( '.wp-list-table.tags tbody' ).sortable({
8
+ items: 'tr:not(.inline-edit-row)',
9
+ cursor: 'move',
10
+ axis: 'y',
11
+ containment: 'table.widefat',
12
+ scrollSensitivity: 40,
13
+ stop: function( event, ui ) {
14
+ // array for the ids and positions
15
+ var rows = new Array([]);
16
+
17
+ // show "activity" with spinner
18
+ hsam_taxonomy_sort_activity_spinner( true );
19
+
20
+ $( '.wp-list-table.tags tbody tr:not(.inline-edit-row)' ).each( function( i, e ) {
21
+ var rowID = parseInt( $( e ).attr( 'id' ).substr( 4 ) );
22
+ rows[i] = rowID;
23
+ } );
24
+
25
+ // post rows for sorting
26
+ $.post( ajaxurl, { 'rows' : rows, 'action' : 'get_inline_boxes' }, function(response) {
27
+ // stop activity spinner
28
+ hsam_taxonomy_sort_activity_spinner( false );
29
+ });
30
+ }
31
+ });
32
+ }
33
+
34
+ /**
35
+ * Adds, shows or hides the activity status spinner
36
+
37
+ */
38
+ function hsam_taxonomy_sort_activity_spinner( status ) {
39
+ var actions_table = $( '.tablenav .actions' );
40
+
41
+ if( actions_table.find( '.spinner' ).length === 0 && status === true ) {
42
+ // add spinner
43
+ actions_table.prepend( '<div class="spinner" style="display: inline;" />' );
44
+ }
45
+ else if ( status === true ) {
46
+ // show spinner
47
+ actions_table.find( '.spinner' ).show();
48
+ }
49
+ else {
50
+ // hide spinner
51
+ actions_table.find( '.spinner' ).hide();
52
+ }
53
+ }
54
  } );
readme.md CHANGED
@@ -1,81 +1,84 @@
1
- === Simple Custom Post Order ===
2
- Contributors: colorlibplugins, silkalns
3
- Tags: custom post order, post order, js post order, page order, posts order, category order, sort posts, sort pages, sort custom posts
4
- Requires at least: 3.5.1
5
- Tested up to: 5.0.0
6
- Stable tag: 2.3.9
7
- License: GPLv2 or later
8
- License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
-
10
- Order posts(posts, any custom post types) using a Drag and Drop Sortable JavaScript. Configuration is unnecessary.
11
-
12
- == Description ==
13
-
14
- Order posts(posts, any custom post types) using a Drag and Drop Sortable JavaScript. Configuration is unnecessary. You can do directly on default WordPress administration.
15
- Excluding custom query which uses order or orderby parameters, in get_posts or query_posts and so on.
16
-
17
- This plugins is now supported and maintained by <a href="https://colorlib.com/wp/" target="_blank">Colorlib</a>.
18
-
19
- == Installation ==
20
-
21
- 1. Upload `plugin-name.php` to the `/wp-content/plugins/` directory
22
- 1. Activate the plugin through the 'Plugins' menu in WordPress
23
-
24
- == Frequently asked questions ==
25
-
26
- = A question that someone might have =
27
-
28
- An answer to that question.
29
-
30
- == Screenshots ==
31
-
32
- 1. Order Custom Posts
33
- 2. Order Posts
34
- 3. Settings
35
-
36
- == Changelog ==
37
-
38
- = Version 2.3.9 =
39
- * Added button to dismiss the admin notice
40
-
41
- = Version 2.3.8 =
42
- * Fixed white screen ( https://wordpress.org/support/topic/white-screen-after-upgrade-to-2-3-6/ )
43
-
44
- = Version 2.3.7 =
45
- * Fixed white screen ( https://wordpress.org/support/topic/white-screen-after-upgrade-to-2-3-6/ )
46
-
47
- = Version 2.3.6 =
48
- * Fixed https://github.com/ColorlibHQ/simple-custom-post-order/issues/3
49
-
50
- = Version 2.3.5 =
51
- * Fixed https://github.com/ColorlibHQ/simple-custom-post-order/issues/12
52
-
53
- = Version 2.3.4 =
54
- * Removed deprecated function "screen_icon"
55
-
56
- = Version 2.3.2 (17-03-2017) =
57
- * Minor documentation and readme tweaks
58
-
59
- = Version 2.3 (24-03-2014) =
60
- * Fixed major bug on taxonomy and post order
61
-
62
- = Version 2.2 (02-07-2014) =
63
- * Fixed bug: Custom Query which uses 'order' or 'orderby' parameters is preferred
64
- * It does not depend on the designation manner of arguments( Parameters ). ( $args = 'orderby=&order=' or $args = array( 'orderby' => '', 'order' => '' ) )
65
- * Previous Versions Issues were Improved.
66
- * Removed Taxonomy Sort( Will add in next Version :) )
67
-
68
- = Version 2.1 (31-12-2013) =
69
- * Prevent Breaking autocomplete
70
-
71
- = Version 2.0 (22-11-2013) =
72
- * Fixed Undefined Notice Error in wp version 3.7.1
73
- * Taxonomy Activate Checkbox removed.
74
-
75
- = Version 1.5 (25-07-2013) =
76
- * Fix : fix errors
77
- * Added Taxonomy Sort
78
- * Added Taxonomy Sort option In setting Page
79
-
80
- = Version 1.0 (20-07-2013) =
81
- * Initial release.
 
 
 
1
+ === Simple Custom Post Order ===
2
+ Contributors: colorlibplugins, silkalns
3
+ Tags: custom post order, post order, js post order, page order, posts order, category order, sort posts, sort pages, sort custom posts
4
+ Requires at least: 3.5.1
5
+ Tested up to: 5.0.0
6
+ Stable tag: 2.4.0
7
+ License: GPLv2 or later
8
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
+
10
+ Order posts(posts, any custom post types) using a Drag and Drop Sortable JavaScript. Configuration is unnecessary.
11
+
12
+ == Description ==
13
+
14
+ Order posts(posts, any custom post types) using a Drag and Drop Sortable JavaScript. Configuration is unnecessary. You can do directly on default WordPress administration.
15
+ Excluding custom query which uses order or orderby parameters, in get_posts or query_posts and so on.
16
+
17
+ This plugins is now supported and maintained by <a href="https://colorlib.com/wp/" target="_blank">Colorlib</a>.
18
+
19
+ == Installation ==
20
+
21
+ 1. Upload `plugin-name.php` to the `/wp-content/plugins/` directory
22
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
23
+
24
+ == Frequently asked questions ==
25
+
26
+ = A question that someone might have =
27
+
28
+ An answer to that question.
29
+
30
+ == Screenshots ==
31
+
32
+ 1. Order Custom Posts
33
+ 2. Order Posts
34
+ 3. Settings
35
+
36
+ == Changelog ==
37
+
38
+ = Version 2.4.0 =
39
+ * Optimized our db queries ( https://wordpress.org/support/topic/update-optimization/ )
40
+
41
+ = Version 2.3.9 =
42
+ * Added button to dismiss the admin notice
43
+
44
+ = Version 2.3.8 =
45
+ * Fixed white screen ( https://wordpress.org/support/topic/white-screen-after-upgrade-to-2-3-6/ )
46
+
47
+ = Version 2.3.7 =
48
+ * Fixed white screen ( https://wordpress.org/support/topic/white-screen-after-upgrade-to-2-3-6/ )
49
+
50
+ = Version 2.3.6 =
51
+ * Fixed https://github.com/ColorlibHQ/simple-custom-post-order/issues/3
52
+
53
+ = Version 2.3.5 =
54
+ * Fixed https://github.com/ColorlibHQ/simple-custom-post-order/issues/12
55
+
56
+ = Version 2.3.4 =
57
+ * Removed deprecated function "screen_icon"
58
+
59
+ = Version 2.3.2 (17-03-2017) =
60
+ * Minor documentation and readme tweaks
61
+
62
+ = Version 2.3 (24-03-2014) =
63
+ * Fixed major bug on taxonomy and post order
64
+
65
+ = Version 2.2 (02-07-2014) =
66
+ * Fixed bug: Custom Query which uses 'order' or 'orderby' parameters is preferred
67
+ * It does not depend on the designation manner of arguments( Parameters ). ( $args = 'orderby=&order=' or $args = array( 'orderby' => '', 'order' => '' ) )
68
+ * Previous Versions Issues were Improved.
69
+ * Removed Taxonomy Sort( Will add in next Version :) )
70
+
71
+ = Version 2.1 (31-12-2013) =
72
+ * Prevent Breaking autocomplete
73
+
74
+ = Version 2.0 (22-11-2013) =
75
+ * Fixed Undefined Notice Error in wp version 3.7.1
76
+ * Taxonomy Activate Checkbox removed.
77
+
78
+ = Version 1.5 (25-07-2013) =
79
+ * Fix : fix errors
80
+ * Added Taxonomy Sort
81
+ * Added Taxonomy Sort option In setting Page
82
+
83
+ = Version 1.0 (20-07-2013) =
84
+ * Initial release.
simple-custom-post-order.php CHANGED
@@ -1,562 +1,563 @@
1
- <?php
2
-
3
- /*
4
- Plugin Name: Simple Custom Post Order
5
- Plugin URI: https://wordpress.org/plugins-wp/simple-custom-post-order/
6
- Description: Order Items (Posts, Pages, and Custom Post Types) using a Drag and Drop Sortable JavaScript.
7
- Version: 2.3.9
8
- Author: Colorlib
9
- Author URI: https://colorlib.com/wp/
10
- */
11
-
12
-
13
- define('SCPORDER_URL', plugins_url('', __FILE__));
14
- define('SCPORDER_DIR', plugin_dir_path(__FILE__));
15
-
16
- $scporder = new SCPO_Engine();
17
-
18
- class SCPO_Engine {
19
-
20
- function __construct() {
21
- if (!get_option('scporder_install'))
22
- $this->scporder_install();
23
-
24
- add_action('admin_menu', array($this, 'admin_menu'));
25
-
26
- add_action('admin_init', array($this, 'refresh'));
27
-
28
- add_action('admin_init', array($this, 'update_options'));
29
- add_action('admin_init', array($this, 'load_script_css'));
30
-
31
- add_action('wp_ajax_update-menu-order', array($this, 'update_menu_order'));
32
- add_action('wp_ajax_update-menu-order-tags', array($this, 'update_menu_order_tags'));
33
-
34
- add_action('pre_get_posts', array($this, 'scporder_pre_get_posts'));
35
-
36
- add_filter('get_previous_post_where', array($this, 'scporder_previous_post_where'));
37
- add_filter('get_previous_post_sort', array($this, 'scporder_previous_post_sort'));
38
- add_filter('get_next_post_where', array($this, 'scporder_next_post_where'));
39
- add_filter('get_next_post_sort', array($this, 'scporder_next_post_sort'));
40
-
41
- add_filter('get_terms_orderby', array($this, 'scporder_get_terms_orderby'), 10, 3);
42
- add_filter('wp_get_object_terms', array($this, 'scporder_get_object_terms'), 10, 3);
43
- add_filter('get_terms', array($this, 'scporder_get_object_terms'), 10, 3);
44
-
45
- add_action( 'admin_notices', array( $this, 'scporder_notice_not_checked' ) );
46
- add_action( 'wp_ajax_scporder_dismiss_notices', array( $this, 'dismiss_notices' ) );
47
-
48
- }
49
-
50
- public function dismiss_notices() {
51
-
52
- if ( ! check_admin_referer( 'scporder_dismiss_notice', 'scporder_nonce' ) ) {
53
- wp_die( 'nok' );
54
- }
55
-
56
- update_option( 'scporder_notice', '1' );
57
-
58
- wp_die( 'ok' );
59
-
60
- }
61
-
62
- public function scporder_notice_not_checked() {
63
-
64
- $settings = $this->get_scporder_options_objects();
65
- if ( ! empty( $settings ) ){
66
- return;
67
- }
68
-
69
- $screen = get_current_screen();
70
-
71
- if ( 'settings_page_scporder-settings' == $screen->id ) {
72
- return;
73
- }
74
-
75
- $dismessed = get_option( 'scporder_notice', false );
76
-
77
- if ( $dismessed ) {
78
- return;
79
- }
80
-
81
- ?>
82
- <div class="notice scpo-notice" id="scpo-notice">
83
- <img src="<?php echo esc_url( plugins_url( 'assets/logo.jpg', __FILE__ ) ); ?>" width="80">
84
-
85
- <h1><?php esc_html_e( 'Simple Custom Post Order', 'scporder' ); ?></h1>
86
-
87
- <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.', 'scporder' ); ?></p>
88
-
89
- <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 !', 'scporder' ); ?></a></p>
90
- <button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'scporder' ); ?></span></button>
91
- </div>
92
-
93
- <style>
94
- .scpo-notice {
95
- background: #e9eff3;
96
- border: 10px solid #fff;
97
- color: #608299;
98
- padding: 30px;
99
- text-align: center;
100
- position: relative;
101
- }
102
- </style>
103
- <script>
104
- jQuery(document).ready(function(){
105
- jQuery( '#scpo-notice .notice-dismiss' ).click(function( evt ){
106
- evt.preventDefault();
107
-
108
- console.log( 'asdasdas' );
109
-
110
- var ajaxData = {
111
- 'action' : 'scporder_dismiss_notices',
112
- 'scporder_nonce' : '<?php echo wp_create_nonce( 'scporder_dismiss_notice' ) ?>'
113
- }
114
-
115
- jQuery.ajax({
116
- url: "<?php echo admin_url('admin-ajax.php'); ?>",
117
- method: "POST",
118
- data: ajaxData,
119
- dataType: "html"
120
- }).done(function(){
121
- jQuery("#scpo-notice").hide();
122
- });
123
-
124
- });
125
- })
126
- </script>
127
- <?php
128
- }
129
-
130
- public function scporder_install() {
131
- global $wpdb;
132
- $result = $wpdb->query("DESCRIBE $wpdb->terms `term_order`");
133
- if (!$result) {
134
- $query = "ALTER TABLE $wpdb->terms ADD `term_order` INT( 4 ) NULL DEFAULT '0'";
135
- $result = $wpdb->query($query);
136
- }
137
- update_option('scporder_install', 1);
138
- }
139
-
140
- public function admin_menu() {
141
- add_options_page(__('SCPOrder', 'scporder'), __('SCPOrder', 'scporder'), 'manage_options', 'scporder-settings', array($this, 'admin_page'));
142
- }
143
-
144
- public function admin_page() {
145
- require SCPORDER_DIR . 'settings.php';
146
- }
147
-
148
- public function _check_load_script_css() {
149
- $active = false;
150
-
151
- $objects = $this->get_scporder_options_objects();
152
- $tags = $this->get_scporder_options_tags();
153
-
154
- if (empty($objects) && empty($tags))
155
- return false;
156
-
157
- if (isset($_GET['orderby']) || strstr($_SERVER['REQUEST_URI'], 'action=edit') || strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php'))
158
- return false;
159
-
160
- if (!empty($objects)) {
161
- if (isset($_GET['post_type']) && !isset($_GET['taxonomy']) && in_array($_GET['post_type'], $objects)) { // if page or custom post types
162
- $active = true;
163
- }
164
- if (!isset($_GET['post_type']) && strstr($_SERVER['REQUEST_URI'], 'wp-admin/edit.php') && in_array('post', $objects)) { // if post
165
- $active = true;
166
- }
167
- }
168
-
169
- if (!empty($tags)) {
170
- if (isset($_GET['taxonomy']) && in_array($_GET['taxonomy'], $tags)) {
171
- $active = true;
172
- }
173
- }
174
-
175
- return $active;
176
- }
177
-
178
- public function load_script_css() {
179
- if ($this->_check_load_script_css()) {
180
- wp_enqueue_script('jquery');
181
- wp_enqueue_script('jquery-ui-sortable');
182
- wp_enqueue_script('scporderjs', SCPORDER_URL . '/assets/scporder.js', array('jquery'), null, true);
183
-
184
- wp_enqueue_style('scporder', SCPORDER_URL . '/assets/scporder.css', array(), null);
185
- }
186
- }
187
-
188
- public function refresh() {
189
- global $wpdb;
190
- $objects = $this->get_scporder_options_objects();
191
- $tags = $this->get_scporder_options_tags();
192
-
193
- if (!empty($objects)) {
194
- foreach ($objects as $object) {
195
- $result = $wpdb->get_results("
196
- SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
197
- FROM $wpdb->posts
198
- WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
199
- ");
200
- if ($result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max)
201
- continue;
202
-
203
- $results = $wpdb->get_results("
204
- SELECT ID
205
- FROM $wpdb->posts
206
- WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
207
- ORDER BY menu_order ASC
208
- ");
209
- foreach ($results as $key => $result) {
210
- $wpdb->update($wpdb->posts, array('menu_order' => $key + 1), array('ID' => $result->ID));
211
- }
212
- }
213
- }
214
-
215
- if (!empty($tags)) {
216
- foreach ($tags as $taxonomy) {
217
- $result = $wpdb->get_results("
218
- SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min
219
- FROM $wpdb->terms AS terms
220
- INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
221
- WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
222
- ");
223
- if ($result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max)
224
- continue;
225
-
226
- $results = $wpdb->get_results("
227
- SELECT terms.term_id
228
- FROM $wpdb->terms AS terms
229
- INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
230
- WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
231
- ORDER BY term_order ASC
232
- ");
233
- foreach ($results as $key => $result) {
234
- $wpdb->update($wpdb->terms, array('term_order' => $key + 1), array('term_id' => $result->term_id));
235
- }
236
- }
237
- }
238
- }
239
-
240
- public function update_menu_order() {
241
- global $wpdb;
242
-
243
- parse_str($_POST['order'], $data);
244
-
245
- if (!is_array($data))
246
- return false;
247
-
248
- $id_arr = array();
249
- foreach ($data as $key => $values) {
250
- foreach ($values as $position => $id) {
251
- $id_arr[] = $id;
252
- }
253
- }
254
-
255
- $menu_order_arr = array();
256
- foreach ($id_arr as $key => $id) {
257
- $results = $wpdb->get_results("SELECT menu_order FROM $wpdb->posts WHERE ID = " . intval($id));
258
- foreach ($results as $result) {
259
- $menu_order_arr[] = $result->menu_order;
260
- }
261
- }
262
-
263
- sort($menu_order_arr);
264
-
265
- foreach ($data as $key => $values) {
266
- foreach ($values as $position => $id) {
267
- $wpdb->update($wpdb->posts, array('menu_order' => $menu_order_arr[$position]), array('ID' => intval($id)));
268
- }
269
- }
270
- }
271
-
272
- public function update_menu_order_tags() {
273
- global $wpdb;
274
-
275
- parse_str($_POST['order'], $data);
276
-
277
- if (!is_array($data))
278
- return false;
279
-
280
- $id_arr = array();
281
- foreach ($data as $key => $values) {
282
- foreach ($values as $position => $id) {
283
- $id_arr[] = $id;
284
- }
285
- }
286
-
287
- $menu_order_arr = array();
288
- foreach ($id_arr as $key => $id) {
289
- $results = $wpdb->get_results("SELECT term_order FROM $wpdb->terms WHERE term_id = " . intval($id));
290
- foreach ($results as $result) {
291
- $menu_order_arr[] = $result->term_order;
292
- }
293
- }
294
- sort($menu_order_arr);
295
-
296
- foreach ($data as $key => $values) {
297
- foreach ($values as $position => $id) {
298
- $wpdb->update($wpdb->terms, array('term_order' => $menu_order_arr[$position]), array('term_id' => intval($id)));
299
- }
300
- }
301
- }
302
-
303
- public function update_options() {
304
- global $wpdb;
305
-
306
- if (!isset($_POST['scporder_submit']))
307
- return false;
308
-
309
- check_admin_referer('nonce_scporder');
310
-
311
- $input_options = array();
312
- $input_options['objects'] = isset($_POST['objects']) ? $_POST['objects'] : '';
313
- $input_options['tags'] = isset($_POST['tags']) ? $_POST['tags'] : '';
314
-
315
- update_option('scporder_options', $input_options);
316
-
317
- $objects = $this->get_scporder_options_objects();
318
- $tags = $this->get_scporder_options_tags();
319
-
320
- if (!empty($objects)) {
321
- foreach ($objects as $object) {
322
- $result = $wpdb->get_results("
323
- SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
324
- FROM $wpdb->posts
325
- WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
326
- ");
327
- if ($result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max)
328
- continue;
329
-
330
- if ($object == 'page') {
331
- $results = $wpdb->get_results("
332
- SELECT ID
333
- FROM $wpdb->posts
334
- WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
335
- ORDER BY post_title ASC
336
- ");
337
- } else {
338
- $results = $wpdb->get_results("
339
- SELECT ID
340
- FROM $wpdb->posts
341
- WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
342
- ORDER BY post_date DESC
343
- ");
344
- }
345
- foreach ($results as $key => $result) {
346
- $wpdb->update($wpdb->posts, array('menu_order' => $key + 1), array('ID' => $result->ID));
347
- }
348
- }
349
- }
350
-
351
- if (!empty($tags)) {
352
- foreach ($tags as $taxonomy) {
353
- $result = $wpdb->get_results("
354
- SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min
355
- FROM $wpdb->terms AS terms
356
- INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
357
- WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
358
- ");
359
- if ($result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max)
360
- continue;
361
-
362
- $results = $wpdb->get_results("
363
- SELECT terms.term_id
364
- FROM $wpdb->terms AS terms
365
- INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
366
- WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
367
- ORDER BY name ASC
368
- ");
369
- foreach ($results as $key => $result) {
370
- $wpdb->update($wpdb->terms, array('term_order' => $key + 1), array('term_id' => $result->term_id));
371
- }
372
- }
373
- }
374
-
375
- wp_redirect('admin.php?page=scporder-settings&msg=update');
376
- }
377
-
378
- public function scporder_previous_post_where($where) {
379
- global $post;
380
-
381
- $objects = $this->get_scporder_options_objects();
382
- if (empty($objects))
383
- return $where;
384
-
385
- if (isset($post->post_type) && in_array($post->post_type, $objects)) {
386
- $where = preg_replace("/p.post_date < \'[0-9\-\s\:]+\'/i", "p.menu_order > '" . $post->menu_order . "'", $where);
387
- }
388
- return $where;
389
- }
390
-
391
- public function scporder_previous_post_sort($orderby) {
392
- global $post;
393
-
394
- $objects = $this->get_scporder_options_objects();
395
- if (empty($objects))
396
- return $orderby;
397
-
398
- if (isset($post->post_type) && in_array($post->post_type, $objects)) {
399
- $orderby = 'ORDER BY p.menu_order ASC LIMIT 1';
400
- }
401
- return $orderby;
402
- }
403
-
404
- public function scporder_next_post_where($where) {
405
- global $post;
406
-
407
- $objects = $this->get_scporder_options_objects();
408
- if (empty($objects))
409
- return $where;
410
-
411
- if (isset($post->post_type) && in_array($post->post_type, $objects)) {
412
- $where = preg_replace("/p.post_date > \'[0-9\-\s\:]+\'/i", "p.menu_order < '" . $post->menu_order . "'", $where);
413
- }
414
- return $where;
415
- }
416
-
417
- public function scporder_next_post_sort($orderby) {
418
- global $post;
419
-
420
- $objects = $this->get_scporder_options_objects();
421
- if (empty($objects))
422
- return $orderby;
423
-
424
- if (isset($post->post_type) && in_array($post->post_type, $objects)) {
425
- $orderby = 'ORDER BY p.menu_order DESC LIMIT 1';
426
- }
427
- return $orderby;
428
- }
429
-
430
- public function scporder_pre_get_posts($wp_query) {
431
- $objects = $this->get_scporder_options_objects();
432
- if (empty($objects))
433
- return false;
434
- if (is_admin()) {
435
-
436
- if (isset($wp_query->query['post_type']) && !isset($_GET['orderby'])) {
437
- if (in_array($wp_query->query['post_type'], $objects)) {
438
- $wp_query->set('orderby', 'menu_order');
439
- $wp_query->set('order', 'ASC');
440
- }
441
- }
442
- } else {
443
-
444
- $active = false;
445
-
446
- if (isset($wp_query->query['post_type'])) {
447
- if (!is_array($wp_query->query['post_type'])) {
448
- if (in_array($wp_query->query['post_type'], $objects)) {
449
- $active = true;
450
- }
451
- }
452
- } else {
453
- if (in_array('post', $objects)) {
454
- $active = true;
455
- }
456
- }
457
-
458
- if (!$active)
459
- return false;
460
-
461
- if (isset($wp_query->query['suppress_filters'])) {
462
- if ($wp_query->get('orderby') == 'date')
463
- $wp_query->set('orderby', 'menu_order');
464
- if ($wp_query->get('order') == 'DESC')
465
- $wp_query->set('order', 'ASC');
466
- } else {
467
- if (!$wp_query->get('orderby'))
468
- $wp_query->set('orderby', 'menu_order');
469
- if (!$wp_query->get('order'))
470
- $wp_query->set('order', 'ASC');
471
- }
472
- }
473
- }
474
-
475
- public function scporder_get_terms_orderby($orderby, $args) {
476
- if (is_admin())
477
- return $orderby;
478
-
479
- $tags = $this->get_scporder_options_tags();
480
-
481
- if (!isset($args['taxonomy']))
482
- return $orderby;
483
-
484
- $taxonomy = $args['taxonomy'];
485
- if (!in_array($taxonomy, $tags))
486
- return $orderby;
487
-
488
- $orderby = 't.term_order';
489
- return $orderby;
490
- }
491
-
492
- public function scporder_get_object_terms($terms) {
493
- $tags = $this->get_scporder_options_tags();
494
-
495
- if (is_admin() && isset($_GET['orderby']))
496
- return $terms;
497
-
498
- foreach ($terms as $key => $term) {
499
- if (is_object($term) && isset($term->taxonomy)) {
500
- $taxonomy = $term->taxonomy;
501
- if (!in_array($taxonomy, $tags))
502
- return $terms;
503
- } else {
504
- return $terms;
505
- }
506
- }
507
-
508
- usort($terms, array($this, 'taxcmp'));
509
- return $terms;
510
- }
511
-
512
- public function taxcmp($a, $b) {
513
- if ($a->term_order == $b->term_order)
514
- return 0;
515
- return ( $a->term_order < $b->term_order ) ? -1 : 1;
516
- }
517
-
518
- public function get_scporder_options_objects() {
519
- $scporder_options = get_option('scporder_options') ? get_option('scporder_options') : array();
520
- $objects = isset($scporder_options['objects']) && is_array($scporder_options['objects']) ? $scporder_options['objects'] : array();
521
- return $objects;
522
- }
523
-
524
- public function get_scporder_options_tags() {
525
- $scporder_options = get_option('scporder_options') ? get_option('scporder_options') : array();
526
- $tags = isset($scporder_options['tags']) && is_array($scporder_options['tags']) ? $scporder_options['tags'] : array();
527
- return $tags;
528
- }
529
-
530
- }
531
-
532
- /**
533
- * SCP Order Uninstall hook
534
- */
535
- register_uninstall_hook(__FILE__, 'scporder_uninstall');
536
-
537
- function scporder_uninstall() {
538
- global $wpdb;
539
- if (function_exists('is_multisite') && is_multisite()) {
540
- $curr_blog = $wpdb->blogid;
541
- $blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
542
- foreach ($blogids as $blog_id) {
543
- switch_to_blog($blog_id);
544
- scporder_uninstall_db();
545
- }
546
- switch_to_blog($curr_blog);
547
- } else {
548
- scporder_uninstall_db();
549
- }
550
- }
551
-
552
- function scporder_uninstall_db() {
553
- global $wpdb;
554
- $result = $wpdb->query("DESCRIBE $wpdb->terms `term_order`");
555
- if ($result) {
556
- $query = "ALTER TABLE $wpdb->terms DROP `term_order`";
557
- $result = $wpdb->query($query);
558
- }
559
- delete_option('scporder_install');
560
- }
561
-
 
562
  ?>
1
+ <?php
2
+
3
+ /*
4
+ Plugin Name: Simple Custom Post Order
5
+ Plugin URI: https://wordpress.org/plugins-wp/simple-custom-post-order/
6
+ Description: Order Items (Posts, Pages, and Custom Post Types) using a Drag and Drop Sortable JavaScript.
7
+ Version: 2.4.0
8
+ Author: Colorlib
9
+ Author URI: https://colorlib.com/wp/
10
+ */
11
+
12
+
13
+ define('SCPORDER_URL', plugins_url('', __FILE__));
14
+ define('SCPORDER_DIR', plugin_dir_path(__FILE__));
15
+
16
+ $scporder = new SCPO_Engine();
17
+
18
+ class SCPO_Engine {
19
+
20
+ function __construct() {
21
+ if (!get_option('scporder_install'))
22
+ $this->scporder_install();
23
+
24
+ add_action('admin_menu', array($this, 'admin_menu'));
25
+
26
+ add_action('admin_init', array($this, 'refresh'));
27
+
28
+ add_action('admin_init', array($this, 'update_options'));
29
+ add_action('admin_init', array($this, 'load_script_css'));
30
+
31
+ add_action('wp_ajax_update-menu-order', array($this, 'update_menu_order'));
32
+ add_action('wp_ajax_update-menu-order-tags', array($this, 'update_menu_order_tags'));
33
+
34
+ add_action('pre_get_posts', array($this, 'scporder_pre_get_posts'));
35
+
36
+ add_filter('get_previous_post_where', array($this, 'scporder_previous_post_where'));
37
+ add_filter('get_previous_post_sort', array($this, 'scporder_previous_post_sort'));
38
+ add_filter('get_next_post_where', array($this, 'scporder_next_post_where'));
39
+ add_filter('get_next_post_sort', array($this, 'scporder_next_post_sort'));
40
+
41
+ add_filter('get_terms_orderby', array($this, 'scporder_get_terms_orderby'), 10, 3);
42
+ add_filter('wp_get_object_terms', array($this, 'scporder_get_object_terms'), 10, 3);
43
+ add_filter('get_terms', array($this, 'scporder_get_object_terms'), 10, 3);
44
+
45
+ add_action( 'admin_notices', array( $this, 'scporder_notice_not_checked' ) );
46
+ add_action( 'wp_ajax_scporder_dismiss_notices', array( $this, 'dismiss_notices' ) );
47
+
48
+ }
49
+
50
+ public function dismiss_notices() {
51
+
52
+ if ( ! check_admin_referer( 'scporder_dismiss_notice', 'scporder_nonce' ) ) {
53
+ wp_die( 'nok' );
54
+ }
55
+
56
+ update_option( 'scporder_notice', '1' );
57
+
58
+ wp_die( 'ok' );
59
+
60
+ }
61
+
62
+ public function scporder_notice_not_checked() {
63
+
64
+ $settings = $this->get_scporder_options_objects();
65
+ if ( ! empty( $settings ) ){
66
+ return;
67
+ }
68
+
69
+ $screen = get_current_screen();
70
+
71
+ if ( 'settings_page_scporder-settings' == $screen->id ) {
72
+ return;
73
+ }
74
+
75
+ $dismessed = get_option( 'scporder_notice', false );
76
+
77
+ if ( $dismessed ) {
78
+ return;
79
+ }
80
+
81
+ ?>
82
+ <div class="notice scpo-notice" id="scpo-notice">
83
+ <img src="<?php echo esc_url( plugins_url( 'assets/logo.jpg', __FILE__ ) ); ?>" width="80">
84
+
85
+ <h1><?php esc_html_e( 'Simple Custom Post Order', 'scporder' ); ?></h1>
86
+
87
+ <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.', 'scporder' ); ?></p>
88
+
89
+ <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 !', 'scporder' ); ?></a></p>
90
+ <button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'scporder' ); ?></span></button>
91
+ </div>
92
+
93
+ <style>
94
+ .scpo-notice {
95
+ background: #e9eff3;
96
+ border: 10px solid #fff;
97
+ color: #608299;
98
+ padding: 30px;
99
+ text-align: center;
100
+ position: relative;
101
+ }
102
+ </style>
103
+ <script>
104
+ jQuery(document).ready(function(){
105
+ jQuery( '#scpo-notice .notice-dismiss' ).click(function( evt ){
106
+ evt.preventDefault();
107
+
108
+ var ajaxData = {
109
+ 'action' : 'scporder_dismiss_notices',
110
+ 'scporder_nonce' : '<?php echo wp_create_nonce( 'scporder_dismiss_notice' ) ?>'
111
+ }
112
+
113
+ jQuery.ajax({
114
+ url: "<?php echo admin_url('admin-ajax.php'); ?>",
115
+ method: "POST",
116
+ data: ajaxData,
117
+ dataType: "html"
118
+ }).done(function(){
119
+ jQuery("#scpo-notice").hide();
120
+ });
121
+
122
+ });
123
+ })
124
+ </script>
125
+ <?php
126
+ }
127
+
128
+ public function scporder_install() {
129
+ global $wpdb;
130
+ $result = $wpdb->query("DESCRIBE $wpdb->terms `term_order`");
131
+ if (!$result) {
132
+ $query = "ALTER TABLE $wpdb->terms ADD `term_order` INT( 4 ) NULL DEFAULT '0'";
133
+ $result = $wpdb->query($query);
134
+ }
135
+ update_option('scporder_install', 1);
136
+ }
137
+
138
+ public function admin_menu() {
139
+ add_options_page(__('SCPOrder', 'scporder'), __('SCPOrder', 'scporder'), 'manage_options', 'scporder-settings', array($this, 'admin_page'));
140
+ }
141
+
142
+ public function admin_page() {
143
+ require SCPORDER_DIR . 'settings.php';
144
+ }
145
+
146
+ public function _check_load_script_css() {
147
+ $active = false;
148
+
149
+ $objects = $this->get_scporder_options_objects();
150
+ $tags = $this->get_scporder_options_tags();
151
+
152
+ if (empty($objects) && empty($tags))
153
+ return false;
154
+
155
+ if (isset($_GET['orderby']) || strstr($_SERVER['REQUEST_URI'], 'action=edit') || strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php'))
156
+ return false;
157
+
158
+ if (!empty($objects)) {
159
+ if (isset($_GET['post_type']) && !isset($_GET['taxonomy']) && in_array($_GET['post_type'], $objects)) { // if page or custom post types
160
+ $active = true;
161
+ }
162
+ if (!isset($_GET['post_type']) && strstr($_SERVER['REQUEST_URI'], 'wp-admin/edit.php') && in_array('post', $objects)) { // if post
163
+ $active = true;
164
+ }
165
+ }
166
+
167
+ if (!empty($tags)) {
168
+ if (isset($_GET['taxonomy']) && in_array($_GET['taxonomy'], $tags)) {
169
+ $active = true;
170
+ }
171
+ }
172
+
173
+ return $active;
174
+ }
175
+
176
+ public function load_script_css() {
177
+ if ($this->_check_load_script_css()) {
178
+ wp_enqueue_script('jquery');
179
+ wp_enqueue_script('jquery-ui-sortable');
180
+ wp_enqueue_script('scporderjs', SCPORDER_URL . '/assets/scporder.js', array('jquery'), null, true);
181
+
182
+ wp_enqueue_style('scporder', SCPORDER_URL . '/assets/scporder.css', array(), null);
183
+ }
184
+ }
185
+
186
+ public function refresh() {
187
+ global $wpdb;
188
+ $objects = $this->get_scporder_options_objects();
189
+ $tags = $this->get_scporder_options_tags();
190
+
191
+ if (!empty($objects)) {
192
+ foreach ($objects as $object) {
193
+ $result = $wpdb->get_results("
194
+ SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
195
+ FROM $wpdb->posts
196
+ WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
197
+ ");
198
+
199
+ if ($result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max)
200
+ continue;
201
+
202
+ // Here's the optimization
203
+ $wpdb->query("SET @row_number = 0;");
204
+ $wpdb->query("UPDATE $wpdb->posts as pt JOIN (
205
+ SELECT ID, (@row_number:=@row_number + 1) AS rank
206
+ FROM $wpdb->posts
207
+ WHERE post_type = '$object' AND post_status IN ( 'publish', 'pending', 'draft', 'private', 'future' )
208
+ ORDER BY menu_order ASC
209
+ ) as pt2
210
+ ON pt.id = pt2.id
211
+ SET pt.menu_order = pt2.rank;");
212
+
213
+ }
214
+ }
215
+
216
+ if (!empty($tags)) {
217
+ foreach ($tags as $taxonomy) {
218
+ $result = $wpdb->get_results("
219
+ SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min
220
+ FROM $wpdb->terms AS terms
221
+ INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
222
+ WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
223
+ ");
224
+ if ($result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max)
225
+ continue;
226
+
227
+ $results = $wpdb->get_results("
228
+ SELECT terms.term_id
229
+ FROM $wpdb->terms AS terms
230
+ INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
231
+ WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
232
+ ORDER BY term_order ASC
233
+ ");
234
+ foreach ($results as $key => $result) {
235
+ $wpdb->update($wpdb->terms, array('term_order' => $key + 1), array('term_id' => $result->term_id));
236
+ }
237
+ }
238
+ }
239
+ }
240
+
241
+ public function update_menu_order() {
242
+ global $wpdb;
243
+
244
+ parse_str($_POST['order'], $data);
245
+
246
+ if (!is_array($data))
247
+ return false;
248
+
249
+ $id_arr = array();
250
+ foreach ($data as $key => $values) {
251
+ foreach ($values as $position => $id) {
252
+ $id_arr[] = $id;
253
+ }
254
+ }
255
+
256
+ $menu_order_arr = array();
257
+ foreach ($id_arr as $key => $id) {
258
+ $results = $wpdb->get_results("SELECT menu_order FROM $wpdb->posts WHERE ID = " . intval($id));
259
+ foreach ($results as $result) {
260
+ $menu_order_arr[] = $result->menu_order;
261
+ }
262
+ }
263
+
264
+ sort($menu_order_arr);
265
+
266
+ foreach ($data as $key => $values) {
267
+ foreach ($values as $position => $id) {
268
+ $wpdb->update($wpdb->posts, array('menu_order' => $menu_order_arr[$position]), array('ID' => intval($id)));
269
+ }
270
+ }
271
+ }
272
+
273
+ public function update_menu_order_tags() {
274
+ global $wpdb;
275
+
276
+ parse_str($_POST['order'], $data);
277
+
278
+ if (!is_array($data))
279
+ return false;
280
+
281
+ $id_arr = array();
282
+ foreach ($data as $key => $values) {
283
+ foreach ($values as $position => $id) {
284
+ $id_arr[] = $id;
285
+ }
286
+ }
287
+
288
+ $menu_order_arr = array();
289
+ foreach ($id_arr as $key => $id) {
290
+ $results = $wpdb->get_results("SELECT term_order FROM $wpdb->terms WHERE term_id = " . intval($id));
291
+ foreach ($results as $result) {
292
+ $menu_order_arr[] = $result->term_order;
293
+ }
294
+ }
295
+ sort($menu_order_arr);
296
+
297
+ foreach ($data as $key => $values) {
298
+ foreach ($values as $position => $id) {
299
+ $wpdb->update($wpdb->terms, array('term_order' => $menu_order_arr[$position]), array('term_id' => intval($id)));
300
+ }
301
+ }
302
+ }
303
+
304
+ public function update_options() {
305
+ global $wpdb;
306
+
307
+ if (!isset($_POST['scporder_submit']))
308
+ return false;
309
+
310
+ check_admin_referer('nonce_scporder');
311
+
312
+ $input_options = array();
313
+ $input_options['objects'] = isset($_POST['objects']) ? $_POST['objects'] : '';
314
+ $input_options['tags'] = isset($_POST['tags']) ? $_POST['tags'] : '';
315
+
316
+ update_option('scporder_options', $input_options);
317
+
318
+ $objects = $this->get_scporder_options_objects();
319
+ $tags = $this->get_scporder_options_tags();
320
+
321
+ if (!empty($objects)) {
322
+ foreach ($objects as $object) {
323
+ $result = $wpdb->get_results("
324
+ SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
325
+ FROM $wpdb->posts
326
+ WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
327
+ ");
328
+ if ($result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max)
329
+ continue;
330
+
331
+ if ($object == 'page') {
332
+ $results = $wpdb->get_results("
333
+ SELECT ID
334
+ FROM $wpdb->posts
335
+ WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
336
+ ORDER BY post_title ASC
337
+ ");
338
+ } else {
339
+ $results = $wpdb->get_results("
340
+ SELECT ID
341
+ FROM $wpdb->posts
342
+ WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
343
+ ORDER BY post_date DESC
344
+ ");
345
+ }
346
+ foreach ($results as $key => $result) {
347
+ $wpdb->update($wpdb->posts, array('menu_order' => $key + 1), array('ID' => $result->ID));
348
+ }
349
+ }
350
+ }
351
+
352
+ if (!empty($tags)) {
353
+ foreach ($tags as $taxonomy) {
354
+ $result = $wpdb->get_results("
355
+ SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min
356
+ FROM $wpdb->terms AS terms
357
+ INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
358
+ WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
359
+ ");
360
+ if ($result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max)
361
+ continue;
362
+
363
+ $results = $wpdb->get_results("
364
+ SELECT terms.term_id
365
+ FROM $wpdb->terms AS terms
366
+ INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
367
+ WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
368
+ ORDER BY name ASC
369
+ ");
370
+ foreach ($results as $key => $result) {
371
+ $wpdb->update($wpdb->terms, array('term_order' => $key + 1), array('term_id' => $result->term_id));
372
+ }
373
+ }
374
+ }
375
+
376
+ wp_redirect('admin.php?page=scporder-settings&msg=update');
377
+ }
378
+
379
+ public function scporder_previous_post_where($where) {
380
+ global $post;
381
+
382
+ $objects = $this->get_scporder_options_objects();
383
+ if (empty($objects))
384
+ return $where;
385
+
386
+ if (isset($post->post_type) && in_array($post->post_type, $objects)) {
387
+ $where = preg_replace("/p.post_date < \'[0-9\-\s\:]+\'/i", "p.menu_order > '" . $post->menu_order . "'", $where);
388
+ }
389
+ return $where;
390
+ }
391
+
392
+ public function scporder_previous_post_sort($orderby) {
393
+ global $post;
394
+
395
+ $objects = $this->get_scporder_options_objects();
396
+ if (empty($objects))
397
+ return $orderby;
398
+
399
+ if (isset($post->post_type) && in_array($post->post_type, $objects)) {
400
+ $orderby = 'ORDER BY p.menu_order ASC LIMIT 1';
401
+ }
402
+ return $orderby;
403
+ }
404
+
405
+ public function scporder_next_post_where($where) {
406
+ global $post;
407
+
408
+ $objects = $this->get_scporder_options_objects();
409
+ if (empty($objects))
410
+ return $where;
411
+
412
+ if (isset($post->post_type) && in_array($post->post_type, $objects)) {
413
+ $where = preg_replace("/p.post_date > \'[0-9\-\s\:]+\'/i", "p.menu_order < '" . $post->menu_order . "'", $where);
414
+ }
415
+ return $where;
416
+ }
417
+
418
+ public function scporder_next_post_sort($orderby) {
419
+ global $post;
420
+
421
+ $objects = $this->get_scporder_options_objects();
422
+ if (empty($objects))
423
+ return $orderby;
424
+
425
+ if (isset($post->post_type) && in_array($post->post_type, $objects)) {
426
+ $orderby = 'ORDER BY p.menu_order DESC LIMIT 1';
427
+ }
428
+ return $orderby;
429
+ }
430
+
431
+ public function scporder_pre_get_posts($wp_query) {
432
+ $objects = $this->get_scporder_options_objects();
433
+ if (empty($objects))
434
+ return false;
435
+ if (is_admin()) {
436
+
437
+ if (isset($wp_query->query['post_type']) && !isset($_GET['orderby'])) {
438
+ if (in_array($wp_query->query['post_type'], $objects)) {
439
+ $wp_query->set('orderby', 'menu_order');
440
+ $wp_query->set('order', 'ASC');
441
+ }
442
+ }
443
+ } else {
444
+
445
+ $active = false;
446
+
447
+ if (isset($wp_query->query['post_type'])) {
448
+ if (!is_array($wp_query->query['post_type'])) {
449
+ if (in_array($wp_query->query['post_type'], $objects)) {
450
+ $active = true;
451
+ }
452
+ }
453
+ } else {
454
+ if (in_array('post', $objects)) {
455
+ $active = true;
456
+ }
457
+ }
458
+
459
+ if (!$active)
460
+ return false;
461
+
462
+ if (isset($wp_query->query['suppress_filters'])) {
463
+ if ($wp_query->get('orderby') == 'date')
464
+ $wp_query->set('orderby', 'menu_order');
465
+ if ($wp_query->get('order') == 'DESC')
466
+ $wp_query->set('order', 'ASC');
467
+ } else {
468
+ if (!$wp_query->get('orderby'))
469
+ $wp_query->set('orderby', 'menu_order');
470
+ if (!$wp_query->get('order'))
471
+ $wp_query->set('order', 'ASC');
472
+ }
473
+ }
474
+ }
475
+
476
+ public function scporder_get_terms_orderby($orderby, $args) {
477
+ if (is_admin())
478
+ return $orderby;
479
+
480
+ $tags = $this->get_scporder_options_tags();
481
+
482
+ if (!isset($args['taxonomy']))
483
+ return $orderby;
484
+
485
+ $taxonomy = $args['taxonomy'];
486
+ if (!in_array($taxonomy, $tags))
487
+ return $orderby;
488
+
489
+ $orderby = 't.term_order';
490
+ return $orderby;
491
+ }
492
+
493
+ public function scporder_get_object_terms($terms) {
494
+ $tags = $this->get_scporder_options_tags();
495
+
496
+ if (is_admin() && isset($_GET['orderby']))
497
+ return $terms;
498
+
499
+ foreach ($terms as $key => $term) {
500
+ if (is_object($term) && isset($term->taxonomy)) {
501
+ $taxonomy = $term->taxonomy;
502
+ if (!in_array($taxonomy, $tags))
503
+ return $terms;
504
+ } else {
505
+ return $terms;
506
+ }
507
+ }
508
+
509
+ usort($terms, array($this, 'taxcmp'));
510
+ return $terms;
511
+ }
512
+
513
+ public function taxcmp($a, $b) {
514
+ if ($a->term_order == $b->term_order)
515
+ return 0;
516
+ return ( $a->term_order < $b->term_order ) ? -1 : 1;
517
+ }
518
+
519
+ public function get_scporder_options_objects() {
520
+ $scporder_options = get_option('scporder_options') ? get_option('scporder_options') : array();
521
+ $objects = isset($scporder_options['objects']) && is_array($scporder_options['objects']) ? $scporder_options['objects'] : array();
522
+ return $objects;
523
+ }
524
+
525
+ public function get_scporder_options_tags() {
526
+ $scporder_options = get_option('scporder_options') ? get_option('scporder_options') : array();
527
+ $tags = isset($scporder_options['tags']) && is_array($scporder_options['tags']) ? $scporder_options['tags'] : array();
528
+ return $tags;
529
+ }
530
+
531
+ }
532
+
533
+ /**
534
+ * SCP Order Uninstall hook
535
+ */
536
+ register_uninstall_hook(__FILE__, 'scporder_uninstall');
537
+
538
+ function scporder_uninstall() {
539
+ global $wpdb;
540
+ if (function_exists('is_multisite') && is_multisite()) {
541
+ $curr_blog = $wpdb->blogid;
542
+ $blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
543
+ foreach ($blogids as $blog_id) {
544
+ switch_to_blog($blog_id);
545
+ scporder_uninstall_db();
546
+ }
547
+ switch_to_blog($curr_blog);
548
+ } else {
549
+ scporder_uninstall_db();
550
+ }
551
+ }
552
+
553
+ function scporder_uninstall_db() {
554
+ global $wpdb;
555
+ $result = $wpdb->query("DESCRIBE $wpdb->terms `term_order`");
556
+ if ($result) {
557
+ $query = "ALTER TABLE $wpdb->terms DROP `term_order`";
558
+ $result = $wpdb->query($query);
559
+ }
560
+ delete_option('scporder_install');
561
+ }
562
+
563
  ?>