Issues (823)

classes/frontend/class-layout.php (43 issues)

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
Expected 2 blank lines before function; 1 found
Loading history...
26
		add_action( 'wp', array( $this, 'load_functions' ), 24 );
27
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
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
Method name "Layout::get_instance" is not in camel caps format
Loading history...
37
		// If the single instance hasn't been set, set it now.
38
		if ( null == self::$instance ) {
0 ignored issues
show
Found: ==. Use strict comparisons (=== or !==).
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
39
			self::$instance = new self();
40
		}
41
		return self::$instance;
42
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
43
44
	/**
45
	 * Check all settings.
46
	 */
47
	public function load_functions() {
0 ignored issues
show
Method name "Layout::load_functions" is not in camel caps format
Loading history...
48
		$lsx_search = LSX_Search::get_instance();
49
		if ( $lsx_search->frontend->search_enabled ) {
0 ignored issues
show
Expected 0 spaces before closing bracket; 1 found
Loading history...
50
			if ( isset( $lsx_search->frontend->options['display'][ $lsx_search->frontend->search_prefix . '_layout_switcher_enable' ] ) ) {
0 ignored issues
show
Expected 0 spaces before closing bracket; 1 found
Loading history...
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
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
61
62
	/**
63
	 * Display the woocommerce archive swticher.
64
	 */
65
	public function show_layout_switcher( $show = false ) {
0 ignored issues
show
Method name "Layout::show_layout_switcher" is not in camel caps format
Loading history...
66
		if ( is_search() ) {
0 ignored issues
show
Expected 0 spaces before closing bracket; 1 found
Loading history...
67
			$archive_layout_switcher = get_theme_mod( 'lsx_blog_customizer_archive_layout_switcher', false );
68
			if ( true === $archive_layout_switcher ) {
0 ignored issues
show
Expected 0 spaces before closing bracket; 1 found
Loading history...
69
				$show = true;
70
			}
71
		}
72
		return $show;
73
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
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
Method name "Layout::lsx_layout_switcher_options" is not in camel caps format
Loading history...
82
		unset( $layout_options['default'] );
83
		unset( $layout_options['half-grid'] );
84
		return $layout_options;
85
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
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
Method name "Layout::lsx_layout_switcher_page_key" is not in camel caps format
Loading history...
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
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
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
Method name "Layout::lsx_layout_switcher_options_default" is not in camel caps format
Loading history...
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
Expected 0 spaces before closing bracket; 1 found
Loading history...
109
			$default = $lsx_search->frontend->options['display'][ $lsx_search->frontend->search_prefix . '_grid_list' ];
110
		}
111
		return $default;
112
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
113
114
	/**
115
	 * Controls the layout for the woocommerce shop page.
116
	 */
117
	public function woocommerce_layout_class( $output = '' ) {
0 ignored issues
show
Method name "Layout::woocommerce_layout_class" is not in camel caps format
Loading history...
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
Expected 0 spaces before closing bracket; 1 found
Loading history...
121
			$default_class = $selected;
122
		}
123
		$output = str_replace( 'products', 'products ' . $default_class, $output );
124
		return $output;
125
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
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
Method name "Layout::get_layout_value_from_cookie" is not in camel caps format
Loading history...
133
		$archive_layout = 'grid';
134
135
		if ( isset( $_COOKIE[ 'lsx-' . $page_key . '-layout' ] ) ) {
0 ignored issues
show
Expected 0 spaces before closing bracket; 1 found
Loading history...
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
Expected 0 spaces before closing bracket; 1 found
Loading history...
140
				$archive_layout = $archive_layout_from_cookie;
141
			}
142
		}
143
		return $archive_layout;
144
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
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
Method name "Layout::sanitize_select_layout_switcher" is not in camel caps format
Loading history...
152
		$valid = array(
153
			'list'      => esc_html__( 'List', 'lsx-search' ),
0 ignored issues
show
Array double arrow not aligned correctly; expected 1 space(s) between "'list'" and double arrow, but found 6.
Loading history...
154
			'grid'      => esc_html__( 'Grid', 'lsx-search' ),
0 ignored issues
show
Array double arrow not aligned correctly; expected 1 space(s) between "'grid'" and double arrow, but found 6.
Loading history...
155
		);
156
157
		if ( array_key_exists( $input, $valid ) ) {
0 ignored issues
show
Expected 0 spaces before closing bracket; 1 found
Loading history...
158
			return $input;
159
		} else {
160
			return '';
161
		}
162
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 0 found
Loading history...
163
}
164