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