-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathDocs_List_Table.php
More file actions
123 lines (102 loc) · 3.68 KB
/
Copy pathDocs_List_Table.php
File metadata and controls
123 lines (102 loc) · 3.68 KB
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
<?php
namespace WeDevs\WeDocs\Admin;
/**
* Modifier functions in docs list table.
*/
class Docs_List_Table {
/**
* Constructor
*/
public function __construct() {
add_filter('manage_docs_posts_columns', [$this, 'docs_list_columns']);
add_action('manage_docs_posts_custom_column', [$this, 'docs_list_columns_row'], 10, 2);
add_filter('manage_edit-docs_sortable_columns', [$this, 'docs_sortable_columns']);
add_action('load-edit.php', [$this, 'edit_docs_load']);
add_action('load-post.php', [$this, 'add_meta_box']);
// load css
add_action('admin_print_styles-post.php', [$this, 'helpfulness_css']);
add_action('admin_print_styles-edit.php', [$this, 'helpfulness_css']);
}
public function add_meta_box() {
add_meta_box('op-menu-meta-box-id', __('Helpfulness', 'wedocs'), [$this, 'helpfulness_metabox'], 'docs', 'side', 'core');
}
public function helpfulness_css() {
if ('docs' != get_current_screen()->post_type) {
return;
} ?>
<style type="text/css">
.wedocs-positive {
color: green;
}
.wedocs-negative {
color: red;
text-align: right;
}
</style>
<?php
}
public function helpfulness_metabox() {
global $post;
$positive = (int) get_post_meta($post->ID, 'positive', true);
$negative = (int) get_post_meta($post->ID, 'negative', true);
?>
<table style="width: 100%;">
<tr>
<td class="wedocs-positive">
<span class="dashicons dashicons-thumbs-up"></span>
<?php printf('%d', $positive); ?>
</td>
<td class="wedocs-negative">
<span class="dashicons dashicons-thumbs-down"></span>
<?php printf('%d', $negative); ?>
</td>
</tr>
</table>
<?php
}
/**
* Vote column in the class UI.
*
* @param array $column
*
* @return array
*/
public function docs_list_columns($columns) {
$vote = ['votes' => __('Votes', 'wedocs')];
// insert before last element, date
$first_items = array_splice($columns, 0, 3); // remove first 3 items and store to $first_items, date remains to $columns
$new_columns = array_merge($first_items, $vote, $columns); // merge all those
return $new_columns;
}
public function docs_sortable_columns($columns) {
$columns['votes'] = ['votes', true];
return $columns;
}
public function docs_list_columns_row($column_name, $post_id) {
if ('votes' == $column_name) {
$positive = get_post_meta($post_id, 'positive', true);
$negative = get_post_meta($post_id, 'negative', true);
printf('<span class="wedocs-positive">%d</span>/<span class="wedocs-negative">%d</span>', $positive, $negative);
}
}
public function edit_docs_load() {
add_filter('request', [$this, 'sort_docs']);
}
// Sorts the movies.
public function sort_docs($vars) {
// Check if we're viewing the 'movie' post type.
if (isset($vars['post_type']) && 'docs' == $vars['post_type']) {
// Check if 'orderby' is set to 'duration'.
if (isset($vars['orderby']) && 'votes' == $vars['orderby']) {
$vars = array_merge(
$vars,
[
'meta_key' => 'positive',
'orderby' => 'meta_value_num',
]
);
}
}
return $vars;
}
}