| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpinupWp; |
| 4 |
|
| 5 |
class AdminNotices { |
| 6 |
|
| 7 |
/** |
| 8 |
* @var string |
| 9 |
*/ |
| 10 |
public $url; |
| 11 |
|
| 12 |
/** |
| 13 |
* AdminNotices constructor. |
| 14 |
* |
| 15 |
* @param string $url |
| 16 |
*/ |
| 17 |
public function __construct( $url ) { |
| 18 |
$this->url = $url; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Init |
| 23 |
*/ |
| 24 |
public function init() { |
| 25 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 26 |
add_action( 'wp_ajax_spinupwp_dismiss_notice', array( $this, 'ajax_dismiss_notice' ) ); |
| 27 |
|
| 28 |
if ( is_multisite() ) { |
| 29 |
add_action( 'network_admin_notices', array( $this, 'show_notices' ) ); |
| 30 |
} else { |
| 31 |
add_action( 'admin_notices', array( $this, 'show_notices' ) ); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Enqueue admin scripts. |
| 37 |
*/ |
| 38 |
public function enqueue_scripts() { |
| 39 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
wp_enqueue_script( 'spinupwp-dismiss', $this->url . 'assets/js/dismiss-notice.js', array( 'jquery' ), '1.0' ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Handle AJAX request to dismiss notice. |
| 48 |
*/ |
| 49 |
public function ajax_dismiss_notice() { |
| 50 |
if ( ! check_ajax_referer( 'dismiss-notice', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 51 |
wp_die( - 1, 403 ); |
| 52 |
} |
| 53 |
|
| 54 |
$notice = isset( $_POST['notice'] ) ? sanitize_text_field( $_POST['notice'] ) : ''; |
| 55 |
|
| 56 |
if ( empty( $notice ) ) { |
| 57 |
wp_die( - 1, 400 ); |
| 58 |
} |
| 59 |
|
| 60 |
update_site_option( "spinupwp_{$notice}_notice_dismissed", true ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Show notices. |
| 65 |
*/ |
| 66 |
public function show_notices() { |
| 67 |
$this->show_mail_notice(); |
| 68 |
$this->show_redis_cache_disabled_notice(); |
| 69 |
$this->show_object_cache_dropin_updated_notice(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Show a notice about configuring mail. |
| 74 |
*/ |
| 75 |
public function show_mail_notice() { |
| 76 |
if ( ! current_user_can( 'manage_options' ) || get_site_option( 'spinupwp_mail_notice_dismissed' ) ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
$msg = __( 'Your site is ready to go! You will need to set up email if you wish to send outgoing emails from this site.', 'spinupwp' ); |
| 81 |
$link = sprintf( '<a href="%s">%s »</a>', 'https://spinupwp.com/doc/setting-up-transactional-email-wordpress/?utm_campaign=support%2Bdocs&utm_source=SpinupWP%2BMU&utm_medium=insideplugin', __( 'More info', 'spinupwp' ) ); |
| 82 |
$nonce = wp_create_nonce( 'dismiss-notice' ); |
| 83 |
echo "<div class=\"spinupwp notice notice-success is-dismissible\" data-nonce=\"{$nonce}\" data-notice=\"mail\"><p><strong>SpinupWP</strong> — {$msg} {$link}</p></div>"; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Show a notice about Redis Object Cache plugin being disabled. |
| 88 |
*/ |
| 89 |
public function show_redis_cache_disabled_notice() { |
| 90 |
if ( ! current_user_can( 'manage_options' ) || ! get_site_option( 'spinupwp_redis_cache_disabled' ) || get_site_option( 'spinupwp_redis_cache_disabled_notice_dismissed' ) ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$msg = __( 'The Redis Object Cache plugin has been deactivated and can be removed. The SpinupWP plugin now handles clearing the Redis object cache.', 'spinupwp' ); |
| 95 |
$nonce = wp_create_nonce( 'dismiss-notice' ); |
| 96 |
echo "<div class=\"spinupwp notice notice-success is-dismissible\" data-nonce=\"{$nonce}\" data-notice=\"redis_cache_disabled\"><p><strong>SpinupWP</strong> — {$msg}</p></div>"; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Show a notice when the object-cache.php drop-in has been updated. |
| 101 |
*/ |
| 102 |
public function show_object_cache_dropin_updated_notice() { |
| 103 |
$wpcontent_dir = untrailingslashit( WP_CONTENT_DIR ); |
| 104 |
|
| 105 |
if ( file_exists( $wpcontent_dir . '/object-cache.php' ) ) { |
| 106 |
$plugin_path = untrailingslashit( dirname( __DIR__ ) ); |
| 107 |
|
| 108 |
$dropin = get_plugin_data( $wpcontent_dir . '/object-cache.php' ); |
| 109 |
$plugin = get_plugin_data( $plugin_path . '/drop-ins/object-cache.php' ); |
| 110 |
|
| 111 |
if ( $dropin['PluginURI'] !== $plugin['PluginURI'] ) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
if ( version_compare( $dropin['Version'], $plugin['Version'], '<' ) ) { |
| 116 |
$result = Plugin::update_object_cache_dropin(); |
| 117 |
|
| 118 |
if ( $result ) { |
| 119 |
$msg = __( 'Object cache drop-in updated.', 'spinupwp' ); |
| 120 |
echo "<div class=\"spinupwp notice notice-success\"><p><strong>SpinupWP</strong> — {$msg}</p></div>"; |
| 121 |
} else { |
| 122 |
$msg = __( 'Object cache drop-in could not be updated.', 'spinupwp' ); |
| 123 |
echo "<div class=\"spinupwp notice notice-error\"><p><strong>SpinupWP</strong> — {$msg}</p></div>"; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|