block-api
11 months ago
templates
11 months ago
class-gutenberg-block-styles.php
11 months ago
class-gutenberg-controller.php
11 months ago
class-gutenberg-enhancements-controller.php
11 months ago
class-gutenberg-controller.php
377 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Gutenberg\Controllers; |
| 4 | |
| 5 | defined('ABSPATH') || exit(); |
| 6 | |
| 7 | use SuperbAddons\Admin\Controllers\SettingsController; |
| 8 | use SuperbAddons\Admin\Controllers\Wizard\WizardController; |
| 9 | use SuperbAddons\Admin\Controllers\Wizard\WizardRestorationPointController; |
| 10 | use SuperbAddons\Admin\Utils\AdminLinkSource; |
| 11 | use SuperbAddons\Admin\Utils\AdminLinkUtil; |
| 12 | use SuperbAddons\Data\Controllers\CompatibilitySettingsOptionKey; |
| 13 | use SuperbAddons\Data\Controllers\RestController; |
| 14 | use SuperbAddons\Data\Utils\AllowedTemplateHTMLUtil; |
| 15 | use SuperbAddons\Data\Utils\ScriptTranslations; |
| 16 | use SuperbAddons\Gutenberg\BlocksAPI\Controllers\DynamicBlockAssets; |
| 17 | use SuperbAddons\Gutenberg\BlocksAPI\Controllers\RecentPostsController; |
| 18 | use SuperbAddons\Library\Controllers\LibraryController; |
| 19 | use SuperbAddons\Library\Controllers\LibraryRequestController; |
| 20 | use SuperbAddons\Data\Controllers\OptionController; |
| 21 | |
| 22 | class GutenbergController |
| 23 | { |
| 24 | const MINIMUM_WORDPRESS_VERSION = '5.8'; |
| 25 | const MINIMUM_PHP_VERSION = '5.6'; |
| 26 | |
| 27 | const PATTERN_BLOCK_ARG = 'is_pattern_block'; |
| 28 | const BLOCKS = array( |
| 29 | ['path' => "animated-heading", "args" => ['render_callback' => array(DynamicBlockAssets::class, 'EnqueueAnimatedHeader')]], |
| 30 | ['path' => "author-box", "args" => []], |
| 31 | ['path' => "ratings", "args" => []], |
| 32 | ['path' => "table-of-contents", "args" => []], |
| 33 | ['path' => "recent-posts", "args" => ['render_callback' => array(RecentPostsController::class, 'DynamicRender')]], |
| 34 | ['path' => "cover-image", "args" => []], |
| 35 | ['path' => "google-maps", "args" => []], |
| 36 | ['path' => "reveal-buttons", "args" => []], |
| 37 | ['path' => "reveal-button", "args" => ['render_callback' => array(DynamicBlockAssets::class, 'EnqueueRevealButton')]], |
| 38 | ['path' => "accordion", "args" => ['render_callback' => array(DynamicBlockAssets::class, 'EnqueueAccordion')]] |
| 39 | ); |
| 40 | |
| 41 | public function __construct() |
| 42 | { |
| 43 | WizardRestorationPointController::Initialize(); |
| 44 | |
| 45 | if (!self::is_compatible()) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | add_action('block_categories_all', array($this, 'RegisterBlockCategory'), defined('PHP_INT_MAX') ? PHP_INT_MAX : 999, 2); |
| 50 | add_action('init', array($this, 'RegisterBlocks'), 0); |
| 51 | add_action('enqueue_block_editor_assets', array($this, 'EnqueueBlockEditorAssets')); |
| 52 | |
| 53 | add_action("enqueue_block_assets", array($this, 'EnqueueEditorIframeAssets')); |
| 54 | add_action("wp_enqueue_scripts", array($this, 'EnqueuePatternAssets')); |
| 55 | |
| 56 | add_action('enqueue_block_editor_assets', array($this, 'EnqueueVariableFallbacks'), PHP_INT_MIN); |
| 57 | add_action("wp_enqueue_scripts", array($this, 'EnqueueVariableFallbacks'), PHP_INT_MIN); |
| 58 | |
| 59 | GutenbergEnhancementsController::Initialize(); |
| 60 | WizardController::Initialize(); |
| 61 | } |
| 62 | |
| 63 | public static function is_compatible() |
| 64 | { |
| 65 | // Check for required WP version |
| 66 | if (version_compare(get_bloginfo('version'), self::MINIMUM_WORDPRESS_VERSION, '<')) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | // Check for required PHP version |
| 71 | if (version_compare(PHP_VERSION, self::MINIMUM_PHP_VERSION, '<')) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | public static function is_block_theme() |
| 79 | { |
| 80 | if (!function_exists('wp_is_block_theme')) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if (!wp_is_block_theme()) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | public static function GetGutenbergLibraryMenuItems() |
| 92 | { |
| 93 | return array( |
| 94 | array( |
| 95 | "id" => "patterns", |
| 96 | "premium_url" => AdminLinkUtil::GetLink(AdminLinkSource::LIBRARY_ITEM), |
| 97 | "title" => esc_html__('Patterns', "superb-blocks"), |
| 98 | "routes" => array( |
| 99 | "list" => LibraryRequestController::GUTENBERG_LIST_ROUTE . LibraryRequestController::GUTENBERG_ROUTE_TYPE_PATTERNS, |
| 100 | "insert" => LibraryRequestController::GUTENBERG_INSERT_ROUTE . LibraryRequestController::GUTENBERG_ROUTE_TYPE_PATTERNS |
| 101 | ), |
| 102 | "hidden" => false |
| 103 | ), |
| 104 | array( |
| 105 | "id" => "pages", |
| 106 | "premium_url" => AdminLinkUtil::GetLink(AdminLinkSource::LIBRARY_PAGE_ITEM), |
| 107 | "title" => esc_html__('Pages', "superb-blocks"), |
| 108 | "routes" => array( |
| 109 | "list" => LibraryRequestController::GUTENBERG_LIST_ROUTE . LibraryRequestController::GUTENBERG_ROUTE_TYPE_PAGES, |
| 110 | "insert" => LibraryRequestController::GUTENBERG_INSERT_ROUTE . LibraryRequestController::GUTENBERG_ROUTE_TYPE_PAGES |
| 111 | ), |
| 112 | "hidden" => false |
| 113 | ) |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | public function EnqueuePatternAssets() |
| 118 | { |
| 119 | wp_register_style( |
| 120 | 'superb-addons-patterns', |
| 121 | SUPERBADDONS_ASSETS_PATH . '/css/patterns.min.css', |
| 122 | array(), |
| 123 | SUPERBADDONS_VERSION |
| 124 | ); |
| 125 | wp_enqueue_style( |
| 126 | 'superb-addons-patterns' |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | public function EnqueueVariableFallbacks() |
| 131 | { |
| 132 | $fallbacks = ":root{--wp--preset--color--primary:#1f7cec;--wp--preset--color--primary-hover:#3993ff;--wp--preset--color--base:#fff;--wp--preset--color--featured:#0a284b;--wp--preset--color--contrast-light:#fff;--wp--preset--color--contrast-dark:#000;--wp--preset--color--mono-1:#0d3c74;--wp--preset--color--mono-2:#64748b;--wp--preset--color--mono-3:#e2e8f0;--wp--preset--color--mono-4:#f8fafc;--wp--preset--spacing--superbspacing-xxsmall:clamp(5px,1vw,10px);--wp--preset--spacing--superbspacing-xsmall:clamp(10px,2vw,20px);--wp--preset--spacing--superbspacing-small:clamp(20px,4vw,40px);--wp--preset--spacing--superbspacing-medium:clamp(30px,6vw,60px);--wp--preset--spacing--superbspacing-large:clamp(40px,8vw,80px);--wp--preset--spacing--superbspacing-xlarge:clamp(50px,10vw,100px);--wp--preset--spacing--superbspacing-xxlarge:clamp(60px,12vw,120px);--wp--preset--font-size--superbfont-tiny:clamp(10px,0.625rem + ((1vw - 3.2px) * 0.227),12px);--wp--preset--font-size--superbfont-xxsmall:clamp(12px,0.75rem + ((1vw - 3.2px) * 0.227),14px);--wp--preset--font-size--superbfont-xsmall:clamp(16px,1rem + ((1vw - 3.2px) * 1),16px);--wp--preset--font-size--superbfont-small:clamp(16px,1rem + ((1vw - 3.2px) * 0.227),18px);--wp--preset--font-size--superbfont-medium:clamp(18px,1.125rem + ((1vw - 3.2px) * 0.227),20px);--wp--preset--font-size--superbfont-large:clamp(24px,1.5rem + ((1vw - 3.2px) * 0.909),32px);--wp--preset--font-size--superbfont-xlarge:clamp(32px,2rem + ((1vw - 3.2px) * 1.818),48px);--wp--preset--font-size--superbfont-xxlarge:clamp(40px,2.5rem + ((1vw - 3.2px) * 2.727),64px)}.has-primary-color{color:var(--wp--preset--color--primary)!important}.has-primary-hover-color{color:var(--wp--preset--color--primary-hover)!important}.has-base-color{color:var(--wp--preset--color--base)!important}.has-featured-color{color:var(--wp--preset--color--featured)!important}.has-contrast-light-color{color:var(--wp--preset--color--contrast-light)!important}.has-contrast-dark-color{color:var(--wp--preset--color--contrast-dark)!important}.has-mono-1-color{color:var(--wp--preset--color--mono-1)!important}.has-mono-2-color{color:var(--wp--preset--color--mono-2)!important}.has-mono-3-color{color:var(--wp--preset--color--mono-3)!important}.has-mono-4-color{color:var(--wp--preset--color--mono-4)!important}.has-primary-background-color{background-color:var(--wp--preset--color--primary)!important}.has-primary-hover-background-color{background-color:var(--wp--preset--color--primary-hover)!important}.has-base-background-color{background-color:var(--wp--preset--color--base)!important}.has-featured-background-color{background-color:var(--wp--preset--color--featured)!important}.has-contrast-light-background-color{background-color:var(--wp--preset--color--contrast-light)!important}.has-contrast-dark-background-color{background-color:var(--wp--preset--color--contrast-dark)!important}.has-mono-1-background-color{background-color:var(--wp--preset--color--mono-1)!important}.has-mono-2-background-color{background-color:var(--wp--preset--color--mono-2)!important}.has-mono-3-background-color{background-color:var(--wp--preset--color--mono-3)!important}.has-mono-4-background-color{background-color:var(--wp--preset--color--mono-4)!important}.has-superbfont-tiny-font-size{font-size:var(--wp--preset--font-size--superbfont-tiny)!important}.has-superbfont-xxsmall-font-size{font-size:var(--wp--preset--font-size--superbfont-xxsmall)!important}.has-superbfont-xsmall-font-size{font-size:var(--wp--preset--font-size--superbfont-xsmall)!important}.has-superbfont-small-font-size{font-size:var(--wp--preset--font-size--superbfont-small)!important}.has-superbfont-medium-font-size{font-size:var(--wp--preset--font-size--superbfont-medium)!important}.has-superbfont-large-font-size{font-size:var(--wp--preset--font-size--superbfont-large)!important}.has-superbfont-xlarge-font-size{font-size:var(--wp--preset--font-size--superbfont-xlarge)!important}.has-superbfont-xxlarge-font-size{font-size:var(--wp--preset--font-size--superbfont-xxlarge)!important}"; |
| 133 | wp_register_style('superb-addons-variable-fallbacks', false, array(), SUPERBADDONS_VERSION); |
| 134 | wp_enqueue_style('superb-addons-variable-fallbacks'); |
| 135 | wp_add_inline_style('superb-addons-variable-fallbacks', $fallbacks); |
| 136 | } |
| 137 | |
| 138 | public function EnqueueEditorIframeAssets() |
| 139 | { |
| 140 | global $pagenow; |
| 141 | // Enqueues as block assets - check the current page to make sure we only enqueue on the site editor page and not on the frontend |
| 142 | if ('site-editor.php' === $pagenow) { |
| 143 | wp_enqueue_style( |
| 144 | 'superb-gutenberg-layout-library', |
| 145 | SUPERBADDONS_ASSETS_PATH . '/css/layout-library-preview.min.css', |
| 146 | array(), |
| 147 | SUPERBADDONS_VERSION |
| 148 | ); |
| 149 | // Enhancements |
| 150 | wp_enqueue_style( |
| 151 | 'superb-addons-editor-enhancements', |
| 152 | SUPERBADDONS_ASSETS_PATH . '/css/editor-enhancements.min.css', |
| 153 | array(), |
| 154 | SUPERBADDONS_VERSION |
| 155 | ); |
| 156 | // Patterns |
| 157 | $this->EnqueuePatternAssets(); |
| 158 | $this->EnqueueVariableFallbacks(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | public function EnqueueBlockEditorAssets() |
| 163 | { |
| 164 | self::AddonsLibrary(); |
| 165 | self::EditorEnhancements(); |
| 166 | $this->EnqueuePatternAssets(); |
| 167 | wp_enqueue_script( |
| 168 | 'superb-addons-gutenberg-library', |
| 169 | SUPERBADDONS_ASSETS_PATH . '/js/gutenberg/pattern-library.js', |
| 170 | array("jquery", "wp-plugins", "wp-hooks", "wp-data", "wp-element", "wp-i18n", "wp-components", "wp-compose", "wp-blocks", "wp-editor"), |
| 171 | SUPERBADDONS_VERSION, |
| 172 | true |
| 173 | ); |
| 174 | ScriptTranslations::Set('superb-addons-gutenberg-library'); |
| 175 | wp_localize_script('superb-addons-gutenberg-library', 'superblayoutlibrary_g', array( |
| 176 | "style_placeholder" => esc_html__('All themes', "superb-blocks"), |
| 177 | "category_placeholder" => esc_html__('All categories', "superb-blocks"), |
| 178 | "snacks" => array( |
| 179 | "settings_save_message" => esc_html__("Settings saved successfully.", "superb-blocks"), |
| 180 | "settings_save_error" => esc_html__("Something went wrong while attempting to save your settings. Please try again or contact support if the problem persists.", "superb-blocks"), |
| 181 | "insert_error" => esc_html__('Something went wrong while attempting to insert this element. Please try again or contact support if the problem persists.', "superb-blocks"), |
| 182 | "list_error" => esc_html__('Something went wrong while attempting to list elements. Please try again or contact support if the problem persists.', "superb-blocks") |
| 183 | ), |
| 184 | "menu_items" => self::GetGutenbergLibraryMenuItems(), |
| 185 | "rest" => array( |
| 186 | "base" => \get_rest_url(), |
| 187 | "namespace" => RestController::NAMESPACE, |
| 188 | "nonce" => wp_create_nonce("wp_rest") |
| 189 | ), |
| 190 | "addons_link_id" => AdminLinkUtil::GetLinkID() |
| 191 | )); |
| 192 | wp_enqueue_style( |
| 193 | 'superb-addons-elements', |
| 194 | SUPERBADDONS_ASSETS_PATH . '/css/framework.min.css', |
| 195 | array(), |
| 196 | SUPERBADDONS_VERSION |
| 197 | ); |
| 198 | wp_enqueue_style( |
| 199 | 'superb-addons-font-manrope', |
| 200 | SUPERBADDONS_ASSETS_PATH . '/fonts/manrope/manrope.css', |
| 201 | array(), |
| 202 | SUPERBADDONS_VERSION |
| 203 | ); |
| 204 | wp_enqueue_style( |
| 205 | 'superb-gutenberg-editor-layout-library', |
| 206 | SUPERBADDONS_ASSETS_PATH . '/css/layout-library-editor.min.css', |
| 207 | array(), |
| 208 | SUPERBADDONS_VERSION |
| 209 | ); |
| 210 | wp_enqueue_style( |
| 211 | 'superb-gutenberg-layout-library', |
| 212 | SUPERBADDONS_ASSETS_PATH . '/css/layout-library-preview.min.css', |
| 213 | array(), |
| 214 | SUPERBADDONS_VERSION |
| 215 | ); |
| 216 | wp_enqueue_style( |
| 217 | 'superbaddons-js-snackbar', |
| 218 | SUPERBADDONS_ASSETS_PATH . '/lib/js-snackbar.min.css', |
| 219 | array(), |
| 220 | SUPERBADDONS_VERSION |
| 221 | ); |
| 222 | wp_enqueue_script('superb-addons-select2', SUPERBADDONS_ASSETS_PATH . '/lib/select2.min.js', array('jquery'), SUPERBADDONS_VERSION, true); |
| 223 | wp_enqueue_style( |
| 224 | 'superbaddons-select2', |
| 225 | SUPERBADDONS_ASSETS_PATH . '/lib/select2.min.css', |
| 226 | array(), |
| 227 | SUPERBADDONS_VERSION |
| 228 | ); |
| 229 | |
| 230 | wp_enqueue_script( |
| 231 | 'superbaddons-animated-heading', |
| 232 | SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/animated-heading.js', |
| 233 | [], |
| 234 | SUPERBADDONS_VERSION, |
| 235 | true |
| 236 | ); |
| 237 | |
| 238 | // Enhancements |
| 239 | wp_enqueue_style( |
| 240 | 'superb-addons-editor-enhancements', |
| 241 | SUPERBADDONS_ASSETS_PATH . '/css/editor-enhancements.min.css', |
| 242 | array(), |
| 243 | SUPERBADDONS_VERSION |
| 244 | ); |
| 245 | |
| 246 | /// Compatibility |
| 247 | |
| 248 | if (SettingsController::IsCompatibilitySettingRelevantAndEnabled(CompatibilitySettingsOptionKey::SPECTRA_BLOCK_SPACING)) { |
| 249 | wp_enqueue_script( |
| 250 | 'superb-addons-block-spacing-compatibility-fix', |
| 251 | SUPERBADDONS_ASSETS_PATH . '/js/compatibility/block-spacing.js', |
| 252 | array('jquery'), |
| 253 | SUPERBADDONS_VERSION, |
| 254 | true |
| 255 | ); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | public function RegisterBlockCategory($block_categories, $block_editor_context) |
| 260 | { |
| 261 | return array_merge( |
| 262 | array( |
| 263 | array( |
| 264 | 'slug' => 'superb-addons-blocks', |
| 265 | 'title' => __('Superb Addons', "superb-blocks"), |
| 266 | ), |
| 267 | ), |
| 268 | $block_categories |
| 269 | ); |
| 270 | } |
| 271 | |
| 272 | public function RegisterBlocks() |
| 273 | { |
| 274 | foreach (self::BLOCKS as $block) { |
| 275 | if (isset($block['args'][self::PATTERN_BLOCK_ARG])) { |
| 276 | $block['args']['category'] = 'superb-addons-blocks-patterns'; |
| 277 | unset($block['args'][self::PATTERN_BLOCK_ARG]); |
| 278 | } |
| 279 | register_block_type(SUPERBADDONS_PLUGIN_DIR . 'blocks/' . $block['path'], $block['args']); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | private static function EditorEnhancements() |
| 284 | { |
| 285 | add_action('admin_footer', function () { |
| 286 | AllowedTemplateHTMLUtil::enable_safe_styles(); |
| 287 | ob_start(); |
| 288 | include(SUPERBADDONS_PLUGIN_DIR . 'src/gutenberg/templates/block-quick-options.php'); |
| 289 | $template = ob_get_clean(); |
| 290 | echo '<script type="text/template" id="tmpl-gutenberg-superb-block-quick-options">' . wp_kses($template, AllowedTemplateHTMLUtil::get_allowed_html()) . '</script>'; |
| 291 | AllowedTemplateHTMLUtil::disable_safe_styles(); |
| 292 | }); |
| 293 | } |
| 294 | |
| 295 | public static function AddonsLibrary() |
| 296 | { |
| 297 | add_action('admin_footer', function () { |
| 298 | AllowedTemplateHTMLUtil::enable_safe_styles(); |
| 299 | ///// Buttons |
| 300 | ob_start(); |
| 301 | include(SUPERBADDONS_PLUGIN_DIR . 'src/gutenberg/templates/library-button.php'); |
| 302 | $template = ob_get_clean(); |
| 303 | echo '<script type="text/template" id="tmpl-gutenberg-superb-library-button">' . wp_kses($template, "post") . '</script>'; |
| 304 | // |
| 305 | ob_start(); |
| 306 | include(SUPERBADDONS_PLUGIN_DIR . 'src/gutenberg/templates/pattern-tab-library-button.php'); |
| 307 | $template = ob_get_clean(); |
| 308 | echo '<script type="text/template" id="tmpl-gutenberg-superb-library-button-patternstab">' . wp_kses($template, "post") . '</script>'; |
| 309 | // |
| 310 | ob_start(); |
| 311 | include(SUPERBADDONS_PLUGIN_DIR . 'src/gutenberg/templates/appender-button.php'); |
| 312 | $template = ob_get_clean(); |
| 313 | echo '<script type="text/template" id="tmpl-gutenberg-superb-library-appender-button">' . wp_kses($template, "post") . '</script>'; |
| 314 | AllowedTemplateHTMLUtil::disable_safe_styles(); |
| 315 | ////// Library |
| 316 | LibraryController::InsertTemplatesWithWrapper(); |
| 317 | }); |
| 318 | } |
| 319 | |
| 320 | public static function GutenbergDataImportAction($data) |
| 321 | { |
| 322 | if (!isset($data['content'])) { |
| 323 | return $data; |
| 324 | } |
| 325 | |
| 326 | $options_controller = new OptionController(); |
| 327 | $preferred_domain = $options_controller->GetPreferredDomain(); |
| 328 | // Proxy is preferred if domain starts with https://superbthemes.com |
| 329 | $is_proxy_preferred = strpos($preferred_domain, 'https://superbthemes.com') === 0; |
| 330 | |
| 331 | $content = $data['content']; |
| 332 | $content = preg_replace_callback("/(http)?s?:?(\/\/[^\"']*\.(?:png|jpg|jpeg|gif|png|webp))/", function ($matches) use ($preferred_domain, $is_proxy_preferred) { |
| 333 | // Get the URL |
| 334 | $url = $matches[0]; |
| 335 | if ($is_proxy_preferred) { |
| 336 | $url = $preferred_domain . "image-library/theme-designer-images?path=" . $url; |
| 337 | } |
| 338 | $basename = pathinfo($url, PATHINFO_BASENAME); |
| 339 | $title = sanitize_title($basename); |
| 340 | |
| 341 | // Check if attachment exists based on the file slug |
| 342 | $posts = get_posts( |
| 343 | array( |
| 344 | 'post_type' => 'attachment', |
| 345 | 'title' => $title, |
| 346 | 'numberposts' => 1, |
| 347 | ) |
| 348 | ); |
| 349 | if (!empty($posts)) { |
| 350 | // Return existing attachment URL |
| 351 | $attachment = $posts[0]; |
| 352 | return wp_get_attachment_image_url($attachment->ID, 'full'); |
| 353 | } |
| 354 | |
| 355 | if (!function_exists('media_sideload_image')) { |
| 356 | require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 357 | require_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 358 | require_once(ABSPATH . 'wp-admin/includes/media.php'); |
| 359 | } |
| 360 | |
| 361 | // Create new attachment |
| 362 | $attachment_id = \media_sideload_image($url, 0, $title, 'id'); |
| 363 | if (is_wp_error($attachment_id) || !is_numeric($attachment_id)) { |
| 364 | // Return original URL if any error occurred |
| 365 | return $url; |
| 366 | } |
| 367 | |
| 368 | // Return new attachment URL |
| 369 | return wp_get_attachment_image_url($attachment_id, 'full'); |
| 370 | }, $content); |
| 371 | |
| 372 | $data['content'] = $content; |
| 373 | |
| 374 | return $data; |
| 375 | } |
| 376 | } |
| 377 |