| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPPIZZA_MODULE_ORDERSETTINGS_REPURCHASE Class |
| 4 |
* |
| 5 |
* @package WPPIZZA |
| 6 |
* @subpackage WPPIZZA_MODULE_ORDERSETTINGS_REPURCHASE |
| 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_ORDERSETTINGS_REPURCHASE{ |
| 24 |
|
| 25 |
private $settings_page = 'order_settings';/* which admin subpage (identified there by this->class_key) are we adding this to */ |
| 26 |
|
| 27 |
private $localization_page = 'localization';/* which admin subpage (identified there by this->class_key) are we adding this to */ |
| 28 |
private $localization_position = 'user_purchase_history';/* at which admin position (section) should this appear on above admin subpage, integer(zero indexed - for numeric position) or key ('layout-style' for example) after which it should appear*/ |
| 29 |
|
| 30 |
private $section_key = 'repurchase';/* must be unique */ |
| 31 |
|
| 32 |
|
| 33 |
function __construct() { |
| 34 |
/********************************************************** |
| 35 |
[add settings to admin] |
| 36 |
***********************************************************/ |
| 37 |
if(is_admin()){ |
| 38 |
/* add admin options settings page*/ |
| 39 |
add_filter('wppizza_filter_settings_sections_'.$this->settings_page.'', array($this, 'admin_options_settings'), 70, 5); |
| 40 |
/* add admin options settings page fields */ |
| 41 |
add_action('wppizza_admin_settings_section_fields_'.$this->settings_page.'', array($this, 'admin_options_fields_settings'), 10, 5); |
| 42 |
/**add default options **/ |
| 43 |
add_filter('wppizza_filter_setup_default_options', array( $this, 'options_default')); |
| 44 |
/**validate options**/ |
| 45 |
add_filter('wppizza_filter_options_validate', array( $this, 'options_validate'), 10, 2 ); |
| 46 |
|
| 47 |
/* add admin options localization page - does not need fields action*/ |
| 48 |
add_filter('wppizza_filter_settings_sections_'.$this->localization_page.'', array($this, 'admin_options_localization'), 100, 5);/* highish priority to be able to splice in the right place*/ |
| 49 |
} |
| 50 |
/********************************************************** |
| 51 |
[filter/actions depending on settings] |
| 52 |
***********************************************************/ |
| 53 |
/** add repurchase buttons (and header) in user purchase history **/ |
| 54 |
add_filter('wppizza_filter_order_item_header_markup',array($this, 'orderhistory_reorder_purchase_item_header'), 10, 3); |
| 55 |
//add_filter('wppizza_filter_order_item_markup',array($this, 'orderhistory_reorder_purchase_item'), 10, 9); |
| 56 |
add_filter('wppizza_filter_order_item_columns',array($this, 'orderhistory_reorder_purchase_item'), 10, 8); |
| 57 |
} |
| 58 |
|
| 59 |
/******************************************************************************************************************************************************* |
| 60 |
* |
| 61 |
* |
| 62 |
* |
| 63 |
* [frontend filters] |
| 64 |
* |
| 65 |
* |
| 66 |
* |
| 67 |
********************************************************************************************************************************************************/ |
| 68 |
/*************************************** |
| 69 |
|
| 70 |
[check if we can reorder an item from user purchase history |
| 71 |
will return false if reordering cannot take place due to items/ids/etc not available |
| 72 |
else an array of parameters per item, if all match also allow reorder of whole order] |
| 73 |
|
| 74 |
***************************************/ |
| 75 |
function orderhistory_reorder_purchase_item_header($markup_header, $txt, $type){ |
| 76 |
global $wppizza_options; |
| 77 |
/** only apply filter in user's order history **/ |
| 78 |
if( $type != 'orderhistory' || empty($wppizza_options[$this->settings_page]['repurchase'])){ |
| 79 |
return $markup_header; |
| 80 |
} |
| 81 |
|
| 82 |
/* add repurchase label/legend to end*/ |
| 83 |
$markup_header['thead_th_reorder']= '<th class="'.WPPIZZA_SLUG.'-item-reorder-th">'.$txt['history_label_reorder_header'].'</th>';/* table cell */ |
| 84 |
|
| 85 |
return $markup_header; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
function orderhistory_reorder_purchase_item($markup, $key , $item, $items, $item_count, $order_id, $txt, $type){ |
| 90 |
global $wppizza_options, $blog_id; |
| 91 |
|
| 92 |
/** only apply filter in user's order history and if enabled**/ |
| 93 |
if($type != 'orderhistory' || empty($wppizza_options[$this->settings_page]['repurchase']) ){return $markup;} |
| 94 |
|
| 95 |
/* to count items we can repurchase */ |
| 96 |
static $can_repurchase = array(); |
| 97 |
|
| 98 |
/* get number of unique items in cart. if we can reorder all individually, add button to enable purchase of the whole order */ |
| 99 |
$unique_items_in_cart = count($items); |
| 100 |
|
| 101 |
/* |
| 102 |
remove filter first of all |
| 103 |
resetting counts for each order |
| 104 |
*/ |
| 105 |
if(empty($can_repurchase[$order_id])){ |
| 106 |
$can_repurchase[$order_id]['item'] = 0; |
| 107 |
$can_repurchase[$order_id]['order'] = 0; |
| 108 |
remove_filter( 'wppizza_filter_order_itemised_markup', array($this, 'orderhistory_reorder_purchase_order') ); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/*************************************** |
| 113 |
check if item still exists as it was |
| 114 |
to allow reordering |
| 115 |
***************************************/ |
| 116 |
/** same blog as current ! */ |
| 117 |
if($item['blog_id'] == $blog_id || !is_multisite()){ |
| 118 |
/* get object */ |
| 119 |
$post = get_post( $item['post_id']); |
| 120 |
$terms = get_the_terms($item['post_id'], WPPIZZA_TAXONOMY); |
| 121 |
$termids_of_item = wppizza_array_column($terms, 'term_id', true); |
| 122 |
|
| 123 |
//print"------------todo perhaps -- get sizes and size key-------------"; |
| 124 |
|
| 125 |
/* check if (still) published , compare title to make sure it's not something completely different now */ |
| 126 |
if ( $post->post_status == 'publish' && wppizza_compare_title($item['title']) == wppizza_compare_title($post->post_title) && isset($termids_of_item[$item['cat_id_selected']])) { |
| 127 |
/* we can reorder this item - filterable */ |
| 128 |
$repurchase_item = apply_filters('wppizza_filter_can_repurchase_item', true, $key, $item, $items ); |
| 129 |
$repurchase_order = apply_filters('wppizza_filter_can_repurchase_order', true, $key, $item, $items ); |
| 130 |
/* increase counter for items enabled fr re-order */ |
| 131 |
if($repurchase_item){ |
| 132 |
$can_repurchase[$order_id]['item']++; |
| 133 |
} |
| 134 |
/* increase counter for items enabled fr re-order */ |
| 135 |
if($repurchase_order){ |
| 136 |
$can_repurchase[$order_id]['order']++; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
/** different blog then current - only if multisite in the first place ?! */ |
| 144 |
if($item['blog_id'] != $blog_id && is_multisite()){ |
| 145 |
switch_to_blog($item['blog_id']); |
| 146 |
/* get object */ |
| 147 |
$post = get_post( $item['post_id']); |
| 148 |
$terms = get_the_terms($item['post_id'], WPPIZZA_TAXONOMY); |
| 149 |
$termids_of_item = wppizza_array_column($terms, 'term_id', true); |
| 150 |
|
| 151 |
//print"------------todo perhaps -- get sizes and size key-------------"; |
| 152 |
|
| 153 |
/* check if (still) published , compare title to make sure it's not something completely different now */ |
| 154 |
if ( $post->post_status == 'publish' && wppizza_compare_title($item['title']) == wppizza_compare_title($post->post_title) && isset($termids_of_item[$item['cat_id_selected']])) { |
| 155 |
/* we can reorder this item - filterable */ |
| 156 |
$repurchase_item = apply_filters('wppizza_filter_can_repurchase_item', true, $key, $item, $items ); |
| 157 |
$repurchase_order = apply_filters('wppizza_filter_can_repurchase_order', true, $key, $item, $items ); |
| 158 |
/* increase counter for items enabled fr re-order */ |
| 159 |
if($repurchase_item){ |
| 160 |
$can_repurchase[$order_id]['item']++; |
| 161 |
} |
| 162 |
/* increase counter for items enabled fr re-order */ |
| 163 |
if($repurchase_order){ |
| 164 |
$can_repurchase[$order_id]['order']++; |
| 165 |
} |
| 166 |
} |
| 167 |
/**restore current**/ |
| 168 |
restore_current_blog(); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
/* |
| 176 |
add repurchase button for entire order |
| 177 |
if all items can be reordered |
| 178 |
*/ |
| 179 |
if($can_repurchase[$order_id]['order'] == $unique_items_in_cart){ |
| 180 |
add_filter('wppizza_filter_order_itemised_markup', array($this, 'orderhistory_reorder_purchase_order'), 10, 7); |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
cannot reorder, insert n/a |
| 185 |
*/ |
| 186 |
if(empty($repurchase_item)){ |
| 187 |
$markup['reorder'] = '<td class="'.WPPIZZA_SLUG.'-item-reorder '.WPPIZZA_SLUG.'-item-noreorder">'.$txt['history_reorder_not_available'].'</td>'; |
| 188 |
}else{ |
| 189 |
|
| 190 |
/** |
| 191 |
repurchase button id - allow filtering - |
| 192 |
to check do we actually need an id here ? this can be taken from closest tr after all (thoughg that has "." as splits) == to do !? |
| 193 |
*/ |
| 194 |
$id = ''.WPPIZZA_SLUG.'-'.$item['blog_id'].'-'.$item['cat_id_selected'].'-'.$item['post_id'].'-'.$item['sizes'].'-'.$item['size'].''; |
| 195 |
$class = apply_filters('wppizza_filter_reorder_item_button_id', $id, $key , $item, $items, $item_count, $order_id, $txt, $type ); |
| 196 |
/** repurchase button class - allow filtering */ |
| 197 |
$class="".WPPIZZA_SLUG."-add-to-cart ".WPPIZZA_SLUG."-do-reorder"; |
| 198 |
$class = apply_filters('wppizza_filter_reorder_item_button_class', $class, $key , $item, $items, $item_count, $order_id, $txt, $type ); |
| 199 |
|
| 200 |
/** add repurchase button for item - allow filtering */ |
| 201 |
$markup['reorder'] = '<td class="'.WPPIZZA_SLUG.'-item-reorder"><input id="'.$id.'" class="'.$class.'" type="button" value="'.$txt['history_label_item_reorder_button'].'" title="'.$txt['history_title_item_reorder_button'].'" /></td>'; |
| 202 |
$markup['reorder'] = apply_filters('wppizza_filter_reorder_item_button', $markup['reorder'], $key , $item, $items, $item_count, $order_id, $txt, $type ); |
| 203 |
} |
| 204 |
|
| 205 |
/* reset counter for every order after last item */ |
| 206 |
//if($unique_items_in_cart == ($item_count+1)){ |
| 207 |
// $repurchase_count = 0; |
| 208 |
//} |
| 209 |
|
| 210 |
return $markup; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
/* button to re purchase whole order, skip for cart */ |
| 216 |
function orderhistory_reorder_purchase_order($markup, $blog_id, $order_id, $cart, $txt, $colspan, $type){ |
| 217 |
/* |
| 218 |
don't add this to current cart though |
| 219 |
as that would be silly |
| 220 |
*/ |
| 221 |
if($type == 'cart'){ |
| 222 |
return $markup; |
| 223 |
} |
| 224 |
/* |
| 225 |
add the repurchase button |
| 226 |
*/ |
| 227 |
$reorder_entire_purchase_markup = '<div class="'.WPPIZZA_SLUG.'-reorder"><input id="'.WPPIZZA_SLUG.'-reorder-'.$blog_id.'-'.$order_id.'" class="'.WPPIZZA_SLUG.'-reorder-purchase" type="button" title="'.$txt['history_title_reorder_purchase_button'].'" value="'.$txt['history_label_reorder_purchase_button'].'" /></div>'; |
| 228 |
/** add button after itemised order */ |
| 229 |
array_splice($markup, (count($markup)), 0, $reorder_entire_purchase_markup); |
| 230 |
|
| 231 |
return $markup; |
| 232 |
} |
| 233 |
|
| 234 |
/******************************************************************************************************************************************************* |
| 235 |
* |
| 236 |
* |
| 237 |
* |
| 238 |
* [add admin page options] |
| 239 |
* |
| 240 |
* |
| 241 |
* |
| 242 |
********************************************************************************************************************************************************/ |
| 243 |
|
| 244 |
/*------------------------------------------------------------------------------ |
| 245 |
# |
| 246 |
# |
| 247 |
# [settings page] |
| 248 |
# |
| 249 |
# |
| 250 |
------------------------------------------------------------------------------*/ |
| 251 |
|
| 252 |
/*------------------------------------------------------------------------------ |
| 253 |
# [settings section - setting page] |
| 254 |
# @since 3.0 |
| 255 |
# @return array() |
| 256 |
------------------------------------------------------------------------------*/ |
| 257 |
function admin_options_settings($settings, $sections, $fields, $inputs, $help){ |
| 258 |
|
| 259 |
/*section*/ |
| 260 |
if($sections){ |
| 261 |
$settings['sections'][$this->section_key] = __('Repurchase', 'wppizza-admin'); |
| 262 |
} |
| 263 |
/*help*/ |
| 264 |
if($help){ |
| 265 |
$settings['help'][$this->section_key][] = array( |
| 266 |
'label'=>__('Repurchase', 'wppizza-admin'), |
| 267 |
'description'=>array( |
| 268 |
__('Please adjust settings as appropriate according to the information provided next to each individual option.', 'wppizza-admin') |
| 269 |
) |
| 270 |
); |
| 271 |
} |
| 272 |
/*fields*/ |
| 273 |
if($fields){ |
| 274 |
$field = 'repurchase'; |
| 275 |
$settings['fields'][$this->section_key][$field] = array( __('Order Repurchase', 'wppizza-admin'), array( |
| 276 |
'value_key'=>$field, |
| 277 |
'option_key'=>$this->settings_page, |
| 278 |
/* Translators: 1: URL to applicable shortcodes documentation, 1: WPPizza Name as defined by constant */ |
| 279 |
'label'=>__('allow items to be repurchased from users order purchase page. ', 'wppizza-admin'). ' '.sprintf(__('if it deos not exists already create a page with the shortcode as described <a href="%s">here</a>.', 'wppizza-admin'), 'https://docs.wp-pizza.com/shortcodes/?section=user-orderhistory') .' '.sprintf(__('furthermore ensure this page also has a %s cart on it (widget/shortcode) as without it there is nothing to put the re-order into.','wppizza-admin'), WPPIZZA_NAME) , |
| 280 |
'description'=>array() |
| 281 |
)); |
| 282 |
} |
| 283 |
|
| 284 |
return $settings; |
| 285 |
} |
| 286 |
/*------------------------------------------------------------------------------ |
| 287 |
# [output option fields - setting page] |
| 288 |
# @since 3.0 |
| 289 |
# @return array() |
| 290 |
------------------------------------------------------------------------------*/ |
| 291 |
function admin_options_fields_settings($wppizza_options, $options_key, $field, $label, $description){ |
| 292 |
if($field=='repurchase'){ |
| 293 |
echo "<label>"; |
| 294 |
echo "<input id='".$field."' name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' type='checkbox' ". checked($wppizza_options[$options_key][$field],true,false)." value='1' />"; |
| 295 |
echo "" . $label . ""; |
| 296 |
echo "</label>"; |
| 297 |
echo"".$description.""; |
| 298 |
} |
| 299 |
} |
| 300 |
/*------------------------------------------------------------------------------ |
| 301 |
# |
| 302 |
# |
| 303 |
# [localization page] |
| 304 |
# |
| 305 |
# |
| 306 |
------------------------------------------------------------------------------*/ |
| 307 |
/**************************************************************** |
| 308 |
* [settigs section - localization page] |
| 309 |
* @since 3.0 |
| 310 |
* @return array() |
| 311 |
****************************************************************/ |
| 312 |
function admin_options_localization($settings, $sections, $fields, $inputs, $help){ |
| 313 |
global $wppizza_options; |
| 314 |
/* skip if not enabled */ |
| 315 |
if(empty($wppizza_options[$this->settings_page]['repurchase'])){ |
| 316 |
return $settings; |
| 317 |
} |
| 318 |
/******************************** |
| 319 |
* [Labels] |
| 320 |
********************************/ |
| 321 |
/*sections*/ |
| 322 |
if($sections){ |
| 323 |
$add_settings['sections'][$this->section_key] = __('User Purchase History - Repurchasing', 'wppizza-admin'); |
| 324 |
} |
| 325 |
/*fields*/ |
| 326 |
if($fields){ |
| 327 |
$field = 'history_label_reorder_header'; |
| 328 |
$add_settings['fields'][$this->section_key][$field] = array( '' , array( |
| 329 |
'value_key'=>$field, |
| 330 |
'option_key'=>$this->localization_page, |
| 331 |
'label'=>__('Label Header Reorder', 'wppizza-admin') |
| 332 |
)); |
| 333 |
$field = 'history_label_item_reorder_button'; |
| 334 |
$add_settings['fields'][$this->section_key][$field] = array( '' , array( |
| 335 |
'value_key'=>$field, |
| 336 |
'option_key'=>$this->localization_page, |
| 337 |
'label'=>__('Label Reorder Item Button', 'wppizza-admin') |
| 338 |
)); |
| 339 |
$field = 'history_title_item_reorder_button'; |
| 340 |
$add_settings['fields'][$this->section_key][$field] = array( '' , array( |
| 341 |
'value_key'=>$field, |
| 342 |
'option_key'=>$this->localization_page, |
| 343 |
'label'=>__('Title Reorder Item Button (shown on hover)', 'wppizza-admin') |
| 344 |
)); |
| 345 |
$field = 'history_label_reorder_purchase_button'; |
| 346 |
$add_settings['fields'][$this->section_key][$field] = array( '' , array( |
| 347 |
'value_key'=>$field, |
| 348 |
'option_key'=>$this->localization_page, |
| 349 |
'label'=>__('Label Reorder Entire Purchase Button', 'wppizza-admin') |
| 350 |
)); |
| 351 |
$field = 'history_title_reorder_purchase_button'; |
| 352 |
$add_settings['fields'][$this->section_key][$field] = array( '' , array( |
| 353 |
'value_key'=>$field, |
| 354 |
'option_key'=>$this->localization_page, |
| 355 |
'label'=>__('Title Reorder Entire Purchase Button (shown on hover)', 'wppizza-admin') |
| 356 |
)); |
| 357 |
$field = 'history_reorder_not_available'; |
| 358 |
$add_settings['fields'][$this->section_key][$field] = array( '' , array( |
| 359 |
'value_key'=>$field, |
| 360 |
'option_key'=>$this->localization_page, |
| 361 |
'label'=>__('Label Reorder Not Available', 'wppizza-admin') |
| 362 |
)); |
| 363 |
|
| 364 |
} |
| 365 |
|
| 366 |
/* |
| 367 |
splice section and fields into the required position on admin page |
| 368 |
*/ |
| 369 |
if(!empty($add_settings)){ |
| 370 |
if($sections){ |
| 371 |
$settings['sections'] = wppizza_array_splice($settings['sections'], $add_settings['sections'], $this->localization_position); |
| 372 |
} |
| 373 |
if($fields){ |
| 374 |
$settings['fields'] = wppizza_array_splice($settings['fields'], $add_settings['fields'], $this->localization_position); |
| 375 |
} |
| 376 |
} |
| 377 |
return $settings; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
/*------------------------------------------------------------------------------ |
| 385 |
# [insert default option on install] |
| 386 |
# $parameter $options array() | filter passing on filtered options |
| 387 |
# @since 3.0 |
| 388 |
# @return array() |
| 389 |
------------------------------------------------------------------------------*/ |
| 390 |
function options_default($options){ |
| 391 |
|
| 392 |
/* |
| 393 |
order settings |
| 394 |
*/ |
| 395 |
$options[$this->settings_page]['repurchase'] = false; |
| 396 |
|
| 397 |
/* |
| 398 |
localization |
| 399 |
*/ |
| 400 |
$options[$this->localization_page]['history_label_reorder_header'] = wppizza_textdomain('Re-order', 'wppizza'); |
| 401 |
$options[$this->localization_page]['history_label_item_reorder_button'] = wppizza_textdomain('+', 'wppizza'); |
| 402 |
$options[$this->localization_page]['history_title_item_reorder_button'] = wppizza_textdomain('Re-order this item', 'wppizza'); |
| 403 |
$options[$this->localization_page]['history_label_reorder_purchase_button'] = wppizza_textdomain('Re-order', 'wppizza'); |
| 404 |
$options[$this->localization_page]['history_title_reorder_purchase_button'] = wppizza_textdomain('Re-order', 'wppizza'); |
| 405 |
$options[$this->localization_page]['history_reorder_not_available'] = wppizza_textdomain('N/A', 'wppizza-admin'); |
| 406 |
|
| 407 |
return $options; |
| 408 |
} |
| 409 |
|
| 410 |
/*------------------------------------------------------------------------------ |
| 411 |
# [validate options on save/update] |
| 412 |
# |
| 413 |
# @since 3.0 |
| 414 |
# @return array() |
| 415 |
------------------------------------------------------------------------------*/ |
| 416 |
function options_validate($options, $input){ |
| 417 |
/**make sure we get the full array on install/update**/ |
| 418 |
if ( empty( $_POST['_wp_http_referer'] ) ) { |
| 419 |
return $input; |
| 420 |
} |
| 421 |
/* |
| 422 |
settings |
| 423 |
*/ |
| 424 |
if(isset($_POST[''.WPPIZZA_SLUG.'_'.$this->settings_page.''])){ |
| 425 |
$options[$this->settings_page]['repurchase'] = !empty($input[$this->settings_page]['repurchase']) ? true : false; |
| 426 |
} |
| 427 |
|
| 428 |
/* |
| 429 |
localization strings are automatically validated |
| 430 |
*/ |
| 431 |
|
| 432 |
return $options; |
| 433 |
} |
| 434 |
} |
| 435 |
/*************************************************************** |
| 436 |
* |
| 437 |
* [ini] |
| 438 |
* |
| 439 |
***************************************************************/ |
| 440 |
$WPPIZZA_MODULE_ORDERSETTINGS_REPURCHASE = new WPPIZZA_MODULE_ORDERSETTINGS_REPURCHASE(); |
| 441 |
?> |