block-editor-settings.php
4 years ago
sidebar.php
1 year ago
templates-filters.php
1 year ago
utils.php
2 years ago
woocommerce-basic.php
2 years ago
sidebar.php
130 lines
| 1 | <?php |
| 2 | |
| 3 | use Kubio\Flags; |
| 4 | |
| 5 | add_filter( |
| 6 | 'kubio/instance-flags-default', |
| 7 | function ( $flags ) { |
| 8 | |
| 9 | $flags['wc_sidebar_populated'] = false; |
| 10 | |
| 11 | return $flags; |
| 12 | } |
| 13 | ); |
| 14 | |
| 15 | function kubio_get_woocommerce_sidebar_defaults() { |
| 16 | return array( |
| 17 | |
| 18 | array( |
| 19 | 'id' => 'woocommerce_widget_cart', |
| 20 | 'data' => array( |
| 21 | 'title' => __( 'Cart', 'kubio' ), |
| 22 | 'hide_if_empty' => 1, |
| 23 | ), |
| 24 | ), |
| 25 | |
| 26 | array( |
| 27 | 'id' => 'block', |
| 28 | 'data' => array( |
| 29 | 'content' => '<!-- wp:separator {"color":"kubio-color-1","className":"is-style-wide"} -->' . |
| 30 | '<hr class="wp-block-separator has-text-color has-background has-kubio-color-1-background-color has-kubio-color-1-color is-style-wide"/>' . |
| 31 | '<!-- /wp:separator -->', |
| 32 | ), |
| 33 | ), |
| 34 | |
| 35 | array( |
| 36 | 'id' => 'woocommerce_price_filter', |
| 37 | 'data' => array( |
| 38 | 'title' => __( 'Filter by price', 'kubio' ), |
| 39 | ), |
| 40 | ), |
| 41 | |
| 42 | array( |
| 43 | 'id' => 'woocommerce_recently_viewed_products', |
| 44 | 'data' => array( |
| 45 | 'title' => __( 'Recently Viewed Products', 'kubio' ), |
| 46 | 'number' => 10, |
| 47 | ), |
| 48 | ), |
| 49 | array( |
| 50 | 'id' => 'woocommerce_top_rated_products', |
| 51 | 'data' => array( |
| 52 | 'title' => __( 'Top rated products', 'kubio' ), |
| 53 | 'number' => 5, |
| 54 | ), |
| 55 | ), |
| 56 | array( |
| 57 | 'id' => 'woocommerce_layered_nav_filters', |
| 58 | 'data' => array( |
| 59 | 'title' => __( 'Active filters', 'kubio' ), |
| 60 | ), |
| 61 | ), |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | function kubio_insert_widget_in_sidebar( $widget_id, $widget_data, $sidebar ) { |
| 66 | // Retrieve sidebars, widgets and their instances |
| 67 | $sidebars_widgets = get_option( 'sidebars_widgets', array() ); |
| 68 | $widget_instances = get_option( 'widget_' . $widget_id, array() ); |
| 69 | |
| 70 | // Retrieve the key of the next widget instance |
| 71 | $numeric_keys = array_filter( array_keys( $widget_instances ), 'is_int' ); |
| 72 | $next_key = $numeric_keys ? max( $numeric_keys ) + 1 : 2; |
| 73 | |
| 74 | // Add this widget to the sidebar |
| 75 | if ( ! isset( $sidebars_widgets[ $sidebar ] ) ) { |
| 76 | $sidebars_widgets[ $sidebar ] = array(); |
| 77 | } |
| 78 | $sidebars_widgets[ $sidebar ][] = $widget_id . '-' . $next_key; |
| 79 | |
| 80 | // Add the new widget instance |
| 81 | $widget_instances[ $next_key ] = $widget_data; |
| 82 | |
| 83 | // Store updated sidebars, widgets and their instances |
| 84 | update_option( 'sidebars_widgets', $sidebars_widgets ); |
| 85 | update_option( 'widget_' . $widget_id, $widget_instances ); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | function kubio_maybe_populate_woocommerce_sidebar() { |
| 90 | $sidebars_widgets = get_option( 'sidebars_widgets', array() ); |
| 91 | |
| 92 | $woocommerce_sidebar = isset( $sidebars_widgets['kubio-woocommerce'] ) ? $sidebars_widgets['kubio-woocommerce'] : array(); |
| 93 | |
| 94 | if ( empty( $woocommerce_sidebar ) && ! Flags::get( 'wc_sidebar_populated' ) ) { |
| 95 | Flags::set( 'wc_sidebar_populated', true ); |
| 96 | $kubio_woocommerce_sidebar_defaults = kubio_get_woocommerce_sidebar_defaults(); |
| 97 | |
| 98 | $woocommerce_sidebar = array(); |
| 99 | |
| 100 | foreach ( $kubio_woocommerce_sidebar_defaults as $widget ) { |
| 101 | kubio_insert_widget_in_sidebar( $widget['id'], $widget['data'], 'kubio-woocommerce' ); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
| 107 | function kubio_register_woocommerce_sidebar() { |
| 108 | |
| 109 | if ( ! class_exists( 'WooCommerce' ) ) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | if ( kubio_has_kubio_woocommerce_support() ) { |
| 114 | register_sidebar( |
| 115 | array( |
| 116 | 'name' => __( 'WooCommerce Widgets Area', 'kubio' ), |
| 117 | 'id' => 'kubio-woocommerce', |
| 118 | 'description' => __( 'Add widgets here to appear in the WooCommerce sidebar.', 'kubio' ), |
| 119 | 'before_widget' => '<div id="%1$s" class="widget %2$s">', |
| 120 | 'before_title' => '<h5 class="widgettitle">', |
| 121 | 'after_title' => '</h5>', |
| 122 | 'after_widget' => '</div>', |
| 123 | ) |
| 124 | ); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | add_action( 'widgets_init', 'kubio_register_woocommerce_sidebar' ); |
| 129 | add_action( 'admin_init', 'kubio_maybe_populate_woocommerce_sidebar' ); |
| 130 |