wizard
5 months ago
class-addons-template-util.php
5 months ago
class-allowed-template-html-util.php
5 months ago
class-base-exception.php
5 months ago
class-cache-constants.php
5 months ago
class-cache-exception.php
5 months ago
class-key-exception.php
5 months ago
class-keytypes.php
5 months ago
class-option-exception.php
5 months ago
class-quiet-skin.php
5 months ago
class-request-exception.php
5 months ago
class-script-translations.php
5 months ago
class-settings-exception.php
5 months ago
class-theme-installer-exception.php
5 months ago
class-theme-installer.php
5 months ago
class-addons-template-util.php
192 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Data\Utils\Wizard; |
| 4 | |
| 5 | use SuperbAddons\Data\Utils\AllowedTemplateHTMLUtil; |
| 6 | use WP_Block_Template; |
| 7 | |
| 8 | defined('ABSPATH') || exit(); |
| 9 | |
| 10 | class AddonsPageTemplateUtil |
| 11 | { |
| 12 | const TEMPLATE_ID = 'superbaddons-page-template'; |
| 13 | const PLUGIN_SLUG = 'superb-blocks/plugin'; |
| 14 | |
| 15 | public static function GetAddonsPageBlockTemplateObject() |
| 16 | { |
| 17 | $template_content = false; |
| 18 | $template_source = "plugin"; |
| 19 | |
| 20 | $template = new WP_Block_Template(); |
| 21 | $template->type = WizardItemTypes::WP_TEMPLATE; |
| 22 | $template->theme = self::PLUGIN_SLUG; |
| 23 | $template->slug = self::TEMPLATE_ID; |
| 24 | $template->id = self::PLUGIN_SLUG . '//' . self::TEMPLATE_ID; |
| 25 | $template->title = __("Superb Addons Template Page", "superb-blocks"); |
| 26 | $template->description = __("This is a basic full width page template that includes a header and footer. This template is used when creating template pages in Superb Addons.", "superb-blocks"); |
| 27 | $template->origin = 'plugin'; |
| 28 | $template->status = 'publish'; |
| 29 | $template->has_theme_file = true; |
| 30 | $template->is_custom = true; |
| 31 | $template->post_types = ['page']; |
| 32 | |
| 33 | // Get the saved custom template from the database if it exists. |
| 34 | $custom_template_query_args = array( |
| 35 | 'post_type' => WizardItemTypes::WP_TEMPLATE, |
| 36 | 'posts_per_page' => 1, |
| 37 | 'name' => self::TEMPLATE_ID, |
| 38 | 'no_found_rows' => true, |
| 39 | // Ensure we only get template parts for the current theme -> Tax query is used like in WP core |
| 40 | // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 41 | 'tax_query' => array( |
| 42 | array( |
| 43 | 'taxonomy' => 'wp_theme', |
| 44 | 'field' => 'name', |
| 45 | 'terms' => array(self::PLUGIN_SLUG, get_stylesheet()), |
| 46 | ), |
| 47 | ), |
| 48 | ); |
| 49 | |
| 50 | $custom_template_query = new \WP_Query($custom_template_query_args); |
| 51 | $custom_superb_templates = $custom_template_query->posts; |
| 52 | |
| 53 | if ($custom_superb_templates && !empty($custom_superb_templates)) { |
| 54 | $template_content = $custom_superb_templates[0]->post_content; |
| 55 | $template_source = "custom"; |
| 56 | } else { |
| 57 | $page_content = '<!-- wp:post-content {"align":"full","layout":{"type":"default"},"lock":{"move":true,"remove":true}} /-->'; |
| 58 | $template_content = self::GetAddonsPageTemplateContent($page_content); |
| 59 | } |
| 60 | |
| 61 | $template->source = $template_source; |
| 62 | $template->content = $template_content; |
| 63 | |
| 64 | return $template; |
| 65 | } |
| 66 | |
| 67 | public static function GetAddonsPageTemplateContent($page_content) |
| 68 | { |
| 69 | $template = '<!-- wp:template-part {"slug":"header","lock":{"move":true,"remove":true}} /-->' . "\n"; |
| 70 | $template .= '<!-- wp:group {"tagName":"main","lock":{"move":true,"remove":true},"style":{"spacing":{"margin":{"top":"0rem"},"padding":{"top":"0","bottom":"0","left":"0","right":"0"},"blockGap":"var:preset|spacing|superbspacing-medium"}},"layout":{"type":"default"}} -->' . "\n"; |
| 71 | $template .= '<main class="wp-block-group" style="margin-top:0rem;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0">' . "\n"; |
| 72 | |
| 73 | AllowedTemplateHTMLUtil::enable_safe_styles(); |
| 74 | $template .= wp_kses($page_content, "post"); |
| 75 | AllowedTemplateHTMLUtil::disable_safe_styles(); |
| 76 | |
| 77 | $template .= '</main>' . "\n"; |
| 78 | $template .= '<!-- /wp:group -->' . "\n"; |
| 79 | $template .= '<!-- wp:template-part {"slug":"footer","lock":{"move":true,"remove":true}} /-->'; |
| 80 | |
| 81 | return $template; |
| 82 | } |
| 83 | |
| 84 | public static function GetAddonsTemplatePartFallbackObject($slug, $area, $title, $description, $content, $is_file_template) |
| 85 | { |
| 86 | $template_content = $content; |
| 87 | $template_source = self::PLUGIN_SLUG; |
| 88 | |
| 89 | $template = new WP_Block_Template(); |
| 90 | $template->type = WizardItemTypes::WP_TEMPLATE_PART; |
| 91 | $template->theme = get_stylesheet(); |
| 92 | $template->slug = $slug; |
| 93 | $template->id = get_stylesheet() . '//' . $slug; |
| 94 | $template->title = $title; |
| 95 | $template->description = $description; |
| 96 | $template->origin = 'theme'; |
| 97 | $template->status = 'publish'; |
| 98 | $template->has_theme_file = true; |
| 99 | $template->is_custom = true; |
| 100 | $template->area = $area; |
| 101 | |
| 102 | if (!$is_file_template) { |
| 103 | // Get the saved custom template from the database if it exists. |
| 104 | $custom_template_query_args = array( |
| 105 | 'post_type' => WizardItemTypes::WP_TEMPLATE_PART, |
| 106 | 'posts_per_page' => -1, |
| 107 | 'no_found_rows' => true, |
| 108 | // Ensure we only get template parts for the current theme -> Tax query is used like in WP core |
| 109 | // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 110 | 'tax_query' => array( |
| 111 | 'relation' => 'AND', |
| 112 | array( |
| 113 | 'taxonomy' => 'wp_theme', |
| 114 | 'field' => 'name', |
| 115 | 'terms' => get_stylesheet() |
| 116 | ), |
| 117 | array( |
| 118 | 'taxonomy' => 'wp_template_part_area', |
| 119 | 'field' => 'slug', |
| 120 | 'terms' => array($area), |
| 121 | ), |
| 122 | ) |
| 123 | ); |
| 124 | |
| 125 | $custom_template_query = new \WP_Query($custom_template_query_args); |
| 126 | $custom_superb_templates = $custom_template_query->posts; |
| 127 | |
| 128 | if ($custom_superb_templates && !empty($custom_superb_templates)) { |
| 129 | foreach ($custom_superb_templates as $custom_template) { |
| 130 | if ($custom_template->post_name === $slug) { |
| 131 | $template_content = $custom_template->post_content; |
| 132 | $template_source = "custom"; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | $template->source = $template_source; |
| 140 | $template->content = $template_content; |
| 141 | |
| 142 | return $template; |
| 143 | } |
| 144 | |
| 145 | public static function GetAddonsHeaderTemplatePartObject($is_file_template) |
| 146 | { |
| 147 | return self::GetAddonsTemplatePartFallbackObject( |
| 148 | 'header', |
| 149 | 'header', |
| 150 | __("Superb Addons Header", "superb-blocks"), |
| 151 | __("This is a basic header template part that includes a site title and navigation menu. This template part is used as a fallback when no header template part exists in the current theme.", "superb-blocks"), |
| 152 | self::GetAddonsHeaderTemplatePartContent(), |
| 153 | $is_file_template |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | public static function GetAddonsHeaderTemplatePartContent() |
| 158 | { |
| 159 | $template = '<!-- wp:group {"lock":{"move":true,"remove":true},"layout":{"type":"constrained"}} -->' . "\n"; |
| 160 | $template .= '<div class="wp-block-group"><!-- wp:group {"tagName":"header","lock":{"move":true,"remove":true},"style":{"spacing":{"padding":{"top":"var:preset|spacing|superbspacing-small","bottom":"var:preset|spacing|superbspacing-small","left":"var:preset|spacing|superbspacing-small","right":"var:preset|spacing|superbspacing-small"},"blockGap":"var:preset|spacing|superbspacing-small"}},"layout":{"type":"flex","flexWrap":"nowrap","justifyContent":"space-between"}} -->' . "\n"; |
| 161 | $template .= '<header class="wp-block-group" style="padding-top:var(--wp--preset--spacing--superbspacing-small);padding-right:var(--wp--preset--spacing--superbspacing-small);padding-bottom:var(--wp--preset--spacing--superbspacing-small);padding-left:var(--wp--preset--spacing--superbspacing-small)"><!-- wp:site-title {"level":0,"lock":{"move":true,"remove":true}} /-->' . "\n"; |
| 162 | $template .= '<!-- wp:navigation {"lock":{"move":true,"remove":true},"style":{"spacing":{"blockGap":"var:preset|spacing|superbspacing-small"}}} /--></header>' . "\n"; |
| 163 | $template .= '<!-- /wp:group --></div>' . "\n"; |
| 164 | $template .= '<!-- /wp:group -->'; |
| 165 | |
| 166 | return $template; |
| 167 | } |
| 168 | |
| 169 | public static function GetAddonsFooterTemplatePartObject($is_file_template) |
| 170 | { |
| 171 | return self::GetAddonsTemplatePartFallbackObject( |
| 172 | 'footer', |
| 173 | 'footer', |
| 174 | __("Superb Addons Footer", "superb-blocks"), |
| 175 | __("This is a basic footer template part that includes site info. This template part is used as a fallback when no footer template part exists in the current theme.", "superb-blocks"), |
| 176 | self::GetAddonsFooterTemplatePartContent(), |
| 177 | $is_file_template |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | public static function GetAddonsFooterTemplatePartContent() |
| 182 | { |
| 183 | $template = '<!-- wp:group {"lock":{"move":true,"remove":true},"layout":{"type":"constrained"}} -->' . "\n"; |
| 184 | $template .= '<div class="wp-block-group"><!-- wp:group {"tagName":"footer","lock":{"move":true,"remove":true},"style":{"spacing":{"padding":{"top":"var:preset|spacing|superbspacing-small","bottom":"var:preset|spacing|superbspacing-small","left":"var:preset|spacing|superbspacing-small","right":"var:preset|spacing|superbspacing-small"},"blockGap":"var:preset|spacing|superbspacing-small"}},"layout":{"type":"constrained"}} -->' . "\n"; |
| 185 | $template .= '<footer class="wp-block-group" style="padding-top:var(--wp--preset--spacing--superbspacing-small);padding-right:var(--wp--preset--spacing--superbspacing-small);padding-bottom:var(--wp--preset--spacing--superbspacing-small);padding-left:var(--wp--preset--spacing--superbspacing-small)"><!-- wp:site-info {"lock":{"move":true,"remove":true}} /--></footer>' . "\n"; |
| 186 | $template .= '<!-- /wp:group --></div>' . "\n"; |
| 187 | $template .= '<!-- /wp:group -->'; |
| 188 | |
| 189 | return $template; |
| 190 | } |
| 191 | } |
| 192 |