update_template_library(); } if ( empty( get_option( self::ELEMENTOR_TEMPLATE_OPTION_KEY ) ) ) { $this->update_elementor_template_library(); } set_transient( self::AUTOFETCH_TRANSIENT_KEY, true, HOUR_IN_SECONDS ); } /** * 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() { return $this->update_templates_from_remote( '/template-library.json', self::TEMPLATE_OPTION_KEY ); } /** * Ajax handler for getting template library */ public function get_template_library() { $this->get_templates_ajax( self::TEMPLATE_OPTION_KEY, 'update_template_library' ); } /** * Update Elementor template library from remote source */ public function update_elementor_template_library() { return $this->update_templates_from_remote( '/elementor-template-library.json', self::ELEMENTOR_TEMPLATE_OPTION_KEY ); } /** * Ajax handler for getting Elementor template library */ public function get_elementor_template_library() { $this->get_templates_ajax( self::ELEMENTOR_TEMPLATE_OPTION_KEY, 'update_elementor_template_library' ); } /** * Generic method to handle template library AJAX requests * * @param string $option_key The option key to retrieve templates from * @param string $update_method The method name to call if templates are empty */ private function get_templates_ajax( $option_key, $update_method ) { check_ajax_referer( 'affiliatex_ajax_nonce', 'nonce' ); $templates = get_option( $option_key, array() ); if ( empty( $templates ) ) { // If no templates in database, try to fetch them $this->$update_method(); $templates = get_option( $option_key, array() ); } wp_send_json_success( $templates ); } /** * Generic method to update templates from remote source * * @param string $json_file The JSON file path on the remote server * @param string $option_key The option key to store templates * @return bool True on success, false on failure */ private function update_templates_from_remote( $json_file, $option_key ) { $response = wp_remote_get( self::TEMPLATE_LIBRARY_DOMAIN . $json_file ); 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( $option_key, $data ); return true; } return false; } /** * Check if Elementor templates exist */ public function has_elementor_templates() { $templates = get_option( self::ELEMENTOR_TEMPLATE_OPTION_KEY, array() ); return ! empty( $templates ); } } AffiliateXTemplateLibrary::instance();