| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin templates for LearnPress notices. |
| 4 |
* |
| 5 |
* @package LearnPress\TemplateHooks\Admin\Tools |
| 6 |
* @since 4.4.6 |
| 7 |
* @version 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace LearnPress\TemplateHooks\Admin\Notices; |
| 11 |
|
| 12 |
use Exception; |
| 13 |
use LearnPress\Helpers\Response; |
| 14 |
use LearnPress\Helpers\Singleton; |
| 15 |
use LearnPress\Helpers\Template; |
| 16 |
use LearnPress\Models\UserModel; |
| 17 |
use LearnPress\Services\AdminNoticeService; |
| 18 |
use LearnPress\Services\AddonService; |
| 19 |
use LP_Settings; |
| 20 |
use LP_Updater; |
| 21 |
use stdClass; |
| 22 |
use Throwable; |
| 23 |
|
| 24 |
defined( 'ABSPATH' ) || exit; |
| 25 |
|
| 26 |
/** |
| 27 |
* Class AdminNotesTemplate |
| 28 |
* |
| 29 |
* @since 4.4.9 |
| 30 |
* @version 1.0.0 |
| 31 |
*/ |
| 32 |
class AdminNotesTemplate { |
| 33 |
use Singleton; |
| 34 |
|
| 35 |
/** |
| 36 |
* Hooks initialization. |
| 37 |
*/ |
| 38 |
public function init(): void { |
| 39 |
add_filter( 'lp/rest/ajax/allow_callback', [ $this, 'allow_callback' ] ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Register AJAX callbacks allowed for loading content via AJAX. |
| 44 |
* |
| 45 |
* @param array $callbacks List of allowed callbacks. |
| 46 |
* |
| 47 |
* @return array |
| 48 |
*/ |
| 49 |
public function allow_callback( array $callbacks ): array { |
| 50 |
$callbacks[] = self::class . ':render_notices'; |
| 51 |
$callbacks[] = self::class . ':dismiss_notice'; |
| 52 |
|
| 53 |
return $callbacks; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Aggregate and render all admin notices. |
| 58 |
* |
| 59 |
* @param array $data Request arguments. |
| 60 |
* |
| 61 |
* @return stdClass |
| 62 |
* @since 4.4.9 |
| 63 |
* @version 1.0.0 |
| 64 |
*/ |
| 65 |
public static function render_notices( array $data ): stdClass { |
| 66 |
$response = new stdClass(); |
| 67 |
$response->content = ''; |
| 68 |
$response->status = Response::STATUS_SUCCESS; |
| 69 |
|
| 70 |
try { |
| 71 |
// Check permission |
| 72 |
if ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR ) ) { |
| 73 |
throw new Exception( |
| 74 |
__( |
| 75 |
'You do not have permission to perform this action.', |
| 76 |
'learnpress' |
| 77 |
) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
$notices = [ |
| 82 |
self::html_check_wp_remote( $data ), |
| 83 |
self::html_check_plugin_base( $data ), |
| 84 |
self::html_lp_upgrade_db( $data ), |
| 85 |
self::html_lp_permalink( $data ), |
| 86 |
self::html_lp_setup_wizard( $data ), |
| 87 |
self::html_lp_addons_new_version( $data ), |
| 88 |
self::html_addons_need_extend( $data ), |
| 89 |
self::html_lp_beta_version( $data ), |
| 90 |
]; |
| 91 |
|
| 92 |
$response->content = implode( '', array_filter( $notices ) ); |
| 93 |
} catch ( Throwable $exception ) { |
| 94 |
$response->status = Response::STATUS_ERROR; |
| 95 |
$response->content = Template::print_message( |
| 96 |
$exception->getMessage(), |
| 97 |
Response::STATUS_ERROR, |
| 98 |
false |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
return $response; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Render a notice when wp_remote_get is unavailable. |
| 107 |
* |
| 108 |
* @param array $data Request arguments. |
| 109 |
* |
| 110 |
* @return string |
| 111 |
*/ |
| 112 |
public static function html_check_wp_remote( array $data = [] ): string { |
| 113 |
$result = AdminNoticeService::check_wp_remote(); |
| 114 |
if ( ! is_wp_error( $result ) ) { |
| 115 |
return ''; |
| 116 |
} |
| 117 |
|
| 118 |
$message = sprintf( |
| 119 |
'<div>%s</div>', |
| 120 |
sprintf( |
| 121 |
'<strong>wp_remote</strong>: %s. <a href="%s">%s</a>', |
| 122 |
$result->get_error_message(), |
| 123 |
esc_url( admin_url( 'site-health.php' ) ), |
| 124 |
esc_html__( 'Check Site Health', 'learnpress' ) |
| 125 |
) |
| 126 |
); |
| 127 |
|
| 128 |
return Template::print_message( $message, 'error', false ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Render a notice when the LearnPress plugin base directory is wrong. |
| 133 |
* |
| 134 |
* @param array $data Request arguments. |
| 135 |
* |
| 136 |
* @return string |
| 137 |
*/ |
| 138 |
public static function html_check_plugin_base( array $data = [] ): string { |
| 139 |
if ( ! AdminNoticeService::check_plugin_base() ) { |
| 140 |
return ''; |
| 141 |
} |
| 142 |
|
| 143 |
$message = sprintf( |
| 144 |
'<div>%s</div>', |
| 145 |
sprintf( |
| 146 |
/* translators: 1: expected plugin basename wrapped in strong, 2: current plugin basename wrapped in strong */ |
| 147 |
__( |
| 148 |
'The LearnPress plugin base directory must be %1$s (case-sensitive) to ensure all functions work properly and are fully operational (currently %2$s).', |
| 149 |
'learnpress' |
| 150 |
), |
| 151 |
'<strong>learnpress/learnpress.php</strong>', |
| 152 |
'<strong>' . LP_PLUGIN_BASENAME . '</strong>' |
| 153 |
) |
| 154 |
); |
| 155 |
|
| 156 |
return Template::print_message( $message, 'warning', false ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Render a notice to upgrade the LearnPress database. |
| 161 |
* |
| 162 |
* @param array $data Request arguments. |
| 163 |
* |
| 164 |
* @return string |
| 165 |
*/ |
| 166 |
public static function html_lp_upgrade_db( array $data = [] ): string { |
| 167 |
if ( ! LP_Updater::instance()->check_lp_db_need_upgrade() ) { |
| 168 |
return ''; |
| 169 |
} |
| 170 |
|
| 171 |
$message = sprintf( |
| 172 |
'<div>%s</div>', |
| 173 |
sprintf( |
| 174 |
'%s |
| 175 |
<div style="margin-top: 0.5em"> |
| 176 |
<a class="button lp-btn-go-upgrade-db" data-context="message" href="%s">%s</a> |
| 177 |
</div>', |
| 178 |
__( '<strong>LearnPress update</strong> – We need to update your database to the latest version.', 'learnpress' ), |
| 179 |
admin_url( 'admin.php?page=learn-press-tools&tab=database&action=upgrade-db' ), |
| 180 |
esc_html__( 'Go to Update', 'learnpress' ) |
| 181 |
) |
| 182 |
); |
| 183 |
|
| 184 |
return Template::print_message( $message, 'warning', false ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Render a notice when permalink structure is not enabled. |
| 189 |
* |
| 190 |
* @param array $data Request arguments. |
| 191 |
* |
| 192 |
* @return string |
| 193 |
*/ |
| 194 |
public static function html_lp_permalink( array $data = [] ): string { |
| 195 |
$permalink_structure = get_option( 'permalink_structure' ); |
| 196 |
if ( ! empty( $permalink_structure ) ) { |
| 197 |
return ''; |
| 198 |
} |
| 199 |
|
| 200 |
$message = sprintf( |
| 201 |
'<div>%s</div>', |
| 202 |
sprintf( |
| 203 |
'LearnPress requires permalink option <strong>Post name</strong> is enabled. Please enable it <a href="%s">here</a> to ensure that all functions work properly.', |
| 204 |
admin_url( 'options-permalink.php' ) |
| 205 |
) |
| 206 |
); |
| 207 |
|
| 208 |
return Template::print_message( $message, 'warning', false ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Render a notice to run the LearnPress setup wizard. |
| 213 |
* |
| 214 |
* @param array $data Request arguments. |
| 215 |
* |
| 216 |
* @return string |
| 217 |
*/ |
| 218 |
public static function html_lp_setup_wizard( array $data = [] ): string { |
| 219 |
if ( get_option( 'learn_press_setup_wizard_completed', false ) || |
| 220 |
LP_Settings::is_admin_notice_dismissed( 'lp-setup-wizard' ) ) { |
| 221 |
return ''; |
| 222 |
} |
| 223 |
|
| 224 |
$message = sprintf( |
| 225 |
'<div style="display: flex; gap: 5px; align-items: center">%s</div>', |
| 226 |
sprintf( |
| 227 |
'%s<a class="button button-primary" href="%s">%s</a>%s</p>', |
| 228 |
__( 'LearnPress LMS is ready to set up', 'learnpress' ), |
| 229 |
admin_url( 'index.php?page=lp-setup' ), |
| 230 |
__( 'Quick Setup', 'learnpress' ), |
| 231 |
self::html_button_dismiss( 'lp-setup-wizard' ) |
| 232 |
) |
| 233 |
); |
| 234 |
|
| 235 |
return Template::print_message( $message, 'success', false ); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Render a notice for new versions of LearnPress add-ons. |
| 240 |
* |
| 241 |
* @param array $data Request arguments. |
| 242 |
* |
| 243 |
* @return string |
| 244 |
*/ |
| 245 |
public static function html_lp_addons_new_version( array $data = [] ): string { |
| 246 |
/*if ( LP_Settings::is_admin_notice_dismissed( 'lp-addons-new-version' ) ) { |
| 247 |
return ''; |
| 248 |
}*/ |
| 249 |
|
| 250 |
$addons = AddonService::instance()->list_addon_new_version(); |
| 251 |
if ( empty( $addons ) ) { |
| 252 |
return ''; |
| 253 |
} |
| 254 |
|
| 255 |
$addons_html_arr = []; |
| 256 |
foreach ( $addons as $addon ) { |
| 257 |
$addons_html_arr[] = sprintf( |
| 258 |
'<a href="%s" class="">%s</a>', |
| 259 |
admin_url( 'admin.php?page=learn-press-addons&tab=update' ), |
| 260 |
$addon->name |
| 261 |
); |
| 262 |
} |
| 263 |
|
| 264 |
$addons_html = implode( ', ', $addons_html_arr ); |
| 265 |
|
| 266 |
$message = sprintf( |
| 267 |
'<div class="lp-addons-new-version-totals" data-total="%d">%s</div>', |
| 268 |
count( $addons ), |
| 269 |
sprintf( |
| 270 |
'%s: %s', |
| 271 |
__( 'New version available', 'learnpress' ), |
| 272 |
$addons_html |
| 273 |
) |
| 274 |
); |
| 275 |
|
| 276 |
return Template::print_message( $message, 'info', false ); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Render add-on licenses need extend notice. |
| 281 |
* |
| 282 |
* @param array $data Request arguments. |
| 283 |
* |
| 284 |
* @return string |
| 285 |
* @since 4.4.9 |
| 286 |
* @version 1.0.0 |
| 287 |
*/ |
| 288 |
public static function html_addons_need_extend( array $data = [] ): string { |
| 289 |
if ( LP_Settings::is_admin_notice_dismissed( 'lp-addons-purchased-extend' ) || |
| 290 |
! AddonService::instance()->check_addons_purchased_need_extend() ) { |
| 291 |
return ''; |
| 292 |
} |
| 293 |
|
| 294 |
$message = sprintf( |
| 295 |
'<div>%s</div>', |
| 296 |
sprintf( |
| 297 |
'%1$s <strong><a style="color: #E64B50" href="%2$s">%3$s</a></strong>%4$s', |
| 298 |
esc_html__( 'You have LearnPress Add-on licenses that need to be extended.', 'learnpress' ), |
| 299 |
esc_url( admin_url( 'admin.php?page=learn-press-addons&tab=license' ) ), |
| 300 |
esc_html__( 'Check now!', 'learnpress' ), |
| 301 |
self::html_button_dismiss( 'lp-addons-purchased-extend' ) |
| 302 |
) |
| 303 |
); |
| 304 |
|
| 305 |
return Template::print_message( |
| 306 |
$message, |
| 307 |
'info', |
| 308 |
false |
| 309 |
); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Render a notice for a new LearnPress beta version. |
| 314 |
* |
| 315 |
* @param array $data Request arguments. |
| 316 |
* |
| 317 |
* @return string |
| 318 |
*/ |
| 319 |
public static function html_lp_beta_version( array $data = [] ): string { |
| 320 |
$lp_beta_version_info = AdminNoticeService::check_lp_beta_version(); |
| 321 |
if ( ! $lp_beta_version_info instanceof stdClass ) { |
| 322 |
return ''; |
| 323 |
} |
| 324 |
|
| 325 |
$admin_notices_dismiss = get_option( 'lp_admin_notices_dismiss', [] ); |
| 326 |
if ( isset( $admin_notices_dismiss['lp-beta-version'] ) || |
| 327 |
! AddonService::instance()->check_addons_purchased_need_extend() ) { |
| 328 |
return ''; |
| 329 |
} |
| 330 |
|
| 331 |
$version = $lp_beta_version_info->version ?? ''; |
| 332 |
$link_download = $lp_beta_version_info->link_download ?? ''; |
| 333 |
$link_feedback = $lp_beta_version_info->link_feedback ?? ''; |
| 334 |
$link_changelog = $lp_beta_version_info->link_changelog ?? ''; |
| 335 |
$description = __( |
| 336 |
'This version of the LearnPress software is under development. Please do not install, run, or test this version of LearnPress on production or mission-critical websites. Instead, it is recommended that you test Beta version on a staging site.', |
| 337 |
'learnpress' |
| 338 |
); |
| 339 |
|
| 340 |
$title = sprintf( |
| 341 |
/* translators: %s: beta version number */ |
| 342 |
__( 'LearnPress %s Beta is available', 'learnpress' ), |
| 343 |
esc_html( $version ) |
| 344 |
); |
| 345 |
|
| 346 |
$message = sprintf( |
| 347 |
'<div>%s</div>', |
| 348 |
sprintf( |
| 349 |
'<h3>%1$s</h3> |
| 350 |
<p>%2$s</p> |
| 351 |
<p> |
| 352 |
<a class="button" href="%3$s" target="_blank">%4$s</a> |
| 353 |
<a class="button" href="%5$s" target="_blank">%6$s</a> |
| 354 |
<a class="button" href="%7$s" target="_blank">%8$s</a> |
| 355 |
</p>%9$s', |
| 356 |
esc_html( $title ), |
| 357 |
esc_html( $description ), |
| 358 |
esc_url( $link_download ), |
| 359 |
__( 'Download', 'learnpress' ), |
| 360 |
esc_url( $link_feedback ), |
| 361 |
__( 'Feedback', 'learnpress' ), |
| 362 |
esc_url( $link_changelog ), |
| 363 |
__( 'Changelog', 'learnpress' ), |
| 364 |
self::html_button_dismiss( 'lp-beta-version' ) |
| 365 |
) |
| 366 |
); |
| 367 |
|
| 368 |
return Template::print_message( $message, 'info', false ); |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Render a dismiss button for a notice. |
| 373 |
* |
| 374 |
* @param string $key Notice key. |
| 375 |
* |
| 376 |
* @return string |
| 377 |
*/ |
| 378 |
public static function html_button_dismiss( string $key ): string { |
| 379 |
$data_send = wp_json_encode( |
| 380 |
array( |
| 381 |
'args' => array( 'dismiss' => $key ), |
| 382 |
/** @use self::dismiss_notice */ |
| 383 |
'callback' => array( |
| 384 |
'class' => AdminNotesTemplate::class, |
| 385 |
'method' => 'dismiss_notice', |
| 386 |
), |
| 387 |
) |
| 388 |
); |
| 389 |
|
| 390 |
return sprintf( |
| 391 |
'<span class="btn-lp-notice-dismiss lp-icon-close" |
| 392 |
data-dismiss="%1$s" |
| 393 |
data-send="%2$s" |
| 394 |
data-message="%3$s" |
| 395 |
title="%4$s"> |
| 396 |
</span>', |
| 397 |
esc_attr( $key ), |
| 398 |
esc_attr( $data_send ), |
| 399 |
esc_attr__( 'Are you sure you want to dismiss this notice?', 'learnpress' ), |
| 400 |
esc_attr__( 'Dismiss notice', 'learnpress' ) |
| 401 |
); |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Dismiss an admin notice. |
| 406 |
* |
| 407 |
* @param array $data Request arguments. |
| 408 |
* |
| 409 |
* @return stdClass |
| 410 |
*/ |
| 411 |
public static function dismiss_notice( array $data = [] ): stdClass { |
| 412 |
$content = new stdClass(); |
| 413 |
$content->content = ''; |
| 414 |
$content->status = Response::STATUS_SUCCESS; |
| 415 |
$content->message = ''; |
| 416 |
|
| 417 |
try { |
| 418 |
if ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR ) ) { |
| 419 |
throw new Exception( |
| 420 |
__( 'You do not have permission to perform this action.', 'learnpress' ) |
| 421 |
); |
| 422 |
} |
| 423 |
|
| 424 |
$key = sanitize_key( $data['dismiss'] ?? '' ); |
| 425 |
if ( empty( $key ) ) { |
| 426 |
throw new Exception( __( 'The notice key is invalid.', 'learnpress' ) ); |
| 427 |
} |
| 428 |
|
| 429 |
$admin_notices_dismiss = LP_Settings::get_admin_notices_dismiss(); |
| 430 |
$admin_notices_dismiss[ $key ] = $key; |
| 431 |
update_option( 'lp_admin_notices_dismiss', $admin_notices_dismiss ); |
| 432 |
|
| 433 |
$content->message = __( 'Note Dismissed!', 'learnpress' ); |
| 434 |
} catch ( Throwable $exception ) { |
| 435 |
$content->status = Response::STATUS_ERROR; |
| 436 |
$content->message = $exception->getMessage(); |
| 437 |
} |
| 438 |
|
| 439 |
return $content; |
| 440 |
} |
| 441 |
} |
| 442 |
|