Skip to content

Commit b5e7d47

Browse files
committed
feat: on admin posts list page, add abilty to order by item importance
1 parent 9708fbe commit b5e7d47

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

src/Master.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function init() {
4646
(new Registrations())->init();
4747
(new Explanations\Explanations())->init();
4848
(new ParsedContent())->init();
49+
PostAddons\ItemImportance\ItemImportance::init();
4950
Queries::init();
5051
Formatting::init();
5152
SourceTypeTerm::init();
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
namespace Aivec\Plugins\DocParser\PostAddons\ItemImportance;
4+
5+
use AVCPDP\Aivec\Core\CSS\Loader;
6+
7+
/**
8+
* Item importance for `wp-parser-*` post types
9+
*/
10+
class ItemImportance
11+
{
12+
/**
13+
* Registers hooks
14+
*
15+
* @author Evan D Shaw <[email protected]>
16+
* @return void
17+
*/
18+
public static function init() {
19+
add_action('pre_get_posts', [get_class(), 'preGetPosts'], 10, 1);
20+
// Add admin post listing column for item importance indicator.
21+
add_filter('manage_posts_columns', [get_class(), 'addPostColumns']);
22+
// Output checkmark in item importance column if post is important.
23+
add_action('manage_posts_custom_column', [get_class(), 'handleColumnData'], 10, 2);
24+
foreach (avcpdp_get_parsed_post_types() as $ptype) {
25+
add_filter("manage_edit-{$ptype}_sortable_columns", [get_class(), 'filterSortableCols'], 10, 1);
26+
}
27+
add_action('admin_enqueue_scripts', [get_class(), 'loadAssets'], 10, 1);
28+
}
29+
30+
/**
31+
* Handles various custom query vars
32+
*
33+
* @param \WP_Query $query
34+
* @return void
35+
*/
36+
public static function preGetPosts($query) {
37+
if (!is_admin()) {
38+
return;
39+
}
40+
41+
$post_type = isset($_REQUEST['post_type']) ? (string)$_REQUEST['post_type'] : '';
42+
if (empty($post_type)) {
43+
return;
44+
}
45+
46+
if (!avcpdp_is_parsed_post_type($post_type)) {
47+
return;
48+
}
49+
50+
$mquery = $query->get('meta_query', []);
51+
$mquery[] = ['key' => '_wp-parser_important'];
52+
$query->set('meta_query', $mquery);
53+
}
54+
55+
/**
56+
* Loads admin post list page assets
57+
*
58+
* @author Evan D Shaw <[email protected]>
59+
* @param string $screenid
60+
* @return void
61+
*/
62+
public static function loadAssets($screenid) {
63+
if ($screenid !== 'edit.php') {
64+
return;
65+
}
66+
67+
$post_type = isset($_REQUEST['post_type']) ? (string)$_REQUEST['post_type'] : '';
68+
if (empty($post_type)) {
69+
return;
70+
}
71+
72+
if (!avcpdp_is_parsed_post_type($post_type)) {
73+
return;
74+
}
75+
76+
Loader::loadCoreCss();
77+
wp_enqueue_style(
78+
'avcpdp-postaddons-item-importance',
79+
AVCPDP_PLUGIN_URL . '/src/PostAddons/ItemImportance/item-importance.css',
80+
[],
81+
AVCPDP_VERSION
82+
);
83+
}
84+
85+
/**
86+
* Makes custom columns sortable
87+
*
88+
* @author Evan D Shaw <[email protected]>
89+
* @param array $sortable_columns
90+
* @return array
91+
*/
92+
public static function filterSortableCols($sortable_columns) {
93+
$sortable_columns['item_importance'] = 'meta_value';
94+
return $sortable_columns;
95+
}
96+
97+
/**
98+
* Adds custom columns in the admin listing of posts for parsed post types
99+
*
100+
* Custom columns include:
101+
* - Item Importance
102+
*
103+
* Also removes the comment column since comments aren't supported
104+
*
105+
* @param array $columns Associative array of post column ids and labels.
106+
* @return array
107+
*/
108+
public static function addPostColumns($columns) {
109+
if (empty($_GET['post_type'])) {
110+
return $columns;
111+
}
112+
113+
if (!avcpdp_is_parsed_post_type($_GET['post_type'])) {
114+
return $columns;
115+
}
116+
117+
$index = array_search('title', array_keys($columns));
118+
$pos = false === $index ? count($columns) : $index + 1;
119+
120+
$col_data = [
121+
'item_importance' => sprintf(
122+
'<span class="dashicons dashicons-yes" title="%s"></span><span class="screen-reader-text">%s</span>',
123+
esc_attr__('Important?', 'wp-parser'),
124+
esc_html__('Important?', 'wp-parser')
125+
),
126+
];
127+
$columns = array_merge(array_slice($columns, 0, $pos), $col_data, array_slice($columns, $pos));
128+
129+
// remove comments col
130+
unset($columns['comments']);
131+
132+
return $columns;
133+
}
134+
135+
/**
136+
* Outputs an indicator for the explanations column if post has an explanation.
137+
*
138+
* @param string $column_name The name of the column.
139+
* @param int $post_id The ID of the post.
140+
* @return void
141+
*/
142+
public static function handleColumnData($column_name, $post_id) {
143+
if ('item_importance' === $column_name) {
144+
$importance = (bool)(int)get_post_meta($post_id, '_wp-parser_important', true);
145+
if ($importance == true) {
146+
?>
147+
<div class="avc-v3 flex row-nowrap ai-center item-importance-container">
148+
<span><?php esc_html_e('Important', 'wp-parser'); ?></span>
149+
<span class="dashicons dashicons-yes" aria-hidden="true"></span>
150+
<span class="screen-reader-text"><?php esc_html_e('Important', 'wp-parser'); ?></span>
151+
</div>
152+
<?php
153+
}
154+
}
155+
}
156+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.item-importance-container {
2+
color: #cd0000;
3+
}
4+
5+
.fixed .column-item_importance {
6+
width: 5em;
7+
}

src/Queries.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public static function orderByNotDeprecated($orderby, $query) {
6161
* @return void
6262
*/
6363
public static function preGetPosts($query) {
64+
if (is_admin()) {
65+
return;
66+
}
67+
6468
$orderbynotdep = false;
6569
$orderbynotdephook = false;
6670
$ptype = !empty($query->query['post_type']) ? $query->query['post_type'] : '';

0 commit comments

Comments
 (0)