lightspeeddevelopment /
lsx-search
| 1 | <?php |
||
| 2 | namespace lsx\search\classes\frontend; |
||
| 3 | |||
| 4 | use LSX_Search; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Houses the functions for the CMB2 Settings page. |
||
| 8 | * |
||
| 9 | * @package lsx-search |
||
| 10 | */ |
||
| 11 | class Layout { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Holds class instance |
||
| 15 | * |
||
| 16 | * @since 1.0.0 |
||
| 17 | * |
||
| 18 | * @var object \lsx\search\classes\frontend\Layout() |
||
| 19 | */ |
||
| 20 | protected static $instance = null; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Contructor |
||
| 24 | */ |
||
| 25 | public function __construct() { |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 26 | add_action( 'wp', array( $this, 'load_functions' ), 24 ); |
||
| 27 | } |
||
|
0 ignored issues
–
show
|
|||
| 28 | |||
| 29 | /** |
||
| 30 | * Return an instance of this class. |
||
| 31 | * |
||
| 32 | * @since 1.0.0 |
||
| 33 | * |
||
| 34 | * @return object \lsx\search\classes\frontend\Layout() A single instance of this class. |
||
| 35 | */ |
||
| 36 | public static function get_instance() { |
||
|
0 ignored issues
–
show
|
|||
| 37 | // If the single instance hasn't been set, set it now. |
||
| 38 | if ( null == self::$instance ) { |
||
|
0 ignored issues
–
show
|
|||
| 39 | self::$instance = new self(); |
||
| 40 | } |
||
| 41 | return self::$instance; |
||
| 42 | } |
||
|
0 ignored issues
–
show
|
|||
| 43 | |||
| 44 | /** |
||
| 45 | * Check all settings. |
||
| 46 | */ |
||
| 47 | public function load_functions() { |
||
|
0 ignored issues
–
show
|
|||
| 48 | $lsx_search = LSX_Search::get_instance(); |
||
| 49 | if ( $lsx_search->frontend->search_enabled ) { |
||
|
0 ignored issues
–
show
|
|||
| 50 | if ( isset( $lsx_search->frontend->options['display'][ $lsx_search->frontend->search_prefix . '_layout_switcher_enable' ] ) ) { |
||
|
0 ignored issues
–
show
|
|||
| 51 | add_filter( 'lsx_blog_customizer_show_switcher', array( $this, 'show_layout_switcher' ), 10, 1 ); |
||
| 52 | add_filter( 'lsx_layout_switcher_options', array( $this, 'lsx_layout_switcher_options' ), 10, 1 ); |
||
| 53 | add_filter( 'lsx_layout_switcher_page_key', array( $this, 'lsx_layout_switcher_page_key' ), 10, 1 ); |
||
| 54 | add_filter( 'lsx_layout_switcher_options_default', array( $this, 'lsx_layout_switcher_options_default' ), 10, 1 ); |
||
| 55 | |||
| 56 | // Layout Classes |
||
| 57 | add_filter( 'woocommerce_product_loop_start', array( $this, 'woocommerce_layout_class' ), 10, 1 ); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | } |
||
|
0 ignored issues
–
show
|
|||
| 61 | |||
| 62 | /** |
||
| 63 | * Display the woocommerce archive swticher. |
||
| 64 | */ |
||
| 65 | public function show_layout_switcher( $show = false ) { |
||
|
0 ignored issues
–
show
|
|||
| 66 | if ( is_search() ) { |
||
|
0 ignored issues
–
show
|
|||
| 67 | $archive_layout_switcher = get_theme_mod( 'lsx_blog_customizer_archive_layout_switcher', false ); |
||
| 68 | if ( true === $archive_layout_switcher ) { |
||
|
0 ignored issues
–
show
|
|||
| 69 | $show = true; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | return $show; |
||
| 73 | } |
||
|
0 ignored issues
–
show
|
|||
| 74 | |||
| 75 | /** |
||
| 76 | * Remove the default and half-grid options from the results layouts. |
||
| 77 | * |
||
| 78 | * @param array $layout_options |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | public function lsx_layout_switcher_options( $layout_options ) { |
||
|
0 ignored issues
–
show
|
|||
| 82 | unset( $layout_options['default'] ); |
||
| 83 | unset( $layout_options['half-grid'] ); |
||
| 84 | return $layout_options; |
||
| 85 | } |
||
|
0 ignored issues
–
show
|
|||
| 86 | |||
| 87 | /** |
||
| 88 | * Replace the key for the layout switcher. |
||
| 89 | * |
||
| 90 | * @param string $page_key |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | public function lsx_layout_switcher_page_key( $page_key ) { |
||
|
0 ignored issues
–
show
|
|||
| 94 | $lsx_search = LSX_Search::get_instance(); |
||
| 95 | $page_key = str_replace( '_search', '', $lsx_search->frontend->search_prefix ); |
||
| 96 | return $page_key; |
||
| 97 | } |
||
|
0 ignored issues
–
show
|
|||
| 98 | |||
| 99 | /** |
||
| 100 | * CHange the default layout to a grid layout. |
||
| 101 | * |
||
| 102 | * @param string $default |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function lsx_layout_switcher_options_default( $default = 'grid' ) { |
||
|
0 ignored issues
–
show
|
|||
| 106 | $lsx_search = LSX_Search::get_instance(); |
||
| 107 | $default = 'grid'; |
||
| 108 | if ( isset( $lsx_search->frontend->options['display'][ $lsx_search->frontend->search_prefix . '_grid_list' ] ) && ! empty( $lsx_search->frontend->options['display'][ $lsx_search->frontend->search_prefix . '_grid_list' ] ) ) { |
||
|
0 ignored issues
–
show
|
|||
| 109 | $default = $lsx_search->frontend->options['display'][ $lsx_search->frontend->search_prefix . '_grid_list' ]; |
||
| 110 | } |
||
| 111 | return $default; |
||
| 112 | } |
||
|
0 ignored issues
–
show
|
|||
| 113 | |||
| 114 | /** |
||
| 115 | * Controls the layout for the woocommerce shop page. |
||
| 116 | */ |
||
| 117 | public function woocommerce_layout_class( $output = '' ) { |
||
|
0 ignored issues
–
show
|
|||
| 118 | $default_class = $this->lsx_layout_switcher_options_default(); |
||
| 119 | $selected = $this->get_layout_value_from_cookie( 'product' ); |
||
| 120 | if ( '' !== $selected ) { |
||
|
0 ignored issues
–
show
|
|||
| 121 | $default_class = $selected; |
||
| 122 | } |
||
| 123 | $output = str_replace( 'products', 'products ' . $default_class, $output ); |
||
| 124 | return $output; |
||
| 125 | } |
||
|
0 ignored issues
–
show
|
|||
| 126 | |||
| 127 | /** |
||
| 128 | * Get layout value from cookie |
||
| 129 | * |
||
| 130 | * @since 1.0.0 |
||
| 131 | */ |
||
| 132 | public function get_layout_value_from_cookie( $page_key = 'blog' ) { |
||
|
0 ignored issues
–
show
|
|||
| 133 | $archive_layout = 'grid'; |
||
| 134 | |||
| 135 | if ( isset( $_COOKIE[ 'lsx-' . $page_key . '-layout' ] ) ) { |
||
|
0 ignored issues
–
show
|
|||
| 136 | $archive_layout_from_cookie = sanitize_key( $_COOKIE[ 'lsx-' . $page_key . '-layout' ] ); |
||
| 137 | $archive_layout_from_cookie = $this->sanitize_select_layout_switcher( $archive_layout_from_cookie ); |
||
| 138 | |||
| 139 | if ( ! empty( $archive_layout_from_cookie ) ) { |
||
|
0 ignored issues
–
show
|
|||
| 140 | $archive_layout = $archive_layout_from_cookie; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | return $archive_layout; |
||
| 144 | } |
||
|
0 ignored issues
–
show
|
|||
| 145 | |||
| 146 | /** |
||
| 147 | * Sanitize select (layout switcher). |
||
| 148 | * |
||
| 149 | * @since 1.0.0 |
||
| 150 | */ |
||
| 151 | public function sanitize_select_layout_switcher( $input ) { |
||
|
0 ignored issues
–
show
|
|||
| 152 | $valid = array( |
||
| 153 | 'list' => esc_html__( 'List', 'lsx-search' ), |
||
|
0 ignored issues
–
show
|
|||
| 154 | 'grid' => esc_html__( 'Grid', 'lsx-search' ), |
||
|
0 ignored issues
–
show
|
|||
| 155 | ); |
||
| 156 | |||
| 157 | if ( array_key_exists( $input, $valid ) ) { |
||
|
0 ignored issues
–
show
|
|||
| 158 | return $input; |
||
| 159 | } else { |
||
| 160 | return ''; |
||
| 161 | } |
||
| 162 | } |
||
|
0 ignored issues
–
show
|
|||
| 163 | } |
||
| 164 |