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-part-creator.php
153 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Data\Utils\Wizard; |
| 4 | |
| 5 | use SuperbAddons\Admin\Controllers\Wizard\WizardRestorationPointController; |
| 6 | use SuperbAddons\Library\Controllers\LibraryRequestController; |
| 7 | |
| 8 | defined('ABSPATH') || exit(); |
| 9 | |
| 10 | class WizardPartCreator |
| 11 | { |
| 12 | public static function CreateTemplateParts($selection_data, $wizardData) |
| 13 | { |
| 14 | foreach ($wizardData->GetStages() as $stage_type) { |
| 15 | if ($stage_type !== WizardStageTypes::HEADER_STAGE && $stage_type !== WizardStageTypes::FOOTER_STAGE) { |
| 16 | continue; |
| 17 | } |
| 18 | |
| 19 | if (!isset($selection_data[$stage_type])) { |
| 20 | throw new WizardException(__('Couldn\'t process template part selections. If the issue persists, please contact support.', 'superb-blocks')); |
| 21 | } |
| 22 | |
| 23 | $template_part_data = isset($selection_data[$stage_type][0]) ? $selection_data[$stage_type][0] : []; |
| 24 | if (!boolval($template_part_data['isChanged'])) { |
| 25 | continue; |
| 26 | } |
| 27 | |
| 28 | self::ValidateTemplatePartDataOrThrow($template_part_data); |
| 29 | $template_part_slug = $stage_type === WizardStageTypes::HEADER_STAGE ? 'header' : 'footer'; |
| 30 | $restoration_point = WizardRestorationPointController::CreateTemplateRestorationPoint($template_part_slug, WizardItemTypes::WP_TEMPLATE_PART); |
| 31 | if (!$restoration_point) { |
| 32 | throw new WizardException(__('Template part restoration point could not be created. If the issue persists, please contact support.', 'superb-blocks')); |
| 33 | } |
| 34 | $part_created = self::CreateTemplatePart($template_part_slug, $template_part_data['slug'], $template_part_data['package'], $template_part_data['type']); |
| 35 | if (!$part_created) { |
| 36 | throw new WizardException(__('Template part could not be created. If the issue persists, please contact support.', 'superb-blocks')); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | private static function ValidateTemplatePartDataOrThrow($data) |
| 44 | { |
| 45 | if ( |
| 46 | !isset($data['slug']) |
| 47 | || !isset($data['package']) |
| 48 | || !isset($data['type']) |
| 49 | || empty($data['slug']) |
| 50 | || empty($data['package']) |
| 51 | || empty($data['type']) |
| 52 | ) { |
| 53 | throw new WizardException(__('Missing template part selection data. If the issue persists, please contact support.', 'superb-blocks')); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | private static function CreateTemplatePart($slug, $template_id, $package, $template_type) |
| 58 | { |
| 59 | // Slug is the same as the template id, no need to create a new template part. |
| 60 | if ($template_type === WizardItemTypes::WP_TEMPLATE_PART && $slug === $template_id) { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | // Do not create if template part does not exist in theme. |
| 65 | if (!get_block_template(get_stylesheet() . "//" . $slug, WizardItemTypes::WP_TEMPLATE_PART)) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | $current_template_part_post_id = WizardCreationUtil::GetTemplatePostID($slug, WizardItemTypes::WP_TEMPLATE_PART); |
| 70 | |
| 71 | $template_content = false; |
| 72 | if ($template_type === WizardItemTypes::WP_TEMPLATE_PART) { |
| 73 | $is_file_template = WizardCreationUtil::IsFileTemplate($template_id, $template_type, WizardItemTypes::WP_TEMPLATE_PART); |
| 74 | $is_restoration_point = WizardCreationUtil::IsRestorationPoint($template_id, $template_type, WizardItemTypes::WP_TEMPLATE_PART); |
| 75 | if ($is_file_template) { |
| 76 | $template_content = self::GetFileTemplateContent($template_id); |
| 77 | } elseif ($is_restoration_point) { |
| 78 | $template_content = self::GetRestorationPointContent($template_id); |
| 79 | } else { |
| 80 | $template_content = self::GetTemplateContent($template_id); |
| 81 | } |
| 82 | } else { |
| 83 | $template_content = self::GetSelectedAddonsTemplateContent($template_id, $package); |
| 84 | } |
| 85 | |
| 86 | if (!$template_content) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | if ($slug === 'header') { |
| 91 | $template_content = self::InjectCurrentHeaderNavigationId($template_content); |
| 92 | } |
| 93 | |
| 94 | return $current_template_part_post_id |
| 95 | ? WizardCreationUtil::UpdateTemplatePost($current_template_part_post_id, $template_content) |
| 96 | : WizardCreationUtil::CreateNewTemplatePartPost($slug, $template_content); |
| 97 | } |
| 98 | |
| 99 | private static function GetFileTemplateContent($template_id) |
| 100 | { |
| 101 | $template = get_block_file_template(get_stylesheet() . '//' . $template_id, WizardItemTypes::WP_TEMPLATE_PART); |
| 102 | if ($template->theme !== get_stylesheet() || empty($template->content)) { |
| 103 | return false; |
| 104 | } |
| 105 | return $template->content; |
| 106 | } |
| 107 | |
| 108 | private static function GetTemplateContent($template_id) |
| 109 | { |
| 110 | $template = get_block_template(get_stylesheet() . '//' . $template_id, WizardItemTypes::WP_TEMPLATE_PART); |
| 111 | if ($template->theme !== get_stylesheet() || empty($template->content)) { |
| 112 | return false; |
| 113 | } |
| 114 | return $template->content; |
| 115 | } |
| 116 | |
| 117 | private static function GetRestorationPointContent($template_id) |
| 118 | { |
| 119 | $restoration_point = WizardRestorationPointController::GetTemplateRestorationPoint($template_id); |
| 120 | if (!$restoration_point) { |
| 121 | return false; |
| 122 | } |
| 123 | return $restoration_point['content']; |
| 124 | } |
| 125 | |
| 126 | private static function GetSelectedAddonsTemplateContent($template_id, $package) |
| 127 | { |
| 128 | $data = LibraryRequestController::GetInsertData( |
| 129 | [ |
| 130 | 'id' => $template_id, |
| 131 | 'package' => $package, |
| 132 | ], |
| 133 | LibraryRequestController::GUTENBERG_ENDPOINT_BASE, |
| 134 | LibraryRequestController::GUTENBERG_ROUTE_TYPE_PATTERNS |
| 135 | ); |
| 136 | return $data['content'] ?? false; |
| 137 | } |
| 138 | |
| 139 | private static function InjectCurrentHeaderNavigationId($template_content) |
| 140 | { |
| 141 | if (!has_block('core/navigation', $template_content)) { |
| 142 | return $template_content; |
| 143 | } |
| 144 | |
| 145 | $navigation_id = WizardCreationUtil::GetNavigationTemplatePartMenuId(); |
| 146 | if (!$navigation_id) { |
| 147 | return $template_content; |
| 148 | } |
| 149 | |
| 150 | return WizardCreationUtil::UpdateNavigationBlockContentRefAndRemoveInnerLinks($template_content, $navigation_id); |
| 151 | } |
| 152 | } |
| 153 |