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-menu-creator.php
144 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Data\Utils\Wizard; |
| 4 | |
| 5 | use WP_Post; |
| 6 | |
| 7 | defined('ABSPATH') || exit(); |
| 8 | |
| 9 | class WizardMenuCreator |
| 10 | { |
| 11 | public static function MaybeUpdateMenu($selection_data, $menu_items) |
| 12 | { |
| 13 | if (!isset($selection_data[WizardStageTypes::NAVIGATION_MENU_STAGE])) { |
| 14 | return; |
| 15 | } |
| 16 | |
| 17 | $navigation_selections = $selection_data[WizardStageTypes::NAVIGATION_MENU_STAGE]; |
| 18 | if (self::ShouldSkipMenu($navigation_selections)) { |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | // Create navigation menu content |
| 23 | $menu_content = self::CreateMenuContent($menu_items); |
| 24 | $navigation_process = $navigation_selections[0]['slug']; |
| 25 | |
| 26 | if ($navigation_process === WizardNavigationMenuOptions::CREATE_NEW_MENU) { |
| 27 | $menu_post_id = self::CreateOrUpdateMenu($menu_content); |
| 28 | } else if ($navigation_process === WizardNavigationMenuOptions::APPEND_EXISTING_MENU) { |
| 29 | $menu_post_id = self::AppendToExistingMenu($menu_content); |
| 30 | } |
| 31 | |
| 32 | // Update the known navigation template part with the new navigation menu id. |
| 33 | self::UpdateNavigationTemplatePart($menu_post_id, 'header'); |
| 34 | } |
| 35 | |
| 36 | private static function ShouldSkipMenu($navigation_selections) |
| 37 | { |
| 38 | return empty($navigation_selections) || !isset($navigation_selections[0]['slug']) || $navigation_selections[0]['slug'] === WizardNavigationMenuOptions::SKIP_MENU; |
| 39 | } |
| 40 | |
| 41 | private static function CreateMenuContent($menu_items) |
| 42 | { |
| 43 | $menu_content = ''; |
| 44 | foreach ($menu_items as $menu_item) { |
| 45 | $menu_content .= self::CreateMenuItem($menu_item); |
| 46 | } |
| 47 | return $menu_content; |
| 48 | } |
| 49 | |
| 50 | private static function CreateMenuItem($menu_item) |
| 51 | { |
| 52 | if ($menu_item['id'] === 0) { |
| 53 | return "<!-- wp:home-link {\"label\":\"{$menu_item['title']}\"} /-->"; |
| 54 | } |
| 55 | return "<!-- wp:navigation-link {\"label\":\"{$menu_item['title']}\",\"type\":\"page\",\"id\":{$menu_item['id']},\"url\":\"{$menu_item['url']}\",\"kind\":\"post-type\",\"isTopLevelLink\":true} /-->"; |
| 56 | } |
| 57 | |
| 58 | private static function CreateOrUpdateMenu($menu_content) |
| 59 | { |
| 60 | $menu_post_id = self::GetWizardNavigationPostID(); |
| 61 | if ($menu_post_id) { |
| 62 | $menu_post_id = self::UpdateExistingMenuPost($menu_post_id, $menu_content); |
| 63 | } |
| 64 | |
| 65 | if (!$menu_post_id) { |
| 66 | $menu_post_id = self::CreateNewMenuPost($menu_content); |
| 67 | self::SetWizardNavigationPostID($menu_post_id); |
| 68 | } |
| 69 | |
| 70 | return $menu_post_id; |
| 71 | } |
| 72 | |
| 73 | private static function UpdateExistingMenuPost($menu_post_id, $menu_content, $append = false) |
| 74 | { |
| 75 | $menu_post = get_post($menu_post_id); |
| 76 | if (!$menu_post instanceof WP_Post || $menu_post->post_type !== WizardItemTypes::WP_NAVIGATION) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | if ($append) { |
| 81 | $menu_content = $menu_post->post_content . $menu_content; |
| 82 | } |
| 83 | WizardCreationUtil::UpdateTemplatePost($menu_post_id, $menu_content); |
| 84 | |
| 85 | return $menu_post_id; |
| 86 | } |
| 87 | |
| 88 | private static function CreateNewMenuPost($menu_content) |
| 89 | { |
| 90 | return wp_insert_post( |
| 91 | array( |
| 92 | 'post_title' => esc_html__("Navigation - Superb Addons - Theme Designer", "superb-blocks"), |
| 93 | 'post_content' => $menu_content, |
| 94 | 'post_status' => 'publish', |
| 95 | 'post_type' => WizardItemTypes::WP_NAVIGATION, |
| 96 | 'comment_status' => 'closed', |
| 97 | 'ping_status' => 'closed' |
| 98 | ) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | private static function AppendToExistingMenu($menu_content) |
| 103 | { |
| 104 | $menu_post_id = WizardCreationUtil::GetNavigationTemplatePartMenuId('header'); |
| 105 | if (!$menu_post_id) { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | self::UpdateExistingMenuPost($menu_post_id, $menu_content, true); |
| 110 | return $menu_post_id; |
| 111 | } |
| 112 | |
| 113 | private static function SetWizardNavigationPostID($post_id) |
| 114 | { |
| 115 | return update_option('superbaddons_wizard_navigation_post_id', $post_id, false); |
| 116 | } |
| 117 | |
| 118 | private static function GetWizardNavigationPostID() |
| 119 | { |
| 120 | return get_option('superbaddons_wizard_navigation_post_id', false); |
| 121 | } |
| 122 | |
| 123 | private static function UpdateNavigationTemplatePart($navigation_id, $template_part_slug = 'header') |
| 124 | { |
| 125 | $template_data = WizardCreationUtil::GetNavigationTemplateData($template_part_slug); |
| 126 | if (!$template_data) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | $new_content = WizardCreationUtil::UpdateNavigationBlockContentRefAndRemoveInnerLinks($template_data['content'], $navigation_id); |
| 131 | |
| 132 | // If the template part has a post id, custom changes were made to the template part. |
| 133 | if (isset($template_data['post_id']) && $template_data['post_id'] > 0) { |
| 134 | // Update the template part post. |
| 135 | WizardCreationUtil::UpdateTemplatePost($template_data['post_id'], $new_content); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | // No post id, create a new template part post with the change. |
| 141 | WizardCreationUtil::CreateNewTemplatePartPost($template_part_slug, $new_content); |
| 142 | } |
| 143 | } |
| 144 |