| @@ -12,5 +12,46 @@ | ||
| 12 | 12 | |
| 13 | 13 | // abstract public function get_saved_templates( $params = [] ); |
| 14 | 14 | // abstract public function delete( $params = [] ); |
| 15 | 15 | abstract public function create_page( $id, $title, $importer = null, $settings = [] ); |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Resolve remote template_type to the correct Builder template type. | |
| 19 | + * Maps API template types to internal Builder keys with fallback to page_single. | |
| 20 | + * | |
| 21 | + * @param array $template_data Template data from API containing 'template_type' key | |
| 22 | + * | |
| 23 | + * @return string Builder template type key | |
| 24 | + */ | |
| 25 | + protected static function resolve_library_type( array $template_data ): string { | |
| 26 | + $template_type = (string) ( $template_data['template_type'] ?? '' ); | |
| 27 | + | |
| 28 | + if ( $template_type === '' ) { | |
| 29 | + throw new \InvalidArgumentException( "template_type is required for library import." ); | |
| 30 | + } | |
| 31 | + | |
| 32 | + // Direct match: if the slug is already a registered Builder type key, use it as-is | |
| 33 | + // (e.g. 'header', 'footer', 'archive', 'search', 'docs_single', 'docs_archive', etc.) | |
| 34 | + $registered_types = templately()->theme_builder::$templates_manager->get_template_types(); | |
| 35 | + if ( isset( $registered_types[ $template_type ] ) ) { | |
| 36 | + return $template_type; | |
| 37 | + } | |
| 38 | + | |
| 39 | + // Translation map for API slugs that differ from their Builder type key | |
| 40 | + $map = [ | |
| 41 | + 'single-post' => 'single', | |
| 42 | + 'single-doc' => 'docs_single', | |
| 43 | + 'single-product' => 'product_single', | |
| 44 | + 'fluent-product-single' => 'fluent_product_single', | |
| 45 | + 'docs-archive' => 'docs_archive', | |
| 46 | + 'course-archive' => 'course_archive', | |
| 47 | + 'product-archive' => 'product_archive', | |
| 48 | + '404' => 'error', | |
| 49 | + ]; | |
| 50 | + | |
| 51 | + if ( isset( $map[ $template_type ] ) ) { | |
| 52 | + return $map[ $template_type ]; | |
| 53 | + } | |
| 54 | + | |
| 55 | + throw new \InvalidArgumentException( "Unknown template_type '{$template_type}' — no matching Builder type." ); | |
| 56 | + } | |
| 16 | 57 | } |