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

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

390 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4 /*---------------------------------------------------------*/
5 /* Check for Updates */
6 /*---------------------------------------------------------*/
7
8 function mbt_update_check() {
9 $version = mbt_get_setting('version');
10
11 if(version_compare($version, '1.1.0') < 0) { mbt_update_1_1_0(); }
12 if(version_compare($version, '1.1.3') < 0) { mbt_update_1_1_3(); }
13 if(version_compare($version, '1.1.4') < 0) { mbt_update_1_1_4(); }
14 if(version_compare($version, '1.2.7') < 0) { mbt_update_1_2_7(); }
15 if(version_compare($version, '1.3.1') < 0) { mbt_update_1_3_1(); }
16 if(version_compare($version, '1.3.8') < 0) { mbt_update_1_3_8(); }
17 if(version_compare($version, '2.0.1') < 0) { mbt_update_2_0_1(); }
18 if(version_compare($version, '2.0.4') < 0) { mbt_update_2_0_4(); }
19 if(version_compare($version, '2.1.0') < 0) { mbt_update_2_1_0(); }
20 if(version_compare($version, '2.2.0') < 0) { mbt_update_2_2_0(); }
21 if(version_compare($version, '2.3.0') < 0) { mbt_update_2_3_0(); }
22 if(version_compare($version, '3.0.0') < 0) { mbt_update_3_0_0(); }
23
24 if($version !== MBT_VERSION) {
25 mbt_update_setting('version', MBT_VERSION);
26 }
27 }
28
29 function mbt_update_1_1_0() {
30 if(mbt_get_setting('compatibility_mode') !== false) {
31 mbt_update_setting('compatibility_mode', true);
32 }
33 }
34
35 function mbt_update_1_1_3() {
36 global $wpdb;
37 $books = $wpdb->get_col('SELECT ID FROM '.$wpdb->posts.' WHERE post_type = "mbt_book"');
38 if(!empty($books)) {
39 foreach($books as $book_id) {
40 $image_id = get_post_meta($book_id, '_thumbnail_id', true);
41 $mbt_book_image_id = get_post_meta($book_id, 'mbt_book_image_id', true);
42 if(empty($mbt_book_image_id) && !empty($image_id)) { update_post_meta($book_id, 'mbt_book_image_id', $image_id); }
43 }
44 }
45 }
46
47 function mbt_update_1_1_4() {
48 global $wpdb;
49 $books = $wpdb->get_col('SELECT ID FROM '.$wpdb->posts.' WHERE post_type = "mbt_book"');
50 if(!empty($books)) {
51 foreach($books as $book_id) {
52 delete_post_meta($book_id, '_thumbnail_id');
53
54 $buybuttons = get_post_meta($book_id, 'mbt_buybuttons', true);
55 if(is_array($buybuttons) and !empty($buybuttons)) {
56 for($i = 0; $i < count($buybuttons); $i++)
57 {
58 if($buybuttons[$i]['type']) {
59 $buybuttons[$i]['store'] = $buybuttons[$i]['type'];
60 unset($buybuttons[$i]['type']);
61 }
62 }
63 }
64 update_post_meta($book_id, 'mbt_buybuttons', $buybuttons);
65 }
66 }
67 }
68
69 function mbt_update_1_2_7() {
70 if(mbt_get_setting('enable_default_affiliates') !== false) {
71 mbt_update_setting('enable_default_affiliates', true);
72 }
73 }
74
75 function mbt_update_1_3_1() {
76 //mbt_update_setting('help_page_email_subscribe_popup', 'show');
77 mbt_update_setting('product_name', __("Books", 'mybooktable'));
78 mbt_update_setting('product_slug', _x('books', 'URL slug', 'mybooktable'));
79 }
80
81 function mbt_update_1_3_8() {
82 mbt_update_setting('domc_notice_text', esc_attr_e('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'));
83 }
84
85 function mbt_update_2_0_1() {
86 mbt_verify_api_key();
87 }
88
89 function mbt_update_2_0_4() {
90 mbt_update_setting('show_find_bookstore_buybuttons_shadowbox', true);
91 }
92
93 function mbt_update_2_1_0() {
94 global $wpdb;
95 $books = $wpdb->get_col('SELECT ID FROM '.$wpdb->posts.' WHERE post_type = "mbt_book"');
96 if(!empty($books)) {
97 foreach($books as $book_id) {
98 $buybuttons = get_post_meta($book_id, 'mbt_buybuttons', true);
99 if(is_array($buybuttons) and !empty($buybuttons)) {
100 for($i = 0; $i < count($buybuttons); $i++) {
101 $buybuttons[$i]['display'] = ($buybuttons[$i]['display'] == 'text_only' or $buybuttons[$i]['display'] == 'text') ? 'text' : 'button';
102 }
103 }
104 update_post_meta($book_id, 'mbt_buybuttons', $buybuttons);
105 }
106 }
107
108 mbt_update_setting('buybutton_shadowbox', mbt_get_setting('enable_buybutton_shadowbox') ? 'all' : 'none');
109 }
110
111 function mbt_update_2_2_0() {
112 global $wpdb;
113 $books = $wpdb->get_col('SELECT ID FROM '.$wpdb->posts.' WHERE post_type = "mbt_book"');
114 if(!empty($books)) {
115 foreach($books as $book_id) {
116 update_post_meta($book_id, 'mbt_unique_id_isbn', get_post_meta($book_id, 'mbt_unique_id', true));
117 }
118 }
119 }
120
121 function mbt_update_2_3_0() {
122 $style_pack = mbt_get_setting('style_pack');
123 if($style_pack == 'Default') { mbt_update_setting('style_pack', 'silver'); }
124 if($style_pack == 'Golden') { mbt_update_setting('style_pack', 'golden'); }
125 if($style_pack == 'Blue Flat') { mbt_update_setting('style_pack', 'blue_flat'); }
126 if($style_pack == 'Gold Flat') { mbt_update_setting('style_pack', 'gold_flat'); }
127 if($style_pack == 'Green Flat') { mbt_update_setting('style_pack', 'green_flat'); }
128 if($style_pack == 'Grey Flat') { mbt_update_setting('style_pack', 'grey_flat'); }
129 if($style_pack == 'Orange Flat') { mbt_update_setting('style_pack', 'orange_flat'); }
130 }
131
132 function mbt_update_3_0_0() {
133 mbt_update_setting('show_about_author', true);
134 mbt_update_setting('reviews_type', mbt_get_setting('reviews_box'));
135 mbt_update_setting('enable_socialmedia_single_book', mbt_get_setting('enable_socialmedia_badges_single_book') or mbt_get_setting('enable_socialmedia_bar_single_book'));
136 mbt_update_setting('enable_socialmedia_book_excerpt', mbt_get_setting('enable_socialmedia_badges_book_excerpt'));
137
138 $books_query = new WP_Query(array('post_type' => 'mbt_book', 'posts_per_page' => -1));
139 foreach ($books_query->posts as $book) {
140 update_post_meta($book->ID, 'mbt_display_mode', 'storefront');
141
142 if(get_post_meta($book->ID, 'mbt_unique_id_asin', true) == '') {
143 $buybuttons = get_post_meta($book->ID, 'mbt_buybuttons', true);
144 if(is_array($buybuttons) and !empty($buybuttons)) {
145 foreach($buybuttons as $buybutton) {
146 if($buybutton['store'] == 'amazon' or $buybutton['store'] == 'kindle') {
147 $asin = mbt_get_amazon_AISN($buybutton['url']);
148 if(!empty($asin)) { update_post_meta($book->ID, 'mbt_unique_id_asin', $asin); }
149 break;
150 }
151 }
152 }
153 }
154 }
155 }
156
157
158
159 /*---------------------------------------------------------*/
160 /* Rewrites Check */
161 /*---------------------------------------------------------*/
162
163 function mbt_rewrites_check_init() {
164 add_action('wp_loaded', 'mbt_rewrites_check', 999);
165 }
166 add_action('mbt_init', 'mbt_rewrites_check_init');
167
168 function mbt_rewrites_check() {
169 if(!mbt_check_rewrites()) {
170 flush_rewrite_rules();
171 if(!mbt_check_rewrites()) { add_action('admin_notices', 'mbt_rewrites_check_admin_notice'); }
172 }
173 }
174
175 function mbt_check_rewrites() {
176 global $pagenow;
177 $settings_updated = filter_input(INPUT_GET,'settings-updated');
178
179 if($pagenow == 'options-permalink.php' and !empty($settings_updated)) { return true; }
180
181 global $wp_rewrite;
182 $rules = $wp_rewrite->wp_rewrite_rules();
183 if(empty($rules) or !is_array($rules)) { return true; }
184
185 $book_page_correct = false;
186 $books = new WP_Query(array('post_type' => 'mbt_book', 'post_status' => 'publish', 'posts_per_page' => 1));
187 if(empty($books->posts)) {
188 $book_page_correct = true;
189 } else {
190 $book = $books->posts[0];
191 $book_page_correct = mbt_get_rewrite($rules, get_permalink($book)) === 'index.php?mbt_book=$matches[1]&page=$matches[2]';
192 }
193
194 $archive_correct = mbt_get_rewrite($rules, get_post_type_archive_link('mbt_book')) === 'index.php?post_type=mbt_book';
195 $genres_correct = mbt_check_tax_rewrites($rules, 'mbt_genre');
196 $authors_correct = mbt_check_tax_rewrites($rules, 'mbt_author');
197 $series_correct = mbt_check_tax_rewrites($rules, 'mbt_series');
198 $tags_correct = mbt_check_tax_rewrites($rules, 'mbt_tag');
199
200 return $archive_correct and $book_page_correct and $genres_correct and $authors_correct and $series_correct and $tags_correct;
201 }
202
203 function mbt_check_tax_rewrites($rules, $tax) {
204 $terms = get_terms($tax);
205 if(empty($terms)) { return true; }
206 return mbt_get_rewrite($rules, get_term_link(reset($terms), $tax)) === 'index.php?'.$tax.'=$matches[1]';
207 }
208
209 function mbt_get_rewrite($rules, $url) {
210 $parts = wp_parse_url(home_url('/'));
211 $default_path = $parts['path'];
212 $parts = wp_parse_url($url);
213 $url = $parts['path'];
214 $url = substr($url, strlen($default_path));
215
216 foreach($rules as $match => $query) {
217 if(preg_match("#^$match#", $url)) {
218 return $query;
219 }
220 }
221 return '';
222 }
223
224 function mbt_rewrites_check_admin_notice() {
225 ?>
226 <div id="message" class="error">
227 <p>
228 <strong><?php esc_attr_e('MyBookTable Rewrites Error', 'mybooktable'); ?></strong> &#8211;
229 <?php esc_attr_e('You have a plugin or theme that has post types or taxonomies that are conflicting with MyBookTable. MyBookTable pages will not display correctly.', 'mybooktable'); ?>
230 </p>
231 </div>
232 <?php
233 }
234
235
236
237 /*---------------------------------------------------------*/
238 /* Admin notices */
239 /*---------------------------------------------------------*/
240
241 function mbt_admin_notices_init() {
242 add_action('admin_init', 'mbt_add_admin_notices', 20);
243 }
244 add_action('mbt_init', 'mbt_admin_notices_init');
245
246 function mbt_add_admin_notices() {
247 $install_mbt = filter_input(INPUT_GET,'install_mbt');
248 $skip_install_mbt = filter_input(INPUT_GET,'skip_install_mbt');
249 $mbt_setup_default_affiliates = filter_input(INPUT_GET,'mbt_setup_default_affiliates');
250 $mbt_finish_install = filter_input(INPUT_GET,'mbt_finish_install');
251 $mbt_install_examples = filter_input(INPUT_GET,'mbt_install_examples');
252 $mbt_add_booktable_page = filter_input(INPUT_GET,'mbt_add_booktable_page');
253 $mbt_remove_booktable_page = filter_input(INPUT_GET,'mbt_remove_booktable_page');
254 if(!mbt_get_setting('installed')) {
255 if(isset($install_mbt)) {
256 mbt_install();
257 mbt_update_setting('installed', 'check_api_key');
258 } elseif(isset($skip_install_mbt) || mbt_get_setting('booktable_page') != 0) {
259 mbt_update_setting('installed', 'check_api_key');
260 } else {
261 add_action('admin_notices', 'mbt_admin_install_notice');
262 }
263 }
264 if(mbt_get_setting('installed') == 'check_api_key') {
265 if(!mbt_get_setting('api_key') and mbt_get_upgrade_plugin_exists(false)) {
266 add_action('admin_notices', 'mbt_admin_setup_api_key_notice');
267 } else {
268 mbt_update_setting('installed', 'setup_default_affiliates');
269 }
270 }
271 if(mbt_get_setting('installed') == 'setup_default_affiliates') {
272 if(!mbt_get_setting('enable_default_affiliates') and mbt_get_upgrade() === false and !isset($mbt_setup_default_affiliates)) {
273 add_action('admin_notices', 'mbt_admin_setup_default_affiliates_notice');
274 } else {
275 mbt_update_setting('installed', 'post_install');
276 }
277 }
278 if(mbt_get_setting('installed') == 'post_install') {
279 if(isset($mbt_finish_install)) {
280 do_action('mbt_installed');
281 mbt_update_setting('installed', 'done');
282 } else {
283 add_action('admin_notices', 'mbt_admin_installed_notice');
284 }
285 }
286 if(mbt_get_setting('installed') == 'done' or is_int(mbt_get_setting('installed'))) {
287 if(!mbt_get_setting('api_key') and mbt_get_upgrade_plugin_exists(false)) {
288 add_action('admin_notices', 'mbt_admin_setup_api_key_notice');
289 } else if(mbt_get_upgrade() and !mbt_get_upgrade_plugin_exists()) {
290 add_action('admin_notices', 'mbt_admin_enable_upgrade_notice');
291 }
292 }
293
294 if(isset($mbt_install_examples)) {
295 mbt_install_examples();
296 }
297
298 if(isset($mbt_add_booktable_page)) {
299 if((isset($_REQUEST['nonce_add_page']) && wp_verify_nonce(sanitize_key($_REQUEST['nonce_add_page']),'add_page_nonce'))) {
300 mbt_add_booktable_page();
301 }
302 }
303 if(isset($mbt_remove_booktable_page)) {
304 if((isset($_REQUEST['nonce_remove_page']) && wp_verify_nonce(sanitize_key($_REQUEST['nonce_remove_page']),'remove_page_nonce'))) {
305 mbt_update_setting('booktable_page', 0);
306 }
307 }
308 }
309
310 function mbt_admin_install_notice() {
311 ?>
312 <div class="mbt-admin-notice">
313 <h4><?php echo('<strong>'.esc_attr_e('Welcome to MyBookTable', 'mybooktable').'</strong> &#8211; '.esc_attr_e("You're almost ready to start promoting your books", 'mybooktable').' :)'); ?></h4>
314 <a class="notice-button primary" href="<?php echo(esc_url(admin_url('admin.php?page=mbt_settings&install_mbt=1'))); ?>"><?php esc_attr_e('Install MyBookTable Pages', 'mybooktable'); ?></a>
315 <a class="notice-button secondary" href="<?php echo(esc_url(admin_url('admin.php?page=mbt_settings&skip_install_mbt=1'))); ?>"><?php esc_attr_e('Skip setup', 'mybooktable'); ?></a>
316 </div>
317 <?php
318 }
319
320 function mbt_admin_installed_notice() {
321 ?>
322 <div id="message" class="mbt-admin-notice">
323 <h4><?php echo('<strong>'.esc_attr_e('MyBookTable has been installed', 'mybooktable').'</strong> &#8211; '.esc_attr_e("You're ready to start promoting your books", 'mybooktable').' :)'); ?></h4>
324 <a class="notice-button primary" href="<?php echo(esc_url(admin_url('admin.php?page=mbt_help&mbt_finish_install=1'))); ?>"><?php esc_attr_e('Show Me How', 'mybooktable'); ?></a>
325 <a class="notice-button secondary" href="<?php echo(esc_url(admin_url('admin.php?page=mbt_settings&mbt_finish_install=1'))); ?>"><?php esc_attr_e('Thanks, I Got This', 'mybooktable'); ?></a>
326 </div>
327 <?php
328 }
329
330 function mbt_admin_setup_api_key_notice() {
331 ?>
332 <div id="message" class="mbt-admin-notice">
333 <h4><?php echo('<strong>'.esc_attr_e('Setup your License Key', 'mybooktable').'</strong> &#8211; '.esc_attr_e('MyBookTable needs your License key to enable enhanced features', 'mybooktable')); ?></h4>
334 <a class="notice-button primary" href="<?php echo(esc_url(admin_url('admin.php?page=mbt_settings'))); ?>" ><?php esc_attr_e('Go To Settings', 'mybooktable'); ?></a>
335 </div>
336 <?php
337 }
338
339 function mbt_admin_setup_default_affiliates_notice() {
340 ?>
341 <div id="message" class="mbt-admin-notice">
342 <h4><?php echo('<strong>'.esc_attr_e('Setup your Amazon and Barnes &amp; Noble Buttons', 'mybooktable').'</strong> &#8211; '.esc_attr_e('MyBookTable needs your input to enable these features', 'mybooktable')); ?></h4>
343 <a class="notice-button primary" href="<?php echo(esc_url(admin_url('admin.php?page=mbt_settings&mbt_setup_default_affiliates=1'))); ?>"><?php esc_attr_e('Go To Settings', 'mybooktable'); ?></a>
344 </div>
345 <?php
346 }
347
348 function mbt_admin_enable_upgrade_notice() {
349 $subpage = filter_input(INPUT_GET,'subpage');
350 if(isset($subpage) and $subpage == 'mbt_get_upgrade_page') { return; }
351 $upgradepage_url =
352 wp_nonce_url(esc_url(admin_url('admin.php?page=mbt_dashboard&subpage=mbt_get_upgrade_page'),'dashboard_nonce','nonce_dashboard'));
353 ?>
354 <div id="message" class="mbt-admin-notice">
355 <h4><?php echo('<strong>'.esc_attr_e('Enable your Upgrade', 'mybooktable').'</strong> &#8211; '.esc_attr_e('Download or Activate your MyBookTable Upgrade plugin to enable your advanced features!', 'mybooktable')); ?></h4>
356 <a class="notice-button primary" href="<?php echo(esc_attr($upgradepage_url)); ?>"><?php esc_attr_e('Enable', 'mybooktable'); ?></a>
357 </div>
358 <?php
359 }
360
361 /*---------------------------------------------------------*/
362 /* Installation Functions */
363 /*---------------------------------------------------------*/
364
365 function mbt_install() {
366 mbt_add_booktable_page();
367 mbt_install_examples();
368 }
369
370 function mbt_add_booktable_page() {
371 if(mbt_get_setting('booktable_page') <= 0 or !get_page(mbt_get_setting('booktable_page'))) {
372 $post_id = wp_insert_post(array(
373 'post_title' => esc_attr_e('Book Table', 'mybooktable'),
374 'post_content' => '',
375 'post_status' => 'publish',
376 'post_type' => 'page'
377 ));
378 mbt_update_setting("booktable_page", $post_id);
379 }
380 }
381
382 function mbt_install_examples() {
383 if(!mbt_get_setting('installed_examples')) {
384 include("examples.php");
385 mbt_update_setting('installed_examples', true);
386 }
387 }
388
389
390