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