|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Addons class. |
|
4
|
|
|
* |
|
5
|
|
|
* @since 6.0.0 |
|
6
|
|
|
* |
|
7
|
|
|
* @package MonsterInsights |
|
8
|
|
|
* @author Chris Christoff |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
// Exit if accessed directly |
|
12
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
13
|
|
|
exit; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Callback to output the MonsterInsights addons page. |
|
19
|
|
|
* |
|
20
|
|
|
* @since 6.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
function monsterinsights_addons_page() { |
|
23
|
|
|
echo monsterinsights_ublock_notice(); // phpcs:ignore |
|
24
|
|
|
monsterinsights_settings_error_page( 'monsterinsights-addons' ); |
|
25
|
|
|
monsterinsights_settings_inline_js(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Retrieves addons from the stored transient or remote server. |
|
30
|
|
|
* |
|
31
|
|
|
* @return bool | array false | Array of licensed and unlicensed Addons. |
|
32
|
|
|
* @since 6.0.0 |
|
33
|
|
|
* |
|
34
|
|
|
*/ |
|
35
|
|
|
function monsterinsights_get_addons() { |
|
36
|
|
|
|
|
37
|
|
|
// Get license key and type. |
|
38
|
|
|
$key = ''; |
|
39
|
|
|
$type = 'lite'; |
|
40
|
|
|
if ( monsterinsights_is_pro_version() ) { |
|
41
|
|
|
$key = is_network_admin() ? MonsterInsights()->license->get_network_license_key() : MonsterInsights()->license->get_site_license_key(); |
|
|
|
|
|
|
42
|
|
|
$type = is_network_admin() ? MonsterInsights()->license->get_network_license_type() : MonsterInsights()->license->get_site_license_type(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// Get addons data from transient or perform API query if no transient. |
|
46
|
|
|
if ( false === ( $addons = get_transient( '_monsterinsights_addons' ) ) ) { |
|
47
|
|
|
$addons = monsterinsights_get_addons_data( $key ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// If no Addons exist, return false |
|
51
|
|
|
if ( ! $addons ) { |
|
52
|
|
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Iterate through Addons, to build two arrays: |
|
56
|
|
|
// - Addons the user is licensed to use, |
|
57
|
|
|
// - Addons the user isn't licensed to use. |
|
58
|
|
|
$results = array( |
|
59
|
|
|
'licensed' => array(), |
|
60
|
|
|
'unlicensed' => array(), |
|
61
|
|
|
); |
|
62
|
|
|
foreach ( (array) $addons as $i => $addon ) { |
|
63
|
|
|
|
|
64
|
|
|
// Determine whether the user is licensed to use this Addon or not. |
|
65
|
|
|
if ( |
|
66
|
|
|
empty( $type ) || |
|
67
|
|
|
( in_array( 'Pro', $addon->categories ) && ( $type != 'pro' && $type != 'master' ) ) || |
|
68
|
|
|
( in_array( 'Plus', $addon->categories ) && $type != 'plus' && $type != 'pro' && $type != 'master' ) || |
|
69
|
|
|
( in_array( 'Basic', $addon->categories ) && ( $type != 'basic' && $type != 'plus' && $type != 'pro' && $type != 'master' ) ) |
|
70
|
|
|
) { |
|
71
|
|
|
// Unlicensed |
|
72
|
|
|
$results['unlicensed'][] = $addon; |
|
73
|
|
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Licensed |
|
77
|
|
|
$results['licensed'][] = $addon; |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// Return Addons, split by licensed and unlicensed. |
|
82
|
|
|
return $results; |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Pings the remote server for addons data. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $key The user license key. |
|
90
|
|
|
* |
|
91
|
|
|
* @return array Array of addon data otherwise. |
|
92
|
|
|
* @since 6.0.0 |
|
93
|
|
|
* |
|
94
|
|
|
*/ |
|
95
|
|
|
function monsterinsights_get_addons_data( $key ) { |
|
96
|
|
|
// Get Addons |
|
97
|
|
|
// If the key is valid, we'll get personalised upgrade URLs for each Addon (if necessary) and plugin update information. |
|
98
|
|
|
if ( monsterinsights_is_pro_version() && $key ) { |
|
99
|
|
|
$addons = monsterinsights_perform_remote_request( 'get-addons-data-v600', array( 'tgm-updater-key' => $key ) ); |
|
100
|
|
|
} else { |
|
101
|
|
|
$addons = monsterinsights_get_all_addons_data(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// If there was an API error, set transient for only 10 minutes. |
|
105
|
|
|
if ( ! $addons ) { |
|
106
|
|
|
set_transient( '_monsterinsights_addons', false, 10 * MINUTE_IN_SECONDS ); |
|
107
|
|
|
|
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// If there was an error retrieving the addons, set the error. |
|
112
|
|
|
if ( isset( $addons->error ) ) { |
|
|
|
|
|
|
113
|
|
|
set_transient( '_monsterinsights_addons', false, 10 * MINUTE_IN_SECONDS ); |
|
114
|
|
|
|
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// Otherwise, our request worked. Save the data and return it. |
|
119
|
|
|
set_transient( '_monsterinsights_addons', $addons, 4 * HOUR_IN_SECONDS ); |
|
120
|
|
|
|
|
121
|
|
|
return $addons; |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get all addons without a license, for lite users. |
|
127
|
|
|
* |
|
128
|
|
|
* @return array|bool|mixed|object |
|
129
|
|
|
*/ |
|
130
|
|
|
function monsterinsights_get_all_addons_data() { |
|
131
|
|
|
|
|
132
|
|
|
$body = array( |
|
133
|
|
|
'tgm-updater-action' => 'get-all-addons-data', |
|
134
|
|
|
'tgm-updater-key' => '', |
|
135
|
|
|
'tgm-updater-wp-version' => get_bloginfo( 'version' ), |
|
136
|
|
|
'tgm-updater-referer' => site_url(), |
|
137
|
|
|
'tgm-updater-mi-version' => MONSTERINSIGHTS_VERSION, |
|
138
|
|
|
'tgm-updater-is-pro' => false, |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
return monsterinsights_perform_remote_request( 'verify-key', $body ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
function monsterinsights_get_addon( $installed_plugins, $addons_type, $addon, $slug ) { |
|
145
|
|
|
$active = false; |
|
146
|
|
|
$installed = false; |
|
147
|
|
|
|
|
148
|
|
|
$slug = apply_filters( 'monsterinsights_addon_slug', $slug ); |
|
149
|
|
|
|
|
150
|
|
|
$plugin_basename = monsterinsights_get_plugin_basename_from_slug( $slug ); |
|
151
|
|
|
|
|
152
|
|
|
if ( isset( $installed_plugins[ $plugin_basename ] ) ) { |
|
153
|
|
|
$installed = true; |
|
154
|
|
|
|
|
155
|
|
|
if ( is_multisite() && is_network_admin() ) { |
|
156
|
|
|
$active = is_plugin_active_for_network( $plugin_basename ); |
|
157
|
|
|
} else { |
|
158
|
|
|
$active = is_plugin_active( $plugin_basename ); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
if ( empty( $addon->url ) ) { |
|
162
|
|
|
$addon->url = ''; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$active_version = false; |
|
166
|
|
|
if ( $active ) { |
|
167
|
|
|
if ( ! empty( $installed_plugins[ $plugin_basename ]['Version'] ) ) { |
|
168
|
|
|
$active_version = $installed_plugins[ $plugin_basename ]['Version']; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$addon->type = $addons_type; |
|
173
|
|
|
$addon->installed = $installed; |
|
174
|
|
|
$addon->active_version = $active_version; |
|
175
|
|
|
$addon->active = $active; |
|
176
|
|
|
$addon->basename = $plugin_basename; |
|
177
|
|
|
|
|
178
|
|
|
return $addon; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Retrieve the plugin basename from the plugin slug. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $slug The plugin slug. |
|
185
|
|
|
* |
|
186
|
|
|
* @return string The plugin basename if found, else the plugin slug. |
|
187
|
|
|
* @since 6.0.0 |
|
188
|
|
|
* |
|
189
|
|
|
*/ |
|
190
|
|
|
function monsterinsights_get_plugin_basename_from_slug( $slug ) { |
|
191
|
|
|
$keys = array_keys( get_plugins() ); |
|
192
|
|
|
|
|
193
|
|
|
foreach ( $keys as $key ) { |
|
194
|
|
|
if ( preg_match( '|^' . $slug . '|', $key ) ) { |
|
195
|
|
|
return $key; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return $slug; |
|
200
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|