Skip to content

Commit 1bf7383

Browse files
committed
New. Hook. Filter user roles who can see the dashboard widget.
1 parent 7fc6d6d commit 1bf7383

File tree

1 file changed

+61
-40
lines changed

1 file changed

+61
-40
lines changed

inc/cleantalk-admin.php

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,16 @@ function ct_dashboard_statistics_widget()
9595
if (isset($apbct->data['wl_brandname']) && $apbct->data['wl_brandname'] !== APBCT_NAME) {
9696
$actual_plugin_name = $apbct->data['wl_brandname'];
9797
}
98+
/**
99+
* Hook. List of allowed user roles for the Dashboard widget.
100+
* add_filter('apbct_hook_dashboard_widget_allowed_roles_list', function($roles_list) {
101+
* $roles_list[] = 'editor';
102+
* return $roles_list;
103+
* });
104+
*/
105+
$roles_list = apply_filters('apbct_hook_dashboard_widget_allowed_roles_list', array('administrator'));
98106

99-
if ( apbct_is_user_role_in(array('administrator')) ) {
107+
if (is_array($roles_list) && apbct_is_user_role_in($roles_list) ) {
100108
wp_add_dashboard_widget(
101109
'ct_dashboard_statistics_widget',
102110
$actual_plugin_name,
@@ -172,7 +180,7 @@ function ct_dashboard_statistics_widget_output($_post, $_callback_args)
172180
"<u>{$apbct->brief_data['error']}</u>"
173181
)
174182
. '</h2>';
175-
if ( $apbct->user_token && ! $apbct->white_label ) {
183+
if (apbct_is_user_role_in(array('administrator')) && $apbct->user_token && ! $apbct->white_label ) {
176184
$link = LinkConstructor::buildCleanTalkLink(
177185
'anti_crawler_inactive',
178186
'my',
@@ -225,7 +233,7 @@ function ct_dashboard_statistics_widget_output($_post, $_callback_args)
225233
} ?>
226234
</table>
227235
<?php
228-
if ( $apbct->user_token && ! $apbct->data["wl_mode_enabled"] ) {
236+
if (apbct_is_user_role_in(array('administrator')) && $apbct->user_token && ! $apbct->data["wl_mode_enabled"] ) {
229237
$link = LinkConstructor::buildCleanTalkLink(
230238
'dashboard_widget_all_data_link',
231239
'my/show_requests',
@@ -244,50 +252,55 @@ function ct_dashboard_statistics_widget_output($_post, $_callback_args)
244252
<?php
245253
}
246254
// Notice at the bottom
247-
if ( isset($current_user) && in_array('administrator', $current_user->roles) ) {
248-
if ( $apbct->spam_count && $apbct->spam_count > 0 ) {
255+
if ( $apbct->spam_count && $apbct->spam_count > 0 ) {
256+
$cp_total_stats = '';
257+
//Link to CP is only for admins due the token provided
258+
if ( apbct_is_user_role_in(array('administrator')) ) {
249259
$link = LinkConstructor::buildCleanTalkLink(
250260
'dashboard_widget_go_to_cp',
251261
'my',
252262
array(
253263
'user_token' => $apbct->user_token,
254-
'cp_mode' => 'antispam'
264+
'cp_mode' => 'antispam'
255265
)
256266
);
257-
echo '<div class="ct_widget_wprapper_total_blocked">'
258-
. ($apbct->data["wl_mode_enabled"] ? '' : '<img src="' . Escape::escUrl($apbct->logo__small__colored) . '" class="ct_widget_small_logo"/>')
259-
. '<span title="' . sprintf(
260-
__(
261-
'This is the count from the %s\'s cloud and could be different to admin bar counters',
262-
'cleantalk-spam-protect'
263-
) . '">',
264-
$actual_plugin_name
265-
)
266-
. sprintf(
267-
/* translators: %s: Number of spam messages */
268-
__(
269-
'%s%s%s has blocked %s spam for past year. The statistics are automatically updated every 24 hours.',
270-
'cleantalk-spam-protect'
271-
),
272-
! $apbct->data["wl_mode_enabled"] ? '<a href="' . $link . '" target="_blank">' : '',
273-
$actual_plugin_name,
274-
! $apbct->data["wl_mode_enabled"] ? '</a>' : '',
275-
number_format($apbct->data['spam_count'], 0, ',', ' ')
276-
)
277-
. '</span>'
278-
. (! $apbct->white_label && ! $apbct->data["wl_mode_enabled"]
279-
? '<br><br>'
280-
. '<b style="font-size: 16px;">'
281-
. sprintf(
282-
__('Do you like CleanTalk? %sPost your feedback here%s.', 'cleantalk-spam-protect'),
283-
'<u><a href="https://wordpress.org/support/plugin/cleantalk-spam-protect/reviews/#new-post" target="_blank">',
284-
'</a></u>'
285-
)
286-
. '</b>'
287-
: ''
288-
)
289-
. '</div>';
267+
$cp_total_stats =
268+
($apbct->data["wl_mode_enabled"] ? '' : '<img src="' . Escape::escUrl($apbct->logo__small__colored) . '" class="ct_widget_small_logo"/>')
269+
. '<span title="'
270+
. sprintf(
271+
__(
272+
'This is the count from the %s\'s cloud and could be different to admin bar counters',
273+
'cleantalk-spam-protect'
274+
) . '">',
275+
$actual_plugin_name
276+
)
277+
. sprintf(
278+
/* translators: %s: Number of spam messages */
279+
__(
280+
'%s%s%s has blocked %s spam for past year. The statistics are automatically updated every 24 hours.',
281+
'cleantalk-spam-protect'
282+
),
283+
! $apbct->data["wl_mode_enabled"] ? '<a href="' . $link . '" target="_blank">' : '',
284+
$actual_plugin_name,
285+
! $apbct->data["wl_mode_enabled"] ? '</a>' : '',
286+
number_format($apbct->data['spam_count'], 0, ',', ' ')
287+
)
288+
. '</span>';
290289
}
290+
echo '<div class="ct_widget_wprapper_total_blocked">'
291+
. $cp_total_stats
292+
. (! $apbct->white_label && ! $apbct->data["wl_mode_enabled"]
293+
? '<br><br>'
294+
. '<b style="font-size: 16px;">'
295+
. sprintf(
296+
__('Do you like CleanTalk? %sPost your feedback here%s.', 'cleantalk-spam-protect'),
297+
'<u><a href="https://wordpress.org/support/plugin/cleantalk-spam-protect/reviews/#new-post" target="_blank">',
298+
'</a></u>'
299+
)
300+
. '</b>'
301+
: ''
302+
)
303+
. '</div>';
291304
}
292305
echo '</div>';
293306
}
@@ -527,10 +540,18 @@ function apbct_admin__enqueue_scripts($hook)
527540
$data = array_merge($data, EmailEncoder::getLocalizationText());
528541
wp_localize_script('cleantalk-admin-js', 'ctAdminCommon', $data);
529542

543+
/**
544+
* Hook. List of allowed user roles for the Dashboard widget.
545+
* add_filter('apbct_hook_dashboard_widget_allowed_roles_list', function($roles_list) {
546+
* $roles_list[] = 'editor';
547+
* return $roles_list;
548+
* });
549+
*/
550+
$roles_list = apply_filters('apbct_hook_dashboard_widget_allowed_roles_list', array('administrator'));
530551
// DASHBOARD page JavaScript and CSS
531552
if (
532553
$hook == 'index.php' &&
533-
apbct_is_user_role_in(array('administrator')) &&
554+
is_array($roles_list) && apbct_is_user_role_in($roles_list) &&
534555
$apbct->settings['wp__dashboard_widget__show'] &&
535556
! $apbct->moderate_ip
536557
) {

0 commit comments

Comments
 (0)