gravityview /
GravityView
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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') |
||
| 15 | ); |
||
| 16 | |||
| 17 | $widget_display = array( |
||
| 18 | 'width' => 650 |
||
| 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' ) ) { |
||
| 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' |
||
| 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 ); |
||
| 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']; |
||
| 67 | |||
| 68 | if ( $title ) { |
||
| 69 | echo $args['before_title'] . $title . $args['after_title']; |
||
| 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']; |
||
| 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; |
||
| 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>' ); |
||
| 139 | |||
| 140 | echo wpautop( GravityView_Admin::get_floaty() . $warning ); |
||
| 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_Admin::no_views_text(); ?></div> |
||
|
0 ignored issues
–
show
introduced
by
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> |
||
| 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> |
||
| 176 | </div> |
||
| 177 | <?php |
||
| 178 | unset ( $error ); |
||
| 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"> |
||
| 185 | <option value=""><?php esc_html_e( '— Select a View —', 'gravityview' ); ?></option> |
||
| 186 | <?php |
||
| 187 | foreach( $views as $view_option ) { |
||
| 188 | $title = empty( $view_option->post_title ) ? __('(no title)', 'gravityview') : $view_option->post_title; |
||
| 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>'; |
||
| 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'] ) ) { |
||
| 202 | ?> |
||
| 203 | <div class="error inline"> |
||
| 204 | <p><?php echo $instance['error_post_id']; ?></p> |
||
| 205 | </div> |
||
| 206 | <?php |
||
| 207 | unset ( $error ); |
||
| 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> |
||
| 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 ); ?>" /> |
||
| 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' ); |
||
| 216 | echo ' '.gravityview_get_link('http://docs.gravityview.co/article/222-the-search-widget', __('Learn more…', 'gravityview' ), 'target=_blank' ); |
||
| 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> |
||
| 222 | <input name="<?php echo $this->get_field_name('search_clear'); ?>" type="hidden" value="0"> |
||
| 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 ); ?>> |
||
| 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"> |
||
| 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 ); ?>> |
||
| 230 | <?php esc_html_e( 'Match Any Fields', 'gravityview' ); ?> |
||
| 231 | </label> |
||
| 232 | <label for="<?php echo $this->get_field_id('search_mode'); ?>_all"> |
||
| 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 ); ?>> |
||
| 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 |
||
| 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> |
||
| 244 | <div class="gv-widget-search-fields" title="<?php esc_html_e('Search Fields', 'gravityview'); ?>"> |
||
| 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"> |
||
| 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' ); |
||
| 254 | </script> |
||
| 255 | <?php |
||
| 256 | } |
||
| 257 | |||
| 258 | } |