| @@ -20,8 +20,13 @@ | ||
| 20 | 20 | class TemplateSource extends Source_Base { |
| 21 | 21 | protected $template_prefix = 'betterdocs_'; |
| 22 | 22 | protected $cloud_url = 'https://betterdocs.co/wp-json/bd_cloud/v1/'; |
| 23 | 23 | |
| 24 | + /** | |
| 25 | + * Local template IDs (templates bundled with the plugin) | |
| 26 | + */ | |
| 27 | + const LOCAL_TAG_ARCHIVE_ID = 'betterdocs_local_tag_archive_1'; | |
| 28 | + | |
| 24 | 29 | public function get_prefix() { |
| 25 | 30 | return $this->template_prefix; |
| 26 | 31 | } |
| 27 | 32 | |
| @@ -35,21 +40,34 @@ | ||
| 35 | 40 | |
| 36 | 41 | public function register_data() {} |
| 37 | 42 | |
| 38 | 43 | public function get_items( $args = [] ) { |
| 39 | - $url = $this->cloud_url . 'templates'; | |
| 40 | - $response = wp_remote_get( $url, [ 'timeout' => 60 ] ); | |
| 41 | - $body = wp_remote_retrieve_body( $response ); | |
| 42 | - $body = json_decode( $body, true ); | |
| 43 | - $templates_data = ! empty( $body['data'] ) ? $body['data'] : []; | |
| 44 | - $templates = []; | |
| 44 | + $_cache_key = 'betterdocs_remote_templates_v1'; | |
| 45 | + $templates = get_transient( $_cache_key ); | |
| 45 | 46 | |
| 46 | - if ( ! empty( $templates_data ) ) { | |
| 47 | - foreach ( $templates_data as $template_data ) { | |
| 48 | - $templates[] = $this->prepare_template( $template_data ); | |
| 47 | + if ( false === $templates ) { | |
| 48 | + $templates = []; | |
| 49 | + | |
| 50 | + // Fetch remote templates | |
| 51 | + $url = $this->cloud_url . 'templates'; | |
| 52 | + $response = wp_remote_get( $url, [ 'timeout' => 60 ] ); | |
| 53 | + $body = wp_remote_retrieve_body( $response ); | |
| 54 | + $body = json_decode( $body, true ); | |
| 55 | + $templates_data = ! empty( $body['data'] ) ? $body['data'] : []; | |
| 56 | + | |
| 57 | + if ( ! empty( $templates_data ) ) { | |
| 58 | + foreach ( $templates_data as $template_data ) { | |
| 59 | + $templates[] = $this->prepare_template( $template_data ); | |
| 60 | + } | |
| 49 | 61 | } |
| 62 | + | |
| 63 | + set_transient( $_cache_key, $templates, DAY_IN_SECONDS ); | |
| 50 | 64 | } |
| 51 | 65 | |
| 66 | + // Note: local bundled templates are NOT included here. | |
| 67 | + // They are injected exclusively via Elementor.php::get_templates() → http_response filter | |
| 68 | + // to avoid duplicates (Elementor calls get_items() on registered sources separately). | |
| 69 | + | |
| 52 | 70 | if ( ! empty( $args ) ) { |
| 53 | 71 | $templates = wp_list_filter( $templates, $args ); |
| 54 | 72 | } |
| 55 | 73 | |
| @@ -55,8 +73,36 @@ | ||
| 55 | 73 | |
| 56 | 74 | return $templates; |
| 57 | 75 | } |
| 58 | 76 | |
| 77 | + /** | |
| 78 | + * Returns locally bundled premade templates (no remote fetch required). | |
| 79 | + * | |
| 80 | + * @return array | |
| 81 | + */ | |
| 82 | + public function get_local_templates() { | |
| 83 | + return [ | |
| 84 | + [ | |
| 85 | + 'accessLevel' => 0, | |
| 86 | + 'template_id' => self::LOCAL_TAG_ARCHIVE_ID, | |
| 87 | + 'source' => 'remote', | |
| 88 | + 'type' => 'block', | |
| 89 | + 'subtype' => 'Docs Archive', | |
| 90 | + 'title' => __( 'BetterDocs Tag Archive', 'betterdocs' ), | |
| 91 | + 'thumbnail' => plugins_url( '/assets/static/admin/images/templates/tag-archive-thumb.png', BETTERDOCS_PLUGIN_FILE ), | |
| 92 | + 'date' => '2024-01-01', | |
| 93 | + 'author' => 'WPDeveloper', | |
| 94 | + 'tags' => [ 'Docs Archive' ], | |
| 95 | + 'isPro' => false, | |
| 96 | + 'popularityIndex' => 0, | |
| 97 | + 'trendIndex' => 0, | |
| 98 | + 'hasPageSettings' => false, | |
| 99 | + 'url' => plugins_url( '/assets/static/admin/images/templates/tag-archive-thumb.png', BETTERDOCS_PLUGIN_FILE ), | |
| 100 | + 'favorite' => false, | |
| 101 | + ], | |
| 102 | + ]; | |
| 103 | + } | |
| 104 | + | |
| 59 | 105 | public function prepare_template( $template_data ) { |
| 60 | 106 | return [ |
| 61 | 107 | 'accessLevel' => 0, |
| 62 | 108 | 'template_id' => $template_data['template_id'], |
| @@ -111,8 +157,13 @@ | ||
| 111 | 157 | return false; |
| 112 | 158 | } |
| 113 | 159 | |
| 114 | 160 | public function get_data( array $args, $context = 'display' ) { |
| 161 | + // Serve local bundled templates without a remote HTTP request | |
| 162 | + if ( isset( $args['template_id'] ) && $args['template_id'] === self::LOCAL_TAG_ARCHIVE_ID ) { | |
| 163 | + return $this->get_local_tag_archive_data(); | |
| 164 | + } | |
| 165 | + | |
| 115 | 166 | $file_url = $this->cloud_url . 'template/' . $args['template_id']; |
| 116 | 167 | $request = wp_remote_get( $file_url ); |
| 117 | 168 | $response = wp_remote_retrieve_body( $request ); |
| 118 | 169 | $body = json_decode( $response, true ); |
| @@ -120,8 +171,153 @@ | ||
| 120 | 171 | |
| 121 | 172 | $result = []; |
| 122 | 173 | |
| 123 | 174 | $result['content'] = $this->replace_elements_ids( $data ); |
| 175 | + $result['content'] = $this->process_export_import_content( $result['content'], 'on_import' ); | |
| 176 | + $result['page_settings'] = []; | |
| 177 | + | |
| 178 | + return $result; | |
| 179 | + } | |
| 180 | + | |
| 181 | + /** | |
| 182 | + * Returns the Elementor template content for the Tag Archive premade template. | |
| 183 | + * Layout (mirrors the screenshot): Search bar (full-width) + Sidebar / Content two-column area. | |
| 184 | + * | |
| 185 | + * @return array | |
| 186 | + */ | |
| 187 | + protected function get_local_tag_archive_data() { | |
| 188 | + $content = [ | |
| 189 | + // ── Row 1: Search bar (full-width container) ────────────────────── | |
| 190 | + [ | |
| 191 | + 'id' => 'bdta0001', | |
| 192 | + 'elType' => 'container', | |
| 193 | + 'settings' => [ | |
| 194 | + 'flex_direction' => 'row', | |
| 195 | + 'content_width' => 'full', | |
| 196 | + 'padding' => [ | |
| 197 | + 'unit' => 'px', | |
| 198 | + 'top' => '20', | |
| 199 | + 'right' => '20', | |
| 200 | + 'bottom' => '20', | |
| 201 | + 'left' => '20', | |
| 202 | + 'isLinked' => false, | |
| 203 | + ], | |
| 204 | + ], | |
| 205 | + 'elements' => [ | |
| 206 | + [ | |
| 207 | + 'id' => 'bdta0003', | |
| 208 | + 'elType' => 'widget', | |
| 209 | + 'widgetType' => 'betterdocs-search-form', | |
| 210 | + 'settings' => [], | |
| 211 | + 'elements' => [], | |
| 212 | + ], | |
| 213 | + ], | |
| 214 | + ], | |
| 215 | + | |
| 216 | + // ── Row 2: Sidebar (33 %) + Content (67 %) ─────────────────────── | |
| 217 | + [ | |
| 218 | + 'id' => 'bdta0010', | |
| 219 | + 'elType' => 'container', | |
| 220 | + 'settings' => [ | |
| 221 | + 'flex_direction' => 'row', | |
| 222 | + 'content_width' => 'full', | |
| 223 | + 'gap' => [ | |
| 224 | + 'unit' => 'px', | |
| 225 | + 'size' => 0, | |
| 226 | + ], | |
| 227 | + 'padding' => [ | |
| 228 | + 'unit' => 'px', | |
| 229 | + 'top' => '30', | |
| 230 | + 'right' => '20', | |
| 231 | + 'bottom' => '40', | |
| 232 | + 'left' => '20', | |
| 233 | + 'isLinked' => false, | |
| 234 | + ], | |
| 235 | + ], | |
| 236 | + 'elements' => [ | |
| 237 | + | |
| 238 | + // Left container – BetterDocs Sidebar | |
| 239 | + [ | |
| 240 | + 'id' => 'bdta0011', | |
| 241 | + 'elType' => 'container', | |
| 242 | + 'settings' => [ | |
| 243 | + 'flex_direction' => 'column', | |
| 244 | + 'content_width' => 'full', | |
| 245 | + 'width' => [ | |
| 246 | + 'unit' => '%', | |
| 247 | + 'size' => 33, | |
| 248 | + ], | |
| 249 | + ], | |
| 250 | + 'elements' => [ | |
| 251 | + [ | |
| 252 | + 'id' => 'bdta0012', | |
| 253 | + 'elType' => 'widget', | |
| 254 | + 'widgetType' => 'betterdocs-sidebar', | |
| 255 | + 'settings' => [], | |
| 256 | + 'elements' => [], | |
| 257 | + ], | |
| 258 | + ], | |
| 259 | + ], | |
| 260 | + | |
| 261 | + // Right container – Breadcrumbs + Tag title + Doc list | |
| 262 | + [ | |
| 263 | + 'id' => 'bdta0020', | |
| 264 | + 'elType' => 'container', | |
| 265 | + 'settings' => [ | |
| 266 | + 'flex_direction' => 'column', | |
| 267 | + 'content_width' => 'full', | |
| 268 | + 'width' => [ | |
| 269 | + 'unit' => '%', | |
| 270 | + 'size' => 67, | |
| 271 | + ], | |
| 272 | + 'padding' => [ | |
| 273 | + 'unit' => 'px', | |
| 274 | + 'top' => '0', | |
| 275 | + 'right' => '0', | |
| 276 | + 'bottom' => '0', | |
| 277 | + 'left' => '30', | |
| 278 | + 'isLinked' => false, | |
| 279 | + ], | |
| 280 | + ], | |
| 281 | + 'elements' => [ | |
| 282 | + // Breadcrumbs | |
| 283 | + [ | |
| 284 | + 'id' => 'bdta0021', | |
| 285 | + 'elType' => 'widget', | |
| 286 | + 'widgetType' => 'betterdocs-breadcrumb', | |
| 287 | + 'settings' => [], | |
| 288 | + 'elements' => [], | |
| 289 | + ], | |
| 290 | + | |
| 291 | + // Archive title – shows "Docs Tag: <tag-name>" dynamically | |
| 292 | + [ | |
| 293 | + 'id' => 'bdta0022', | |
| 294 | + 'elType' => 'widget', | |
| 295 | + 'widgetType' => 'theme-archive-title', | |
| 296 | + 'settings' => [ | |
| 297 | + 'title_size' => 'h2', | |
| 298 | + ], | |
| 299 | + 'elements' => [], | |
| 300 | + ], | |
| 301 | + | |
| 302 | + // Docs list for the current tag | |
| 303 | + [ | |
| 304 | + 'id' => 'bdta0023', | |
| 305 | + 'elType' => 'widget', | |
| 306 | + 'widgetType' => 'betterdocs-category-archive-list', | |
| 307 | + 'settings' => [ | |
| 308 | + 'section_betterdocs_archive_list_layout' => 'layout-1', | |
| 309 | + ], | |
| 310 | + 'elements' => [], | |
| 311 | + ], | |
| 312 | + ], | |
| 313 | + ], | |
| 314 | + ], | |
| 315 | + ], | |
| 316 | + ]; | |
| 317 | + | |
| 318 | + $result = []; | |
| 319 | + $result['content'] = $this->replace_elements_ids( $content ); | |
| 124 | 320 | $result['content'] = $this->process_export_import_content( $result['content'], 'on_import' ); |
| 125 | 321 | $result['page_settings'] = []; |
| 126 | 322 | |
| 127 | 323 | return $result; |