-
Notifications
You must be signed in to change notification settings - Fork 8
/
widgetcode.php
executable file
·405 lines (325 loc) · 17.4 KB
/
widgetcode.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
//Exit if not called in proper context
if (!defined('ABSPATH')) exit();
add_action('widgets_init', 'foxyshop_load_widgets');
function foxyshop_load_widgets() {
register_widget('FoxyShop_Category');
register_widget('FoxyShop_Cart_Link');
register_widget('FoxyShop_Category_List');
}
class FoxyShop_Category extends WP_Widget {
//Widget Setup
function __construct() {
$widget_ops = array('classname' => 'foxyshop_category', 'description' => sprintf(__('Show the contents of a FoxyShop %s category.', 'foxyshop'), strtolower(FOXYSHOP_PRODUCT_NAME_SINGULAR)));
$control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'foxyshop-category-widget');
parent::__construct('foxyshop-category-widget', __('FoxyShop Category', 'foxyshop'), $widget_ops, $control_ops);
}
//Widget Display
function widget($args, $instance) {
extract($args);
//Our variables from the widget settings
$title = apply_filters('widget_title', $instance['title']);
$categoryName = $instance['categoryName'];
$showMoreDetails = isset($instance['showMoreDetails']) ? $instance['showMoreDetails'] : false;
$showAddToCart = isset($instance['showAddToCart']) ? $instance['showAddToCart'] : false;
$showMax = $instance['showMax'] > 0 ? $instance['showMax'] : -1;
echo wp_kses_post($before_widget);
if ($title) echo wp_kses_post($before_title . $title . $after_title);
if ($instance['simpleView']) {
echo ('<div class="foxyshop_category_simple_widget">');
foxyshop_featured_category($categoryName, $showAddToCart, $showMoreDetails, $showMax, True);
echo ('</div>');
} else {
echo ('<div class="foxyshop_category_widget">');
foxyshop_featured_category($categoryName, $showAddToCart, $showMoreDetails, $showMax);
echo ('</div>');
}
echo wp_kses_post($after_widget);
}
//Update Widget Settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
/* Strip tags for title and name to remove HTML (important for text inputs). */
$instance['title'] = strip_tags($new_instance['title']);
$instance['categoryName'] = strip_tags($new_instance['categoryName']);
$instance['showMax'] = (int)strip_tags($new_instance['showMax']);
/* No need to strip tags */
$instance['simpleView'] = $new_instance['simpleView'];
$instance['showAddToCart'] = $new_instance['showAddToCart'];
$instance['showMoreDetails'] = $new_instance['showMoreDetails'];
return $instance;
}
//Widget Control Panel
function form($instance) {
//Defaults
$defaults = array(
'title' => "",
'categoryName' => "",
'showAddToCart' => "",
'showMoreDetails' => "on",
'simpleView' => "",
'showMax' => -1
);
$instance = wp_parse_args((array)$instance, $defaults);
?>
<!-- Widget Title: Text Input -->
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php _e('Title', 'foxyshop'); ?>:</label>
<input id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($instance['title']); ?>" style="width:260px;" />
</p>
<!-- Select Category -->
<p>
<label for="<?php echo esc_attr($this->get_field_id('categoryName')); ?>"><?php _e('Category', 'foxyshop'); ?>:</label>
<select id="<?php echo esc_attr($this->get_field_id('categoryName')); ?>" name="<?php echo esc_attr($this->get_field_name('categoryName')); ?>" class="widefat" style="width:100%;">
<option value="">- - <?php _e('Select Category', 'foxyshop'); ?> - -</option>
<?php
$toplevelterms = get_terms('foxyshop_categories', 'hide_empty=0&hierarchical=0');
$arrCategory = array();
foreach ($toplevelterms as $toplevelterm) {
echo ('<option value="' . esc_attr($toplevelterm->slug) .'"'.
(($instance['categoryName'] == $toplevelterm->slug) ? ' selected="selected"' : '').
'>' . esc_html(str_replace("_","",$toplevelterm->name)) . '</option>');
}
?>
</select>
</p>
<!-- Max Entries -->
<p>
<label for="<?php echo esc_attr($this->get_field_id('showMax')); ?>"><?php echo (sprintf(__('Max %s to Show', 'foxyshop'), esc_html(FOXYSHOP_PRODUCT_NAME_PLURAL))); ?>:</label>
<input id="<?php echo esc_attr($this->get_field_id('showMax')); ?>" name="<?php echo esc_attr($this->get_field_name('showMax')); ?>" value="<?php echo esc_attr(($instance['showMax'] != 0 ? $instance['showMax'] : '')); ?>" style="width:50px;" /> <span class="small">(<?php _e('optional', 'foxyshop'); ?>)</span>
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked($instance['simpleView'], 'on'); ?> id="<?php echo esc_attr($this->get_field_id('simpleView')); ?>" name="<?php echo esc_attr($this->get_field_name('simpleView')); ?>" />
<label for="<?php echo esc_attr($this->get_field_id('simpleView')); ?>"><?php _e('Show Simple View', 'foxyshop'); ?></label>
</p>
<!-- Show Checkboxes for Button Selection -->
<p>
<input class="checkbox" type="checkbox" <?php checked($instance['showAddToCart'], 'on'); ?> id="<?php echo esc_attr($this->get_field_id('showAddToCart')); ?>" name="<?php echo esc_attr($this->get_field_name('showAddToCart')); ?>" />
<label for="<?php echo esc_attr($this->get_field_id('showAddToCart')); ?>"><?php _e('Show Add To Cart Button', 'foxyshop'); ?></label>
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked($instance['showMoreDetails'], 'on'); ?> id="<?php echo esc_attr($this->get_field_id('showMoreDetails')); ?>" name="<?php echo esc_attr($this->get_field_name('showMoreDetails')); ?>" />
<label for="<?php echo esc_attr($this->get_field_id('showMoreDetails')); ?>"><?php _e('Show More Details Button', 'foxyshop'); ?></label>
</p>
<?php
}
}
class FoxyShop_Cart_Link extends WP_Widget {
//Widget Setup
function __construct() {
$widget_ops = array('classname' => 'foxyshop_cart_link', 'description' => __('Show a link to view shopping cart.', 'foxyshop'));
$control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'foxyshop-cart-link-widget');
parent::__construct('foxyshop-cart-link-widget', __('FoxyShop Cart Link', 'foxyshop'), $widget_ops, $control_ops);
}
//Widget Display
function widget($args, $instance) {
extract($args);
/* Our variables from the widget settings. */
$title = apply_filters('widget_title', $instance['title']);
$linkText = $instance['linkText'];
$hideEmpty = isset($instance['hideEmpty']) ? $instance['hideEmpty'] : false;
echo wp_kses_post($before_widget);
if ($title) echo wp_kses_post($before_title . $title . $after_title);
echo ('<ul class="foxyshop_cart_link_widget"><li>');
foxyshop_cart_link($linkText, $hideEmpty);
echo ('</li></ul>');
echo wp_kses_post($after_widget);
}
//Update Widget Settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
/* Strip tags for title and name to remove HTML (important for text inputs). */
$instance['title'] = strip_tags($new_instance['title']);
/* No need to strip tags */
$instance['linkText'] = $new_instance['linkText'];
$instance['hideEmpty'] = $new_instance['hideEmpty'];
return $instance;
}
//Widget Control Panel
function form($instance) {
//Defaults
$defaults = array(
'title' => __('Your Shopping Cart', 'foxyshop'),
'linkText' => __('View Cart', 'foxyshop'),
'hideEmpty' => ""
);
$instance = wp_parse_args((array)$instance, $defaults); ?>
<!-- Widget Title: Text Input -->
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php _e('Title', 'foxyshop'); ?>:</label>
<input id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($instance['title']); ?>" style="width:260px;" />
</p>
<!-- Max Entries -->
<p>
<div><?php _e('Link Text:'); ?></div>
<textarea id="<?php echo esc_attr($this->get_field_id('linkText')); ?>" name="<?php echo esc_attr($this->get_field_name('linkText')); ?>" style="width: 100%;"><?php echo esc_textarea($instance['linkText']); ?></textarea>
<span class="small"><?php _e('Example', 'foxyshop'); ?>: View Cart (%q Items) ($%p)</span>
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked($instance['hideEmpty'], 'on'); ?> id="<?php echo esc_attr($this->get_field_id('hideEmpty')); ?>" name="<?php echo esc_attr($this->get_field_name('hideEmpty')); ?>" />
<label for="<?php echo esc_attr($this->get_field_id('hideEmpty')); ?>"><?php _e('Hide Link if Cart is Empty', 'foxyshop'); ?></label>
</p>
<?php
}
}
class FoxyShop_Category_List extends WP_Widget {
//Widget setup.
function __construct() {
$widget_ops = array('classname' => 'foxyshop_category_list', 'description' => __('Show the FoxyShop category list.', 'foxyshop'));
$control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'foxyshop-category-list-widget');
parent::__construct('foxyshop-category-list-widget', __('FoxyShop Category List', 'foxyshop'), $widget_ops, $control_ops);
}
//Widget Display
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title']);
$categoryID = $instance['categoryID'];
$depth = $instance['depth'];
if ($depth == "") $depth = 1;
echo wp_kses_post($before_widget);
if ($title) echo wp_kses_post($before_title . $title . $after_title);
echo ('<ul>');
foxyshop_simple_category_children($categoryID, $depth);
echo ('</ul>');
echo wp_kses_post($after_widget);
}
//Update Widget Settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
// Strip tags for title and name to remove HTML (important for text inputs)
$instance['title'] = strip_tags($new_instance['title']);
$instance['categoryID'] = (int)strip_tags($new_instance['categoryID']);
$instance['depth'] = strip_tags($new_instance['depth']);
return $instance;
}
//Widget Control Panel
function form($instance) {
//Defaults
$defaults = array(
'title' => "",
'categoryID' => 0,
'depth' => 1
);
$instance = wp_parse_args((array)$instance, $defaults); ?>
<!-- Widget Title: Text Input -->
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php _e('Title', 'foxyshop'); ?>:</label>
<input id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($instance['title']); ?>" style="width:260px;" />
</p>
<!-- Select Category -->
<p>
<label for="<?php echo esc_attr($this->get_field_id('categoryID')); ?>"><?php _e('Parent Category', 'foxyshop'); ?>:</label>
<select id="<?php echo esc_attr($this->get_field_id('categoryID')); ?>" name="<?php echo esc_attr($this->get_field_name('categoryID')); ?>" class="widefat" style="width:100%;">
<option value="0"><?php _e('Top Level Categories', 'foxyshop'); ?></option>
<?php
$toplevelterms = get_terms('foxyshop_categories', 'hide_empty=0&hierarchical=0');
$arrCategory = array();
foreach ($toplevelterms as $toplevelterm) {
echo ('<option value="' . esc_attr($toplevelterm->term_id) .'"'.
(($instance['categoryID'] == $toplevelterm->term_id) ? ' selected="selected"' : '')
. '>' . esc_html(str_replace("_","",$toplevelterm->name)) . '</option>');
}
?>
</select>
</p>
<!-- Depth -->
<p>
<label for="<?php echo esc_attr($this->get_field_id('depth')); ?>"><?php _e('Depth', 'foxyshop'); ?>:</label>
<input id="<?php echo esc_attr($this->get_field_id('depth')); ?>" name="<?php echo esc_attr($this->get_field_name('depth')); ?>" value="<?php echo esc_attr($instance['depth']); ?>" style="width:50px;" /> <span class="small"><?php _e('(default: 1)', 'foxyshop'); ?></span>
</p>
<?php
}
}
//Dashboard Stats
if (is_array($foxyshop_settings)) {
if ($foxyshop_settings['enable_dashboard_stats']) {
add_action('wp_dashboard_setup', 'add_foxyshop_dashboard_stats');
add_action('wp_ajax_foxyshop_order_history_dashboard_action', 'foxyshop_order_history_dashboard_ajax');
}
}
function add_foxyshop_dashboard_stats() {
if (!current_user_can(apply_filters('foxyshop_dashboard_stats_perm', 'manage_options'))) return;
wp_add_dashboard_widget('foxyshop_dashboard_widget', __('FoxyShop Statistics', 'foxyshop'), 'foxyshop_dashboard_stats');
//Move Widget to Right Column
global $wp_meta_boxes;
$my_widget = $wp_meta_boxes['dashboard']['normal']['core']['foxyshop_dashboard_widget'];
unset($wp_meta_boxes['dashboard']['normal']['core']['foxyshop_dashboard_widget']);
$wp_meta_boxes['dashboard']['side']['core']['foxyshop_dashboard_widget'] = $my_widget;
}
function foxyshop_dashboard_stats() {
echo '<div id="foxyshop_statsright">'."\n";
echo '<img src="' . esc_url(FOXYSHOP_DIR) . '/images/logo.png" alt="FoxyShop" style="width: 100%; max-width: 230px; float: right;" />'."\n";
echo '</div>';
echo '<div id="foxyshop_statsleft">'."\n";
echo '<h4>' . __('Order History', 'foxyshop') . '</h4>'."\n";
echo '<ul id="foxyshop_dashboard_order_history">'."\n";
echo '<li>' . __('Loading', 'foxyshop') . '...</li>';
echo '</ul>'."\n";
$count_posts = wp_count_posts('foxyshop_product');
$tax = get_terms('foxyshop_categories', array("hide_empty" => 0));
echo '<h4 style="margin-top: 24px;">' . esc_html(FOXYSHOP_PRODUCT_NAME_SINGULAR) . ' ' . __('Summary', 'foxyshop') . '</h4>';
echo '<ul>';
echo '<li><a href="edit.php?post_type=foxyshop_product">' . esc_html($count_posts->publish . ' ' . $count_posts->publish == 1 ? FOXYSHOP_PRODUCT_NAME_SINGULAR : FOXYSHOP_PRODUCT_NAME_PLURAL) . '</a></li>';
echo '<li><a href="edit-tags.php?taxonomy=foxyshop_categories&post_type=foxyshop_product">' . esc_html(count($tax) . ' ' . FOXYSHOP_PRODUCT_NAME_SINGULAR) . ' ' . _n('Category', 'Categories', count($tax), 'foxyshop') . '</a></li>';
echo '</ul>';
echo '</div>'."\n";
?>
<?php
function foxyshop_inline_dashboard_stats_js() {
echo "<script type='text/javascript'>
jQuery(document).ready(function($){
$.post(ajaxurl, { action: 'foxyshop_order_history_dashboard_action', security: '" . wp_create_nonce("foxyshop-order-info-dashboard") . "' }, function(response) {
$(\"#foxyshop_dashboard_order_history\").html(response)
});
});
</script>";
}
add_action( 'admin_print_footer_scripts', 'foxyshop_inline_dashboard_stats_js' );
echo '<div style="clear: both;"></div>'."\n";
}
//AJAX Order History - this is in an AJAX call to avoid a delay in rendering the dashboard
function foxyshop_order_history_dashboard_ajax() {
global $foxyshop_settings;
check_ajax_referer('foxyshop-order-info-dashboard', 'security');
$pagination_start = 1;
$filtered_total = 2;
$orderstats = array(1 => array(0, 0), 7 => array(0, 0), 30 => array(0, 0));
while ($pagination_start < $filtered_total) {
//Get Order Info
$foxy_data = array(
"api_action" => "transaction_list",
"entries_per_page" => 50,
"pagination_start" => $pagination_start,
"transaction_date_filter_begin" => date("Y-m-d", strtotime("-30 days")),
"transaction_date_filter_end" => date("Y-m-d"),
"is_test_filter" => 0,
"hide_transaction_filter" => "",
);
$foxy_response = foxyshop_get_foxycart_data($foxy_data);
$xml = simplexml_load_string($foxy_response, NULL, LIBXML_NOCDATA);
if ($xml->result != "ERROR") {
$filtered_total = (int)$xml->statistics->filtered_total;
$pagination_start = (int)$xml->statistics->pagination_end + 1;
foreach($xml->transactions->transaction as $transaction) {
$transaction_date = (string)$transaction->transaction_date;
$transaction_total = (double)$transaction->order_total;
if (strtotime($transaction_date) >= strtotime("-24 hours")) {
$orderstats[1][0]++;
$orderstats[1][1] += $transaction_total;
}
if (strtotime($transaction_date) >= strtotime("-7 days")) {
$orderstats[7][0]++;
$orderstats[7][1] += $transaction_total;
}
$orderstats[30][0]++;
$orderstats[30][1] += $transaction_total;
}
} else {
$filtered_total = 0;
}
}
echo '<li>' . __('One Day', 'foxyshop') . ': <a href="edit.php?foxyshop_search=1&is_test_filter=&post_type=foxyshop_product&page=foxyshop_order_management&transaction_date_filter_begin=' . date("Y-m-d", strtotime("-1 day")) . '&transaction_date_filter_end='.date("Y-m-d") . '">' . esc_html($orderstats[1][0] . ' ' . _n('order', 'orders', $orderstats[1][0], 'foxyshop') . ', ' . foxyshop_currency($orderstats[1][1])) . '</a></li>'."\n";
echo '<li>' . __('Seven Days', 'foxyshop') . ': <a href="edit.php?foxyshop_search=1&is_test_filter=&post_type=foxyshop_product&page=foxyshop_order_management&transaction_date_filter_begin=' . date("Y-m-d", strtotime("-7 days")) . '&transaction_date_filter_end='.date("Y-m-d") . '">' . esc_html($orderstats[7][0] . ' ' . _n('order', 'orders', $orderstats[7][0], 'foxyshop') . ', ' . foxyshop_currency($orderstats[7][1])) . '</a></li>'."\n";
echo '<li>' . __('30 Days', 'foxyshop') . ': <a href="edit.php?foxyshop_search=1&is_test_filter=&post_type=foxyshop_product&page=foxyshop_order_management&transaction_date_filter_begin=' . date("Y-m-d", strtotime("-30 days")) . '&transaction_date_filter_end='.date("Y-m-d") . '">' . esc_html($orderstats[30][0] . ' ' . _n('order', 'orders', $orderstats[30][0], 'foxyshop') . ', ' . foxyshop_currency($orderstats[30][1])) . '</a></li>'."\n";
die;
}