| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPPIZZA_MODULE_SETTINGS_PERMALINKS Class |
| 4 |
* |
| 5 |
* @package WPPIZZA |
| 6 |
* @subpackage WPPIZZA_MODULE_SETTINGS_PERMALINKS |
| 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_PERMALINKS{ |
| 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 = 'permalinks';/* 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'), 70, 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 |
/** rewrite single item permalinks**/ |
| 49 |
add_filter('wppizza_filter_cpt_args',array( $this, 'rewrite_single_item_permalink')); |
| 50 |
/* rewrite taxonomy parent page */ |
| 51 |
add_filter('wppizza_filter_ctx_args', array( $this, 'taxonomy_parent_page')); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
/******************************************************************************************************************************************************* |
| 56 |
* |
| 57 |
* |
| 58 |
* |
| 59 |
* [frontend filters] |
| 60 |
* |
| 61 |
* |
| 62 |
* |
| 63 |
********************************************************************************************************************************************************/ |
| 64 |
/************************************* |
| 65 |
|
| 66 |
rewrite_single_item_permalink |
| 67 |
|
| 68 |
**************************************/ |
| 69 |
function rewrite_single_item_permalink($args){ |
| 70 |
global $wppizza_options; |
| 71 |
|
| 72 |
if(!empty($wppizza_options[$this->settings_page]['single_item_permalink_rewrite'])){ |
| 73 |
/**change single item post slug from wppizza to selected slug**/ |
| 74 |
/* Translators: 1: %s slug as set for permalinks. */ |
| 75 |
$args['rewrite'] = array( 'slug' => sprintf( __( '%s', 'wppizza-admin'), $wppizza_options[$this->settings_page]['single_item_permalink_rewrite'] ) ); |
| 76 |
} |
| 77 |
return $args; |
| 78 |
} |
| 79 |
|
| 80 |
/************************************* |
| 81 |
|
| 82 |
category_parent_page |
| 83 |
|
| 84 |
**************************************/ |
| 85 |
function taxonomy_parent_page($args){ |
| 86 |
global $wppizza_options; |
| 87 |
if(!empty($wppizza_options[$this->settings_page]['category_parent_page'])){ |
| 88 |
/**get the right one when using wpml**/ |
| 89 |
if(function_exists('icl_object_id') && $options!=0) { |
| 90 |
$wppizza_options[$this->settings_page]['category_parent_page'] = icl_object_id($wppizza_options[$this->settings_page]['category_parent_page'],'page'); |
| 91 |
} |
| 92 |
$set_category_parent = get_post($wppizza_options[$this->settings_page]['category_parent_page'],ARRAY_A);/*orig*/ |
| 93 |
/* rewrite slug if not still empty */ |
| 94 |
if(!empty($set_category_parent['post_name'])){ |
| 95 |
$args['rewrite']['slug'] = $set_category_parent['post_name']; |
| 96 |
} |
| 97 |
} |
| 98 |
return $args; |
| 99 |
} |
| 100 |
/******************************************************************************************************************************************************* |
| 101 |
* |
| 102 |
* |
| 103 |
* |
| 104 |
* [add admin page options] |
| 105 |
* |
| 106 |
* |
| 107 |
* |
| 108 |
********************************************************************************************************************************************************/ |
| 109 |
|
| 110 |
/*------------------------------------------------------------------------------ |
| 111 |
# |
| 112 |
# |
| 113 |
# [settings page] |
| 114 |
# |
| 115 |
# |
| 116 |
------------------------------------------------------------------------------*/ |
| 117 |
|
| 118 |
/*------------------------------------------------------------------------------ |
| 119 |
# [settings section - setting page] |
| 120 |
# @since 3.0 |
| 121 |
# @return array() |
| 122 |
------------------------------------------------------------------------------*/ |
| 123 |
function admin_options_settings($settings, $sections, $fields, $inputs, $help){ |
| 124 |
|
| 125 |
/*section*/ |
| 126 |
if($sections){ |
| 127 |
$settings['sections'][$this->section_key] = __('Permalinks', 'wppizza-admin'); |
| 128 |
} |
| 129 |
/*help*/ |
| 130 |
if($help){ |
| 131 |
$settings['help'][$this->section_key][] = array( |
| 132 |
'label'=>__('Permalinks', 'wppizza-admin'), |
| 133 |
'description'=>array( |
| 134 |
__('Please adjust settings as appropriate according to the information provided next to each individual option.', 'wppizza-admin'), |
| 135 |
'<span class="wppizza-highlight-important">'.__('Note: Settings here are only applicable if *not* using plain permalink settings. When changing any of the permalink options, you MUST re-save your permalink settings in Wordpress Settings -> Permalinks', 'wppizza-admin').'<span>' |
| 136 |
) |
| 137 |
); |
| 138 |
} |
| 139 |
/*fields*/ |
| 140 |
if($fields){ |
| 141 |
$field = 'category_parent_page'; |
| 142 |
$settings['fields'][$this->section_key][$field] = array( __('Categories/Pages', 'wppizza-admin'), array( |
| 143 |
'value_key'=>$field, |
| 144 |
'option_key'=>$this->settings_page, |
| 145 |
'label'=>'', |
| 146 |
'description'=>array( |
| 147 |
__('Only used and relevant when using widget or shortcode to display wppizza category navigation !!!','wppizza-admin'), |
| 148 |
__('Page cannot be used as static post page (wp settings) or have any children', 'wppizza-admin') |
| 149 |
) |
| 150 |
)); |
| 151 |
$field = 'single_item_permalink_rewrite'; |
| 152 |
$settings['fields'][$this->section_key][$field] = array( __('Single Menu Items', 'wppizza-admin'), array( |
| 153 |
'value_key'=>$field, |
| 154 |
'option_key'=>$this->settings_page, |
| 155 |
'label'=>'', |
| 156 |
'description'=>array( |
| 157 |
__('Only used and relevant when actually linking to a single item from anywhere.', 'wppizza-admin'), |
| 158 |
__('Defaults to "wppizza" if left empty. Any value used here can not be used in by any other custom post type.', 'wppizza-admin'), |
| 159 |
/* Translators: 1: URL to applicable documentation */ |
| 160 |
sprintf(__('Note: by default, wppizza templates/shortcodes do not link to any single menu items. However, if you are including menu items in search results for example or have edited a/the template(s) to include links to individual menu items you will also (probably) want to edit the single item template. see <a href="%1$s" target="_blank">%1$s</a>', 'wppizza-admin'), 'https://docs.wp-pizza.com/developers/?section=wppizza-markup-single-single-php') |
| 161 |
) |
| 162 |
)); |
| 163 |
} |
| 164 |
|
| 165 |
return $settings; |
| 166 |
} |
| 167 |
/*------------------------------------------------------------------------------ |
| 168 |
# [output option fields - setting page] |
| 169 |
# @since 3.0 |
| 170 |
# @return array() |
| 171 |
------------------------------------------------------------------------------*/ |
| 172 |
function admin_options_fields_settings($wppizza_options, $options_key, $field, $label, $description){ |
| 173 |
|
| 174 |
if($field=='category_parent_page'){ |
| 175 |
/**check which pages have children (so we can exclude from dropdown as otherwise children pages will not be accessible*/ |
| 176 |
$exclude=array(); |
| 177 |
foreach(wppizza_get_wordpress_pages() as $k=>$v){ |
| 178 |
$children = get_pages('child_of='.$v->ID); |
| 179 |
if( count( $children ) != 0 ) {$exclude[]=$v->ID;} |
| 180 |
} |
| 181 |
$exclude[]=get_option('page_for_posts');/*also exclude page thats set for default posts*/ |
| 182 |
|
| 183 |
echo "<label>"; |
| 184 |
echo "<select name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' />"; |
| 185 |
echo"<option value=''>".__('no parent [default]', 'wppizza-admin')."</option>"; |
| 186 |
foreach(wppizza_get_wordpress_pages() as $k=>$v){ |
| 187 |
if(in_array($v->ID,$exclude)){ |
| 188 |
echo"<option value='' style='color:red'>".$v->post_title." ".__('[not selectable]', 'wppizza-admin')."</option>"; |
| 189 |
}else{ |
| 190 |
if($wppizza_options[$options_key][$field]==$v->ID){$sel=' selected="selected"';}else{$sel='';} |
| 191 |
echo"<option value='".$v->ID."' ".$sel.">".$v->post_title."</option>"; |
| 192 |
} |
| 193 |
} |
| 194 |
echo "</select>"; |
| 195 |
echo "" . $label . ""; |
| 196 |
echo "</label>"; |
| 197 |
echo"".$description.""; |
| 198 |
echo"<br /><span class='wppizza-highlight'>".__('When changing this setting, you MUST re-save your permalink settings', 'wppizza-admin')."</span>"; |
| 199 |
} |
| 200 |
|
| 201 |
if($field=='single_item_permalink_rewrite'){ |
| 202 |
echo "<label>"; |
| 203 |
echo "<input name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' size='20' type='text' value='".$wppizza_options[$options_key][$field]."' />"; |
| 204 |
echo "" . $label . ""; |
| 205 |
echo "</label>"; |
| 206 |
echo"".$description.""; |
| 207 |
echo"<br /><span class='wppizza-highlight'>".__('When changing this setting, you MUST re-save your permalink settings', 'wppizza-admin')."</span>"; |
| 208 |
} |
| 209 |
} |
| 210 |
/*------------------------------------------------------------------------------ |
| 211 |
# [insert default option on install] |
| 212 |
# $parameter $options array() | filter passing on filtered options |
| 213 |
# @since 3.0 |
| 214 |
# @return array() |
| 215 |
------------------------------------------------------------------------------*/ |
| 216 |
function options_default($options){ |
| 217 |
$options[$this->settings_page]['category_parent_page'] = ''; |
| 218 |
$options[$this->settings_page]['single_item_permalink_rewrite'] = ''; |
| 219 |
return $options; |
| 220 |
} |
| 221 |
|
| 222 |
/*------------------------------------------------------------------------------ |
| 223 |
# [validate options on save/update] |
| 224 |
# |
| 225 |
# @since 3.0 |
| 226 |
# @return array() |
| 227 |
------------------------------------------------------------------------------*/ |
| 228 |
function options_validate($options, $input){ |
| 229 |
/**make sure we get the full array on install/update**/ |
| 230 |
if ( empty( $_POST['_wp_http_referer'] ) ) { |
| 231 |
return $input; |
| 232 |
} |
| 233 |
/******************************** |
| 234 |
* [validate] |
| 235 |
********************************/ |
| 236 |
if(isset($_POST[''.WPPIZZA_SLUG.'_'.$this->settings_page.''])){ |
| 237 |
$options[$this->settings_page]['category_parent_page'] = !empty($input[$this->settings_page]['category_parent_page']) ? (int)$input[$this->settings_page]['category_parent_page'] : ''; |
| 238 |
$options[$this->settings_page]['single_item_permalink_rewrite'] = sanitize_title($input[$this->settings_page]['single_item_permalink_rewrite']); |
| 239 |
} |
| 240 |
return $options; |
| 241 |
} |
| 242 |
} |
| 243 |
/*************************************************************** |
| 244 |
* |
| 245 |
* [ini] |
| 246 |
* |
| 247 |
***************************************************************/ |
| 248 |
$WPPIZZA_MODULE_SETTINGS_PERMALINKS = new WPPIZZA_MODULE_SETTINGS_PERMALINKS(); |
| 249 |
?> |