PluginProbe
MyBookTable Bookstore by Stormhill Media / trunk
MyBookTable Bookstore by Stormhill Media vtrunk
3.6.5 trunk
mybooktable / includes / extras / booksorting.php

booksorting.php in MyBookTable Bookstore by Stormhill Media trunk, at includes/extras/booksorting.php

262 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4 function mbt_booksorting_init() {
5 add_filter('views_edit-mbt_book', 'mbt_add_sort_books_link');
6 mbt_add_custom_page('mbt_sort_books', 'mbt_render_sort_books_page');
7 }
8 add_action('mbt_init', 'mbt_booksorting_init');
9
10 function mbt_add_sort_books_link($views) {
11 // <a href="'.mbt_get_custom_page_url('mbt_sort_books').'">'.__('Sort Books', 'mybooktable').'</a>
12 $link = wp_nonce_url(mbt_get_custom_page_url('mbt_sort_books'),'nonce_mbt','mbt_nonce');
13 $views['sorting'] = '<a href="'.$link.'">'.__('Sort Books', 'mybooktable').'</a>';
14 return $views;
15 }
16
17 function mbt_sort_books_page_sorter($books, $attributes) {
18 $attribute = array_shift($attributes);
19 $parts = explode('-', $attribute);
20 $attrib_name = $parts[0];
21 $attrib_dir = $parts[1];
22
23 $sorted_books = array();
24 foreach($books as $book) {
25 $val = $book[$attrib_name];
26 if(!isset($sorted_books[$val])) { $sorted_books[$val] = array(); }
27 $sorted_books[$val][] = $book;
28 }
29
30 $keys = array_keys($sorted_books);
31 natcasesort($keys);
32 if($attrib_dir !== 'desc') { $keys = array_reverse($keys); }
33
34 $final_books = array();
35 foreach($keys as $key) {
36 $section = $sorted_books[$key];
37 if(count($section) > 1 and count($attributes) > 0) {
38 $section = mbt_sort_books_page_sorter($section, $attributes);
39 }
40 foreach($section as $book) {
41 $final_books[] = $book;
42 }
43 }
44
45 return $final_books;
46 }
47
48 function mbt_render_sort_books_page() {
49 global $wpdb, $post;
50
51 if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), 'nonce_mbt')) { return; }
52 if(!empty($_REQUEST['book_order']) && !empty($_REQUEST['mbt_manual_book_order']) && $_REQUEST['book_order'] == 'manual') {
53 $data = json_decode(str_replace('\"', '"', sanitize_text_field(wp_unslash($_REQUEST['mbt_manual_book_order']))));
54 if(!empty($data) and is_object($data)) {
55 $data = (array)$data;
56 foreach($data as $key => $value) {
57 $wpdb->update($wpdb->posts, array('menu_order' => $value), array('ID' => $key), array('%d'), array('%d'));
58 }
59 }
60 }
61
62 if(!empty($_REQUEST['book_order']) && isset($_REQUEST['mbt_book_sorting_attributes']) && $_REQUEST['book_order'] == 'attribute') {
63 $data = json_decode(str_replace('\"', '"', sanitize_text_field(wp_unslash($_REQUEST['mbt_book_sorting_attributes']))));
64 if(!empty($data) and is_array($data)) {
65 $query = new WP_Query(array('post_type' => 'mbt_book', 'posts_per_page' => -1));
66 $books = array();
67 foreach ($query->posts as $post) {
68 $new_book = array();
69 $new_book['ID'] = $post->ID;
70 $new_book['title'] = $post->post_title;
71 $new_book['date'] = strtotime($post->post_date_gmt);
72
73 $author = '';
74 $authors = get_the_terms($post->ID, 'mbt_author');
75 if(!empty($authors) and !is_wp_error($authors)) {
76 $sortfunc = function($a, $b) {
77 $a = mbt_get_author_priority($a->term_id);
78 $b = mbt_get_author_priority($b->term_id);
79 return ($a > $b) ? -1 : (($a < $b) ? 1 : 0);
80 };
81 usort($authors, $sortfunc);
82 if(!empty($authors[0]) and !is_wp_error($authors[0])) {
83 $author = $authors[0]->name;
84 }
85 }
86 $new_book['author'] = $author;
87
88 $genre = '';
89 $genres = get_the_terms($post->ID, 'mbt_genre');
90 if(!empty($genres) and !is_wp_error($genres)) {
91 if(!empty($genres[0]) and !is_wp_error($genres[0])) {
92 $genre = $genres[0]->name;
93 }
94 }
95 $new_book['genre'] = $genre;
96
97 $tag = '';
98 $tags = get_the_terms($post->ID, 'mbt_tag');
99 if(!empty($tags) and !is_wp_error($tags)) {
100 if(!empty($tags[0]) and !is_wp_error($tags[0])) {
101 $tag = $tags[0]->name;
102 }
103 }
104 $new_book['tag'] = $tag;
105
106 $series = mbt_get_book_series($post->ID);
107 if(!empty($series) and !is_wp_error($series)) { $series = $series->name; } else { $series = ''; }
108 $new_book['series'] = $series;
109 $new_book['series_order'] = strval(floatval(get_post_meta($post->ID, 'mbt_series_order', true)));
110 $books[] = $new_book;
111 }
112
113 $books = mbt_sort_books_page_sorter($books, $data);
114
115 for($i=0; $i < count($books); $i++) {
116 $wpdb->update($wpdb->posts, array('menu_order' => $i), array('ID' => $books[$i]['ID']), array('%d'), array('%d'));
117 }
118 }
119 mbt_update_setting('book_sorting_attributes', sanitize_text_field(wp_unslash($_REQUEST['mbt_book_sorting_attributes'])));
120 }
121 $book_sorting_attributes = mbt_get_setting('book_sorting_attributes');
122
123 ?>
124 <div class="wrap mbt_sort_books">
125 <div id="icon-options-general" class="icon32"><br></div><h2><?php esc_attr_e('Sort Books', 'mybooktable'); ?></h2>
126
127 <div class="mbt_sorting_box">
128 <div class="mbt_sorting_box_title"><?php esc_attr_e('Attribute Sorting', 'mybooktable'); ?></div>
129 <div class="mbt_sorting_box_content">
130 <form id="mbt_sort_books_form" method="post" action="<?php echo(esc_url(admin_url('admin.php?page=mbt_sort_books&book_order=attribute'))); ?>">
131 <input id="mbt_book_sorting_attributes" name="mbt_book_sorting_attributes" type="hidden" value="<?php echo(wp_kses_post(htmlentities(str_replace('\"', '"', $book_sorting_attributes)))); ?>">
132 <div id="mbt_book_attributes">
133 <div id="mbt_book_attribute_adder"><span class="dashicons dashicons-plus"></span></div>
134 </div>
135 <input type="submit" name="save_settings" class="button button-primary" value="<?php esc_attr_e('Sort', 'mybooktable'); ?>" onclick="return mbt_submit_book_sorting_attributes();">
136 </form>
137 </div>
138 </div>
139
140 <div class="mbt_sorting_box">
141 <div class="mbt_sorting_box_title"><?php esc_attr_e('Manual Sorting', 'mybooktable'); ?></div>
142 <div class="mbt_sorting_box_content">
143 <form id="mbt_sort_books_form" method="post" action="<?php echo(esc_url(admin_url('admin.php?page=mbt_sort_books&book_order=manual'))); ?>">
144 <?php
145 wp_nonce_field('nonce_mbt','mbt_nonce');
146 $query = new WP_Query(array('post_type' => 'mbt_book', 'orderby' => 'menu_order', 'posts_per_page' => 300));
147 if($query->have_posts()) {
148 if(count($query->posts) > 300) {
149 echo('<div class="mbt_sorting_error">'.esc_attr_e('Manual book sorting is not supported for more than 300 books. Please use Attribute Sorting.', 'mybooktable').'</div>');
150 } else {
151 echo('<input type="submit" name="save_settings" class="button button-primary" value="'.sprintf(esc_html__('Save Changes', 'mybooktable')).'" onclick="return mbt_submit_manual_book_order();">');
152 echo('<input id="mbt_manual_book_order" name="mbt_manual_book_order" type="hidden" value="">');
153 echo('<ul id="mbt_manual_book_sorter">');
154 while($query->have_posts()) {
155 $query->the_post();
156 echo('<li data-id="'.esc_attr($post->ID).'" class="mbt_book">'.esc_attr($post->post_title).'</li>');
157 }
158 echo('</ul>');
159 }
160 } else {
161 esc_attr_e('No books to sort!', 'mybooktable');
162 }
163 ?>
164 </form>
165 </div>
166 </div>
167
168 </div>
169 <script type="text/javascript">
170
171 /*---------------------------------------------------------*/
172 /* Attribute Sorting */
173 /*---------------------------------------------------------*/
174
175 function mbt_submit_book_sorting_attributes() {
176 data = [];
177 jQuery("#mbt_book_attributes .mbt_book_attribute").each(function(i, e) {
178 data.push(jQuery(e).find('.mbt_book_attribute_selector').val()+'-'+jQuery(e).find('.mbt_book_direction_selector').val());
179 });
180 jQuery("#mbt_book_sorting_attributes").val(JSON.stringify(data));
181 return true;
182 }
183
184 function mbt_add_book_sorting_attribute(attribute) {
185 attribute = (typeof attribute === 'undefined') ? 'title-asc' : attribute;
186 parts = attribute.split('-');
187 attrib_name = parts[0];
188 attrib_dir = parts[1];
189
190 var element = '';
191 element += '<div class="mbt_book_attribute">';
192 if(jQuery('.mbt_book_attribute').length < 1) {
193 element += 'Sort by: ';
194 } else {
195 element += 'Then: ';
196 }
197 var attributes = {
198 'title': mbt_admin_pages_i18n.post_title,
199 'date': mbt_admin_pages_i18n.post_date,
200 'author': mbt_admin_pages_i18n.author,
201 'genre': mbt_admin_pages_i18n.genre,
202 'tag': mbt_admin_pages_i18n.tag,
203 'series': mbt_admin_pages_i18n.series,
204 'series_order': mbt_admin_pages_i18n.series_order,
205 };
206 element += '<select name="mbt_book_attribute_selector" class="mbt_book_attribute_selector">';
207 for(var name in attributes) {
208 element += '<option value="'+name+'" '+(attrib_name === name ? 'selected="selected"' : '')+'>'+attributes[name]+'</option>';
209 }
210 element += '</select>';
211 element += '<select name="mbt_book_direction_selector" class="mbt_book_direction_selector">';
212 element += '<option value="asc" '+(attrib_dir === 'asc' ? 'selected="selected"' : '')+'>'+mbt_admin_pages_i18n.ascending+'</option>';
213 element += '<option value="desc" '+(attrib_dir === 'desc' ? 'selected="selected"' : '')+'>'+mbt_admin_pages_i18n.descending+'</option>';
214 element += '</select>';
215 if(jQuery('.mbt_book_attribute').length >= 1) {
216 element += '<div class="mbt_book_attribute_remover">';
217 element += '<span class="dashicons dashicons-minus"></span>';
218 element += '</div>';
219 }
220 element += '</div>';
221 jQuery('#mbt_book_attribute_adder').before(jQuery(element));
222 }
223
224 jQuery(document).ready(function() {
225 jQuery('#mbt_book_attribute_adder').click(function() { mbt_add_book_sorting_attribute(); });
226
227 jQuery('#mbt_book_attributes').on('click', '.mbt_book_attribute_remover', function() {
228 jQuery(this).parents('.mbt_book_attribute').remove();
229 });
230
231 if(jQuery('#mbt_book_sorting_attributes').val()) {
232 jQuery.each(JSON.parse(jQuery('#mbt_book_sorting_attributes').val()), function(i, e) {
233 mbt_add_book_sorting_attribute(e);
234 });
235 } else {
236 mbt_add_book_sorting_attribute();
237 }
238 });
239
240 /*---------------------------------------------------------*/
241 /* Manual Sorting */
242 /*---------------------------------------------------------*/
243
244 jQuery.fn.reverse = [].reverse;
245
246 function mbt_submit_manual_book_order() {
247 data = {};
248 jQuery("#mbt_manual_book_sorter .mbt_book").reverse().each(function(i, e) {
249 data[jQuery(e).attr('data-id')] = i;
250 });
251 jQuery("#mbt_manual_book_order").val(JSON.stringify(data));
252 return true;
253 }
254
255 jQuery(document).ready(function() {
256 jQuery("#mbt_manual_book_sorter").sortable();
257 });
258
259 </script>
260 <?php
261 }
262