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