-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclass-admin.php
More file actions
277 lines (243 loc) · 6.5 KB
/
Copy pathclass-admin.php
File metadata and controls
277 lines (243 loc) · 6.5 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
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
<?php
/**
* Admin class.
*
* @since 2.3.0
*
* @package WebberZone\Knowledge_Base
*/
namespace WebberZone\Knowledge_Base\Admin;
use WebberZone\Knowledge_Base\Util\Cache;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Class to register the Better Search Admin Area.
*
* @since 2.3.0
*/
class Admin {
/**
* Settings API.
*
* @since 2.3.0
*
* @var object Settings API.
*/
public $settings;
/**
* Activator class.
*
* @since 2.3.0
*
* @var object Activator class.
*/
public $activator;
/**
* Cache.
*
* @since 2.3.0
*
* @var object Cache.
*/
public $cache;
/**
* Admin columns.
*
* @since 2.3.0
*
* @var object Admin columns.
*/
public $admin_columns;
/**
* Product Migrator class.
*
* @since 3.0.0
*
* @var object Product Migrator class.
*/
public $product_migrator;
/**
* Main constructor class.
*
* @since 2.3.0
*/
public function __construct() {
$this->hooks();
// Initialise admin classes.
$this->settings = new Settings();
$this->activator = new Activator();
$this->cache = new Cache();
$this->admin_columns = new Admin_Columns();
$this->product_migrator = new Product_Migrator();
}
/**
* Run the hooks.
*
* @since 2.3.0
*/
public function hooks() {
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
add_filter( 'dashboard_glance_items', array( $this, 'dashboard_glance_items' ), 10, 1 );
add_filter( 'admin_head', array( $this, 'admin_head' ) );
add_action( 'admin_footer', array( $this, 'maybe_add_button_to_post_list' ) );
}
/**
* Enqueue scripts in admin area.
*
* @since 2.3.0
*/
public function admin_enqueue_scripts() {
$minimize = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_register_script(
'wzkb-admin',
plugins_url( "js/admin-scripts{$minimize}.js", __FILE__ ),
array( 'jquery', 'jquery-ui-tabs' ),
WZKB_VERSION,
true
);
wp_localize_script(
'wzkb-admin',
'WZKBAdminData',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'security' => wp_create_nonce( 'wzkb-admin' ),
'strings' => array(
'confirm_message' => esc_html__( 'Are you sure you want to clear the cache?', 'knowledgebase' ),
'success_message' => esc_html__( 'Cache cleared successfully!', 'knowledgebase' ),
'fail_message' => esc_html__( 'Failed to clear cache. Please try again.', 'knowledgebase' ),
'request_fail_message' => esc_html__( 'Request failed: ', 'knowledgebase' ),
),
)
);
wp_register_style(
'wzkb-admin-ui',
plugins_url( "css/admin{$minimize}.css", __FILE__ ),
array(),
WZKB_VERSION
);
if ( isset( $_GET['post_type'] ) && 'wz_knowledgebase' === $_GET['post_type'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
wp_enqueue_style( 'wzkb-admin-ui' );
}
}
/**
* Display admin notices.
*
* @since 2.3.0
*/
public function admin_notices() {
$kb_slug = \wzkb_get_option( 'kb_slug', 'not-set-random-string' );
$product_slug = \wzkb_get_option( 'product_slug', 'not-set-random-string' );
$cat_slug = \wzkb_get_option( 'category_slug', 'not-set-random-string' );
$tag_slug = \wzkb_get_option( 'tag_slug', 'not-set-random-string' );
// Only add the notice if the user is an admin.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Only add the notice if the settings cannot be found.
if ( 'not-set-random-string' === $kb_slug || 'not-set-random-string' === $product_slug || 'not-set-random-string' === $cat_slug || 'not-set-random-string' === $tag_slug ) {
?>
<div class="updated">
<p>
<?php
printf(
/* translators: 1. Link to admin page. */
esc_html__( 'Knowledge Base settings for the slug have not been registered. Please visit the <a href="%s">admin page</a> to update and save the options.', 'knowledgebase' ),
esc_url( admin_url( 'edit.php?post_type=wz_knowledgebase&page=wzkb-settings' ) )
);
?>
</p>
</div>
<?php
}
}
/**
* Add number of articles to At a Glance widget
*
* @since 2.3.0
*
* @param array $items Array of items.
* @return array Updated array of items
*/
public function dashboard_glance_items( $items ) {
$num_posts = wp_count_posts( 'wz_knowledgebase' );
if ( ! empty( $num_posts->publish ) ) {
/* translators: 1. Number of articles */
$text = _n( '%s KB article', '%s KB articles', $num_posts->publish, 'knowledgebase' );
$text = sprintf( $text, number_format_i18n( $num_posts->publish ) );
if ( current_user_can( 'edit_posts' ) ) {
$text = sprintf( '<a class="wzkb-article-count" href="edit.php?post_type=wz_knowledgebase">%1$s</a>', $text );
} else {
$text = sprintf( '<span class="wzkb-article-count">%1$s</span>', $text );
}
$items[] = $text;
}
return $items;
}
/**
* Add CSS to Admin head
*
* @since 2.3.0
*
* return void
*/
public function admin_head() {
if ( 'index.php' === $GLOBALS['pagenow'] ) {
?>
<style type="text/css" media="screen">
#dashboard_right_now .wzkb-article-count:before {
content: "\f331";
}
</style>
<?php
}
}
/**
* Display admin sidebar.
*
* @since 2.3.0
*/
public static function display_admin_sidebar() {
require_once __DIR__ . '/settings/sidebar.php';
}
/**
* Add button to knowledgebase post type list screen
*/
public function maybe_add_button_to_post_list() {
$screen = get_current_screen();
if ( ! $screen ||
( 'wz_knowledgebase' !== $screen->post_type && 'wzkb_category' !== $screen->taxonomy ) ||
( 'edit' !== $screen->base && 'wz_knowledgebase' !== $screen->post_type ) ) {
return;
}
$this->render_custom_button( 'Visit Knowledge Base' );
}
/**
* Render the custom button
*
* @since 2.3.0
*
* @param string $button_text Text to display on the button.
*/
private function render_custom_button( $button_text = 'Custom Action' ) {
?>
<script>
jQuery(document).ready(function($) {
// Find the H1 and insert our button right after it
var $h1 = $('.wrap h1');
// Create the button
var $kbUrlButton = $('<a>', {
href: '<?php wzkb_the_kb_url(); ?>',
class: 'page-title-action wzkb_button wzkb_button_blue',
text: '<?php echo esc_js( $button_text ); ?>',
target: '_blank'
});
// Insert the button right after the H1
$h1.after($kbUrlButton);
});
</script>
<?php
}
}