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

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

249 lines 12.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_register_widgets() {
5 register_widget("MBT_Featured_Book");
6 register_widget("MBT_Taxonomies");
7 add_action('admin_enqueue_scripts', 'mbt_enqueue_widget_admin_js');
8 }
9 add_action('widgets_init', 'mbt_register_widgets');
10
11 function mbt_enqueue_widget_admin_js() {
12 global $pagenow; if($pagenow != 'widgets.php' and $pagenow != 'customize.php') { return; }
13
14 wp_enqueue_script("mbt-widgets", plugins_url('js/widgets.js', dirname(dirname(__FILE__))), 'jquery', MBT_VERSION, true);
15 }
16
17 /*---------------------------------------------------------*/
18 /* Featured Books Widget */
19 /*---------------------------------------------------------*/
20
21 class MBT_Featured_Book extends WP_Widget {
22 function __construct() {
23 $widget_ops = array('classname' => 'mbt_featured_book', 'description' => __("Displays featured or random books.", 'mybooktable'));
24 parent::__construct('mbt_featured_book', __('MyBookTable Featured Books', 'mybooktable'), $widget_ops);
25 $this->defaultargs = array('title' => __('Featured Books', 'mybooktable'), 'selectmode' => 'by_date', 'featured_books' => array(), 'image_size' => 'medium', 'num_books' => 1, 'show_blurb' => true, 'use_shadowbox' => true);
26 }
27
28 function widget($args, $instance) {
29 extract(wp_parse_args($instance, $this->defaultargs));
30
31 $num_books = intval($num_books);
32 if($num_books > 10 or $num_books < 1) { $num_books = 1; }
33 if(!empty($featured_book)) { $featured_books = array((int)$featured_book); }
34
35 echo(wp_kses_post($args['before_widget']));
36 if($title) { echo(wp_kses_post($args['before_title'].$title.$args['after_title'])); }
37
38 if($selectmode == 'manual_select' and !empty($featured_books)) {
39 $books = array();
40 foreach($featured_books as $featured_book) {
41 $new_book = get_post($featured_book);
42 if($new_book) { $books[] = $new_book; }
43 }
44 } else if($selectmode == 'random') {
45 $wp_query = new WP_Query(array('post_type' => 'mbt_book', 'posts_per_page' => -1));
46 $books = array();
47 $keys = array_rand($wp_query->posts, $num_books);
48 if(!is_array($keys)) { $keys = array($keys); }
49 foreach($keys as $key) {
50 $books[] = $wp_query->posts[$key];
51 }
52 } else {
53 $wp_query = new WP_Query(array('post_type' => 'mbt_book', 'orderby' => 'date', 'posts_per_page' => $num_books));
54 $books = $wp_query->posts;
55 }
56
57 if(!empty($books)) {
58 mbt_enqueue_frontend_scripts();
59 ?> <div class="mbt-featured-book-widget"> <?php
60 foreach($books as $book) {
61 $permalink = get_permalink($book->ID);
62 mbt_start_template_context('featured-book-widget');
63 ?>
64 <div class="mbt-featured-book-widget-book">
65 <h2 class="mbt-book-title widget-title"><a href="<?php echo(esc_url($permalink)); ?>"><?php echo(esc_attr(get_the_title($book->ID))); ?></a></h2>
66 <div class="mbt-book-images"><a href="<?php echo(esc_url($permalink)); ?>"><?php echo(wp_kses_post(mbt_get_book_image($book->ID, array('class' => $image_size, 'size' => '25vw')))); ?></a></div>
67 <?php if($show_blurb) { ?><div class="mbt-book-blurb"><?php echo(wp_kses_post(mbt_get_book_blurb($book->ID, true))); ?></div><?php } ?>
68 <?php echo(wp_kses_post(mbt_get_buybuttons($book->ID, true, !empty($use_shadowbox)))); ?>
69 </div>
70 <?php
71 mbt_end_template_context();
72 }
73 ?> </div> <?php
74 }
75
76 echo('<div style="clear:both;"></div>');
77 echo(wp_kses_post($args['after_widget']));
78 }
79
80 function update($new_instance, $old_instance) {
81 $instance = $old_instance;
82 $instance['title'] = wp_strip_all_tags($new_instance['title']);
83 $instance['selectmode'] = wp_strip_all_tags($new_instance['selectmode']);
84 $instance['image_size'] = $new_instance['image_size'];
85 $instance['num_books'] = intval($new_instance['num_books']);
86 $instance['show_blurb'] = (bool)$new_instance['show_blurb'];
87 $instance['use_shadowbox'] = (bool)$new_instance['use_shadowbox'];
88 $instance['featured_books'] = (array)json_decode($new_instance['featured_books']);
89 unset($instance['featured_book']);
90 return $instance;
91 }
92
93 function form($instance) {
94 extract(wp_parse_args($instance, $this->defaultargs));
95 if(!empty($featured_book)) { $featured_books = array((int)$featured_book); }
96 ?>
97
98 <div class="mbt-featured-book-widget-editor" onmouseover="mbt_initialize_featured_book_widget_editor(this);">
99 <p>
100 <label><?php esc_attr_e('Title', 'mybooktable'); ?>: <input type="text" name="<?php echo(esc_attr($this->get_field_name('title'))); ?>" value="<?php echo(esc_attr($title)); ?>"></label>
101 </p>
102 <p>
103 <label><?php esc_attr_e('Book image size:', 'mybooktable'); ?></label><br>
104 <?php $sizes = array('small' =>__('Small', 'mybooktable'), 'medium' => __('Medium', 'mybooktable'), 'large' => __('Large', 'mybooktable')); ?>
105 <?php foreach($sizes as $size => $size_name) { ?>
106 <input type="radio" name="<?php echo(esc_attr($this->get_field_name('image_size'))); ?>" id="<?php echo(esc_attr($this->get_field_id('image_size').' '.$size)); ?>" value="<?php echo(esc_attr($size)); ?>" <?php echo($image_size == $size ? ' checked' : ''); ?> >
107 <label for="<?php echo(esc_attr($this->get_field_id('image_size').' '.$size)); ?>"><?php echo(esc_attr($size_name)); ?></label><br>
108 <?php } ?>
109 </p>
110 <p>
111 <input type="checkbox" class="checkbox" id="<?php echo esc_attr($this->get_field_id('show_blurb')); ?>" name="<?php echo(esc_attr($this->get_field_name('show_blurb'))); ?>"<?php checked($show_blurb); ?> />
112 <label for="<?php echo(esc_attr($this->get_field_id('show_blurb'))); ?>"><?php esc_attr_e('Show book blurb', 'mybooktable'); ?></label>
113 </p>
114 <p>
115 <input type="checkbox" class="checkbox" id="<?php echo esc_attr($this->get_field_id('use_shadowbox')); ?>" name="<?php echo(esc_attr($this->get_field_name('use_shadowbox'))); ?>"<?php checked($use_shadowbox); ?> />
116 <label for="<?php echo(esc_attr($this->get_field_id('use_shadowbox'))); ?>"><?php esc_attr_e('Use shadow box for Buy Buttons', 'mybooktable'); ?></label>
117 </p>
118 <p>
119 <label for="<?php echo(esc_attr($this->get_field_id('selectmode'))); ?>"><?php esc_attr_e('Choose how to select the featured books:', 'mybooktable'); ?></label>
120 <select class="mbt_featured_book_selectmode" name="<?php echo(esc_attr($this->get_field_name('selectmode'))); ?>" id="<?php echo(esc_attr($this->get_field_id('selectmode'))); ?>">
121 <option value="by_date"<?php selected($selectmode, 'by_date'); ?>><?php esc_attr_e('Most Recent Books', 'mybooktable'); ?></option>
122 <option value="manual_select"<?php selected($selectmode, 'manual_select'); ?>><?php esc_attr_e('Choose Manually', 'mybooktable'); ?></option>
123 <option value="random"<?php selected($selectmode, 'random'); ?>><?php esc_attr_e('Random Books', 'mybooktable'); ?></option>
124 </select>
125 </p>
126 <div class="mbt-featured-book-manual-selector" <?php echo($selectmode === 'manual_select' ? '' : 'style="display:none"'); ?>>
127 <label for="mbt-featured-book-selector"><?php esc_attr_e('Select Books:', 'mybooktable'); ?></label></br>
128 <select class="mbt-featured-book-selector">
129 <option value=""><?php esc_attr_e('-- Choose One --', 'mybooktable'); ?></option>
130 <?php
131 $wp_query = new WP_Query(array('post_type' => 'mbt_book', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => -1));
132 if(!empty($wp_query->posts)) {
133 foreach($wp_query->posts as $book) {
134 $shorttitle = substr($book->post_title, 0, 25).(strlen($book->post_title) > 25 ? '...' : '');
135 echo '<option value="'.esc_attr($book->ID).'">'.esc_attr($shorttitle).'</option>';
136 }
137 }
138 ?>
139 </select>
140 <input type="button" class="mbt-featured-book-adder button" value="<?php esc_attr_e('Add', 'mybooktable'); ?>" /><br>
141
142 <?php
143 echo('<ul class="mbt-featured-book-list">');
144 foreach($featured_books as $featured_book) {
145 $book = get_post($featured_book);
146 if($book) {
147 $shorttitle = substr($book->post_title, 0, 25).(strlen($book->post_title) > 25 ? '...' : '');
148 echo('<li data-id="'.esc_attr($book->ID).'" class="mbt-book">'.esc_attr($shorttitle).'<a class="mbt-book-remover">X</a></li>');
149 }
150 }
151 echo('</ul>');
152 ?>
153 <input class="mbt-featured-books" id="<?php echo(esc_attr($this->get_field_id('featured_books'))); ?>" name="<?php echo(esc_attr($this->get_field_name('featured_books'))); ?>" type="hidden" value="<?php echo(wp_json_encode($featured_books)); ?>">
154 </div>
155 <div class="mbt-featured-book-options" <?php echo($selectmode !== 'manual_select' ? '' : 'style="display:none"');
156 $nbooks = intval($num_books ? $num_books : 1);
157 ?>>
158 <p>
159 <label><?php esc_attr_e('Number of Books:', 'mybooktable'); ?>
160 <input type="number" name="<?php echo(esc_attr($this->get_field_name('num_books'))); ?>" value="<?php echo(esc_attr($nbooks)); ?>" min="1" max="10">
161 </label>
162 </p>
163 </div>
164 </div>
165 <?php
166 }
167 }
168
169 /*---------------------------------------------------------*/
170 /* Taxonomy Widget */
171 /*---------------------------------------------------------*/
172
173 class MBT_Taxonomies extends WP_Widget {
174 function __construct() {
175 $widget_ops = array('classname' => 'mbt_taxonomies', 'description' => __("A list of Authors, Genres, Series, or Tags.", 'mybooktable'));
176 parent::__construct('mbt_taxonomies', __('MyBookTable Taxonomy Widget', 'mybooktable'), $widget_ops);
177 }
178
179 function widget($args, $instance) {
180 extract($args);
181
182 $tax = empty($instance['tax']) ? 'mbt_author' : $instance['tax'];
183 if($tax === 'mbt_genre') {
184 $title = __('Genres', 'mybooktable');
185 } else if($tax === 'mbt_series') {
186 $title = __('Series', 'mybooktable');
187 } else if($tax === 'mbt_tag') {
188 $title = __('Tags', 'mybooktable');
189 } else {
190 $tax = 'mbt_author';
191 $title = __('Authors', 'mybooktable');
192 }
193 if(!empty($instance['title'])) { $title = $instance['title']; }
194 $title = apply_filters('widget_title', $title, $instance, $this->id_base);
195 $c = !empty($instance['count']) ? '1' : '0';
196
197 echo(wp_kses_post($before_widget));
198 if($title) {
199 $alltitle = $before_title.$title.$after_title;
200 echo(wp_kses_post($alltitle));
201 }
202
203 $args = array('orderby' => 'name', 'title_li' => '', 'show_count' => $c, 'taxonomy' => $tax);
204
205 echo('<ul>');
206 wp_list_categories(apply_filters('mbt_taxonomy_widget_args', $args));
207 echo('</ul>');
208
209 echo(wp_kses_post($after_widget));
210 }
211
212 function update($new_instance, $old_instance) {
213 $instance = $old_instance;
214 $instance['title'] = wp_strip_all_tags($new_instance['title']);
215 $instance['count'] = !empty($new_instance['count']) ? 1 : 0;
216 $instance['tax'] = $new_instance['tax'];
217 return $instance;
218 }
219
220 function form($instance) {
221 $instance = wp_parse_args((array)$instance, array('title' => ''));
222 $count = isset($instance['count']) ? (bool)$instance['count'] : false;
223 $tax = isset($instance['tax']) ? $instance['tax'] : '';
224
225 ?>
226 <div class="mbt-taxonomies-widget-editor">
227 <p>
228 <label><?php esc_attr_e('Title', 'mybooktable'); ?>: <input type="text" name="<?php echo(wp_kses_post($this->get_field_name('title'))); ?>" value="<?php echo(wp_kses_post($instance['title'])); ?>"></label>
229 </p>
230 <p>
231 <label for="<?php echo esc_attr($this->get_field_id('tax')); ?>"><?php esc_attr_e('Displayed taxonomy', 'mybooktable'); ?>:</label>
232 <select name="<?php echo esc_attr($this->get_field_name('tax')); ?>" id="<?php echo esc_attr($this->get_field_id('tax')); ?>" class="widefat">
233 <option value=""><?php esc_attr_e('-- Choose One --', 'mybooktable'); ?></option>
234 <option value="mbt_author"<?php selected($tax, 'mbt_author'); ?>><?php esc_attr_e('Authors', 'mybooktable'); ?></option>
235 <option value="mbt_genre"<?php selected($tax, 'mbt_genre'); ?>><?php esc_attr_e('Genres', 'mybooktable'); ?></option>
236 <option value="mbt_series"<?php selected($tax, 'mbt_series'); ?>><?php esc_attr_e('Series', 'mybooktable'); ?></option>
237 <option value="mbt_tag"<?php selected($tax, 'mbt_tag'); ?>><?php esc_attr_e('Tags', 'mybooktable'); ?></option>
238 </select>
239 </p>
240 <p>
241 <input type="checkbox" class="checkbox" id="<?php echo esc_attr($this->get_field_name('count')); ?>"<?php checked($count); ?> />
242 <label for="<?php echo esc_attr($this->get_field_id('count')); ?>" name="<?php echo esc_attr($this->get_field_name('count')); ?>"<?php checked($count); ?> />
243 <label for="<?php echo esc_attr($this->get_field_id('count')); ?>"><?php esc_attr_e('Show post counts', 'mybooktable'); ?></label>
244 </p>
245 </div>
246 <?php
247 }
248 }
249