1 <?php |
1 <?php |
2 |
|
3 /** |
2 /** |
4 * Load demo data |
3 * Load demo data |
5 * @version 1.0.4 |
4 * @version 1.0.7 |
6 */ |
5 */ |
7 |
6 |
8 if ( ! class_exists( 'Bws_Demo_Data' ) ) { |
7 if ( ! class_exists( 'Bws_Demo_Data' ) ) { |
9 class Bws_Demo_Data { |
8 class Bws_Demo_Data { |
10 private $bws_plugin_text_domain, $bws_plugin_prefix, $bws_plugin_page, $bws_plugin_name, $bws_plugin_basename, $bws_demo_options, $bws_demo_folder; |
9 private $bws_plugin_text_domain; |
|
10 private $bws_plugin_prefix; |
|
11 private $bws_plugin_page; |
|
12 private $bws_plugin_name; |
|
13 private $bws_plugin_basename; |
|
14 private $bws_demo_options; |
|
15 private $bws_plugin_options; |
|
16 private $bws_demo_folder; |
|
17 private $install_callback; |
|
18 private $remove_callback; |
11 |
19 |
12 public function __construct( $args ) { |
20 public function __construct( $args ) { |
13 $plugin_dir_array = explode( '/', $args['plugin_basename'] ); |
21 $plugin_dir_array = explode( '/', $args['plugin_basename'] ); |
14 $this->bws_plugin_basename = $args['plugin_basename']; |
22 $this->bws_plugin_basename = $args['plugin_basename']; |
15 $this->bws_plugin_prefix = $args['plugin_prefix']; |
23 $this->bws_plugin_prefix = $args['plugin_prefix']; |
16 $this->bws_plugin_name = $args['plugin_name']; |
24 $this->bws_plugin_name = $args['plugin_name']; |
17 $this->bws_plugin_page = $args['plugin_page']; |
25 $this->bws_plugin_page = $args['plugin_page']; |
18 $this->bws_demo_folder = $args['demo_folder']; |
26 $this->bws_demo_folder = $args['demo_folder']; |
|
27 $this->install_callback = isset( $args['install_callback'] ) ? $args['install_callback'] : false; |
|
28 $this->remove_callback = isset( $args['remove_callback'] ) ? $args['remove_callback'] : false; |
19 $this->bws_plugin_text_domain = $plugin_dir_array[0]; |
29 $this->bws_plugin_text_domain = $plugin_dir_array[0]; |
20 $this->bws_demo_options = get_option( $this->bws_plugin_prefix . 'demo_options' ); |
30 $this->bws_demo_options = get_option( $this->bws_plugin_prefix . 'demo_options' ); |
|
31 $this->bws_plugin_options = get_option( $this->bws_plugin_prefix . 'options' ); |
21 } |
32 } |
22 |
33 |
23 /** |
34 /** |
24 * Display "Install demo data" or "Uninstal demo data" buttons |
35 * Display "Install demo data" or "Uninstal demo data" buttons |
25 * @return void |
36 * @return void |
55 <p><?php echo $label; ?></p> |
66 <p><?php echo $label; ?></p> |
56 <form method="post" action=""> |
67 <form method="post" action=""> |
57 <p> |
68 <p> |
58 <button class="button button-primary" name="bws_<?php echo $_POST['bws_handle_demo']; ?>_demo_confirm" value="true"><?php echo $button_title; ?></button> |
69 <button class="button button-primary" name="bws_<?php echo $_POST['bws_handle_demo']; ?>_demo_confirm" value="true"><?php echo $button_title; ?></button> |
59 <button class="button" name="bws_<?php echo $_POST['bws_handle_demo']; ?>_demo_deny" value="true"><?php _e( 'No, go back to the settings page', $this->bws_plugin_text_domain ) ?></button> |
70 <button class="button" name="bws_<?php echo $_POST['bws_handle_demo']; ?>_demo_deny" value="true"><?php _e( 'No, go back to the settings page', $this->bws_plugin_text_domain ) ?></button> |
60 <?php wp_nonce_field( $this->bws_plugin_basename, 'bws_settings_nonce_name' ); ?> |
71 <?php wp_nonce_field( $this->bws_plugin_basename, 'bws_nonce_demo_name' ); ?> |
61 </p> |
72 </p> |
62 </form> |
73 </form> |
63 </div> |
74 </div> |
64 <?php } |
75 <?php } |
65 |
76 |
66 /** |
77 /** |
67 * @param bool $install_callback |
|
68 * @param bool $remove_callback |
|
69 * |
|
70 * @return array |
78 * @return array |
71 */ |
79 */ |
72 function bws_handle_demo_data( $install_callback = false, $remove_callback = false ) { |
80 function bws_handle_demo_data() { |
73 if ( isset( $_POST['bws_install_demo_confirm'] ) && check_admin_referer( $this->bws_plugin_basename, 'bws_settings_nonce_name' ) ) { |
81 if ( isset( $_POST['bws_install_demo_confirm'] ) ) |
74 return $this->bws_install_demo_data( $install_callback ); |
82 return $this->bws_install_demo_data(); |
75 } elseif ( isset( $_POST['bws_remove_demo_confirm'] ) && check_admin_referer( $this->bws_plugin_basename, 'bws_settings_nonce_name' ) ) { |
83 elseif ( isset( $_POST['bws_remove_demo_confirm'] ) ) |
76 return $this->bws_remove_demo_data( $remove_callback ); |
84 return $this->bws_remove_demo_data(); |
77 } else { |
85 else |
78 return false; |
86 return false; |
79 } |
|
80 } |
87 } |
81 |
88 |
82 /** |
89 /** |
83 * Load demo data |
90 * Load demo data |
84 * |
91 * |
85 * @param bool|string $callback |
|
86 * |
|
87 * @return array $message message about the result of the query |
92 * @return array $message message about the result of the query |
88 */ |
93 */ |
89 function bws_install_demo_data( $callback = false ) { |
94 function bws_install_demo_data() { |
90 global $wpdb; |
95 global $wpdb; |
91 /* get demo data*/ |
96 /* get demo data*/ |
92 $message = array( |
97 $message = array( |
93 'error' => NULL, |
98 'error' => NULL, |
94 'done' => NULL, |
99 'done' => NULL, |
125 |
130 |
126 /* |
131 /* |
127 * load demo options |
132 * load demo options |
128 */ |
133 */ |
129 if ( ! empty( $demo_data['options'] ) ) { |
134 if ( ! empty( $demo_data['options'] ) ) { |
130 $plugin_options = get_option( $this->bws_plugin_prefix . 'options' ); |
|
131 /* remember old plugin options */ |
135 /* remember old plugin options */ |
132 if ( ! empty( $plugin_options ) ) { |
136 if ( ! empty( $this->bws_plugin_options ) ) { |
133 $this->bws_demo_options['options'] = $plugin_options; |
137 $this->bws_demo_options['options'] = $this->bws_plugin_options; |
134 $demo_data['options']['display_demo_notice'] = 0; |
138 $demo_data['options']['display_demo_notice'] = 0; |
135 update_option( $this->bws_plugin_prefix . 'options', array_merge( $plugin_options, $demo_data['options'] ) ); |
139 update_option( $this->bws_plugin_prefix . 'options', array_merge( $this->bws_plugin_options, $demo_data['options'] ) ); |
136 } |
140 } |
137 } else { |
141 } else { |
138 /* remove demo notice */ |
142 /* remove demo notice */ |
139 $plugin_options = get_option( $this->bws_plugin_prefix . 'options' ); |
143 if ( 0 != $this->bws_plugin_options['display_demo_notice'] ) { |
140 if ( 0 != $plugin_options['display_demo_notice'] ) { |
144 $this->bws_plugin_options['display_demo_notice'] = 0; |
141 $plugin_options['display_demo_notice'] = 0; |
145 update_option( $this->bws_plugin_prefix . 'options', $this->bws_plugin_options ); |
142 update_option( $this->bws_plugin_prefix . 'options', $plugin_options ); |
|
143 } |
146 } |
144 } |
147 } |
145 |
148 |
146 /* |
149 /* |
147 * load demo posts |
150 * load demo posts |
151 $attachments_folder = $this->bws_demo_folder . 'images'; |
154 $attachments_folder = $this->bws_demo_folder . 'images'; |
152 /* |
155 /* |
153 * load demo terms |
156 * load demo terms |
154 */ |
157 */ |
155 if ( ! empty( $demo_data['terms'] ) ) { |
158 if ( ! empty( $demo_data['terms'] ) ) { |
156 foreach ( $demo_data['terms'] as $taxonomy_name => $terms_values_array ) { |
159 foreach ( $demo_data['terms'] as $taxonomy_slug => $terms_values_array ) { |
157 foreach ( $terms_values_array as $term_key => $term_value_single ) { |
160 foreach ( $terms_values_array as $term_slug => $term_name ) { |
158 $term_exists = term_exists( $term_key, $taxonomy_name ); |
161 $term_exists = term_exists( $term_slug, $taxonomy_slug ); |
159 if ( ! $term_exists ) { |
162 if ( ! $term_exists ) { |
160 $term_id = wp_insert_term( |
163 $term_id = wp_insert_term( |
161 $term_value_single, /* the term. */ |
164 $term_name, /* the term. */ |
162 $taxonomy_name, /* the taxonomy. */ |
165 $taxonomy_slug, /* the taxonomy. */ |
163 array( |
166 array( |
164 'slug' => $term_key |
167 'slug' => $term_slug |
165 ) |
168 ) |
166 ); |
169 ); |
167 if ( is_wp_error( $term_id ) ) { |
170 if ( is_wp_error( $term_id ) ) { |
168 $error ++; |
171 $error ++; |
169 } else { |
172 } else { |
170 $term_IDs[ $taxonomy_name ][ $term_key ] = $term_id['term_id']; |
173 $term_IDs[ $taxonomy_slug ][ $term_slug ] = $term_id['term_id']; |
171 $term_IDs_new[ $taxonomy_name ][ $term_key ] = $term_id['term_id']; |
174 $term_IDs_new[ $taxonomy_slug ][ $term_slug ] = $term_id['term_id']; |
172 } |
175 } |
173 } else { |
176 } else { |
174 $term_IDs[ $taxonomy_name ][ $term_key ] = $term_exists['term_id']; |
177 $term_IDs[ $taxonomy_slug ][ $term_slug ] = $term_exists['term_id']; |
175 } |
178 } |
176 } |
179 } |
177 } |
180 } |
178 if ( ! empty( $term_IDs_new ) ) { |
181 if ( ! empty( $term_IDs_new ) ) { |
179 $this->bws_demo_options['terms'] = isset( $this->bws_demo_options['terms'] ) ? array_merge( $this->bws_demo_options['terms'], $term_IDs_new ) : $term_IDs_new; |
182 $this->bws_demo_options['terms'] = isset( $this->bws_demo_options['terms'] ) ? array_merge( $this->bws_demo_options['terms'], $term_IDs_new ) : $term_IDs_new; |
186 foreach ( $demo_data['posts'] as $post ) { |
189 foreach ( $demo_data['posts'] as $post ) { |
187 if ( preg_match( '/{last_post_id}/', $post['post_content'] ) && ! empty( $post_id ) ) { |
190 if ( preg_match( '/{last_post_id}/', $post['post_content'] ) && ! empty( $post_id ) ) { |
188 $post['post_content'] = preg_replace( '/{last_post_id}/', $post_id, $post['post_content'] ); |
191 $post['post_content'] = preg_replace( '/{last_post_id}/', $post_id, $post['post_content'] ); |
189 } |
192 } |
190 if ( preg_match( '/{template_page}/', $post['post_content'] ) ) { |
193 if ( preg_match( '/{template_page}/', $post['post_content'] ) ) { |
191 if ( empty( $page_id ) && ! empty( $page_template ) ) { |
194 if ( empty( $page_id ) && ! empty( $page_template ) ) |
192 $page_id = intval( $wpdb->get_var( "SELECT `post_id` FROM $wpdb->postmeta WHERE `meta_key` LIKE '_wp_page_template' AND `meta_value` LIKE '" . $page_template . "' LIMIT 1" ) ); |
195 $page_id = intval( $wpdb->get_var( "SELECT `post_id` FROM $wpdb->postmeta WHERE `meta_key` LIKE '_wp_page_template' AND `meta_value` LIKE '" . $page_template . "' LIMIT 1" ) ); |
193 } |
196 if ( ! empty( $page_id ) ) |
194 if ( ! empty( $page_id ) ) { |
|
195 $post['post_content'] = preg_replace( '/{template_page}/', '<a href="' . get_permalink( $page_id ) . '">' . get_the_title( $page_id ) . '</a>', $post['post_content'] ); |
197 $post['post_content'] = preg_replace( '/{template_page}/', '<a href="' . get_permalink( $page_id ) . '">' . get_the_title( $page_id ) . '</a>', $post['post_content'] ); |
196 } |
|
197 } |
198 } |
198 /* insert current post */ |
199 /* insert current post */ |
199 $post_id = wp_insert_post( $post ); |
200 $post_id = wp_insert_post( $post ); |
200 if ( 'post' == $post['post_type'] ) { |
201 if ( 'post' == $post['post_type'] ) |
201 $posttype_post_id = $post_id; |
202 $posttype_post_id = $post_id; |
202 } |
|
203 |
203 |
204 /* add taxonomy for posttype */ |
204 /* add taxonomy for posttype */ |
205 if ( 'post' != $post['post_type'] && 'page' != $post['post_type'] && ! empty( $term_IDs ) ) { |
205 if ( 'post' != $post['post_type'] && 'page' != $post['post_type'] && ! empty( $term_IDs ) ) { |
206 foreach ( $term_IDs as $taxonomy_name => $term_array ) { |
206 foreach ( $term_IDs as $taxonomy_slug => $term_array ) { |
207 if ( isset( $post['terms'][ $taxonomy_name ] ) ) { |
207 if ( isset( $post['terms'][ $taxonomy_slug ] ) ) { |
208 $selected_terms = $post['terms'][ $taxonomy_name ]; |
208 $selected_terms = $post['terms'][ $taxonomy_slug ]; |
209 } else { |
209 } else { |
210 $selected_terms = array(); |
210 $selected_terms = array(); |
211 $selected_terms[] = intval( $term_array[ array_rand( $term_array ) ] ); |
211 $selected_terms[] = intval( $term_array[ array_rand( $term_array ) ] ); |
212 } |
212 } |
213 |
213 |
214 if ( ! wp_set_object_terms( $post_id, $selected_terms, $taxonomy_name, false ) ) |
214 if ( ! wp_set_object_terms( $post_id, $selected_terms, $taxonomy_slug, false ) ) |
215 $error ++; |
215 $error ++; |
216 } |
216 } |
217 } |
217 } |
218 |
218 |
219 $attach_id = 0; |
219 $attach_id = 0; |
342 if ( ! empty( $post['page_template'] ) ) { |
341 if ( ! empty( $post['page_template'] ) ) { |
343 update_post_meta( $post_id, '_wp_page_template', $post['page_template'] ); |
342 update_post_meta( $post_id, '_wp_page_template', $post['page_template'] ); |
344 $page_id = $post_id; |
343 $page_id = $post_id; |
345 $page_template = $post['page_template']; |
344 $page_template = $post['page_template']; |
346 } |
345 } |
|
346 /* save page_id to options */ |
|
347 if ( ! empty( $post['save_to_options'] ) ) { |
|
348 $page_id = $post_id; |
|
349 $this->bws_plugin_options[ $post['save_to_options'] ] = $post_id; |
|
350 update_option( $this->bws_plugin_prefix . 'options', $this->bws_plugin_options ); |
|
351 } |
|
352 |
347 /* first inserted image is thummbnail for post */ |
353 /* first inserted image is thummbnail for post */ |
348 if ( ! empty( $featured_attach_id ) ) |
354 if ( ! empty( $featured_attach_id ) ) |
349 update_post_meta( $post_id, '_thumbnail_id', $featured_attach_id ); |
355 update_post_meta( $post_id, '_thumbnail_id', $featured_attach_id ); |
350 } |
356 } |
351 } |
357 } |
364 $message['done'] .= '<br />' . __( 'View page with examples', $this->bws_plugin_text_domain ) . ': <a href="'. get_permalink( $page_id ) . '" target="_blank">' . get_the_title( $page_id ) . '</a>'; |
370 $message['done'] .= '<br />' . __( 'View page with examples', $this->bws_plugin_text_domain ) . ': <a href="'. get_permalink( $page_id ) . '" target="_blank">' . get_the_title( $page_id ) . '</a>'; |
365 } |
371 } |
366 |
372 |
367 if ( ! empty( $demo_data['options'] ) ) |
373 if ( ! empty( $demo_data['options'] ) ) |
368 $message['options'] = $demo_data['options']; |
374 $message['options'] = $demo_data['options']; |
369 |
375 else |
370 if ( $callback && function_exists( $callback ) ) |
376 $message['options'] = $this->bws_plugin_options; |
371 call_user_func( $callback ); |
377 |
|
378 if ( $this->install_callback && function_exists( $this->install_callback ) ) |
|
379 call_user_func( $this->install_callback ); |
372 } else { |
380 } else { |
373 $message['error'] = __( 'Demo data installation proceeded with some errors.', $this->bws_plugin_text_domain ); |
381 $message['error'] = __( 'Demo data installation proceeded with some errors.', $this->bws_plugin_text_domain ); |
374 } |
382 } |
375 } else { |
383 } else { |
376 $message['error'] = __( 'Post data is missing.', $this->bws_plugin_text_domain ); |
384 $message['error'] = __( 'Post data is missing.', $this->bws_plugin_text_domain ); |
454 * Restore plugin options |
460 * Restore plugin options |
455 */ |
461 */ |
456 if ( ! empty( $this->bws_demo_options['options'] ) ) { |
462 if ( ! empty( $this->bws_demo_options['options'] ) ) { |
457 $this->bws_demo_options['options']['display_demo_notice'] = 0; |
463 $this->bws_demo_options['options']['display_demo_notice'] = 0; |
458 update_option( $this->bws_plugin_prefix . 'options', $this->bws_demo_options['options'] ); |
464 update_option( $this->bws_plugin_prefix . 'options', $this->bws_demo_options['options'] ); |
459 if ( $callback && function_exists( $callback ) ) |
465 if ( $this->remove_callback && function_exists( $this->remove_callback ) ) |
460 call_user_func( $callback ); |
466 call_user_func( $this->remove_callback ); |
461 } |
467 } |
462 $done = $this->bws_delete_demo_option(); |
468 $done = $this->bws_delete_demo_option(); |
463 if ( ! $done ) { |
469 if ( ! $done ) |
464 $error ++; |
470 $error ++; |
465 } |
|
466 |
471 |
467 /* |
472 /* |
468 * Delete all posts |
473 * Delete all posts |
469 */ |
474 */ |
470 if ( ! empty( $this->bws_demo_options['posts'] ) ) { |
475 if ( ! empty( $this->bws_demo_options['posts'] ) ) { |
471 foreach ( $this->bws_demo_options['posts'] as $post_id => $last_modified ) { |
476 foreach ( $this->bws_demo_options['posts'] as $post_id => $last_modified ) { |
472 /* delete only not modified posts */ |
477 /* delete only not modified posts */ |
473 if ( get_post_modified_time( 'U', false, $post_id, false ) == $last_modified ) { |
478 if ( get_post_modified_time( 'U', false, $post_id, false ) == $last_modified ) { |
474 $done = wp_delete_post( $post_id, true ); |
479 $done = wp_delete_post( $post_id, true ); |
475 if ( ! $done ) { |
480 if ( ! $done ) |
476 $error ++; |
481 $error ++; |
477 } |
|
478 } |
482 } |
479 } |
483 } |
480 } |
484 } |
481 |
485 |
482 /* Delete terms */ |
486 /* Delete terms */ |
492 * Delete all attachments |
496 * Delete all attachments |
493 */ |
497 */ |
494 if ( ! empty( $this->bws_demo_options['attachments'] ) ) { |
498 if ( ! empty( $this->bws_demo_options['attachments'] ) ) { |
495 foreach ( $this->bws_demo_options['attachments'] as $post_id ) { |
499 foreach ( $this->bws_demo_options['attachments'] as $post_id ) { |
496 $done = wp_delete_attachment( $post_id, true ); |
500 $done = wp_delete_attachment( $post_id, true ); |
497 if ( ! $done ) { |
501 if ( ! $done ) |
498 $error ++; |
502 $error ++; |
499 } |
|
500 } |
503 } |
501 } |
504 } |
502 if ( ! empty( $this->bws_demo_options['distant_attachments'] ) ) { |
505 if ( ! empty( $this->bws_demo_options['distant_attachments'] ) ) { |
503 foreach ( $this->bws_demo_options['distant_attachments'] as $post_id ) { |
506 foreach ( $this->bws_demo_options['distant_attachments'] as $post_id ) { |
504 $done = wp_delete_attachment( $post_id, true ); |
507 $done = wp_delete_attachment( $post_id, true ); |
505 if ( ! $done ) { |
508 if ( ! $done ) |
506 $error ++; |
509 $error ++; |
507 } |
|
508 } |
510 } |
509 } |
511 } |
510 if ( empty( $error ) ) { |
512 if ( empty( $error ) ) { |
511 $message['done'] = __( 'Demo data successfully removed.', $this->bws_plugin_text_domain ); |
513 $message['done'] = __( 'Demo data successfully removed.', $this->bws_plugin_text_domain ); |
512 $message['options'] = get_option( $this->bws_plugin_prefix . 'options' ); |
514 $message['options'] = get_option( $this->bws_plugin_prefix . 'options' ); |
531 |
533 |
532 if ( 1 == $show_demo_notice && empty( $this->bws_demo_options ) ) { |
534 if ( 1 == $show_demo_notice && empty( $this->bws_demo_options ) ) { |
533 global $wp_version; |
535 global $wp_version; |
534 |
536 |
535 if ( isset( $_POST['bws_hide_demo_notice'] ) && check_admin_referer( $this->bws_plugin_basename, 'bws_demo_nonce_name' ) ) { |
537 if ( isset( $_POST['bws_hide_demo_notice'] ) && check_admin_referer( $this->bws_plugin_basename, 'bws_demo_nonce_name' ) ) { |
536 $plugin_options = get_option( $this->bws_plugin_prefix . 'options' ); |
538 $this->bws_plugin_options['display_demo_notice'] = 0; |
537 $plugin_options['display_demo_notice'] = 0; |
539 update_option( $this->bws_plugin_prefix . 'options', $this->bws_plugin_options ); |
538 update_option( $this->bws_plugin_prefix . 'options', $plugin_options ); |
|
539 return; |
540 return; |
540 } |
541 } |
541 if ( ! isset( $_POST['bws_handle_demo'] ) && ! isset( $_POST['bws_install_demo_confirm'] ) ) { |
542 if ( ! isset( $_POST['bws_handle_demo'] ) && ! isset( $_POST['bws_install_demo_confirm'] ) ) { |
542 if ( 4.2 > $wp_version ) { |
543 if ( 4.2 > $wp_version ) { |
543 $plugin_dir_array = explode( '/', plugin_basename( __FILE__ ) ); ?> |
544 $plugin_dir_array = explode( '/', plugin_basename( __FILE__ ) ); ?> |