-
Notifications
You must be signed in to change notification settings - Fork 8
/
customsorting.php
executable file
·218 lines (189 loc) · 9.22 KB
/
customsorting.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
<?php
//Exit if not called in proper context
if (!defined('ABSPATH')) exit();
//Only run this if sort key is set to custom
if ($foxyshop_settings['sort_key'] == "menu_order") add_action('admin_menu', 'foxyshop_custom_sorting_menu');
function foxyshop_custom_sorting_menu() {
add_submenu_page('edit.php?post_type=foxyshop_product', sprintf(__('Custom %s Sorting', 'foxyshop'), FOXYSHOP_PRODUCT_NAME_SINGULAR), sprintf(__('Set %s Order', 'foxyshop'), FOXYSHOP_PRODUCT_NAME_SINGULAR), apply_filters('foxyshop_product_sort_perm', 'edit_others_pages'), 'foxyshop_custom_sort', 'foxyshop_custom_sort');
}
add_action("save_post", "foxyshop_init_menu_order");
function foxyshop_init_menu_order($post_id) {
$cats = wp_get_post_terms($post_id, "foxyshop_categories", array("fields" => "ids"));
foreach ($cats as $cat_id) {
add_post_meta($post_id, "_foxyshop_menu_order_" . $cat_id, 0, true);
}
return;
}
function foxyshop_upgrade_menu_order() {
$products = get_posts(array('post_type' => 'foxyshop_product', 'posts_per_page' => -1, 'post_status' => null));
foreach ($products as $product) {
$cats = wp_get_post_terms($product->ID, "foxyshop_categories", array("fields" => "ids"));
foreach ($cats as $cat_id) {
add_post_meta($product->ID, "_foxyshop_menu_order_" . $cat_id, $product->menu_order, 1);
}
}
}
//Update Order
function foxyshop_update_order() {
if ($_POST['foxyshop_product_order_value'] != "") {
global $wpdb;
$foxyshop_product_order_value = sanitize_text_field($_POST['foxyshop_product_order_value']);
$IDs = explode(",", $foxyshop_product_order_value);
$result = count($IDs);
for($i = 0; $i < $result; $i++) {
$post_id = (int)str_replace(array("'", "id_"), "", $IDs[$i]);
$categoryID = (int)sanitize_text_field($_POST['categoryID']);
if ($categoryID == 0) {
$wpdb->query("UPDATE $wpdb->posts SET menu_order = '" . esc_sql($i) . "' WHERE id = '" . esc_sql($post_id) . "'");
} else {
update_post_meta($post_id, "_foxyshop_menu_order_" . $categoryID, $i);
}
}
return ('<div id="message" class="updated fade"><p>'. FOXYSHOP_PRODUCT_NAME_SINGULAR . ' ' . __('order updated successfully', 'foxyshop').'.</p></div>');
} else {
return ('<div id="message" class="updated fade"><p>'. __('An error occured, order has not been saved', 'foxyshop').'.</p></div>');
}
}
//Revert Order to Original (set all to 0)
function foxyshop_revert_order() {
global $wpdb;
$foxyshop_product_order_value = sanitize_text_field($_POST['foxyshop_product_order_value']);
$IDs = explode(",", $foxyshop_product_order_value);
$result = count($IDs);
for($i = 0; $i < $result; $i++) {
$post_id = (int)str_replace(array("'", "id_"), "", $IDs[$i]);
$categoryID = (int)sanitize_text_field($_POST['categoryID']);
if ($categoryID == 0) {
$wpdb->query("UPDATE $wpdb->posts SET menu_order = '0' WHERE id = '" . esc_sql($post_id) . "'");
} else {
update_post_meta($post_id, "_foxyshop_menu_order_" . $categoryID, 0);
}
}
return ('<div id="message" class="updated fade"><p>'. FOXYSHOP_PRODUCT_NAME_SINGULAR . ' ' . __('order reverted to original', 'foxyshop').'.</p></div>');
}
//The Main Function
function foxyshop_custom_sort() {
global $wpdb, $product;
$parentID = 0;
$success = "";
//Reset Order
if (isset($_GET['upgrade_menu_order'])) {
foxyshop_upgrade_menu_order();
}
if (isset($_POST['submit_new_product_order'])) {
if (check_admin_referer('update-foxyshop-sorting-options')) $success = foxyshop_update_order();
} elseif (isset($_POST['revert_product_order'])) {
if (check_admin_referer('update-foxyshop-sorting-options')) $success = foxyshop_revert_order();
}
?>
<div class="wrap">
<div class="icon32" id="icon-tools"><br></div>
<h2><?php echo sprintf(__('Custom %s Order'), esc_html(FOXYSHOP_PRODUCT_NAME_SINGULAR)); ?></h2>
<?php if ($success) echo wp_kses_post($success); ?>
<?php
$product_categories = get_terms('foxyshop_categories', 'hide_empty=0&hierarchical=0&orderby=name&order=ASC');
$categoryID = 0;
if (isset($_REQUEST['categoryID'])) {
$categoryID = sanitize_text_field($_REQUEST['categoryID']);
}
if ($product_categories) {
echo ('<p>' . sprintf(__("Select a category from the drop down to order the %s in that category.", 'foxyshop'), strtolower(FOXYSHOP_PRODUCT_NAME_PLURAL)) . "</p>\n");
echo ('<form name="form_product_category_order" method="post" action="edit.php?post_type=foxyshop_product&page=foxyshop_custom_sort">');
echo ('<select name="categoryID" id="categoryID">'."\n");
echo ('<option value="0"' . ($categoryID == 0 ? ' selected="selected"' : '') . '>' . __('All', 'foxyshop') . ' ' . FOXYSHOP_PRODUCT_NAME_PLURAL . '</option>'."\n");
foreach($product_categories as $cat) {
echo ('<option value="' . esc_attr($cat->term_id) . '"' . ($categoryID == $cat->term_id ? ' selected="selected"' : '') . '>' . esc_html($cat->name . ' (' . $cat->count . ')') . '</option>'."\n");
}
echo ('</select>'."\n");
echo ('<input type="submit" name="btnSubPages" class="button" id="btnSubPages" value="' . __('Select Category', 'foxyshop') . '" /></form>');
} else {
$categoryID = 0;
}
if ($categoryID >= 0) {
if ($categoryID > 0) {
$term = get_term_by('id', $categoryID, "foxyshop_categories");
$current_category_name = $term->name;
$args = array(
'post_type' => 'foxyshop_product',
'foxyshop_categories' => $term->slug,
'posts_per_page' => -1,
'orderby' => "meta_value_num",
"meta_key" => "_foxyshop_menu_order_" . $categoryID,
'order' => "ASC",
'post__not_in' => foxyshop_hide_children_array($categoryID),
);
} else {
$current_category_name = __("All", 'foxyshop') . ' ' . FOXYSHOP_PRODUCT_NAME_PLURAL;
$args = array(
'post_type' => 'foxyshop_product',
'numberposts' => -1,
'orderby' => "menu_order",
'order' => "ASC",
);
}
$product_list = get_posts($args);
if ($product_list) {
echo ('<h3>' . wp_kses_post($current_category_name) . '</h3>'."\n");
echo ('<p>' . esc_html(sprintf(__('Drag %s to the preferred order and then click the Save button at the bottom of the page.', 'foxyshop'), strtolower(FOXYSHOP_PRODUCT_NAME_PLURAL))) . '</p>');
echo ('<form name="form_product_order" method="post" action="">'."\n");
echo ('<ul id="foxyshop_product_order_list" class="foxyshop_sort_list">'."\n");
foreach ($product_list as $prod) {
$product = foxyshop_setup_product($prod);
if ($categoryID == 0) {
$current_count = $prod->menu_order;
} else {
$current_count = (int)get_post_meta($prod->ID, "_foxyshop_menu_order_" . $categoryID, true);
}
echo ('<li id="id_' . esc_attr($prod->ID) . '" class="lineitem">');
echo ('<img src="' . esc_url(foxyshop_get_main_image()) . '" />');
echo ('<h4>' . esc_html($prod->post_title) . '</h4>'."\n");
foxyshop_price();
echo ('<div class="counter">' . esc_html($current_count + 1) . '</div>');
echo ('<div style="clear: both; height: 1px;"></div>'."\n");
echo ('</li>'."\n");
}
echo '</ul>'."\n";
?>
<div style="width: 90%; height: 100px;">
<input type="submit" name="submit_new_product_order" id="submit_new_product_order" class="button-primary" value="<?php _e('Save Custom Order', 'foxyshop'); ?>" onclick="javascript:foxyshop_orderPages(); return true;" /> <strong id="updateText"></strong>
<input type="submit" name="revert_product_order" id="revert_product_order" class="button" style="float: right;" value="<?php _e('Revert To Original', 'foxyshop'); ?>" onclick="javascript:foxyshop_orderPages(); return true;" />
</div>
<input type="hidden" id="foxyshop_product_order_value" name="foxyshop_product_order_value" />
<input type="hidden" id="hdnParentID" name="hdnParentID" value="<?php echo esc_attr($parentID); ?>" />
<input type="hidden" id="categoryID" name="categoryID" value="<?php echo esc_attr($categoryID); ?>" />
<?php wp_nonce_field('update-foxyshop-sorting-options'); ?>
</form>
<?php
} else {
echo ('<p><em>' . esc_html(sprintf(__('No %s Found For This Category.', 'foxyshop'), FOXYSHOP_PRODUCT_NAME_PLURAL)) . '</em></p>');
echo ('<p><a href="edit.php?post_type=foxyshop_product&page=foxyshop_custom_sort&upgrade_menu_order=1&categoryID=' . esc_attr($categoryID) . '">Missing products? Click here.</a></p>');
}
}
?>
</div>
<?php
function foxyshop_inline_categorysorting_js() {
echo "<script type='text/javascript'>
function foxyshop_custom_order_load_event(){
jQuery(\"#foxyshop_product_order_list\").sortable({
placeholder: \"sortable-placeholder\",
revert: false,
tolerance: \"pointer\",
update: function() {
var counter = 1;
jQuery(\"#foxyshop_product_order_list li\").each(function() {
jQuery(this).find('.counter').html(counter);
counter++;
});
}
});
};
addLoadEvent(foxyshop_custom_order_load_event);
function foxyshop_orderPages() {
jQuery(\"#updateText\").html(\"" . sprintf(__('Updating %s Order...', 'foxyshop'), FOXYSHOP_PRODUCT_NAME_SINGULAR) . "\");
jQuery(\"#foxyshop_product_order_value\").val(jQuery(\"#foxyshop_product_order_list\").sortable(\"toArray\"));
}
</script>";
}
add_action( 'admin_print_footer_scripts', 'foxyshop_inline_categorysorting_js' );
}