wizard-items
1 year ago
class-wizard-constants.php
1 year ago
class-wizard-creation-util.php
1 year ago
class-wizard-exception.php
1 year ago
class-wizard-menu-creator.php
1 year ago
class-wizard-page-creator.php
1 year ago
class-wizard-part-creator.php
1 year ago
class-wizard-stage-util.php
1 year ago
class-wizard-template-provider.php
1 year ago
class-wizard-template-provider.php
415 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Data\Utils\Wizard; |
| 4 | |
| 5 | use SuperbAddons\Admin\Controllers\Wizard\WizardRestorationPointController; |
| 6 | use SuperbAddons\Data\Controllers\KeyController; |
| 7 | use SuperbAddons\Data\Controllers\RestController; |
| 8 | use SuperbAddons\Library\Controllers\LibraryRequestController; |
| 9 | |
| 10 | use WP_REST_Request; |
| 11 | |
| 12 | defined('ABSPATH') || exit(); |
| 13 | |
| 14 | class WizardTemplateProvider |
| 15 | { |
| 16 | private $headerTemplates; |
| 17 | private $footerTemplates; |
| 18 | private $blogTemplates; |
| 19 | private $homeTemplates; |
| 20 | private $indexTemplates; |
| 21 | private $frontPageTemplates; |
| 22 | private $templatePages; |
| 23 | |
| 24 | private $hasFrontPageTemplate; |
| 25 | private $hasHomeTemplate; |
| 26 | private $hasIndexTemplate; |
| 27 | |
| 28 | private $userIsPremium; |
| 29 | |
| 30 | public function __construct() |
| 31 | { |
| 32 | $this->headerTemplates = array(); |
| 33 | $this->footerTemplates = array(); |
| 34 | $this->blogTemplates = array(); |
| 35 | $this->homeTemplates = array(); |
| 36 | $this->indexTemplates = array(); |
| 37 | $this->frontPageTemplates = array(); |
| 38 | $this->templatePages = array(); |
| 39 | |
| 40 | $this->hasFrontPageTemplate = false; |
| 41 | $this->hasHomeTemplate = false; |
| 42 | $this->hasIndexTemplate = false; |
| 43 | $this->userIsPremium = KeyController::HasValidPremiumKey(); |
| 44 | } |
| 45 | |
| 46 | private function HasModifiedTemplate($slug, $template_type) |
| 47 | { |
| 48 | $modifiedTemplatePost = get_posts( |
| 49 | array( |
| 50 | 'post_type' => $template_type, |
| 51 | 'name' => $slug, |
| 52 | 'posts_per_page' => 1, |
| 53 | 'tax_query' => array( |
| 54 | array( |
| 55 | 'taxonomy' => 'wp_theme', |
| 56 | 'field' => 'slug', |
| 57 | 'terms' => get_stylesheet() |
| 58 | ) |
| 59 | ) |
| 60 | ) |
| 61 | ); |
| 62 | |
| 63 | return $modifiedTemplatePost && !empty($modifiedTemplatePost) && $modifiedTemplatePost[0]->post_name === $slug; |
| 64 | } |
| 65 | |
| 66 | private function InitializePart($slugOrWizardItem, $type, $regularTitle, $modifiedTitle) |
| 67 | { |
| 68 | |
| 69 | if ($slugOrWizardItem instanceof WizarditemRestorationPoint) { |
| 70 | $this->AddToAppropriateArray($slugOrWizardItem, $slugOrWizardItem->GetBaseSlug()); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | $datatype = false; |
| 75 | if ($slugOrWizardItem instanceof WizardItem) { |
| 76 | $datatype = $slugOrWizardItem->datatype; |
| 77 | $slug = $slugOrWizardItem->GetBaseSlug(); |
| 78 | } else { |
| 79 | $slug = $slugOrWizardItem; |
| 80 | } |
| 81 | |
| 82 | $partTemplate = get_block_template(get_stylesheet() . '//' . $slug, $type); |
| 83 | |
| 84 | $hasModifiedPart = $this->HasModifiedTemplate($slug, $type); |
| 85 | if ($partTemplate) { |
| 86 | $partItem = new WizardItemTemplate($partTemplate, $regularTitle, $modifiedTitle, $hasModifiedPart); |
| 87 | if ($datatype) { |
| 88 | $partItem->datatype = $datatype; |
| 89 | } |
| 90 | |
| 91 | $this->AddToAppropriateArray($partItem, $slug); |
| 92 | } |
| 93 | if ($hasModifiedPart) { |
| 94 | // Get the original part template from the theme file |
| 95 | $themePartItem = new WizardItemFile($slug, $type, $regularTitle); |
| 96 | if ($datatype) { |
| 97 | $partItem->datatype = $datatype; |
| 98 | } |
| 99 | $this->AddToAppropriateArray($themePartItem, $slug); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public function InitializePatterns($required_plugin = false) |
| 104 | { |
| 105 | // Check if the theme has a modified header template part post |
| 106 | $this->InitializePart('header', WizardItemTypes::WP_TEMPLATE_PART, __("Header (Theme)", "superb-blocks"), __("Header (Current)", "superb-blocks")); |
| 107 | $this->InitializePart('footer', WizardItemTypes::WP_TEMPLATE_PART, __("Footer (Theme)", "superb-blocks"), __("Footer (Current)", "superb-blocks")); |
| 108 | |
| 109 | try { |
| 110 | $request = new WP_REST_Request('GET', '/' . RestController::NAMESPACE . LibraryRequestController::GUTENBERG_LIST_ROUTE . 'patterns'); |
| 111 | $patterns_response = rest_do_request($request); |
| 112 | if (!$patterns_response || is_wp_error($patterns_response) || $patterns_response->is_error() || !isset($patterns_response->data) || !isset($patterns_response->data->items)) { |
| 113 | throw new WizardException(__("An error occurred while fetching available patterns.", "superb-blocks")); |
| 114 | } |
| 115 | |
| 116 | // Order free to the top of list array if the user is not premium |
| 117 | if (!$this->userIsPremium) { |
| 118 | $freePatterns = array(); |
| 119 | $premiumPatterns = array(); |
| 120 | foreach ($patterns_response->data->items as $pattern) { |
| 121 | if (isset($pattern->package) && $pattern->package === 'premium') { |
| 122 | $premiumPatterns[] = $pattern; |
| 123 | } else { |
| 124 | $freePatterns[] = $pattern; |
| 125 | } |
| 126 | } |
| 127 | $patterns_response->data->items = array_merge($freePatterns, $premiumPatterns); |
| 128 | } |
| 129 | // Get header and footer templates from patterns |
| 130 | foreach ($patterns_response->data->items as $pattern) { |
| 131 | if ($required_plugin && !isset($pattern->required_plugins) || $required_plugin && !in_array($required_plugin, $pattern->required_plugins)) { |
| 132 | continue; |
| 133 | } |
| 134 | $patternItem = new WizardItemPattern($pattern); |
| 135 | foreach ($pattern->categories as $category) { |
| 136 | if ($category->id === WizardDataCategory::NAVIGATION) { |
| 137 | $this->headerTemplates[] = $patternItem; |
| 138 | } else if ($category->id === WizardDataCategory::FOOTER) { |
| 139 | $this->footerTemplates[] = $patternItem; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } catch (WizardException $e) { |
| 144 | // Exception is automatically logged. Catch it so that the wizard can continue with the available theme templates. |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | public function InitalizePageTemplates() |
| 149 | { |
| 150 | $acceptedTemplates = $this->GetAcceptedTemplates(); |
| 151 | |
| 152 | // Initial sort templates by slug |
| 153 | $this->SortTemplatesIntoArraysBySlug($acceptedTemplates); |
| 154 | |
| 155 | // Additional template logic |
| 156 | $this->HandleTemplateLogic(); |
| 157 | |
| 158 | // Add addons service templates |
| 159 | try { |
| 160 | $request = new WP_REST_Request('GET', '/' . RestController::NAMESPACE . LibraryRequestController::GUTENBERG_LIST_ROUTE . 'pages'); |
| 161 | $pages_response = rest_do_request($request); |
| 162 | if (!$pages_response || is_wp_error($pages_response) || $pages_response->is_error() || !isset($pages_response->data) || !isset($pages_response->data->items)) { |
| 163 | throw new WizardException(__("An error occurred while fetching available pages.", "superb-blocks")); |
| 164 | } |
| 165 | |
| 166 | // Order free to the top of list array if the user is not premium |
| 167 | if (!$this->userIsPremium) { |
| 168 | $freePages = array(); |
| 169 | $premiumPages = array(); |
| 170 | foreach ($pages_response->data->items as $page) { |
| 171 | if (isset($page->package) && $page->package === 'premium') { |
| 172 | $premiumPages[] = $page; |
| 173 | } else { |
| 174 | $freePages[] = $page; |
| 175 | } |
| 176 | } |
| 177 | $pages_response->data->items = array_merge($freePages, $premiumPages); |
| 178 | } |
| 179 | |
| 180 | foreach ($pages_response->data->items as $page) { |
| 181 | $pageItem = new WizardItemPage($page); |
| 182 | $pageItem->use_custom_page_template_preview = 1; |
| 183 | $this->templatePages[] = $pageItem; |
| 184 | |
| 185 | // Add landing page templates to the front page templates |
| 186 | foreach ($page->categories as $category) { |
| 187 | if ($category->id === WizardDataCategory::LANDING_PAGE) { |
| 188 | $landingPageItem = new WizardItemPage($page); |
| 189 | $this->frontPageTemplates[] = $landingPageItem; |
| 190 | } |
| 191 | if ($category->id === WizardDataCategory::BLOG) { |
| 192 | $this->blogTemplates[] = $pageItem; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } catch (WizardException $e) { |
| 197 | // Exception is automatically logged. Catch it so that the wizard can continue with the available theme templates. |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | private function HandleTemplateLogic($logicType = 'default') |
| 202 | { |
| 203 | // Check for static pages |
| 204 | $has_static_pages = get_option('show_on_front') === 'page'; |
| 205 | $staticFrontPageID = get_option('page_on_front'); |
| 206 | $hasStaticFrontPage = $has_static_pages && $staticFrontPageID && $staticFrontPageID !== 0; |
| 207 | $staticBlogPageID = get_option('page_for_posts'); |
| 208 | $hasStaticBlogPage = $has_static_pages && $staticBlogPageID && $staticBlogPageID !== 0; |
| 209 | |
| 210 | if ($logicType === "default") { |
| 211 | // Front page templates additional logic |
| 212 | if (!$this->hasFrontPageTemplate) { |
| 213 | // If no front page template exists, set regular theme templates |
| 214 | $this->frontPageTemplates = $this->templatePages; |
| 215 | |
| 216 | if ($hasStaticFrontPage) { |
| 217 | $static_page = new WizardItemStatic(__("Static Homepage (Current)", "superb-blocks"), $staticFrontPageID); |
| 218 | $this->frontPageTemplates = array_merge(array($static_page), $this->frontPageTemplates); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Add "No blog page" selection |
| 223 | $noBlogPage = array(new WizardItemIgnore(__("No Blog Page", "superb-blocks"))); |
| 224 | // Blog templates logic |
| 225 | if ($this->hasHomeTemplate || $this->hasIndexTemplate) { |
| 226 | $this->blogTemplates = array_merge($noBlogPage, $this->homeTemplates, $this->indexTemplates, $this->blogTemplates); |
| 227 | } else { |
| 228 | if ($hasStaticBlogPage) { |
| 229 | $static_page = array(new WizardItemStatic(__("Static Blog Page (Current)", "superb-blocks"), $staticBlogPageID)); |
| 230 | $this->blogTemplates = array_merge($noBlogPage, $static_page); |
| 231 | } else { |
| 232 | $this->blogTemplates = $noBlogPage; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // If home templates exist and no front page template exists, add the home templates to the front page templates |
| 237 | if (!empty($this->homeTemplates) && !$this->hasFrontPageTemplate) { |
| 238 | if ($hasStaticFrontPage) { |
| 239 | // Add the home templates after the first item |
| 240 | array_splice($this->frontPageTemplates, 1, 0, $this->homeTemplates); |
| 241 | } else { |
| 242 | // Add the home templates to the front of the front page templates |
| 243 | $this->frontPageTemplates = array_merge($this->homeTemplates, $this->frontPageTemplates); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if ($logicType === "restoration") { |
| 249 | if (!empty($this->headerTemplates)) { |
| 250 | $currentHeader = get_block_template(get_stylesheet() . '//' . 'header', WizardItemTypes::WP_TEMPLATE_PART); |
| 251 | $currentHeader->title = $currentHeader->title . __(" (Current)", "superb-blocks"); |
| 252 | $currentHeader = new WizardItem($currentHeader); |
| 253 | $this->headerTemplates = array_merge(array($currentHeader), $this->headerTemplates); |
| 254 | } |
| 255 | |
| 256 | if (!empty($this->footerTemplates)) { |
| 257 | $currentFooter = get_block_template(get_stylesheet() . '//' . 'footer', WizardItemTypes::WP_TEMPLATE_PART); |
| 258 | $currentFooter->title = $currentFooter->title . __(" (Current)", "superb-blocks"); |
| 259 | $currentFooter = new WizardItem($currentFooter); |
| 260 | $this->footerTemplates = array_merge(array($currentFooter), $this->footerTemplates); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | if (!empty($this->frontPageTemplates)) { |
| 265 | if ($this->hasFrontPageTemplate) { |
| 266 | $currentFrontPage = get_block_template(get_stylesheet() . '//' . 'front-page', WizardItemTypes::WP_TEMPLATE); |
| 267 | $currentFrontPage->title = $currentFrontPage->title . __(" (Current)", "superb-blocks"); |
| 268 | $currentFrontPage = new WizardItem($currentFrontPage); |
| 269 | $this->frontPageTemplates = array_merge(array($currentFrontPage), $this->frontPageTemplates); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | $this->blogTemplates = array_merge($this->homeTemplates, $this->indexTemplates, $this->blogTemplates); |
| 274 | if (!empty($this->blogTemplates)) { |
| 275 | if ($this->hasHomeTemplate) { |
| 276 | $currentBlogPage = get_block_template(get_stylesheet() . '//' . 'home', WizardItemTypes::WP_TEMPLATE); |
| 277 | } elseif ($this->hasIndexTemplate) { |
| 278 | $currentBlogPage = get_block_template(get_stylesheet() . '//' . 'index', WizardItemTypes::WP_TEMPLATE); |
| 279 | } |
| 280 | |
| 281 | if ($currentBlogPage) { |
| 282 | $currentBlogPage->datatype = 'blog'; |
| 283 | $currentBlogPage->title = $currentBlogPage->title . __(" (Current)", "superb-blocks"); |
| 284 | $currentBlogPage = new WizardItem($currentBlogPage); |
| 285 | $this->blogTemplates = array_merge(array($currentBlogPage), $this->blogTemplates); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | private function GetAcceptedTemplates() |
| 292 | { |
| 293 | $templates = get_block_templates(); |
| 294 | $acceptedTemplates = array(); |
| 295 | // Filter out templates that are not accepted for the current theme |
| 296 | foreach ($templates as $template) { |
| 297 | if ($template->status !== 'publish') continue; |
| 298 | if ($template->type !== WizardItemTypes::WP_TEMPLATE) continue; |
| 299 | if ($template->theme !== get_stylesheet()) continue; |
| 300 | if ($template->slug === AddonsPageTemplateUtil::TEMPLATE_ID) continue; // Do not include the wizard page creation template |
| 301 | if ($template->slug !== 'front-page' && $template->slug !== 'home' && $template->slug !== 'index') { |
| 302 | if (!$template->is_custom) continue; |
| 303 | if (!$template->post_types || !in_array('page', $template->post_types)) continue; |
| 304 | } |
| 305 | $acceptedTemplates[] = new WizardItem($template); |
| 306 | } |
| 307 | return $acceptedTemplates; |
| 308 | } |
| 309 | |
| 310 | public function InitializeRestorationTemplates() |
| 311 | { |
| 312 | $theme_restoration = WizardRestorationPointController::GetThemeRestorationPoints(); |
| 313 | if (!$theme_restoration) { |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | $restoration_templates = []; |
| 318 | foreach ($theme_restoration as $restorationId => $restorationPointArray) { |
| 319 | $restoration_templates[] = new WizarditemRestorationPoint($restorationId, $restorationPointArray); |
| 320 | } |
| 321 | |
| 322 | // Sort restoration templates by timestamp |
| 323 | usort($restoration_templates, function ($a, $b) { |
| 324 | return $a->timestamp < $b->timestamp; |
| 325 | }); |
| 326 | |
| 327 | $this->SortTemplatesIntoArraysBySlug($restoration_templates, "restoration"); |
| 328 | $this->HandleTemplateLogic('restoration'); |
| 329 | } |
| 330 | |
| 331 | private function SortTemplatesIntoArraysBySlug($acceptedTemplates, $logicType = "default") |
| 332 | { |
| 333 | foreach ($acceptedTemplates as $template) { |
| 334 | $slug = $template->GetBaseSlug(); |
| 335 | |
| 336 | if ($slug === 'front-page' || $slug === 'home' || $slug === 'index') { |
| 337 | switch ($slug) { |
| 338 | case "front-page": |
| 339 | $base_label = __("Front Page", "superb-blocks"); |
| 340 | break; |
| 341 | case "home": |
| 342 | $template->datatype = 'blog'; |
| 343 | $base_label = __("Blog Home", "superb-blocks"); |
| 344 | break; |
| 345 | case "index": |
| 346 | $template->datatype = 'blog'; |
| 347 | $base_label = __("Index", "superb-blocks"); |
| 348 | break; |
| 349 | } |
| 350 | $this->InitializePart($template, WizardItemTypes::WP_TEMPLATE, $base_label . __(" (Theme)", "superb-blocks"), $base_label . __(" (Current)", "superb-blocks")); |
| 351 | } else { |
| 352 | if ($logicType === "default") { |
| 353 | $template->title = $template->title . ' (' . __('Theme', 'superb-blocks') . ')'; |
| 354 | } |
| 355 | $this->AddToAppropriateArray($template, $slug); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | private function AddToAppropriateArray($item, $slug) |
| 361 | { |
| 362 | switch ($slug) { |
| 363 | case 'header': |
| 364 | $this->headerTemplates[] = $item; |
| 365 | break; |
| 366 | case 'footer': |
| 367 | $this->footerTemplates[] = $item; |
| 368 | break; |
| 369 | case 'front-page': |
| 370 | $this->hasFrontPageTemplate = true; |
| 371 | $this->frontPageTemplates[] = $item; |
| 372 | break; |
| 373 | case 'home': |
| 374 | $this->hasHomeTemplate = true; |
| 375 | $this->homeTemplates[] = $item; |
| 376 | break; |
| 377 | case 'index': |
| 378 | $this->hasIndexTemplate = true; |
| 379 | $this->indexTemplates[] = $item; |
| 380 | break; |
| 381 | default: |
| 382 | if (strpos($slug, 'blog') !== false) { |
| 383 | $this->blogTemplates[] = $item; |
| 384 | } |
| 385 | $this->templatePages[] = $item; |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | public function GetHeaderTemplates() |
| 391 | { |
| 392 | return $this->headerTemplates; |
| 393 | } |
| 394 | |
| 395 | public function GetFooterTemplates() |
| 396 | { |
| 397 | return $this->footerTemplates; |
| 398 | } |
| 399 | |
| 400 | public function GetBlogTemplates() |
| 401 | { |
| 402 | return $this->blogTemplates; |
| 403 | } |
| 404 | |
| 405 | public function GetFrontPageTemplates() |
| 406 | { |
| 407 | return $this->frontPageTemplates; |
| 408 | } |
| 409 | |
| 410 | public function GetTemplatePages() |
| 411 | { |
| 412 | return $this->templatePages; |
| 413 | } |
| 414 | } |
| 415 |