-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwidget-list.php
120 lines (102 loc) · 3.75 KB
/
widget-list.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
<?php
require_once(TIMEXPAPI__PLUGIN_DIR . "expedia_api_wrapper/expedia-api.php");
require_once(TIMEXPAPI__PLUGIN_DIR . "hotel-list-renderer.php");
class TimExpediaApiListWidget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'timexpapi_list', // Base ID
__( 'Hotels List', 'text_domain' ), // Name
array( 'description' => __( 'Hotel list from Expedia', 'text_domain' ), ) // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
wp_enqueue_style('timexpapi-list-style', TIMEXPAPI__PLUGIN_URL.'css/hotels-list.css');
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
}
$api_public_key = 'cbrzfta369qwyrm9t5b8y8kf'; // If you get authentication Error
$customer_id = '55505'; // Change this to your own customer ID.
$currency_code = 'USD';
$minor_rev = 99;
$locale = 'en_US' ;
// initiate the API object
$myapi = new API( $customer_id , $api_public_key );
// set some basics parameters
$myapi->set_customer_session_id( session_id() );
$myapi->set_customer_ip_address($_SERVER['REMOTE_ADDR']);
$myapi->set_customer_user_agent($_SERVER['HTTP_USER_AGENT']);
$myapi->set_currency_code($currency_code);
$myapi->set_minor_rev($minor_rev);
$myapi->set_locale($locale);
// get hotels list for specified destination
// lang=en
// currency=USD
// destination=Kiev
// checkin=05%2F15%2F2015
// checkout=05%2F16%2F2015
// roomsCount=1
// rooms%5B0%5D.adultsCount=1
// rooms%5B0%5D.childrenCount=0
$params = TimExpediaApiListWidget::parseParams();
$hotels = $myapi->getHotelList($params);
TimExpediaApiListRenderer::renderHotelList($hotels);
echo $args['after_widget'];
}
private static function parseParams() {
$params = array();
$params['destinationString'] = $_GET['destination'];
$params['arrivalDate'] = $_GET['checkin'];
$params['departureDate'] = $_GET['checkout'];
// TODO: load from settings
$params['numberOfResults'] = 500;
foreach ($_GET['rooms'] as $key => $value) {
// $value['adultsCount'];// . ",13,6";
$roomGuests = count($value['children']) > 0 ? $value['adultsCount'].",".implode(",", $value['children']) : $value['adultsCount'];
$params['room' . ($key+1)] = $roomGuests;
}
return $params;
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'New title', 'text_domain' );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<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 ); ?>">
</p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}