PageRenderTime 49ms CodeModel.GetById 16ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/wordpress-seo/admin/class-plugin-availability.php

https://bitbucket.org/carloskikea/helpet
PHP | 344 lines | 183 code | 38 blank | 123 comment | 11 complexity | 00392a56ba82802e482174a88c044bfc MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO\Plugin_Availability
  6 */
  7
  8/**
  9 * Class WPSEO_Plugin_Availability
 10 */
 11class WPSEO_Plugin_Availability {
 12
 13	/**
 14	 * @var array
 15	 */
 16	protected $plugins = array();
 17
 18	/**
 19	 * Registers the plugins so we can access them.
 20	 */
 21	public function register() {
 22		$this->register_yoast_plugins();
 23		$this->register_yoast_plugins_status();
 24	}
 25
 26	/**
 27	 * Registers all the available Yoast SEO plugins.
 28	 */
 29	protected function register_yoast_plugins() {
 30		$this->plugins = array(
 31			'yoast-seo-premium' => array(
 32				'url'          => WPSEO_Shortlinker::get( 'https://yoa.st/1y7' ),
 33				'title'        => 'Yoast SEO Premium',
 34				'description'  => sprintf(
 35					/* translators: %1$s expands to Yoast SEO */
 36					__( 'The premium version of %1$s with more features & support.', 'wordpress-seo' ),
 37					'Yoast SEO'
 38				),
 39				'installed'    => false,
 40				'slug'         => 'wordpress-seo-premium/wp-seo-premium.php',
 41				'version_sync' => true,
 42				'premium'      => true,
 43			),
 44
 45			'video-seo-for-wordpress-seo-by-yoast' => array(
 46				'url'          => WPSEO_Shortlinker::get( 'https://yoa.st/1y8' ),
 47				'title'        => 'Video SEO',
 48				'description'  => __( 'Optimize your videos to show them off in search results and get more clicks!', 'wordpress-seo' ),
 49				'installed'    => false,
 50				'slug'         => 'wpseo-video/video-seo.php',
 51				'version_sync' => true,
 52				'premium'      => true,
 53			),
 54
 55			'yoast-news-seo' => array(
 56				'url'          => WPSEO_Shortlinker::get( 'https://yoa.st/1y9' ),
 57				'title'        => 'News SEO',
 58				'description'  => __( 'Are you in Google News? Increase your traffic from Google News by optimizing for it!', 'wordpress-seo' ),
 59				'installed'    => false,
 60				'slug'         => 'wpseo-news/wpseo-news.php',
 61				'version_sync' => true,
 62				'premium'      => true,
 63			),
 64
 65			'local-seo-for-yoast-seo' => array(
 66				'url'          => WPSEO_Shortlinker::get( 'https://yoa.st/1ya' ),
 67				'title'        => 'Local SEO',
 68				'description'  => __( 'Rank better locally and in Google Maps, without breaking a sweat!', 'wordpress-seo' ),
 69				'installed'    => false,
 70				'slug'         => 'wordpress-seo-local/local-seo.php',
 71				'version_sync' => true,
 72				'premium'      => true,
 73			),
 74
 75			'yoast-woocommerce-seo' => array(
 76				'url'           => WPSEO_Shortlinker::get( 'https://yoa.st/1o0' ),
 77				'title'         => 'Yoast WooCommerce SEO',
 78				'description'   => sprintf(
 79					/* translators: %1$s expands to Yoast SEO */
 80					__( 'Seamlessly integrate WooCommerce with %1$s and get extra features!', 'wordpress-seo' ),
 81					'Yoast SEO'
 82				),
 83				'_dependencies' => array(
 84					'WooCommerce' => array(
 85						'slug' => 'woocommerce/woocommerce.php',
 86					),
 87				),
 88				'installed'     => false,
 89				'slug'          => 'wpseo-woocommerce/wpseo-woocommerce.php',
 90				'version_sync'  => true,
 91				'premium'       => true,
 92			),
 93
 94			'yoast-acf-analysis' => array(
 95				'url'           => 'https://wordpress.org/plugins/acf-content-analysis-for-yoast-seo/',
 96				'title'         => 'ACF Content Analysis for Yoast SEO',
 97				'description'   => sprintf(
 98					/* translators: %1$s expands to Yoast SEO, %2$s expands to Advanced Custom Fields */
 99					__( 'Seamlessly integrate %2$s with %1$s for the content analysis!', 'wordpress-seo' ),
100					'Yoast SEO',
101					'Advanced Custom Fields'
102				),
103				'installed'     => false,
104				'slug'          => 'acf-content-analysis-for-yoast-seo/yoast-acf-analysis.php',
105				'_dependencies' => array(
106					'Advanced Custom Fields' => array(
107						'slug' => 'advanced-custom-fields/acf.php',
108					),
109				),
110				'version_sync'  => false,
111			),
112
113			'yoastseo-amp' => array(
114				'url'           => 'https://wordpress.org/plugins/glue-for-yoast-seo-amp/',
115				'title'         => 'Yoast SEO AMP Glue',
116				'description'   => sprintf(
117					/* translators: %1$s expands to Yoast SEO */
118					__( 'Seamlessly integrate %1$s into your AMP pages!', 'wordpress-seo' ), 'Yoast SEO'
119				),
120				'installed'     => false,
121				'slug'          => 'glue-for-yoast-seo-amp/yoastseo-amp.php',
122				'_dependencies' => array(
123					'AMP' => array(
124						'slug' => 'amp/amp.php',
125					),
126				),
127				'version_sync'  => false,
128			),
129		);
130	}
131
132	/**
133	 * Sets certain plugin properties based on WordPress' status.
134	 */
135	protected function register_yoast_plugins_status() {
136
137		foreach ( $this->plugins as $name => $plugin ) {
138
139			$plugin_slug = $plugin['slug'];
140			$plugin_path = WP_PLUGIN_DIR . '/' . $plugin_slug;
141
142			if ( file_exists( $plugin_path ) ) {
143				$plugin_data                         = get_plugin_data( $plugin_path, false, false );
144				$this->plugins[ $name ]['installed'] = true;
145				$this->plugins[ $name ]['version']   = $plugin_data['Version'];
146				$this->plugins[ $name ]['active']    = is_plugin_active( $plugin_slug );
147			}
148		}
149	}
150
151	/**
152	 * Checks whether or not a plugin is known within the Yoast SEO collection.
153	 *
154	 * @param {string} $plugin The plugin to search for.
155	 *
156	 * @return bool Whether or not the plugin is exists.
157	 */
158	protected function plugin_exists( $plugin ) {
159		return isset( $this->plugins[ $plugin ] );
160	}
161
162	/**
163	 * Gets all the possibly available plugins.
164	 *
165	 * @return array Array containing the information about the plugins.
166	 */
167	public function get_plugins() {
168		return $this->plugins;
169	}
170
171	/**
172	 * Gets a specific plugin. Returns an empty array if it cannot be found.
173	 *
174	 * @param {string} $plugin The plugin to search for.
175	 *
176	 * @return array The plugin properties.
177	 */
178	public function get_plugin( $plugin ) {
179		if ( ! $this->plugin_exists( $plugin ) ) {
180			return array();
181		}
182
183		return $this->plugins[ $plugin ];
184	}
185
186	/**
187	 * Gets the version of the plugin.
188	 *
189	 * @param {string} $plugin The plugin to search for.
190	 *
191	 * @return string The version associated with the plugin.
192	 */
193	public function get_version( $plugin ) {
194		if ( ! isset( $plugin['version'] ) ) {
195			return '';
196		}
197
198		return $plugin['version'];
199	}
200
201	/**
202	 * Checks if there are dependencies available for the plugin.
203	 *
204	 * @param {string} $plugin The plugin to search for.
205	 *
206	 * @return bool Whether or not there is a dependency present.
207	 */
208	public function has_dependencies( $plugin ) {
209		return ( isset( $plugin['_dependencies'] ) && ! empty( $plugin['_dependencies'] ) );
210	}
211
212	/**
213	 * Gets the dependencies for the plugin.
214	 *
215	 * @param {string} $plugin The plugin to search for.
216	 *
217	 * @return array Array containing all the dependencies associated with the plugin.
218	 */
219	public function get_dependencies( $plugin ) {
220		if ( ! $this->has_dependencies( $plugin ) ) {
221			return array();
222		}
223
224		return $plugin['_dependencies'];
225	}
226
227	/**
228	 * Checks if all dependencies are satisfied.
229	 *
230	 * @param {string} $plugin The plugin to search for.
231	 *
232	 * @return bool Whether or not the dependencies are satisfied.
233	 */
234	public function dependencies_are_satisfied( $plugin ) {
235		if ( ! $this->has_dependencies( $plugin ) ) {
236			return true;
237		}
238
239		$dependencies           = $this->get_dependencies( $plugin );
240		$installed_dependencies = array_filter( $dependencies, array( $this, 'is_dependency_available' ) );
241
242		return count( $installed_dependencies ) === count( $dependencies );
243	}
244
245	/**
246	 * Checks whether or not one of the plugins is properly installed and usable.
247	 *
248	 * @param {string} $plugin The plugin to search for.
249	 *
250	 * @return bool Whether or not the plugin is properly installed.
251	 */
252	public function is_installed( $plugin ) {
253		if ( empty( $plugin ) ) {
254			return false;
255		}
256
257		return $this->is_available( $plugin );
258	}
259
260	/**
261	 * Gets all installed plugins.
262	 *
263	 * @return array The installed plugins.
264	 */
265	public function get_installed_plugins() {
266		$installed = array();
267
268		foreach ( $this->plugins as $plugin_key => $plugin ) {
269			if ( $this->is_installed( $plugin ) ) {
270				$installed[ $plugin_key ] = $plugin;
271			}
272		}
273
274		return $installed;
275	}
276
277	/**
278	 * Checks for the availability of the plugin.
279	 *
280	 * @param {string} $plugin The plugin to search for.
281	 *
282	 * @return bool Whether or not the plugin is available.
283	 */
284	public function is_available( $plugin ) {
285		return isset( $plugin['installed'] ) && $plugin['installed'] === true;
286	}
287
288	/**
289	 * Checks whether a dependency is available.
290	 *
291	 * @param {string} $dependency The dependency to look for.
292	 *
293	 * @return bool Whether or not the dependency is available.
294	 */
295	public function is_dependency_available( $dependency ) {
296		return in_array( $dependency['slug'], array_keys( get_plugins() ), true );
297	}
298
299	/**
300	 * Gets the names of the dependencies.
301	 *
302	 * @param array $plugin The plugin to get the dependency names from.
303	 *
304	 * @return array Array containing the names of the associated dependencies.
305	 */
306	public function get_dependency_names( $plugin ) {
307		if ( ! $this->has_dependencies( $plugin ) ) {
308			return array();
309		}
310
311		return array_keys( $plugin['_dependencies'] );
312	}
313
314	/**
315	 * Gets an array of plugins that have defined dependencies.
316	 *
317	 * @return array Array of the plugins that have dependencies.
318	 */
319	public function get_plugins_with_dependencies() {
320		return array_filter( $this->plugins, array( $this, 'has_dependencies' ) );
321	}
322
323	/**
324	 * Determines whether or not a plugin is active.
325	 *
326	 * @param string $plugin The plugin slug to check.
327	 *
328	 * @return bool Whether or not the plugin is active.
329	 */
330	public function is_active( $plugin ) {
331		return is_plugin_active( $plugin );
332	}
333
334	/**
335	 * Determines whether or not a plugin is a Premium product.
336	 *
337	 * @param array $plugin The plugin to check.
338	 *
339	 * @return bool Whether or not the plugin is a Premium product.
340	 */
341	public function is_premium( $plugin ) {
342		return isset( $plugin['premium'] ) && $plugin['premium'] === true;
343	}
344}