admin-pages
4 years ago
api
4 years ago
blog
4 years ago
filters
4 years ago
full-site-editing
4 years ago
importer
4 years ago
integrations
4 years ago
menu
4 years ago
polyfills
4 years ago
preview
4 years ago
shapes
4 years ago
shortcodes
4 years ago
src
4 years ago
add-edit-in-kubio.php
4 years ago
editor-assets.php
4 years ago
env.php
4 years ago
filters.php
4 years ago
frontend.php
4 years ago
global-data.php
4 years ago
init.php
4 years ago
kubio-block-library.php
4 years ago
kubio-editor.php
4 years ago
load.php
4 years ago
filters.php
239 lines
| 1 | <?php |
| 2 | |
| 3 | use IlluminateAgnostic\Arr\Support\Arr; |
| 4 | use Kubio\Core\LodashBasic; |
| 5 | use Kubio\Core\Utils; |
| 6 | |
| 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 | |
| 26 | function kubio_blocks_update_template_parts_theme( $parsed_blocks, $theme ) { |
| 27 | $parts_block_names = apply_filters( 'kubio/preview/template_part_blocks', array() ); |
| 28 | |
| 29 | Utils::walkBlocks( |
| 30 | $parsed_blocks, |
| 31 | function ( &$block ) use ( $theme, $parts_block_names ) { |
| 32 | |
| 33 | $block_name = Arr::get( $block, 'blockName' ); |
| 34 | $current_theme = Arr::get( $block, 'attrs.theme' ); |
| 35 | $is_template_part = in_array( $block_name, $parts_block_names ); |
| 36 | |
| 37 | if ( $block_name && ( $current_theme || $is_template_part ) ) { |
| 38 | Arr::set( $block, 'attrs.theme', $theme ); |
| 39 | } |
| 40 | |
| 41 | } |
| 42 | ); |
| 43 | |
| 44 | return $parsed_blocks; |
| 45 | } |
| 46 | |
| 47 | //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 |
| 48 | //anchor attribute |
| 49 | function kubio_register_anchor_attribute( $metaData ) { |
| 50 | $supportsAnchor = LodashBasic::get( $metaData, array( 'supports', 'anchor' ), false ); |
| 51 | if ( $supportsAnchor ) { |
| 52 | $hasAnchorAttribute = LodashBasic::get( $metaData, array( 'attributes', 'anchor' ), false ); |
| 53 | if ( ! $hasAnchorAttribute ) { |
| 54 | $anchorData = array( |
| 55 | 'type' => 'string', |
| 56 | ); |
| 57 | LodashBasic::set( $metaData, array( 'attributes', 'anchor' ), $anchorData ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return $metaData; |
| 62 | } |
| 63 | |
| 64 | add_filter( |
| 65 | 'kubio/blocks/register_block_type', |
| 66 | 'kubio_register_anchor_attribute' |
| 67 | ); |
| 68 | |
| 69 | |
| 70 | function kubio_add_full_hd_image_size() { |
| 71 | add_image_size( 'kubio-fullhd', 1920, 1080 ); |
| 72 | } |
| 73 | |
| 74 | add_filter( 'after_setup_theme', 'kubio_add_full_hd_image_size' ); |
| 75 | |
| 76 | |
| 77 | function kubio_url_import_cdn_files( $url ) { |
| 78 | |
| 79 | if ( strpos( $url, 'wp-content/uploads' ) !== false ) { |
| 80 | |
| 81 | if ( \_\startsWith( $url, site_url() ) ) { |
| 82 | return $url; |
| 83 | } |
| 84 | |
| 85 | return str_replace( 'https://demos.kubiobuilder.com', 'https://static-assets.kubiobuilder.com/demos', $url ); |
| 86 | } |
| 87 | |
| 88 | return $url; |
| 89 | } |
| 90 | |
| 91 | add_filter( 'kubio/importer/kubio-source-url', 'kubio_url_import_cdn_files' ); |
| 92 | |
| 93 | |
| 94 | //load full width template if the page is empty |
| 95 | add_action( |
| 96 | 'wp', |
| 97 | function () { |
| 98 | /** @var WP_Query $wp_query */ |
| 99 | |
| 100 | //only apply to pages |
| 101 | $isKubioTheme = kubio_theme_has_kubio_block_support(); |
| 102 | if ( is_page() && ! is_front_page() && $isKubioTheme ) { |
| 103 | global $post; |
| 104 | $template = get_page_template_slug( $post->ID ); |
| 105 | $referer = Arr::get( $_SERVER, 'HTTP_REFERER', '' ); |
| 106 | $callFromKubio = strpos( $referer, 'page=kubio' ) !== false && strpos( $referer, admin_url() ) !== false; |
| 107 | if ( empty( trim( $post->post_content ) ) && isset( $_GET['_wp-find-template'] ) && $callFromKubio && empty( $template ) ) { |
| 108 | /** |
| 109 | * The locate_block_template function has a check if the get parameter the _wp-find-template is set it |
| 110 | * returns wp_send_json_success( $block_template ) with the template provided; |
| 111 | * the wp_send_json_success( $block_template ); |
| 112 | * |
| 113 | * If the full width template is found the wp_send_json_success will return the full width tempalte then die |
| 114 | * the request. If the full width template is not found then the function will return the 'full-width' text |
| 115 | * and we do nothing with it. But the code will run normally and return the normal template that should be |
| 116 | * Page. |
| 117 | */ |
| 118 | locate_block_template( 'full-width', 'page', array( 'full-width.php' ) ); |
| 119 | locate_block_template( 'kubio-full-width', 'page', array( 'kubio-full-width.php' ) ); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | }, |
| 124 | 5 |
| 125 | ); |
| 126 | |
| 127 | //show index when on latest posts page. It's weird but 2022 theme also shows front page when you click edit and latest |
| 128 | //posts is your homepage which is wrong. |
| 129 | add_action( |
| 130 | 'wp', |
| 131 | function () { |
| 132 | /** @var WP_Query $wp_query */ |
| 133 | |
| 134 | //when the front page is the latest posts load the index template |
| 135 | if ( is_front_page() && is_home() ) { |
| 136 | |
| 137 | $referer = Arr::get( $_SERVER, 'HTTP_REFERER', '' ); |
| 138 | $callFromKubio = strpos( $referer, 'page=kubio' ) !== false && strpos( $referer, admin_url() ) !== false; |
| 139 | if ( isset( $_GET['_wp-find-template'] ) && $callFromKubio ) { |
| 140 | locate_block_template( 'index', 'page', array( 'index.php' ) ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | }, |
| 145 | 5 |
| 146 | ); |
| 147 | function kubio_change_customize_link_to_open_kubio_editor() { |
| 148 | $kubio_url = add_query_arg( array( 'page' => 'kubio' ), admin_url( 'admin.php' ) ); |
| 149 | ?> |
| 150 | <script> |
| 151 | (function () { |
| 152 | var button = document.querySelector('.button.load-customize'); |
| 153 | |
| 154 | if (button) { |
| 155 | button.href = "<?php echo esc_url( $kubio_url ); ?>"; |
| 156 | } |
| 157 | })(); |
| 158 | </script> |
| 159 | <?php |
| 160 | } |
| 161 | |
| 162 | add_action( 'welcome_panel', 'kubio_change_customize_link_to_open_kubio_editor', 20 ); |
| 163 | |
| 164 | function kubio_plugin_meta( $plugin_meta, $plugin_file ) { |
| 165 | $plugins_dir = trailingslashit( wp_normalize_path( WP_CONTENT_DIR . '/plugins/' ) ); |
| 166 | $kubio_file = str_replace( $plugins_dir, '', wp_normalize_path( KUBIO_ENTRY_FILE ) ); |
| 167 | if ( $plugin_file === $kubio_file ) { |
| 168 | $plugin_meta[0] = "{$plugin_meta[0]} (build: " . KUBIO_BUILD_NUMBER . ')'; |
| 169 | } |
| 170 | |
| 171 | return $plugin_meta; |
| 172 | } |
| 173 | |
| 174 | add_filter( 'plugin_row_meta', 'kubio_plugin_meta', 10, 4 ); |
| 175 | |
| 176 | add_filter( |
| 177 | 'kubio/importer/kubio-url-placeholder-replacement', |
| 178 | function () { |
| 179 | $stylesheet = get_stylesheet(); |
| 180 | |
| 181 | return "https://static-assets.kubiobuilder.com/themes/{$stylesheet}/assets/"; |
| 182 | }, |
| 183 | 5 |
| 184 | ); |
| 185 | |
| 186 | add_action( |
| 187 | 'plugins_loaded', |
| 188 | function() { |
| 189 | // init the hasEnoughRemainingTime static variable |
| 190 | Utils::hasEnoughRemainingTime(); |
| 191 | } |
| 192 | ); |
| 193 | |
| 194 | /** |
| 195 | * This filter checks the attributes for every imported block and replaces the link values stored on the demo site like |
| 196 | * `https://support-work.kubiobuilder.com` with the site url. |
| 197 | * |
| 198 | * @param $parsed_blocks |
| 199 | * @param $demo_url |
| 200 | * @return mixed |
| 201 | */ |
| 202 | function kubio_blocks_update_block_links( $parsed_blocks, $demo_url ) { |
| 203 | $replace = site_url(); |
| 204 | |
| 205 | Utils::walkBlocks( |
| 206 | $parsed_blocks, |
| 207 | function ( &$block ) use ( $demo_url, $replace ) { |
| 208 | |
| 209 | $old_url = Arr::get( $block, 'attrs.link.value' ); |
| 210 | |
| 211 | if ( $old_url !== null && ! empty( $old_url ) ) { |
| 212 | $next_url = $old_url; |
| 213 | |
| 214 | if ( strpos( $old_url, 'http://wpsites.' ) === 0 ) { |
| 215 | // replace internal ( extendstudio links ) |
| 216 | $next_url = preg_replace( '#^http://wpsites\.(.*?)\.(.*?)/(.*?)/(.*?)/([a-zA-Z0-9-]+)#', $replace, $old_url ); |
| 217 | } else { |
| 218 | $next_url = str_replace( $demo_url, $replace, $old_url ); |
| 219 | } |
| 220 | |
| 221 | if ( $old_url !== $next_url ) { |
| 222 | Arr::set( $block, 'attrs.link.value', $next_url ); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | ); |
| 227 | |
| 228 | return $parsed_blocks; |
| 229 | } |
| 230 | |
| 231 | require_once __DIR__ . '/filters/dismissable-notice.php'; |
| 232 | require_once __DIR__ . '/filters/svg-kses.php'; |
| 233 | require_once __DIR__ . '/filters/post-insert.php'; |
| 234 | require_once __DIR__ . '/filters/gutenerg-plugin-check.php'; |
| 235 | require_once __DIR__ . '/filters/classic-editor-overlay.php'; |
| 236 | require_once __DIR__ . '/filters/requirements-notices.php'; |
| 237 | require_once __DIR__ . '/filters/site-urls.php'; |
| 238 | |
| 239 |