| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPPIZZA_MODULE_SETTINGS_SEARCH Class |
| 4 |
* |
| 5 |
* @package WPPIZZA |
| 6 |
* @subpackage WPPIZZA_MODULE_SETTINGS_SEARCH |
| 7 |
* @copyright Copyright (c) 2015, Oliver Bach |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 3.0 |
| 10 |
* |
| 11 |
*/ |
| 12 |
if ( ! defined( 'ABSPATH' ) ) exit;/*Exit if accessed directly*/ |
| 13 |
|
| 14 |
|
| 15 |
/************************************************************************************************************************ |
| 16 |
* |
| 17 |
* |
| 18 |
* |
| 19 |
* |
| 20 |
* |
| 21 |
* |
| 22 |
************************************************************************************************************************/ |
| 23 |
class WPPIZZA_MODULE_SETTINGS_SEARCH{ |
| 24 |
|
| 25 |
private $settings_page = 'settings';/* which admin subpage (identified there by this->class_key) are we adding this to */ |
| 26 |
|
| 27 |
|
| 28 |
private $section_key = 'search';/* must be unique */ |
| 29 |
|
| 30 |
|
| 31 |
function __construct() { |
| 32 |
/********************************************************** |
| 33 |
[add settings to admin] |
| 34 |
***********************************************************/ |
| 35 |
if(is_admin()){ |
| 36 |
/* add admin options settings page*/ |
| 37 |
add_filter('wppizza_filter_settings_sections_'.$this->settings_page.'', array($this, 'admin_options_settings'), 80, 5); |
| 38 |
/* add admin options settings page fields */ |
| 39 |
add_action('wppizza_admin_settings_section_fields_'.$this->settings_page.'', array($this, 'admin_options_fields_settings'), 10, 5); |
| 40 |
/**add default options **/ |
| 41 |
add_filter('wppizza_filter_setup_default_options', array( $this, 'options_default')); |
| 42 |
/**validate options**/ |
| 43 |
add_filter('wppizza_filter_options_validate', array( $this, 'options_validate'), 10, 2 ); |
| 44 |
} |
| 45 |
/********************************************************** |
| 46 |
[filter/actions depending on settings] |
| 47 |
***********************************************************/ |
| 48 |
/*** |
| 49 |
alter search query if/when required to filter search to include wppizza etc |
| 50 |
frontend only ! or else the admin search in pst types gets messes up |
| 51 |
**/ |
| 52 |
if(!is_admin()){ |
| 53 |
add_filter( 'pre_get_posts', array( $this, 'set_search_query' )); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/******************************************************************************************************************************************************* |
| 58 |
* |
| 59 |
* |
| 60 |
* |
| 61 |
* [frontend filters] |
| 62 |
* |
| 63 |
* |
| 64 |
* |
| 65 |
********************************************************************************************************************************************************/ |
| 66 |
/******************************************************* |
| 67 |
[set search box queries ] |
| 68 |
******************************************************/ |
| 69 |
function set_search_query( $query ) { |
| 70 |
if (is_search() && $query->is_main_query()) { |
| 71 |
|
| 72 |
global $wppizza_options; |
| 73 |
/******************************************************************* |
| 74 |
exclude wppizza cpt from search results if not enabled |
| 75 |
|
| 76 |
furthermore, if no post_type var has been set in query, set all other used ones |
| 77 |
if they HAVE been set, we should not need to do anything |
| 78 |
as the wppizza cpt will be specifically set/added or not |
| 79 |
*******************************************************************/ |
| 80 |
if(!$wppizza_options[$this->settings_page]['search_include'] && !isset($_REQUEST['post_type'])){ |
| 81 |
if(!isset($query->query_vars['post_type']) ){ |
| 82 |
/**get all queryable and exclude/unset wppizza***/ |
| 83 |
$post_types = get_post_types( array('public' => true,'exclude_from_search' => false), 'names' ); |
| 84 |
unset($post_types[WPPIZZA_POST_TYPE]); |
| 85 |
$query->set('post_type',$post_types); |
| 86 |
} |
| 87 |
} |
| 88 |
/**post types set when using shortcodes/widget etc**/ |
| 89 |
if(isset($_REQUEST['post_type'])){ |
| 90 |
$request_types=explode(",",$_REQUEST['post_type']); |
| 91 |
/**if we have set another permalink for single mnu items, rewrite this here so the query finds wppizza after all**/ |
| 92 |
if(isset($wppizza_options[$this->settings_page]['single_item_permalink_rewrite']) && $wppizza_options[$this->settings_page]['single_item_permalink_rewrite']!='' && in_array($wppizza_options[$this->settings_page]['single_item_permalink_rewrite'],$request_types)){ |
| 93 |
$key = array_search($wppizza_options[$this->settings_page]['single_item_permalink_rewrite'], $request_types); |
| 94 |
$request_types[$key]=WPPIZZA_POST_TYPE; |
| 95 |
} |
| 96 |
/**get all queryable and get intersection just to be tidy and stop people from entering random query vars***/ |
| 97 |
$post_types = get_post_types( array('public' => true,'exclude_from_search' => false), 'names' ); |
| 98 |
$post_types_array = array_intersect($request_types, $post_types); |
| 99 |
$query->set( 'post_type', $post_types_array); |
| 100 |
|
| 101 |
|
| 102 |
$query = apply_filters('wppizza_filter_search', $query); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
return $query; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
/******************************************************************************************************************************************************* |
| 113 |
* |
| 114 |
* |
| 115 |
* |
| 116 |
* [add admin page options] |
| 117 |
* |
| 118 |
* |
| 119 |
* |
| 120 |
********************************************************************************************************************************************************/ |
| 121 |
|
| 122 |
/*------------------------------------------------------------------------------ |
| 123 |
# |
| 124 |
# |
| 125 |
# [settings page] |
| 126 |
# |
| 127 |
# |
| 128 |
------------------------------------------------------------------------------*/ |
| 129 |
|
| 130 |
/*------------------------------------------------------------------------------ |
| 131 |
# [settings section - setting page] |
| 132 |
# @since 3.0 |
| 133 |
# @return array() |
| 134 |
------------------------------------------------------------------------------*/ |
| 135 |
function admin_options_settings($settings, $sections, $fields, $inputs, $help){ |
| 136 |
|
| 137 |
/*section*/ |
| 138 |
if($sections){ |
| 139 |
$settings['sections'][$this->section_key] = __('Search', 'wppizza-admin'); |
| 140 |
} |
| 141 |
/*help*/ |
| 142 |
if($help){ |
| 143 |
$settings['help'][$this->section_key][] = array( |
| 144 |
'label'=>__('Search', 'wppizza-admin'), |
| 145 |
'description'=>array( |
| 146 |
__('Please adjust settings as appropriate according to the information provided next to each individual option.', 'wppizza-admin') |
| 147 |
) |
| 148 |
); |
| 149 |
} |
| 150 |
/*fields*/ |
| 151 |
if($fields){ |
| 152 |
$field = 'search_include'; |
| 153 |
$settings['fields'][$this->section_key][$field] = array( __('Include wppizza menu items in regular search results', 'wppizza-admin'), array( |
| 154 |
'value_key'=>$field, |
| 155 |
'option_key'=>$this->settings_page, |
| 156 |
'label'=>__('you could also leave this off and use the wppizza widget/shortcode for a dedicated search box', 'wppizza-admin'), |
| 157 |
'description'=>array() |
| 158 |
)); |
| 159 |
} |
| 160 |
|
| 161 |
return $settings; |
| 162 |
} |
| 163 |
/*------------------------------------------------------------------------------ |
| 164 |
# [output option fields - setting page] |
| 165 |
# @since 3.0 |
| 166 |
# @return array() |
| 167 |
------------------------------------------------------------------------------*/ |
| 168 |
function admin_options_fields_settings($wppizza_options, $options_key, $field, $label, $description){ |
| 169 |
if($field=='search_include'){ |
| 170 |
echo "<label>"; |
| 171 |
echo "<input id='".$field."' name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' type='checkbox' ". checked($wppizza_options[$options_key][$field],true,false)." value='1' />"; |
| 172 |
echo "" . $label . ""; |
| 173 |
echo "</label>"; |
| 174 |
echo"".$description.""; |
| 175 |
} |
| 176 |
} |
| 177 |
/*------------------------------------------------------------------------------ |
| 178 |
# [insert default option on install] |
| 179 |
# $parameter $options array() | filter passing on filtered options |
| 180 |
# @since 3.0 |
| 181 |
# @return array() |
| 182 |
------------------------------------------------------------------------------*/ |
| 183 |
function options_default($options){ |
| 184 |
|
| 185 |
$options[$this->settings_page]['search_include'] = false; |
| 186 |
|
| 187 |
return $options; |
| 188 |
} |
| 189 |
|
| 190 |
/*------------------------------------------------------------------------------ |
| 191 |
# [validate options on save/update] |
| 192 |
# |
| 193 |
# @since 3.0 |
| 194 |
# @return array() |
| 195 |
------------------------------------------------------------------------------*/ |
| 196 |
function options_validate($options, $input){ |
| 197 |
/**make sure we get the full array on install/update**/ |
| 198 |
if ( empty( $_POST['_wp_http_referer'] ) ) { |
| 199 |
return $input; |
| 200 |
} |
| 201 |
/******************************** |
| 202 |
* [validate] |
| 203 |
********************************/ |
| 204 |
if(isset($_POST[''.WPPIZZA_SLUG.'_'.$this->settings_page.''])){ |
| 205 |
$options[$this->settings_page]['search_include'] = !empty($input[$this->settings_page]['search_include']) ? true : false; |
| 206 |
} |
| 207 |
return $options; |
| 208 |
} |
| 209 |
} |
| 210 |
/*************************************************************** |
| 211 |
* |
| 212 |
* [ini] |
| 213 |
* |
| 214 |
***************************************************************/ |
| 215 |
$WPPIZZA_MODULE_SETTINGS_SEARCH = new WPPIZZA_MODULE_SETTINGS_SEARCH(); |
| 216 |
?> |