| 1 |
<?php |
| 2 |
namespace Dudlewebs\WPMCS; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
class Admin { |
| 7 |
private static $instance = null; |
| 8 |
private $assets_url; |
| 9 |
private $version; |
| 10 |
private $token; |
| 11 |
private $script_suffix; |
| 12 |
|
| 13 |
protected $hook_suffix = []; |
| 14 |
/** |
| 15 |
* Admin constructor. |
| 16 |
* @since 1.0.0 |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
$this->assets_url = WPMCS_ASSETS_URL; |
| 20 |
$this->version = WPMCS_VERSION; |
| 21 |
$this->token = WPMCS_TOKEN; |
| 22 |
$this->script_suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
| 23 |
|
| 24 |
$plugin = plugin_basename(WPMCS_FILE); |
| 25 |
|
| 26 |
// Create file directory on action do |
| 27 |
add_action( $this->token.'_create_plugin_dir', [ $this, 'create_plugin_dir' ] ); |
| 28 |
|
| 29 |
// add action links to link to link list display on the plugins page. |
| 30 |
add_filter("plugin_action_links_$plugin", [$this, 'plugin_action_links']); |
| 31 |
|
| 32 |
// add our custom CSS classes to <body> |
| 33 |
add_filter( 'admin_body_class', [ $this, 'admin_body_class' ] ); |
| 34 |
// Admin Init |
| 35 |
add_action('admin_init', [$this, 'adminInit']); |
| 36 |
|
| 37 |
add_action('admin_menu', [$this, 'add_menu'], 10); |
| 38 |
|
| 39 |
add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_scripts'], 10, 1); |
| 40 |
add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_styles'], 10, 1); |
| 41 |
// Load media scripts |
| 42 |
add_action( 'load-upload.php', [ $this, 'load_media_assets' ], 11 ); |
| 43 |
|
| 44 |
// Plugin Deactivation Survey |
| 45 |
add_action('admin_footer', array($this, 'wpmcs_deactivation_form')); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Method that is used on plugin initialization time |
| 50 |
* @since 1.0.0 |
| 51 |
*/ |
| 52 |
public function adminInit() { |
| 53 |
if(get_option( $this->token.'_db_version' ) !== WPMCS_DB_VERSION) { |
| 54 |
$database = Db::instance(); |
| 55 |
$database->do_database_upgrade(); |
| 56 |
} |
| 57 |
|
| 58 |
if (get_option($this->token.'_do_activation_redirect', false) && !Utils::get_service()) { |
| 59 |
delete_option($this->token.'_do_activation_redirect'); |
| 60 |
wp_redirect(admin_url('admin.php?page=' . $this->token . '-admin-ui#/configure')); |
| 61 |
} else { |
| 62 |
delete_option($this->token.'_do_activation_redirect'); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Installation. Runs on activation. |
| 68 |
* |
| 69 |
* @access public |
| 70 |
* @return void |
| 71 |
* @since 1.0.0 |
| 72 |
*/ |
| 73 |
public function install(){ |
| 74 |
$database = Db::instance(); |
| 75 |
|
| 76 |
$database->create_table(); |
| 77 |
|
| 78 |
// Redirection on activation |
| 79 |
add_option($this->token.'_do_activation_redirect', true); |
| 80 |
|
| 81 |
//Protect directories |
| 82 |
$this->_protect_upload_dir(); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Create plugin specific upload directory |
| 87 |
* @since 1.0.0 |
| 88 |
*/ |
| 89 |
public function create_plugin_dir() { |
| 90 |
//Protect directories |
| 91 |
$this->_protect_upload_dir(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Protect Directory from external access. |
| 96 |
* |
| 97 |
* @access private |
| 98 |
* @return void |
| 99 |
* @since 1.0.0 |
| 100 |
*/ |
| 101 |
private function _protect_upload_dir(){ |
| 102 |
$upload_dir = wp_upload_dir(); |
| 103 |
|
| 104 |
$files = array( |
| 105 |
array( |
| 106 |
'base' => $upload_dir['basedir'] . '/' . Schema::getConstant('UPLOADS'), |
| 107 |
'file' => '.htaccess', |
| 108 |
'content' => 'Options -Indexes' . "\n" |
| 109 |
. '<Files *.php>' . "\n" |
| 110 |
. 'deny from all' . "\n" |
| 111 |
. '</Files>' |
| 112 |
), |
| 113 |
array( |
| 114 |
'base' => $upload_dir['basedir'] . '/' . Schema::getConstant('UPLOADS'), |
| 115 |
'file' => 'index.php', |
| 116 |
'content' => '<?php ' . "\n" |
| 117 |
. '// Silence is golden.' |
| 118 |
) |
| 119 |
); |
| 120 |
|
| 121 |
foreach ($files as $file) { |
| 122 |
if ((wp_mkdir_p($file['base'])) && (!file_exists(trailingslashit($file['base']) . $file['file'])) // If file not exist |
| 123 |
) { |
| 124 |
if ($file_handle = @fopen(trailingslashit($file['base']) . $file['file'], 'w')) { |
| 125 |
fwrite($file_handle, $file['content']); |
| 126 |
fclose($file_handle); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Deactivation hook |
| 134 |
*/ |
| 135 |
public function deactivation(){ |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Load admin Javascript. |
| 140 |
* @access public |
| 141 |
* @return void |
| 142 |
* @since 1.0.0 |
| 143 |
*/ |
| 144 |
public function admin_enqueue_scripts($hook = '') { |
| 145 |
if (!isset($this->hook_suffix) || empty($this->hook_suffix)) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
$screen = get_current_screen(); |
| 150 |
|
| 151 |
// deactivate form js |
| 152 |
if ($screen->id == 'plugins') { |
| 153 |
wp_enqueue_script($this->token . '-deactivate-form', esc_url($this->assets_url) . 'js/deactivate.js', array('jquery'), $this->version, true); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
if (in_array($screen->id, $this->hook_suffix, true)) { |
| 158 |
// Enqueue WordPress media scripts. |
| 159 |
if (!did_action('wp_enqueue_media')) { |
| 160 |
wp_enqueue_media(); |
| 161 |
} |
| 162 |
|
| 163 |
// Enqueue custom backend script. |
| 164 |
wp_enqueue_script($this->token . '-backend', esc_url($this->assets_url) . 'js/backend.js', array('wp-i18n'), $this->version, true); |
| 165 |
|
| 166 |
// Localize a script. |
| 167 |
wp_localize_script( |
| 168 |
$this->token . '-backend', |
| 169 |
$this->token . '_object', |
| 170 |
array( |
| 171 |
'api_nonce' => wp_create_nonce('wp_rest'), |
| 172 |
'root' => rest_url($this->token . '/v1/'), |
| 173 |
'assets_url' => $this->assets_url, |
| 174 |
) |
| 175 |
); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* Load admin CSS. |
| 183 |
* @access public |
| 184 |
* @return void |
| 185 |
* @since 1.0.0 |
| 186 |
*/ |
| 187 |
public function admin_enqueue_styles($hook = '') { |
| 188 |
if ( ! isset($this->hook_suffix) || empty($this->hook_suffix)) { |
| 189 |
return; |
| 190 |
} |
| 191 |
$screen = get_current_screen(); |
| 192 |
if (in_array($screen->id, $this->hook_suffix)) { |
| 193 |
wp_register_style($this->token.'-backend', |
| 194 |
esc_url($this->assets_url).'css/backend.css?nocache='.rand(0, 10000), array(), $this->version); |
| 195 |
wp_enqueue_style($this->token.'-backend'); |
| 196 |
} |
| 197 |
|
| 198 |
if ($screen->id == 'plugins') { |
| 199 |
wp_enqueue_style($this->token . '-deactivate-form', esc_url($this->assets_url) . 'css/deactivate.css', array(), $this->version); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Show action links on the plugin screen. |
| 205 |
* |
| 206 |
* @param mixed $links Plugin Action links. |
| 207 |
* |
| 208 |
* @return array |
| 209 |
*/ |
| 210 |
public function plugin_action_links($links){ |
| 211 |
$action_links = array( |
| 212 |
'getstarted' => '<a href="' . admin_url('admin.php?page=' . $this->token . '-admin-ui#/configure') . '">' . esc_html__('Get Started', 'media-cloud-sync') . '</a>', |
| 213 |
'settings' => '<a href="' . admin_url('admin.php?page=' . $this->token . '-admin-ui/') . '">' . esc_html__('Settings', 'media-cloud-sync') . '</a>', |
| 214 |
); |
| 215 |
|
| 216 |
return array_merge($action_links, $links); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Add Admin Menu |
| 224 |
*/ |
| 225 |
public function add_menu() { |
| 226 |
$this->hook_suffix[] = add_menu_page( |
| 227 |
esc_html__('Media Cloud Sync', 'media-cloud-sync'), |
| 228 |
esc_html__('Media Cloud Sync', 'media-cloud-sync'), |
| 229 |
'manage_options', |
| 230 |
$this->token.'-admin-ui', |
| 231 |
array($this, 'adminUi'), |
| 232 |
'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iOTAiIGhlaWdodD0iNjAiIHZpZXdCb3g9IjAgMCA5MCA2MCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTcwLjMwODYgMjMuMDM1N0w3MC42MDQxIDI0LjUzOUw3Mi4xMzI0IDI0LjY0NTJDODAuODA4NiAyNS4yNDgzIDg3LjYzODYgMzIuNDE0MyA4Ny42Mzg2IDQxLjI1Qzg3LjYzODYgNTAuNTAzIDgwLjE2MDEgNTggNzAuOTYzOSA1OEgyMi40MDk2QzExLjE1OTEgNTggMiA0OC44MTU1IDIgMzcuNVYzNy40OTk4QzEuOTk5NDIgMzIuNDQ3IDMuODU4NjYgMjcuNTczNSA3LjIxODQ4IDIzLjgxNTVDMTAuNTc4MSAyMC4wNTc3IDE1LjIwMDggMTcuNjgwNSAyMC4xOTc3IDE3LjEzODNMMjEuMjYzNiAxNy4wMjI3TDIxLjc1NzMgMTYuMDcwOUMyNi4wOTc2IDcuNzAzOTEgMzQuODA1MyAyIDQ0LjgxOTMgMkg0NC44MTk3QzUwLjgzNDggMS45OTg2MyA1Ni42NjQ3IDQuMDk0MDEgNjEuMzEzOCA3LjkyOTg2QzY1Ljk2MyAxMS43NjU4IDY5LjE0MyAxNy4xMDQ4IDcwLjMwODYgMjMuMDM1N1oiIHN0cm9rZT0iIzAyMDQ0QSIgc3Ryb2tlLXdpZHRoPSI0Ii8+CjxjaXJjbGUgY3g9IjQ1IiBjeT0iMzMiIHI9IjEyI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tYWwiIGluMj0iQmFja2dyb3VuZEltYWdlRml4IiByZXN1bHQ9ImVmZmVjdDFfZHJvcFNoYWRvd18xODFfNDk4Ii8+CjxmZUJsZW5kIG1vZGU9Im5vcm1hbCIgaW49IlNvdXJjZUdyYXBoaWMiIGluMj0iZWZmZWN0MV9kcm9wU2hhZG93XzE4MV80OTgiIHJlc3VsdD0ic2hhcGUiLz4KPC9maWx0ZXI+CjwvZGVmcz4KPC9zdmc+Cg==', |
| 233 |
25 |
| 234 |
); |
| 235 |
|
| 236 |
$this->hook_suffix[] = add_submenu_page( |
| 237 |
$this->token . '-admin-ui', |
| 238 |
esc_html__('Dashboard', 'media-cloud-sync'), |
| 239 |
esc_html__('Dashboard', 'media-cloud-sync'), |
| 240 |
'manage_options', |
| 241 |
$this->token . '-admin-ui' , |
| 242 |
array($this, 'adminUi') |
| 243 |
); |
| 244 |
|
| 245 |
if(Utils::is_service_enabled()) { |
| 246 |
$this->hook_suffix[] = add_submenu_page( |
| 247 |
$this->token . '-admin-ui', |
| 248 |
esc_html__('Settings', 'media-cloud-sync'), |
| 249 |
esc_html__('Settings', 'media-cloud-sync'), |
| 250 |
'manage_options', |
| 251 |
$this->token . '-admin-ui#/settings' , |
| 252 |
array($this, 'adminUi') |
| 253 |
); |
| 254 |
} else { |
| 255 |
$this->hook_suffix[] = add_submenu_page( |
| 256 |
$this->token . '-admin-ui', |
| 257 |
esc_html__('Configure', 'media-cloud-sync'), |
| 258 |
esc_html__('Configure', 'media-cloud-sync'), |
| 259 |
'manage_options', |
| 260 |
$this->token . '-admin-ui#/configure' , |
| 261 |
array($this, 'adminUi') |
| 262 |
); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Calling view function for admin page components |
| 268 |
*/ |
| 269 |
public function adminUi() { |
| 270 |
echo wp_kses_post(sprintf( |
| 271 |
'<div id="%s_ui_root"> |
| 272 |
<div class="%s_loader"> |
| 273 |
<h1>%s</h1> |
| 274 |
<p>%s</p> |
| 275 |
</div> |
| 276 |
</div>', |
| 277 |
$this->token, |
| 278 |
$this->token, |
| 279 |
esc_html__('Media Cloud Sync By Dudlewebs','media-cloud-sync'), |
| 280 |
esc_html__('Plugin is loading Please wait for a while..', 'media-cloud-sync') |
| 281 |
)); |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
/** |
| 287 |
* Load the media assets |
| 288 |
*/ |
| 289 |
public function load_media_assets() { |
| 290 |
/** CSS */ |
| 291 |
wp_enqueue_style($this->token . '-media', esc_url($this->assets_url) . 'css/media.css', array(), $this->version); |
| 292 |
|
| 293 |
/** JS */ |
| 294 |
wp_enqueue_script( |
| 295 |
$this->token . '-media', |
| 296 |
esc_url($this->assets_url) . 'js/media.js', |
| 297 |
array( |
| 298 |
'jquery', |
| 299 |
'media-views', |
| 300 |
'media-grid', |
| 301 |
'wp-util' |
| 302 |
), |
| 303 |
$this->version, |
| 304 |
true |
| 305 |
); |
| 306 |
|
| 307 |
// Localize a script. |
| 308 |
wp_localize_script( |
| 309 |
$this->token . '-media', |
| 310 |
$this->token . '_media_object', |
| 311 |
array( |
| 312 |
'file_details_nonce' => wp_create_nonce('get_media_provider_details'), |
| 313 |
'admin_ajax_url' => admin_url('admin-ajax.php'), |
| 314 |
'strings' => array( |
| 315 |
'provider' => esc_html__("Provider: ", "media-cloud-sync"), |
| 316 |
'region' => esc_html__("Region: ", "media-cloud-sync"), |
| 317 |
'access' => esc_html__("Access: ", "media-cloud-sync"), |
| 318 |
'access_private' => esc_html__("Private", "media-cloud-sync"), |
| 319 |
'access_public' => esc_html__("Public", "media-cloud-sync"), |
| 320 |
) |
| 321 |
) |
| 322 |
); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
/** |
| 327 |
* Deactivation form |
| 328 |
* @since 1.0.2 |
| 329 |
*/ |
| 330 |
public function wpmcs_deactivation_form() { |
| 331 |
$currentScreen = get_current_screen(); |
| 332 |
$screenID = $currentScreen->id; |
| 333 |
if ($screenID == 'plugins') { |
| 334 |
?> |
| 335 |
<div id="dw-wpmcs-survey-form-wrap"> |
| 336 |
<div id="dw-wpmcs-survey-form"> |
| 337 |
<h2>We Value Your Feedback</h2> |
| 338 |
<p>We're sorry to see you go! Please let us know why you are deactivating this plugin. Your feedback is anonymous and will help us improve.</p> |
| 339 |
<form method="POST"> |
| 340 |
<input name="plugin_name" type="hidden" value="<?= $this->token ?>" required> |
| 341 |
<input name="website" type="hidden" value="<?= get_site_url(); ?>" required> |
| 342 |
<input name="title" type="hidden" value="<?= get_bloginfo('name'); ?>" required> |
| 343 |
<input name="version" type="hidden" value="<?= $this->version; ?>" required> |
| 344 |
|
| 345 |
<div class="dw-wpmcs-reason"> |
| 346 |
<label><input type="radio" name="reason" value="I\'m only deactivating temporarily"> I'm only deactivating temporarily</label> |
| 347 |
<label><input type="radio" name="reason" value="I no longer need the plugin"> I no longer need the plugin</label> |
| 348 |
<label><input type="radio" name="reason" value="I only needed the plugin for a short period"> I only needed the plugin for a short period</label> |
| 349 |
<label><input type="radio" name="reason" value="I found a better plugin"> I found a better plugin</label> |
| 350 |
<label><input type="radio" name="reason" value="Upgrading to PRO version"> Upgrading to PRO version</label> |
| 351 |
<label><input type="radio" name="reason" value="Plugin doesn\'t meet my requirements"> Plugin doesn't meet my requirements</label> |
| 352 |
<label><input type="radio" name="reason" value="Plugin broke my site"> Plugin broke my site</label> |
| 353 |
<label><input type="radio" name="reason" value="Plugin suddenly stopped working"> Plugin suddenly stopped working</label> |
| 354 |
<label><input type="radio" name="reason" value="I found a bug"> I found a bug</label> |
| 355 |
<label><input type="radio" name="reason" value="Other"> Other</label> |
| 356 |
</div> |
| 357 |
|
| 358 |
<div class="dw-wpmcs-comments" style="display:none;"> |
| 359 |
<textarea name="comments" placeholder="Please specify" rows="3"></textarea> |
| 360 |
<p>Need help? <a href="https://dudlewebs.com/" target="_blank">Use live chat support</a></p> |
| 361 |
</div> |
| 362 |
|
| 363 |
<p id="dw-wpmcs-error" class="dw-wpmcs-error"></p> |
| 364 |
<div class="dw-wpmcs-actions"> |
| 365 |
<button type="submit" class="dw-button dw-primary" id="dw-wpmcs-deactivate">Submit & Deactivate</button> |
| 366 |
<button type="button" class="dw-button dw-secondary" id="dw-wpmcs-cancel">Cancel</button> |
| 367 |
<button type="button" class="dw-button dw-skip" id="dw-wpmcs-skip">Skip & Deactivate</button> |
| 368 |
</div> |
| 369 |
</form> |
| 370 |
</div> |
| 371 |
</div> |
| 372 |
<?php } |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
/** |
| 378 |
* Add custom classes to the HTML body tag |
| 379 |
* |
| 380 |
* @param string $classes |
| 381 |
* |
| 382 |
* @return string |
| 383 |
*/ |
| 384 |
public function admin_body_class( $classes ) { |
| 385 |
if ( ! $classes ) { |
| 386 |
$classes = array(); |
| 387 |
} else { |
| 388 |
$classes = explode( ' ', $classes ); |
| 389 |
} |
| 390 |
$classes[] = $this->token.'_page'; |
| 391 |
/** |
| 392 |
* Recommended way to target WP 3.8+ |
| 393 |
* http://make.wordpress.org/ui/2013/11/19/targeting-the-new-dashboard-design-in-a-post-mp6-world/ |
| 394 |
* |
| 395 |
*/ |
| 396 |
if ( version_compare( $GLOBALS['wp_version'], '3.8-alpha', '>' ) ) { |
| 397 |
if ( ! in_array( 'mp6', $classes ) ) { |
| 398 |
$classes[] = 'mp6'; |
| 399 |
} |
| 400 |
} |
| 401 |
return implode( ' ', $classes ); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
/** |
| 406 |
* Ensures only one instance of Class is loaded or can be loaded. |
| 407 |
* |
| 408 |
* @return Main Class instance |
| 409 |
* @since 1.0.0 |
| 410 |
* @static |
| 411 |
*/ |
| 412 |
public static function instance(){ |
| 413 |
if (is_null(self::$instance)) { |
| 414 |
self::$instance = new self(); |
| 415 |
} |
| 416 |
return self::$instance; |
| 417 |
} |
| 418 |
|
| 419 |
} |