| 1 |
<?php |
| 2 |
// Exit if accessed directly. |
| 3 |
defined('ABSPATH') || exit; |
| 4 |
|
| 5 |
class Blockspare_Setup |
| 6 |
{ |
| 7 |
|
| 8 |
public $name; |
| 9 |
public $type; |
| 10 |
public $dismiss_url; |
| 11 |
public $temporary_dismiss_url; |
| 12 |
public $pricing_url; |
| 13 |
public $current_user_id; |
| 14 |
|
| 15 |
/** |
| 16 |
* Constructor. |
| 17 |
*/ |
| 18 |
public function __construct($name, $type, $dismiss_url, $temporary_dismiss_url) |
| 19 |
{ |
| 20 |
|
| 21 |
$this->name = $name; |
| 22 |
$this->type = $type; |
| 23 |
$this->dismiss_url = $dismiss_url; |
| 24 |
$this->temporary_dismiss_url = $temporary_dismiss_url; |
| 25 |
$this->pricing_url = 'https://www.blockspare.com/'; |
| 26 |
$this->current_user_id = get_current_user_id(); |
| 27 |
|
| 28 |
add_action('admin_notices', array($this, 'notice')); |
| 29 |
|
| 30 |
$this->dismiss_notice(); |
| 31 |
$this->dismiss_notice_temporary(); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Display notice. |
| 36 |
*/ |
| 37 |
public function notice() |
| 38 |
{ |
| 39 |
|
| 40 |
if (! current_user_can('publish_posts')) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
$current_screen = get_current_screen(); |
| 45 |
|
| 46 |
if (! $current_screen) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
if ( |
| 52 |
'dashboard' !== $current_screen->id && |
| 53 |
'themes' !== $current_screen->id && |
| 54 |
'plugins' !== $current_screen->id |
| 55 |
) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if (! $this->is_dismiss_notice()) { |
| 60 |
$this->notice_markup(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Check if notice dismissed. |
| 66 |
*/ |
| 67 |
private function is_dismiss_notice() |
| 68 |
{ |
| 69 |
return apply_filters('blockspare_' . $this->name . '_notice_dismiss', false); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Notice markup. |
| 74 |
*/ |
| 75 |
public function notice_markup() |
| 76 |
{ |
| 77 |
echo ''; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Permanently dismiss notice. |
| 82 |
*/ |
| 83 |
public function dismiss_notice() |
| 84 |
{ |
| 85 |
|
| 86 |
if ( |
| 87 |
isset($_GET['blockspare_setup_notice_dismiss']) && |
| 88 |
isset($_GET['_blockspare_setup_notice_dismiss_nonce']) |
| 89 |
) { |
| 90 |
|
| 91 |
if ( |
| 92 |
! wp_verify_nonce( |
| 93 |
wp_unslash($_GET['_blockspare_setup_notice_dismiss_nonce']), |
| 94 |
'blockspare_setup_notice_dismiss_nonce' |
| 95 |
) |
| 96 |
) { |
| 97 |
wp_die(esc_html__('Action failed. Please refresh the page and retry.', 'blockspare')); |
| 98 |
} |
| 99 |
|
| 100 |
if (! current_user_can('publish_posts')) { |
| 101 |
wp_die(esc_html__('Cheatin’ huh?', 'blockspare')); |
| 102 |
} |
| 103 |
|
| 104 |
$dismiss_notice = sanitize_text_field(wp_unslash($_GET['blockspare_setup_notice_dismiss'])); |
| 105 |
|
| 106 |
// FIX: Match current notice name dynamically instead of hardcoded 'setup' |
| 107 |
if ($this->name === $dismiss_notice) { |
| 108 |
update_user_meta( |
| 109 |
$this->current_user_id, |
| 110 |
'blockspare_' . $dismiss_notice . '_notice_dismiss', |
| 111 |
'yes' |
| 112 |
); |
| 113 |
|
| 114 |
wp_safe_redirect(remove_query_arg(array('blockspare_setup_notice_dismiss', '_blockspare_setup_notice_dismiss_nonce'))); |
| 115 |
exit; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Temporarily dismiss notice. |
| 122 |
*/ |
| 123 |
public function dismiss_notice_temporary() |
| 124 |
{ |
| 125 |
|
| 126 |
if ( |
| 127 |
isset($_GET['blockspare_setup_notice_dismiss_temporary']) && |
| 128 |
isset($_GET['_blockspare_setup_notice_dismiss_temporary_nonce']) |
| 129 |
) { |
| 130 |
|
| 131 |
if ( |
| 132 |
! wp_verify_nonce( |
| 133 |
wp_unslash($_GET['_blockspare_setup_notice_dismiss_temporary_nonce']), |
| 134 |
'blockspare_setup_notice_dismiss_temporary_nonce' |
| 135 |
) |
| 136 |
) { |
| 137 |
wp_die(esc_html__('Action failed. Please refresh the page and retry.', 'blockspare')); |
| 138 |
} |
| 139 |
|
| 140 |
if (! current_user_can('publish_posts')) { |
| 141 |
wp_die(esc_html__('Cheatin’ huh?', 'blockspare')); |
| 142 |
} |
| 143 |
|
| 144 |
$dismiss_notice = sanitize_text_field(wp_unslash($_GET['blockspare_setup_notice_dismiss_temporary'])); |
| 145 |
|
| 146 |
// FIX: Match current notice name dynamically instead of hardcoded 'setup' |
| 147 |
if ($this->name === $dismiss_notice) { |
| 148 |
update_user_meta( |
| 149 |
$this->current_user_id, |
| 150 |
'blockspare_' . $dismiss_notice . '_notice_dismiss_temporary', |
| 151 |
'yes' |
| 152 |
); |
| 153 |
|
| 154 |
wp_safe_redirect(remove_query_arg(array('blockspare_setup_notice_dismiss_temporary', '_blockspare_setup_notice_dismiss_temporary_nonce'))); |
| 155 |
exit; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
class Blockspare_Setup_Notice extends Blockspare_Setup |
| 162 |
{ |
| 163 |
|
| 164 |
/** |
| 165 |
* Constructor. |
| 166 |
*/ |
| 167 |
public function __construct() |
| 168 |
{ |
| 169 |
|
| 170 |
$dismiss_url = wp_nonce_url( |
| 171 |
add_query_arg( |
| 172 |
'blockspare_setup_notice_dismiss', |
| 173 |
'setup', |
| 174 |
admin_url() |
| 175 |
), |
| 176 |
'blockspare_setup_notice_dismiss_nonce', |
| 177 |
'_blockspare_setup_notice_dismiss_nonce' |
| 178 |
); |
| 179 |
|
| 180 |
$temporary_dismiss_url = wp_nonce_url( |
| 181 |
add_query_arg( |
| 182 |
'blockspare_setup_notice_dismiss_temporary', |
| 183 |
'setup', |
| 184 |
admin_url() |
| 185 |
), |
| 186 |
'blockspare_setup_notice_dismiss_temporary_nonce', |
| 187 |
'_blockspare_setup_notice_dismiss_temporary_nonce' |
| 188 |
); |
| 189 |
|
| 190 |
parent::__construct( |
| 191 |
'setup', |
| 192 |
'info', |
| 193 |
$dismiss_url, |
| 194 |
$temporary_dismiss_url |
| 195 |
); |
| 196 |
|
| 197 |
$this->set_notice_time(); |
| 198 |
$this->set_temporary_dismiss_notice_time(); |
| 199 |
$this->set_dismiss_notice(); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Set initial notice time. |
| 204 |
*/ |
| 205 |
private function set_notice_time() |
| 206 |
{ |
| 207 |
if (! get_option('blockspare_setup_notice_start_time')) { |
| 208 |
update_option('blockspare_setup_notice_start_time', time()); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Set temporary dismiss time. |
| 214 |
*/ |
| 215 |
private function set_temporary_dismiss_notice_time() |
| 216 |
{ |
| 217 |
if ( |
| 218 |
isset($_GET['blockspare_setup_notice_dismiss_temporary']) && |
| 219 |
'setup' === $_GET['blockspare_setup_notice_dismiss_temporary'] |
| 220 |
) { |
| 221 |
update_user_meta( |
| 222 |
$this->current_user_id, |
| 223 |
'blockspare_setup_notice_dismiss_temporary_start_time', |
| 224 |
time() |
| 225 |
); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Set dismiss conditions. |
| 231 |
*/ |
| 232 |
public function set_dismiss_notice() |
| 233 |
{ |
| 234 |
$user_id = get_current_user_id(); |
| 235 |
|
| 236 |
$is_permanently_dismissed = ('yes' === get_user_meta($user_id, 'blockspare_setup_notice_dismiss', true)); |
| 237 |
|
| 238 |
$temporary_dismiss_time = (int) get_user_meta($user_id, 'blockspare_setup_notice_dismiss_temporary_start_time', true); |
| 239 |
$is_temporarily_dismissed = ($temporary_dismiss_time > strtotime('-2 days')); |
| 240 |
|
| 241 |
// FIX: Clean, predictable condition parsing |
| 242 |
if ($is_permanently_dismissed || $is_temporarily_dismissed) { |
| 243 |
add_filter('blockspare_setup_notice_dismiss', '__return_true'); |
| 244 |
} else { |
| 245 |
add_filter('blockspare_setup_notice_dismiss', '__return_false'); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Notice markup. |
| 251 |
*/ |
| 252 |
public function notice_markup() |
| 253 |
{ |
| 254 |
|
| 255 |
if (! function_exists('get_plugin_data')) { |
| 256 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 257 |
} |
| 258 |
|
| 259 |
$plugin_data = get_plugin_data(BLOCKSPARE_BASE_FILE); |
| 260 |
$version = isset($plugin_data['Version']) ? $plugin_data['Version'] : ''; |
| 261 |
|
| 262 |
?> |
| 263 |
|
| 264 |
<div class="notice notice-success blockspare-notice-setup" style="padding: 18px; position: relative;"> |
| 265 |
|
| 266 |
<div style="display:flex; justify-content:space-between; gap:20px; align-items:flex-start;"> |
| 267 |
|
| 268 |
<div style="flex:1;"> |
| 269 |
|
| 270 |
<h2 style="margin:0 0 8px; font-size:18px; line-height:1.4;"> |
| 271 |
<?php esc_html_e('Welcome to BlockSpare 👋', 'blockspare'); ?> |
| 272 |
</h2> |
| 273 |
|
| 274 |
<p style="margin:0 0 12px; font-size:14px; color:#555;"> |
| 275 |
<?php esc_html_e('Build professional news, magazine, blog, and business websites faster using ready-made Gutenberg blocks, starter templates, and content-focused layouts.', 'blockspare'); ?> |
| 276 |
</p> |
| 277 |
|
| 278 |
<div style="display:flex; gap:10px; flex-wrap:wrap; margin-top:10px;"> |
| 279 |
|
| 280 |
<a |
| 281 |
class="button button-primary" |
| 282 |
href="<?php echo esc_url(admin_url('post-new.php?post_type=page&blockspare_show_intro=true')); ?>"> |
| 283 |
<?php esc_html_e('Get Started', 'blockspare'); ?> |
| 284 |
</a> |
| 285 |
|
| 286 |
<a |
| 287 |
class="button" |
| 288 |
href="<?php echo esc_url(admin_url('admin.php?page=blockspare')); ?>"> |
| 289 |
<?php esc_html_e('Browse Templates', 'blockspare'); ?> |
| 290 |
</a> |
| 291 |
|
| 292 |
<a |
| 293 |
class="button button-secondary" |
| 294 |
href="<?php echo esc_url('https://blockspare.com/'); ?>" |
| 295 |
target="_blank"> |
| 296 |
<?php esc_html_e('Learn More', 'blockspare'); ?> |
| 297 |
</a> |
| 298 |
|
| 299 |
</div> |
| 300 |
|
| 301 |
<?php if ($version) : ?> |
| 302 |
<p style="margin-top:10px; font-size:12px; color:#888;"> |
| 303 |
<?php echo esc_html('Version ' . $version); ?> |
| 304 |
</p> |
| 305 |
<?php endif; ?> |
| 306 |
|
| 307 |
</div> |
| 308 |
|
| 309 |
<div style="min-width:120px; text-align:right;"> |
| 310 |
|
| 311 |
<div style="font-size:12px; color:#777; margin-bottom:8px;"> |
| 312 |
<?php esc_html_e('Quick Setup', 'blockspare'); ?> |
| 313 |
</div> |
| 314 |
|
| 315 |
<div style="font-size:11px; color:#999;"> |
| 316 |
<?php esc_html_e('Takes less than 2 minutes', 'blockspare'); ?> |
| 317 |
</div> |
| 318 |
|
| 319 |
</div> |
| 320 |
|
| 321 |
</div> |
| 322 |
|
| 323 |
<a |
| 324 |
class="notice-dismiss" |
| 325 |
href="<?php echo esc_url($this->dismiss_url); ?>"></a> |
| 326 |
|
| 327 |
</div> |
| 328 |
|
| 329 |
<?php |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Initialize notice after WordPress admin loads. |
| 335 |
*/ |
| 336 |
function blockspare_setup_notice_init() |
| 337 |
{ |
| 338 |
|
| 339 |
if (! is_admin()) { |
| 340 |
return; |
| 341 |
} |
| 342 |
|
| 343 |
new Blockspare_Setup_Notice(); |
| 344 |
} |
| 345 |
|
| 346 |
add_action('admin_init', 'blockspare_setup_notice_init'); |
| 347 |
|