Completed
Pull Request — master (#664)
by Zack
07:00 queued 03:02
created

GravityView_Search_WP_Widget::get_defaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
4
/**
5
 * Search widget class
6
 * @since 1.6
7
 */
8
class GravityView_Search_WP_Widget extends WP_Widget {
9
10
	public function __construct() {
11
12
		$widget_ops = array(
13
			'classname' => 'widget_gravityview_search',
14
			'description' => __( 'A search form for a specific GravityView.', 'gravityview')
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
15
		);
16
17
		$widget_display = array(
18
			'width' => 650
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
19
		);
20
21
		parent::__construct( 'gravityview_search', __( 'GravityView Search', 'gravityview' ), $widget_ops, $widget_display );
22
23
		$this->load_required_files();
24
25
		$gravityview_widget = GravityView_Widget_Search::getInstance();
26
27
		// frontend - filter entries
28
		add_filter( 'gravityview_fe_search_criteria', array( $gravityview_widget, 'filter_entries' ), 10, 1 );
29
30
		// frontend - add template path
31
		add_filter( 'gravityview_template_paths', array( $gravityview_widget, 'add_template_path' ) );
32
33
		unset( $gravityview_widget );
34
	}
35
36
	private function load_required_files() {
37
		if( !class_exists( 'GravityView_Widget_Search' ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
38
			gravityview_register_gravityview_widgets();
39
		}
40
	}
41
42
	private static function get_defaults() {
43
		return array(
44
			'title' => '',
45
			'view_id' => 0,
46
			'post_id' => '',
47
			'search_fields' => '',
48
			'search_clear' => 0,
49
			'search_mode' => 'any'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
50
		);
51
	}
52
53
	public function widget( $args, $instance ) {
54
55
		// Don't show unless a View ID has been set.
56
		if( empty( $instance['view_id'] ) ) {
57
58
			do_action('gravityview_log_debug', sprintf( '%s[widget]: No View ID has been defined. Not showing the widget.', get_class($this)), $instance );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
59
60
			return;
61
		}
62
63
		/** This filter is documented in wp-includes/default-widgets.php */
64
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
65
66
		echo $args['before_widget'];
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$args'
Loading history...
67
68
		if ( $title ) {
69
			echo $args['before_title'] . $title . $args['after_title'];
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$args'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$title'
Loading history...
70
		}
71
72
		// @todo Add to the widget configuration form
73
		$instance['search_layout'] = apply_filters( 'gravityview/widget/search/layout', 'vertical', $instance );
74
75
		$instance['context'] = 'wp_widget';
76
77
		// form
78
		$instance['form_id'] = GVCommon::get_meta_form_id( $instance['view_id'] );
79
		$instance['form'] = GVCommon::get_form( $instance['form_id'] );
80
81
		// We don't want to overwrite existing context, etc.
82
		$previous_view = GravityView_View::getInstance();
83
84
		/** @hack */
85
		new GravityView_View( $instance );
86
87
		GravityView_Widget_Search::getInstance()->render_frontend( $instance );
88
89
		/**
90
		 * Restore previous View context
91
		 * @hack
92
		 */
93
		new GravityView_View( $previous_view );
94
95
		echo $args['after_widget'];
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$args'
Loading history...
96
	}
97
98
	/**
99
	 * @inheritDoc
100
	 */
101
	public function update( $new_instance, $old_instance ) {
102
103
		$instance = $old_instance;
104
105
		if( $this->is_preview() ) {
106
			//Oh! Sorry but still not fully compatible with customizer
107
			return $instance;
108
		}
109
110
		$new_instance = wp_parse_args( (array) $new_instance, self::get_defaults() );
111
112
		$instance['title'] = strip_tags( $new_instance['title'] );
113
		$instance['view_id'] = absint( $new_instance['view_id'] );
114
		$instance['search_fields'] = $new_instance['search_fields'];
115
		$instance['post_id'] = $new_instance['post_id'];
116
		$instance['search_clear'] = $new_instance['search_clear'];
117
		$instance['search_mode'] = $new_instance['search_mode'];
118
119
		$is_valid_embed_id = GravityView_View_Data::is_valid_embed_id( $new_instance['post_id'], $instance['view_id'] );
120
121
		//check if post_id is a valid post with embedded View
122
		$instance['error_post_id'] = is_wp_error( $is_valid_embed_id ) ? $is_valid_embed_id->get_error_message() : NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
123
124
		// Share that the widget isn't brand new
125
		$instance['updated']  = 1;
126
127
		return $instance;
128
	}
129
130
	/**
131
	 * @inheritDoc
132
	 */
133
	public function form( $instance ) {
134
135
		// @todo Make compatible with Customizer
136
		if( $this->is_preview() ) {
137
138
			$warning = sprintf( esc_html__( 'This widget is not configurable from this screen. Please configure it on the %sWidgets page%s.', 'gravityview' ), '<a href="'.admin_url('widgets.php').'">', '</a>' );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
139
140
			echo wpautop( GravityView_Admin::get_floaty() . $warning );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'wpautop'
Loading history...
141
142
			return;
143
		}
144
145
		$instance = wp_parse_args( (array) $instance, self::get_defaults() );
146
147
		$title    = $instance['title'];
148
		$view_id  = $instance['view_id'];
149
		$post_id  = $instance['post_id'];
150
		$search_fields = $instance['search_fields'];
151
		$search_clear = $instance['search_clear'];
152
		$search_mode = $instance['search_mode'];
153
154
		$views = GVCommon::get_all_views();
155
156
		// If there are no views set up yet, we get outta here.
157
		if( empty( $views ) ) { ?>
158
			<div id="select_gravityview_view">
159
				<div class="wrap"><?php echo GravityView_Post_Types::no_views_text(); ?></div>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'GravityView_Post_Types'
Loading history...
160
			</div>
161
			<?php return;
162
		}
163
		?>
164
165
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'gravityview'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
166
167
		<?php
168
		/**
169
		 * Display errors generated for invalid embed IDs
170
		 * @see GravityView_View_Data::is_valid_embed_id
171
		 */
172
		if( isset( $instance['updated'] ) && empty( $instance['view_id'] ) ) {
173
			?>
174
			<div class="error inline hide-on-view-change">
175
				<p><?php esc_html_e('Please select a View to search.', 'gravityview'); ?></p>
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
176
			</div>
177
			<?php
178
			unset ( $error );
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
179
		}
180
		?>
181
182
		<p>
183
			<label for="gravityview_view_id"><?php _e( 'View:', 'gravityview' ); ?></label>
184
			<select id="gravityview_view_id" name="<?php echo $this->get_field_name('view_id'); ?>" class="widefat">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
185
				<option value=""><?php esc_html_e( '&mdash; Select a View &mdash;', 'gravityview' ); ?></option>
186
				<?php
187
				foreach( $views as $view_option ) {
188
					$title = empty( $view_option->post_title ) ? __('(no title)', 'gravityview') : $view_option->post_title;
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
189
					echo '<option value="'. $view_option->ID .'" ' . selected( esc_attr( $view_id ), $view_option->ID, false ) . '>'. esc_html( sprintf('%s #%d', $title, $view_option->ID ) ) .'</option>';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$view_option'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
190
				}
191
				?>
192
			</select>
193
194
		</p>
195
196
		<?php
197
		/**
198
		 * Display errors generated for invalid embed IDs
199
		 * @see GravityView_View_Data::is_valid_embed_id
200
		 */
201
		if( !empty( $instance['error_post_id'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
202
			?>
203
			<div class="error inline">
204
				<p><?php echo $instance['error_post_id']; ?></p>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$instance'
Loading history...
205
			</div>
206
			<?php
207
			unset ( $error );
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
208
		}
209
		?>
210
211
		<p>
212
			<label for="<?php echo $this->get_field_id('post_id'); ?>"><?php esc_html_e( 'If Embedded, Page ID:', 'gravityview' ); ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
213
			<input class="code" size="3" id="<?php echo $this->get_field_id('post_id'); ?>" name="<?php echo $this->get_field_name('post_id'); ?>" type="text" value="<?php echo esc_attr( $post_id ); ?>" />
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
214
			<span class="howto"><?php
215
				esc_html_e('To have a search performed on an embedded View, enter the ID of the post or page where the View is embedded.', 'gravityview' );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
216
				echo ' '.gravityview_get_link('http://docs.gravityview.co/article/222-the-search-widget', __('Learn more&hellip;', 'gravityview' ), 'target=_blank' );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'gravityview_get_link'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
217
				?></span>
218
		</p>
219
220
		<p>
221
			<label for="<?php echo $this->get_field_id('search_clear'); ?>"><?php esc_html_e( 'Show Clear button', 'gravityview' ); ?>:</label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
222
			<input name="<?php echo $this->get_field_name('search_clear'); ?>" type="hidden" value="0">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
223
			<input id="<?php echo $this->get_field_id('search_clear'); ?>" name="<?php echo $this->get_field_name('search_clear'); ?>" type="checkbox" class="checkbox" value="1" <?php checked( $search_clear, 1, true ); ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
224
		</p>
225
226
		<p>
227
			<label><?php esc_html_e( 'Search Mode', 'gravityview' ); ?>:</label>
228
			<label for="<?php echo $this->get_field_id('search_mode'); ?>_any">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
229
				<input id="<?php echo $this->get_field_id('search_mode'); ?>_any" name="<?php echo $this->get_field_name('search_mode'); ?>" type="radio" class="radio" value="any" <?php checked( $search_mode, 'any', true ); ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
230
				<?php esc_html_e( 'Match Any Fields', 'gravityview' ); ?>
231
			</label>
232
			<label for="<?php echo $this->get_field_id('search_mode'); ?>_all">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
233
				<input id="<?php echo $this->get_field_id('search_mode'); ?>_all" name="<?php echo $this->get_field_name('search_mode'); ?>" type="radio" class="radio" value="all" <?php checked( $search_mode, 'all', true ); ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
234
				<?php esc_html_e( 'Match All Fields', 'gravityview' ); ?>
235
			</label>
236
			<span class="howto"><?php esc_html_e('Should search results match all search fields, or any?', 'gravityview' ); ?></span
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
237
		</p>
238
239
		<hr />
240
241
		<?php // @todo: move style to CSS ?>
242
		<div style="margin-bottom: 1em;">
243
			<label class="screen-reader-text" for="<?php echo $this->get_field_id('search_fields'); ?>"><?php _e( 'Searchable fields:', 'gravityview' ); ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
244
			<div class="gv-widget-search-fields" title="<?php esc_html_e('Search Fields', 'gravityview'); ?>">
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
245
				<input id="<?php echo $this->get_field_id('search_fields'); ?>" name="<?php echo $this->get_field_name('search_fields'); ?>" type="hidden" value="<?php echo esc_attr( $search_fields ); ?>" class="gv-search-fields-value">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
246
			</div>
247
248
		</div>
249
250
		<script>
251
			// When the widget is saved or added, refresh the Merge Tags (here for backward compatibility)
252
			// WordPress 3.9 added widget-added and widget-updated actions
253
			jQuery('#<?php echo $this->get_field_id( 'view_id' ); ?>').trigger( 'change' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
254
		</script>
255
	<?php
256
	}
257
258
}