This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOffersManager.class.php
308 lines (277 loc) · 9.97 KB
/
OffersManager.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* Definition of OffersManager feature
*/
/**
* Manage the Offers linked to the booking engine by promo code
*/
class OffersManager extends Singleton{
/**
* Stores the custom post name
* @var string
*/
const CUSTOM_POST_NAME = 'offer';
/**
* Stores the custom taxonomy name
* @var string
*/
const CUSTOM_TAX_NAME = 'offer-type';
/**
* Stores the meta key name where store the offers details
* @var string
*/
const META_KEY_NAME = '_wtu_offers_details';
/**
* Use to retrieve the book now button compatible with Fblib
* @var int
*/
const FORMAT_OOFBLIB = 1;
/**
* Use to retrieve the book now button compatible with fblib.js
* @var int
*/
const FORMAT_FBLIB = 2;
/**
* Stores the connect name
* @var string
*/
public $connect_name;
/**
* Initializes the object
* @param string $connect_name the connect name
*/
protected function __construct(){
global $data;
$this->connect_name = $data['hotel_connect_name'];
add_action('init', array(&$this, 'register_custom_post'), 100);
add_action('init', array(&$this, 'register_custom_taxonomy'), 90);
add_action('add_meta_boxes', array(&$this, 'register_metaboxes'));
add_action('save_post', array(&$this, 'save_metabox_data'));
}
/**
* Returns the list of allowed offers details
* @return array
*/
public function offer_details_list(){
return array(
'cname' => array(
'name' => 'cname',
'label' => __('Connect Name', 'wtu_framework')
),
'lg' => array(
'name' => 'lg',
'label' => __('Language', 'wtu_framework')
),
'codeprice' => array(
'name' => 'codeprice',
'label' => __('Price Code', 'wtu_framework')
),
'firstroom' => array(
'name' => 'firstroom',
'label' => __('First Room', 'wtu_framework')
),
'codetrack' => array(
'name' => 'codetrack',
'label' => __('Track Code', 'wtu_framework')
),
'firstdate' => array(
'name' => 'firstdate',
'label' => __('First Date', 'wtu_framework')
),
'price' => array(
'name' => 'price',
'label' => __('Price', 'wtu_framework')
),
'price_old' => array(
'name' => 'price_old',
'label' => __('Price Old', 'wtu_framework')
)
);
}
/**
* Register the custom post type
*/
public function register_custom_post(){
register_post_type(
static::CUSTOM_POST_NAME,
array(
'label' => __('Offers', 'wtu_framework'),
'description' => __('A special offer for your hotel', 'wtu_framework'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => ''),
'query_var' => true,
'exclude_from_search' => false,
'supports' => array(
'title',
'editor',
'excerpt',
'trackbacks',
'custom-fields',
'comments',
'revisions',
'thumbnail',
'author',
'page-attributes',
),
'labels' => array (
'name' => __('Offers', 'wtu_framework'),
'singular_name' => __('Offer', 'wtu_framework'),
'menu_name' => __('Offers', 'wtu_framework'),
'add_new' => __('Add Offer', 'wtu_framework'),
'add_new_item' => __('Add New Offer', 'wtu_framework'),
'edit' => __('Edit', 'wtu_framework'),
'edit_item' => __('Edit Offer', 'wtu_framework'),
'new_item' => __('New Offer', 'wtu_framework'),
'view' => __('View Offer', 'wtu_framework'),
'view_item' => __('View Offer', 'wtu_framework'),
'search_items' => __('Search Offers', 'wtu_framework'),
'not_found' => __('No Offers Found', 'wtu_framework'),
'not_found_in_trash' => __('No Offers Found in Trash', 'wtu_framework'),
'parent' => __('Parent Offer', 'wtu_framework'),
),
)
);
}
/**
* Registers the needed custom taxonomy
*/
public function register_custom_taxonomy(){
register_taxonomy(
static::CUSTOM_TAX_NAME,
array (0 => static::CUSTOM_POST_NAME),
array(
'hierarchical' => true,
'label' => __('Types', 'wtu_framework'),
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => ''),
'singular_label' => __('Type', 'wtu_framework')
)
);
}
/**
* Registers the needed metaboxes
*/
public function register_metaboxes(){
add_meta_box(
'wpu-offers-manager',
__( 'Offer Detail', 'wtu_framework' ),
array(&$this, 'metabox_html'),
static::CUSTOM_POST_NAME,
'side',
'high'
);
}
/**
* Prints the metabox for the offer details
* @param object $post the current post
*/
public function metabox_html($post){
wp_nonce_field(__FILE__, static::META_KEY_NAME.'_nonce');
$values = get_post_meta($post->ID, static::META_KEY_NAME, true);
echo '<table class="form-table">';
foreach($this->offer_details_list() as $k => $v){
$name = static::META_KEY_NAME.'['.$v['name'].']';
echo '<tr>';
echo '<th>'.HtmlHelper::label($v['label'], $name).'</th>';
echo '<td>'.HtmlHelper::input(
$name,
'text',
array('value' => isset($values[$k]) ? $values[$k] : '')
).'</td>';
echo '</tr>';
}
echo '</table>';
}
/**
* Saves the metabox data while saving the offer
*/
public function save_metabox_data($post_id){
// First we need to check if the current user is authorised to do this action.
$post_obj = get_post_type_object($_POST['post_type']);
if(!current_user_can($post_obj->cap->edit_post, $post_id)) return;
// Secondly we need to check if the user intended to change this value.
if(
!isset($_POST[static::META_KEY_NAME.'_nonce'])
|| !wp_verify_nonce($_POST[static::META_KEY_NAME.'_nonce'], __FILE__)
) return;
$sanitized = array();
$cname = $this->offer_details_list();
$cname = $cname['cname']['name'];
if(empty($_POST[static::META_KEY_NAME][$cname]))
$_POST[static::META_KEY_NAME][$cname] = $this->connect_name;
foreach($_POST[static::META_KEY_NAME] as $k => $v){
$sanitized[$k] = sanitize_text_field($v);
}
update_post_meta(
$_POST['post_ID'],
static::META_KEY_NAME,
$sanitized
);
}
/**
* Retrieves the offer details
*
* Format:
* array (
* 'cname' => 'ITAOSHTLExpress',
* 'lg' => '',
* 'codeprice' => 'PROMO-Terme-e-gastronomia',
* 'firstroom' => 'Double-Superior',
* 'codetrack' => '',
* 'firstdate' => '',
* )
* @param object $post the post object
* @return array a list of details
*/
public function get_offer_details($post=null){
if(is_null($post)) global $post;
return get_post_meta($post->ID, static::META_KEY_NAME, true);
}
/**
* Retrieves the book now onclick attribute
* @param object $post the offer custom post
* @param number $format the format
* @return string the onclick attribute
*/
public function get_booknow($post=null, $format=0){
$values = $this->get_offer_details($post);
switch($format){
case static::FORMAT_OOFBLIB:
default:
$toret = <<<EOF
Fblib.hhotelResaDirect('{$values['cname']}', '{$values['lg']}', '{$values['codeprice']}', '{$values['firstroom']}', '{$values['codetrack']}', '{$values['firstdate']}');
EOF;
break;
case static::FORMAT_FBLIB:
$toret = <<<EOF
hhotelResaDirect('{$values['cname']}', '{$values['lg']}', '{$values['codeprice']}', '{$values['firstroom']}', '{$values['codetrack']}', '{$values['firstdate']}');
EOF;
break;
}
return $toret;
}
/**
* Retrieves the price_content div
* @return string
*/
public function get_offer_price($post=null){
$values = static::get_offer_details($post);
$from = __('From', 'theme');
$currency = 'EUR';
$per_night = __('per night', 'theme');
return <<< EOF
<div class="price_content">
<span class="price_apd">$from</span>
<span class="price_rate_strike">{$values['price_old']}</span>
<span class="price_rate">{$values['price']}</span>
<span class="price_currency">$currency</span>
<span class="price_pn">$per_night</span>
</div>
EOF;
}
}