class-capabilities.php
1 year ago
class-compatibility.php
1 year ago
class-install.php
1 year ago
class-uninstall.php
1 year ago
index.php
1 year ago
class-compatibility.php
79 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Installation Compatibility. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.50.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Installation; |
| 11 | |
| 12 | use AdvancedAds\Constants; |
| 13 | use AdvancedAds\Utilities\WordPress; |
| 14 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | /** |
| 19 | * Installation Compatibility. |
| 20 | */ |
| 21 | class Compatibility implements Integration_Interface { |
| 22 | |
| 23 | /** |
| 24 | * Hook into WordPress. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function hooks(): void { |
| 29 | if ( is_admin() ) { |
| 30 | add_action( 'plugin_loaded', [ $this, 'deactivate_plugins' ], 0 ); |
| 31 | } |
| 32 | add_filter( 'upgrader_post_install', [ $this, 'upgrader_post_install' ], 10, 3 ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Check if the plugin was updated and run compatibility checks. |
| 37 | * |
| 38 | * @param bool $response Installation response. |
| 39 | * @param array $hook_extra Extra arguments passed to hooked filters. |
| 40 | * @param array $result Installation result data. |
| 41 | * |
| 42 | * @return bool |
| 43 | */ |
| 44 | public function upgrader_post_install( $response, $hook_extra, $result ): bool { |
| 45 | if ( $response && isset( $result['source_files'] ) && in_array( 'advanced-ads.php', $result['source_files'], true ) ) { |
| 46 | $this->deactivate_plugins(); |
| 47 | } |
| 48 | |
| 49 | return $response; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Deactivate plugins that are not compatible with the current version of Advanced Ads. |
| 54 | * |
| 55 | * @return void |
| 56 | */ |
| 57 | public function deactivate_plugins(): void { |
| 58 | // Early bail!! |
| 59 | if ( get_option( 'advanced-ads-2-compatibility-flag' ) ) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | $plugins = WordPress::get_wp_plugins(); |
| 64 | foreach ( Constants::ADDONS_NON_COMPATIBLE_VERSIONS as $version => $slug ) { |
| 65 | $addon = $plugins[ $slug ] ?? null; |
| 66 | if ( ! $addon || ! is_plugin_active( $addon['file'] ) ) { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | if ( version_compare( $addon['version'], $version, '<=' ) ) { |
| 71 | \deactivate_plugins( plugin_basename( $addon['file'] ), true ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Add flag as if we upload or update the plugin using FTP this hook will not be triggered. |
| 76 | update_option( 'advanced-ads-2-compatibility-flag', true ); |
| 77 | } |
| 78 | } |
| 79 |