PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
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 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / TemplateHooks / Admin / AdminThemesDataTemplate.php

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

129 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LearnPress\TemplateHooks\Admin;
4
5 use Exception;
6 use LP_Helper;
7 use stdClass;
8 use Throwable;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Class AdminThemesDataTemplate
14 *
15 * Display themes via AJAX
16 * @since 4.4.7
17 * @version 1.0.0
18 */
19 class AdminThemesDataTemplate {
20
21 protected static $url_themes_data = 'https://learnpress.github.io/learnpress/themes.json';
22
23 /**
24 * Register hooks.
25 *
26 * @return void
27 */
28 public static function init(): void {
29 add_filter( 'lp/rest/ajax/allow_callback', array( __CLASS__, 'allow_ajax_callback' ) );
30 }
31
32 /**
33 * Add the themes AJAX callback to the allowlist.
34 *
35 * @param array $callbacks Allowed AJAX callbacks.
36 *
37 * @return array
38 */
39 public static function allow_ajax_callback( array $callbacks ): array {
40 /** @use self::html_data_online */
41 $callbacks[] = __CLASS__ . ':html_data_online';
42
43 return $callbacks;
44 }
45
46 /**
47 * Render themes data for the AJAX response.
48 *
49 * @return stdClass
50 */
51 public static function html_data_online(): stdClass {
52 $data = self::get_remote_data();
53
54 $response = new stdClass();
55
56 $response->content = learn_press_admin_view_content(
57 'themes/data-online',
58 array( 'themes' => $data )
59 );
60
61 return $response;
62 }
63
64 /**
65 * Get themes data from the remote source or local fallback.
66 *
67 * @return array
68 */
69 protected static function get_remote_data(): array {
70 $data = array();
71
72 try {
73 if ( ! empty( self::$url_themes_data ) ) {
74 $response = wp_remote_get(
75 self::$url_themes_data,
76 array(
77 'timeout' => 30,
78 )
79 );
80
81 if ( ! is_wp_error( $response )
82 && 200 === wp_remote_retrieve_response_code( $response ) ) {
83 $data = LP_Helper::json_decode(
84 wp_remote_retrieve_body( $response ),
85 true
86 );
87 }
88 }
89
90 // Get data local
91 if ( empty( $data ) || ! is_array( $data ) ) {
92 $data = self::get_local_data();
93 }
94 } catch ( Throwable $e ) {
95 $data = array();
96 }
97
98 return $data;
99 }
100
101 /**
102 * Get themes data from the local JSON file.
103 *
104 * @return array
105 * @throws Exception If JSON decoding fails.
106 */
107 protected static function get_local_data(): array {
108 $file = LP_PLUGIN_PATH . 'inc/admin/views/themes/themes-data.json';
109
110 if ( ! file_exists( $file ) || ! is_readable( $file ) ) {
111 return array();
112 }
113
114 $content = file_get_contents( $file );
115
116 if ( false === $content ) {
117 return array();
118 }
119
120 $data = LP_Helper::json_decode( $content, true );
121
122 if ( empty( $data ) || ! is_array( $data ) ) {
123 return array();
124 }
125
126 return $data;
127 }
128 }
129