| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Admin\Notices; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || die(); |
| 6 |
|
| 7 |
/** |
| 8 |
* Woocommerce not installed. |
| 9 |
* |
| 10 |
* @since 1.1.0 |
| 11 |
*/ |
| 12 |
class Woocommerce_Not_Installed extends Notice_Base { |
| 13 |
|
| 14 |
/** |
| 15 |
* Notice key. |
| 16 |
* |
| 17 |
* @since 1.1.0 |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
public $key = 'sellkit-woocommerce-not-installed'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Construct of class. |
| 24 |
* |
| 25 |
* @since 1.1.0 |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
parent::__construct(); |
| 29 |
|
| 30 |
add_action( 'wp_ajax_sellkit_install_woocommerce_plugin_by_notice', [ $this, 'installer' ] ); |
| 31 |
|
| 32 |
$this->assets(); |
| 33 |
|
| 34 |
$this->content = $this->content_html(); |
| 35 |
$this->buttons = [ |
| 36 |
'#install-woo' => esc_html__( 'Install WooCommerce', 'sellkit' ), |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Enqueue required assets because these won't be loaded if Woocommerce is not installed. |
| 42 |
* |
| 43 |
* @since 1.1.0 |
| 44 |
*/ |
| 45 |
public function assets() { |
| 46 |
if ( ! $this->is_valid() ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
add_action( 'admin_enqueue_scripts', function() { |
| 51 |
wp_enqueue_style( |
| 52 |
'sellkit-admin-notices-woo', |
| 53 |
sellkit()->plugin_url() . 'assets/dist/css/admin.min.css', |
| 54 |
[], |
| 55 |
sellkit()->version() |
| 56 |
); |
| 57 |
|
| 58 |
wp_enqueue_script( |
| 59 |
'sellkit-admin-notice-woo', |
| 60 |
sellkit()->plugin_url() . 'assets/dist/js/admin.min.js', |
| 61 |
[ 'jquery' ], |
| 62 |
sellkit()->version(), |
| 63 |
true |
| 64 |
); |
| 65 |
} ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Check if notice is valid or not. |
| 70 |
* |
| 71 |
* @since 1.1.0 |
| 72 |
* @return bool |
| 73 |
*/ |
| 74 |
public function is_valid() { |
| 75 |
if ( class_exists( 'woocommerce' ) ) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Content of notice. |
| 84 |
* |
| 85 |
* @since 1.1.0 |
| 86 |
*/ |
| 87 |
public function content_html() { |
| 88 |
$message = sprintf( |
| 89 |
'<span>' |
| 90 |
. '<h3>' |
| 91 |
. esc_html__( 'Welcome to Sellkit', 'sellkit' ) |
| 92 |
. '</h3>' |
| 93 |
/* Translators: 1: bold sellkit 2: bold woocommerce */ |
| 94 |
. esc_html__( 'The %1$s plugin requires the %2$s plugin to be installed & activated as well.', 'sellkit' ) |
| 95 |
. '</span>', |
| 96 |
'<b>Sellkit</b>', |
| 97 |
'<b>WooCommerce</b>' |
| 98 |
); |
| 99 |
|
| 100 |
return $message; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Set the priority of notice. |
| 105 |
* |
| 106 |
* @since 1.1.0 |
| 107 |
* @return int |
| 108 |
*/ |
| 109 |
public function priority() { |
| 110 |
return 1; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Install WooCommerce plugin. |
| 115 |
* |
| 116 |
* @since 1.2.1 |
| 117 |
*/ |
| 118 |
public function installer() { |
| 119 |
check_ajax_referer( 'sellkit_admin', 'nonce' ); |
| 120 |
|
| 121 |
require_once ABSPATH . '/wp-load.php'; |
| 122 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 123 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 124 |
require_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 125 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 126 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 127 |
|
| 128 |
$result = $this->sellkit_install_plugin( 'woocommerce' ); |
| 129 |
|
| 130 |
if ( false === $result ) { |
| 131 |
wp_send_json_error(); |
| 132 |
} |
| 133 |
|
| 134 |
wp_send_json_success(); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Install WordPress plugin by slug. |
| 139 |
* |
| 140 |
* @param array $slug plugins slug. |
| 141 |
* @since 1.2.1 |
| 142 |
* @return bool |
| 143 |
*/ |
| 144 |
private function sellkit_install_plugin( $slug ) { |
| 145 |
$plugin_dir = WP_PLUGIN_DIR . '/' . $slug; |
| 146 |
|
| 147 |
// Plugin not installed. |
| 148 |
if ( ! is_dir( $plugin_dir ) ) { |
| 149 |
$api = plugins_api( |
| 150 |
'plugin_information', |
| 151 |
[ |
| 152 |
'slug' => $slug, |
| 153 |
'fields' => [ |
| 154 |
'short_description' => false, |
| 155 |
'sections' => false, |
| 156 |
'requires' => false, |
| 157 |
'rating' => false, |
| 158 |
'ratings' => false, |
| 159 |
'downloaded' => false, |
| 160 |
'last_updated' => false, |
| 161 |
'added' => false, |
| 162 |
'tags' => false, |
| 163 |
'compatibility' => false, |
| 164 |
'homepage' => false, |
| 165 |
'donate_link' => false, |
| 166 |
], |
| 167 |
] |
| 168 |
); |
| 169 |
|
| 170 |
$skin = new \Plugin_Installer_Skin( [ 'api' => $api ] ); |
| 171 |
|
| 172 |
$upgrader = new \Plugin_Upgrader( $skin ); |
| 173 |
|
| 174 |
$install = $upgrader->install( $api->download_link ); |
| 175 |
|
| 176 |
if ( true !== $install ) { |
| 177 |
return false; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
$plugin_path = $plugin_dir . '/' . $slug . '.php'; |
| 182 |
|
| 183 |
if ( file_exists( $plugin_path ) ) { |
| 184 |
activate_plugin( $plugin_path ); |
| 185 |
|
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
return false; |
| 190 |
} |
| 191 |
} |
| 192 |
|