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