|
1 <?php |
|
2 |
|
3 /** |
|
4 * Load demo data |
|
5 * @version 1.0.3 |
|
6 */ |
|
7 |
|
8 if ( ! class_exists( 'Bws_Demo_Data' ) ) { |
|
9 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; |
|
11 |
|
12 public function __construct( $args ) { |
|
13 $plugin_dir_array = explode( '/', $args['plugin_basename'] ); |
|
14 $this->bws_plugin_basename = $args['plugin_basename']; |
|
15 $this->bws_plugin_prefix = $args['plugin_prefix']; |
|
16 $this->bws_plugin_name = $args['plugin_name']; |
|
17 $this->bws_plugin_page = $args['plugin_page']; |
|
18 $this->bws_demo_folder = $args['demo_folder']; |
|
19 $this->bws_plugin_text_domain = $plugin_dir_array[0]; |
|
20 $this->bws_demo_options = get_option( $this->bws_plugin_prefix . 'demo_options' ); |
|
21 } |
|
22 |
|
23 /** |
|
24 * Display "Install demo data" or "Uninstal demo data" buttons |
|
25 * @return void |
|
26 */ |
|
27 function bws_show_demo_button( $form_info ) { |
|
28 if ( ! ( is_multisite() && is_network_admin() ) ) { |
|
29 if ( empty( $this->bws_demo_options ) ) { |
|
30 $value = 'install'; |
|
31 $button_title = __( 'Install Demo Data', $this->bws_plugin_text_domain ); |
|
32 } else { |
|
33 $value = 'remove'; |
|
34 $button_title = __( 'Remove Demo Data', $this->bws_plugin_text_domain ); |
|
35 $form_info = __( 'Delete demo data and restore previous plugin settings.', $this->bws_plugin_text_domain ); |
|
36 } ?> |
|
37 <button class="button" name="bws_handle_demo" value="<?php echo $value; ?>"><?php echo $button_title; ?></button> |
|
38 <div class="bws_info"><?php echo $form_info; ?></div> |
|
39 <?php } |
|
40 } |
|
41 |
|
42 /** |
|
43 * Display page for confirmation action to install demo data |
|
44 * @return void |
|
45 */ |
|
46 function bws_demo_confirm() { |
|
47 if ( 'install' == $_POST['bws_handle_demo'] ) { |
|
48 $button_title = __( 'Yes, install demo data', $this->bws_plugin_text_domain ); |
|
49 $label = __( 'Are you sure you want to install demo data?', $this->bws_plugin_text_domain ); |
|
50 } else { |
|
51 $button_title = __( 'Yes, remove demo data', $this->bws_plugin_text_domain ); |
|
52 $label = __( 'Are you sure you want to remove demo data?', $this->bws_plugin_text_domain ); |
|
53 } ?> |
|
54 <div> |
|
55 <p><?php echo $label; ?></p> |
|
56 <form method="post" action=""> |
|
57 <p> |
|
58 <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> |
|
60 <?php wp_nonce_field( $this->bws_plugin_basename, 'bws_settings_nonce_name' ); ?> |
|
61 </p> |
|
62 </form> |
|
63 </div> |
|
64 <?php } |
|
65 |
|
66 /** |
|
67 * @param bool $install_callback |
|
68 * @param bool $remove_callback |
|
69 * |
|
70 * @return array |
|
71 */ |
|
72 function bws_handle_demo_data( $install_callback = false, $remove_callback = false ) { |
|
73 if ( isset( $_POST['bws_install_demo_confirm'] ) && check_admin_referer( $this->bws_plugin_basename, 'bws_settings_nonce_name' ) ) { |
|
74 return $this->bws_install_demo_data( $install_callback ); |
|
75 } elseif ( isset( $_POST['bws_remove_demo_confirm'] ) && check_admin_referer( $this->bws_plugin_basename, 'bws_settings_nonce_name' ) ) { |
|
76 return $this->bws_remove_demo_data( $remove_callback ); |
|
77 } else { |
|
78 return false; |
|
79 } |
|
80 } |
|
81 |
|
82 /** |
|
83 * Load demo data |
|
84 * |
|
85 * @param bool|string $callback |
|
86 * |
|
87 * @return array $message message about the result of the query |
|
88 */ |
|
89 function bws_install_demo_data( $callback = false ) { |
|
90 global $wpdb; |
|
91 /* get demo data*/ |
|
92 $message = array( |
|
93 'error' => NULL, |
|
94 'done' => NULL, |
|
95 'options' => NULL |
|
96 ); |
|
97 $demo_data = array( |
|
98 'posts' => NULL, |
|
99 'attachments' => NULL, |
|
100 'distant_attachments' => NULL, |
|
101 'distant_attachments_metadata' => NULL, |
|
102 'terms' => NULL, |
|
103 'options' => NULL |
|
104 ); |
|
105 $error = 0; |
|
106 $page_id = $posttype_post_id = $post_id = ''; |
|
107 /* get demo data */ |
|
108 @include_once( $this->bws_demo_folder . 'demo-data.php' ); |
|
109 $received_demo_data = bws_demo_data_array(); |
|
110 |
|
111 /* |
|
112 * load demo data |
|
113 */ |
|
114 if ( empty( $received_demo_data ) ) { |
|
115 $message['error'] = __( 'Can not get demo data.', $this->bws_plugin_text_domain ); |
|
116 } else { |
|
117 $demo_data = array_merge( $demo_data, $received_demo_data ); |
|
118 /* |
|
119 * check if demo options already loaded |
|
120 */ |
|
121 if ( ! empty( $this->bws_demo_options ) ) { |
|
122 $message['error'] = __( 'Demo settings are already installed.', $this->bws_plugin_text_domain ); |
|
123 return $message; |
|
124 } |
|
125 |
|
126 /* |
|
127 * load demo options |
|
128 */ |
|
129 if ( ! empty( $demo_data['options'] ) ) { |
|
130 $plugin_options = get_option( $this->bws_plugin_prefix . 'options' ); |
|
131 /* remember old plugin options */ |
|
132 if ( ! empty( $plugin_options ) ) { |
|
133 $this->bws_demo_options['options'] = $plugin_options; |
|
134 $demo_data['options']['display_demo_notice'] = 0; |
|
135 update_option( $this->bws_plugin_prefix . 'options', array_merge( $plugin_options, $demo_data['options'] ) ); |
|
136 } |
|
137 } else { |
|
138 /* remove demo notice */ |
|
139 $plugin_options = get_option( $this->bws_plugin_prefix . 'options' ); |
|
140 if ( 0 != $plugin_options['display_demo_notice'] ) { |
|
141 $plugin_options['display_demo_notice'] = 0; |
|
142 update_option( $this->bws_plugin_prefix . 'options', $plugin_options ); |
|
143 } |
|
144 } |
|
145 |
|
146 /* |
|
147 * load demo posts |
|
148 */ |
|
149 if ( ! empty( $demo_data['posts'] ) ) { |
|
150 $wp_upload_dir = wp_upload_dir(); |
|
151 $attachments_folder = $this->bws_demo_folder . 'images'; |
|
152 /* |
|
153 * load demo terms |
|
154 */ |
|
155 if ( ! empty( $demo_data['terms'] ) ) { |
|
156 foreach ( $demo_data['terms'] as $taxonomy_name => $terms_values_array ) { |
|
157 foreach ( $terms_values_array as $term_key => $term_value_single ) { |
|
158 $term_exists = term_exists( $term_key, $taxonomy_name ); |
|
159 if ( ! $term_exists ) { |
|
160 $term_id = wp_insert_term( |
|
161 $term_value_single, /* the term. */ |
|
162 $taxonomy_name, /* the taxonomy. */ |
|
163 array( |
|
164 'slug' => $term_key |
|
165 ) |
|
166 ); |
|
167 if ( is_wp_error( $term_id ) ) { |
|
168 $error ++; |
|
169 } else { |
|
170 $term_IDs[ $taxonomy_name ][ $term_key ] = $term_id['term_id']; |
|
171 $term_IDs_new[ $taxonomy_name ][ $term_key ] = $term_id['term_id']; |
|
172 } |
|
173 } else { |
|
174 $term_IDs[ $taxonomy_name ][ $term_key ] = $term_exists['term_id']; |
|
175 } |
|
176 } |
|
177 } |
|
178 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; |
|
180 } |
|
181 } |
|
182 |
|
183 /* |
|
184 * load demo posts |
|
185 */ |
|
186 foreach ( $demo_data['posts'] as $post ) { |
|
187 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'] ); |
|
189 } |
|
190 if ( preg_match( '/{template_page}/', $post['post_content'] ) ) { |
|
191 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" ) ); |
|
193 } |
|
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'] ); |
|
196 } |
|
197 } |
|
198 /* insert current post */ |
|
199 $post_id = wp_insert_post( $post ); |
|
200 if ( 'post' == $post['post_type'] ) { |
|
201 $posttype_post_id = $post_id; |
|
202 } |
|
203 |
|
204 /* add taxonomy for posttype */ |
|
205 if ( 'post' != $post['post_type'] && 'page' != $post['post_type'] && ! empty( $term_IDs ) ) { |
|
206 foreach ( $term_IDs as $taxonomy_name => $term_array ) { |
|
207 if ( isset( $post['terms'][ $taxonomy_name ] ) ) { |
|
208 $selected_terms = $post['terms'][ $taxonomy_name ]; |
|
209 } else { |
|
210 $selected_terms = array(); |
|
211 $selected_terms[] = intval( $term_array[ array_rand( $term_array ) ] ); |
|
212 } |
|
213 |
|
214 if ( ! wp_set_object_terms( $post_id, $selected_terms, $taxonomy_name, false ) ) |
|
215 $error ++; |
|
216 } |
|
217 } |
|
218 |
|
219 $attach_id = 0; |
|
220 |
|
221 if ( is_wp_error( $post_id ) || 0 == $post_id ) { |
|
222 $error ++; |
|
223 } else { |
|
224 /* remember post ID */ |
|
225 $this->bws_demo_options['posts'][ $post_id ] = get_post_modified_time( 'U', false, $post_id, false ); |
|
226 |
|
227 $featured_attach_id = ''; |
|
228 /* |
|
229 * load post attachments |
|
230 */ |
|
231 if ( ! empty( $post['attachments_folder'] ) ) { |
|
232 $attachments_list = @scandir( $attachments_folder . '/' . $post['attachments_folder'] ); |
|
233 if ( 2 < count( $attachments_list ) ) { |
|
234 foreach ( $attachments_list as $attachment ) { |
|
235 $file = $attachments_folder . '/' . $post['attachments_folder'] . '/' . $attachment; |
|
236 /* insert current attachment */ |
|
237 /* Check if file is image */ |
|
238 $file_data = @getimagesize( $file ); |
|
239 $bws_is_image = ! ( $file_data || in_array( $file_data[2], array( 1, 2, 3 ) ) ) ? false : true; |
|
240 if ( $bws_is_image ) { |
|
241 |
|
242 $destination = $wp_upload_dir['path'] . '/' . $this->bws_plugin_prefix . 'demo_' . $attachment; /* path to new file */ |
|
243 $wp_filetype = wp_check_filetype( $file, null ); /* Mime-type */ |
|
244 |
|
245 if ( copy( $file, $destination ) ) { /* if attachment copied */ |
|
246 |
|
247 $attachment_data = array( |
|
248 'post_mime_type' => $wp_filetype['type'], |
|
249 'post_title' => $attachment, |
|
250 'post_content' => '', |
|
251 'post_status' => 'inherit' |
|
252 ); |
|
253 |
|
254 /* insert attschment in to database */ |
|
255 $attach_id = wp_insert_attachment( $attachment_data, $destination, $post_id ); |
|
256 if ( 0 != $attach_id ) { |
|
257 if ( empty( $featured_attach_id ) ) |
|
258 $featured_attach_id = $attach_id; |
|
259 /* remember attachment ID */ |
|
260 $this->bws_demo_options['attachments'][] = $attach_id; |
|
261 |
|
262 /* insert attachment metadata */ |
|
263 $attach_data = wp_generate_attachment_metadata( $attach_id, $destination ); |
|
264 wp_update_attachment_metadata( $attach_id, $attach_data ); |
|
265 /* insert additional metadata */ |
|
266 if ( isset( $demo_data['attachments'][ $attachment ] ) ) { |
|
267 foreach ( $demo_data['attachments'][ $attachment ] as $meta_key => $meta_value ) { |
|
268 if ( '{get_lorem_ipsum}' == $meta_value ) |
|
269 $meta_value = $this->bws_get_lorem_ipsum(); |
|
270 add_post_meta( $attach_id, $meta_key, $meta_value ); |
|
271 } |
|
272 } |
|
273 } else { |
|
274 $error ++; |
|
275 } |
|
276 } else { |
|
277 $error ++; |
|
278 } |
|
279 } |
|
280 } |
|
281 } |
|
282 } |
|
283 |
|
284 /* |
|
285 * load post attachments |
|
286 */ |
|
287 if ( ! empty( $post['distant_attachments'] ) ) { |
|
288 foreach ( $post['distant_attachments'] as $attachment_name ) { |
|
289 if ( isset( $demo_data['distant_attachments_metadata'][ $attachment_name ] ) ) { |
|
290 $data = $demo_data['distant_attachments_metadata'][ $attachment_name ]; |
|
291 |
|
292 $attachment_data = array( |
|
293 'post_mime_type' => $data['mime_type'], |
|
294 'post_title' => $data['title'], |
|
295 'post_content' => '', |
|
296 'post_status' => 'inherit' |
|
297 ); |
|
298 |
|
299 /* insert attschment in to database */ |
|
300 $attach_id = wp_insert_attachment( $attachment_data, $data['url'], $post_id ); |
|
301 if ( 0 != $attach_id ) { |
|
302 if ( empty( $featured_attach_id ) ) { |
|
303 $featured_attach_id = $attach_id; |
|
304 } |
|
305 /* remember attachment ID */ |
|
306 $this->bws_demo_options['distant_attachments'][ $attachment_name ] = $attach_id; |
|
307 |
|
308 /* insert attachment metadata */ |
|
309 $imagesize = @getimagesize( $data['url'] ); |
|
310 $sizes = ( isset( $data['sizes'] ) ) ? $data['sizes'] : array(); |
|
311 $attach_data = array( |
|
312 'width' => $imagesize[0], |
|
313 'height' => $imagesize[1], |
|
314 'file' => $data['url'], |
|
315 'sizes' => $sizes |
|
316 ); |
|
317 |
|
318 wp_update_attachment_metadata( $attach_id, $attach_data ); |
|
319 |
|
320 /* insert additional metadata */ |
|
321 if ( isset( $demo_data['distant_attachments'][ $attachment_name ] ) ) { |
|
322 foreach ( $demo_data['distant_attachments'][ $attachment_name ] as $meta_key => $meta_value ) { |
|
323 if ( '{get_lorem_ipsum}' == $meta_value ) { |
|
324 $meta_value = $this->bws_get_lorem_ipsum(); |
|
325 } |
|
326 add_post_meta( $attach_id, $meta_key, $meta_value ); |
|
327 } |
|
328 } |
|
329 } else { |
|
330 $error ++; |
|
331 } |
|
332 } else { |
|
333 $error ++; |
|
334 } |
|
335 } |
|
336 } |
|
337 |
|
338 /* insert additional post meta */ |
|
339 if ( isset( $post['post_meta'] ) && ! empty( $post['post_meta'] ) ) { |
|
340 foreach ( $post['post_meta'] as $meta_key => $meta_value ) { |
|
341 add_post_meta( $post_id, $meta_key, $meta_value ); |
|
342 } |
|
343 } |
|
344 /* set template for post type "page" */ |
|
345 if ( ! empty( $post['page_template'] ) ) { |
|
346 update_post_meta( $post_id, '_wp_page_template', $post['page_template'] ); |
|
347 $page_id = $post_id; |
|
348 $page_template = $post['page_template']; |
|
349 } |
|
350 /* first inserted image is thummbnail for post */ |
|
351 if ( ! empty( $featured_attach_id ) ) |
|
352 update_post_meta( $post_id, '_thumbnail_id', $featured_attach_id ); |
|
353 } |
|
354 } |
|
355 |
|
356 /* |
|
357 * Save demo options |
|
358 */ |
|
359 add_option( $this->bws_plugin_prefix . 'demo_options', $this->bws_demo_options ); |
|
360 |
|
361 if ( 0 == $error ) { |
|
362 $message['done'] = __( 'Demo data successfully installed.', $this->bws_plugin_text_domain ); |
|
363 if ( ! empty( $posttype_post_id ) ) { |
|
364 $message['done'] .= '<br />' . __( 'View post with shortcodes', $this->bws_plugin_text_domain ) . ': <a href="'. get_permalink( $posttype_post_id ) . '" target="_blank">' . get_the_title( $posttype_post_id ) . '</a>'; |
|
365 } |
|
366 if ( ! empty( $page_id ) ) { |
|
367 $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>'; |
|
368 } |
|
369 |
|
370 if ( ! empty( $demo_data['options'] ) ) |
|
371 $message['options'] = $demo_data['options']; |
|
372 |
|
373 if ( $callback && function_exists( $callback ) ) |
|
374 call_user_func( $callback ); |
|
375 } else { |
|
376 $message['error'] = __( 'Demo data installation proceeded with some errors.', $this->bws_plugin_text_domain ); |
|
377 } |
|
378 } else { |
|
379 $message['error'] = __( 'Post data is missing.', $this->bws_plugin_text_domain ); |
|
380 } |
|
381 } |
|
382 return $message; |
|
383 } |
|
384 |
|
385 /** |
|
386 * Change url for distant attachments |
|
387 * @return $url string |
|
388 */ |
|
389 function bws_wp_get_attachment_url( $url, $id ) { |
|
390 if ( ! empty( $this->bws_demo_options['distant_attachments'] ) && in_array( $id, $this->bws_demo_options['distant_attachments'] ) ) { |
|
391 $url = substr( $url, strpos( $url, 'https://' ) ); |
|
392 } |
|
393 return $url; |
|
394 } |
|
395 |
|
396 /** |
|
397 * Replace metadata to default for images after saving ( to prevent editing image ) |
|
398 * @return $data array() |
|
399 */ |
|
400 function bws_wp_update_attachment_metadata( $data, $id ) { |
|
401 if ( ! empty( $data ) && ! empty( $this->bws_demo_options['distant_attachments'] ) && $attachment_name = array_search( $id, $this->bws_demo_options['distant_attachments'] ) ) { |
|
402 /* get demo data */ |
|
403 @include_once( $this->bws_demo_folder . 'demo-data.php' ); |
|
404 $received_demo_data = bws_demo_data_array(); |
|
405 |
|
406 if ( isset( $received_demo_data['distant_attachments_metadata'][ $attachment_name ] ) ) { |
|
407 |
|
408 /* insert attachment metadata */ |
|
409 $imagesize = @getimagesize( $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['url'] ); |
|
410 $sizes = ( isset( $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['sizes'] ) ) ? $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['sizes'] : array(); |
|
411 $data = array( |
|
412 'width' => $imagesize[0], |
|
413 'height' => $imagesize[1], |
|
414 'file' => $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['url'], |
|
415 'sizes' => $sizes |
|
416 ); |
|
417 } |
|
418 } |
|
419 return $data; |
|
420 } |
|
421 |
|
422 /** |
|
423 * Change url for distant attachments |
|
424 * @return $url string |
|
425 */ |
|
426 function bws_wp_get_attachment_image_attributes( $attr, $attachment, $size = false ) { |
|
427 if ( ! empty( $attr['srcset'] ) && ! empty( $this->bws_demo_options['distant_attachments'] ) && in_array( $attachment->ID, $this->bws_demo_options['distant_attachments'] ) ) { |
|
428 $srcset = explode( ', ', $attr['srcset'] ); |
|
429 foreach ( $srcset as $key => $value ) { |
|
430 $srcset[ $key ] = substr( $value, strpos( $value, 'https://' ) ); |
|
431 } |
|
432 $attr['srcset'] = implode( ', ', $srcset ); |
|
433 } |
|
434 return $attr; |
|
435 } |
|
436 |
|
437 /** |
|
438 * Remove demo data |
|
439 * |
|
440 * @param $callback |
|
441 * |
|
442 * @return array $message message about the result of the query |
|
443 */ |
|
444 function bws_remove_demo_data( $callback = false ) { |
|
445 $error = 0; |
|
446 $message = array( |
|
447 'error' => null, |
|
448 'done' => null, |
|
449 'options' => null |
|
450 ); |
|
451 |
|
452 if ( empty( $this->bws_demo_options ) ) { |
|
453 $message['error'] = __( 'Demo data have been already removed.', $this->bws_plugin_text_domain ); |
|
454 } else { |
|
455 |
|
456 /* |
|
457 * Restore plugin options |
|
458 */ |
|
459 if ( ! empty( $this->bws_demo_options['options'] ) ) { |
|
460 $this->bws_demo_options['options']['display_demo_notice'] = 0; |
|
461 update_option( $this->bws_plugin_prefix . 'options', $this->bws_demo_options['options'] ); |
|
462 if ( $callback && function_exists( $callback ) ) |
|
463 call_user_func( $callback ); |
|
464 } |
|
465 $done = $this->bws_delete_demo_option(); |
|
466 if ( ! $done ) { |
|
467 $error ++; |
|
468 } |
|
469 |
|
470 /* |
|
471 * Delete all posts |
|
472 */ |
|
473 if ( ! empty( $this->bws_demo_options['posts'] ) ) { |
|
474 foreach ( $this->bws_demo_options['posts'] as $post_id => $last_modified ) { |
|
475 /* delete only not modified posts */ |
|
476 if ( get_post_modified_time( 'U', false, $post_id, false ) == $last_modified ) { |
|
477 $done = wp_delete_post( $post_id, true ); |
|
478 if ( ! $done ) { |
|
479 $error ++; |
|
480 } |
|
481 } |
|
482 } |
|
483 } |
|
484 |
|
485 /* Delete terms */ |
|
486 if ( ! empty( $this->bws_demo_options['terms'] ) ) { |
|
487 foreach ( $this->bws_demo_options['terms'] as $taxonomy_name => $terms_values_array ) { |
|
488 foreach ( $terms_values_array as $term_id ) { |
|
489 wp_delete_term( $term_id, $taxonomy_name ); |
|
490 } |
|
491 } |
|
492 } |
|
493 |
|
494 /* |
|
495 * Delete all attachments |
|
496 */ |
|
497 if ( ! empty( $this->bws_demo_options['attachments'] ) ) { |
|
498 foreach ( $this->bws_demo_options['attachments'] as $post_id ) { |
|
499 $done = wp_delete_attachment( $post_id, true ); |
|
500 if ( ! $done ) { |
|
501 $error ++; |
|
502 } |
|
503 } |
|
504 } |
|
505 if ( ! empty( $this->bws_demo_options['distant_attachments'] ) ) { |
|
506 foreach ( $this->bws_demo_options['distant_attachments'] as $post_id ) { |
|
507 $done = wp_delete_attachment( $post_id, true ); |
|
508 if ( ! $done ) { |
|
509 $error ++; |
|
510 } |
|
511 } |
|
512 } |
|
513 if ( empty( $error ) ) { |
|
514 $message['done'] = __( 'Demo data successfully removed.', $this->bws_plugin_text_domain ); |
|
515 $message['options'] = get_option( $this->bws_plugin_prefix . 'options' ); |
|
516 $this->bws_demo_options = array(); |
|
517 } else { |
|
518 $message['error'] = __( 'Removing demo data with some errors occurred.', $this->bws_plugin_text_domain ); |
|
519 } |
|
520 } |
|
521 return $message; |
|
522 } |
|
523 |
|
524 /** |
|
525 * Delete demo-options |
|
526 * @return boolean |
|
527 */ |
|
528 function bws_delete_demo_option() { |
|
529 $done = delete_option( $this->bws_plugin_prefix . 'demo_options' ); |
|
530 return $done; |
|
531 } |
|
532 |
|
533 function bws_handle_demo_notice( $show_demo_notice ) { |
|
534 |
|
535 if ( 1 == $show_demo_notice ) { |
|
536 global $wp_version; |
|
537 |
|
538 if ( isset( $_POST['bws_hide_demo_notice'] ) && check_admin_referer( $this->bws_plugin_basename, 'bws_demo_nonce_name' ) ) { |
|
539 $plugin_options = get_option( $this->bws_plugin_prefix . 'options' ); |
|
540 $plugin_options['display_demo_notice'] = 0; |
|
541 update_option( $this->bws_plugin_prefix . 'options', $plugin_options ); |
|
542 return; |
|
543 } |
|
544 if ( ! isset( $_POST['bws_handle_demo'] ) && ! isset( $_POST['bws_install_demo_confirm'] ) ) { |
|
545 if ( 4.2 > $wp_version ) { |
|
546 $plugin_dir_array = explode( '/', plugin_basename( __FILE__ ) ); ?> |
|
547 <style type="text/css"> |
|
548 #bws_handle_notice_form { |
|
549 float: right; |
|
550 width: 20px; |
|
551 height: 20px; |
|
552 margin-bottom: 0; |
|
553 } |
|
554 .bws_hide_demo_notice { |
|
555 width: 100%; |
|
556 height: 100%; |
|
557 border: none; |
|
558 background: url("<?php echo plugins_url( $plugin_dir_array[0] . '/bws_menu/images/close_banner.png' ); ?>") no-repeat center center; |
|
559 box-shadow: none; |
|
560 <?php if ( 3.8 <= $wp_version ) { ?> |
|
561 position: relative; |
|
562 top: -4px; |
|
563 <?php } ?> |
|
564 } |
|
565 .bws_hide_demo_notice:hover { |
|
566 cursor: pointer; |
|
567 } |
|
568 .rtl #bws_handle_notice_form { |
|
569 float: left; |
|
570 } |
|
571 </style> |
|
572 <?php } |
|
573 if ( 4.2 <= $wp_version ) { ?> |
|
574 <style type="text/css"> |
|
575 #bws_handle_notice_form { |
|
576 position: absolute; |
|
577 top: 2px; |
|
578 right: 0; |
|
579 } |
|
580 .rtl #bws_handle_notice_form { |
|
581 left: 0; |
|
582 } |
|
583 </style> |
|
584 <?php } ?> |
|
585 <div class="update-nag" style="position: relative;"> |
|
586 <form id="bws_handle_notice_form" action="" method="post"> |
|
587 <button class="notice-dismiss bws_hide_demo_notice" title="<?php _e( 'Close notice', $this->bws_plugin_text_domain ); ?>"></button> |
|
588 <input type="hidden" name="bws_hide_demo_notice" value="hide" /> |
|
589 <?php wp_nonce_field( $this->bws_plugin_basename, 'bws_demo_nonce_name' ); ?> |
|
590 </form> |
|
591 <div style="margin: 0 20px;"><?php printf( __( 'Do you want to install demo content and settings for %s (You can do this later using Import / Export settings)?', $this->bws_plugin_text_domain ), '<a href="edit.php?post_type=portfolio&page=portfolio.php">Portfolio by BestWebSoft</a>' ); ?> <a href="<?php echo admin_url( 'admin.php?page=' . $this->bws_plugin_page ); ?>"><?php _e( 'Yes, install demo now', $this->bws_plugin_text_domain ); ?></a></div> |
|
592 </div> |
|
593 <?php } |
|
594 } |
|
595 } |
|
596 |
|
597 /** |
|
598 * Generate Lorem Ipsum |
|
599 * @return string |
|
600 */ |
|
601 function bws_get_lorem_ipsum() { |
|
602 $lorem_ipsum = array( |
|
603 "Fusce quis varius quam, non molestie dui. ", |
|
604 "Ut eu feugiat eros. Aliquam justo mauris, volutpat eu lacinia et, bibendum non velit. ", |
|
605 "Aenean in justo et nunc facilisis varius feugiat quis elit. ", |
|
606 "Proin luctus non quam in bibendum. ", |
|
607 "Sed finibus, risus eu blandit ullamcorper, sapien urna vulputate ante, quis semper magna nibh vel orci. ", |
|
608 "Nullam eu aliquam erat. ", |
|
609 "Suspendisse massa est, feugiat nec dolor non, varius finibus massa. ", |
|
610 "Sed velit justo, semper ut ante eu, feugiat ultricies velit. ", |
|
611 "Ut sed velit ut nisl laoreet malesuada vitae non elit. ", |
|
612 "Integer eu sem justo. Nunc sit amet erat tristique, mollis neque et, iaculis purus. ", |
|
613 "Vestibulum sit amet varius sapien. Quisque maximus tempor scelerisque. ", |
|
614 "Ut eleifend, felis vel rhoncus cursus, purus ipsum consectetur ex, nec elementum mauris ipsum eget quam. ", |
|
615 "Integer sem diam, iaculis in arcu vel, pulvinar scelerisque magna. ", |
|
616 "Cras rhoncus neque aliquet, molestie justo id, finibus erat. ", |
|
617 "Proin eleifend, eros et interdum faucibus, ligula dui accumsan sem, ac tristique dolor erat vel est. ", |
|
618 "Etiam ut nulla risus. Aliquam non consequat turpis, id hendrerit magna. Suspendisse potenti. ", |
|
619 "Donec fringilla libero ac sapien porta ultricies. ", |
|
620 "Donec sapien lacus, blandit vitae fermentum vitae, accumsan ut magna. ", |
|
621 "Curabitur maximus lorem lectus, eu porta ipsum fringilla eu. ", |
|
622 "Integer vitae justo ultricies, aliquam neque in, venenatis nunc. ", |
|
623 "Pellentesque non nulla venenatis, posuere erat id, faucibus leo. ", |
|
624 "Nullam fringilla sodales arcu, nec rhoncus lorem fringilla in. ", |
|
625 "Quisque consequat lorem vel nisl pharetra iaculis. Donec aliquet interdum tristique. Sed ullamcorper urna odio. ", |
|
626 "Nam dictum dictum neque id congue. ", |
|
627 "Donec quis quam id turpis condimentum condimentum. ", |
|
628 "Morbi tincidunt, nunc nec pellentesque scelerisque, tortor eros efficitur lectus, eget molestie lacus est eu est. ", |
|
629 "Morbi non augue a tellus interdum condimentum id ac enim. ", |
|
630 "In dictum velit ultricies, dictum est ac, tempus arcu. ", |
|
631 "Duis maximus, mi nec pulvinar suscipit, arcu purus vestibulum urna, ", |
|
632 "consectetur rutrum mi sapien et massa. Donec faucibus ex vel nibh consequat, ut molestie lacus elementum. ", |
|
633 "Interdum et malesuada fames ac ante ipsum primis in faucibus. ", |
|
634 "Phasellus quam dolor, convallis vel nulla sed, pretium tristique felis. ", |
|
635 "Morbi condimentum nunc vel augue tincidunt, in porttitor metus interdum. Sed nec venenatis elit. ", |
|
636 "Donec non urna dui. Maecenas sit amet venenatis eros, sed aliquam metus. ", |
|
637 "Nulla venenatis eros ac velit pellentesque, nec semper orci faucibus. ", |
|
638 "Etiam sit amet dapibus lacus, non semper erat. ", |
|
639 "Donec dolor metus, iaculis nec lacinia a, tristique sed libero. ", |
|
640 "Phasellus a quam gravida, tincidunt metus ac, eleifend odio. ", |
|
641 "Integer facilisis mauris ut velit gravida ornare. Quisque viverra sagittis lacus, non dapibus turpis iaculis sit amet. ", |
|
642 "Vestibulum vehicula pulvinar blandit. ", |
|
643 "Praesent sit amet consectetur augue, vitae tincidunt nulla. ", |
|
644 "Curabitur metus nibh, molestie vel massa in, egestas dapibus felis. ", |
|
645 "Phasellus id erat massa. Aliquam bibendum purus ac ante imperdiet, mattis gravida dui mollis. ", |
|
646 "Fusce id purus et mauris condimentum fermentum. ", |
|
647 "Fusce tempus et purus ut fringilla. Suspendisse ornare et ligula in gravida. ", |
|
648 "Nunc id nunc mauris. Curabitur auctor sodales felis, nec dapibus urna pellentesque et. ", |
|
649 "Phasellus quam dolor, convallis vel nulla sed, pretium tristique felis. ", |
|
650 "Morbi condimentum nunc vel augue tincidunt, in porttitor metus interdum. ", |
|
651 "Sed scelerisque eget mauris et sagittis. ", |
|
652 "In eget enim nec arcu malesuada malesuada. ", |
|
653 "Nulla eu odio vel nibh elementum vestibulum vel vel magna. " |
|
654 ); |
|
655 return $lorem_ipsum[ rand( 0, 50 ) ]; |
|
656 } |
|
657 } |
|
658 } |