-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-reorder-admin.php
452 lines (384 loc) · 16.8 KB
/
class-reorder-admin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/**
* Reorder posts
*
* @package WordPress
* @subpackage Metronet Reorder Posts plugin
*/
/**
* Reorder posts
* Adds drag and drop editor for reordering WordPress posts
*
* This class is self-instantiated and should not be used directly
*
* @license http://www.gnu.org/licenses/gpl.html GPL
* @author Ronald Huereca
* @since 2.0.0
*/
class MN_Reorder_Admin {
/**
* @var $instance
* @desc Instance of the admin class
* @access private
*/
private static $instance = null;
/**
* @var $options
* @desc WordPress options for access within this class
* @access private
*/
private $options = false;
/**
* Class constructor
*
* Sets definitions
* Adds methods to appropriate hooks
*
* @author Ronald Huereca
* @since Reorder 1.1
* @access private
*/
private function __construct( ) {
//Filter to hide the admin panel options
if ( !apply_filters( 'metronet_reorder_post_allow_admin', true ) ) return; //Use this filter if you want to disable the admin panel settings for this plugin, including disabling the menu order overrides
//Initialize actions
/* Dev note: If you know what you're doing code-wise, add the menu_order in get_posts, pre_get_posts, or WP_Query, disable these filters, and/or disable the admin settings (see above). */
add_filter( 'posts_orderby', array( $this, 'modify_menu_order_sql' ), 30, 2 );
add_action( 'pre_get_posts', array( $this, 'modify_menu_order_pre' ), 30, 1 );
//Admin Settings
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
add_action( 'admin_init', array( $this, 'init_admin_settings' ) );
//Plugin settings
add_filter( 'plugin_action_links_' . REORDER_BASENAME , array( $this, 'add_settings_link' ) );
}
//Singleton
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
} //end get_instance
/**
* Initialize options page
*
* Create plugin options page and callback
*
* @since 2.0.0
* @access public
*
* @see init
*
*/
public function add_admin_menu() {
add_options_page( _x( 'Reorder Posts', 'Plugin Name - Settings Page Title', 'metronet-reorder-posts' ), _x( 'Reorder Posts', 'Plugin Name - Menu Item', 'metronet-reorder-posts' ), 'manage_options', 'metronet-reorder-posts', array( $this, 'options_page' ) );
}
/**
* Add query output
*
* Add query output
*
* @since 1.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $desc Description for the setting.
}
*/
public function add_settings_reorder_query( $args = array() ) {
//Get options/defaults
$settings = $this->get_plugin_options();
$settings_query = isset( $settings[ 'show_query' ] ) ? $settings[ 'show_query' ] : 'on';
printf( '<p><input type="radio" name="metronet-reorder-posts[show_query]" value="on" id="reorder_show_query_yes" %s /> <label for="reorder_show_query_yes">%s</label></p>', checked( 'on', $settings_query, false ), esc_html__( 'Yes', 'metronet-reorder-posts' ) );
printf( '<p><input type="radio" name="metronet-reorder-posts[show_query]" value="off" id="reorder_show_query_no" %s /> <label for="reorder_show_query_no">%s</label></p>', checked( 'off', $settings_query, false ), esc_html__( 'No', 'metronet-reorder-posts' ) );
}
/**
* Add post types to be overridden using menu_order
*
* Output options for menu_order
*
* @since 1.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $desc Description for the setting.
}
*/
public function add_settings_field_menu_order( $args = array() ) {
//Get options/defaults
$settings = $this->get_plugin_options();
$settings_menu_order = isset( $settings[ 'menu_order' ] ) ? $settings[ 'menu_order' ] : array();
$post_types = get_post_types( array(), 'object' );
//Foreach loop to show de awesome post types
foreach( $post_types as $post_type_name => $post_type ) {
//Determine whether to show this post type (show_ui = true)
$show_ui = (bool)isset( $post_type->show_ui ) ? $post_type->show_ui : false;
if ( !$show_ui || 'attachment' === $post_type_name ) continue;
//Get post type labels for checkbox
$post_type_value = isset( $settings_post_types[ $post_type_name ] ) ? $settings_post_types[ $post_type_name ] : 'on';
$post_type_label = isset( $post_type->label ) ? $post_type->label : $post_type_name;
printf( '<div id="metronet-reorder-posts-%s">', esc_attr( $post_type_name ) );
printf( '<h3>%s</h3>', esc_html( $post_type_label ) );
//Get menu order arguments
$menu_orderby = isset( $settings_menu_order[ $post_type_name ][ 'orderby' ] ) ? $settings_menu_order[ $post_type_name ][ 'orderby' ] : 'none';
//Output Menu Order Arguments
printf( '<p>%s</p>', esc_html__( 'Sort by:', 'metronet-reorder-posts' ) );
printf( '<select name="metronet-reorder-posts[menu_order][%1$s][orderby]">', esc_attr( $post_type_name ) );
printf( '<option value="none" %s>%s</option>', selected( 'none', $menu_orderby, false ), esc_html__( 'None', 'metronet-reorder-posts' ) );
printf( '<option value="menu_order" %s>%s</option>', selected( 'menu_order', $menu_orderby, false ), esc_html__( 'Menu Order', 'metronet-reorder-posts' ) );
echo '</select>';
//Get order arguments
$menu_order = isset( $settings_menu_order[ $post_type_name ][ 'order' ] ) ? $settings_menu_order[ $post_type_name ][ 'order' ] : 'DESC'; //DESC is WP_Query default
//Output Menu Order Arguments
printf( '<p>%s</p>', esc_html__( 'Sort Order:', 'metronet-reorder-posts' ) );
printf( '<select name="metronet-reorder-posts[menu_order][%1$s][order]">', esc_attr( $post_type_name ) );
printf( '<option value="ASC" %s>%s</option>', selected( 'ASC', $menu_order, false ), esc_html__( 'ASC', 'metronet-reorder-posts' ) );
printf( '<option value="DESC" %s>%s</option>', selected( 'DESC', $menu_order, false ), esc_html__( 'DESC', 'metronet-reorder-posts' ) );
echo '</select>';
//Output closing div
echo '</div>';
}
}
/**
* Add post types to be reordered using the plugin
*
* Output checkboxes for post types
*
* @since 1.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $desc Description for the setting.
}
*/
public function add_settings_field_post_types( $args = array() ) {
//Get options/defaults
$settings = $this->get_plugin_options();
$settings_post_types = isset( $settings[ 'post_types' ] ) ? $settings[ 'post_types' ] : array();
$post_types = get_post_types( array(), 'object' );
//Foreach loop to show de awesome post types
foreach( $post_types as $post_type_name => $post_type ) {
//Determine whether to show this post type (show_ui = true)
$show_ui = (bool)isset( $post_type->show_ui ) ? $post_type->show_ui : false;
if ( !$show_ui || 'attachment' === $post_type_name ) continue;
//Get post type labels for checkbox
$post_type_value = isset( $settings_post_types[ $post_type_name ] ) ? $settings_post_types[ $post_type_name ] : 'on';
$post_type_label = isset( $post_type->label ) ? $post_type->label : $post_type_name;
$checked = '';
if ( $post_type_value === 'on' ) {
$checked = checked( true, true, false );
}
//Output post type option
printf( '<div><input type="hidden" name="metronet-reorder-posts[post_types][%1$s]" value="off" /> <input type="checkbox" name="metronet-reorder-posts[post_types][%1$s]" id="post_type_%1$s" value="on" %2$s /><label for="post_type_%1$s"> %3$s</label></div>', esc_attr( $post_type_name ), $checked, esc_html( $post_type_label ) );
}
}
public function add_settings_heading_menu_order() {
printf( '<p>%s</p>', esc_html__( 'These are advanced options and you should only use these if you know what you are doing. These can overwrite the menu orders of custom queries (using get_posts or WP_Query), custom post type archives, and even the order your blog posts display.', 'metronet-reorder-posts' ) );
}
/**
* Add a settings link to the plugin's options.
*
* Add a settings link on the WordPress plugin's page.
*
* @since 2.0.0
* @access public
*
* @see init
*
* @param array $links Array of plugin options
* @return array $links Array of plugin options
*/
public function add_settings_link( $links ) {
$settings_link = sprintf( '<a href="%s">%s</a>', esc_url( admin_url( 'options-general.php?page=metronet-reorder-posts' ) ), _x( 'Settings', 'Plugin settings link on the plugins page', 'metronet-reorder-posts' ) );
array_unshift($links, $settings_link);
return $links;
}
/**
* Initialize and return plugin options.
*
* Return an array of plugin options.
*
* @since 1.0.0
* @access public
*
* @see init
*
* @return array Plugin options
*/
public function get_plugin_options() {
if ( false === $this->options ) {
$settings = get_option( 'metronet-reorder-posts' );
} else {
$settings = $this->options;
}
if ( false === $settings || !is_array( $settings ) ) {
$defaults = array(
'post_types' => array(),
'menu_order' => array(),
'show_query' => 'on'
);
update_option( 'metronet-reorder-posts', $defaults );
return $defaults;
}
$this->options = $settings;
return $settings;
}
/**
* Initialize options
*
* Initialize page settings, fields, and sections and their callbacks
*
* @since 2.0.0
* @access public
*
* @see init
*
*/
public function init_admin_settings() {
register_setting( 'metronet-reorder-posts', 'metronet-reorder-posts', array( $this, 'sanitization' ) );
add_settings_section( 'mn-reorder-post-types', _x( 'Enable Post Types', 'plugin settings heading' , 'metronet-reorder-posts' ), '__return_empty_string', 'metronet-reorder-posts' );
add_settings_section( 'mn-reorder-menu-order', _x( 'Advanced', 'plugin settings heading' , 'metronet-reorder-posts' ), array( $this, 'add_settings_heading_menu_order' ), 'metronet-reorder-posts' );
add_settings_section( 'mn-reorder-query', _x( 'Reorder Posts', 'plugin settings heading' , 'metronet-reorder-posts' ), '__return_empty_string', 'metronet-reorder-posts' );
add_settings_field( 'mn-post-types', __( 'Post Types', 'metronet-reorder-posts' ), array( $this, 'add_settings_field_post_types' ), 'metronet-reorder-posts', 'mn-reorder-post-types', array( 'desc' => __( 'Select the post types you would like this plugin enabled for.', 'metronet-reorder-posts' ) ) );
add_settings_field( 'mn-menu-order', __( 'Select the menu order arguments.', 'metronet-reorder-posts' ), array( $this, 'add_settings_field_menu_order' ), 'metronet-reorder-posts', 'mn-reorder-menu-order', array( 'desc' => __( 'Select the menu order for the post types.', 'metronet-reorder-posts' ) ) );
add_settings_field( 'mn-reorder-query', __( 'Show Posts Query', 'metronet-reorder-posts' ), array( $this, 'add_settings_reorder_query' ), 'metronet-reorder-posts', 'mn-reorder-query', array( 'desc' => __( 'Show a helpful sample query for Reorder posts.', 'metronet-reorder-posts' ) ) );
}
//Used for get_posts calls
public function modify_menu_order_pre( $query ) {
if( $query->is_main_query() || is_admin() || is_feed() ) return;
if ( !apply_filters( 'metronet_reorder_allow_menu_order', true ) ) return; //Return false to disable this completely
//Get main plugin options
$plugin_options = $this->get_plugin_options();
if ( !isset( $plugin_options[ 'menu_order' ] ) || !is_array( $plugin_options[ 'menu_order' ] ) || empty( $plugin_options[ 'menu_order' ] ) ) return;
$menu_order = $plugin_options[ 'menu_order' ];
//Get main post type
$main_query = $query->query;
$post_type = false;
if( isset( $main_query[ 'post_type' ] ) ) {
//Typically set on post type archives or custom WP_Query or get_posts instances
$post_type = $main_query[ 'post_type' ];
}
if ( !$post_type || is_array( $post_type ) ) return;
//See if suppress filters is on (using get_posts) (if it's not, use modify_menu_order_sql for that)
if ( !isset( $main_query[ 'suppress_filters' ] ) ) return;
if ( !apply_filters( 'metronet_reorder_allow_menu_order_' . $post_type, true ) ) return; //Return false to disable this for each individual post type as opposed to using a global filter above
//See if we're modifying the menu_order
$menu_order_orderby = isset( $menu_order[ $post_type ][ 'orderby' ] ) ? $menu_order[ $post_type ][ 'orderby' ] : 'none';
$menu_order_order = isset( $menu_order[ $post_type ][ 'order' ] ) ? $menu_order[ $post_type ][ 'order' ] : 'DESC';
//Return if post type is not be ordered
if ( $menu_order_orderby === 'none' ) return;
//Return of $menu_order_order is invalid
if ( $menu_order_order !== 'ASC' && $menu_order_order !== 'DESC' ) return;
//Overwrite the orderby clause
$query->set( 'orderby', 'menu_order' );
$query->set( 'order', $menu_order_order );
}
//Used on homepage archives, post type archives, or custom WP_Query calls
public function modify_menu_order_sql( $sql_orderby, $query ) {
if ( is_admin() || is_404() || is_feed() ) return $sql_orderby;
if ( !apply_filters( 'metronet_reorder_allow_menu_order', true ) ) return $sql_orderby; //Return false to disable this completely
//Get main plugin options
$plugin_options = $this->get_plugin_options();
if ( !isset( $plugin_options[ 'menu_order' ] ) || !is_array( $plugin_options[ 'menu_order' ] ) || empty( $plugin_options[ 'menu_order' ] ) ) return $sql_orderby;
$menu_order = $plugin_options[ 'menu_order' ];
$main_query = $query->query;
//Get main post type - Try to get post_type first, if not, see if we're on the main page showing blog posts
$post_type = false;
if( isset( $main_query[ 'post_type' ] ) ) {
//Typically set on post type archives or custom WP_Query or get_posts instances
$post_type = $main_query[ 'post_type' ];
} elseif ( is_home() ) {
$post_type = 'post';
}
if ( !$post_type || is_array( $post_type ) ) return $sql_orderby;
if ( !apply_filters( 'metronet_reorder_allow_menu_order_' . $post_type, true ) ) return $sql_orderby; //Return false to disable this for each individual post type as opposed to using a global filter above
//See if we're modifying the menu_order
$menu_order_orderby = isset( $menu_order[ $post_type ][ 'orderby' ] ) ? $menu_order[ $post_type ][ 'orderby' ] : 'none';
$menu_order_order = isset( $menu_order[ $post_type ][ 'order' ] ) ? $menu_order[ $post_type ][ 'order' ] : 'DESC';
//Return if post type is not be ordered
if ( $menu_order_orderby === 'none' ) return $sql_orderby;
//Return of $menu_order_order is invalid
if ( $menu_order_order !== 'ASC' && $menu_order_order !== 'DESC' ) return $sql_orderby;
//Overwrite the orderby clause
global $wpdb;
$sql_orderby = sprintf( '%s.menu_order %s', $wpdb->posts, $menu_order_order ); //for devs: no sanitization required as tablename is from $wpdb and $menu_order_order is set to match only ASC or DESC.
//Return
return $sql_orderby;
}
/**
* Output options page HTML.
*
* Output option page HTML and fields/sections.
*
* @since 2.0.0
* @access public
*
* @see add_admin_menu
*
*/
public function options_page() {
?>
<div class="wrap">
<h2><?php echo esc_html( _x( 'Reorder Posts', 'Plugin Name - Settings Page Title', 'metronet-reorder-posts' ) ); ?></h2>
<form action="<?php echo esc_url( admin_url( 'options.php' ) ); ?>" method="POST">
<?php settings_fields( 'metronet-reorder-posts' ); ?>
<?php do_settings_sections( 'metronet-reorder-posts' ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Sanitize options before they are saved.
*
* Sanitize and prepare error messages when saving options.
*
* @since 2.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $input {
@type array $post_types
@type array $menu_order
}
* @return array Sanitized array of options
*/
public function sanitization( $input = array() ) {
//Get post type options
$post_types = $input[ 'post_types' ];
if ( !empty( $post_types ) ) {
foreach( $post_types as $post_type_name => &$value ) {
if ( $value !== 'on' ) {
$value == 'off';
}
}
$input[ 'post_types' ] = $post_types;
}
//Get menu order options
$menu_order = $input[ 'menu_order' ];
if ( !empty( $menu_order ) ) {
foreach( $post_types as $post_type_name => &$values ) {
$orderby = isset( $menu_order[ $post_type_name ][ 'orderby' ] ) ? $menu_order[ $post_type_name ] : 'none';
if ( $orderby !== 'menu_order' ) {
$menu_order[ $post_type_name ][ 'orderby' ] == 'none';
}
$order = isset( $menu_order[ $post_type_name ][ 'order' ] ) ? $menu_order[ $post_type_name ] : 'DESC';
if ( $orderby !== 'ASC' ) {
$menu_order[ $post_type_name ][ 'order' ] == 'DESC';
}
}
$input[ 'menu_order' ] = $menu_order;
}
return $input;
}
} //end class Reorder_Admin
add_action( 'init', 'mn_reorder_admin_instantiate', 15 );
function mn_reorder_admin_instantiate() {
MN_Reorder_Admin::get_instance();
}