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(); |
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
|
|
|
* @since 6.0.0 |
32
|
|
|
* |
33
|
|
|
* @return bool | array false | Array of licensed and unlicensed Addons. |
34
|
|
|
*/ |
35
|
|
|
function monsterinsights_get_addons() { |
36
|
|
|
|
37
|
|
|
// Get license key and type. |
38
|
|
|
$key = is_network_admin() ? MonsterInsights()->license->get_network_license_key() : MonsterInsights()->license->get_site_license_key(); |
|
|
|
|
39
|
|
|
$type = is_network_admin() ? MonsterInsights()->license->get_network_license_type() : MonsterInsights()->license->get_site_license_type(); |
40
|
|
|
|
41
|
|
|
// Get addons data from transient or perform API query if no transient. |
42
|
|
|
if ( false === ( $addons = get_transient( '_monsterinsights_addons' ) ) ) { |
43
|
|
|
$addons = monsterinsights_get_addons_data( $key ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// If no Addons exist, return false |
47
|
|
|
if ( ! $addons ) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Iterate through Addons, to build two arrays: |
52
|
|
|
// - Addons the user is licensed to use, |
53
|
|
|
// - Addons the user isn't licensed to use. |
54
|
|
|
$results = array( |
55
|
|
|
'licensed' => array(), |
56
|
|
|
'unlicensed'=> array(), |
57
|
|
|
); |
58
|
|
|
foreach ( (array) $addons as $i => $addon ) { |
59
|
|
|
|
60
|
|
|
// Determine whether the user is licensed to use this Addon or not. |
61
|
|
|
if ( |
62
|
|
|
empty( $type ) || |
63
|
|
|
( in_array( 'Pro', $addon->categories ) && ( $type != 'pro' && $type != 'master' ) ) || |
64
|
|
|
( in_array( 'Plus', $addon->categories ) && $type != 'plus' && $type != 'pro' && $type != 'master' ) || |
65
|
|
|
( in_array( 'Basic', $addon->categories ) && ( $type != 'basic' && $type != 'plus' && $type != 'pro' && $type != 'master' ) ) |
66
|
|
|
) { |
67
|
|
|
// Unlicensed |
68
|
|
|
$results['unlicensed'][] = $addon; |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Licensed |
73
|
|
|
$results['licensed'][] = $addon; |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Return Addons, split by licensed and unlicensed. |
78
|
|
|
return $results; |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Pings the remote server for addons data. |
84
|
|
|
* |
85
|
|
|
* @since 6.0.0 |
86
|
|
|
* |
87
|
|
|
* @param string $key The user license key. |
88
|
|
|
* @return array Array of addon data otherwise. |
89
|
|
|
*/ |
90
|
|
|
function monsterinsights_get_addons_data( $key ) { |
91
|
|
|
$type = is_network_admin() ? MonsterInsights()->license->get_network_license_type() : MonsterInsights()->license->get_site_license_type(); |
|
|
|
|
92
|
|
|
|
93
|
|
|
// Get Addons |
94
|
|
|
// If the key is valid, we'll get personalised upgrade URLs for each Addon (if necessary) and plugin update information. |
95
|
|
|
if ( $key ) { |
96
|
|
|
$addons = MonsterInsights()->license_actions->perform_remote_request( 'get-addons-data-v600', array( 'tgm-updater-key' => $key ) ); |
97
|
|
|
} else { |
98
|
|
|
$addons = MonsterInsights()->license_actions->perform_remote_request( 'get-all-addons-data', array() ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// If there was an API error, set transient for only 10 minutes. |
102
|
|
|
if ( ! $addons ) { |
103
|
|
|
set_transient( '_monsterinsights_addons', false, 10 * MINUTE_IN_SECONDS ); |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// If there was an error retrieving the addons, set the error. |
108
|
|
|
if ( isset( $addons->error ) ) { |
|
|
|
|
109
|
|
|
set_transient( '_monsterinsights_addons', false, 10 * MINUTE_IN_SECONDS ); |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Otherwise, our request worked. Save the data and return it. |
114
|
|
|
set_transient( '_monsterinsights_addons', $addons, 4 * HOUR_IN_SECONDS ); |
115
|
|
|
return $addons; |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Retrieve the plugin basename from the plugin slug. |
121
|
|
|
* |
122
|
|
|
* @since 6.0.0 |
123
|
|
|
* |
124
|
|
|
* @param string $slug The plugin slug. |
125
|
|
|
* @return string The plugin basename if found, else the plugin slug. |
126
|
|
|
*/ |
127
|
|
|
function monsterinsights_get_plugin_basename_from_slug( $slug ) { |
128
|
|
|
$keys = array_keys( get_plugins() ); |
129
|
|
|
|
130
|
|
|
foreach ( $keys as $key ) { |
131
|
|
|
if ( preg_match( '|^' . $slug . '|', $key ) ) { |
132
|
|
|
return $key; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $slug; |
137
|
|
|
|
138
|
|
|
} |