| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Controllers; |
| 4 |
|
| 5 |
use RadiusTheme\SB\Helpers\Fns; |
| 6 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 7 |
|
| 8 |
// Do not allow directly accessing this file. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit( 'This script cannot be accessed directly.' ); |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
class Dependencies { |
| 15 |
|
| 16 |
const MINIMUM_PHP_VERSION = '7.4'; |
| 17 |
|
| 18 |
const PLUGIN_NAME = 'ShopBuilder - Woocommerce Builder for Elementor'; |
| 19 |
|
| 20 |
use SingletonTrait; |
| 21 |
|
| 22 |
private $missing = []; |
| 23 |
/** |
| 24 |
* @var bool |
| 25 |
*/ |
| 26 |
private $allOk = true; |
| 27 |
|
| 28 |
/** |
| 29 |
* @return bool |
| 30 |
*/ |
| 31 |
public function check() { |
| 32 |
|
| 33 |
if ( version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '<' ) ) { |
| 34 |
add_action( 'admin_notices', [ $this, 'minimum_php_version' ] ); |
| 35 |
$this->allOk = false; |
| 36 |
} |
| 37 |
|
| 38 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 39 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 40 |
} |
| 41 |
if ( ! function_exists( 'wp_create_nonce' ) ) { |
| 42 |
require_once ABSPATH . 'wp-includes/pluggable.php'; |
| 43 |
} |
| 44 |
// Check WooCommerce |
| 45 |
$woocommerce = 'woocommerce/woocommerce.php'; |
| 46 |
|
| 47 |
if ( ! is_plugin_active( $woocommerce ) ) { |
| 48 |
if ( $this->is_plugins_installed( $woocommerce ) ) { |
| 49 |
$activation_url = wp_nonce_url( 'plugins.php?action=activate&plugin=' . $woocommerce . '&plugin_status=all&paged=1&s', 'activate-plugin_' . $woocommerce ); |
| 50 |
$message = sprintf( |
| 51 |
'<strong>%s</strong> %s <strong>%s</strong> %s', |
| 52 |
esc_html( self::PLUGIN_NAME ), |
| 53 |
esc_html__( 'requires', 'shopbuilder' ), |
| 54 |
esc_html__( 'WooCommerce', 'shopbuilder' ), |
| 55 |
esc_html__( 'plugin to be active. Please activate WooCommerce to continue.', 'shopbuilder' ) |
| 56 |
); |
| 57 |
$button_text = esc_html__( 'Activate WooCommerce', 'shopbuilder' ); |
| 58 |
|
| 59 |
} else { |
| 60 |
$activation_url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=woocommerce' ), 'install-plugin_woocommerce' ); |
| 61 |
$message = sprintf( |
| 62 |
'<strong>%s</strong> %s <strong>%s</strong> %s', |
| 63 |
esc_html( self::PLUGIN_NAME ), |
| 64 |
esc_html__( 'requires', 'shopbuilder' ), |
| 65 |
esc_html__( 'WooCommerce', 'shopbuilder' ), |
| 66 |
esc_html__( 'plugin to be installed and activated. Please install WooCommerce to continue.', 'shopbuilder' ) |
| 67 |
); |
| 68 |
$button_text = esc_html__( 'Install WooCommerce', 'shopbuilder' ); |
| 69 |
} |
| 70 |
$this->missing['woocommerce'] = [ |
| 71 |
'name' => 'WooCommerce', |
| 72 |
'slug' => 'woocommerce', |
| 73 |
'file_name' => $woocommerce, |
| 74 |
'url' => $activation_url, |
| 75 |
'message' => $message, |
| 76 |
'button_txt' => $button_text, |
| 77 |
]; |
| 78 |
} |
| 79 |
|
| 80 |
$elementor = 'elementor/elementor.php'; |
| 81 |
|
| 82 |
if ( ! is_plugin_active( $elementor ) ) { |
| 83 |
|
| 84 |
if ( $this->is_plugins_installed( $elementor ) ) { |
| 85 |
$activation_url = wp_nonce_url( 'plugins.php?action=activate&plugin=' . $elementor . '&plugin_status=all&paged=1&s', 'activate-plugin_' . $elementor ); |
| 86 |
$message = sprintf( |
| 87 |
'<strong>%s</strong> %s <strong>%s</strong> %s', |
| 88 |
esc_html( self::PLUGIN_NAME ), |
| 89 |
esc_html__( 'requires', 'shopbuilder' ), |
| 90 |
esc_html__( 'Elementor', 'shopbuilder' ), |
| 91 |
esc_html__( 'plugin to be active. Please activate Elementor to continue.', 'shopbuilder' ) |
| 92 |
); |
| 93 |
$button_text = esc_html__( 'Activate Elementor', 'shopbuilder' ); |
| 94 |
|
| 95 |
} else { |
| 96 |
$activation_url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=elementor' ), |
| 97 |
'install-plugin_elementor' ); |
| 98 |
$message = sprintf( |
| 99 |
'<strong>%s</strong> %s <strong>%s</strong> %s', |
| 100 |
esc_html( self::PLUGIN_NAME ), |
| 101 |
esc_html__( 'requires', 'shopbuilder' ), |
| 102 |
esc_html__( 'Elementor', 'shopbuilder' ), |
| 103 |
esc_html__( 'plugin to be installed and activated. Please install Elementor to continue.', 'shopbuilder' ) |
| 104 |
); |
| 105 |
$button_text = esc_html__( 'Install Elementor', 'shopbuilder' ); |
| 106 |
} |
| 107 |
|
| 108 |
$this->missing['elementor'] = [ |
| 109 |
'name' => 'Elementor', |
| 110 |
'slug' => 'elementor', |
| 111 |
'file_name' => $elementor, |
| 112 |
'url' => $activation_url, |
| 113 |
'message' => $message, |
| 114 |
'button_txt' => $button_text, |
| 115 |
]; |
| 116 |
} |
| 117 |
if ( ! empty( $this->missing ) ) { |
| 118 |
add_action( 'admin_notices', [ $this, '_missing_plugins_warning' ] ); |
| 119 |
|
| 120 |
$this->allOk = false; |
| 121 |
} |
| 122 |
|
| 123 |
return $this->allOk; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Admin Notice For Required PHP Version |
| 128 |
*/ |
| 129 |
public function minimum_php_version() { |
| 130 |
if ( isset( $_GET['activate'] ) ) { |
| 131 |
unset( $_GET['activate'] ); |
| 132 |
} |
| 133 |
$message = sprintf( |
| 134 |
/* translators: 1: Plugin name 2: PHP 3: Required PHP version */ |
| 135 |
esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'shopbuilder' ), |
| 136 |
'<strong>' . esc_html__( 'ShopBuilder', 'shopbuilder' ) . '</strong>', |
| 137 |
'<strong>' . esc_html__( 'PHP', 'shopbuilder' ) . '</strong>', |
| 138 |
self::MINIMUM_PHP_VERSION |
| 139 |
); |
| 140 |
printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Adds admin notice. |
| 145 |
*/ |
| 146 |
public function _missing_plugins_warning() { |
| 147 |
$missingPlugins = ''; |
| 148 |
$counter = 0; |
| 149 |
foreach ( $this->missing as $plugin ) { |
| 150 |
$counter ++; |
| 151 |
if ( $counter == sizeof( $this->missing ) ) { |
| 152 |
$sep = ''; |
| 153 |
} elseif ( $counter == sizeof( $this->missing ) - 1 ) { |
| 154 |
$sep = ' ' . esc_html__( 'and', 'shopbuilder' ) . ' '; |
| 155 |
} else { |
| 156 |
$sep = ', '; |
| 157 |
} |
| 158 |
if ( current_user_can( 'activate_plugins' ) ) { |
| 159 |
$button = '<p><a href="' . esc_url( $plugin['url'] ) . '" class="button-primary plugin-install-by-ajax">' . esc_html( $plugin['button_txt'] ) . '</a></p>'; |
| 160 |
// $plugin['message'] Already used escaping function |
| 161 |
printf( '<div class="error"><p>%1$s</p>%2$s</div>', $plugin['message'], $button ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 162 |
} else { |
| 163 |
$missingPlugins .= '<strong>' . esc_html( $plugin['name'] ) . '</strong>' . $sep; |
| 164 |
} |
| 165 |
} |
| 166 |
// TODO:: AJax plugin installation will do later. |
| 167 |
// self::notice(); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* @param $plugin_file_path |
| 172 |
* |
| 173 |
* @return bool |
| 174 |
*/ |
| 175 |
public function is_plugins_installed( $plugin_file_path = null ) { |
| 176 |
$installed_plugins_list = get_plugins(); |
| 177 |
|
| 178 |
return isset( $installed_plugins_list[ $plugin_file_path ] ); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
/** |
| 184 |
* Undocumented function. |
| 185 |
* |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
public static function notice() { |
| 189 |
add_action( |
| 190 |
'admin_enqueue_scripts', |
| 191 |
function () { |
| 192 |
wp_enqueue_script('jquery'); |
| 193 |
} |
| 194 |
); |
| 195 |
|
| 196 |
add_action( |
| 197 |
'admin_footer', |
| 198 |
function () { |
| 199 |
?> |
| 200 |
<script type="text/javascript"> |
| 201 |
/* |
| 202 |
(function ($) { |
| 203 |
$(function () { |
| 204 |
setTimeout(function () { |
| 205 |
$('.plugin-install-by-ajax') |
| 206 |
.on('click', function (e) { |
| 207 |
e.preventDefault(); |
| 208 |
let plugin_slug = e.target.url; |
| 209 |
const data = { |
| 210 |
action: 'rtsb_plugin_installation', |
| 211 |
plugin_slug: plugin_slug ? plugin_slug : null, |
| 212 |
__rtsb_wpnonce: rtsbParams.__rtsb_wpnonce, |
| 213 |
}; |
| 214 |
wp.updates.installPlugin({ |
| 215 |
slug: 'hello-dolly', |
| 216 |
success: function() { |
| 217 |
console.log('Plugin installed successfully!'); |
| 218 |
}, |
| 219 |
error: function(error) { |
| 220 |
console.log('An error occurred: ' + error.statusText); |
| 221 |
}, |
| 222 |
installing: function() { |
| 223 |
console.log('Installing plugin...'); |
| 224 |
} |
| 225 |
}); |
| 226 |
|
| 227 |
$.ajax({ |
| 228 |
url: '<?php // echo admin_url( 'admin-ajax.php' ); ?>', |
| 229 |
data, |
| 230 |
type: 'POST', |
| 231 |
beforeSend() { |
| 232 |
modal.addModal().addLoading(); |
| 233 |
}, |
| 234 |
success(response) { |
| 235 |
|
| 236 |
wp.updates.installPlugin({ |
| 237 |
slug: 'hello-dolly', |
| 238 |
success: function() { |
| 239 |
console.log('Plugin installed successfully!'); |
| 240 |
}, |
| 241 |
error: function(error) { |
| 242 |
console.log('An error occurred: ' + error.statusText); |
| 243 |
}, |
| 244 |
installing: function() { |
| 245 |
console.log('Installing plugin...'); |
| 246 |
} |
| 247 |
}); |
| 248 |
|
| 249 |
}, |
| 250 |
error(e) {}, |
| 251 |
}); |
| 252 |
|
| 253 |
}); |
| 254 |
}, 1000); |
| 255 |
}); |
| 256 |
})(jQuery); |
| 257 |
*/ |
| 258 |
</script> |
| 259 |
<?php |
| 260 |
} |
| 261 |
); |
| 262 |
/* |
| 263 |
add_action( |
| 264 |
'wp_ajax_rtsb_plugin_installation', |
| 265 |
function () { |
| 266 |
$return = [ |
| 267 |
'success' => false, |
| 268 |
]; |
| 269 |
if ( ! Fns::verify_nonce() ) { |
| 270 |
wp_send_json_error($return); |
| 271 |
} |
| 272 |
error_log( print_r( $_REQUEST , true ) . "\n\n" , 3, __DIR__ . '/log.txt' ); |
| 273 |
wp_send_json_success( $return ); |
| 274 |
wp_die(); |
| 275 |
} |
| 276 |
); |
| 277 |
*/ |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
} |
| 284 |
|