block-editor-settings.php
4 years ago
sidebar.php
1 year ago
templates-filters.php
1 month ago
utils.php
2 years ago
woocommerce-basic.php
2 years ago
templates-filters.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | /** |
| 5 | * Override the default woocommerce templates with block templates |
| 6 | * |
| 7 | * @param string $template |
| 8 | * |
| 9 | * @return string |
| 10 | */ |
| 11 | function kubio_woocommerce_support_template_include( $template ) { |
| 12 | |
| 13 | $post_type = get_post_type(); |
| 14 | $is_displaying_products = is_archive() || is_single() || is_tax(); |
| 15 | |
| 16 | // This theme doesn't have a traditional sidebar. |
| 17 | remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 ); |
| 18 | |
| 19 | if ( $post_type === 'product' && $is_displaying_products ) { |
| 20 | |
| 21 | if ( !empty($template) && strpos( $template, 'template-canvas' ) === false ) { |
| 22 | $pathinfo = pathinfo( $template ); |
| 23 | $default_file = isset( $pathinfo['filename'] ) ? $pathinfo['filename'] : null; |
| 24 | |
| 25 | if ( ! $default_file ) { |
| 26 | return $template; |
| 27 | } |
| 28 | |
| 29 | $default_file_with_extension = "$default_file.php"; |
| 30 | $templates = is_archive() ? array( $default_file_with_extension, 'archive-product.php', 'archive.php' ) : array( $default_file_with_extension, 'single-product.php', 'single.php' ); |
| 31 | |
| 32 | if ( is_tax() ) { |
| 33 | $templates = array( 'archive-product.php', 'archive.php' ); |
| 34 | } |
| 35 | |
| 36 | $templates = array_unique( $templates ); |
| 37 | $template = locate_block_template( $default_file_with_extension, 'wp_template', $templates ); |
| 38 | } |
| 39 | |
| 40 | global $_wp_current_template_content; |
| 41 | |
| 42 | if ( ! empty( $_wp_current_template_content ) ) { |
| 43 | |
| 44 | $content = kubio_get_woocommerce_content(); |
| 45 | // escape the dollar sign so preg_replace will not think it's a backreference |
| 46 | $content = addcslashes( $content, '$' ); |
| 47 | |
| 48 | if ( is_single() ) { |
| 49 | add_theme_support( 'wc-product-gallery-zoom' ); |
| 50 | add_theme_support( 'wc-product-gallery-lightbox' ); |
| 51 | add_theme_support( 'wc-product-gallery-slider' ); |
| 52 | } |
| 53 | |
| 54 | // replace the 'post-content' block with the <!-- wp:html --> block to display the woocommerce content |
| 55 | $_wp_current_template_content = preg_replace( |
| 56 | '#<!-- wp:post-content(.*)/-->#', |
| 57 | '<!-- wp:html -->' . $content . '<!-- /wp:html -->', |
| 58 | $_wp_current_template_content |
| 59 | ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return $template; |
| 64 | } |
| 65 | |
| 66 | function kubio_woocommerce_support_rendered_content( $content ) { |
| 67 | $post_type = get_post_type(); |
| 68 | $is_displaying_products = is_archive() || is_single(); |
| 69 | |
| 70 | if ( $post_type === 'product' && $is_displaying_products ) { |
| 71 | $content = kubio_get_woocommerce_content(); |
| 72 | } |
| 73 | |
| 74 | return $content; |
| 75 | } |
| 76 | |
| 77 | add_filter( 'template_include', 'kubio_woocommerce_support_template_include', 20, 1 ); |
| 78 | |
| 79 | add_filter( 'kubio/editor/rendered-content', 'kubio_woocommerce_support_rendered_content' ); |
| 80 | |
| 81 | |
| 82 | function kubio_woocommerce_achive_page_title() { |
| 83 | if ( function_exists( 'is_shop' ) && is_shop() && wc_get_page_id( 'shop' ) ) { |
| 84 | return get_the_title( wc_get_page_id( 'shop' ) ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | add_filter( 'post_type_archive_title', 'kubio_woocommerce_achive_page_title' ); |
| 89 |