| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Admin; |
| 4 |
|
| 5 |
/** |
| 6 |
* Install & Activate required plugins |
| 7 |
* |
| 8 |
* Class Required |
| 9 |
*/ |
| 10 |
class Required { |
| 11 |
|
| 12 |
private $plugin_file = true; |
| 13 |
|
| 14 |
/** |
| 15 |
* Names of the plugins this one needs but does not have active. |
| 16 |
* |
| 17 |
* @var string[] |
| 18 |
*/ |
| 19 |
private $required_plugins = array(); |
| 20 |
|
| 21 |
public function __construct() { |
| 22 |
add_action( 'init', array( $this, 'check_plugins' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
public function check_plugins() { |
| 26 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 27 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 28 |
} |
| 29 |
|
| 30 |
$plugin_file = WP_PLUGIN_DIR . '/woocommerce/woocommerce.php'; |
| 31 |
|
| 32 |
if ( ! file_exists( $plugin_file ) ) { |
| 33 |
$this->plugin_file = false; |
| 34 |
} |
| 35 |
|
| 36 |
// Only wire the notice, its assets, and the plugins-list row when a |
| 37 |
// dependency is actually missing — the same gating the pro plugin uses, |
| 38 |
// so the red row never lingers once WooCommerce is active. |
| 39 |
if ( ! file_exists( $plugin_file ) || ! is_plugin_active( 'woocommerce/woocommerce.php' ) ) { |
| 40 |
$this->required_plugins[] = 'WooCommerce'; |
| 41 |
|
| 42 |
add_action( 'admin_notices', array( $this, 'install_plugin_notice' ) ); |
| 43 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 44 |
add_action( 'after_plugin_row_' . plugin_basename( SUBSCRPT_FILE ), array( $this, 'required_plugins_row' ), 10, 2 ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Render a warning row below this plugin's row on the plugins list page. |
| 50 |
* |
| 51 |
* Mirrors the pro plugin's row: flags that the plugin is not fully active |
| 52 |
* and lists what it still needs. |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function required_plugins_row() { |
| 57 |
if ( empty( $this->required_plugins ) ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
$show_auto_updates = function_exists( 'wp_is_auto_update_enabled_for_type' ) && wp_is_auto_update_enabled_for_type( 'plugin' ); |
| 62 |
?> |
| 63 |
<tr class="active subscrpt-plugin-warning-row"> |
| 64 |
<th scope="row" class="check-column"> |
| 65 |
<span class="dashicons dashicons-warning"></span> |
| 66 |
</th> |
| 67 |
<td class="column-primary"> |
| 68 |
<p><?php esc_html_e( 'This plugin is not fully active', 'subscription' ); ?></p> |
| 69 |
</td> |
| 70 |
<td class="column-description desc"> |
| 71 |
<div class="plugin-description"> |
| 72 |
<strong><?php esc_html_e( 'Required Plugins', 'subscription' ); ?></strong> |
| 73 |
</div> |
| 74 |
<ul> |
| 75 |
<?php foreach ( $this->required_plugins as $plugin_name ) : ?> |
| 76 |
<li><?php echo esc_html( $plugin_name ); ?></li> |
| 77 |
<?php endforeach; ?> |
| 78 |
</ul> |
| 79 |
</td> |
| 80 |
<?php if ( $show_auto_updates ) : ?> |
| 81 |
<td class="column-auto-updates"> |
| 82 |
<span class="label"></span> |
| 83 |
<div class="notice notice-error notice-alt inline hidden"><p></p></div> |
| 84 |
</td> |
| 85 |
<?php endif; ?> |
| 86 |
</tr> |
| 87 |
<?php |
| 88 |
} |
| 89 |
|
| 90 |
public function enqueue_assets() { |
| 91 |
// Only load the notice styles/script — including the red plugins-list row |
| 92 |
// tint — when a dependency is actually missing. Otherwise the row would |
| 93 |
// stay tinted even after WooCommerce is active. |
| 94 |
if ( empty( $this->required_plugins ) ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
// Version the installer stylesheet by file mtime so CSS edits bust the |
| 99 |
// browser cache: the plugin version is a build-time placeholder and does |
| 100 |
// not change in place, so it cannot do that on its own. |
| 101 |
$installer_css = SUBSCRPT_PATH . '/assets/css/installer.css'; |
| 102 |
if ( file_exists( $installer_css ) && wp_style_is( 'sdevs_installer', 'registered' ) ) { |
| 103 |
wp_styles()->registered['sdevs_installer']->ver = (string) filemtime( $installer_css ); |
| 104 |
} |
| 105 |
|
| 106 |
if ( ! wp_style_is( 'sdevs_installer' ) ) { |
| 107 |
wp_enqueue_style( 'sdevs_installer' ); |
| 108 |
} |
| 109 |
|
| 110 |
if ( ! wp_script_is( 'sdevs_installer' ) ) { |
| 111 |
wp_enqueue_script( 'sdevs_installer' ); |
| 112 |
wp_localize_script( |
| 113 |
'sdevs_installer', |
| 114 |
'sdevs_installer_helper_obj', |
| 115 |
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) |
| 116 |
); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
public function install_plugin_notice() { |
| 121 |
if ( $this->plugin_file ) { |
| 122 |
$id = 'sdevs-activate-plugin'; |
| 123 |
$label = __( 'Activate Woocommerce', 'subscription' ); |
| 124 |
} else { |
| 125 |
$id = 'sdevs-install-plugin'; |
| 126 |
$label = __( 'Install Woocommerce', 'subscription' ); |
| 127 |
} |
| 128 |
|
| 129 |
include 'views/required-notice.php'; |
| 130 |
} |
| 131 |
} |
| 132 |
|