| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
function mbt_metaboxes_init() { |
| 5 |
add_action('wp_ajax_mbt_buybuttons_metabox', 'mbt_buybuttons_metabox_ajax'); |
| 6 |
add_action('wp_ajax_mbt_book_image_preview', 'mbt_book_image_preview_ajax'); |
| 7 |
add_action('wp_ajax_mbt_endorsement_image_preview', 'mbt_endorsement_image_preview_ajax'); |
| 8 |
add_action('wp_ajax_mbt_isbn_preview', 'mbt_isbn_preview_ajax'); |
| 9 |
add_action('wp_ajax_mbt_asin_preview', 'mbt_asin_preview_ajax'); |
| 10 |
add_action('wp_ajax_mbt_overview_image_preview', 'mbt_overview_image_preview_ajax'); |
| 11 |
add_action('wp_ajax_mbt_main_author_url', 'mbt_main_author_url_ajax'); |
| 12 |
add_action('wp_ajax_mbt_change_booksections_displaymode', 'mbt_change_booksections_displaymode_ajax'); |
| 13 |
add_action('admin_enqueue_scripts', 'mbt_enqueue_metabox_js'); |
| 14 |
|
| 15 |
add_action('save_post', 'mbt_save_book_blurb_metabox'); |
| 16 |
add_action('save_post', 'mbt_save_metadata_metabox'); |
| 17 |
add_action('save_post', 'mbt_save_buybuttons_metabox'); |
| 18 |
add_action('save_post', 'mbt_save_series_order_metabox'); |
| 19 |
add_action('save_post', 'mbt_save_endorsements_metabox'); |
| 20 |
add_action('save_post', 'mbt_save_bookclub_metabox'); |
| 21 |
add_action('save_post', 'mbt_save_audiobook_metabox'); |
| 22 |
add_action('save_post', 'mbt_save_overview_metabox'); |
| 23 |
add_action('save_post', 'mbt_save_display_mode_field'); |
| 24 |
add_action('save_post', 'mbt_save_post_author_field'); |
| 25 |
add_action('save_post', 'mbt_save_sectionsorting_metabox'); |
| 26 |
|
| 27 |
add_action('add_meta_boxes', 'mbt_add_metaboxes', 9); |
| 28 |
add_action('post_submitbox_misc_actions', 'mbt_add_post_author_field'); |
| 29 |
add_action('post_submitbox_misc_actions', 'mbt_add_display_mode_field'); |
| 30 |
} |
| 31 |
add_action('mbt_init', 'mbt_metaboxes_init'); |
| 32 |
|
| 33 |
function mbt_add_metaboxes() { |
| 34 |
add_meta_box('mbt_blurb', __('Book Blurb', 'mybooktable'), 'mbt_book_blurb_metabox', 'mbt_book', 'normal', 'high'); |
| 35 |
add_meta_box('mbt_metadata', __('Book Details', 'mybooktable'), 'mbt_metadata_metabox', 'mbt_book', 'normal', 'high'); |
| 36 |
add_meta_box('mbt_buybuttons', __('Buy Buttons', 'mybooktable'), 'mbt_buybuttons_metabox', 'mbt_book', 'normal', 'high'); |
| 37 |
add_meta_box('mbt_overview', __('About the Book', 'mybooktable'), 'mbt_overview_metabox', 'mbt_book', 'normal', 'high'); |
| 38 |
add_meta_box('mbt_endorsements', __('Endorsements', 'mybooktable'), 'mbt_endorsements_metabox', 'mbt_book', 'normal', 'high'); |
| 39 |
add_meta_box('mbt_bookclub', __('Book Club Resources', 'mybooktable'), 'mbt_bookclub_metabox', 'mbt_book', 'normal', 'low'); |
| 40 |
add_meta_box('mbt_audiobook', __('Audio Book Resources', 'mybooktable'), 'mbt_audiobook_metabox', 'mbt_book', 'normal', 'low'); |
| 41 |
add_meta_box('mbt_series_order', __('Series Order', 'mybooktable'), 'mbt_series_order_metabox', 'mbt_book', 'side', 'default'); |
| 42 |
add_meta_box('mbt_sectionsorting', __('Section Order', 'mybooktable'), 'mbt_sectionsorting_metabox', 'mbt_book', 'side', 'default'); |
| 43 |
add_filter('postbox_classes_mbt_book_mbt_sectionsorting', 'mbt_minify_sectionsorting_metabox'); |
| 44 |
} |
| 45 |
|
| 46 |
function mbt_enqueue_metabox_js() { |
| 47 |
if(!mbt_is_mbt_admin_page()) { return; } |
| 48 |
|
| 49 |
wp_enqueue_script('mbt-metaboxes', plugins_url('js/metaboxes.js', dirname(__FILE__)), array('jquery'), MBT_VERSION); |
| 50 |
wp_enqueue_script('mbt-star-ratings', plugins_url('js/lib/jquery.rating.js', dirname(__FILE__)), array('jquery'), MBT_VERSION); |
| 51 |
wp_enqueue_script('mbt-colorpicker', plugins_url('js/lib/spectrum.js', dirname(__FILE__)), array('jquery'), MBT_VERSION); |
| 52 |
wp_enqueue_style('mbt-colorpicker', plugins_url('css/lib/spectrum.css', dirname(__FILE__)), array(), MBT_VERSION); |
| 53 |
add_action('admin_head', 'mbt_override_authors_metabox'); |
| 54 |
} |
| 55 |
|
| 56 |
function mbt_minify_sectionsorting_metabox($classes) { |
| 57 |
array_push($classes, 'closed'); |
| 58 |
return $classes; |
| 59 |
} |
| 60 |
|
| 61 |
function mbt_should_not_save_metabox($post_id) { |
| 62 |
return ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || get_post_status($post_id) == 'auto-draft' || get_post_type($post_id) !== 'mbt_book'); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
/*---------------------------------------------------------*/ |
| 68 |
/* Override Authors Metabox */ |
| 69 |
/*---------------------------------------------------------*/ |
| 70 |
|
| 71 |
function mbt_override_authors_metabox() { |
| 72 |
?> |
| 73 |
<script type="text/javascript"> |
| 74 |
jQuery(document).ready(function() { |
| 75 |
jQuery('#mbt_authordiv .inside').append(jQuery('<p class="description"><a href="<?php echo(esc_url(admin_url('edit-tags.php?taxonomy=mbt_author&post_type=mbt_book'))); ?>" target="_blank">'+mbt_admin_pages_i18n.set_author_priority+'</a></p>')); |
| 76 |
jQuery('#mbt_authordiv .inside').append(jQuery('<p class="description"><a id="mbt_main_author_link" data-nonce="<?php echo(esc_attr(wp_create_nonce('mbt-ajax-nonce'))); ?>" href="<?php echo(esc_url(admin_url('edit-tags.php?taxonomy=mbt_author&post_type=mbt_book'))); ?>" target="_blank">'+mbt_admin_pages_i18n.enter_main_author_bio+'</a></p>')); |
| 77 |
}); |
| 78 |
</script> |
| 79 |
<?php |
| 80 |
} |
| 81 |
|
| 82 |
function mbt_main_author_url_ajax() { |
| 83 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), 'mbt-ajax-nonce')) { |
| 84 |
die(); |
| 85 |
} |
| 86 |
$main_author = NULL; |
| 87 |
if(!empty($_REQUEST['authors'])) { |
| 88 |
$authors = sanitize_text_field(wp_unslash($_REQUEST['authors'])); |
| 89 |
if(is_array($authors)) { |
| 90 |
$sortfunc = function($a, $b) { |
| 91 |
$a = mbt_get_author_priority(intval($a)); |
| 92 |
$b = mbt_get_author_priority(intval($b)); |
| 93 |
return ($a > $b) ? -1 : (($a < $b) ? 1 : 0); |
| 94 |
}; |
| 95 |
usort($authors, $sortfunc); |
| 96 |
$main_author = $authors[0]; |
| 97 |
} |
| 98 |
} |
| 99 |
if(empty($main_author)) { |
| 100 |
echo(esc_url(admin_url('edit-tags.php?taxonomy=mbt_author&post_type=mbt_book'))); |
| 101 |
} else { |
| 102 |
echo(esc_url(admin_url('term.php?taxonomy=mbt_author&post_type=mbt_book&tag_ID='.$main_author))); |
| 103 |
} |
| 104 |
die(); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
/*---------------------------------------------------------*/ |
| 110 |
/* Publish Metabox */ |
| 111 |
/*---------------------------------------------------------*/ |
| 112 |
|
| 113 |
function mbt_add_display_mode_field($post) { |
| 114 |
if(get_post_type($post->ID) !== 'mbt_book') { return; } |
| 115 |
|
| 116 |
$display_modes = mbt_get_book_display_modes(); |
| 117 |
if(empty($display_modes)) { return; } |
| 118 |
$current_mode = get_post_meta($post->ID, 'mbt_display_mode', true); |
| 119 |
if(empty($display_modes[$current_mode])) { $current_mode = mbt_get_default_book_display_mode(); } |
| 120 |
?> |
| 121 |
<div class="misc-pub-section misc-pub-display-mode" id="mbt_display_mode_field"> |
| 122 |
<input type="hidden" id="mbt_display_modes" value='<?php echo(esc_js(str_replace('\'', ''', wp_json_encode($display_modes)))); ?>'> |
| 123 |
<label for="mbt_display_mode">Display Mode:</span> |
| 124 |
<select name="mbt_display_mode" id="mbt_display_mode"> |
| 125 |
<?php foreach($display_modes as $display_mode_id => $display_mode) { ?> |
| 126 |
<option value="<?php echo(esc_attr($display_mode_id)); ?>" <?php esc_attr(selected($display_mode_id, $current_mode)); ?> ><?php echo(esc_attr($display_mode['name'])); ?></option> |
| 127 |
<?php } ?> |
| 128 |
</select> |
| 129 |
</div> |
| 130 |
<?php |
| 131 |
} |
| 132 |
|
| 133 |
function mbt_save_display_mode_field($post_id) { |
| 134 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), plugin_basename(__FILE__))) { return; } |
| 135 |
if(mbt_should_not_save_metabox($post_id)) { return; } |
| 136 |
if(isset($_REQUEST['mbt_display_mode'])) { update_post_meta($post_id, 'mbt_display_mode', sanitize_text_field(wp_unslash($_REQUEST['mbt_display_mode']))); } |
| 137 |
} |
| 138 |
|
| 139 |
function mbt_add_post_author_field($post) { |
| 140 |
if(get_post_type($post->ID) !== 'mbt_book') { return; } |
| 141 |
?> |
| 142 |
<div class="misc-pub-section misc-pub-post-author" id="mbt_post_author_field"> |
| 143 |
<label for="mbt_post_author">Post Author:</span> |
| 144 |
<select name="mbt_post_author" id="mbt_post_author"> |
| 145 |
<?php foreach(get_users() as $author) { ?> |
| 146 |
<option value="<?php echo(esc_attr($author->ID)); ?>" <?php esc_attr(selected($author->ID, $post->post_author)); ?> ><?php echo(esc_attr($author->display_name)); ?> (<?php echo(esc_attr($author->user_login)); ?>)</option> |
| 147 |
<?php } ?> |
| 148 |
</select> |
| 149 |
</div> |
| 150 |
<?php |
| 151 |
} |
| 152 |
|
| 153 |
function mbt_save_post_author_field($post_id) { |
| 154 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), plugin_basename(__FILE__))) { return; } |
| 155 |
if(mbt_should_not_save_metabox($post_id)) { return; } |
| 156 |
if(isset($_REQUEST['mbt_post_author'])) { global $wpdb; $wpdb->update($wpdb->posts, array('post_author' => intval($_REQUEST['mbt_post_author'])), array('ID' => $post_id), array('%d'), array('%d')); } |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
/*---------------------------------------------------------*/ |
| 162 |
/* Book Blurb Metabox */ |
| 163 |
/*---------------------------------------------------------*/ |
| 164 |
|
| 165 |
function mbt_book_blurb_metabox($post) { |
| 166 |
?> |
| 167 |
<div class="mbt_book_teaser_field" data-mbt-supports="teaser"> |
| 168 |
<label for="mbt_book_teaser"><?php esc_attr_e('Teaser Text:', 'mybooktable'); ?></label> |
| 169 |
<div class="mbt_book_teaser_input"><input type="text" name="mbt_book_teaser" id="mbt_book_teaser" value="<?php echo(esc_html(get_post_meta($post->ID, 'mbt_book_teaser', true))); ?>"></div> |
| 170 |
</div> |
| 171 |
<?php wp_editor(wp_specialchars_decode($post->post_excerpt, ENT_QUOTES), 'excerpt', array('media_buttons' => false, 'quicktags' => true, 'editor_height' => 150)); ?> |
| 172 |
<p class="description"> |
| 173 |
<?php |
| 174 |
/* translators: %s: link to tutorial */ |
| 175 |
echo sprintf(esc_html__('Book Blurbs are hand-crafted summaries of your book. The goal of a book blurb is to convince strangers that they need buy your book in 100 words or less. Answer the question "why would I want to read this book?" %s', 'mybooktable'), '<a href="'.esc_url(admin_url('admin.php?page=mbt_help&mbt_video_tutorial=book_blurbs')).'" target="_blank">Learn more about writing your book blurb.</a>'); |
| 176 |
|
| 177 |
?> |
| 178 |
</p> |
| 179 |
<?php |
| 180 |
} |
| 181 |
|
| 182 |
function mbt_save_book_blurb_metabox($post_id) { |
| 183 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), plugin_basename(__FILE__))) { return; } |
| 184 |
if(mbt_should_not_save_metabox($post_id)) { return; } |
| 185 |
if(isset($_REQUEST['mbt_book_teaser'])) { update_post_meta($post_id, 'mbt_book_teaser', sanitize_text_field(wp_unslash($_REQUEST['mbt_book_teaser']))); } |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
/*---------------------------------------------------------*/ |
| 191 |
/* Overview Metabox */ |
| 192 |
/*---------------------------------------------------------*/ |
| 193 |
|
| 194 |
function mbt_overview_metabox($post) { |
| 195 |
?> |
| 196 |
<div class="mbt_overview_image_field" data-mbt-supports="overview_image"> |
| 197 |
<label for="mbt_overview_image"><?php esc_attr_e('Image:', 'mybooktable'); ?></label> |
| 198 |
<div class="mbt_overview_image_preview"><?php echo(wp_kses_post(mbt_get_overview_image_preview(get_post_meta($post->ID, 'mbt_overview_image', true)))); ?></div> |
| 199 |
<input type="hidden" id="mbt_overview_image" name="mbt_overview_image" value="<?php echo(wp_kses_post(get_post_meta($post->ID, 'mbt_overview_image', true))); ?>" /> |
| 200 |
<input class="button mbt_upload_button" data-upload-target="mbt_overview_image" data-upload-property="id" type="button" value="<?php esc_attr_e('Choose', 'mybooktable'); ?>" /> |
| 201 |
<input class="button mbt_upload_clear_button" data-upload-target="mbt_overview_image" type="button" value="X" /> |
| 202 |
<div id="mbt_overview_nonce" data-nonce="<?php echo(esc_attr(wp_create_nonce('mbt-ajax-nonce'))); ?>"></div> |
| 203 |
</div> |
| 204 |
<?php |
| 205 |
wp_editor($post->post_content, 'content', array('dfw' => true, 'tabfocus_elements' => 'sample-permalink,post-preview', 'editor_height' => 360) ); |
| 206 |
echo('<p class="description">'); |
| 207 |
esc_attr_e('The About the Book section is a longer description of your book. This typically includes all the text from the back cover of the book plus, endorsements and any other promotional materials from interior flaps or initial pages. This is also a good place to embed a book trailer if you have one.', 'mybooktable'); |
| 208 |
echo('</p>'); |
| 209 |
} |
| 210 |
|
| 211 |
function mbt_save_overview_metabox($post_id) { |
| 212 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), plugin_basename(__FILE__))) { return; } |
| 213 |
if(mbt_should_not_save_metabox($post_id)) { return; } |
| 214 |
if(isset($_REQUEST['mbt_overview_image'])) { update_post_meta($post_id, 'mbt_overview_image', sanitize_text_field(wp_unslash($_REQUEST['mbt_overview_image']))); } |
| 215 |
} |
| 216 |
|
| 217 |
function mbt_get_overview_image_preview($image_id) { |
| 218 |
list($src, $width, $height) = wp_get_attachment_image_src($image_id); |
| 219 |
return $src ? basename($src) : 'None'; |
| 220 |
} |
| 221 |
|
| 222 |
function mbt_overview_image_preview_ajax() { |
| 223 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), 'mbt-ajax-nonce')) { die(); } |
| 224 |
if(isset($_REQUEST['image_id'])) { echo(wp_kses_post(mbt_get_overview_image_preview(intval($_REQUEST['image_id'])))); } |
| 225 |
die(); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
/*---------------------------------------------------------*/ |
| 231 |
/* Metadata Metabox */ |
| 232 |
/*---------------------------------------------------------*/ |
| 233 |
|
| 234 |
function mbt_book_image_preview_ajax() { |
| 235 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), 'mbt-ajax-nonce')) { die(); } |
| 236 |
if(isset($_REQUEST['image_id'])) { |
| 237 |
$image = wp_get_attachment_image_src(intval($_REQUEST['image_id']), 'mbt_book_image'); |
| 238 |
list($src, $width, $height) = $image ? $image : mbt_get_placeholder_image_src(); |
| 239 |
echo(wp_kses_post($src)); |
| 240 |
} |
| 241 |
die(); |
| 242 |
} |
| 243 |
|
| 244 |
function mbt_isbn_preview_ajax() { |
| 245 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), 'mbt-ajax-nonce')) { die(); } |
| 246 |
if(isset($_REQUEST['data'])){ |
| 247 |
echo(wp_kses_post(mbt_isbn_preview_feedback(sanitize_text_field(wp_unslash($_REQUEST['data']))))); |
| 248 |
} |
| 249 |
die(); |
| 250 |
} |
| 251 |
|
| 252 |
function mbt_isbn_preview_feedback($data) { |
| 253 |
$output = ''; |
| 254 |
if(!is_array($data)) { return; } |
| 255 |
$isbn = $data['mbt_unique_id_isbn']; |
| 256 |
$post_id = $data['mbt_post_id']; |
| 257 |
|
| 258 |
if(empty($isbn)) { |
| 259 |
if(get_post_status($post_id) === 'publish' and (mbt_get_setting('reviews_type') === 'goodreads' or (mbt_get_setting('reviews_type') === 'amazon' and get_post_meta($post_id, 'mbt_unique_id_asin', true) === ''))) { |
| 260 |
$output = '<span class="mbt_admin_message_warning">'.__('Cannot show reviews without a valid ISBN.', 'mybooktable').'</span>'; |
| 261 |
} |
| 262 |
} else { |
| 263 |
$matches = array(); |
| 264 |
preg_match("/^([0-9][0-9\-]{8,}[0-9Xx])$/", $isbn, $matches); |
| 265 |
if(!empty($matches[1])) { |
| 266 |
$filtered_isbn = preg_replace("/[^0-9Xx]/", "", $isbn); |
| 267 |
$output = '<span class="mbt_admin_message_success">'.__('Valid ISBN', 'mybooktable').' <a href="https://www.isbnsearch.org/isbn/'.$filtered_isbn.'" target="_blank">'.__('(verify book)', 'mybooktable').'</a></span>'; |
| 268 |
} else { |
| 269 |
$output = '<span class="mbt_admin_message_failure">'.__('Invalid ISBN', 'mybooktable').'</span>'; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
return $output; |
| 274 |
} |
| 275 |
|
| 276 |
function mbt_asin_preview_ajax() { |
| 277 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), 'mbt-ajax-nonce')) { die(); } |
| 278 |
if(isset($_REQUEST['data'])){ |
| 279 |
echo(wp_kses_post(mbt_asin_preview_feedback(sanitize_text_field(wp_unslash($_REQUEST['data']))))); |
| 280 |
} |
| 281 |
die(); |
| 282 |
} |
| 283 |
|
| 284 |
function mbt_asin_preview_feedback($data) { |
| 285 |
$output = ''; |
| 286 |
if(!is_array($data)) { return; } |
| 287 |
$asin = $data['mbt_unique_id_asin']; |
| 288 |
$post_id = $data['mbt_post_id']; |
| 289 |
|
| 290 |
if(empty($asin)) { |
| 291 |
if(get_post_status($post_id) === 'publish' and get_post_meta($post_id, 'mbt_show_instant_preview', true) === 'yes') { |
| 292 |
$output = '<span class="mbt_admin_message_warning">'.__('Cannot show Kindle Instant Preview without a valid ASIN.', 'mybooktable').' <a href="https://www.amazon.com/gp/help/customer/display.html?nodeId=200202190#find_asins" target="_blank">'.__('(Find your ASIN)', 'mybooktable').'</a>'.'</span>'; |
| 293 |
} |
| 294 |
} else { |
| 295 |
$matches = array(); |
| 296 |
preg_match("/^([A-Za-z0-9]{10})$/", $asin, $matches); |
| 297 |
if(!empty($matches[1])) { |
| 298 |
$output = '<span class="mbt_admin_message_success">'.__('Valid ASIN', 'mybooktable').' <a href="https://www.amazon.com/dp/'.$asin.'" target="_blank">'.__('(verify book)', 'mybooktable').'</a></span>'; |
| 299 |
} else { |
| 300 |
$output = '<span class="mbt_admin_message_failure">'.__('Invalid ASIN', 'mybooktable').'</span>'; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
return $output; |
| 305 |
} |
| 306 |
|
| 307 |
function mbt_metadata_text($post_id, $field_id, $data) { |
| 308 |
$value = get_post_meta($post_id, $field_id, true); |
| 309 |
return '<input type="text" name="'.$field_id.'" id="'.$field_id.'" value="'.esc_attr($value).'" />'; |
| 310 |
} |
| 311 |
|
| 312 |
function mbt_metadata_checkbox($post_id, $field_id, $data) { |
| 313 |
$value = get_post_meta($post_id, $field_id, true); |
| 314 |
if(!empty($data['default']) and $value === '') { $value = $data['default']; } |
| 315 |
return '<input type="checkbox" name="'.$field_id.'" id="'.$field_id.'" '.checked($value, 'yes', false).'>'; |
| 316 |
} |
| 317 |
|
| 318 |
function mbt_metadata_kindle_instant_preview($post_id, $field_id, $data) { |
| 319 |
$output = mbt_metadata_checkbox($post_id, $field_id, $data); |
| 320 |
$asin = get_post_meta($post_id, 'mbt_unique_id_asin', true); |
| 321 |
$output .= empty($asin) ? '<br><span class="mbt_admin_message_warning" id="mbt_show_instant_preview_asin_warning">'.__('Cannot show Kindle Instant Preview without a valid ASIN.', 'mybooktable').' <a href="https://www.amazon.com/gp/help/customer/display.html?nodeId=200202190#find_asins" target="_blank">'.__('(Find your ASIN)', 'mybooktable').'</a>'.'</span>' : ''; |
| 322 |
return $output; |
| 323 |
} |
| 324 |
|
| 325 |
function mbt_metadata_upload($post_id, $field_id, $data) { |
| 326 |
$output = ''; |
| 327 |
$output .= '<input type="text" name="'.$field_id.'" id="'.$field_id.'" value="'.esc_attr(get_post_meta($post_id, $field_id, true)).'" /> '; |
| 328 |
$output .= '<input class="button mbt_upload_button" data-upload-target="'.$field_id.'" data-upload-title="'.__('Choose Sample', 'mybooktable').'" type="button" value="'.__('Upload', 'mybooktable').'" />'; |
| 329 |
return $output; |
| 330 |
} |
| 331 |
|
| 332 |
function mbt_metadata_star_rating($post_id, $field_id, $data) { |
| 333 |
$star_rating = get_post_meta($post_id, 'mbt_star_rating', true); |
| 334 |
$output = ''; |
| 335 |
$output .= '<div class="mbt_star_rating_container">'; |
| 336 |
$output .= '<input name="mbt_star_rating" value="1" type="radio" class="mbt-star" '.checked($star_rating, 1, false).'/>'; |
| 337 |
$output .= '<input name="mbt_star_rating" value="2" type="radio" class="mbt-star" '.checked($star_rating, 2, false).'/>'; |
| 338 |
$output .= '<input name="mbt_star_rating" value="3" type="radio" class="mbt-star" '.checked($star_rating, 3, false).'/>'; |
| 339 |
$output .= '<input name="mbt_star_rating" value="4" type="radio" class="mbt-star" '.checked($star_rating, 4, false).'/>'; |
| 340 |
$output .= '<input name="mbt_star_rating" value="5" type="radio" class="mbt-star" '.checked($star_rating, 5, false).'/>'; |
| 341 |
$output .= '</div>'; |
| 342 |
return $output; |
| 343 |
} |
| 344 |
|
| 345 |
function mbt_metadata_colorpicker($post_id, $field_id, $data) { |
| 346 |
$output = ''; |
| 347 |
$output .= '<input type="text" class="mbt-colorpicker" name="'.$field_id.'" id="'.$field_id.'" value="'.esc_attr(get_post_meta($post_id, $field_id, true)).'"'.(empty($data['default']) ? '' : ' data-default-color="'.esc_attr($data['default']).'"').'/>'; |
| 348 |
$output .= '<input class="button mbt-colorpicker-clear" type="button" value="'.__('Clear', 'mybooktable').'" />'; |
| 349 |
return $output; |
| 350 |
} |
| 351 |
|
| 352 |
function mbt_get_metadata_fields() { |
| 353 |
return array( |
| 354 |
'Book Samples' => array( |
| 355 |
'mbt_show_instant_preview' => array( |
| 356 |
'type' => 'mbt_metadata_kindle_instant_preview', |
| 357 |
'name' => __('Kindle Instant Preview', 'mybooktable'), |
| 358 |
'desc' => __('Displays a free instant preview of your book from Amazon.', 'mybooktable'), |
| 359 |
'default' => 'yes', |
| 360 |
), |
| 361 |
'mbt_sample_url' => array( |
| 362 |
'type' => 'mbt_metadata_upload', |
| 363 |
'name' => __('Sample Chapter', 'mybooktable'), |
| 364 |
'desc' => __('Upload a sample chapter from your book to give viewers a preview. We recommend using a .pdf format for the sample chapter.', 'mybooktable'), |
| 365 |
), |
| 366 |
'mbt_sample_audio' => array( |
| 367 |
'type' => 'mbt_metadata_upload', |
| 368 |
'name' => __('Audio Sample', 'mybooktable'), |
| 369 |
'desc' => __('Upload a sample from your audiobook to give viewers a preview. We recommend using a .mp3 format for the sample.', 'mybooktable'), |
| 370 |
), |
| 371 |
'mbt_sample_video' => array( |
| 372 |
'type' => 'mbt_metadata_text', |
| 373 |
'name' => __('Book Trailer', 'mybooktable'), |
| 374 |
'desc' => __('Paste in the URL of a YouTube or Vimeo video that shows off your book.', 'mybooktable'), |
| 375 |
), |
| 376 |
), |
| 377 |
'Price' => array( |
| 378 |
'mbt_price' => array( |
| 379 |
'type' => 'mbt_metadata_text', |
| 380 |
'name' => __('List Price', 'mybooktable'), |
| 381 |
'desc' => __('You can typically find the list price just above the ISBN barcode on the back cover of the book.', 'mybooktable'), |
| 382 |
), |
| 383 |
'mbt_sale_price' => array( |
| 384 |
'type' => 'mbt_metadata_text', |
| 385 |
'name' => __('Sale Price', 'mybooktable'), |
| 386 |
'desc' => __('Setting a sale price will cross out the normal price and show the sale price prominently.', 'mybooktable'), |
| 387 |
'supports' => 'sale_price', |
| 388 |
), |
| 389 |
'mbt_ebook_price' => array( |
| 390 |
'type' => 'mbt_metadata_text', |
| 391 |
'name' => __('eBook Price', 'mybooktable'), |
| 392 |
'desc' => __('If your book is available in multiple formats, you can use this to display the eBook price.', 'mybooktable'), |
| 393 |
), |
| 394 |
'mbt_audiobook_price' => array( |
| 395 |
'type' => 'mbt_metadata_text', |
| 396 |
'name' => __('Audiobook Price', 'mybooktable'), |
| 397 |
'desc' => __('If your book is available in multiple formats, you can use this to display the audiobook price.', 'mybooktable'), |
| 398 |
), |
| 399 |
), |
| 400 |
'Publisher' => array( |
| 401 |
'mbt_publisher_name' => array( |
| 402 |
'type' => 'mbt_metadata_text', |
| 403 |
'name' => __('Publisher Name', 'mybooktable'), |
| 404 |
), |
| 405 |
'mbt_publisher_url' => array( |
| 406 |
'type' => 'mbt_metadata_text', |
| 407 |
'name' => __('Publisher URL', 'mybooktable'), |
| 408 |
'desc' => __('Setting a publisher URL will turn the "Publisher Name" into a link to this address.', 'mybooktable'), |
| 409 |
), |
| 410 |
'mbt_publication_year' => array( |
| 411 |
'type' => 'mbt_metadata_text', |
| 412 |
'name' => __('Publication Year', 'mybooktable'), |
| 413 |
), |
| 414 |
), |
| 415 |
'Other' => array( |
| 416 |
'mbt_star_rating' => array( |
| 417 |
'type' => 'mbt_metadata_star_rating', |
| 418 |
'name' => __('Star Rating', 'mybooktable'), |
| 419 |
), |
| 420 |
'mbt_book_format' => array( |
| 421 |
'type' => 'mbt_metadata_text', |
| 422 |
'name' => __('Book Format', 'mybooktable'), |
| 423 |
'desc' => __('What format is the book presented in?', 'mybooktable'), |
| 424 |
), |
| 425 |
'mbt_book_length' => array( |
| 426 |
'type' => 'mbt_metadata_text', |
| 427 |
'name' => __('Book Length', 'mybooktable'), |
| 428 |
'desc' => __('Is this book a short story, a complete novel, or an epic drama?', 'mybooktable'), |
| 429 |
), |
| 430 |
'mbt_narrator' => array( |
| 431 |
'type' => 'mbt_metadata_text', |
| 432 |
'name' => __('Narrator', 'mybooktable'), |
| 433 |
'desc' => __('If applicable, who is the book narrated by?', 'mybooktable'), |
| 434 |
), |
| 435 |
'mbt_illustrator' => array( |
| 436 |
'type' => 'mbt_metadata_text', |
| 437 |
'name' => __('Illustrator', 'mybooktable'), |
| 438 |
'desc' => __('If applicable, who is the book illustrated by?', 'mybooktable'), |
| 439 |
), |
| 440 |
), |
| 441 |
'Section Colors' => array( |
| 442 |
'supports' => 'section_colors', |
| 443 |
'mbt_bg_color' => array( |
| 444 |
'type' => 'mbt_metadata_colorpicker', |
| 445 |
'name' => __('Background Color', 'mybooktable'), |
| 446 |
'default' => '#fff', |
| 447 |
), |
| 448 |
'mbt_bg_color_alt' => array( |
| 449 |
'type' => 'mbt_metadata_colorpicker', |
| 450 |
'name' => __('Alternate Background Color', 'mybooktable'), |
| 451 |
'default' => '#ddd', |
| 452 |
), |
| 453 |
'mbt_button_color' => array( |
| 454 |
'type' => 'mbt_metadata_colorpicker', |
| 455 |
'name' => __('Button Color', 'mybooktable'), |
| 456 |
'default' => '#64ACF3', |
| 457 |
), |
| 458 |
), |
| 459 |
); |
| 460 |
} |
| 461 |
|
| 462 |
function mbt_metadata_metabox($post) { |
| 463 |
$nonce = wp_create_nonce('mbt-ajax-nonce'); |
| 464 |
$metadata = mbt_get_metadata_fields(); |
| 465 |
?> |
| 466 |
<input type="hidden" id="mbt_post_id" value="<?php echo(esc_attr($post->ID)); ?>" /> |
| 467 |
<div id="mbt_metadata_nonce" data-nonce="<?php echo(esc_attr($nonce)); ?>"></div> |
| 468 |
<table> |
| 469 |
<tr> |
| 470 |
<td rowspan="3" class="mbt_cover_image_container"> |
| 471 |
<h4 class="mbt-cover-image-title"><?php esc_attr_e('Book Cover Image', 'mybooktable'); ?></h4> |
| 472 |
<?php mbt_the_book_image(); ?><br> |
| 473 |
<input type="hidden" id="mbt_book_image_id" name="mbt_book_image_id" value="<?php echo(esc_attr(get_post_meta($post->ID, "mbt_book_image_id", true))); ?>" /> |
| 474 |
<input id="mbt_set_book_image_button" class="button mbt_upload_button" data-upload-target="mbt_book_image_id" data-upload-property="id" data-upload-title="<?php esc_attr_e('Book Cover Image', 'mybooktable'); ?>" type="button" value="<?php esc_attr_e('Set cover image', 'mybooktable'); ?>" /> |
| 475 |
<input id="mbt_clear_book_image_button" class="button mbt_upload_clear_button" data-upload-target="mbt_book_image_id" type="button" value="X" /> |
| 476 |
</td> |
| 477 |
<td class="mbt_unique_identifier_container"> |
| 478 |
<label><?php esc_attr_e('ISBN', 'mybooktable'); ?>:</label> |
| 479 |
<?php $isbn = get_post_meta($post->ID, 'mbt_unique_id_isbn', true); ?> |
| 480 |
<div class="mbt_unique_identifier_input"> |
| 481 |
<input type="text" name="mbt_unique_id_isbn" id="mbt_unique_id_isbn" value="<?php echo(esc_attr($isbn)); ?>" class="mbt_feedback_refresh mbt_feedback_colorize" data-refresh-action="mbt_isbn_preview" data-refresh-nonce="<?php echo(esc_attr($nonce)); ?>" data-element="mbt_unique_id_isbn,mbt_post_id"/> |
| 482 |
<div class="mbt_feedback"><?php echo(wp_kses_post(mbt_isbn_preview_feedback(array('mbt_unique_id_isbn' => $isbn, 'mbt_post_id' => $post->ID)))); ?></div> |
| 483 |
</div> |
| 484 |
<p class="description"><?php esc_attr_e('This is the International Standard Book Number, used to populate GoodReads and Amazon reviews.', 'mybooktable'); ?></p> |
| 485 |
</td> |
| 486 |
</tr> |
| 487 |
<tr> |
| 488 |
<td class="mbt_unique_identifier_container"> |
| 489 |
<label><?php esc_attr_e('ASIN', 'mybooktable'); ?>:</label> |
| 490 |
<?php $asin = get_post_meta($post->ID, 'mbt_unique_id_asin', true); ?> |
| 491 |
<div class="mbt_unique_identifier_input"> |
| 492 |
<input type="text" name="mbt_unique_id_asin" id="mbt_unique_id_asin" value="<?php echo(esc_attr($asin)); ?>" class="mbt_feedback_refresh mbt_feedback_colorize" data-refresh-action="mbt_asin_preview" data-refresh-nonce="<?php echo(esc_attr($nonce)); ?>" data-element="mbt_unique_id_asin,mbt_post_id"/> |
| 493 |
<div class="mbt_feedback"><?php echo(wp_kses_post(mbt_asin_preview_feedback(array('mbt_unique_id_asin' => $asin, 'mbt_post_id' => $post->ID)))); ?></div> |
| 494 |
</div> |
| 495 |
<p class="description"><?php esc_attr_e('This is the Amazon Standard Identification Number, used to populate Amazon reviews and Kindle Instant Preview.', 'mybooktable'); ?></p> |
| 496 |
</td> |
| 497 |
</tr> |
| 498 |
<tr> |
| 499 |
<td class="mbt_show_unique_identifier_container"> |
| 500 |
<?php $show_unique_id = get_post_meta($post->ID, 'mbt_show_unique_id', true) !== 'no' ? 'yes' : 'no'; ?> |
| 501 |
<input type="checkbox" name="mbt_show_unique_id" id="mbt_show_unique_id" <?php checked($show_unique_id, 'yes'); ?> > |
| 502 |
<label for="mbt_show_unique_id"><?php esc_attr_e('Show ISBN/ASIN on book page', 'mybooktable'); ?></label> |
| 503 |
</td> |
| 504 |
</tr> |
| 505 |
</table> |
| 506 |
<div class="mbt_metadata_fields"> |
| 507 |
<?php foreach($metadata as $section_name => $section) { |
| 508 |
echo('<div class="mbt-accordion"'.(empty($section['supports']) ? '' : ' data-mbt-supports="'.esc_attr($section['supports']).'"').'><h4>'.esc_attr($section_name).'</h4><div>'); |
| 509 |
foreach($section as $field_id => $field_data) { |
| 510 |
if($field_id == 'supports') { continue; } |
| 511 |
echo('<div class="mbt_metadata_field mbt_metadata_field_'.esc_attr($field_id).'"'.(empty($field_data['supports']) ? '' : ' data-mbt-supports="'.esc_attr($field_data['supports']).'"').'>'); |
| 512 |
echo('<label for="'.esc_attr($field_id).'">'.esc_attr($field_data['name']).':</label>'); |
| 513 |
echo(wp_kses_post(call_user_func_array($field_data['type'], array($post->ID, $field_id, $field_data)))); |
| 514 |
if(!empty($field_data['desc'])) { echo(wp_kses_post('<p class="description">'.$field_data['desc'].'</p>')); } |
| 515 |
echo('</div>'); |
| 516 |
} |
| 517 |
echo('</div></div>'); |
| 518 |
} ?> |
| 519 |
</div> |
| 520 |
<?php |
| 521 |
} |
| 522 |
|
| 523 |
function mbt_save_metadata_metabox($post_id) { |
| 524 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), plugin_basename(__FILE__))) { return; } |
| 525 |
if(mbt_should_not_save_metabox($post_id)) { return; } |
| 526 |
|
| 527 |
if(get_post_type($post_id) == 'mbt_book') { |
| 528 |
if(isset($_REQUEST['mbt_book_image_id'])) { update_post_meta($post_id, 'mbt_book_image_id', intval($_REQUEST['mbt_book_image_id'])); } |
| 529 |
if(isset($_REQUEST['mbt_unique_id_asin'])) { update_post_meta($post_id, 'mbt_unique_id_asin', preg_replace('/[^A-Za-z0-9]/', '', sanitize_text_field(wp_unslash($_REQUEST['mbt_unique_id_asin'])))); } |
| 530 |
if(isset($_REQUEST['mbt_unique_id_isbn'])) { update_post_meta($post_id, 'mbt_unique_id_isbn', preg_replace('/[^0-9Xx]/', '', sanitize_text_field(wp_unslash($_REQUEST['mbt_unique_id_isbn'])))); } |
| 531 |
update_post_meta($post_id, 'mbt_show_unique_id', isset($_REQUEST['mbt_show_unique_id']) ? 'yes' : 'no'); |
| 532 |
|
| 533 |
$metadata = mbt_get_metadata_fields(); |
| 534 |
foreach($metadata as $section_name => $section) { |
| 535 |
foreach($section as $field_id => $field_data) { |
| 536 |
if($field_id == 'supports') { continue; } |
| 537 |
$value = isset($_REQUEST[$field_id]) ? sanitize_text_field(wp_unslash($_REQUEST[$field_id])) : null; |
| 538 |
if($field_data['type'] == 'mbt_metadata_text') { $value = wp_strip_all_tags($value); } |
| 539 |
if($field_data['type'] == 'mbt_metadata_checkbox' or $field_data['type'] == 'mbt_metadata_kindle_instant_preview') { $value = $value === null ? 'no' : 'yes'; } |
| 540 |
if($field_data['type'] == 'mbt_metadata_colorpicker') { |
| 541 |
$matches = array(); |
| 542 |
preg_match('/rgb\(([0-9]+), ([0-9]+), ([0-9]+)\)/i', $value, $matches); |
| 543 |
if(!is_array($matches) or count($matches) != 4) { |
| 544 |
preg_match('/#([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})/i', $value, $matches); |
| 545 |
if(is_array($matches) and count($matches) == 4) { |
| 546 |
$value = 'rgb('.hexdec($matches[1]).', '.hexdec($matches[2]).', '.hexdec($matches[3]).')'; |
| 547 |
} else { |
| 548 |
$value = ''; |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
update_post_meta($post_id, $field_id, $value); |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
/*---------------------------------------------------------*/ |
| 561 |
/* Buy Button Metabox */ |
| 562 |
/*---------------------------------------------------------*/ |
| 563 |
|
| 564 |
function mbt_buybuttons_metabox_editor($data, $num, $store) { |
| 565 |
echo '<div class="mbt_buybutton_editor">'; |
| 566 |
echo '<div class="mbt_buybutton_editor_header">'; |
| 567 |
echo '<button class="mbt_buybutton_remover button">'.esc_attr_e('Remove', 'mybooktable').'</button>'; |
| 568 |
echo '<h4 class="mbt_buybutton_title">'.esc_attr($store['name']).'</h4>'; |
| 569 |
echo '</div>'; |
| 570 |
echo '<div class="mbt_buybutton_editor_content">'; |
| 571 |
$btn_ed = mbt_buybutton_editor($data, "mbt_buybutton".$num, $store); |
| 572 |
echo wp_kses_post($btn_ed); |
| 573 |
echo '</div>'; |
| 574 |
echo '<div class="mbt_buybutton_editor_footer">'; |
| 575 |
echo '<span class="mbt_buybutton_display_title">Display as:</span>'; |
| 576 |
$display = (isset($data['display']) && !empty($data['display'])) ? esc_attr($data['display']) : 'button'; |
| 577 |
echo '<label class="mbt_buybutton_display"><input type="radio" name="mbt_buybutton'.esc_attr($num).'[display]" value="button" '.checked($display, 'button', false).'>'.esc_attr_e('Button', 'mybooktable').'</label>'; |
| 578 |
echo '<label class="mbt_buybutton_display"><input type="radio" name="mbt_buybutton'.esc_attr($num).'[display]" value="text" '.checked($display, 'text', false).'>'.esc_attr_e('Text Bullet', 'mybooktable').'</label>'; |
| 579 |
echo '</div>'; |
| 580 |
echo '</div>'; |
| 581 |
} |
| 582 |
|
| 583 |
function mbt_buybuttons_metabox_ajax() { |
| 584 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), 'mbt-ajax-nonce')) { die(); } |
| 585 |
$stores = mbt_get_stores(); |
| 586 |
if(empty($stores[$_REQUEST['store']])) { die(); } |
| 587 |
$thestore = sanitize_text_field(wp_unslash($_REQUEST['store'])); |
| 588 |
mbt_buybuttons_metabox_editor(array('store' => $thestore), 0, $stores[$thestore]); |
| 589 |
die(); |
| 590 |
} |
| 591 |
|
| 592 |
function mbt_buybuttons_metabox($post) { |
| 593 |
wp_nonce_field(plugin_basename(__FILE__), 'mbt_nonce'); |
| 594 |
if(!mbt_get_setting('enable_default_affiliates') and mbt_get_upgrade() === false) { |
| 595 |
echo '<a href="admin.php?page=mbt_settings&mbt_setup_default_affiliates=1">'.esc_attr_e('Activate Amazon & Noble Buttons', 'mybooktable').'</a>'; |
| 596 |
} |
| 597 |
echo '<div class="mbt-buybuttons-note">'.wp_kses_post(mbt_get_upgrade_message(false, esc_attr_e('Want more options? Upgrade your MyBookTable and get the Universal Buy Button.', 'mybooktable'), '')).'</div>'; |
| 598 |
echo '<div id="mbt_buybutton_nonce" data-nonce="'.esc_attr(wp_create_nonce('mbt-ajax-nonce')).'"></div>'; |
| 599 |
|
| 600 |
$stores = mbt_get_stores(); |
| 601 |
uasort($stores, function($a, $b) { return strcasecmp($a["name"], $b["name"]); }); |
| 602 |
echo '<label for="mbt_store_selector">Choose One:</label>'; |
| 603 |
echo '<select id="mbt_store_selector">'; |
| 604 |
echo '<option value="">'.esc_attr_e('-- Choose One --', 'mybooktable').'</option>'; |
| 605 |
foreach($stores as $slug => $store) { |
| 606 |
echo '<option value="'.esc_attr($slug).'">'.esc_attr($store['name']).'</option>'; |
| 607 |
} |
| 608 |
echo '</select>'; |
| 609 |
echo '<button id="mbt_buybutton_adder" class="button">'.esc_attr('Add').'</button>'; |
| 610 |
|
| 611 |
echo '<div id="mbt_buybutton_editors">'; |
| 612 |
$buybuttons = mbt_query_buybuttons($post->ID); |
| 613 |
if(!empty($buybuttons)) { |
| 614 |
for($i = 0; $i < count($buybuttons); $i++) { |
| 615 |
$buybutton = $buybuttons[$i]; |
| 616 |
if(empty($stores[$buybutton['store']])) { continue; } |
| 617 |
mbt_buybuttons_metabox_editor($buybutton, $i+1, $stores[$buybutton['store']]); |
| 618 |
} |
| 619 |
} |
| 620 |
echo '</div>'; |
| 621 |
} |
| 622 |
|
| 623 |
function mbt_save_buybuttons_metabox($post_id) { |
| 624 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), plugin_basename(__FILE__))) { return; } |
| 625 |
if(mbt_should_not_save_metabox($post_id)) { return; } |
| 626 |
$stores = mbt_get_stores(); |
| 627 |
$buybuttons = array(); |
| 628 |
for($i = 1; isset($_REQUEST['mbt_buybutton'.$i]); $i++) { |
| 629 |
$buybutton = array(); |
| 630 |
|
| 631 |
if(isset($_REQUEST['mbt_buybutton'.$i]['store'])){ |
| 632 |
$buybutton['store'] = sanitize_text_field(wp_unslash($_REQUEST['mbt_buybutton'.$i]['store'])); |
| 633 |
} |
| 634 |
if(isset($_REQUEST['mbt_buybutton'.$i]['url'])){ |
| 635 |
$buybutton['url'] = sanitize_text_field(wp_unslash($_REQUEST['mbt_buybutton'.$i]['url'])); |
| 636 |
} |
| 637 |
if(isset($_REQUEST['mbt_buybutton'.$i]['text']) && !empty($_REQUEST['mbt_buybutton'.$i]['text'])){ |
| 638 |
$buybutton['text'] = sanitize_text_field(wp_unslash($_REQUEST['mbt_buybutton'.$i]['text'])); |
| 639 |
} |
| 640 |
if(isset($_REQUEST['mbt_buybutton'.$i]['display']) && !empty($_REQUEST['mbt_buybutton'.$i]['display'])){ |
| 641 |
$buybutton['display'] = sanitize_text_field(wp_unslash($_REQUEST['mbt_buybutton'.$i]['display'])); |
| 642 |
} |
| 643 |
if(!is_array($buybutton)) { continue; } |
| 644 |
if(empty($stores[$buybutton['store']])) { continue; } |
| 645 |
$buybutton['url'] = preg_replace('/[\r\n]/', '', $buybutton['url']); |
| 646 |
$buybuttons[] = apply_filters('mbt_buybutton_save', $buybutton, $stores[$buybutton['store']]); |
| 647 |
} |
| 648 |
update_post_meta($post_id, 'mbt_buybuttons', $buybuttons); |
| 649 |
|
| 650 |
// auto-populate book asin |
| 651 |
if(get_post_meta($post_id, 'mbt_unique_id_asin', true) == '') { |
| 652 |
foreach($buybuttons as $buybutton) { |
| 653 |
if($buybutton['store'] == 'amazon' or $buybutton['store'] == 'kindle') { |
| 654 |
$asin = mbt_get_amazon_AISN($buybutton['url']); |
| 655 |
if(!empty($asin)) { update_post_meta($post_id, 'mbt_unique_id_asin', $asin); } |
| 656 |
break; |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
/*---------------------------------------------------------*/ |
| 665 |
/* Series Order Metabox */ |
| 666 |
/*---------------------------------------------------------*/ |
| 667 |
|
| 668 |
function mbt_series_order_metabox($post) { |
| 669 |
?> |
| 670 |
<label for="mbt_series_order"><?php esc_attr_e('Book Number', 'mybooktable'); ?>: </label><input name="mbt_series_order" type="text" size="4" id="mbt_series_order" value="<?php echo(wp_kses_post(get_post_meta($post->ID, "mbt_series_order", true))); ?>" /> |
| 671 |
<p class="mbt-helper-description"><?php esc_attr_e('Use this to order books within a series.', 'mybooktable'); ?></p> |
| 672 |
<?php |
| 673 |
} |
| 674 |
|
| 675 |
function mbt_save_series_order_metabox($post_id) { |
| 676 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), plugin_basename(__FILE__))) { return; } |
| 677 |
if(mbt_should_not_save_metabox($post_id)) { return; } |
| 678 |
|
| 679 |
if(isset($_REQUEST['mbt_series_order'])) { update_post_meta($post_id, 'mbt_series_order', sanitize_text_field(wp_unslash($_REQUEST['mbt_series_order']))); } |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
/*---------------------------------------------------------*/ |
| 685 |
/* Endorsements Metabox */ |
| 686 |
/*---------------------------------------------------------*/ |
| 687 |
|
| 688 |
function mbt_endorsement_image_preview_ajax() { |
| 689 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), 'mbt-ajax-nonce')) { die(); } |
| 690 |
if(isset($_REQUEST['image_id'])) { |
| 691 |
$image = wp_get_attachment_image_src(intval($_REQUEST['image_id']), 'mbt_endorsement_image'); |
| 692 |
echo(wp_kses_post($image ? $image[0] : plugins_url('images/person-placeholder.png', dirname(__FILE__)))); |
| 693 |
} |
| 694 |
die(); |
| 695 |
} |
| 696 |
|
| 697 |
function mbt_endorsements_metabox($post) { |
| 698 |
$endorsements = get_post_meta($post->ID, 'mbt_endorsements', true); |
| 699 |
if(empty($endorsements)) { $endorsements = array(); } |
| 700 |
?> |
| 701 |
<div class="mbt_endorsements_metabox"> |
| 702 |
<div id="mbt_endorsement_nonce" data-nonce="<?php echo(esc_attr(wp_create_nonce('mbt-ajax-nonce'))); ?>"></div> |
| 703 |
<div class="button button-primary mbt_endorsement_adder">Add Endorsement</div> |
| 704 |
<input type="hidden" class="mbt_endorsements" name="mbt_endorsements" value='<?php echo(esc_js(str_replace('\'', ''', wp_json_encode($endorsements)))); ?>'> |
| 705 |
<div class="mbt_endorsements_editors"></div> |
| 706 |
<p class="description">This is where you post endorsements from folks who have reccommended your book.</p> |
| 707 |
</div> |
| 708 |
<?php |
| 709 |
} |
| 710 |
|
| 711 |
function mbt_save_endorsements_metabox($post_id) { |
| 712 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), plugin_basename(__FILE__))) { return; } |
| 713 |
if(mbt_should_not_save_metabox($post_id)) { return; } |
| 714 |
|
| 715 |
if(isset($_REQUEST['mbt_endorsements'])) { update_post_meta($post_id, 'mbt_endorsements', json_decode(str_replace('\\\\', '\\', str_replace('\"', '"', str_replace('\\\'', '\'', sanitize_text_field(wp_unslash($_REQUEST['mbt_endorsements']))))), true)); } |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
/*---------------------------------------------------------*/ |
| 721 |
/* Book Club Metabox */ |
| 722 |
/*---------------------------------------------------------*/ |
| 723 |
|
| 724 |
function mbt_bookclub_metabox($post) { |
| 725 |
$title = get_post_meta($post->ID, 'mbt_bookclub_title', true); |
| 726 |
$video = get_post_meta($post->ID, 'mbt_bookclub_video', true); |
| 727 |
$links = get_post_meta($post->ID, 'mbt_bookclub_links', true); |
| 728 |
if(empty($title)) { $title = __('Book Club Resources', 'mybooktable'); } |
| 729 |
if(empty($links)) { $links = array(); } |
| 730 |
?> |
| 731 |
<div class="mbt_bookclub_metabox"> |
| 732 |
<div class="mbt_bookclub_links_container"> |
| 733 |
<label>Section Title: <input type="text" name="mbt_bookclub_title" id="mbt_bookclub_title" value="<?php echo(esc_attr($title)); ?>" /></label> |
| 734 |
<div class="mbt_bookclub_links_description"> |
| 735 |
<p>The more resources you provide book clubs, the more they will want to read your book. Suggested materials include:</p> |
| 736 |
<ul> |
| 737 |
<li>PDF Download (Discussion Questions)</li> |
| 738 |
<li>Bulk Ordering Link</li> |
| 739 |
<li>Powerpoint Companion</li> |
| 740 |
<li>Chapter Excerpts PDF</li> |
| 741 |
</ul> |
| 742 |
</div> |
| 743 |
<div class="button button-primary mbt_bookclub_link_adder">Add Resource</div> |
| 744 |
<input type="hidden" class="mbt_bookclub_links" name="mbt_bookclub_links" value='<?php echo(esc_js(str_replace('\'', ''', wp_json_encode($links)))); ?>'> |
| 745 |
<div class="mbt_bookclub_link_editors"></div> |
| 746 |
</div> |
| 747 |
<div class="mbt_bookclub_video_container"> |
| 748 |
<h4 class="mbt_bookclub_video_title">Video Companion</h4> |
| 749 |
<p>This is where you can include a short YouTube or Vimeo video for Book Groups to show as part of the book discussion.</p> |
| 750 |
<label>Video URL: <input type="text" name="mbt_bookclub_video" id="mbt_bookclub_video" value="<?php echo(esc_attr($video)); ?>" /></label> |
| 751 |
</div> |
| 752 |
</div> |
| 753 |
<?php |
| 754 |
} |
| 755 |
|
| 756 |
function mbt_save_bookclub_metabox($post_id) { |
| 757 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), plugin_basename(__FILE__))) { return; } |
| 758 |
if(mbt_should_not_save_metabox($post_id)) { return; } |
| 759 |
|
| 760 |
if(isset($_REQUEST['mbt_bookclub_links'])) { update_post_meta($post_id, 'mbt_bookclub_links', json_decode(str_replace('\\\\', '\\', str_replace('\"', '"', str_replace('\\\'', '\'', sanitize_text_field(wp_unslash($_REQUEST['mbt_bookclub_links']))))), true)); } |
| 761 |
if(isset($_REQUEST['mbt_bookclub_video'])) { update_post_meta($post_id, 'mbt_bookclub_video', sanitize_text_field(wp_unslash($_REQUEST['mbt_bookclub_video']))); } |
| 762 |
if(isset($_REQUEST['mbt_bookclub_title'])) { update_post_meta($post_id, 'mbt_bookclub_title', sanitize_text_field(wp_unslash($_REQUEST['mbt_bookclub_title']))); } |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
/*---------------------------------------------------------*/ |
| 768 |
/* Audio Book Metabox */ |
| 769 |
/*---------------------------------------------------------*/ |
| 770 |
|
| 771 |
function mbt_audiobook_metabox($post) { |
| 772 |
$title = get_post_meta($post->ID, 'mbt_audiobook_title', true); |
| 773 |
$links = get_post_meta($post->ID, 'mbt_audiobook_links', true); |
| 774 |
if(empty($title)) { $title = __('Audio Book Resources', 'mybooktable'); } |
| 775 |
if(empty($links)) { $links = array(); } |
| 776 |
?> |
| 777 |
<div class="mbt_audiobook_metabox"> |
| 778 |
<div class="mbt_audiobook_links_container"> |
| 779 |
<label>Section Title: <input type="text" name="mbt_audiobook_title" id="mbt_audiobook_title" value="<?php echo(esc_attr($title)); ?>" /></label> |
| 780 |
<div class="mbt_audiobook_links_description"> |
| 781 |
<p>This is where you can upload companion material to your Audiobook such as:</p> |
| 782 |
<ul> |
| 783 |
<li>Illustrations, diagrams, or photos from your Book</li> |
| 784 |
<li>Maps of Your World</li> |
| 785 |
<li>PDF Audiobook Companion</li> |
| 786 |
</ul> |
| 787 |
</div> |
| 788 |
<div class="button button-primary mbt_audiobook_link_adder">Add Resource</div> |
| 789 |
<input type="hidden" class="mbt_audiobook_links" name="mbt_audiobook_links" value='<?php echo(esc_js(str_replace('\'', ''', wp_json_encode($links)))); ?>'> |
| 790 |
<div class="mbt_audiobook_link_editors"></div> |
| 791 |
</div> |
| 792 |
</div> |
| 793 |
<?php |
| 794 |
} |
| 795 |
|
| 796 |
function mbt_save_audiobook_metabox($post_id) { |
| 797 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), plugin_basename(__FILE__))) { return; } |
| 798 |
if(mbt_should_not_save_metabox($post_id)) { return; } |
| 799 |
|
| 800 |
if(isset($_REQUEST['mbt_audiobook_links'])) { update_post_meta($post_id, 'mbt_audiobook_links', json_decode(str_replace('\\\\', '\\', str_replace('\"', '"', str_replace('\\\'', '\'', sanitize_text_field(wp_unslash($_REQUEST['mbt_audiobook_links']))))), true)); } |
| 801 |
if(isset($_REQUEST['mbt_audiobook_title'])) { update_post_meta($post_id, 'mbt_audiobook_title', sanitize_text_field(wp_unslash($_REQUEST['mbt_audiobook_title']))); } |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
/*---------------------------------------------------------*/ |
| 807 |
/* Section Sorting Metabox */ |
| 808 |
/*---------------------------------------------------------*/ |
| 809 |
|
| 810 |
function mbt_change_booksections_displaymode_ajax() { |
| 811 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), 'mbt-ajax-nonce')) { die(); } |
| 812 |
if(isset($_REQUEST['display_mode'])) { |
| 813 |
if(isset($_REQUEST['reset']) and $_REQUEST['reset'] === "true") { |
| 814 |
mbt_update_setting('book_section_order_'.sanitize_text_field(wp_unslash($_REQUEST['display_mode'])), array()); |
| 815 |
} |
| 816 |
$sections = mbt_get_sorted_content_sections(sanitize_text_field(wp_unslash($_REQUEST['display_mode']))); |
| 817 |
echo(esc_js(str_replace('\'', ''', wp_json_encode($sections)))); |
| 818 |
} |
| 819 |
die(); |
| 820 |
} |
| 821 |
|
| 822 |
function mbt_sectionsorting_metabox($post) { |
| 823 |
$display_mode = mbt_get_book_display_mode($post->ID); |
| 824 |
$sections = mbt_get_sorted_content_sections($display_mode); |
| 825 |
if(empty($sections)) { $sections = array(); } |
| 826 |
?> |
| 827 |
<div class="mbt_sectionsorting_metabox"> |
| 828 |
<div id="mbt_sectionsorting_nonce" data-nonce="<?php echo(esc_attr(wp_create_nonce('mbt-ajax-nonce'))); ?>"></div> |
| 829 |
<input type="hidden" class="mbt_booksections_displaymode" name="mbt_booksections_displaymode" value="<?php echo(esc_js($display_mode)); ?>"> |
| 830 |
<input type="hidden" class="mbt_booksections" name="mbt_booksections" value='<?php echo(esc_js(str_replace('\'', ''', wp_json_encode($sections)))); ?>'> |
| 831 |
<div class="mbt_booksections_sorters"></div> |
| 832 |
<p class="description" style="margin-bottom: 6px;">Use this to rearrange the display order of the sections on the book page.</p> |
| 833 |
<div class="button mbt_booksections_reset">Restore Default Section Order</div> |
| 834 |
</div> |
| 835 |
<script type="text/javascript">jQuery('.mbt_sectionsorting_metabox').parents('#mbt_sectionsorting').attr('data-mbt-supports', 'sortable_sections');</script> |
| 836 |
<?php |
| 837 |
} |
| 838 |
|
| 839 |
function mbt_save_sectionsorting_metabox($post_id) { |
| 840 |
if(!isset($_REQUEST['mbt_nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), plugin_basename(__FILE__))) { return; } |
| 841 |
if(mbt_should_not_save_metabox($post_id)) { return; } |
| 842 |
|
| 843 |
if(isset($_REQUEST['mbt_booksections']) and isset($_REQUEST['mbt_booksections_displaymode'])) { |
| 844 |
mbt_update_setting('book_section_order_'.sanitize_text_field(wp_unslash($_REQUEST['mbt_booksections_displaymode'])), json_decode(str_replace('\\\\', '\\', str_replace('\"', '"', str_replace('\\\'', '\'', sanitize_text_field(wp_unslash($_REQUEST['mbt_booksections']))))), true)); |
| 845 |
} |
| 846 |
} |
| 847 |
|