diff -r 7b1b88e27a20 -r 48c4eec2b7e6 wp/wp-content/plugins/portfolio/includes/demo-data/class-bws-demo-data.php
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/wp/wp-content/plugins/portfolio/includes/demo-data/class-bws-demo-data.php Fri Sep 05 18:40:08 2025 +0200
@@ -0,0 +1,660 @@
+bws_plugin_basename = $args['plugin_basename'];
+ $this->bws_plugin_prefix = $args['plugin_prefix'];
+ $this->bws_plugin_name = $args['plugin_name'];
+ $this->bws_plugin_page = $args['plugin_page'];
+ $this->bws_demo_folder = $args['demo_folder'];
+ $this->bws_plugin_text_domain = $plugin_dir_array[0];
+ $this->bws_demo_options = get_option( $this->bws_plugin_prefix . 'demo_options' );
+ }
+
+ /**
+ * Display "Install demo data" or "Uninstal demo data" buttons
+ * @return void
+ */
+ function bws_show_demo_button( $form_info ) {
+ if ( ! ( is_multisite() && is_network_admin() ) ) {
+ if ( empty( $this->bws_demo_options ) ) {
+ $value = 'install';
+ $button_title = __( 'Install Demo Data', $this->bws_plugin_text_domain );
+ } else {
+ $value = 'remove';
+ $button_title = __( 'Remove Demo Data', $this->bws_plugin_text_domain );
+ $form_info = __( 'Delete demo data and restore previous plugin settings.', $this->bws_plugin_text_domain );
+ } ?>
+
+
+ bws_plugin_text_domain );
+ $label = __( 'Are you sure you want to install demo data?', $this->bws_plugin_text_domain );
+ } else {
+ $button_title = __( 'Yes, remove demo data', $this->bws_plugin_text_domain );
+ $label = __( 'Are you sure you want to remove demo data?', $this->bws_plugin_text_domain );
+ } ?>
+
+ 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 {
+ 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 ) {
+ global $wpdb;
+ /* get demo data*/
+ $message = array(
+ 'error' => NULL,
+ 'done' => NULL,
+ 'options' => NULL
+ );
+ $demo_data = array(
+ 'posts' => NULL,
+ 'attachments' => NULL,
+ 'distant_attachments' => NULL,
+ 'distant_attachments_metadata' => NULL,
+ 'terms' => NULL,
+ 'options' => NULL
+ );
+ $error = 0;
+ $page_id = $posttype_post_id = $post_id = '';
+ /* get demo data */
+ @include_once( $this->bws_demo_folder . 'demo-data.php' );
+ $received_demo_data = bws_demo_data_array();
+
+ /*
+ * load demo data
+ */
+ if ( empty( $received_demo_data ) ) {
+ $message['error'] = __( 'Can not get demo data.', $this->bws_plugin_text_domain );
+ } else {
+ $demo_data = array_merge( $demo_data, $received_demo_data );
+ /*
+ * check if demo options already loaded
+ */
+ if ( ! empty( $this->bws_demo_options ) ) {
+ $message['error'] = __( 'Demo settings are already installed.', $this->bws_plugin_text_domain );
+ return $message;
+ }
+
+ /*
+ * 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;
+ $demo_data['options']['display_demo_notice'] = 0;
+ update_option( $this->bws_plugin_prefix . 'options', array_merge( $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 );
+ }
+ }
+
+ /*
+ * load demo posts
+ */
+ if ( ! empty( $demo_data['posts'] ) ) {
+ $wp_upload_dir = wp_upload_dir();
+ $attachments_folder = $this->bws_demo_folder . 'images';
+ /*
+ * 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 );
+ if ( ! $term_exists ) {
+ $term_id = wp_insert_term(
+ $term_value_single, /* the term. */
+ $taxonomy_name, /* the taxonomy. */
+ array(
+ 'slug' => $term_key
+ )
+ );
+ 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'];
+ }
+ } else {
+ $term_IDs[ $taxonomy_name ][ $term_key ] = $term_exists['term_id'];
+ }
+ }
+ }
+ if ( ! empty( $term_IDs_new ) ) {
+ $this->bws_demo_options['terms'] = isset( $this->bws_demo_options['terms'] ) ? array_merge( $this->bws_demo_options['terms'], $term_IDs_new ) : $term_IDs_new;
+ }
+ }
+
+ /*
+ * load demo posts
+ */
+ foreach ( $demo_data['posts'] as $post ) {
+ if ( preg_match( '/{last_post_id}/', $post['post_content'] ) && ! empty( $post_id ) ) {
+ $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 ) ) {
+ $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 ) ) {
+ $post['post_content'] = preg_replace( '/{template_page}/', '' . get_the_title( $page_id ) . '', $post['post_content'] );
+ }
+ }
+ /* insert current post */
+ $post_id = wp_insert_post( $post );
+ 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 ];
+ } 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 ) )
+ $error ++;
+ }
+ }
+
+ $attach_id = 0;
+
+ if ( is_wp_error( $post_id ) || 0 == $post_id ) {
+ $error ++;
+ } elseif ( ! empty( $post_id ) ) {
+ /* remember post ID */
+ $this->bws_demo_options['posts'][ $post_id ] = get_post_modified_time( 'U', false, $post_id, false );
+
+ $featured_attach_id = '';
+ /*
+ * load post attachments
+ */
+ if ( ! empty( $post['attachments_folder'] ) ) {
+ $attachments_list = @scandir( $attachments_folder . '/' . $post['attachments_folder'] );
+ if ( 2 < count( $attachments_list ) ) {
+ foreach ( $attachments_list as $attachment ) {
+ $file = $attachments_folder . '/' . $post['attachments_folder'] . '/' . $attachment;
+ /* insert current attachment */
+ /* Check if file is image */
+ $file_data = @getimagesize( $file );
+ $bws_is_image = ! ( $file_data || in_array( $file_data[2], array( 1, 2, 3 ) ) ) ? false : true;
+ if ( $bws_is_image ) {
+
+ $destination = $wp_upload_dir['path'] . '/' . $this->bws_plugin_prefix . 'demo_' . $attachment; /* path to new file */
+ $wp_filetype = wp_check_filetype( $file, null ); /* Mime-type */
+
+ if ( copy( $file, $destination ) ) { /* if attachment copied */
+
+ $attachment_data = array(
+ 'post_mime_type' => $wp_filetype['type'],
+ 'post_title' => $attachment,
+ 'post_content' => '',
+ 'post_status' => 'inherit'
+ );
+
+ /* insert attschment in to database */
+ $attach_id = wp_insert_attachment( $attachment_data, $destination, $post_id );
+ if ( 0 != $attach_id ) {
+ if ( empty( $featured_attach_id ) )
+ $featured_attach_id = $attach_id;
+ /* remember attachment ID */
+ $this->bws_demo_options['attachments'][] = $attach_id;
+
+ /* insert attachment metadata */
+ $attach_data = wp_generate_attachment_metadata( $attach_id, $destination );
+ wp_update_attachment_metadata( $attach_id, $attach_data );
+ /* insert additional metadata */
+ if ( isset( $demo_data['attachments'][ $attachment ] ) ) {
+ foreach ( $demo_data['attachments'][ $attachment ] as $meta_key => $meta_value ) {
+ if ( '{get_lorem_ipsum}' == $meta_value )
+ $meta_value = $this->bws_get_lorem_ipsum();
+ add_post_meta( $attach_id, $meta_key, $meta_value );
+ }
+ }
+ } else {
+ $error ++;
+ }
+ } else {
+ $error ++;
+ }
+ }
+ }
+ }
+ }
+
+ /*
+ * load post attachments
+ */
+ if ( ! empty( $post['distant_attachments'] ) ) {
+ foreach ( $post['distant_attachments'] as $attachment_name ) {
+ if ( isset( $demo_data['distant_attachments_metadata'][ $attachment_name ] ) ) {
+ $data = $demo_data['distant_attachments_metadata'][ $attachment_name ];
+
+ $tmp = download_url( $data['url'] );
+
+ $file_array = array(
+ 'name' => $data['title'],
+ 'tmp_name' => $tmp
+ );
+
+ /**
+ * Check for download errors
+ * if there are error unlink the temp file name
+ */
+ if ( is_wp_error( $tmp ) ) {
+ @unlink( $file_array[ 'tmp_name' ] );
+ continue;
+ }
+
+ $attach_id = media_handle_sideload( $file_array, $post_id );
+
+ if ( 0 != $attach_id ) {
+ if ( empty( $featured_attach_id ) ) {
+ $featured_attach_id = $attach_id;
+ }
+ /* remember attachment ID */
+ $this->bws_demo_options['distant_attachments'][ $attachment_name ] = $attach_id;
+
+ /* insert additional metadata */
+ if ( isset( $demo_data['distant_attachments'][ $attachment_name ] ) ) {
+ foreach ( $demo_data['distant_attachments'][ $attachment_name ] as $meta_key => $meta_value ) {
+ if ( '{get_lorem_ipsum}' == $meta_value ) {
+ $meta_value = $this->bws_get_lorem_ipsum();
+ }
+ add_post_meta( $attach_id, $meta_key, $meta_value );
+ }
+ }
+ } else {
+ $error ++;
+ }
+ } else {
+ $error ++;
+ }
+ }
+ }
+
+ /* insert additional post meta */
+ if ( isset( $post['post_meta'] ) && ! empty( $post['post_meta'] ) ) {
+ foreach ( $post['post_meta'] as $meta_key => $meta_value ) {
+ add_post_meta( $post_id, $meta_key, $meta_value );
+ }
+ }
+ /* set template for post type "page" */
+ if ( ! empty( $post['page_template'] ) ) {
+ update_post_meta( $post_id, '_wp_page_template', $post['page_template'] );
+ $page_id = $post_id;
+ $page_template = $post['page_template'];
+ }
+ /* first inserted image is thummbnail for post */
+ if ( ! empty( $featured_attach_id ) )
+ update_post_meta( $post_id, '_thumbnail_id', $featured_attach_id );
+ }
+ }
+
+ /*
+ * Save demo options
+ */
+ add_option( $this->bws_plugin_prefix . 'demo_options', $this->bws_demo_options );
+
+ if ( 0 == $error ) {
+ $message['done'] = __( 'Demo data successfully installed.', $this->bws_plugin_text_domain );
+ if ( ! empty( $posttype_post_id ) ) {
+ $message['done'] .= '
' . __( 'View post with shortcodes', $this->bws_plugin_text_domain ) . ': ' . get_the_title( $posttype_post_id ) . '';
+ }
+ if ( ! empty( $page_id ) ) {
+ $message['done'] .= '
' . __( 'View page with examples', $this->bws_plugin_text_domain ) . ': ' . get_the_title( $page_id ) . '';
+ }
+
+ if ( ! empty( $demo_data['options'] ) )
+ $message['options'] = $demo_data['options'];
+
+ if ( $callback && function_exists( $callback ) )
+ call_user_func( $callback );
+ } else {
+ $message['error'] = __( 'Demo data installation proceeded with some errors.', $this->bws_plugin_text_domain );
+ }
+ } else {
+ $message['error'] = __( 'Post data is missing.', $this->bws_plugin_text_domain );
+ }
+ }
+ return $message;
+ }
+
+ /**
+ * Change url for distant attachments
+ * @return $url string
+ */
+ function bws_wp_get_attachment_url( $url, $id ) {
+ if ( ! empty( $this->bws_demo_options['distant_attachments'] ) && in_array( $id, $this->bws_demo_options['distant_attachments'] ) ) {
+ $url = substr( $url, strpos( $url, 'https://' ) );
+ }
+ return $url;
+ }
+
+ /**
+ * Replace metadata to default for images after saving ( to prevent editing image )
+ * @return $data array()
+ */
+ function bws_wp_update_attachment_metadata( $data, $id ) {
+ if ( ! empty( $data ) && ! empty( $this->bws_demo_options['distant_attachments'] ) && $attachment_name = array_search( $id, $this->bws_demo_options['distant_attachments'] ) ) {
+ /* get demo data */
+ @include_once( $this->bws_demo_folder . 'demo-data.php' );
+ $received_demo_data = bws_demo_data_array();
+
+ if ( isset( $received_demo_data['distant_attachments_metadata'][ $attachment_name ] ) ) {
+
+ /* insert attachment metadata */
+ $imagesize = @getimagesize( $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['url'] );
+ $sizes = ( isset( $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['sizes'] ) ) ? $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['sizes'] : array();
+ $data = array(
+ 'width' => $imagesize[0],
+ 'height' => $imagesize[1],
+ 'file' => $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['url'],
+ 'sizes' => $sizes
+ );
+ }
+ }
+ return $data;
+ }
+
+ /**
+ * Change url for distant attachments
+ * @return $url string
+ */
+ function bws_wp_get_attachment_image_attributes( $attr, $attachment, $size = false ) {
+ if ( ! empty( $attr['srcset'] ) && ! empty( $this->bws_demo_options['distant_attachments'] ) && in_array( $attachment->ID, $this->bws_demo_options['distant_attachments'] ) ) {
+ $srcset = explode( ', ', $attr['srcset'] );
+ foreach ( $srcset as $key => $value ) {
+ $srcset[ $key ] = substr( $value, strpos( $value, 'https://' ) );
+ }
+ $attr['srcset'] = implode( ', ', $srcset );
+ }
+ return $attr;
+ }
+
+ /**
+ * Remove demo data
+ *
+ * @param $callback
+ *
+ * @return array $message message about the result of the query
+ */
+ function bws_remove_demo_data( $callback = false ) {
+ $error = 0;
+ $message = array(
+ 'error' => null,
+ 'done' => null,
+ 'options' => null
+ );
+
+ if ( empty( $this->bws_demo_options ) ) {
+ $message['error'] = __( 'Demo data have been already removed.', $this->bws_plugin_text_domain );
+ } else {
+
+ /*
+ * Restore plugin options
+ */
+ 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 );
+ }
+ $done = $this->bws_delete_demo_option();
+ if ( ! $done ) {
+ $error ++;
+ }
+
+ /*
+ * Delete all posts
+ */
+ if ( ! empty( $this->bws_demo_options['posts'] ) ) {
+ foreach ( $this->bws_demo_options['posts'] as $post_id => $last_modified ) {
+ /* 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 ) {
+ $error ++;
+ }
+ }
+ }
+ }
+
+ /* Delete terms */
+ if ( ! empty( $this->bws_demo_options['terms'] ) ) {
+ foreach ( $this->bws_demo_options['terms'] as $taxonomy_name => $terms_values_array ) {
+ foreach ( $terms_values_array as $term_id ) {
+ wp_delete_term( $term_id, $taxonomy_name );
+ }
+ }
+ }
+
+ /*
+ * Delete all attachments
+ */
+ 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 ) {
+ $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 ) {
+ $error ++;
+ }
+ }
+ }
+ if ( empty( $error ) ) {
+ $message['done'] = __( 'Demo data successfully removed.', $this->bws_plugin_text_domain );
+ $message['options'] = get_option( $this->bws_plugin_prefix . 'options' );
+ $this->bws_demo_options = array();
+ } else {
+ $message['error'] = __( 'Removing demo data with some errors occurred.', $this->bws_plugin_text_domain );
+ }
+ }
+ return $message;
+ }
+
+ /**
+ * Delete demo-options
+ * @return boolean
+ */
+ function bws_delete_demo_option() {
+ $done = delete_option( $this->bws_plugin_prefix . 'demo_options' );
+ return $done;
+ }
+
+ function bws_handle_demo_notice( $show_demo_notice ) {
+
+ if ( 1 == $show_demo_notice && empty( $this->bws_demo_options ) ) {
+ 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 );
+ return;
+ }
+ if ( ! isset( $_POST['bws_handle_demo'] ) && ! isset( $_POST['bws_install_demo_confirm'] ) ) {
+ if ( 4.2 > $wp_version ) {
+ $plugin_dir_array = explode( '/', plugin_basename( __FILE__ ) ); ?>
+
+
+
+
+
+