PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.10
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.10
4.4.10 4.4.9.1 4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 All 142 releases
learnpress / inc / TemplateHooks / Admin / Notices / AdminNotesTemplate.php

AdminNotesTemplate.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.10, at inc/TemplateHooks/Admin/Notices/AdminNotesTemplate.php

425 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 '<strong>wp_remote</strong>: %s. <a href="%s">%s</a>',
120 $result->get_error_message(),
121 esc_url( admin_url( 'site-health.php' ) ),
122 esc_html__( 'Check Site Health', 'learnpress' )
123 );
124
125 return Template::print_message( $message, 'error', false );
126 }
127
128 /**
129 * Render a notice when the LearnPress plugin base directory is wrong.
130 *
131 * @param array $data Request arguments.
132 *
133 * @return string
134 */
135 public static function html_check_plugin_base( array $data = [] ): string {
136 if ( ! AdminNoticeService::check_plugin_base() ) {
137 return '';
138 }
139
140 $message = sprintf(
141 /* translators: 1: expected plugin basename wrapped in strong, 2: current plugin basename wrapped in strong */
142 __(
143 'The LearnPress plugin base directory must be %1$s (case-sensitive) to ensure all functions work properly and are fully operational (currently %2$s).',
144 'learnpress'
145 ),
146 '<strong>learnpress/learnpress.php</strong>',
147 '<strong>' . LP_PLUGIN_BASENAME . '</strong>'
148 );
149
150 return Template::print_message( $message, 'warning', false );
151 }
152
153 /**
154 * Render a notice to upgrade the LearnPress database.
155 *
156 * @param array $data Request arguments.
157 *
158 * @return string
159 * @throws Exception
160 */
161 public static function html_lp_upgrade_db( array $data = [] ): string {
162 if ( ! LP_Updater::instance()->check_lp_db_need_upgrade() ) {
163 return '';
164 }
165
166 $message = sprintf(
167 '%s
168 <div style="margin-top: 0.5em">
169 <a class="button lp-btn-go-upgrade-db" data-context="message" href="%s">%s</a>
170 </div>',
171 __( '<strong>LearnPress update</strong> – We need to update your database to the latest version.', 'learnpress' ),
172 admin_url( 'admin.php?page=learn-press-tools&tab=database&action=upgrade-db' ),
173 esc_html__( 'Go to Update', 'learnpress' )
174 );
175
176 return Template::print_message( $message, 'warning', false );
177 }
178
179 /**
180 * Render a notice when permalink structure is not enabled.
181 *
182 * @param array $data Request arguments.
183 *
184 * @return string
185 */
186 public static function html_lp_permalink( array $data = [] ): string {
187 $permalink_structure = get_option( 'permalink_structure' );
188 if ( ! empty( $permalink_structure ) ) {
189 return '';
190 }
191
192 $message = sprintf(
193 '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.',
194 admin_url( 'options-permalink.php' )
195 );
196
197 return Template::print_message( $message, 'warning', false );
198 }
199
200 /**
201 * Render a notice to run the LearnPress setup wizard.
202 *
203 * @param array $data Request arguments.
204 *
205 * @return string
206 */
207 public static function html_lp_setup_wizard( array $data = [] ): string {
208 if ( get_option( 'learn_press_setup_wizard_completed', false ) ||
209 LP_Settings::is_admin_notice_dismissed( 'lp-setup-wizard' ) ) {
210 return '';
211 }
212
213 $message = sprintf(
214 '<div style="display: flex; gap: 5px; align-items: center">%s</div>',
215 sprintf(
216 '%s<a class="button button-primary" href="%s">%s</a>%s</p>',
217 __( 'LearnPress LMS is ready to set up', 'learnpress' ),
218 admin_url( 'index.php?page=lp-setup' ),
219 __( 'Quick Setup', 'learnpress' ),
220 self::html_button_dismiss( 'lp-setup-wizard' )
221 )
222 );
223
224 return Template::print_message( $message, 'success', false );
225 }
226
227 /**
228 * Render a notice for new versions of LearnPress add-ons.
229 *
230 * @param array $data Request arguments.
231 *
232 * @return string
233 */
234 public static function html_lp_addons_new_version( array $data = [] ): string {
235 /*if ( LP_Settings::is_admin_notice_dismissed( 'lp-addons-new-version' ) ) {
236 return '';
237 }*/
238
239 $addons = AddonService::instance()->list_addon_new_version();
240 if ( empty( $addons ) ) {
241 return '';
242 }
243
244 $addons_html_arr = [];
245 foreach ( $addons as $addon ) {
246 $addons_html_arr[] = sprintf(
247 '<a href="%s" class="">%s</a>',
248 admin_url( 'admin.php?page=learn-press-addons&tab=update' ),
249 $addon->name
250 );
251 }
252
253 $addons_html = implode( ', ', $addons_html_arr );
254
255 $message = sprintf(
256 '<div class="lp-addons-new-version-totals" data-total="%d">%s</div>',
257 count( $addons ),
258 sprintf(
259 '%s: %s',
260 __( 'New version available', 'learnpress' ),
261 $addons_html
262 )
263 );
264
265 return Template::print_message( $message, 'info', false );
266 }
267
268 /**
269 * Render add-on licenses need extend notice.
270 *
271 * @param array $data Request arguments.
272 *
273 * @return string
274 * @since 4.4.9
275 * @version 1.0.0
276 */
277 public static function html_addons_need_extend( array $data = [] ): string {
278 if ( LP_Settings::is_admin_notice_dismissed( 'lp-addons-purchased-extend' ) ||
279 ! AddonService::instance()->check_addons_purchased_need_extend() ) {
280 return '';
281 }
282
283 $message = sprintf(
284 '%1$s <strong><a style="color: #E64B50" href="%2$s">%3$s</a></strong>%4$s',
285 esc_html__( 'You have LearnPress Add-on licenses that need to be extended.', 'learnpress' ),
286 esc_url( admin_url( 'admin.php?page=learn-press-addons&tab=license' ) ),
287 esc_html__( 'Check now!', 'learnpress' ),
288 self::html_button_dismiss( 'lp-addons-purchased-extend' )
289 );
290
291 return Template::print_message(
292 $message,
293 'info',
294 false
295 );
296 }
297
298 /**
299 * Render a notice for a new LearnPress beta version.
300 *
301 * @param array $data Request arguments.
302 *
303 * @return string
304 */
305 public static function html_lp_beta_version( array $data = [] ): string {
306 $lp_beta_version_info = AdminNoticeService::check_lp_beta_version();
307 if ( ! $lp_beta_version_info instanceof stdClass ) {
308 return '';
309 }
310
311 $admin_notices_dismiss = get_option( 'lp_admin_notices_dismiss', [] );
312 if ( isset( $admin_notices_dismiss['lp-beta-version'] ) ||
313 ! AddonService::instance()->check_addons_purchased_need_extend() ) {
314 return '';
315 }
316
317 $version = $lp_beta_version_info->version ?? '';
318 $link_download = $lp_beta_version_info->link_download ?? '';
319 $link_feedback = $lp_beta_version_info->link_feedback ?? '';
320 $link_changelog = $lp_beta_version_info->link_changelog ?? '';
321 $description = __(
322 '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.',
323 'learnpress'
324 );
325
326 $title = sprintf(
327 /* translators: %s: beta version number */
328 __( 'LearnPress %s Beta is available', 'learnpress' ),
329 esc_html( $version )
330 );
331
332 $message = sprintf(
333 '<h3>%1$s</h3>
334 <p>%2$s</p>
335 <p>
336 <a class="button" href="%3$s" target="_blank">%4$s</a>
337 <a class="button" href="%5$s" target="_blank">%6$s</a>
338 <a class="button" href="%7$s" target="_blank">%8$s</a>
339 </p>%9$s',
340 esc_html( $title ),
341 esc_html( $description ),
342 esc_url( $link_download ),
343 __( 'Download', 'learnpress' ),
344 esc_url( $link_feedback ),
345 __( 'Feedback', 'learnpress' ),
346 esc_url( $link_changelog ),
347 __( 'Changelog', 'learnpress' ),
348 self::html_button_dismiss( 'lp-beta-version' )
349 );
350
351 return Template::print_message( $message, 'info', false );
352 }
353
354 /**
355 * Render a dismiss button for a notice.
356 *
357 * @param string $key Notice key.
358 *
359 * @return string
360 */
361 public static function html_button_dismiss( string $key ): string {
362 $data_send = wp_json_encode(
363 array(
364 'args' => array( 'dismiss' => $key ),
365 /** @use self::dismiss_notice */
366 'callback' => array(
367 'class' => AdminNotesTemplate::class,
368 'method' => 'dismiss_notice',
369 ),
370 )
371 );
372
373 return sprintf(
374 '<span class="btn-lp-notice-dismiss lp-icon-close"
375 data-dismiss="%1$s"
376 data-send="%2$s"
377 data-message="%3$s"
378 title="%4$s">
379 </span>',
380 esc_attr( $key ),
381 esc_attr( $data_send ),
382 esc_attr__( 'Are you sure you want to dismiss this notice?', 'learnpress' ),
383 esc_attr__( 'Dismiss notice', 'learnpress' )
384 );
385 }
386
387 /**
388 * Dismiss an admin notice.
389 *
390 * @param array $data Request arguments.
391 *
392 * @return stdClass
393 */
394 public static function dismiss_notice( array $data = [] ): stdClass {
395 $content = new stdClass();
396 $content->content = '';
397 $content->status = Response::STATUS_SUCCESS;
398 $content->message = '';
399
400 try {
401 if ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR ) ) {
402 throw new Exception(
403 __( 'You do not have permission to perform this action.', 'learnpress' )
404 );
405 }
406
407 $key = sanitize_key( $data['dismiss'] ?? '' );
408 if ( empty( $key ) ) {
409 throw new Exception( __( 'The notice key is invalid.', 'learnpress' ) );
410 }
411
412 $admin_notices_dismiss = LP_Settings::get_admin_notices_dismiss();
413 $admin_notices_dismiss[ $key ] = $key;
414 update_option( 'lp_admin_notices_dismiss', $admin_notices_dismiss );
415
416 $content->message = __( 'Note Dismissed!', 'learnpress' );
417 } catch ( Throwable $exception ) {
418 $content->status = Response::STATUS_ERROR;
419 $content->message = $exception->getMessage();
420 }
421
422 return $content;
423 }
424 }
425