forked from crowdfavorite/wp-admin-column-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin-column-view.php
196 lines (169 loc) · 5.88 KB
/
admin-column-view.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
<?php
/*
Plugin Name: Admin Column View
Plugin URI: http://crowdfavorite/wordpress/plugins/
Description: Adds a columnar view of all your pages (and hierarchical custom post types), similar to the view found in the OS X Finder. Makes it much easier to manage sites with lots of pages. Drag and drop to re-order your pages.
Version: 1.0.3
Author: Crowd Favorite
Author URI: http://crowdfavorite.com/
License: GPL2
*/
if (!class_exists('CF_Admin_Column_View')) {
load_plugin_textdomain('admin-column-view', false, dirname(plugin_basename(__FILE__)).'/languages/');
// add to menu
add_action('admin_menu', array('CF_Admin_Column_View', 'add_submenu_page'));
// controllers
add_action('wp_ajax_cf-admin-column-view-column', array('CF_Admin_Column_View', 'controller_action_column'));
add_action('wp_ajax_cf-admin-column-view-sort', array('CF_Admin_Column_View', 'controller_action_sort'));
// set post_parent
add_action('add_meta_boxes', array('CF_Admin_Column_View', 'set_post_parent'), 10, 2);
class CF_Admin_Column_View {
static function add_submenu_page() {
// get all hierarchical post types
$post_types = get_post_types(array(
'hierarchical' => true,
'show_ui' => true,
), 'objects');
// Allow post types to be filterable
$post_types = apply_filters( 'cf_admin_column_post_types', $post_types);
$menu_label = _x('Column View', 'name in menu', 'cf-admin-column-view');
foreach ($post_types as $post_type => $post_type_obj) {
add_submenu_page(
'edit.php?post_type='.$post_type,
sprintf(_x('%1$s Column View', 'title on admin page', 'cf-admin-column-view'), $post_type_obj->labels->name),
$menu_label,
$post_type_obj->cap->edit_posts,
'cf-admin-column-view-'.$post_type,
array('CF_Admin_Column_View', 'admin_page')
);
}
}
static function admin_page() {
wp_enqueue_script('jquery-ui-sortable');
if (empty($_GET['post_type'])) {
wp_die(_x('Whoops!', 'random error happened', 'cf-admin-column-view'));
}
$post_type = stripslashes($_GET['post_type']);
$column_data = self::column_data($post_type, 0);
include('views/admin-page.php');
}
static function nonce_action_sort($post_type = 'page', $parent_id = 0) {
return 'sort-'.$post_type.'-'.$parent_id;
}
static function column_data($post_type = 'page', $parent_id = 0) {
$post_statuses = array('publish', 'pending', 'draft', 'future', 'private');
// get column pages
add_filter('posts_fields', array('CF_Admin_Column_View', 'column_data_fields'));
add_filter('posts_orderby', array('CF_Admin_Column_View', 'column_data_orderby'));
$query = new WP_Query(array(
'orderby' => 'menu_order',
'post_type' => $post_type,
'post_parent' => $parent_id,
'posts_per_page' => -1,
'status' => $post_statuses,
));
remove_filter('posts_fields', array('CF_Admin_Column_View', 'column_data_fields'));
remove_filter('posts_orderby', array('CF_Admin_Column_View', 'column_data_orderby'));
// check for sub-pages
$has_children = array();
if (!empty($query->posts)) {
$post_ids = array();
foreach ($query->posts as $post) {
$post_ids[] = $post->ID;
}
global $wpdb;
$result = $wpdb->get_results($wpdb->prepare("
SELECT post_parent
FROM $wpdb->posts
WHERE post_parent IN (".implode(',', $post_ids).")
AND post_type = %s
AND post_status IN ('".implode("','", $post_statuses)."')
GROUP BY post_parent
", $post_type));
if (!empty($result)) {
foreach ($result as $data) {
$has_children[] = $data->post_parent;
}
}
foreach ($query->posts as &$post) {
$post->has_children = in_array($post->ID, $has_children);
}
}
$column_data = array(
'items' => $query->posts,
'nonce' => wp_create_nonce(self::nonce_action_sort($post_type, $parent_id)),
'parent_id' => $parent_id,
'post_type' => $post_type,
);
return $column_data;
}
static function column_data_fields($fields) {
global $wpdb;
return "$wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->posts.post_type, $wpdb->posts.menu_order, $wpdb->posts.post_status, $wpdb->posts.post_password";
}
static function column_data_orderby($fields) {
global $wpdb;
return "$wpdb->posts.menu_order, $wpdb->posts.post_title";
}
static function controller_action_column() {
// get post type
$post_type_obj = $post_type = false;
if (!empty($_GET['post_type'])) {
$post_type = stripslashes($_GET['post_type']);
$post_type_obj = get_post_type_object($post_type);
}
// check permissions
if (!$post_type || !current_user_can($post_type_obj->cap->edit_posts)) {
wp_die("Cheatin', eh?");
}
// get data and load view
$column_data = self::column_data($post_type, intval($_GET['parent_id']));
ob_start();
include('views/column.php');
$html = ob_get_clean();
// output
header('Content-type: application/json');
echo json_encode(array(
'result' => 'success',
'html' => $html,
));
die();
}
static function controller_action_sort() {
// get post type
$post_type_obj = $post_type = false;
if (!empty($_POST['post_type'])) {
$post_type = stripslashes($_POST['post_type']);
$post_type_obj = get_post_type_object($post_type);
}
// check nonce and permissions
$nonce_action = self::nonce_action_sort($post_type, intval($_POST['parent_id']));
if (!$post_type ||
!current_user_can($post_type_obj->cap->edit_posts) ||
!wp_verify_nonce(stripslashes($_POST['nonce']), $nonce_action)) {
wp_die("Cheatin', eh?");
}
// set menu order for each post
if (!empty($_POST['order']) && is_array($_POST['order'])) {
foreach ($_POST['order'] as $data) {
wp_update_post(array(
'ID' => $data[0],
'menu_order' => $data[1]
));
}
}
// output
header('Content-type: application/json');
echo json_encode(array(
'result' => 'success',
));
die();
}
static function set_post_parent($post_type, $post) {
global $post;
if (isset($_GET['post_parent'])) {
$post->post_parent = intval($_GET['post_parent']);
}
}
}
} // end class_exists() check