}> */ private $problems = array(); /** * Runs the checks and returns whether all passed. * * @return bool */ public function are_met() { $this->problems = array(); if ( ! class_exists( 'WooCommerce' ) || ! defined( 'WC_VERSION' ) ) { $this->problems[] = array( 'wc_missing', array() ); } elseif ( version_compare( WC_VERSION, self::MIN_WC_VERSION, '<' ) ) { $this->problems[] = array( 'wc_version', array( 'required' => self::MIN_WC_VERSION, 'current' => WC_VERSION, ), ); } if ( ! class_exists( 'WC_Subscriptions' ) || ! function_exists( 'wcs_get_subscriptions' ) ) { $this->problems[] = array( 'wcs_missing', array() ); } elseif ( version_compare( (string) \WC_Subscriptions::$version, self::MIN_WCS_VERSION, '<' ) ) { $this->problems[] = array( 'wcs_version', array( 'required' => self::MIN_WCS_VERSION, 'current' => \WC_Subscriptions::$version, ), ); } return empty( $this->problems ); } /** * Returns the translated problems found by the last are_met() call. * * Call on or after the init hook so translations can load. * * @return string[] */ public function errors() { $messages = array(); foreach ( $this->problems as $problem ) { list( $code, $ctx ) = $problem; switch ( $code ) { case 'wc_missing': $messages[] = __( 'Recast needs WooCommerce. Install and activate it.', 'recast-subscriptions' ); break; case 'wc_version': /* translators: 1: required WooCommerce version, 2: current WooCommerce version */ $messages[] = sprintf( __( 'Recast needs WooCommerce %1$s or newer. This site has WooCommerce %2$s.', 'recast-subscriptions' ), $ctx['required'], $ctx['current'] ); break; case 'wcs_missing': $messages[] = __( 'Recast needs WooCommerce Subscriptions. Install and activate it.', 'recast-subscriptions' ); break; case 'wcs_version': /* translators: 1: required WooCommerce Subscriptions version, 2: current version */ $messages[] = sprintf( __( 'Recast needs WooCommerce Subscriptions %1$s or newer. This site has version %2$s.', 'recast-subscriptions' ), $ctx['required'], $ctx['current'] ); break; } } return $messages; } /** * Returns the raw problem codes (untranslated), safe to call at any time. * * @return string[] */ public function codes() { return array_map( static function ( $problem ) { return $problem[0]; }, $this->problems ); } /** * Handles unmet requirements: stops the Freemius redirect to the Recast page that follows * activation, because that page does not exist without the requirements. Deactivates the * plugin on the next admin request and shows a notice that lists the reasons. * * @return void */ public function handle_unmet() { if ( function_exists( 'recast_fs' ) && recast_fs() ) { recast_fs()->add_filter( 'redirect_on_activation', '__return_false' ); } add_action( 'admin_init', array( $this, 'deactivate' ) ); add_action( 'admin_notices', array( $this, 'render_notice' ) ); add_action( 'network_admin_notices', array( $this, 'render_notice' ) ); } /** * Deactivates the plugin when the current user can manage plugins. * * @return void */ public function deactivate() { if ( ! current_user_can( 'activate_plugins' ) || ! function_exists( 'deactivate_plugins' ) ) { return; } $network = is_multisite() && is_plugin_active_for_network( RECAST_BASENAME ); if ( ! $network && ! is_plugin_active( RECAST_BASENAME ) ) { return; } deactivate_plugins( RECAST_BASENAME, false, $network ); // Freemius prints its notices later in this request; its "activated" notice would be wrong. if ( function_exists( 'recast_fs' ) && recast_fs() ) { recast_fs()->remove_sticky( 'premium_activated' ); } // The plugin was not activated after all, so WordPress must not show "Plugin activated.". unset( $_GET['activate'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended } /** * Prints the notice. * * @return void */ public function render_notice() { if ( ! current_user_can( 'activate_plugins' ) || empty( $this->problems ) ) { return; } echo '

'; echo esc_html__( 'Recast Subscriptions is inactive.', 'recast-subscriptions' ); echo '

'; } }