wp/wp-content/plugins/portfolio/includes/demo-data/class-bws-demo-data.php
changeset 23 417f20492bf7
parent 21 48c4eec2b7e6
--- a/wp/wp-content/plugins/portfolio/includes/demo-data/class-bws-demo-data.php	Fri Sep 05 18:52:52 2025 +0200
+++ b/wp/wp-content/plugins/portfolio/includes/demo-data/class-bws-demo-data.php	Mon Sep 08 19:44:41 2025 +0200
@@ -1,13 +1,21 @@
 <?php
-
 /**
  * Load demo data
- * @version 1.0.4
+ * @version 1.0.7
  */
 
 if ( ! class_exists( 'Bws_Demo_Data' ) ) {
 	class Bws_Demo_Data {
-		private $bws_plugin_text_domain, $bws_plugin_prefix, $bws_plugin_page, $bws_plugin_name, $bws_plugin_basename, $bws_demo_options, $bws_demo_folder;
+		private $bws_plugin_text_domain;
+		private $bws_plugin_prefix;
+		private $bws_plugin_page;
+		private $bws_plugin_name;
+		private $bws_plugin_basename;
+		private $bws_demo_options;
+		private $bws_plugin_options;
+		private $bws_demo_folder;
+		private $install_callback;
+		private $remove_callback;
 
 		public function __construct( $args ) {
 			$plugin_dir_array      	= explode( '/', $args['plugin_basename'] );
@@ -16,8 +24,11 @@
 			$this->bws_plugin_name			= $args['plugin_name'];
 			$this->bws_plugin_page			= $args['plugin_page'];
 			$this->bws_demo_folder			= $args['demo_folder'];
+			$this->install_callback 		= isset( $args['install_callback'] ) ? $args['install_callback'] : false;
+			$this->remove_callback 			= isset( $args['remove_callback'] ) ? $args['remove_callback'] : false;			
 			$this->bws_plugin_text_domain 	= $plugin_dir_array[0];
 			$this->bws_demo_options 		= get_option( $this->bws_plugin_prefix . 'demo_options' );
+			$this->bws_plugin_options		= get_option( $this->bws_plugin_prefix . 'options' );
 		}
 
 		/**
@@ -57,36 +68,30 @@
 					<p>
 						<button class="button button-primary" name="bws_<?php echo $_POST['bws_handle_demo']; ?>_demo_confirm" value="true"><?php echo $button_title; ?></button>
 						<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>
-						<?php wp_nonce_field( $this->bws_plugin_basename, 'bws_settings_nonce_name' ); ?>
+						<?php wp_nonce_field( $this->bws_plugin_basename, 'bws_nonce_demo_name' ); ?>
 					</p>
 				</form>
 			</div>
 		<?php }
 
 		/**
-		 * @param bool $install_callback
-		 * @param bool $remove_callback
-		 *
 		 * @return array
 		 */
-		function bws_handle_demo_data( $install_callback = false, $remove_callback = false ) {
-			if ( isset( $_POST['bws_install_demo_confirm'] ) && check_admin_referer( $this->bws_plugin_basename, 'bws_settings_nonce_name' ) ) {
-				return $this->bws_install_demo_data( $install_callback );
-			} elseif ( isset( $_POST['bws_remove_demo_confirm'] ) && check_admin_referer( $this->bws_plugin_basename, 'bws_settings_nonce_name' ) ) {
-				return $this->bws_remove_demo_data( $remove_callback );
-			} else {
+		function bws_handle_demo_data() {
+			if ( isset( $_POST['bws_install_demo_confirm'] ) )
+				return $this->bws_install_demo_data();
+			elseif ( isset( $_POST['bws_remove_demo_confirm'] ) )
+				return $this->bws_remove_demo_data();
+			else
 				return false;
-			}
 		}
 
 		/**
 		 * Load demo data
 		 *
-		 * @param bool|string $callback
-		 *
 		 * @return array $message   message about the result of the query
 		 */
-		function bws_install_demo_data( $callback = false ) {
+		function bws_install_demo_data() {
 			global $wpdb;
 			/* get demo data*/
 			$message   = array(
@@ -127,19 +132,17 @@
 				 * load demo options
 				 */
 				if ( ! empty( $demo_data['options'] ) ) {
-					$plugin_options = get_option( $this->bws_plugin_prefix . 'options' );
 					/* remember old plugin options */
-					if ( ! empty( $plugin_options ) ) {
-						$this->bws_demo_options['options'] = $plugin_options;
+					if ( ! empty( $this->bws_plugin_options ) ) {
+						$this->bws_demo_options['options'] = $this->bws_plugin_options;
 						$demo_data['options']['display_demo_notice'] = 0;
-						update_option( $this->bws_plugin_prefix . 'options', array_merge( $plugin_options, $demo_data['options'] ) );
+						update_option( $this->bws_plugin_prefix . 'options', array_merge( $this->bws_plugin_options, $demo_data['options'] ) );
 					}
 				} else {
 					/* remove demo notice */
-					$plugin_options = get_option( $this->bws_plugin_prefix . 'options' );
-					if ( 0 != $plugin_options['display_demo_notice'] ) {
-						$plugin_options['display_demo_notice'] = 0;
-						update_option( $this->bws_plugin_prefix . 'options', $plugin_options );
+					if ( 0 != $this->bws_plugin_options['display_demo_notice'] ) {
+						$this->bws_plugin_options['display_demo_notice'] = 0;
+						update_option( $this->bws_plugin_prefix . 'options', $this->bws_plugin_options );
 					}
 				}
 
@@ -153,25 +156,25 @@
 					 * load demo terms
 					 */
 					if ( ! empty( $demo_data['terms'] ) ) {
-						foreach ( $demo_data['terms'] as $taxonomy_name => $terms_values_array ) {
-							foreach ( $terms_values_array as $term_key => $term_value_single ) {
-								$term_exists = term_exists( $term_key, $taxonomy_name );
+						foreach ( $demo_data['terms'] as $taxonomy_slug => $terms_values_array ) {
+							foreach ( $terms_values_array as $term_slug => $term_name ) {
+								$term_exists = term_exists( $term_slug, $taxonomy_slug );
 								if ( ! $term_exists ) {
 									$term_id = wp_insert_term(
-										$term_value_single, /* the term. */
-										$taxonomy_name, /* the taxonomy. */
+										$term_name, /* the term. */
+										$taxonomy_slug, /* the taxonomy. */
 										array(
-											'slug' 			=> $term_key
+											'slug' => $term_slug
 										)
 									);
 									if ( is_wp_error( $term_id ) ) {
 										$error ++;
 									} else {
-										$term_IDs[ $taxonomy_name ][ $term_key ] = $term_id['term_id'];
-										$term_IDs_new[ $taxonomy_name ][ $term_key ] = $term_id['term_id'];
+										$term_IDs[ $taxonomy_slug ][ $term_slug ] = $term_id['term_id'];
+										$term_IDs_new[ $taxonomy_slug ][ $term_slug ] = $term_id['term_id'];
 									}
 								} else {
-									$term_IDs[ $taxonomy_name ][ $term_key ] = $term_exists['term_id'];
+									$term_IDs[ $taxonomy_slug ][ $term_slug ] = $term_exists['term_id'];
 								}
 							}
 						}
@@ -188,30 +191,27 @@
 							$post['post_content'] = preg_replace( '/{last_post_id}/', $post_id, $post['post_content'] );
 						}
 						if ( preg_match( '/{template_page}/', $post['post_content'] ) ) {
-							if ( empty( $page_id ) && ! empty( $page_template ) ) {
+							if ( empty( $page_id ) && ! empty( $page_template ) )
 								$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" ) );
-							}
-							if ( ! empty( $page_id ) ) {
+							if ( ! empty( $page_id ) )
 								$post['post_content'] = preg_replace( '/{template_page}/', '<a href="' . get_permalink( $page_id ) . '">' . get_the_title( $page_id ) . '</a>', $post['post_content'] );
-							}
 						}
 						/* insert current post */
 						$post_id = wp_insert_post( $post );
-						if ( 'post' == $post['post_type'] ) {
+						if ( 'post' == $post['post_type'] )
 							$posttype_post_id = $post_id;
-						}
 
 						/* add taxonomy for posttype */
 						if ( 'post' != $post['post_type'] && 'page' != $post['post_type'] && ! empty( $term_IDs ) ) {
-							foreach ( $term_IDs as $taxonomy_name => $term_array ) {
-								if ( isset( $post['terms'][ $taxonomy_name ] ) ) {
-									$selected_terms = $post['terms'][ $taxonomy_name ];
+							foreach ( $term_IDs as $taxonomy_slug => $term_array ) {
+								if ( isset( $post['terms'][ $taxonomy_slug ] ) ) {
+									$selected_terms = $post['terms'][ $taxonomy_slug ];
 								} else {
 									$selected_terms = array();
 									$selected_terms[] = intval( $term_array[ array_rand( $term_array ) ] );
 								}
 
-								if ( ! wp_set_object_terms( $post_id, $selected_terms, $taxonomy_name, false ) )
+								if ( ! wp_set_object_terms( $post_id, $selected_terms, $taxonomy_slug, false ) )
 									$error ++;
 							}
 						}
@@ -301,7 +301,6 @@
 										 * if there are error unlink the temp file name
 										 */
 										if ( is_wp_error( $tmp ) ) {
-												@unlink( $file_array[ 'tmp_name' ] );
 												continue;
 										}
 
@@ -344,6 +343,13 @@
 								$page_id = $post_id;
 								$page_template = $post['page_template'];
 							}
+							/* save page_id to options */
+							if ( ! empty( $post['save_to_options'] ) ) {
+								$page_id = $post_id;
+								$this->bws_plugin_options[ $post['save_to_options'] ] = $post_id;
+								update_option( $this->bws_plugin_prefix . 'options', $this->bws_plugin_options );
+							}
+
 							/* first inserted image is thummbnail for post */
 							if ( ! empty( $featured_attach_id ) )
 								update_post_meta( $post_id, '_thumbnail_id', $featured_attach_id );
@@ -366,9 +372,11 @@
 
 						if ( ! empty( $demo_data['options'] ) )
 							$message['options'] = $demo_data['options'];
+						else
+							$message['options'] = $this->bws_plugin_options;
 
-						if ( $callback && function_exists( $callback ) )
-							call_user_func( $callback );
+						if ( $this->install_callback && function_exists( $this->install_callback ) )
+							call_user_func( $this->install_callback );
 					} else {
 						$message['error'] = __( 'Demo data installation proceeded with some errors.', $this->bws_plugin_text_domain );
 					}
@@ -434,11 +442,9 @@
 		/**
 		 * Remove demo data
 		 *
-		 * @param $callback
-		 *
 		 * @return array $message   message about the result of the query
 		 */
-		function bws_remove_demo_data( $callback = false ) {
+		function bws_remove_demo_data() {
 			$error        = 0;
 			$message      = array(
 				'error'   => null,
@@ -456,13 +462,12 @@
 				if ( ! empty( $this->bws_demo_options['options'] ) ) {
 					$this->bws_demo_options['options']['display_demo_notice'] = 0;
 					update_option( $this->bws_plugin_prefix . 'options', $this->bws_demo_options['options'] );
-					if ( $callback && function_exists( $callback ) )
-						call_user_func( $callback );
+					if ( $this->remove_callback && function_exists( $this->remove_callback ) )
+						call_user_func( $this->remove_callback );
 				}
 				$done = $this->bws_delete_demo_option();
-				if ( ! $done ) {
+				if ( ! $done )
 					$error ++;
-				}
 
 				/*
 				 * Delete all posts
@@ -472,9 +477,8 @@
 						/* delete only not modified posts */
 						if ( get_post_modified_time( 'U', false, $post_id, false ) == $last_modified ) {
 							$done = wp_delete_post( $post_id, true );
-							if ( ! $done ) {
+							if ( ! $done )
 								$error ++;
-							}
 						}
 					}
 				}
@@ -494,17 +498,15 @@
 				if ( ! empty( $this->bws_demo_options['attachments'] ) ) {
 					foreach ( $this->bws_demo_options['attachments'] as $post_id ) {
 						$done = wp_delete_attachment( $post_id, true );
-						if ( ! $done ) {
+						if ( ! $done )
 							$error ++;
-						}
 					}
 				}
 				if ( ! empty( $this->bws_demo_options['distant_attachments'] ) ) {
 					foreach ( $this->bws_demo_options['distant_attachments'] as $post_id ) {
 						$done = wp_delete_attachment( $post_id, true );
-						if ( ! $done ) {
+						if ( ! $done )
 							$error ++;
-						}
 					}
 				}
 				if ( empty( $error ) ) {
@@ -533,9 +535,8 @@
 				global $wp_version;
 
 				if ( isset( $_POST['bws_hide_demo_notice'] ) && check_admin_referer( $this->bws_plugin_basename, 'bws_demo_nonce_name' ) ) {
-					$plugin_options = get_option( $this->bws_plugin_prefix . 'options' );
-					$plugin_options['display_demo_notice'] = 0;
-					update_option( $this->bws_plugin_prefix . 'options', $plugin_options );
+					$this->bws_plugin_options['display_demo_notice'] = 0;
+					update_option( $this->bws_plugin_prefix . 'options', $this->bws_plugin_options );
 					return;
 				}
 				if ( ! isset( $_POST['bws_handle_demo'] ) && ! isset( $_POST['bws_install_demo_confirm'] ) ) {