| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
/*---------------------------------------------------------*/ |
| 5 |
/* Settings Functions */ |
| 6 |
/*---------------------------------------------------------*/ |
| 7 |
|
| 8 |
function mbt_load_settings() { |
| 9 |
global $mbt_settings; |
| 10 |
$mbt_settings = apply_filters("mbt_settings", get_option("mbt_settings")); |
| 11 |
if(empty($mbt_settings)) { mbt_reset_settings(); } |
| 12 |
} |
| 13 |
|
| 14 |
function mbt_reset_settings() { |
| 15 |
global $mbt_settings; |
| 16 |
$mbt_settings = array( |
| 17 |
'version' => MBT_VERSION, |
| 18 |
'api_key' => '', |
| 19 |
'api_key_status' => 0, |
| 20 |
'api_key_message' => '', |
| 21 |
'upgrade_active' => false, |
| 22 |
'installed' => '', |
| 23 |
'installed_examples' => false, |
| 24 |
'booktable_page' => 0, |
| 25 |
'compatibility_mode' => true, |
| 26 |
'style_pack' => mbt_get_default_style_pack(), |
| 27 |
'image_size' => 'medium', |
| 28 |
'reviews_type' => 'none', |
| 29 |
'enable_socialmedia_single_book' => false, |
| 30 |
'enable_socialmedia_book_excerpt' => false, |
| 31 |
'enable_seo' => true, |
| 32 |
'buybutton_shadowbox' => 'none', |
| 33 |
'enable_breadcrumbs' => true, |
| 34 |
'show_series' => true, |
| 35 |
'show_find_bookstore' => true, |
| 36 |
'show_find_bookstore_buybuttons_shadowbox' => true, |
| 37 |
'show_about_author' => true, |
| 38 |
'book_button_size' => 'medium', |
| 39 |
'listing_button_size' => 'medium', |
| 40 |
'widget_button_size' => 'medium', |
| 41 |
'posts_per_page' => 12, |
| 42 |
'enable_default_affiliates' => false, |
| 43 |
'google_api_key' => '', |
| 44 |
'product_name' => __('Books', 'mybooktable'), |
| 45 |
'product_slug' => _x('books', 'URL slug', 'mybooktable'), |
| 46 |
'hide_domc_notice' => false, |
| 47 |
'domc_notice_text' => __('Disclosure of Material Connection: Some of the links in the page above are "affiliate links." This means if you click on the link and purchase the item, I will receive an affiliate commission. I am disclosing this in accordance with the Federal Trade Commission\'s <a href="https://www.access.gpo.gov/nara/cfr/waisidx_03/16cfr255_03.html" target="_blank">16 CFR, Part 255</a>: "Guides Concerning the Use of Endorsements and Testimonials in Advertising."', 'mybooktable'), |
| 48 |
); |
| 49 |
$mbt_settings = apply_filters("mbt_default_settings", $mbt_settings); |
| 50 |
update_option("mbt_settings", apply_filters("mbt_update_settings", $mbt_settings)); |
| 51 |
} |
| 52 |
|
| 53 |
function mbt_get_setting($name) { |
| 54 |
global $mbt_settings; |
| 55 |
return isset($mbt_settings[$name]) ? $mbt_settings[$name] : NULL; |
| 56 |
} |
| 57 |
|
| 58 |
function mbt_update_setting($name, $value) { |
| 59 |
global $mbt_settings; |
| 60 |
$mbt_settings[$name] = $value; |
| 61 |
update_option("mbt_settings", apply_filters("mbt_update_settings", $mbt_settings)); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
/*---------------------------------------------------------*/ |
| 67 |
/* General */ |
| 68 |
/*---------------------------------------------------------*/ |
| 69 |
|
| 70 |
function mbt_save_taxonomy_image($taxonomy, $term, $url) { |
| 71 |
$taxonomy_images = get_option($taxonomy."_meta"); |
| 72 |
if(empty($taxonomy_images)) { $taxonomy_images = array(); } |
| 73 |
$taxonomy_images[$term] = $url; |
| 74 |
update_option($taxonomy."_meta", $taxonomy_images); |
| 75 |
} |
| 76 |
|
| 77 |
function mbt_get_taxonomy_image($taxonomy, $term) { |
| 78 |
$taxonomy_images = get_option($taxonomy."_meta"); |
| 79 |
if(empty($taxonomy_images)) { $taxonomy_images = array(); } |
| 80 |
return isset($taxonomy_images[$term]) ? $taxonomy_images[$term] : ''; |
| 81 |
} |
| 82 |
|
| 83 |
function mbt_save_author_priority($author_id, $priority) { |
| 84 |
$author_priorities = mbt_get_setting("author_priorities"); |
| 85 |
if(empty($author_priorities)) { $author_priorities = array(); } |
| 86 |
$author_priorities[$author_id] = $priority; |
| 87 |
mbt_update_setting("author_priorities", $author_priorities); |
| 88 |
} |
| 89 |
|
| 90 |
function mbt_get_author_priority($author_id) { |
| 91 |
$author_priorities = mbt_get_setting("author_priorities"); |
| 92 |
if(empty($author_priorities)) { $author_priorities = array(); } |
| 93 |
return isset($author_priorities[$author_id]) ? $author_priorities[$author_id] : 50; |
| 94 |
} |
| 95 |
|
| 96 |
function mbt_get_posts_per_page() { |
| 97 |
$posts_per_page = mbt_get_setting('posts_per_page'); |
| 98 |
return empty($posts_per_page) ? get_option('posts_per_page') : $posts_per_page; |
| 99 |
} |
| 100 |
|
| 101 |
function mbt_is_mbt_page() { |
| 102 |
return (is_post_type_archive('mbt_book') or is_tax('mbt_author') or is_tax('mbt_genre') or is_tax('mbt_series') or is_tax('mbt_tag') or is_singular('mbt_book') or mbt_is_booktable_page() or mbt_is_archive_query()); |
| 103 |
} |
| 104 |
|
| 105 |
function mbt_is_mbt_admin_page() { |
| 106 |
global $pagenow; |
| 107 |
$screen = get_current_screen(); |
| 108 |
return is_admin() and ( |
| 109 |
($pagenow == 'edit.php' and $screen->post_type == 'mbt_book') or |
| 110 |
($pagenow == 'post.php' and $screen->post_type == 'mbt_book') or |
| 111 |
($pagenow == 'post-new.php' and $screen->post_type == 'mbt_book') or |
| 112 |
(($pagenow == 'edit-tags.php' or $pagenow == 'term.php') and $screen->taxonomy == 'mbt_author') or |
| 113 |
(($pagenow == 'edit-tags.php' or $pagenow == 'term.php') and $screen->taxonomy == 'mbt_genre') or |
| 114 |
(($pagenow == 'edit-tags.php' or $pagenow == 'term.php') and $screen->taxonomy == 'mbt_series') or |
| 115 |
(($pagenow == 'edit-tags.php' or $pagenow == 'term.php') and $screen->taxonomy == 'mbt_tag') or |
| 116 |
($pagenow == 'admin.php' and $screen->id == 'mybooktable_page_mbt_import') or |
| 117 |
($pagenow == 'admin.php' and $screen->id == 'mybooktable_page_mbt_sort_books') or |
| 118 |
($pagenow == 'admin.php' and $screen->id == 'toplevel_page_mbt_dashboard') or |
| 119 |
($pagenow == 'admin.php' and $screen->id == 'mybooktable_page_mbt_settings') or |
| 120 |
($pagenow == 'admin.php' and $screen->id == 'mybooktable_page_mbt_help') |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
function mbt_is_booktable_page() { |
| 125 |
global $mbt_is_booktable_page; |
| 126 |
return !empty($mbt_is_booktable_page); |
| 127 |
} |
| 128 |
|
| 129 |
function mbt_get_booktable_url() { |
| 130 |
if(mbt_get_setting('booktable_page') <= 0 or !get_page(mbt_get_setting('booktable_page'))) { |
| 131 |
$url = get_post_type_archive_link('mbt_book'); |
| 132 |
} else { |
| 133 |
$url = get_permalink(mbt_get_setting('booktable_page')); |
| 134 |
} |
| 135 |
return $url; |
| 136 |
} |
| 137 |
|
| 138 |
function mbt_get_product_name() { |
| 139 |
$name = mbt_get_setting('product_name'); |
| 140 |
return apply_filters('mbt_product_name', empty($name) ? __('Books', 'mybooktable') : $name); |
| 141 |
} |
| 142 |
|
| 143 |
function mbt_get_product_slug() { |
| 144 |
$slug = mbt_get_setting('product_slug'); |
| 145 |
return apply_filters('mbt_product_slug', empty($slug) ? _x('books', 'URL slug', 'mybooktable') : $slug); |
| 146 |
} |
| 147 |
|
| 148 |
function mbt_get_reviews_types() { |
| 149 |
// The 'mbt_reviews_boxes' filter is deprecated, but still supported |
| 150 |
return apply_filters('mbt_reviews_boxes', apply_filters('mbt_reviews_types', array())); |
| 151 |
} |
| 152 |
|
| 153 |
function mbt_add_disabled_reviews_types($reviews) { |
| 154 |
$reviews['amazon'] = array( |
| 155 |
'name' => __('Amazon Reviews','mybooktable'), |
| 156 |
'disabled' => mbt_get_upgrade_message(), |
| 157 |
); |
| 158 |
return $reviews; |
| 159 |
} |
| 160 |
//add_filter('mbt_reviews_types', 'mbt_add_disabled_reviews_types', 9); |
| 161 |
|
| 162 |
function mbt_get_wp_filesystem($nonce_url) { |
| 163 |
require_once(ABSPATH.'wp-admin/includes/file.php'); |
| 164 |
|
| 165 |
ob_start(); |
| 166 |
$creds = request_filesystem_credentials($nonce_url, '', false, false, null); |
| 167 |
$output = ob_get_contents(); |
| 168 |
ob_end_clean(); |
| 169 |
if($creds === false) { return $output; } |
| 170 |
|
| 171 |
if(!WP_Filesystem($creds)) { |
| 172 |
ob_start(); |
| 173 |
request_filesystem_credentials($nonce_url, '', true, false, null); |
| 174 |
$output = ob_get_contents(); |
| 175 |
ob_end_clean(); |
| 176 |
return $output; |
| 177 |
} |
| 178 |
|
| 179 |
return ''; |
| 180 |
} |
| 181 |
|
| 182 |
function mbt_download_and_insert_attachment($url) { |
| 183 |
$raw_response = wp_remote_get($url, array('timeout' => 3)); |
| 184 |
if(is_wp_error($raw_response) or wp_remote_retrieve_response_code($raw_response) != 200) { return 0; } |
| 185 |
$file_data = wp_remote_retrieve_body($raw_response); |
| 186 |
|
| 187 |
$nonce_url = wp_nonce_url('admin.php', 'mbt_download_and_insert_attachment'); |
| 188 |
$output = mbt_get_wp_filesystem($nonce_url); |
| 189 |
if(!empty($output)) { return 0; } |
| 190 |
global $wp_filesystem, $wpdb; |
| 191 |
|
| 192 |
$url_parts = wp_parse_url($url); |
| 193 |
$filename = basename($url_parts['path']); |
| 194 |
$filename = preg_replace('/[^A-Za-z0-9_.]/', '', $filename); |
| 195 |
$upload_dir = wp_upload_dir(); |
| 196 |
$filepath = $upload_dir['path'].'/'.$filename; |
| 197 |
$fileurl = $upload_dir['url'].'/'.$filename; |
| 198 |
|
| 199 |
if($wp_filesystem->exists($filepath)) { |
| 200 |
$existing_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid=%s", $fileurl)); |
| 201 |
if(!empty($existing_id)) { return $existing_id; } else { return 0; } |
| 202 |
} |
| 203 |
|
| 204 |
if(!$wp_filesystem->put_contents($filepath, $file_data, FS_CHMOD_FILE)) { return 0; } |
| 205 |
|
| 206 |
$filetype = wp_check_filetype(basename($filepath), null); |
| 207 |
$attachment = array( |
| 208 |
'guid' => $fileurl, |
| 209 |
'post_mime_type' => $filetype['type'], |
| 210 |
'post_title' => preg_replace('/\.[^.]+$/', '', $filename), |
| 211 |
'post_content' => '', |
| 212 |
'post_status' => 'inherit' |
| 213 |
); |
| 214 |
|
| 215 |
$attach_id = wp_insert_attachment($attachment, $filepath); |
| 216 |
|
| 217 |
require_once(ABSPATH.'wp-admin/includes/image.php'); |
| 218 |
$attach_data = wp_generate_attachment_metadata($attach_id, $filepath); |
| 219 |
wp_update_attachment_metadata($attach_id, $attach_data); |
| 220 |
|
| 221 |
return $attach_id; |
| 222 |
} |
| 223 |
|
| 224 |
function mbt_copy_and_insert_attachment($path) { |
| 225 |
$nonce_url = wp_nonce_url('admin.php', 'mbt_copy_and_insert_attachment'); |
| 226 |
$output = mbt_get_wp_filesystem($nonce_url); |
| 227 |
if(!empty($output)) { return 0; } |
| 228 |
global $wp_filesystem, $wpdb; |
| 229 |
|
| 230 |
$filename = basename($path); |
| 231 |
$filename = preg_replace('/[^A-Za-z0-9_.]/', '', $filename); |
| 232 |
$upload_dir = wp_upload_dir(); |
| 233 |
$filepath = $upload_dir['path'].'/'.$filename; |
| 234 |
$fileurl = $upload_dir['url'].'/'.$filename; |
| 235 |
|
| 236 |
if($wp_filesystem->exists($filepath)) { |
| 237 |
$existing_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid=%s", $fileurl)); |
| 238 |
if(!empty($existing_id)) { return $existing_id; } else { return 0; } |
| 239 |
} |
| 240 |
|
| 241 |
if(!$wp_filesystem->copy($path, $filepath, false, FS_CHMOD_FILE)) { return 0; } |
| 242 |
|
| 243 |
$filetype = wp_check_filetype(basename($filepath), null); |
| 244 |
$attachment = array( |
| 245 |
'guid' => $fileurl, |
| 246 |
'post_mime_type' => $filetype['type'], |
| 247 |
'post_title' => preg_replace('/\.[^.]+$/', '', $filename), |
| 248 |
'post_content' => '', |
| 249 |
'post_status' => 'inherit' |
| 250 |
); |
| 251 |
|
| 252 |
$attach_id = wp_insert_attachment($attachment, $filepath); |
| 253 |
|
| 254 |
require_once(ABSPATH.'wp-admin/includes/image.php'); |
| 255 |
$attach_data = wp_generate_attachment_metadata($attach_id, $filepath); |
| 256 |
wp_update_attachment_metadata($attach_id, $attach_data); |
| 257 |
|
| 258 |
return $attach_id; |
| 259 |
} |
| 260 |
|
| 261 |
function mbt_add_book_pages_to_front_page_options($pages, $args) { |
| 262 |
if(!is_array($args) or !isset($args['name']) or $args['name'] !== 'page_on_front') { return $pages; } |
| 263 |
|
| 264 |
$books_query = new WP_Query(array('post_type' => 'mbt_book', 'posts_per_page' => -1)); |
| 265 |
$books = $books_query->posts; |
| 266 |
foreach($books as $book) { |
| 267 |
$book->post_title = __('Book Page', 'mybooktable').': '.$book->post_title; |
| 268 |
} |
| 269 |
|
| 270 |
return array_merge($pages, $books); |
| 271 |
} |
| 272 |
add_filter('get_pages', 'mbt_add_book_pages_to_front_page_options', 10, 2); |
| 273 |
|
| 274 |
function mbt_push_query($name, $query) { |
| 275 |
global $wp_query, $posts, $post, $id, $mbt_query_stack; |
| 276 |
if(empty($mbt_query_stack)) { $mbt_query_stack = array(); } |
| 277 |
$mbt_query_stack[] = array( |
| 278 |
'name' => $name, |
| 279 |
'wp_query' => $wp_query, |
| 280 |
'posts' => $posts, |
| 281 |
'post' => $post, |
| 282 |
'id' => $id, |
| 283 |
); |
| 284 |
$wp_query = $query; |
| 285 |
$posts = $query->posts; |
| 286 |
$post = $query->post; |
| 287 |
$id = $query->post ? $query->post->ID : NULL; |
| 288 |
} |
| 289 |
|
| 290 |
function mbt_pop_query($name) { |
| 291 |
global $wp_query, $posts, $post, $id, $mbt_query_stack; |
| 292 |
if(empty($mbt_query_stack)) { return; } |
| 293 |
$query = array_pop($mbt_query_stack); |
| 294 |
if($name !== $query['name']) { |
| 295 |
trigger_error('MyBookTable tried to close '.esc_attr($name).' query but was prevented by '.esc_attr($query['name']).' query!', E_USER_WARNING); |
| 296 |
$mbt_query_stack[] = $query; |
| 297 |
return; |
| 298 |
} |
| 299 |
$wp_query = $query['wp_query']; |
| 300 |
$posts = $query['posts']; |
| 301 |
$post = $query['post']; |
| 302 |
$id = $query['id']; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
/*---------------------------------------------------------*/ |
| 308 |
/* Display Modes */ |
| 309 |
/*---------------------------------------------------------*/ |
| 310 |
|
| 311 |
function mbt_get_book_display_modes() { |
| 312 |
$modes = apply_filters('mbt_display_modes', array()); |
| 313 |
foreach($modes as $slug => $mode) { |
| 314 |
if(empty($modes[$slug]['name'])) { $modes[$slug]['name'] = __('Unnamed', 'mybooktable'); } |
| 315 |
if(empty($modes[$slug]['supports'])) { $modes[$slug]['supports'] = array(); } |
| 316 |
} |
| 317 |
return $modes; |
| 318 |
} |
| 319 |
|
| 320 |
function mbt_add_default_book_display_modes($modes) { |
| 321 |
$modes['storefront'] = array('name' => __('Storefront', 'mybooktable'), 'supports' => array('embedding', 'sale_price')); |
| 322 |
$modes['singlecolumn'] = array('name' => __('Beautiful Page', 'mybooktable'), 'supports' => array('embedding', 'teaser', 'sortable_sections')); |
| 323 |
return $modes; |
| 324 |
} |
| 325 |
add_filter('mbt_display_modes', 'mbt_add_default_book_display_modes'); |
| 326 |
|
| 327 |
function mbt_get_default_book_display_mode() { |
| 328 |
return apply_filters('mbt_default_book_display_mode', 'singlecolumn'); |
| 329 |
} |
| 330 |
|
| 331 |
function mbt_get_book_display_mode($post_id) { |
| 332 |
$display_mode = get_post_meta($post_id, 'mbt_display_mode', true); |
| 333 |
$display_modes = mbt_get_book_display_modes(); |
| 334 |
if(empty($display_modes[$display_mode])) { $display_mode = mbt_get_default_book_display_mode(); } |
| 335 |
return $display_mode; |
| 336 |
} |
| 337 |
|
| 338 |
function mbt_book_display_mode_supports($display_mode, $supports) { |
| 339 |
$display_modes = mbt_get_book_display_modes(); |
| 340 |
if(empty($display_modes[$display_mode])) { $display_mode = mbt_get_default_book_display_mode(); } |
| 341 |
return array_search($supports, $display_modes[$display_mode]['supports']) !== FALSE; |
| 342 |
} |
| 343 |
|
| 344 |
add_action('admin_footer-edit.php', 'mbt_book_bulk_change_display_mode_admin_footer'); |
| 345 |
function mbt_book_bulk_change_display_mode_admin_footer() { |
| 346 |
$display_modes = mbt_get_book_display_modes(); |
| 347 |
global $post_type; |
| 348 |
if($post_type == 'mbt_book') { |
| 349 |
?> |
| 350 |
<script type="text/javascript"> |
| 351 |
jQuery(document).ready(function() { |
| 352 |
<?php foreach($display_modes as $slug => $mode) { ?> |
| 353 |
jQuery('<option>').val('mbt-switch-mode-<?php echo(esc_attr($slug)); ?>').text(mbt_admin_pages_i18n.swich_to_mode.replace('%s', '<?php htmlspecialchars($mode['name'], ENT_QUOTES); ?>')).appendTo('#bulk-action-selector-top'); |
| 354 |
jQuery('<option>').val('mbt-switch-mode-<?php echo(esc_attr($slug)); ?>').text(mbt_admin_pages_i18n.swich_to_mode.replace('%s', '<?php htmlspecialchars($mode['name'], ENT_QUOTES); ?>')).appendTo('#bulk-action-selector-bottom'); |
| 355 |
<?php } ?> |
| 356 |
}); |
| 357 |
</script> |
| 358 |
<?php |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
add_action('load-edit.php', 'mbt_book_bulk_change_display_mode_action'); |
| 363 |
function mbt_book_bulk_change_display_mode_action() { |
| 364 |
$wp_list_table = _get_list_table('WP_Posts_List_Table'); |
| 365 |
$action = $wp_list_table->current_action(); |
| 366 |
|
| 367 |
if(substr($action, 0, 16) == 'mbt-switch-mode-') { |
| 368 |
check_admin_referer('bulk-posts'); |
| 369 |
|
| 370 |
$selected_mode = substr($action, 16); |
| 371 |
|
| 372 |
$display_modes = mbt_get_book_display_modes(); |
| 373 |
if(empty($display_modes[$selected_mode])) { return; } |
| 374 |
|
| 375 |
$post_ids = array_map('intval', isset($_REQUEST['post']) ? $_REQUEST['post'] : array()); |
| 376 |
if(empty($post_ids)) { return; } |
| 377 |
|
| 378 |
$books_updated = 0; |
| 379 |
foreach($post_ids as $post_id) { |
| 380 |
update_post_meta($post_id, 'mbt_display_mode', $selected_mode); |
| 381 |
|
| 382 |
$books_updated++; |
| 383 |
} |
| 384 |
|
| 385 |
wp_safe_redirect(add_query_arg(array('paged' => $wp_list_table->get_pagenum(), 'mbt-books-updated' => $books_updated), admin_url('edit.php?post_type=mbt_book'))); |
| 386 |
exit(); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
add_action('admin_notices', 'mbt_book_bulk_change_display_mode_admin_notices'); |
| 391 |
function mbt_book_bulk_change_display_mode_admin_notices() { |
| 392 |
global $post_type, $pagenow; |
| 393 |
$books_updated = filter_input( INPUT_GET,'mbt-books-updated'); |
| 394 |
if($pagenow == 'edit.php' && $post_type == 'mbt_book' && isset($books_updated) && intval($books_updated)) { |
| 395 |
/* translators: %s: the number of books */ |
| 396 |
$message = sprintf(_n('%s Book updated.', '%s books updated.', intval($books_updated)), number_format_i18n(intval($books_updated))); |
| 397 |
echo('<div class="updated"><p>'.esc_attr($message).'</p></div>'); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
function mbt_get_sorted_content_sections($display_mode) { |
| 402 |
$sections = apply_filters('mbt_get_'.$display_mode.'_content_sections', array()); |
| 403 |
$prioritized_sections = array(); |
| 404 |
foreach($sections as $id => $section) { $prioritized_sections[] = array_merge($section, array('id' => $id)); } |
| 405 |
$prioritize = function($a, $b) { return ($a['priority'] == $b['priority']) ? 0 : (($a['priority'] < $b['priority']) ? -1 : 1); }; |
| 406 |
usort($prioritized_sections, $prioritize); |
| 407 |
$sections_order = mbt_get_setting('book_section_order_'.$display_mode); |
| 408 |
if(empty($sections_order)) { $sections_order = array(); } |
| 409 |
$sorted_sections = array(); |
| 410 |
$order_index = 0; |
| 411 |
foreach($prioritized_sections as $section) { |
| 412 |
$index = array_search($section['id'], $sections_order); |
| 413 |
if($index === false) { |
| 414 |
$sorted_sections[] = $section; |
| 415 |
} else { |
| 416 |
while(empty($sections[$sections_order[$order_index]])) { $order_index += 1; } |
| 417 |
$order_section = $sections[$sections_order[$order_index]]; |
| 418 |
$sorted_sections[] = array_merge($order_section, array('id' => $sections_order[$order_index], 'priority' => $section['priority'])); |
| 419 |
$order_index += 1; |
| 420 |
} |
| 421 |
} |
| 422 |
return $sorted_sections; |
| 423 |
} |
| 424 |
|
| 425 |
function mbt_render_book_section($post_id, $section_id, $content, $title='') { |
| 426 |
$output = ''; |
| 427 |
$title = apply_filters('mbt_book_section_title', $title, $post_id, $section_id); |
| 428 |
$content = apply_filters('mbt_book_section_content', $content, $post_id, $section_id); |
| 429 |
if(!empty($content)) { |
| 430 |
$output .= '<a class="mbt-book-anchor" id="mbt-book-'.$section_id.'-anchor" name="mbt-book-'.$section_id.'-anchor"></a>'; |
| 431 |
$output .= '<div class="mbt-book-section mbt-book-'.$section_id.'-section">'; |
| 432 |
if($title) { $output .= '<div class="mbt-book-section-title">'.$title.'</div>'; } |
| 433 |
$output .= '<div class="mbt-book-section-content">'.$content.'</div>'; |
| 434 |
$output .= '</div>'; |
| 435 |
} |
| 436 |
return apply_filters('mbt_render_book_section', $output, $post_id, $section_id, $content, $title); |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
/*---------------------------------------------------------*/ |
| 442 |
/* Importers */ |
| 443 |
/*---------------------------------------------------------*/ |
| 444 |
|
| 445 |
function mbt_get_importers() { |
| 446 |
return apply_filters('mbt_importers', array()); |
| 447 |
} |
| 448 |
|
| 449 |
function mbt_add_disabled_importers($importers) { |
| 450 |
/*$importers['amazon'] = array( |
| 451 |
'name' => __('Amazon Bulk Book Importer', 'mybooktable'), |
| 452 |
'desc' => __('Import your books in bulk from Amazon with a list of ISBNs.', 'mybooktable'), |
| 453 |
'disabled' => mbt_get_upgrade_message(), |
| 454 |
);*/ |
| 455 |
$importers['uiee'] = array( |
| 456 |
'name' => __('UIEE File', 'mybooktable'), |
| 457 |
'desc' => __('Import your books from a UIEE (Universal Information Exchange Environment) File.', 'mybooktable'), |
| 458 |
'disabled' => mbt_get_upgrade_message(), |
| 459 |
); |
| 460 |
return $importers; |
| 461 |
} |
| 462 |
add_filter('mbt_importers', 'mbt_add_disabled_importers', 9); |
| 463 |
|
| 464 |
function mbt_import_book($book) { |
| 465 |
$defaults = array( |
| 466 |
'post_status' => 'publish', |
| 467 |
'source_id' => null, |
| 468 |
'title' => '', |
| 469 |
'content' => '', |
| 470 |
'excerpt' => '', |
| 471 |
'authors' => array(), |
| 472 |
'series' => array(), |
| 473 |
'genres' => array(), |
| 474 |
'tags' => array(), |
| 475 |
'buybuttons' => '', |
| 476 |
'image_id' => '', |
| 477 |
'unique_id_isbn' => '', |
| 478 |
'unique_id_asin' => '', |
| 479 |
'series_order' => '', |
| 480 |
'display_mode' => '', |
| 481 |
'show_instant_preview' => '', |
| 482 |
'sample_url' => '', |
| 483 |
'sample_audio' => '', |
| 484 |
'sample_video' => '', |
| 485 |
'price' => '', |
| 486 |
'sale_price' => '', |
| 487 |
'ebook_price' => '', |
| 488 |
'audiobook_price' => '', |
| 489 |
'publisher_name' => '', |
| 490 |
'publisher_url' => '', |
| 491 |
'publication_year' => '', |
| 492 |
'star_rating' => '', |
| 493 |
'book_format' => '', |
| 494 |
'book_length' => '', |
| 495 |
'narrator' => '', |
| 496 |
'illustrator' => '', |
| 497 |
'bg_color' => '', |
| 498 |
'bg_color_alt' => '', |
| 499 |
'button_color' => '', |
| 500 |
'imported_book_id' => '', |
| 501 |
); |
| 502 |
$book = array_merge($defaults, $book); |
| 503 |
if(!empty($book['unique_id'])) { $book['unique_id_isbn'] = $book['unique_id']; } |
| 504 |
|
| 505 |
if(!empty($book['imported_book_id']) and ($imported_book = get_post($book['imported_book_id']))) { |
| 506 |
$post_id = wp_update_post(array( |
| 507 |
'ID' => $imported_book->ID, |
| 508 |
'post_title' => $book['title'], |
| 509 |
)); |
| 510 |
$old_buybuttons = get_post_meta($post_id, 'mbt_buybuttons', true); |
| 511 |
if(empty($old_buybuttons)) { update_post_meta($post_id, 'mbt_buybuttons', $book['buybuttons']); } |
| 512 |
if(!empty($book['image_id'])) { update_post_meta($post_id, 'mbt_book_image_id', $book['image_id']); } |
| 513 |
if(!empty($book['unique_id_isbn'])) { update_post_meta($post_id, 'unique_id_isbn', $book['unique_id_isbn']); } |
| 514 |
if(!empty($book['unique_id_asin'])) { update_post_meta($post_id, 'unique_id_asin', $book['unique_id_asin']); } |
| 515 |
if(!empty($book['series_order'])) { update_post_meta($post_id, 'mbt_series_order', $book['series_order']); } |
| 516 |
if(!empty($book['display_mode'])) { update_post_meta($post_id, 'mbt_display_mode', $book['display_mode']); } |
| 517 |
if(!empty($book['authors'])) { wp_set_object_terms($post_id, mbt_import_taxonomy_terms($book['authors'], 'mbt_author'), 'mbt_author'); } |
| 518 |
if(!empty($book['series'])) { wp_set_object_terms($post_id, mbt_import_taxonomy_terms($book['series'], 'mbt_series'), 'mbt_series'); } |
| 519 |
if(!empty($book['genres'])) { wp_set_object_terms($post_id, mbt_import_taxonomy_terms($book['genres'], 'mbt_genre'), 'mbt_genre'); } |
| 520 |
if(!empty($book['tags'])) { wp_set_object_terms($post_id, mbt_import_taxonomy_terms($book['tags'], 'mbt_tag'), 'mbt_tag'); } |
| 521 |
if(!empty($book['show_instant_preview'])) { update_post_meta($post_id, 'mbt_show_instant_preview', $book['show_instant_preview']); } |
| 522 |
if(!empty($book['sample_url'])) { update_post_meta($post_id, 'mbt_sample_url', $book['sample_url']); } |
| 523 |
if(!empty($book['sample_audio'])) { update_post_meta($post_id, 'mbt_sample_audio', $book['sample_audio']); } |
| 524 |
if(!empty($book['sample_video'])) { update_post_meta($post_id, 'mbt_sample_video', $book['sample_video']); } |
| 525 |
if(!empty($book['price'])) { update_post_meta($post_id, 'mbt_price', $book['price']); } |
| 526 |
if(!empty($book['sale_price'])) { update_post_meta($post_id, 'mbt_sale_price', $book['sale_price']); } |
| 527 |
if(!empty($book['ebook_price'])) { update_post_meta($post_id, 'mbt_ebook_price', $book['ebook_price']); } |
| 528 |
if(!empty($book['audiobook_price'])) { update_post_meta($post_id, 'mbt_audiobook_price', $book['audiobook_price']); } |
| 529 |
if(!empty($book['publisher_name'])) { update_post_meta($post_id, 'mbt_publisher_name', $book['publisher_name']); } |
| 530 |
if(!empty($book['publisher_url'])) { update_post_meta($post_id, 'mbt_publisher_url', $book['publisher_url']); } |
| 531 |
if(!empty($book['publication_year'])) { update_post_meta($post_id, 'mbt_publication_year', $book['publication_year']); } |
| 532 |
if(!empty($book['star_rating'])) { update_post_meta($post_id, 'mbt_star_rating', $book['star_rating']); } |
| 533 |
if(!empty($book['book_format'])) { update_post_meta($post_id, 'mbt_book_format', $book['book_format']); } |
| 534 |
if(!empty($book['book_length'])) { update_post_meta($post_id, 'mbt_book_length', $book['book_length']); } |
| 535 |
if(!empty($book['narrator'])) { update_post_meta($post_id, 'mbt_narrator', $book['narrator']); } |
| 536 |
if(!empty($book['illustrator'])) { update_post_meta($post_id, 'mbt_illustrator', $book['illustrator']); } |
| 537 |
if(!empty($book['bg_color'])) { update_post_meta($post_id, 'mbt_bg_color', $book['bg_color']); } |
| 538 |
if(!empty($book['bg_color_alt'])) { update_post_meta($post_id, 'mbt_bg_color_alt', $book['bg_color_alt']); } |
| 539 |
if(!empty($book['button_color'])) { update_post_meta($post_id, 'mbt_button_color', $book['button_color']); } |
| 540 |
} else { |
| 541 |
$post_id = wp_insert_post(array( |
| 542 |
'post_title' => $book['title'], |
| 543 |
'post_content' => $book['content'], |
| 544 |
'post_excerpt' => $book['excerpt'], |
| 545 |
'post_status' => $book['post_status'], |
| 546 |
'post_type' => 'mbt_book' |
| 547 |
)); |
| 548 |
update_post_meta($post_id, 'mbt_buybuttons', $book['buybuttons']); |
| 549 |
update_post_meta($post_id, 'mbt_unique_id_isbn', $book['unique_id_isbn']); |
| 550 |
update_post_meta($post_id, 'mbt_unique_id_asin', $book['unique_id_asin']); |
| 551 |
update_post_meta($post_id, 'mbt_series_order', $book['series_order']); |
| 552 |
update_post_meta($post_id, 'mbt_display_mode', $book['display_mode']); |
| 553 |
wp_set_object_terms($post_id, mbt_import_taxonomy_terms($book['authors'], 'mbt_author'), 'mbt_author'); |
| 554 |
wp_set_object_terms($post_id, mbt_import_taxonomy_terms($book['series'], 'mbt_series'), 'mbt_series'); |
| 555 |
wp_set_object_terms($post_id, mbt_import_taxonomy_terms($book['genres'], 'mbt_genre'), 'mbt_genre'); |
| 556 |
wp_set_object_terms($post_id, mbt_import_taxonomy_terms($book['tags'], 'mbt_tag'), 'mbt_tag'); |
| 557 |
update_post_meta($post_id, 'mbt_show_instant_preview', $book['show_instant_preview']); |
| 558 |
update_post_meta($post_id, 'mbt_sample_url', $book['sample_url']); |
| 559 |
update_post_meta($post_id, 'mbt_sample_audio', $book['sample_audio']); |
| 560 |
update_post_meta($post_id, 'mbt_sample_video', $book['sample_video']); |
| 561 |
update_post_meta($post_id, 'mbt_price', $book['price']); |
| 562 |
update_post_meta($post_id, 'mbt_sale_price', $book['sale_price']); |
| 563 |
update_post_meta($post_id, 'mbt_ebook_price', $book['ebook_price']); |
| 564 |
update_post_meta($post_id, 'mbt_audiobook_price', $book['audiobook_price']); |
| 565 |
update_post_meta($post_id, 'mbt_publisher_name', $book['publisher_name']); |
| 566 |
update_post_meta($post_id, 'mbt_publisher_url', $book['publisher_url']); |
| 567 |
update_post_meta($post_id, 'mbt_publication_year', $book['publication_year']); |
| 568 |
update_post_meta($post_id, 'mbt_star_rating', $book['star_rating']); |
| 569 |
update_post_meta($post_id, 'mbt_book_format', $book['book_format']); |
| 570 |
update_post_meta($post_id, 'mbt_book_length', $book['book_length']); |
| 571 |
update_post_meta($post_id, 'mbt_narrator', $book['narrator']); |
| 572 |
update_post_meta($post_id, 'mbt_illustrator', $book['illustrator']); |
| 573 |
update_post_meta($post_id, 'mbt_bg_color', $book['bg_color']); |
| 574 |
update_post_meta($post_id, 'mbt_bg_color_alt', $book['bg_color_alt']); |
| 575 |
update_post_meta($post_id, 'mbt_button_color', $book['button_color']); |
| 576 |
|
| 577 |
if(!empty($book['source_id'])) { update_post_meta($book['source_id'], 'mbt_imported_book_id', $post_id); } |
| 578 |
|
| 579 |
// download and import the book image |
| 580 |
if(isset($book['image_src']) && !empty($book['image_src'])) { |
| 581 |
$imgurl = stripslashes($book['image_src']); |
| 582 |
$attach_id = sh_mbt_download_and_insert_attachment_2($imgurl,$book['title']); |
| 583 |
if(isset($attach_id) && !empty($attach_id)){ |
| 584 |
update_post_meta($post_id, 'mbt_book_image_id', $attach_id); |
| 585 |
} |
| 586 |
} |
| 587 |
} |
| 588 |
return $post_id; |
| 589 |
} |
| 590 |
|
| 591 |
function mbt_import_taxonomy_terms($term_names, $taxonomy) { |
| 592 |
$returns = array(); |
| 593 |
foreach($term_names as $name) { |
| 594 |
if(term_exists($name, $taxonomy)) { |
| 595 |
$new_term = (array)get_term_by('name', $name, $taxonomy); |
| 596 |
} else { |
| 597 |
$new_term = (array)wp_insert_term($name, $taxonomy); |
| 598 |
} |
| 599 |
$returns[] = $new_term['term_id']; |
| 600 |
} |
| 601 |
return $returns; |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
/*---------------------------------------------------------*/ |
| 607 |
/* Pages */ |
| 608 |
/*---------------------------------------------------------*/ |
| 609 |
|
| 610 |
function mbt_add_custom_page($name, $function, $permissions="edit_posts") { |
| 611 |
$add_page = function() use ($name, $permissions, $function) { |
| 612 |
add_submenu_page("mbt_dashboard", "", "", $permissions, $name, $function); |
| 613 |
}; |
| 614 |
|
| 615 |
$remove_page = function() use ($name) { |
| 616 |
remove_submenu_page("mbt_dashboard", $name); |
| 617 |
}; |
| 618 |
|
| 619 |
add_action('admin_menu', $add_page, 9); |
| 620 |
add_action('admin_head', $remove_page); |
| 621 |
} |
| 622 |
|
| 623 |
function mbt_get_custom_page_url($name) { |
| 624 |
return admin_url('admin.php?page='.$name); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
/*---------------------------------------------------------*/ |
| 630 |
/* Styles */ |
| 631 |
/*---------------------------------------------------------*/ |
| 632 |
|
| 633 |
function mbt_image_url($image) { |
| 634 |
$url = mbt_current_style_url($image); |
| 635 |
return apply_filters('mbt_image_url', empty($url) ? plugins_url('styles/'.mbt_get_default_style_pack().'/'.$image, dirname(__FILE__)) : $url, $image); |
| 636 |
} |
| 637 |
|
| 638 |
function mbt_current_style_url($file) { |
| 639 |
$style = mbt_get_setting('style_pack'); |
| 640 |
if(empty($style)) { $style = mbt_get_default_style_pack(); } |
| 641 |
|
| 642 |
$url = mbt_style_url($file, $style); |
| 643 |
if(empty($url) and $style !== mbt_get_default_style_pack()) { $url = mbt_style_url($file, mbt_get_default_style_pack()); } |
| 644 |
|
| 645 |
return $url; |
| 646 |
} |
| 647 |
|
| 648 |
function mbt_style_url($file, $style) { |
| 649 |
foreach(mbt_get_style_folders() as $folder) { |
| 650 |
if(file_exists($folder['dir'].'/'.$style)) { |
| 651 |
if(file_exists($folder['dir'].'/'.$style.'/'.$file)) { |
| 652 |
return $folder['url'].'/'.rawurlencode($style).'/'.$file; |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
$meta = mbt_get_style_pack_meta($style); |
| 658 |
if($meta['template']) { return mbt_style_url($file, $meta['template']); } |
| 659 |
|
| 660 |
return ''; |
| 661 |
} |
| 662 |
|
| 663 |
function mbt_get_style_packs() { |
| 664 |
$folders = mbt_get_style_folders(); |
| 665 |
$styles = array(); |
| 666 |
|
| 667 |
foreach($folders as $folder) { |
| 668 |
if(file_exists($folder['dir']) and $handle = opendir($folder['dir'])) { |
| 669 |
while(false !== ($entry = readdir($handle))) { |
| 670 |
if ($entry != '.' and $entry != '..' and !in_array($entry, $styles)) { |
| 671 |
$styles[] = $entry; |
| 672 |
} |
| 673 |
} |
| 674 |
closedir($handle); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
sort($styles); |
| 679 |
return $styles; |
| 680 |
} |
| 681 |
|
| 682 |
function mbt_get_style_pack_meta($style) { |
| 683 |
$default_headers = array( |
| 684 |
'name' => 'Style Pack Name', |
| 685 |
'stylepack_uri' => 'Style Pack URI', |
| 686 |
'template' => 'Template', |
| 687 |
'version' => 'Version', |
| 688 |
'desc' => 'Description', |
| 689 |
'author' => 'Author', |
| 690 |
'author_uri' => 'Author URI', |
| 691 |
); |
| 692 |
|
| 693 |
$data = array( |
| 694 |
'name' => $style, |
| 695 |
'stylepack_uri' => '', |
| 696 |
'template' => '', |
| 697 |
'version' => '', |
| 698 |
'desc' => '', |
| 699 |
'author' => '', |
| 700 |
'author_uri' => '', |
| 701 |
); |
| 702 |
|
| 703 |
$readme = ''; |
| 704 |
foreach(mbt_get_style_folders() as $folder) { |
| 705 |
if(file_exists($folder['dir'].'/'.$style)) { |
| 706 |
if(file_exists($folder['dir'].'/'.$style.'/readme.txt')) { |
| 707 |
$readme = $folder['dir'].'/'.$style.'/readme.txt'; |
| 708 |
break; |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
if($readme) { $data = get_file_data($readme, $default_headers, 'mbt_style_pack'); } |
| 713 |
|
| 714 |
return $data; |
| 715 |
} |
| 716 |
|
| 717 |
function mbt_get_default_style_pack() { |
| 718 |
return apply_filters('mbt_default_style_pack', 'silver'); |
| 719 |
} |
| 720 |
|
| 721 |
function mbt_get_style_folders() { |
| 722 |
return apply_filters('mbt_style_folders', array()); |
| 723 |
} |
| 724 |
|
| 725 |
function mbt_add_default_style_folder($folders) { |
| 726 |
$folders[] = array('dir' => plugin_dir_path(dirname(__FILE__)).'styles', 'url' => plugins_url('styles', dirname(__FILE__))); |
| 727 |
return $folders; |
| 728 |
} |
| 729 |
add_filter('mbt_style_folders', 'mbt_add_default_style_folder', 100); |
| 730 |
|
| 731 |
function mbt_add_uploaded_style_folder($folders) { |
| 732 |
$upload_dir = wp_upload_dir(); |
| 733 |
$folders[] = array('dir' => $upload_dir['basedir'].DIRECTORY_SEPARATOR.'mbt_styles', 'url' => $upload_dir['baseurl'].'/'.'mbt_styles'); |
| 734 |
return $folders; |
| 735 |
} |
| 736 |
add_filter('mbt_style_folders', 'mbt_add_uploaded_style_folder', 100); |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
/*---------------------------------------------------------*/ |
| 741 |
/* Analytics */ |
| 742 |
/*---------------------------------------------------------*/ |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
/*---------------------------------------------------------*/ |
| 747 |
/* API / Upgrades */ |
| 748 |
/*---------------------------------------------------------*/ |
| 749 |
|
| 750 |
function mbt_verify_api_key() { |
| 751 |
global $wp_version; |
| 752 |
|
| 753 |
$to_send = array( |
| 754 |
'action' => 'basic_check', |
| 755 |
'version' => MBT_VERSION, |
| 756 |
'api-key' => mbt_get_setting('api_key'), |
| 757 |
'site' => get_bloginfo('url') |
| 758 |
); |
| 759 |
|
| 760 |
$options = array( |
| 761 |
'timeout' => 3, |
| 762 |
'body' => $to_send, |
| 763 |
'user-agent' => 'WordPress/'.$wp_version |
| 764 |
); |
| 765 |
|
| 766 |
// added by stormhill 6/14/24 |
| 767 |
$checkmode = mbt_get_setting('key_mode'); |
| 768 |
|
| 769 |
$raw_response = wp_remote_post('https://api.stormhillmedia.com/plugins/apikey/check', $options); |
| 770 |
|
| 771 |
if(isset($checkmode) && $checkmode == 'mybooktable-dev3') { |
| 772 |
mbt_update_setting('api_key_status', 10); |
| 773 |
mbt_update_setting('upgrade_active', 'mybooktable-dev3'); |
| 774 |
mbt_update_setting('api_key_message', __('Valid for MyBookTable Developer 3.0', 'mybooktable')); |
| 775 |
return; |
| 776 |
} else if(isset($checkmode) && $checkmode == 'mybooktable-pro3') { |
| 777 |
mbt_update_setting('api_key_status', 10); |
| 778 |
mbt_update_setting('upgrade_active', 'mybooktable-pro3'); |
| 779 |
mbt_update_setting('api_key_message', __('Valid for MyBookTable Professional 3.0', 'mybooktable')); |
| 780 |
return; |
| 781 |
} |
| 782 |
// end edits 6/14/24 |
| 783 |
if(is_wp_error($raw_response) || 200 != wp_remote_retrieve_response_code($raw_response)) { |
| 784 |
mbt_update_setting('api_key_status', -1); |
| 785 |
mbt_update_setting('upgrade_active', false); |
| 786 |
mbt_update_setting('api_key_message', __('Unable to connect to server!', 'mybooktable')); |
| 787 |
return; |
| 788 |
} |
| 789 |
|
| 790 |
$response = maybe_unserialize(wp_remote_retrieve_body($raw_response)); |
| 791 |
|
| 792 |
if(!is_array($response) or empty($response['status'])) { |
| 793 |
mbt_update_setting('api_key_status', -2); |
| 794 |
mbt_update_setting('api_key_message', __('Invalid response received from server', 'mybooktable')); |
| 795 |
return; |
| 796 |
} |
| 797 |
|
| 798 |
$status = $response['status']; |
| 799 |
|
| 800 |
if($status >= 10) { |
| 801 |
$permissions = array(); |
| 802 |
if(!empty($response['permissions']) and is_array($response['permissions'])) { |
| 803 |
$permissions = $response['permissions']; |
| 804 |
} |
| 805 |
|
| 806 |
if(in_array('mybooktable-dev3', $permissions)) { |
| 807 |
mbt_update_setting('api_key_status', $status); |
| 808 |
mbt_update_setting('upgrade_active', 'mybooktable-dev3'); |
| 809 |
mbt_update_setting('api_key_message', __('Valid for MyBookTable Developer 3.0', 'mybooktable')); |
| 810 |
} else if(in_array('mybooktable-pro3', $permissions)) { |
| 811 |
mbt_update_setting('api_key_status', $status); |
| 812 |
mbt_update_setting('upgrade_active', 'mybooktable-pro3'); |
| 813 |
mbt_update_setting('api_key_message', __('Valid for MyBookTable Professional 3.0', 'mybooktable')); |
| 814 |
} else if(in_array('mybooktable-dev2', $permissions)) { |
| 815 |
mbt_update_setting('api_key_status', $status); |
| 816 |
mbt_update_setting('upgrade_active', 'mybooktable-dev2'); |
| 817 |
mbt_update_setting('api_key_message', __('Valid for MyBookTable Developer 2.0', 'mybooktable')); |
| 818 |
} else if(in_array('mybooktable-pro2', $permissions)) { |
| 819 |
mbt_update_setting('api_key_status', $status); |
| 820 |
mbt_update_setting('upgrade_active', 'mybooktable-pro2'); |
| 821 |
mbt_update_setting('api_key_message', __('Valid for MyBookTable Professional 2.0', 'mybooktable')); |
| 822 |
} else if(in_array('mybooktable-dev', $permissions)) { |
| 823 |
mbt_update_setting('api_key_status', $status); |
| 824 |
mbt_update_setting('upgrade_active', 'mybooktable-dev'); |
| 825 |
mbt_update_setting('api_key_message', __('Valid for MyBookTable Developer 1.0', 'mybooktable')); |
| 826 |
} else if(in_array('mybooktable-pro', $permissions)) { |
| 827 |
mbt_update_setting('api_key_status', $status); |
| 828 |
mbt_update_setting('upgrade_active', 'mybooktable-pro'); |
| 829 |
mbt_update_setting('api_key_message', __('Valid for MyBookTable Professional 1.0', 'mybooktable')); |
| 830 |
} else { |
| 831 |
mbt_update_setting('api_key_status', -20); |
| 832 |
mbt_update_setting('api_key_message', __('Permissions error!', 'mybooktable')); |
| 833 |
mbp_update_setting('upgrade_active', false); |
| 834 |
} |
| 835 |
} else if($status == -10) { |
| 836 |
mbt_update_setting('api_key_status', $status); |
| 837 |
mbt_update_setting('api_key_message', __('Key not found', 'mybooktable')); |
| 838 |
mbt_update_setting('upgrade_active', false); |
| 839 |
} else if($status == -11) { |
| 840 |
mbt_update_setting('api_key_status', $status); |
| 841 |
mbt_update_setting('api_key_message', __('Key has been deactivated', 'mybooktable')); |
| 842 |
mbt_update_setting('upgrade_active', false); |
| 843 |
} else { |
| 844 |
mbt_update_setting('api_key_status', -2); |
| 845 |
mbt_update_setting('api_key_message', __('Invalid response received from server', 'mybooktable')); |
| 846 |
mbt_update_setting('upgrade_active', false); |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
function mbt_init_api_key_check() { |
| 851 |
if(!wp_next_scheduled('mbt_periodic_api_key_check')) { wp_schedule_event(time(), 'weekly', 'mbt_periodic_api_key_check'); } |
| 852 |
add_action('mbt_periodic_api_key_check', 'mbt_verify_api_key'); |
| 853 |
} |
| 854 |
add_action('mbt_init', 'mbt_init_api_key_check'); |
| 855 |
|
| 856 |
function mbt_get_upgrade() { |
| 857 |
$upgrade_active = mbt_get_setting('upgrade_active'); |
| 858 |
return empty($upgrade_active) ? false : $upgrade_active; |
| 859 |
} |
| 860 |
|
| 861 |
function mbt_get_upgrade_version() { |
| 862 |
$upgrade = mbt_get_upgrade(); |
| 863 |
if($upgrade == 'mybooktable-dev3' and defined('MBTDEV3_VERSION')) { return MBTDEV3_VERSION; } |
| 864 |
if($upgrade == 'mybooktable-pro3' and defined('MBTPRO3_VERSION')) { return MBTPRO3_VERSION; } |
| 865 |
if($upgrade == 'mybooktable-dev2' and defined('MBTDEV2_VERSION')) { return MBTDEV2_VERSION; } |
| 866 |
if($upgrade == 'mybooktable-pro2' and defined('MBTPRO2_VERSION')) { return MBTPRO2_VERSION; } |
| 867 |
if($upgrade == 'mybooktable-dev' and defined('MBTDEV_VERSION')) { return MBTDEV_VERSION; } |
| 868 |
if($upgrade == 'mybooktable-pro' and defined('MBTPRO_VERSION')) { return MBTPRO_VERSION; } |
| 869 |
return false; |
| 870 |
} |
| 871 |
|
| 872 |
function mbt_get_upgrade_plugin_exists($active=true) { |
| 873 |
if(!$active) { return defined('MBTDEV3_VERSION') or defined('MBTPRO3_VERSION') or defined('MBTDEV2_VERSION') or defined('MBTPRO2_VERSION') or defined('MBTDEV_VERSION') or defined('MBTPRO_VERSION'); } |
| 874 |
$upgrade = mbt_get_upgrade(); |
| 875 |
if($upgrade == 'mybooktable-dev3') { return defined('MBTDEV3_VERSION'); } |
| 876 |
if($upgrade == 'mybooktable-pro3') { return defined('MBTPRO3_VERSION'); } |
| 877 |
if($upgrade == 'mybooktable-dev2') { return defined('MBTDEV2_VERSION'); } |
| 878 |
if($upgrade == 'mybooktable-pro2') { return defined('MBTPRO2_VERSION'); } |
| 879 |
if($upgrade == 'mybooktable-dev') { return defined('MBTDEV_VERSION'); } |
| 880 |
if($upgrade == 'mybooktable-pro') { return defined('MBTPRO_VERSION'); } |
| 881 |
return false; |
| 882 |
} |
| 883 |
|
| 884 |
function mbt_get_upgrade_message($require_upgrade=true, $upgrade_text=null, $thankyou_text=null) { |
| 885 |
if(mbt_get_upgrade()) { |
| 886 |
if(mbt_get_upgrade_plugin_exists()) { |
| 887 |
if($require_upgrade) { |
| 888 |
return '<a href="https://www.stormhillmedia.com/all-products/mybooktable/upgrades/" target="_blank">'.($upgrade_text !== null ? $upgrade_text : __('Upgrade your MyBookTable to enable these advanced features!', 'mybooktable')).'</a>'; |
| 889 |
} else { |
| 890 |
return ($thankyou_text !== null ? $thankyou_text : (__('Thank you for purchasing a MyBookTable Upgrade!', 'mybooktable').' <a href="https://www.stormhillmedia.com/book-table/premium-support/" target="_blank">'.__('Get premium support.', 'mybooktable').'</a>')); |
| 891 |
} |
| 892 |
} else { |
| 893 |
$upgradepage_url = |
| 894 |
wp_nonce_url(admin_url('admin.php?page=mbt_dashboard&subpage=mbt_get_upgrade_page'),'dashboard_nonce','nonce_dashboard'); |
| 895 |
return '<a href="'.$upgradepage_url.'">'.__('Download your MyBookTable Upgrade plugin to enable your advanced features!', 'mybooktable').'</a>'; |
| 896 |
} |
| 897 |
} else { |
| 898 |
if(mbt_get_upgrade_plugin_exists(false)) { |
| 899 |
$api_key = mbt_get_setting('api_key'); |
| 900 |
if(empty($api_key)) { |
| 901 |
return '<a href="'.admin_url('admin.php?page=mbt_settings').'">'.__('Insert your License Key to enable your advanced features!', 'mybooktable').'</a>'; |
| 902 |
} else { |
| 903 |
return '<a href="'.admin_url('admin.php?page=mbt_settings').'">'.__('Update your License Key to enable your advanced features!', 'mybooktable').'</a>'; |
| 904 |
} |
| 905 |
} else { |
| 906 |
return '<a href="https://www.stormhillmedia.com/all-products/mybooktable/upgrades/" target="_blank">'.($upgrade_text !== null ? $upgrade_text : __('Upgrade your MyBookTable to enable these advanced features!', 'mybooktable')).'</a>'; |
| 907 |
} |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
// import image |
| 912 |
if(!function_exists('sh_mbt_download_and_insert_attachment_2')){ |
| 913 |
function sh_mbt_download_and_insert_attachment_2($url,$name) { |
| 914 |
$raw_response = wp_remote_get($url, array('timeout' => 3)); |
| 915 |
if(is_wp_error($raw_response) or wp_remote_retrieve_response_code($raw_response) != 200) { return 0; } |
| 916 |
$file_data = wp_remote_retrieve_body($raw_response); |
| 917 |
$file_header = wp_remote_retrieve_headers($raw_response); |
| 918 |
|
| 919 |
$nonce_url = wp_nonce_url('admin.php', 'mbt_download_and_insert_attachment'); |
| 920 |
$output = mbt_get_wp_filesystem($nonce_url); |
| 921 |
if(!empty($output)) { return 0; } |
| 922 |
global $wp_filesystem, $wpdb; |
| 923 |
|
| 924 |
$url_parts = wp_parse_url($url); |
| 925 |
$filename = str_replace(' ','-',strtolower($name)).'-book-image.jpg'; |
| 926 |
$filename = preg_replace('/[^A-Za-z0-9_.-]/', '', $filename); |
| 927 |
|
| 928 |
$upload_dir = wp_upload_dir(); |
| 929 |
$filepath = $upload_dir['path'].'/'.$filename; |
| 930 |
$fileurl = $upload_dir['url'].'/'.$filename; |
| 931 |
|
| 932 |
if($wp_filesystem->exists($filepath)) { |
| 933 |
|
| 934 |
$existing_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid=%s", $fileurl)); |
| 935 |
if(!empty($existing_id)) { return $existing_id; } else { return 0; } |
| 936 |
} |
| 937 |
|
| 938 |
if(!$wp_filesystem->put_contents($filepath, $file_data, FS_CHMOD_FILE)) { return 0; } |
| 939 |
|
| 940 |
$filetype = wp_check_filetype(basename($filepath), null); |
| 941 |
$attachment = array( |
| 942 |
'guid' => $fileurl, |
| 943 |
'post_mime_type' => $filetype['type'], |
| 944 |
'post_title' => preg_replace('/\.[^.]+$/', '', $filename), |
| 945 |
'post_content' => '', |
| 946 |
'post_status' => 'inherit' |
| 947 |
); |
| 948 |
|
| 949 |
$attach_id = wp_insert_attachment($attachment, $filepath); |
| 950 |
|
| 951 |
require_once(ABSPATH.'wp-admin/includes/image.php'); |
| 952 |
$attach_data = wp_generate_attachment_metadata($attach_id, $filepath); |
| 953 |
wp_update_attachment_metadata($attach_id, $attach_data); |
| 954 |
|
| 955 |
return $attach_id; |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
function allow_admin_script_tags( $allowedposttags,$context ){ |
| 960 |
if(!is_admin() || $context != 'post'){ |
| 961 |
return $allowedposttags; |
| 962 |
} |
| 963 |
$allowedposttags['script'] = array( |
| 964 |
'src' => true, |
| 965 |
'type' => true, |
| 966 |
'charset' => true, |
| 967 |
'class' =>true, |
| 968 |
'async' =>true |
| 969 |
); |
| 970 |
$allowedposttags['input'] = array( |
| 971 |
'id' => true, |
| 972 |
'type' => true, |
| 973 |
'value' => true, |
| 974 |
'name' => true, |
| 975 |
'class' =>true, |
| 976 |
'data-*' =>true, |
| 977 |
'checked' =>true |
| 978 |
); |
| 979 |
$allowedposttags['select'] = array( |
| 980 |
'name' => true, |
| 981 |
'id' => true, |
| 982 |
'class' => true, |
| 983 |
'value' =>true, |
| 984 |
'type' =>true |
| 985 |
); |
| 986 |
$allowedposttags['option'] = array( |
| 987 |
'selected' => true, |
| 988 |
); |
| 989 |
$allowedposttags['textarea'] = array( |
| 990 |
'name' => true, |
| 991 |
'id' => true, |
| 992 |
'class' => true, |
| 993 |
'cols' =>true, |
| 994 |
); |
| 995 |
return $allowedposttags; |
| 996 |
} |
| 997 |
add_filter('wp_kses_allowed_html','allow_admin_script_tags', 50, 2); |
| 998 |
|
| 999 |
function allow_script_tags( $allowedposttags,$context ){ |
| 1000 |
if( is_admin() || $context != 'post'){ |
| 1001 |
return $allowedposttags; |
| 1002 |
} |
| 1003 |
$allowedposttags['iframe'] = array( |
| 1004 |
'src' => true, |
| 1005 |
'height' => true, |
| 1006 |
'width' => true, |
| 1007 |
'class' => true, |
| 1008 |
'frameborder' => true, |
| 1009 |
'allowfullscreen' => true, |
| 1010 |
); |
| 1011 |
$allowedposttags['audio'] = array( |
| 1012 |
'controls' => true, |
| 1013 |
); |
| 1014 |
$allowedposttags['source'] = array( |
| 1015 |
'src' => true, |
| 1016 |
'type' => true, |
| 1017 |
); |
| 1018 |
$allowedposttags['script'] = array( |
| 1019 |
'src' => true, |
| 1020 |
'type' => true, |
| 1021 |
'charset' => true, |
| 1022 |
'class' =>true, |
| 1023 |
'async' =>true |
| 1024 |
); |
| 1025 |
|
| 1026 |
return $allowedposttags; |
| 1027 |
} |
| 1028 |
add_filter('wp_kses_allowed_html','allow_script_tags', 50, 2); |
| 1029 |
|
| 1030 |
|