Skip to content

Commit

Permalink
check_plugin_dependencies: remove $silent, return notice message and …
Browse files Browse the repository at this point in the history
…dependency status flag.
  • Loading branch information
htdat committed Oct 1, 2021
1 parent ae7d0d7 commit a453a88
Showing 1 changed file with 113 additions and 111 deletions.
224 changes: 113 additions & 111 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,26 @@ public static function init() {
include_once __DIR__ . '/class-wc-payments-features.php';
include_once __DIR__ . '/class-wc-payments-utils.php';

if ( ! self::check_plugin_dependencies( true ) ) {
add_filter( 'admin_notices', [ __CLASS__, 'check_plugin_dependencies' ] );
$check = self::check_plugin_dependencies();
$message = $check['message'];
$passed = $check['passed'];

if ( null !== $message ) {
add_action(
'admin_notices',
function() use ( $message ) {
// Do not show alerts while installing plugins.
if ( self::is_at_plugin_install_page() ) {
return;
}

self::display_admin_error( $message );
}
);
}

/**
* After displaying notice errors in WP Admin, still silently load the plugin if the account is connected.
*
* @since 3.1.0
*/
$account_data = get_option( 'wcpay_account_data' );
if ( ! isset( $account_data['account'] ) ) {
return;
};
if ( false === $passed ) {
return;
}

add_action( 'admin_init', [ __CLASS__, 'add_woo_admin_notes' ] );
Expand Down Expand Up @@ -374,141 +382,135 @@ public static function get_plugin_headers() {
/**
* Checks if all the dependencies needed to run this plugin are present
*
* @param bool $silent True if the function should just return true/false, False if this function should display notice messages for failed dependencies.
* @return bool True if all dependencies are met, false otherwise
* @return array{message: ?string, passed: bool} An array including a notice message for admins, and a flag with value True if dependencies are met or False other wise.
*/
public static function check_plugin_dependencies( $silent ) {
public static function check_plugin_dependencies() {
$res = [
'message' => null,
'passed' => false,
];

if ( defined( 'WCPAY_TEST_ENV' ) && WCPAY_TEST_ENV ) {
return true;
$res['passed'] = true;
return $res;
}

$plugin_headers = self::get_plugin_headers();

// Do not show alerts while installing plugins.
if ( ! $silent && self::is_at_plugin_install_page() ) {
return true;
}

$wc_version = $plugin_headers['WCRequires'];
$wp_version = $plugin_headers['RequiresWP'];
$wc_version = $plugin_headers['WCRequires'];
$wp_version = $plugin_headers['RequiresWP'];

// Check if WooCommerce is installed and active.
if ( ! class_exists( 'WooCommerce' ) ) {
if ( ! $silent ) {
$message = WC_Payments_Utils::esc_interpolated_html(
__( 'WooCommerce Payments requires <a>WooCommerce</a> to be installed and active.', 'woocommerce-payments' ),
[ 'a' => '<a href="https://wordpress.org/plugins/woocommerce">' ]
);
$res['message'] = WC_Payments_Utils::esc_interpolated_html(
__( 'WooCommerce Payments requires <a>WooCommerce</a> to be installed and active.', 'woocommerce-payments' ),
[ 'a' => '<a href="https://wordpress.org/plugins/woocommerce">' ]
);

if ( current_user_can( 'install_plugins' ) ) {
if ( is_wp_error( validate_plugin( 'woocommerce/woocommerce.php' ) ) ) {
// WooCommerce is not installed.
$activate_url = wp_nonce_url( admin_url( 'update.php?action=install-plugin&plugin=woocommerce' ), 'install-plugin_woocommerce' );
$activate_text = __( 'Install WooCommerce', 'woocommerce-payments' );
} else {
// WooCommerce is installed, so it just needs to be enabled.
$activate_url = wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=woocommerce/woocommerce.php' ), 'activate-plugin_woocommerce/woocommerce.php' );
$activate_text = __( 'Activate WooCommerce', 'woocommerce-payments' );
}
$message .= ' <a href="' . $activate_url . '">' . $activate_text . '</a>';
if ( current_user_can( 'install_plugins' ) ) {
if ( is_wp_error( validate_plugin( 'woocommerce/woocommerce.php' ) ) ) {
// WooCommerce is not installed.
$activate_url = wp_nonce_url( admin_url( 'update.php?action=install-plugin&plugin=woocommerce' ), 'install-plugin_woocommerce' );
$activate_text = __( 'Install WooCommerce', 'woocommerce-payments' );
} else {
// WooCommerce is installed, so it just needs to be enabled.
$activate_url = wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=woocommerce/woocommerce.php' ), 'activate-plugin_woocommerce/woocommerce.php' );
$activate_text = __( 'Activate WooCommerce', 'woocommerce-payments' );
}

self::display_admin_error( $message );
$res['message'] .= ' <a href="' . $activate_url . '">' . $activate_text . '</a>';
}
return false;

$res['passed'] = false;
return $res;
}

// Check if the version of WooCommerce is compatible with WooCommerce Payments.
if ( version_compare( WC_VERSION, $wc_version, '<' ) ) {
if ( ! $silent ) {
$message = WC_Payments_Utils::esc_interpolated_html(
sprintf(
/* translators: %1: current WooCommerce Payment version, %2: required WC version number, %3: currently installed WC version number */
__( 'WooCommerce Payments %1$s requires <strong>WooCommerce %2$s</strong> or greater to be installed (you are using %3$s). ', 'woocommerce-payments' ),
WCPAY_VERSION_NUMBER,
$wc_version,
WC_VERSION
),
[ 'strong' => '<strong>' ]
);
if ( current_user_can( 'update_plugins' ) ) {
// Take the user to the "plugins" screen instead of trying to update WooCommerce inline. WooCommerce adds important information
// on its plugin row regarding the currently installed extensions and their compatibility with the latest WC version.
$message .= '<br/>';
$message .= WC_Payments_Utils::esc_interpolated_html(
$res['message'] = WC_Payments_Utils::esc_interpolated_html(
sprintf(
/* translators: %1: current WooCommerce Payment version, %2: required WC version number, %3: currently installed WC version number */
__( 'WooCommerce Payments %1$s requires <strong>WooCommerce %2$s</strong> or greater to be installed (you are using %3$s). ', 'woocommerce-payments' ),
WCPAY_VERSION_NUMBER,
$wc_version,
WC_VERSION
),
[ 'strong' => '<strong>' ]
);

if ( current_user_can( 'update_plugins' ) ) {
// Take the user to the "plugins" screen instead of trying to update WooCommerce inline. WooCommerce adds important information
// on its plugin row regarding the currently installed extensions and their compatibility with the latest WC version.
$res['message'] .= '<br/>' . WC_Payments_Utils::esc_interpolated_html(
/* translators: a1: link to the Plugins page, a2: link to the page having all previous versions */
__( '<a1>Update WooCommerce</a1> <strong>(recommended)</strong> or manually re-install <a2>a previous version</a2> of WooCommerce Payments.', 'woocommerce-payments' ),
[

'a1' => '<a href="' . admin_url( 'plugins.php' ) . '">',
'strong' => '<strong>',
'a2' => '<a href="https://wordpress.org/plugins/woocommerce-payments/advanced/#download-previous-link" target="_blank">',
]
);
}
self::display_admin_error( $message );
__( '<a1>Update WooCommerce</a1> <strong>(recommended)</strong> or manually re-install <a2>a previous version</a2> of WooCommerce Payments.', 'woocommerce-payments' ),
[

'a1' => '<a href="' . admin_url( 'plugins.php' ) . '">',
'strong' => '<strong>',
'a2' => '<a href="https://wordpress.org/plugins/woocommerce-payments/advanced/#download-previous-link" target="_blank">',
]
);
}
return false;

$res['passed'] = false;
return $res;
}

// Check if the current WooCommerce version has WooCommerce Admin bundled (WC 4.0+) but it's disabled using a filter.
if ( ! defined( 'WC_ADMIN_VERSION_NUMBER' ) ) {
if ( ! $silent ) {
self::display_admin_error(
WC_Payments_Utils::esc_interpolated_html(
__( 'WooCommerce Payments requires WooCommerce Admin to be enabled. Please remove the <code>woocommerce_admin_disabled</code> filter to use WooCommerce Payments.', 'woocommerce-payments' ),
[ 'code' => '<code>' ]
)
);
}
return false;
$res['message'] = WC_Payments_Utils::esc_interpolated_html(
__( 'WooCommerce Payments requires WooCommerce Admin to be enabled. Please remove the <code>woocommerce_admin_disabled</code> filter to use WooCommerce Payments.', 'woocommerce-payments' ),
[ 'code' => '<code>' ]
);

$res['passed'] = false;
return $res;
}

// Check if the version of WooCommerce Admin is compatible with WooCommerce Payments.
if ( version_compare( WC_ADMIN_VERSION_NUMBER, WCPAY_MIN_WC_ADMIN_VERSION, '<' ) ) {
if ( ! $silent ) {
$message = WC_Payments_Utils::esc_interpolated_html(
sprintf(
/* translators: %1: required WC-Admin version number, %2: currently installed WC-Admin version number */
__( 'WooCommerce Payments requires <strong>WooCommerce Admin %1$s</strong> or greater to be installed (you are using %2$s).', 'woocommerce-payments' ),
WCPAY_MIN_WC_ADMIN_VERSION,
WC_ADMIN_VERSION_NUMBER
),
[ 'strong' => '<strong>' ]
);
$res['message'] = WC_Payments_Utils::esc_interpolated_html(
sprintf(
/* translators: %1: required WC-Admin version number, %2: currently installed WC-Admin version number */
__( 'WooCommerce Payments requires <strong>WooCommerce Admin %1$s</strong> or greater to be installed (you are using %2$s).', 'woocommerce-payments' ),
WCPAY_MIN_WC_ADMIN_VERSION,
WC_ADMIN_VERSION_NUMBER
),
[ 'strong' => '<strong>' ]
);

// Let's assume for now that any WC-Admin version bundled with WooCommerce will meet our minimum requirements.
$message .= ' ' . __( 'There is a newer version of WooCommerce Admin bundled with WooCommerce.', 'woocommerce-payments' );
if ( current_user_can( 'deactivate_plugins' ) ) {
$deactivate_url = wp_nonce_url( admin_url( 'plugins.php?action=deactivate&plugin=woocommerce-admin/woocommerce-admin.php' ), 'deactivate-plugin_woocommerce-admin/woocommerce-admin.php' );
$message .= ' <a href="' . $deactivate_url . '">' . __( 'Use the bundled version of WooCommerce Admin', 'woocommerce-payments' ) . '</a>';
}
self::display_admin_error( $message );
$res['message'] .= ' ' . __( 'There is a newer version of WooCommerce Admin bundled with WooCommerce.', 'woocommerce-payments' );

if ( current_user_can( 'deactivate_plugins' ) ) {
$deactivate_url = wp_nonce_url( admin_url( 'plugins.php?action=deactivate&plugin=woocommerce-admin/woocommerce-admin.php' ), 'deactivate-plugin_woocommerce-admin/woocommerce-admin.php' );
$res['message'] .= ' <a href="' . $deactivate_url . '">' . __( 'Use the bundled version of WooCommerce Admin', 'woocommerce-payments' ) . '</a>';
}
return false;

$res['passed'] = false;
return $res;
}

// Check if the version of WordPress is compatible with WooCommerce Payments.
if ( version_compare( get_bloginfo( 'version' ), $wp_version, '<' ) ) {
if ( ! $silent ) {
$message = WC_Payments_Utils::esc_interpolated_html(
sprintf(
/* translators: %1: required WP version number, %2: currently installed WP version number */
__( 'WooCommerce Payments requires <strong>WordPress %1$s</strong> or greater (you are using %2$s).', 'woocommerce-payments' ),
$wp_version,
get_bloginfo( 'version' )
),
[ 'strong' => '<strong>' ]
);
if ( current_user_can( 'update_core' ) ) {
$message .= ' <a href="' . admin_url( 'update-core.php' ) . '">' . __( 'Update WordPress', 'woocommerce-payments' ) . '</a>';
}
self::display_admin_error( $message );
$res['message'] = WC_Payments_Utils::esc_interpolated_html(
sprintf(
/* translators: %1: required WP version number, %2: currently installed WP version number */
__( 'WooCommerce Payments requires <strong>WordPress %1$s</strong> or greater (you are using %2$s).', 'woocommerce-payments' ),
$wp_version,
get_bloginfo( 'version' )
),
[ 'strong' => '<strong>' ]
);
if ( current_user_can( 'update_core' ) ) {
$res['message'] .= ' <a href="' . admin_url( 'update-core.php' ) . '">' . __( 'Update WordPress', 'woocommerce-payments' ) . '</a>';
}
return false;

$res['passed'] = false;
return $res;
}

return true;
$res['passed'] = true;
return $res;
}

/**
Expand Down

0 comments on commit a453a88

Please sign in to comment.