update_template_library(); } } /** * Schedule daily template update */ public function schedule_template_update() { if (!wp_next_scheduled('affiliatex_daily_template_update')) { wp_schedule_event(time(), 'daily', 'affiliatex_daily_template_update'); } } /** * Update template library from remote source */ public function update_template_library() { $response = wp_remote_get(self::TEMPLATE_LIBRARY_DOMAIN . '/template-library.json'); if (is_wp_error($response)) { return false; } $body = wp_remote_retrieve_body($response); if (empty($body)) { return false; } $data = json_decode($body, true); if (json_last_error() === JSON_ERROR_NONE) { update_option(self::TEMPLATE_OPTION_KEY, $data); return true; } return false; } /** * Ajax handler for getting template library */ public function get_template_library() { check_ajax_referer('affiliatex_ajax_nonce', 'nonce'); $templates = get_option(self::TEMPLATE_OPTION_KEY, array()); if (empty($templates)) { // If no templates in database, try to fetch them $this->update_template_library(); $templates = get_option(self::TEMPLATE_OPTION_KEY, array()); } wp_send_json_success($templates); } } AffiliateXTemplateLibrary::instance();