wp/wp-content/plugins/portfolio/includes/demo-data/class-bws-demo-data.php
changeset 21 48c4eec2b7e6
child 23 417f20492bf7
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Load demo data
       
     5  * @version 1.0.4
       
     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 						} elseif ( ! empty( $post_id ) ) {
       
   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 										$tmp = download_url( $data['url'] );
       
   293 
       
   294 										$file_array = array(
       
   295 											'name'     => $data['title'],
       
   296 											'tmp_name' => $tmp
       
   297 										);
       
   298 
       
   299 										/**
       
   300 										 * Check for download errors
       
   301 										 * if there are error unlink the temp file name
       
   302 										 */
       
   303 										if ( is_wp_error( $tmp ) ) {
       
   304 												@unlink( $file_array[ 'tmp_name' ] );
       
   305 												continue;
       
   306 										}
       
   307 
       
   308 										$attach_id = media_handle_sideload( $file_array, $post_id );
       
   309 
       
   310 										if ( 0 != $attach_id ) {
       
   311 											if ( empty( $featured_attach_id ) ) {
       
   312 												$featured_attach_id = $attach_id;
       
   313 											}
       
   314 											/* remember attachment ID */
       
   315 											$this->bws_demo_options['distant_attachments'][ $attachment_name ] = $attach_id;
       
   316 
       
   317 											/* insert additional metadata */
       
   318 											if ( isset( $demo_data['distant_attachments'][ $attachment_name ] ) ) {
       
   319 												foreach ( $demo_data['distant_attachments'][ $attachment_name ] as $meta_key => $meta_value ) {
       
   320 													if ( '{get_lorem_ipsum}' == $meta_value ) {
       
   321 														$meta_value = $this->bws_get_lorem_ipsum();
       
   322 													}
       
   323 													add_post_meta( $attach_id, $meta_key, $meta_value );
       
   324 												}
       
   325 											}
       
   326 										} else {
       
   327 											$error ++;
       
   328 										}
       
   329 									} else {
       
   330 										$error ++;
       
   331 									}
       
   332 								}
       
   333 							}
       
   334 
       
   335 							/* insert additional post meta */
       
   336 							if ( isset( $post['post_meta'] ) && ! empty( $post['post_meta'] ) ) {
       
   337 								foreach ( $post['post_meta'] as $meta_key => $meta_value ) {
       
   338 									add_post_meta( $post_id, $meta_key, $meta_value );
       
   339 								}
       
   340 							}
       
   341 							/* set template for post type "page" */
       
   342 							if ( ! empty( $post['page_template'] ) ) {
       
   343 								update_post_meta( $post_id, '_wp_page_template', $post['page_template'] );
       
   344 								$page_id = $post_id;
       
   345 								$page_template = $post['page_template'];
       
   346 							}
       
   347 							/* first inserted image is thummbnail for post */
       
   348 							if ( ! empty( $featured_attach_id ) )
       
   349 								update_post_meta( $post_id, '_thumbnail_id', $featured_attach_id );
       
   350 						}
       
   351 					}
       
   352 
       
   353 					/*
       
   354 					 * Save demo options
       
   355 					 */
       
   356 					add_option( $this->bws_plugin_prefix . 'demo_options', $this->bws_demo_options );
       
   357 
       
   358 					if ( 0 == $error ) {
       
   359 						$message['done'] = __( 'Demo data successfully installed.', $this->bws_plugin_text_domain );
       
   360 						if ( ! empty( $posttype_post_id ) ) {
       
   361 							$message['done'] .= '<br />' . __( 'View post with shortcodes', $this->bws_plugin_text_domain ) . ':&nbsp;<a href="'.  get_permalink( $posttype_post_id ) . '" target="_blank">' . get_the_title( $posttype_post_id ) . '</a>';
       
   362 						}
       
   363 						if ( ! empty( $page_id ) ) {
       
   364 							$message['done'] .= '<br />' . __( 'View page with examples', $this->bws_plugin_text_domain ) . ':&nbsp;<a href="'.  get_permalink( $page_id ) . '" target="_blank">' . get_the_title( $page_id ) . '</a>';
       
   365 						}
       
   366 
       
   367 						if ( ! empty( $demo_data['options'] ) )
       
   368 							$message['options'] = $demo_data['options'];
       
   369 
       
   370 						if ( $callback && function_exists( $callback ) )
       
   371 							call_user_func( $callback );
       
   372 					} else {
       
   373 						$message['error'] = __( 'Demo data installation proceeded with some errors.', $this->bws_plugin_text_domain );
       
   374 					}
       
   375 				} else {
       
   376 					$message['error'] = __( 'Post data is missing.', $this->bws_plugin_text_domain );
       
   377 				}
       
   378 			}
       
   379 			return $message;
       
   380 		}
       
   381 
       
   382 		/**
       
   383 		 * Change url for distant attachments
       
   384 		 * @return $url   string
       
   385 		 */
       
   386 		function bws_wp_get_attachment_url( $url, $id ) {
       
   387 			if ( ! empty( $this->bws_demo_options['distant_attachments'] ) && in_array( $id, $this->bws_demo_options['distant_attachments'] ) ) {
       
   388 				$url = substr( $url, strpos( $url, 'https://' ) );
       
   389 			}
       
   390 			return $url;
       
   391 		}
       
   392 
       
   393 		/**
       
   394 		 * Replace metadata to default for images after saving ( to prevent editing image )
       
   395 		 * @return $data   array()
       
   396 		 */
       
   397 		function bws_wp_update_attachment_metadata( $data, $id ) {
       
   398 			if ( ! empty( $data ) && ! empty( $this->bws_demo_options['distant_attachments'] ) && $attachment_name = array_search( $id, $this->bws_demo_options['distant_attachments'] ) ) {
       
   399 				/* get demo data */
       
   400 				@include_once( $this->bws_demo_folder . 'demo-data.php' );
       
   401 				$received_demo_data = bws_demo_data_array();
       
   402 
       
   403 				if ( isset( $received_demo_data['distant_attachments_metadata'][ $attachment_name ] ) ) {
       
   404 
       
   405 					/* insert attachment metadata */
       
   406 					$imagesize = @getimagesize( $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['url'] );
       
   407 					$sizes = ( isset( $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['sizes'] ) ) ? $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['sizes'] : array();
       
   408 					$data = array(
       
   409 						'width' 	=> $imagesize[0],
       
   410 						'height' 	=> $imagesize[1],
       
   411 						'file' 		=> $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['url'],
       
   412 						'sizes' 	=> $sizes
       
   413 					);
       
   414 				}
       
   415 			}
       
   416 			return $data;
       
   417 		}
       
   418 
       
   419 		/**
       
   420 		 * Change url for distant attachments
       
   421 		 * @return $url   string
       
   422 		 */
       
   423 		function bws_wp_get_attachment_image_attributes( $attr, $attachment, $size = false ) {
       
   424 			if ( ! empty( $attr['srcset'] ) && ! empty( $this->bws_demo_options['distant_attachments'] ) && in_array( $attachment->ID, $this->bws_demo_options['distant_attachments'] ) ) {
       
   425 				$srcset = explode( ', ', $attr['srcset'] );
       
   426 				foreach ( $srcset as $key => $value ) {
       
   427 					$srcset[ $key ] = substr( $value, strpos( $value, 'https://' ) );
       
   428 				}
       
   429 				$attr['srcset'] = implode( ', ', $srcset );
       
   430 			}
       
   431 			return $attr;
       
   432 		}
       
   433 
       
   434 		/**
       
   435 		 * Remove demo data
       
   436 		 *
       
   437 		 * @param $callback
       
   438 		 *
       
   439 		 * @return array $message   message about the result of the query
       
   440 		 */
       
   441 		function bws_remove_demo_data( $callback = false ) {
       
   442 			$error        = 0;
       
   443 			$message      = array(
       
   444 				'error'   => null,
       
   445 				'done'    => null,
       
   446 				'options' => null
       
   447 			);
       
   448 
       
   449 			if ( empty( $this->bws_demo_options ) ) {
       
   450 				$message['error'] = __( 'Demo data have been already removed.', $this->bws_plugin_text_domain );
       
   451 			} else {
       
   452 
       
   453 				/*
       
   454 				 * Restore plugin options
       
   455 				 */
       
   456 				if ( ! empty( $this->bws_demo_options['options'] ) ) {
       
   457 					$this->bws_demo_options['options']['display_demo_notice'] = 0;
       
   458 					update_option( $this->bws_plugin_prefix . 'options', $this->bws_demo_options['options'] );
       
   459 					if ( $callback && function_exists( $callback ) )
       
   460 						call_user_func( $callback );
       
   461 				}
       
   462 				$done = $this->bws_delete_demo_option();
       
   463 				if ( ! $done ) {
       
   464 					$error ++;
       
   465 				}
       
   466 
       
   467 				/*
       
   468 				 * Delete all posts
       
   469 				 */
       
   470 				if ( ! empty( $this->bws_demo_options['posts'] ) ) {
       
   471 					foreach ( $this->bws_demo_options['posts'] as $post_id => $last_modified ) {
       
   472 						/* delete only not modified posts */
       
   473 						if ( get_post_modified_time( 'U', false, $post_id, false ) == $last_modified ) {
       
   474 							$done = wp_delete_post( $post_id, true );
       
   475 							if ( ! $done ) {
       
   476 								$error ++;
       
   477 							}
       
   478 						}
       
   479 					}
       
   480 				}
       
   481 
       
   482 				/* Delete terms */
       
   483 				if ( ! empty( $this->bws_demo_options['terms'] ) ) {
       
   484 					foreach ( $this->bws_demo_options['terms'] as $taxonomy_name => $terms_values_array ) {
       
   485 						foreach ( $terms_values_array as $term_id ) {
       
   486 							wp_delete_term( $term_id, $taxonomy_name );
       
   487 						}
       
   488 					}
       
   489 				}
       
   490 
       
   491 				/*
       
   492 				 * Delete all attachments
       
   493 				 */
       
   494 				if ( ! empty( $this->bws_demo_options['attachments'] ) ) {
       
   495 					foreach ( $this->bws_demo_options['attachments'] as $post_id ) {
       
   496 						$done = wp_delete_attachment( $post_id, true );
       
   497 						if ( ! $done ) {
       
   498 							$error ++;
       
   499 						}
       
   500 					}
       
   501 				}
       
   502 				if ( ! empty( $this->bws_demo_options['distant_attachments'] ) ) {
       
   503 					foreach ( $this->bws_demo_options['distant_attachments'] as $post_id ) {
       
   504 						$done = wp_delete_attachment( $post_id, true );
       
   505 						if ( ! $done ) {
       
   506 							$error ++;
       
   507 						}
       
   508 					}
       
   509 				}
       
   510 				if ( empty( $error ) ) {
       
   511 					$message['done']    = __( 'Demo data successfully removed.', $this->bws_plugin_text_domain );
       
   512 					$message['options'] = get_option( $this->bws_plugin_prefix . 'options' );
       
   513 					$this->bws_demo_options = array();
       
   514 				} else {
       
   515 					$message['error'] = __( 'Removing demo data with some errors occurred.', $this->bws_plugin_text_domain );
       
   516 				}
       
   517 			}
       
   518 			return $message;
       
   519 		}
       
   520 
       
   521 		/**
       
   522 		 * Delete demo-options
       
   523 		 * @return boolean
       
   524 		 */
       
   525 		function bws_delete_demo_option() {
       
   526 			$done = delete_option( $this->bws_plugin_prefix . 'demo_options' );
       
   527 			return $done;
       
   528 		}
       
   529 
       
   530 		function bws_handle_demo_notice( $show_demo_notice ) {
       
   531 
       
   532 			if ( 1 == $show_demo_notice && empty( $this->bws_demo_options ) ) {
       
   533 				global $wp_version;
       
   534 
       
   535 				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' );
       
   537 					$plugin_options['display_demo_notice'] = 0;
       
   538 					update_option( $this->bws_plugin_prefix . 'options', $plugin_options );
       
   539 					return;
       
   540 				}
       
   541 				if ( ! isset( $_POST['bws_handle_demo'] ) && ! isset( $_POST['bws_install_demo_confirm'] ) ) {
       
   542 					if ( 4.2 > $wp_version ) {
       
   543 						$plugin_dir_array = explode( '/', plugin_basename( __FILE__ ) ); ?>
       
   544 						<style type="text/css">
       
   545 							#bws_handle_notice_form {
       
   546 								float: right;
       
   547 								width: 20px;
       
   548 								height: 20px;
       
   549 								margin-bottom: 0;
       
   550 							}
       
   551 							.bws_hide_demo_notice {
       
   552 								width: 100%;
       
   553 								height: 100%;
       
   554 								border: none;
       
   555 								background: url("<?php echo plugins_url( $plugin_dir_array[0] . '/bws_menu/images/close_banner.png' ); ?>") no-repeat center center;
       
   556 								box-shadow: none;
       
   557 								<?php if ( 3.8 <= $wp_version ) { ?>
       
   558 									position: relative;
       
   559 									top: -4px;
       
   560 								<?php } ?>
       
   561 							}
       
   562 							.bws_hide_demo_notice:hover {
       
   563 								cursor: pointer;
       
   564 							}
       
   565 							.rtl #bws_handle_notice_form {
       
   566 								float: left;
       
   567 							}
       
   568 						</style>
       
   569 					<?php }
       
   570 					if ( 4.2 <= $wp_version ) { ?>
       
   571 						<style type="text/css">
       
   572 							#bws_handle_notice_form {
       
   573 								position: absolute;
       
   574 								top: 2px;
       
   575 								right: 0;
       
   576 							}
       
   577 							.rtl #bws_handle_notice_form {
       
   578 								left: 0;
       
   579 							}
       
   580 						</style>
       
   581 					<?php } ?>
       
   582                     <div class="notice" style="position: relative;">
       
   583                         <form id="bws_handle_notice_form" action="" method="post">
       
   584                             <button class="notice-dismiss bws_hide_demo_notice" title="<?php _e( 'Close notice', $this->bws_plugin_text_domain ); ?>"></button>
       
   585                             <input type="hidden" name="bws_hide_demo_notice" value="hide" />
       
   586 							<?php wp_nonce_field( $this->bws_plugin_basename, 'bws_demo_nonce_name' ); ?>
       
   587                         </form>
       
   588                         <p>
       
   589 							<?php printf(
       
   590 								__( 'Do you want to install demo content and settings for %s now?', $this->bws_plugin_text_domain ),
       
   591 								'<b>' . $this->bws_plugin_name . ' by BestWebSoft</b>'
       
   592 							); ?>&nbsp;<a href="<?php echo admin_url( 'admin.php?page=' . $this->bws_plugin_page ); ?>"><?php _e( 'Yes, install demo data', $this->bws_plugin_text_domain ); ?></a>
       
   593                         </p>
       
   594                     </div>
       
   595 				<?php }
       
   596 			}
       
   597 		}
       
   598 
       
   599 		/**
       
   600 		 * Generate Lorem Ipsum
       
   601 		 * @return   string
       
   602 		 */
       
   603 		function bws_get_lorem_ipsum() {
       
   604 			$lorem_ipsum = array(
       
   605 				"Fusce quis varius quam, non molestie dui. ",
       
   606 				"Ut eu feugiat eros. Aliquam justo mauris, volutpat eu lacinia et, bibendum non velit. ",
       
   607 				"Aenean in justo et nunc facilisis varius feugiat quis elit. ",
       
   608 				"Proin luctus non quam in bibendum. ",
       
   609 				"Sed finibus, risus eu blandit ullamcorper, sapien urna vulputate ante, quis semper magna nibh vel orci. ",
       
   610 				"Nullam eu aliquam erat. ",
       
   611 				"Suspendisse massa est, feugiat nec dolor non, varius finibus massa. ",
       
   612 				"Sed velit justo, semper ut ante eu, feugiat ultricies velit. ",
       
   613 				"Ut sed velit ut nisl laoreet malesuada vitae non elit. ",
       
   614 				"Integer eu sem justo. Nunc sit amet erat tristique, mollis neque et, iaculis purus. ",
       
   615 				"Vestibulum sit amet varius sapien. Quisque maximus tempor scelerisque. ",
       
   616 				"Ut eleifend, felis vel rhoncus cursus, purus ipsum consectetur ex, nec elementum mauris ipsum eget quam. ",
       
   617 				"Integer sem diam, iaculis in arcu vel, pulvinar scelerisque magna. ",
       
   618 				"Cras rhoncus neque aliquet, molestie justo id, finibus erat. ",
       
   619 				"Proin eleifend, eros et interdum faucibus, ligula dui accumsan sem, ac tristique dolor erat vel est. ",
       
   620 				"Etiam ut nulla risus. Aliquam non consequat turpis, id hendrerit magna. Suspendisse potenti. ",
       
   621 				"Donec fringilla libero ac sapien porta ultricies. ",
       
   622 				"Donec sapien lacus, blandit vitae fermentum vitae, accumsan ut magna. ",
       
   623 				"Curabitur maximus lorem lectus, eu porta ipsum fringilla eu. ",
       
   624 				"Integer vitae justo ultricies, aliquam neque in, venenatis nunc. ",
       
   625 				"Pellentesque non nulla venenatis, posuere erat id, faucibus leo. ",
       
   626 				"Nullam fringilla sodales arcu, nec rhoncus lorem fringilla in. ",
       
   627 				"Quisque consequat lorem vel nisl pharetra iaculis. Donec aliquet interdum tristique. Sed ullamcorper urna odio. ",
       
   628 				"Nam dictum dictum neque id congue. ",
       
   629 				"Donec quis quam id turpis condimentum condimentum. ",
       
   630 				"Morbi tincidunt, nunc nec pellentesque scelerisque, tortor eros efficitur lectus, eget molestie lacus est eu est. ",
       
   631 				"Morbi non augue a tellus interdum condimentum id ac enim. ",
       
   632 				"In dictum velit ultricies, dictum est ac, tempus arcu. ",
       
   633 				"Duis maximus, mi nec pulvinar suscipit, arcu purus vestibulum urna, ",
       
   634 				"consectetur rutrum mi sapien et massa. Donec faucibus ex vel nibh consequat, ut molestie lacus elementum. ",
       
   635 				"Interdum et malesuada fames ac ante ipsum primis in faucibus. ",
       
   636 				"Phasellus quam dolor, convallis vel nulla sed, pretium tristique felis. ",
       
   637 				"Morbi condimentum nunc vel augue tincidunt, in porttitor metus interdum. Sed nec venenatis elit. ",
       
   638 				"Donec non urna dui. Maecenas sit amet venenatis eros, sed aliquam metus. ",
       
   639 				"Nulla venenatis eros ac velit pellentesque, nec semper orci faucibus. ",
       
   640 				"Etiam sit amet dapibus lacus, non semper erat. ",
       
   641 				"Donec dolor metus, iaculis nec lacinia a, tristique sed libero. ",
       
   642 				"Phasellus a quam gravida, tincidunt metus ac, eleifend odio. ",
       
   643 				"Integer facilisis mauris ut velit gravida ornare. Quisque viverra sagittis lacus, non dapibus turpis iaculis sit amet. ",
       
   644 				"Vestibulum vehicula pulvinar blandit. ",
       
   645 				"Praesent sit amet consectetur augue, vitae tincidunt nulla. ",
       
   646 				"Curabitur metus nibh, molestie vel massa in, egestas dapibus felis. ",
       
   647 				"Phasellus id erat massa. Aliquam bibendum purus ac ante imperdiet, mattis gravida dui mollis. ",
       
   648 				"Fusce id purus et mauris condimentum fermentum. ",
       
   649 				"Fusce tempus et purus ut fringilla. Suspendisse ornare et ligula in gravida. ",
       
   650 				"Nunc id nunc mauris. Curabitur auctor sodales felis, nec dapibus urna pellentesque et. ",
       
   651 				"Phasellus quam dolor, convallis vel nulla sed, pretium tristique felis. ",
       
   652 				"Morbi condimentum nunc vel augue tincidunt, in porttitor metus interdum. ",
       
   653 				"Sed scelerisque eget mauris et sagittis. ",
       
   654 				"In eget enim nec arcu malesuada malesuada. ",
       
   655 				"Nulla eu odio vel nibh elementum vestibulum vel vel magna. "
       
   656 			);
       
   657 			return $lorem_ipsum[ rand( 0, 50 ) ];
       
   658 		}
       
   659 	}
       
   660 }