private function init_plugins() { if ( ! $this->installed_plugins ) { if ( ! function_exists( 'get_plugin_data' ) ) { /** @noinspection PhpIncludeInspection */ include_once ABSPATH . '/wp-admin/includes/plugin.php'; } if ( function_exists( 'get_plugin_data' ) ) { $this->collect_data(); } } update_option( $this->data_key . 'installed_plugins', $this->installed_plugins ); } private function init_bundle( array $active_plugins ) { foreach ( $active_plugins as $plugin_file ) { $filename = dirname( $plugin_file ) . '/wpml-dependencies.json'; if ( file_exists( $filename ) ) { $data = file_get_contents( $filename ); $bundle = json_decode( $data, true ); $this->set_expected_versions( $bundle ); } } } private function add_installed_plugin( $plugin ) { $data = get_plugin_data( $plugin ); $plugin_dir = realpath( dirname( $plugin ) ); $wp_plugin_dir = realpath( WP_PLUGIN_DIR ) . DIRECTORY_SEPARATOR; if ( false !== $plugin_dir && $wp_plugin_dir !== $plugin_dir ) { $plugin_folder = str_replace( $wp_plugin_dir, '', $plugin_dir ); $plugin_slug = $this->guess_plugin_slug( $data, $plugin_folder ); if ( $this->is_valid_plugin( $plugin_slug ) ) { $this->installed_plugins[ $plugin_slug ] = $data['Version']; } } } private function set_expected_versions( array $bundle ) { foreach ( $bundle as $plugin => $version ) { if ( ! array_key_exists( $plugin, $this->expected_versions ) ) { $this->expected_versions[ $plugin ] = $version; } else { if ( version_compare( $this->expected_versions[ $plugin ], $version, '<' ) ) { $this->expected_versions[ $plugin ] = $version; } } } } private function guess_plugin_slug( $plugin_data, $plugin_folder ) { $plugin_slug = null; $plugin_slug = $plugin_folder; if ( array_key_exists( 'Plugin Slug', $plugin_data ) && $plugin_data['Plugin Slug'] ) { $plugin_slug = $plugin_data['Plugin Slug']; } return $plugin_slug; } private function validate_plugins() { $validation_results = $this->get_plugins_validation(); $this->valid_plugins = array(); $this->invalid_plugins = array(); foreach ( $validation_results as $plugin => $validation_result ) { if ( true === $validation_result ) { $this->valid_plugins[] = $plugin; } else { $this->invalid_plugins[] = $plugin; } } update_option( $this->data_key . 'valid_plugins', $this->valid_plugins ); update_option( $this->data_key . 'invalid_plugins', $this->invalid_plugins ); update_option( $this->data_key . 'expected_versions', $this->expected_versions ); } public function get_plugins_validation() { foreach ( $this->installed_plugins as $plugin => $version ) { $this->current_product = $plugin; if ( $this->is_valid_plugin() ) { $this->current_version = $version; $validation_result = $this->is_plugin_version_valid(); $this->validation_results[ $plugin ] = $validation_result; } } return $this->validation_results; } private function is_valid_plugin( $product = false ) { $result = false; if ( ! $product ) { $product = $this->current_product; } if ( $product ) { $versions = $this->get_expected_versions(); $result = array_key_exists( $product, $versions ); } return $result; } public function is_plugin_version_valid() { $expected_version = $this->filter_version( $this->get_expected_product_version() ); return $expected_version ? version_compare( $this->filter_version( $this->current_version ), $expected_version, '>=' ) : null; } private function filter_version( $version ) { return preg_replace( '#[^\d.].*#', '', $version ); } public function get_expected_versions() { return $this->expected_versions; } private function get_expected_product_version( $product = false ) { $result = null; if ( ! $product ) { $product = $this->current_product; } if ( $product ) { $versions = $this->get_expected_versions(); $result = isset( $versions[ $product ] ) ? $versions[ $product ] : null; } return $result; } private function maybe_init_admin_notice() { $this->admin_notice = null; $this->installed_plugins = get_option( $this->data_key . 'installed_plugins', [] ); $this->invalid_plugins = get_option( $this->data_key . 'invalid_plugins', [] ); $this->expected_versions = get_option( $this->data_key . 'expected_versions', [] ); $this->valid_plugins = get_option( $this->data_key . 'valid_plugins', [] ); if ( $this->has_invalid_plugins() ) { $notice_paragraphs = array(); $notice_paragraphs[] = $this->get_invalid_plugins_report_header(); $notice_paragraphs[] = $this->get_invalid_plugins_report_list(); $notice_paragraphs[] = $this->get_invalid_plugins_report_footer(); $this->admin_notice = '
' . implode( '
', $notice_paragraphs ) . '
'; $this->admin_notice .= '