| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Reusable admin notice recommending WPEPP plugin. |
| 5 |
* |
| 6 |
* Wrapped in class_exists() so multiple plugins can include |
| 7 |
* this file without conflict — only the first one loaded wins. |
| 8 |
* |
| 9 |
* @link https://wpthemespace.com |
| 10 |
* @since 2.0.0 |
| 11 |
* @package WpSpace |
| 12 |
* @author Noor Alam |
| 13 |
*/ |
| 14 |
|
| 15 |
if ( ! class_exists( 'WpSpace_Recommend_WPEPP' ) ) : |
| 16 |
|
| 17 |
class WpSpace_Recommend_WPEPP { |
| 18 |
|
| 19 |
const PLUGIN_SLUG = 'wp-edit-password-protected'; |
| 20 |
const PLUGIN_BASENAME = 'wp-edit-password-protected/wp-edit-password-protected.php'; |
| 21 |
const DISMISS_OPTION = 'wpspace_wpepp_dismiss'; |
| 22 |
const AJAX_ACTION = 'wpspace_activate_wpepp'; |
| 23 |
const NONCE_DISMISS = 'wpspace_wpepp_dismiss_nonce'; |
| 24 |
|
| 25 |
public function __construct() { |
| 26 |
add_action( 'admin_notices', array( $this, 'render_notice' ) ); |
| 27 |
add_action( 'admin_init', array( $this, 'handle_dismiss' ) ); |
| 28 |
add_action( 'wp_ajax_' . self::AJAX_ACTION, array( $this, 'ajax_activate' ) ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Render the recommendation notice. |
| 33 |
*/ |
| 34 |
public function render_notice() { |
| 35 |
|
| 36 |
if ( ! current_user_can( 'install_plugins' ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
if ( get_option( self::DISMISS_OPTION, false ) ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 45 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 46 |
} |
| 47 |
|
| 48 |
if ( is_plugin_active( self::PLUGIN_BASENAME ) ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
$installed = is_dir( WP_PLUGIN_DIR . '/' . self::PLUGIN_SLUG ); |
| 53 |
|
| 54 |
if ( $installed ) { |
| 55 |
$button_text = esc_html__( 'Activate Now', 'gallery-box' ); |
| 56 |
$data_action = 'activate'; |
| 57 |
} else { |
| 58 |
$button_text = esc_html__( 'Install & Activate', 'gallery-box' ); |
| 59 |
$data_action = 'install'; |
| 60 |
} |
| 61 |
|
| 62 |
$dismiss_url = wp_nonce_url( |
| 63 |
add_query_arg( 'wpspace_wpepp_dismiss', '1' ), |
| 64 |
self::NONCE_DISMISS |
| 65 |
); |
| 66 |
|
| 67 |
$nonce_activate = wp_create_nonce( 'wpspace-activate-' . self::PLUGIN_SLUG ); |
| 68 |
$nonce_updates = wp_create_nonce( 'updates' ); |
| 69 |
|
| 70 |
wp_enqueue_script( 'plugin-install' ); |
| 71 |
wp_enqueue_script( 'updates' ); |
| 72 |
|
| 73 |
$uid = 'wpspace-wpepp-' . wp_rand( 1000, 9999 ); |
| 74 |
?> |
| 75 |
<div id="<?php echo esc_attr( $uid ); ?>" class="notice notice-info is-dismissible" style="padding:14px 20px 18px; border-left-color:#2271b1;"> |
| 76 |
<p style="font-size:14px; margin-bottom:10px;"> |
| 77 |
<span style="font-size:16px; margin-right:4px;">🛡</span> |
| 78 |
<strong><?php esc_html_e( 'Protect Your Site — Free Security Plugin', 'gallery-box' ); ?></strong> |
| 79 |
</p> |
| 80 |
<p style="margin-bottom:12px;"> |
| 81 |
<?php esc_html_e( 'Your WordPress site may be vulnerable to brute force attacks, AI content scrapers, and unauthorized access.', 'gallery-box' ); ?> |
| 82 |
<strong>WPEPP</strong> <?php esc_html_e( 'fixes that in one click:', 'gallery-box' ); ?> |
| 83 |
</p> |
| 84 |
<ul style="margin:0 0 14px 18px; list-style:disc; line-height:1.8;"> |
| 85 |
<li><?php esc_html_e( 'Limit Login Attempts & block brute force attacks', 'gallery-box' ); ?></li> |
| 86 |
<li><?php esc_html_e( 'Block AI Crawlers — stop GPTBot, CCBot & others from scraping your content', 'gallery-box' ); ?></li> |
| 87 |
<li><?php esc_html_e( 'Disable XML-RPC, hide WP version & block user enumeration', 'gallery-box' ); ?></li> |
| 88 |
<li><?php esc_html_e( 'Customize the login page with live preview', 'gallery-box' ); ?></li> |
| 89 |
<li><?php esc_html_e( 'Password protect pages & lock your entire site', 'gallery-box' ); ?></li> |
| 90 |
</ul> |
| 91 |
<p style="margin:0;"> |
| 92 |
<button type="button" |
| 93 |
class="button button-primary wpspace-wpepp-btn" |
| 94 |
data-notice="<?php echo esc_attr( $uid ); ?>" |
| 95 |
data-action="<?php echo esc_attr( $data_action ); ?>" |
| 96 |
data-slug="<?php echo esc_attr( self::PLUGIN_SLUG ); ?>" |
| 97 |
data-basename="<?php echo esc_attr( self::PLUGIN_BASENAME ); ?>" |
| 98 |
data-install-nonce="<?php echo esc_attr( $nonce_updates ); ?>" |
| 99 |
data-activate-nonce="<?php echo esc_attr( $nonce_activate ); ?>" |
| 100 |
style="margin-right:8px; font-weight:600;"> |
| 101 |
<?php echo $button_text; // Already escaped. ?> |
| 102 |
</button> |
| 103 |
|
| 104 |
<a href="<?php echo esc_url( $dismiss_url ); ?>" style="color:#787c82; text-decoration:none;"> |
| 105 |
<?php esc_html_e( 'No thanks', 'gallery-box' ); ?> |
| 106 |
</a> |
| 107 |
<span class="wpspace-wpepp-status" style="margin-left:10px; font-style:italic; color:#666;"></span> |
| 108 |
</p> |
| 109 |
</div> |
| 110 |
<?php |
| 111 |
$this->inline_script(); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Output the inline JS once per page load. |
| 116 |
*/ |
| 117 |
private function inline_script() { |
| 118 |
static $printed = false; |
| 119 |
if ( $printed ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
$printed = true; |
| 123 |
|
| 124 |
$ajax_action = self::AJAX_ACTION; |
| 125 |
$i18n = array( |
| 126 |
'installing' => esc_js( __( 'Installing...', 'gallery-box' ) ), |
| 127 |
'activating' => esc_js( __( 'Activating...', 'gallery-box' ) ), |
| 128 |
'active' => esc_js( __( 'Active!', 'gallery-box' ) ), |
| 129 |
'installBtn' => esc_js( __( 'Install & Activate', 'gallery-box' ) ), |
| 130 |
'activateBtn' => esc_js( __( 'Activate Now', 'gallery-box' ) ), |
| 131 |
'installFail' => esc_js( __( 'Install failed.', 'gallery-box' ) ), |
| 132 |
'activeFail' => esc_js( __( 'Activation failed.', 'gallery-box' ) ), |
| 133 |
'success' => esc_js( __( 'Plugin activated successfully.', 'gallery-box' ) ), |
| 134 |
); |
| 135 |
?> |
| 136 |
<script> |
| 137 |
(function(){ |
| 138 |
var i18n = <?php echo wp_json_encode( $i18n ); ?>; |
| 139 |
document.querySelectorAll('.wpspace-wpepp-btn').forEach(function(btn){ |
| 140 |
btn.addEventListener('click', function(e){ |
| 141 |
e.preventDefault(); |
| 142 |
var noticeEl = document.getElementById(btn.getAttribute('data-notice')); |
| 143 |
var statusEl = noticeEl ? noticeEl.querySelector('.wpspace-wpepp-status') : null; |
| 144 |
var action = btn.getAttribute('data-action'); |
| 145 |
var slug = btn.getAttribute('data-slug'); |
| 146 |
var basename = btn.getAttribute('data-basename'); |
| 147 |
|
| 148 |
btn.disabled = true; |
| 149 |
|
| 150 |
if ( action === 'install' ) { |
| 151 |
btn.textContent = i18n.installing; |
| 152 |
if(statusEl) statusEl.textContent = ''; |
| 153 |
doInstall(btn, statusEl, noticeEl, slug, basename); |
| 154 |
} else { |
| 155 |
btn.textContent = i18n.activating; |
| 156 |
if(statusEl) statusEl.textContent = ''; |
| 157 |
doActivate(btn, statusEl, noticeEl, basename); |
| 158 |
} |
| 159 |
}); |
| 160 |
}); |
| 161 |
|
| 162 |
function doInstall(btn, statusEl, noticeEl, slug, basename){ |
| 163 |
wp.updates.ajax('install-plugin',{ |
| 164 |
slug: slug, |
| 165 |
success: function(){ |
| 166 |
btn.textContent = i18n.activating; |
| 167 |
doActivate(btn, statusEl, noticeEl, basename); |
| 168 |
}, |
| 169 |
error: function(r){ |
| 170 |
if(r.errorCode === 'folder_exists'){ |
| 171 |
btn.textContent = i18n.activating; |
| 172 |
doActivate(btn, statusEl, noticeEl, basename); |
| 173 |
return; |
| 174 |
} |
| 175 |
btn.textContent = i18n.installBtn; |
| 176 |
btn.disabled = false; |
| 177 |
if(statusEl){ statusEl.textContent = r.errorMessage || i18n.installFail; statusEl.style.color='#d63638'; } |
| 178 |
} |
| 179 |
}); |
| 180 |
} |
| 181 |
|
| 182 |
function doActivate(btn, statusEl, noticeEl, basename){ |
| 183 |
var fd = new FormData(); |
| 184 |
fd.append('action','<?php echo esc_js( $ajax_action ); ?>'); |
| 185 |
fd.append('plugin', basename); |
| 186 |
fd.append('_wpnonce', btn.getAttribute('data-activate-nonce')); |
| 187 |
|
| 188 |
fetch(ajaxurl,{method:'POST',body:fd,credentials:'same-origin'}) |
| 189 |
.then(function(r){return r.json();}) |
| 190 |
.then(function(r){ |
| 191 |
if(r.success){ |
| 192 |
btn.textContent = i18n.active; |
| 193 |
btn.classList.remove('button-primary'); |
| 194 |
btn.style.color='#00a32a'; |
| 195 |
btn.style.borderColor='#00a32a'; |
| 196 |
if(statusEl){ statusEl.textContent = '\u2713 '+i18n.success; statusEl.style.color='#00a32a'; } |
| 197 |
setTimeout(function(){ if(noticeEl) noticeEl.style.display='none'; },3000); |
| 198 |
} else { |
| 199 |
btn.textContent = i18n.activateBtn; |
| 200 |
btn.disabled = false; |
| 201 |
if(statusEl){ statusEl.textContent = (r.data&&r.data.message)||i18n.activeFail; statusEl.style.color='#d63638'; } |
| 202 |
} |
| 203 |
}) |
| 204 |
.catch(function(){ |
| 205 |
btn.textContent = i18n.activateBtn; |
| 206 |
btn.disabled = false; |
| 207 |
if(statusEl){ statusEl.textContent = i18n.activeFail; statusEl.style.color='#d63638'; } |
| 208 |
}); |
| 209 |
} |
| 210 |
})(); |
| 211 |
</script> |
| 212 |
<?php |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Handle the dismiss (nonce-protected GET request). |
| 217 |
*/ |
| 218 |
public function handle_dismiss() { |
| 219 |
if ( |
| 220 |
isset( $_GET['wpspace_wpepp_dismiss'] ) |
| 221 |
&& '1' === $_GET['wpspace_wpepp_dismiss'] |
| 222 |
&& isset( $_GET['_wpnonce'] ) |
| 223 |
&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), self::NONCE_DISMISS ) |
| 224 |
&& current_user_can( 'install_plugins' ) |
| 225 |
) { |
| 226 |
update_option( self::DISMISS_OPTION, 1, false ); |
| 227 |
wp_safe_redirect( remove_query_arg( array( 'wpspace_wpepp_dismiss', '_wpnonce' ) ) ); |
| 228 |
exit; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* AJAX: activate the plugin without page reload. |
| 234 |
*/ |
| 235 |
public function ajax_activate() { |
| 236 |
check_ajax_referer( 'wpspace-activate-' . self::PLUGIN_SLUG ); |
| 237 |
|
| 238 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 239 |
wp_send_json_error( array( 'message' => __( 'Permission denied.', 'gallery-box' ) ) ); |
| 240 |
} |
| 241 |
|
| 242 |
$plugin = isset( $_POST['plugin'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) : ''; |
| 243 |
|
| 244 |
if ( empty( $plugin ) || $plugin !== self::PLUGIN_BASENAME ) { |
| 245 |
wp_send_json_error( array( 'message' => __( 'Invalid plugin.', 'gallery-box' ) ) ); |
| 246 |
} |
| 247 |
|
| 248 |
$result = activate_plugin( $plugin ); |
| 249 |
|
| 250 |
if ( is_wp_error( $result ) ) { |
| 251 |
wp_send_json_error( array( 'message' => $result->get_error_message() ) ); |
| 252 |
} |
| 253 |
|
| 254 |
wp_send_json_success(); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
// new WpSpace_Recommend_WPEPP(); |
| 259 |
|
| 260 |
endif; // class_exists |
| 261 |
|
| 262 |
// ---------- Theme recommendation notice (themes.php only) ---------- |
| 263 |
if ( ! function_exists( 'spacehide_go_me' ) ) : |
| 264 |
function spacehide_go_me() { |
| 265 |
global $pagenow; |
| 266 |
if ( $pagenow !== 'themes.php' ) { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
$url = 'https://wpthemespace.com/product-category/pro-theme/'; |
| 271 |
?> |
| 272 |
<div class="notice notice-success is-dismissible" style="padding:10px 15px 20px;"> |
| 273 |
<p> |
| 274 |
<strong><span style="color:red;"><?php esc_html_e( 'Hi Buddy!! Recommended WordPress Theme for you:', 'gallery-box' ); ?></span> |
| 275 |
<span style="color:green;"><?php esc_html_e( 'If you find a Secure, SEO friendly, full functional premium WordPress theme for your site then', 'gallery-box' ); ?></span></strong> |
| 276 |
<a href="<?php echo esc_url( $url ); ?>" target="_blank"><?php esc_html_e( 'see here', 'gallery-box' ); ?></a>. |
| 277 |
</p> |
| 278 |
<a target="_blank" class="button button-danger" href="<?php echo esc_url( $url ); ?>" style="margin-right:10px;"> |
| 279 |
<?php esc_html_e( 'View WordPress Theme', 'gallery-box' ); ?> |
| 280 |
</a> |
| 281 |
</div> |
| 282 |
<?php |
| 283 |
} |
| 284 |
add_action( 'admin_notices', 'spacehide_go_me' ); |
| 285 |
endif; |
| 286 |
|