AdminBar.php
5.31 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
<?php
/**
* SearchWP AdminBar.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP\Admin;
use SearchWP\Utils;
use SearchWP\Settings;
use SearchWP\License;
/**
* Class AdminBar is responsible for implementing an entry in the WordPress Admin Bar.
*
* @since 4.0
*/
class AdminBar {
/**
* Constructor.
*
* @since 4.0
* @return void
*/
function __construct() {
add_action( 'wp_before_admin_bar_render', [ __CLASS__, 'render' ] );
add_filter( 'heartbeat_received', [ __CLASS__, 'heartbeat_received' ], 10, 2 );
}
/**
* Get notifications counter HTML.
*
* @since 4.2.8
*/
private static function get_notifications_counter_html() {
$style = '
display: inline-block;
min-width: 18px;
height: 18px;
border-radius: 9px;
margin: 7px 0 0 2px;
vertical-align: top;
font-size: 11px;
line-height: 1.6;
text-align: center;
';
$notifications_count = 0;
if ( License::inactive_license_notice() ) {
++ $notifications_count;
}
$notifications_count = apply_filters( 'searchwp\admin_bar\notifications_count', $notifications_count );
if ( empty( $notifications_count ) ) {
return '';
}
return '<span class="wp-ui-notification searchwp-menu-notification-counter" style="' . $style . '">' . absint( $notifications_count ) . '</span>';
}
/**
* Callback to implement the Admin Bar entry.
*
* @since 4.0
* @return void
*/
public static function render() {
global $wp_admin_bar, $pagenow, $post;
if ( ! is_admin_bar_showing() || ! apply_filters( 'searchwp\admin_bar', current_user_can( Settings::get_capability() ) ) ) {
return;
}
$algorithm_page_url = add_query_arg(
[ 'page' => 'searchwp-algorithm' ],
admin_url( 'admin.php' )
);
// Add base Admin Bar menu entry.
$wp_admin_bar->add_menu( [
'id' => Utils::$slug,
'title' => '<span>SearchWP</span> ' . self::get_notifications_counter_html(),
'href' => esc_url( $algorithm_page_url ),
] );
if ( License::inactive_license_notice() ) {
// Add link to Settings page.
$wp_admin_bar->add_menu( [
'parent' => Utils::$slug,
'id' => Utils::$slug . '_support',
'title' => '<span style="color: #ff6b6b; line-height: 1;">' . __( 'Activate License', 'searchwp' ) . '</span>',
'href' => esc_url( add_query_arg( [ 'page' => 'searchwp-settings' ], admin_url( 'admin.php' ) ) ),
] );
}
if ( apply_filters( 'searchwp\options\settings_screen', true ) ) {
// Add link to Algorithm page.
$wp_admin_bar->add_menu( [
'parent' => Utils::$slug,
'id' => Utils::$slug . '_settings',
'title' => __( 'Algorithm', 'searchwp' ),
'href' => esc_url( $algorithm_page_url ),
] );
}
if ( apply_filters( 'searchwp\admin_bar\statistics', true ) ) {
// Add link to Statistics page.
$wp_admin_bar->add_menu( [
'parent' => Utils::$slug,
'id' => Utils::$slug . '_statistics',
'title' => __( 'Statistics', 'searchwp' ),
'href' => esc_url( add_query_arg( [ 'page' => 'searchwp-statistics' ], admin_url( 'admin.php' ) ) ),
] );
}
do_action( 'searchwp\admin_bar\render', [ 'menu' => Utils::$slug ] );
// If this is not a post edit screen, bail out.
// TODO: This can be customized to account for any registered Source.
if ( 'post.php' !== $pagenow || ! $post instanceof \WP_Post ) {
return;
}
self::heartbeat_setup();
// Add entry showing the index status.
$wp_admin_bar->add_menu( [
'parent' => Utils::$slug,
'id' => Utils::$slug . '_index_status',
'title' => Utils::get_source_entry_index_status( 'post' . SEARCHWP_SEPARATOR . $post->post_type, $post->ID ),
] );
}
/**
* Setup updater and listener for Heartbeat API.
*
* @since 4.0
* @return void
*/
public static function heartbeat_setup() {
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'heartbeat' );
add_action( 'admin_print_footer_scripts', [ __CLASS__, 'heartbeat_index_status' ], 20 );
}
/**
* Update the index status Admin Bar entry alongside Heartbeat.
*
* @since 4.0
* @return void
*/
public static function heartbeat_index_status() {
global $post;
?>
<script>
(function($){
// Hook into the heartbeat-send.
$(document).on('heartbeat-send', function(e, data) {
data['searchwp_heartbeat_action'] = 'index_status';
data['searchwp_heartbeat_object'] = {
source: '<?php echo esc_js( 'post' . SEARCHWP_SEPARATOR . $post->post_type ); ?>',
id: '<?php echo esc_js( $post->ID ); ?>'
};
});
// listen for the custom event "heartbeat-tick" on $(document).
$(document).on( 'heartbeat-tick', function(e, data) {
if ( data['searchwp_index_status'] ) {
$('#wp-admin-bar-<?php echo esc_js( SEARCHWP_PREFIX ); ?>index_status > div').text( data['searchwp_index_status'] );
}
});
}(jQuery));
</script>
<?php
}
/**
* Append Source entry index status to Heartbeat updates.
*
* @since 4.0
* @param mixed $response
* @param mixed $data
* @return mixed|string[]
*/
public static function heartbeat_received( $response, $data ) {
if ( isset( $data['searchwp_heartbeat_action'] ) && 'index_status' == $data['searchwp_heartbeat_action'] ) {
$response['searchwp_index_status'] = Utils::get_source_entry_index_status(
$data['searchwp_heartbeat_object']['source'],
$data['searchwp_heartbeat_object']['id']
);
}
return $response;
}
}