| 1 |
<?php |
| 2 |
namespace LearnPress\TemplateHooks\Admin; |
| 3 |
|
| 4 |
use Exception; |
| 5 |
use LP_Helper; |
| 6 |
use LP_WP_Filesystem; |
| 7 |
use stdClass; |
| 8 |
|
| 9 |
/** |
| 10 |
* Template Show list items to select in popup. |
| 11 |
* |
| 12 |
* @since 4.4.5 |
| 13 |
* @version 1.0.0 |
| 14 |
*/ |
| 15 |
class AdminHelpCenterDataTemplate { |
| 16 |
/** |
| 17 |
* URL of the remote "What's New" + "Latest Articles" JSON. |
| 18 |
* |
| 19 |
* Fetched with wp_remote_get (30s timeout, no auth) and cached for 12 hours. |
| 20 |
* Falls back to the local demo file so the page still renders when the |
| 21 |
* endpoint is unavailable. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected static $url_help_center_data = 'https://learnpress.github.io/learnpress/help-center-data.json'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Render the online Help Center data section via AJAX. |
| 29 |
* |
| 30 |
* @return stdClass Object with a `content` property containing the rendered HTML. |
| 31 |
*/ |
| 32 |
public static function html_data_online(): stdClass { |
| 33 |
$remote_data = self::get_remote_data(); |
| 34 |
$response = new stdClass(); |
| 35 |
|
| 36 |
$response->content = learn_press_admin_view_content( |
| 37 |
'help-center/data-online', |
| 38 |
array( |
| 39 |
'whats_new' => $remote_data['whats_new'] ?? array(), |
| 40 |
'articles' => $remote_data['articles'] ?? array(), |
| 41 |
'banner_ad' => $remote_data['banner_ad'] ?? array(), |
| 42 |
'tick_icon' => LP_WP_Filesystem::get_icon_svg( 'help-center/ico-hc-tick.svg' ), |
| 43 |
) |
| 44 |
); |
| 45 |
|
| 46 |
return $response; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* "What's New" + "Latest Articles" + "Banner Ad" data. |
| 51 |
* |
| 52 |
* Fetches $url_help_center_data with wp_remote_get (30s timeout, no auth). |
| 53 |
* The response is cached for 12 hours. Falls back to the local demo JSON |
| 54 |
* bundled with the plugin when the URL isn't set yet or the request fails, |
| 55 |
* so the page keeps rendering while the schema is finalized. |
| 56 |
* |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
protected static function get_remote_data(): array { |
| 60 |
$default = array( |
| 61 |
'whats_new' => array(), |
| 62 |
'articles' => array(), |
| 63 |
'banner_ad' => array(), |
| 64 |
); |
| 65 |
|
| 66 |
$data = []; |
| 67 |
|
| 68 |
try { |
| 69 |
if ( ! empty( self::$url_help_center_data ) ) { |
| 70 |
$response = wp_remote_get( self::$url_help_center_data, array( 'timeout' => 30 ) ); |
| 71 |
|
| 72 |
if ( ! is_wp_error( $response ) |
| 73 |
&& 200 === wp_remote_retrieve_response_code( $response ) ) { |
| 74 |
$data = LP_Helper::json_decode( wp_remote_retrieve_body( $response ), true ); |
| 75 |
} |
| 76 |
} |
| 77 |
} catch ( Exception $e ) { |
| 78 |
$data = $default; |
| 79 |
} |
| 80 |
|
| 81 |
return $data; |
| 82 |
} |
| 83 |
} |
| 84 |
|