-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathappsero.php
202 lines (167 loc) · 4.68 KB
/
appsero.php
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
<?php
/**
* Appsero functions.
*
* @package W4_Post_List
*/
/**
* Appsero insights instance
*/
function w4pl_appsero_insights_instance() {
static $insights = null;
if ( $insights === null && class_exists( 'Appsero\Client' ) ) {
$client = new Appsero\Client(
'fc943997-f146-4d10-867c-115d155c7fdd',
'W4 Post List',
W4PL_PLUGIN_FILE
);
$insights = $client->insights();
}
return $insights;
}
/**
* Appsero initializion function
*/
function w4pl_appsero_init() {
$insights = w4pl_appsero_insights_instance();
if ( $insights !== null ) {
$insights
->add_extra( 'w4pl_insights_extra' )
->add_plugin_data()
->hide_notice() // Hide the default notice.
->init();
// Display the modified notice.
add_action( 'admin_notices', 'w4pl_appsero_admin_notices' );
}
}
w4pl_appsero_init();
/**
* Appsero initializion function
*/
function w4pl_appsero_admin_notices() {
global $pagenow, $typenow;
$insights = w4pl_appsero_insights_instance();
if ( $insights === null ) {
return;
}
if (
( in_array( $pagenow, [ 'edit.php' ], true ) && 'w4pl' === $typenow )
) {
$insights->notice = __( 'Allow us to understand how we can improve W4 Post List Plugin by collecting some diagnostic data and usage information.' );
ob_start();
$insights->admin_notice();
$notice = ob_get_clean();
// Remove the privacy policy link.
$notice = str_replace(
[
', Your name and email address',
' <a href="https://appsero.com/privacy-policy/" target="_blank">Learn more</a> about how Appsero collects and handle your data.',
],
'',
$notice
);
$allowed_html = wp_kses_allowed_html( 'post' );
$allowed_html['script'] = [ 'type' => true, 'src' => true, 'async' => true, 'defer' => true ];
$notice = wp_kses( $notice, $allowed_html );
// Not sure why kses is not working for style attribute.
$notice = str_replace(
'<p class="description">',
'<p class="description" style="display:none">',
$notice
);
echo $notice;
}
}
/**
* Admin footer styles for plugins.php page
*/
function w4pl_appsero_admin_footer() {
global $pagenow;
if ( 'plugins.php' !== $pagenow ) {
return;
}
echo '<style>.wd-dr-modal-body{height:280px; overflow-x:auto;}.wd-dr-modal-body textarea{max-width:100%;}</style>';
}
add_action( 'admin_footer', 'w4pl_appsero_admin_footer', 11 );
/**
* Appsero extradata callback function
*
* @return array Possible extra data.
*/
function w4pl_insights_extra() {
global $wpdb;
$extra = [
'ListCount' => 0,
'Editor' => 'Nop',
'Elementor' => 'Nop',
'VisualComposer' => 'Nop',
];
$counts = wp_count_posts( 'w4pl' );
if ( isset( $counts->publish ) || isset( $counts->private ) || isset( $counts->pending ) || isset( $counts->draft ) ) {
$extra['ListCount'] = $counts->publish + $counts->private + $counts->pending + $counts->draft;
}
global $wp_version;
if ( defined( 'GUTENBERG_VERSION' ) ) {
$extra['Editor'] = 'Block';
} elseif ( version_compare( $wp_version, '5.0.0', '>=' ) ) {
if ( class_exists( 'Classic_Editor' ) ) {
$extra['Editor'] = 'Classic';
} else {
$extra['Editor'] = 'Block';
}
}
if ( defined( 'ELEMENTOR_VERSION' ) ) {
$extra['Elementor'] = 'Active';
}
if ( defined( 'WPB_VC_VERSION' ) ) {
$extra['VisualComposer'] = 'Active';
}
return $extra;
}
/**
* We don't need admin name & email, better exclude them.
*
* @param array $args Arguments.
* @param string $url Request url.
* @return array [description].
*/
function w4pl_filter_appsero_request( $args, $url ) {
if ( 0 === strpos( $url, 'https://api.appsero.com' ) ) {
if ( ! empty( $args['body'] ) ) {
if ( isset( $args['body']['admin_email'] ) ) {
$args['body']['admin_email'] = 'anonymous@example.com';
}
if ( isset( $args['body']['user_email'] ) ) {
$args['body']['user_email'] = 'anonymous@example.com';
}
if ( isset( $args['body']['first_name'] ) ) {
$args['body']['first_name'] = 'John';
}
if ( isset( $args['body']['first_name'] ) ) {
$args['body']['last_name'] = 'Doe';
}
if ( isset( $args['body']['ip_address'] ) ) {
$args['body']['ip_address'] = '192.168.' . wp_rand( 0, 255 ) . '.' . wp_rand( 0, 255 );
}
if ( isset( $args['body']['extra'] ) ) {
$extra = sprintf(
'%d, %s',
$args['body']['extra']['ListCount'],
$args['body']['extra']['Editor']
);
foreach ( array( 'Elementor', 'VisualComposer' ) as $param ) {
if ( 'Active' === $args['body']['extra'][ $param ] ) {
$extra .= ', ' . $param;
}
}
$args['body']['site'] = sprintf(
'%s - %s',
substr( $args['body']['site'], 0, 30 ),
$extra
);
}
}
}
return $args;
}
add_filter( 'http_request_args', 'w4pl_filter_appsero_request', 10, 2 );