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

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

212 lines 11.4 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 /* Custom MyBookTable Importer/Exporter */
6 /*---------------------------------------------------------*/
7
8 function mbt_custom_importer_init() {
9 add_filter('mbt_importers', 'mbt_add_custom_importer');
10 add_action('admin_init', 'mbt_detect_custom_export_download');
11 add_filter('mbt_pre_import_book', 'mbt_custom_importer_filter_book', 10, 2);
12 }
13 add_action('mbt_init', 'mbt_custom_importer_init');
14
15 function mbt_add_custom_importer($importers) {
16 $importers['mybooktable'] = array(
17 'name' => __('MyBookTable Import/Export', 'mybooktable'),
18 'desc' => __('Import or Export your book data for backups or site transfer.', 'mybooktable'),
19 'get_book_list' => array(
20 'render_import_form' => 'mbt_render_custom_importer_form',
21 'parse_import_form' => 'mbt_parse_custom_importer_form',
22 ),
23 );
24 return $importers;
25 }
26
27 function mbt_render_custom_importer_form() {
28 ?>
29 <h3 style="margin-bottom: 0px;"><?php esc_attr_e('Export', 'mybooktable'); ?></h3>
30 <div style="margin: 4px 0;">Click the 'Export' button below and save the MyBookTable Backup file to an appropriate location.</div>
31 <div style="margin-bottom: 4px;"><input type="checkbox" id="mbt_export_settings"> <span style="font-weight:bold;">Include Settings data</span> (includes potentially sensitive info such as affiliate codes and integration credentials)</div>
32 <button class="button button-primary" onclick="window.open(window.location+'&export='+(jQuery('#mbt_export_settings').is(':checked') ? 'books,settings' : 'books')); return false;">Export</button>
33 <h3 style="margin-bottom: 0px;margin-top:30px;"><?php esc_attr_e('Import', 'mybooktable'); ?></h3>
34 <div style="margin: 4px 0;">Choose your MyBookTable Backup file with the file picker below then click "Import".</div>
35 <input name="mbt_custom_import_file" type="file">
36 <?php wp_nonce_field('mbt-import-nonce','data-import-nonce'); ?>
37 <?php
38 }
39
40 function mbt_parse_custom_importer_form() {
41 if( isset($_REQUEST['data-import-nonce']) && isset($_FILES['mbt_custom_import_file']['tmp_name']) && wp_verify_nonce(sanitize_key($_REQUEST['data-import-nonce']),'mbt-import-nonce')) {
42 require_once(ABSPATH.'wp-admin/includes/class-wp-filesystem-base.php');
43 require_once(ABSPATH.'wp-admin/includes/class-wp-filesystem-direct.php');
44 $file = sanitize_text_field(wp_unslash($_FILES['mbt_custom_import_file']['tmp_name']));
45 if(!isset($file) || empty($file)){return;}
46 //$file_text = file_get_contents($file);
47 $arg = ''; // no idea why this needs to exist -- Come-on WP - make sense
48 $filesystem = new WP_Filesystem_Direct($arg);
49 $file_text = $filesystem->get_contents($file);
50 $data = json_decode($file_text, true);
51
52 // load settings
53 if(!empty($data['settings'])) {
54 global $mbt_settings;
55 $mbt_settings = array_merge($mbt_settings, $data['settings']);
56 update_option('mbt_settings', $mbt_settings);
57 }
58
59 // load taxonomies
60 foreach(array('mbt_author' => 'authors', 'mbt_series' => 'series', 'mbt_genre' => 'genres', 'mbt_tag' => 'tags') as $tax_slug => $tax_name) {
61 if(empty($data[$tax_name])) { continue; }
62 $terms = $data[$tax_name];
63 foreach($terms as $key => $term) {
64 $term_obj = get_term_by('name', $term['name'], $tax_slug);
65 if($term_obj === false) {
66 $term_data = wp_insert_term($term['name'], $tax_slug, array(
67 'slug' => $term['slug'],
68 'description' => $term['description'],
69 ));
70 if(!empty($term['image'])) { mbt_save_taxonomy_image($tax_slug, $term_data['term_id'], $term['image']); }
71 if($tax_name == 'authors' and !empty($term['priority'])) { mbt_save_author_priority($term_data['term_id'], $term['priority']); }
72 $terms[$key]['imported_id'] = $term_data['term_id'];
73 }
74 }
75 foreach($terms as $term) {
76 if(empty($term['imported_id']) or empty($term['parent'])) { continue; }
77 $parent = get_term_by('name', $term['parent'], $tax_slug);
78 if($parent !== false) {
79 wp_update_term($term['imported_id'], $tax_slug, array('parent' => $parent->term_id));
80 }
81 }
82 }
83 return $data['books'];
84 }
85 }
86
87 function mbt_custom_importer_filter_book($book, $import_type) {
88 if($import_type !== 'mybooktable') { return $book; }
89
90 //if a book exists with the EXACT same data, don't import a copy
91 $book_query = new WP_Query(array('post_type' => 'mbt_book', 'name' => $book['name']));
92 if(!empty($book_query->posts)) {
93 $existing_book = $book_query->posts[0];
94 $book_matches = true;
95 $book_matches = ($book_matches and ($book['title'] === $existing_book->post_title));
96 $book_matches = ($book_matches and ($book['content'] === $existing_book->post_content));
97 $book_matches = ($book_matches and ($book['excerpt'] === $existing_book->post_excerpt));
98 $extract_names = function($term) { return $term->name; };
99 $book_matches = ($book_matches and ($book['authors'] === array_map($extract_names, wp_get_object_terms($existing_book->ID, 'mbt_author'))));
100 $book_matches = ($book_matches and ($book['series'] === array_map($extract_names, wp_get_object_terms($existing_book->ID, 'mbt_series'))));
101 $book_matches = ($book_matches and ($book['genres'] === array_map($extract_names, wp_get_object_terms($existing_book->ID, 'mbt_genre'))));
102 $book_matches = ($book_matches and ($book['tags'] === array_map($extract_names, wp_get_object_terms($existing_book->ID, 'mbt_tag'))));
103 $book_matches = ($book_matches and ($book['price'] === get_post_meta($existing_book->ID, 'mbt_price', true)));
104 $book_matches = ($book_matches and ($book['unique_id_isbn'] === get_post_meta($existing_book->ID, 'mbt_unique_id_isbn', true)));
105 $book_matches = ($book_matches and ($book['unique_id_asin'] === get_post_meta($existing_book->ID, 'mbt_unique_id_asin', true)));
106 $book_matches = ($book_matches and ($book['buybuttons'] === get_post_meta($existing_book->ID, 'mbt_buybuttons', true)));
107 $book_matches = ($book_matches and ($book['publisher_name'] === get_post_meta($existing_book->ID, 'mbt_publisher_name', true)));
108 $book_matches = ($book_matches and ($book['publisher_url'] === get_post_meta($existing_book->ID, 'mbt_publisher_url', true)));
109 $book_matches = ($book_matches and ($book['publication_year'] === get_post_meta($existing_book->ID, 'mbt_publication_year', true)));
110 $book_matches = ($book_matches and ($book['image_filename'] === wp_basename(get_attached_file(get_post_meta($existing_book->ID, 'mbt_book_image_id', true)))));
111 $book_matches = ($book_matches and ($book['sample_url'] === get_post_meta($existing_book->ID, 'mbt_sample_url', true)));
112 $book_matches = ($book_matches and ($book['book_length'] === get_post_meta($existing_book->ID, 'mbt_book_length', true)));
113 $book_matches = ($book_matches and ($book['sale_price'] === get_post_meta($existing_book->ID, 'mbt_sale_price', true)));
114 $book_matches = ($book_matches and ($book['show_instant_preview'] === get_post_meta($existing_book->ID, 'mbt_show_instant_preview', true)));
115 $book_matches = ($book_matches and ($book['series_order'] === get_post_meta($existing_book->ID, 'mbt_series_order', true)));
116 $book_matches = ($book_matches and ($book['display_mode'] === get_post_meta($existing_book->ID, 'mbt_display_mode', true)));
117 if($book_matches) { return sprintf(__('Book "%s" already exists', 'mybooktable'), $book['title']); }
118 }
119
120 //test if image id matches desired image
121 $image_file = get_attached_file($book['image_id']);
122 if(empty($image_file) or $book['image_filename'] !== wp_basename($image_file)) {
123 $book['image_id'] = null;
124 }
125
126 return $book;
127 }
128
129 function mbt_detect_custom_export_download() {
130 global $pagenow;
131 if(!is_admin() or empty($pagenow) or $pagenow !== 'admin.php') { return; }
132 $page = filter_input( INPUT_GET, 'page', FILTER_UNSAFE_RAW );
133 $import_type = filter_input( INPUT_GET, 'mbt_import_type', FILTER_UNSAFE_RAW );
134 $export = filter_input( INPUT_GET, 'export', FILTER_UNSAFE_RAW );
135 if(empty($page) or $page !== 'mbt_import') { return; }
136 if(empty($import_type) or $import_type !== 'mybooktable') { return; }
137 if(empty($export) or ($export !== 'books' and $export !== 'books,settings')) { return; }
138 if(!current_user_can('edit_posts')) { return; }
139
140 header('Content-type: application/json');
141 header('Content-Disposition: attachment; filename="mybooktable_export"');
142
143 $data = array('version' => MBT_VERSION);
144
145 // save settings
146 if($export === 'books,settings') {
147 global $mbt_settings;
148 $data['settings'] = $mbt_settings;
149 unset($data['settings']['version']);
150 unset($data['settings']['author_priorities']);
151 }
152
153 // save taxonomies
154 foreach(array('mbt_author' => 'authors', 'mbt_series' => 'series', 'mbt_genre' => 'genres', 'mbt_tag' => 'tags') as $tax_slug => $tax_name) {
155 $raw_terms = get_terms($tax_slug);
156 $terms = array();
157 foreach($raw_terms as $term) {
158 $parent = get_term_by('id', $term->parent, $tax_slug);
159 $new_term = array(
160 'id' => $term->term_id,
161 'name' => $term->name,
162 'slug' => $term->slug,
163 'description' => $term->description,
164 'parent' => empty($parent->name) ? '' : $parent->name,
165 'image' => mbt_get_taxonomy_image($tax_slug, $term->term_id),
166 );
167 if($tax_name == 'authors') { $new_term['priority'] = mbt_get_author_priority($term->term_id); }
168 $terms[] = $new_term;
169 }
170 $data[$tax_name] = $terms;
171 }
172
173 // save books
174 $books = array();
175 $books_query = new WP_Query(array('post_type' => 'mbt_book', 'posts_per_page' => -1));
176 foreach($books_query->posts as $book) {
177 $new_book = array();
178 $new_book['id'] = $book->ID;
179 $new_book['name'] = $book->post_name;
180 $new_book['title'] = $book->post_title;
181 $new_book['content'] = $book->post_content;
182 $new_book['excerpt'] = $book->post_excerpt;
183 $extract_names = function($term) { return $term->name; };
184 $new_book['authors'] = array_map($extract_names, wp_get_object_terms($book->ID, 'mbt_author'));
185 $new_book['series'] = array_map($extract_names, wp_get_object_terms($book->ID, 'mbt_series'));
186 $new_book['genres'] = array_map($extract_names, wp_get_object_terms($book->ID, 'mbt_genre'));
187 $new_book['tags'] = array_map($extract_names, wp_get_object_terms($book->ID, 'mbt_tag'));
188 $new_book['price'] = get_post_meta($book->ID, 'mbt_price', true);
189 $new_book['unique_id_isbn'] = get_post_meta($book->ID, 'mbt_unique_id_isbn', true);
190 $new_book['unique_id_asin'] = get_post_meta($book->ID, 'mbt_unique_id_asin', true);
191 $new_book['buybuttons'] = get_post_meta($book->ID, 'mbt_buybuttons', true);
192 $new_book['publisher_name'] = get_post_meta($book->ID, 'mbt_publisher_name', true);
193 $new_book['publisher_url'] = get_post_meta($book->ID, 'mbt_publisher_url', true);
194 $new_book['publication_year'] = get_post_meta($book->ID, 'mbt_publication_year', true);
195 $new_book['image_id'] = get_post_meta($book->ID, 'mbt_book_image_id', true);
196 $new_book['image_filename'] = wp_basename(get_attached_file($new_book['image_id']));
197 $book_image_info = wp_get_attachment_image_src($new_book['image_id'],'full',false);
198 $new_book['image_src'] = (is_array($book_image_info) && isset($book_image_info[0])) ? $book_image_info[0] : '';
199 $new_book['sample_url'] = get_post_meta($book->ID, 'mbt_sample_url', true);
200 $new_book['book_length'] = get_post_meta($book->ID, 'mbt_book_length', true);
201 $new_book['sale_price'] = get_post_meta($book->ID, 'mbt_sale_price', true);
202 $new_book['show_instant_preview'] = get_post_meta($book->ID, 'mbt_show_instant_preview', true);
203 $new_book['series_order'] = get_post_meta($book->ID, 'mbt_series_order', true);
204 $new_book['display_mode'] = get_post_meta($book->ID, 'mbt_display_mode', true);
205 $books[] = $new_book;
206 }
207 $data['books'] = $books;
208
209 echo(wp_json_encode($data));
210
211 die();
212 }