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