AI
1 year ago
admin-pages
2 years ago
api
1 year ago
blog
3 years ago
customizer
2 years ago
filters
1 year ago
full-site-editing
2 years ago
importer
4 years ago
integrations
1 year ago
menu
3 years ago
polyfills
3 years ago
preview
2 years ago
shapes
2 years ago
shortcodes
2 years ago
src
1 year ago
add-edit-in-kubio.php
1 year ago
editor-assets.php
1 year ago
env.php
1 year ago
filters.php
1 year ago
frontend.php
4 years ago
global-data.php
1 year ago
init.php
2 years ago
kubio-block-library.php
2 years ago
kubio-editor.php
1 year ago
load.php
1 year ago
filters.php
319 lines
| 1 | <?php |
| 2 | |
| 3 | use IlluminateAgnostic\Arr\Support\Arr; |
| 4 | use Kubio\Core\LodashBasic; |
| 5 | use Kubio\Core\Utils; |
| 6 | |
| 7 | add_filter( |
| 8 | 'kubio/preview/template_part_blocks', |
| 9 | function ($parts = []) { |
| 10 | return array_merge((array) $parts, [ |
| 11 | 'core/template-part', |
| 12 | 'kubio/header', |
| 13 | 'kubio/footer', |
| 14 | 'kubio/sidebar', |
| 15 | ]); |
| 16 | }, |
| 17 | 5, |
| 18 | 1 |
| 19 | ); |
| 20 | |
| 21 | function kubio_blocks_update_template_parts_theme($parsed_blocks, $theme) |
| 22 | { |
| 23 | $parts_block_names = apply_filters( |
| 24 | 'kubio/preview/template_part_blocks', |
| 25 | [] |
| 26 | ); |
| 27 | |
| 28 | Utils::walkBlocks($parsed_blocks, function (&$block) use ( |
| 29 | $theme, |
| 30 | $parts_block_names |
| 31 | ) { |
| 32 | $block_name = Arr::get($block, 'blockName'); |
| 33 | $current_theme = Arr::get($block, 'attrs.theme'); |
| 34 | $is_template_part = in_array($block_name, $parts_block_names); |
| 35 | |
| 36 | if ($block_name && ($current_theme || $is_template_part)) { |
| 37 | Arr::set($block, 'attrs.theme', $theme); |
| 38 | } |
| 39 | }); |
| 40 | |
| 41 | return $parsed_blocks; |
| 42 | } |
| 43 | |
| 44 | //this code is not required for the attributes to work. But it could be a problem in the future if we don't register the |
| 45 | //anchor attribute |
| 46 | function kubio_register_anchor_attribute($metaData) |
| 47 | { |
| 48 | $supportsAnchor = LodashBasic::get( |
| 49 | $metaData, |
| 50 | ['supports', 'anchor'], |
| 51 | false |
| 52 | ); |
| 53 | if ($supportsAnchor) { |
| 54 | $hasAnchorAttribute = LodashBasic::get( |
| 55 | $metaData, |
| 56 | ['attributes', 'anchor'], |
| 57 | false |
| 58 | ); |
| 59 | if (!$hasAnchorAttribute) { |
| 60 | $anchorData = [ |
| 61 | 'type' => 'string', |
| 62 | ]; |
| 63 | LodashBasic::set($metaData, ['attributes', 'anchor'], $anchorData); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return $metaData; |
| 68 | } |
| 69 | |
| 70 | add_filter( |
| 71 | 'kubio/blocks/register_block_type', |
| 72 | 'kubio_register_anchor_attribute' |
| 73 | ); |
| 74 | |
| 75 | function kubio_add_full_hd_image_size() |
| 76 | { |
| 77 | add_image_size('kubio-fullhd', 1920, 1080); |
| 78 | } |
| 79 | |
| 80 | add_filter('after_setup_theme', 'kubio_add_full_hd_image_size'); |
| 81 | |
| 82 | function kubio_url_import_cdn_files($url) |
| 83 | { |
| 84 | if (strpos($url, 'wp-content/uploads') !== false) { |
| 85 | if (\_\startsWith($url, site_url())) { |
| 86 | return $url; |
| 87 | } |
| 88 | |
| 89 | return str_replace( |
| 90 | 'https://demos.kubiobuilder.com', |
| 91 | 'https://static-assets.kubiobuilder.com/demos', |
| 92 | $url |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | return $url; |
| 97 | } |
| 98 | |
| 99 | add_filter('kubio/importer/kubio-source-url', 'kubio_url_import_cdn_files'); |
| 100 | |
| 101 | //load full width template if the page is empty |
| 102 | add_action( |
| 103 | 'wp', |
| 104 | function () { |
| 105 | /** @var WP_Query $wp_query */ |
| 106 | |
| 107 | $is_kubio_theme = kubio_theme_has_kubio_block_support(); |
| 108 | |
| 109 | //only apply to pages |
| 110 | if (is_page() && !is_front_page() && $is_kubio_theme) { |
| 111 | /** @var \WP_Post $post */ |
| 112 | global $post; |
| 113 | |
| 114 | if (!$post) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | $saved_in_kubio = get_post_meta($post->ID, 'saved_in_kubio', true); |
| 119 | if (Utils::isTrue($saved_in_kubio)) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | $template = get_page_template_slug($post->ID); |
| 124 | $is_from_kubio = Utils::hasKubioEditorReferer(); |
| 125 | if ( |
| 126 | empty(trim($post->post_content)) && |
| 127 | isset($_GET['_wp-find-template']) && |
| 128 | $is_from_kubio && |
| 129 | empty($template) |
| 130 | ) { |
| 131 | /** |
| 132 | * The locate_block_template function has a check if the get parameter the _wp-find-template is set it |
| 133 | * returns wp_send_json_success( $block_template ) with the template provided; |
| 134 | * the wp_send_json_success( $block_template ); |
| 135 | * |
| 136 | * If the full width template is found the wp_send_json_success will return the full width tempalte then die |
| 137 | * the request. If the full width template is not found then the function will return the 'full-width' text |
| 138 | * and we do nothing with it. But the code will run normally and return the normal template that should be |
| 139 | * Page. |
| 140 | */ |
| 141 | locate_block_template('full-width', 'page', ['full-width.php']); |
| 142 | locate_block_template('kubio-full-width', 'page', [ |
| 143 | 'kubio-full-width.php', |
| 144 | ]); |
| 145 | } |
| 146 | } |
| 147 | }, |
| 148 | 5 |
| 149 | ); |
| 150 | |
| 151 | //show index when on latest posts page. It's weird but 2022 theme also shows front page when you click edit and latest |
| 152 | //posts is your homepage which is wrong. |
| 153 | add_action( |
| 154 | 'wp', |
| 155 | function () { |
| 156 | /** @var WP_Query $wp_query */ |
| 157 | |
| 158 | //when the front page is the latest posts load the home or index template |
| 159 | if (is_front_page() && is_home()) { |
| 160 | $referer = Arr::get($_SERVER, 'HTTP_REFERER', ''); |
| 161 | $callFromKubio = |
| 162 | strpos($referer, 'page=kubio') !== false && |
| 163 | strpos($referer, admin_url()) !== false; |
| 164 | if (isset($_GET['_wp-find-template']) && $callFromKubio) { |
| 165 | locate_block_template('home', 'page', ['home.php']); |
| 166 | locate_block_template('index', 'page', ['index.php']); |
| 167 | } |
| 168 | } |
| 169 | }, |
| 170 | 5 |
| 171 | ); |
| 172 | function kubio_change_customize_link_to_open_kubio_editor() |
| 173 | { |
| 174 | $kubio_url = Utils::kubioGetEditorURL(); ?> |
| 175 | <script> |
| 176 | (function () { |
| 177 | var button = document.querySelector('.button.load-customize,#welcome-panel .load-customize'); |
| 178 | |
| 179 | if (button) { |
| 180 | button.href = "<?php echo esc_url($kubio_url); ?>"; |
| 181 | } |
| 182 | })(); |
| 183 | </script> |
| 184 | <?php |
| 185 | } |
| 186 | |
| 187 | add_action( |
| 188 | 'welcome_panel', |
| 189 | 'kubio_change_customize_link_to_open_kubio_editor', |
| 190 | 20 |
| 191 | ); |
| 192 | |
| 193 | function kubio_plugin_meta($plugin_meta, $plugin_file) |
| 194 | { |
| 195 | $plugins_dir = trailingslashit( |
| 196 | wp_normalize_path(WP_CONTENT_DIR . '/plugins/') |
| 197 | ); |
| 198 | $kubio_file = str_replace( |
| 199 | $plugins_dir, |
| 200 | '', |
| 201 | wp_normalize_path(KUBIO_ENTRY_FILE) |
| 202 | ); |
| 203 | if ($plugin_file === $kubio_file) { |
| 204 | $plugin_meta[0] = |
| 205 | "{$plugin_meta[0]} (build: " . KUBIO_BUILD_NUMBER . ')'; |
| 206 | } |
| 207 | |
| 208 | return $plugin_meta; |
| 209 | } |
| 210 | |
| 211 | add_filter('plugin_row_meta', 'kubio_plugin_meta', 10, 4); |
| 212 | |
| 213 | add_filter( |
| 214 | 'kubio/importer/kubio-url-placeholder-replacement', |
| 215 | function () { |
| 216 | $stylesheet = get_stylesheet(); |
| 217 | |
| 218 | return "https://static-assets.kubiobuilder.com/themes/{$stylesheet}/assets/"; |
| 219 | }, |
| 220 | 5 |
| 221 | ); |
| 222 | |
| 223 | add_action('plugins_loaded', function () { |
| 224 | // init the hasEnoughRemainingTime static variable |
| 225 | Utils::hasEnoughRemainingTime(); |
| 226 | }); |
| 227 | |
| 228 | /** |
| 229 | * This filter checks the attributes for every imported block and replaces the link values stored on the demo site like |
| 230 | * `https://support-work.kubiobuilder.com` with the site url. |
| 231 | * |
| 232 | * @param $parsed_blocks |
| 233 | * @param $demo_url |
| 234 | * @return mixed |
| 235 | */ |
| 236 | function kubio_blocks_update_block_links($parsed_blocks, $demo_url) |
| 237 | { |
| 238 | $replace = site_url(); |
| 239 | |
| 240 | Utils::walkBlocks($parsed_blocks, function (&$block) use ( |
| 241 | $demo_url, |
| 242 | $replace |
| 243 | ) { |
| 244 | $old_url = Arr::get($block, 'attrs.link.value'); |
| 245 | |
| 246 | if ($old_url !== null && !empty($old_url)) { |
| 247 | $next_url = $old_url; |
| 248 | |
| 249 | if (strpos($old_url, 'http://wpsites.') === 0) { |
| 250 | // replace internal ( extendstudio links ) |
| 251 | $next_url = preg_replace( |
| 252 | '#^http://wpsites\.(.*?)\.(.*?)/(.*?)/(.*?)/([a-zA-Z0-9-]+)#', |
| 253 | $replace, |
| 254 | $old_url |
| 255 | ); |
| 256 | } else { |
| 257 | $next_url = str_replace($demo_url, $replace, $old_url); |
| 258 | } |
| 259 | |
| 260 | if ($old_url !== $next_url) { |
| 261 | Arr::set($block, 'attrs.link.value', $next_url); |
| 262 | } |
| 263 | } |
| 264 | }); |
| 265 | |
| 266 | return $parsed_blocks; |
| 267 | } |
| 268 | |
| 269 | //This is added for woocomerce but it fixes a general issue. If the page that we preview is being redirected we need to |
| 270 | //add our flag to the redirected page or the editor will not work. |
| 271 | function kubio_add_flags_to_redirects($location) |
| 272 | { |
| 273 | $kubioFlags = [ |
| 274 | '_wp-find-template', |
| 275 | '__kubio-rendered-content', |
| 276 | '__kubio-rendered-styles', |
| 277 | '__kubio-site-edit-iframe-preview', |
| 278 | '__kubio-site-edit-iframe-classic-template', |
| 279 | '__kubio-body-class', |
| 280 | '__kubio-page-title', |
| 281 | '__kubio-page-query', |
| 282 | ]; |
| 283 | |
| 284 | foreach ($kubioFlags as $flag) { |
| 285 | if (isset($_GET[$flag]) && !empty($_GET[$flag])) { |
| 286 | $location = add_query_arg($flag, $_GET[$flag], $location); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return $location; |
| 291 | } |
| 292 | add_filter('wp_redirect', 'kubio_add_flags_to_redirects'); |
| 293 | |
| 294 | function kubio_is_black_wizard_onboarding_enabled() { |
| 295 | return apply_filters( 'kubio/is_black_wizard_onboarding_enabled', false ); |
| 296 | } |
| 297 | |
| 298 | // deactivate new block editor |
| 299 | function kubio_remove_widget_block_editor() |
| 300 | { |
| 301 | remove_theme_support('widgets-block-editor'); |
| 302 | } |
| 303 | add_action('after_setup_theme', 'kubio_remove_widget_block_editor'); |
| 304 | |
| 305 | require_once __DIR__ . '/filters/kubio-fresh-site.php'; |
| 306 | require_once __DIR__ . '/filters/dismissable-notice.php'; |
| 307 | require_once __DIR__ . '/filters/svg-kses.php'; |
| 308 | require_once __DIR__ . '/filters/post-insert.php'; |
| 309 | require_once __DIR__ . '/filters/gutenerg-plugin-check.php'; |
| 310 | require_once __DIR__ . '/filters/default-editor-overlay.php'; |
| 311 | require_once __DIR__ . '/filters/requirements-notices.php'; |
| 312 | require_once __DIR__ . '/filters/site-urls.php'; |
| 313 | require_once __DIR__ . '/filters/after-kubio-activation.php'; |
| 314 | require_once __DIR__ . '/filters/wp-import.php'; |
| 315 | require_once __DIR__ . '/filters/starter-sites-feature.php'; |
| 316 | require_once __DIR__ . '/filters/register-meta-fields.php'; |
| 317 | require_once __DIR__ . '/filters/allow-kubio-blog-override.php'; |
| 318 | require_once __DIR__ . '/filters/image-size-auto-fix.php'; |
| 319 |