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
|
<?php /** * WPSEO plugin file. * * @package WPSEO\Admin */
/** * Represents the class that contains the available extensions for Yoast SEO. */ class WPSEO_Extension_Manager {
/** * The transient key to save the cache in. * * @var string */ const TRANSIENT_CACHE_KEY = 'wpseo_license_active_extensions';
/** * @var WPSEO_Extension[] */ protected $extensions = array();
/** * List of active plugins. * * @var array */ protected static $active_extensions;
/** * Adds an extension to the manager. * * @param string $extension_name The extension name. * @param WPSEO_Extension $extension The extension value object. * * @return void */ public function add( $extension_name, WPSEO_Extension $extension = null ) { $this->extensions[ $extension_name ] = $extension; }
/** * Removes an extension from the manager. * * @param string $extension_name The name of the extension to remove. * * @return void */ public function remove( $extension_name ) { if ( array_key_exists( $extension_name, $this->extensions ) ) { unset( $this->extensions[ $extension_name ] ); } }
/** * Returns the extension for the given extension name. * * @param string $extension_name The name of the extension to get. * * @return null|WPSEO_Extension The extension object or null when it doesn't exist. */ public function get( $extension_name ) { if ( array_key_exists( $extension_name, $this->extensions ) ) { return $this->extensions[ $extension_name ]; }
return null; }
/** * Returns all set extension. * * @return WPSEO_Extension[] Array with the extensions. */ public function get_all() { return $this->extensions; }
/** * Checks if the plugin is activated within My Yoast. * * @param string $extension_name The extension name to check. * * @return bool True when the plugin is activated. */ public function is_activated( $extension_name ) { if ( self::$active_extensions === null ) { // Force re-check on license & dashboard pages. $current_page = $this->get_current_page();
// Check whether the licenses are valid or whether we need to show notifications. $exclude_cache = ( $current_page === 'wpseo_licenses' || $current_page === 'wpseo_dashboard' );
// Fetch transient data on any other page. if ( ! $exclude_cache ) { self::$active_extensions = $this->get_cached_extensions(); }
// If the active extensions is still NULL, we need to set it. if ( ! is_array( self::$active_extensions ) ) { self::$active_extensions = $this->retrieve_active_extensions();
$this->set_cached_extensions( self::$active_extensions ); } }
return in_array( $extension_name, self::$active_extensions, true ); }
/** * Retrieves the active extensions via an external request. * * @return array Array containing the active extensions. */ protected function retrieve_active_extensions() { return (array) apply_filters( 'yoast-active-extensions', array() ); }
/** * Returns the current page. * * @return string The current page. */ protected function get_current_page() { return filter_input( INPUT_GET, 'page' ); }
/** * Gets a cached list of active extensions. * * @return boolean|array The cached extensions. */ protected function get_cached_extensions() { return get_transient( self::TRANSIENT_CACHE_KEY ); }
/** * Sets the active extensions transient for the set duration. * * @param array $extensions The extensions to add. * @param int $duration The duration that the list of extensions needs to remain cached. * * @return void */ protected function set_cached_extensions( $extensions, $duration = DAY_IN_SECONDS ) { set_transient( self::TRANSIENT_CACHE_KEY, $extensions, $duration ); } }
|