: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
if ( ! class_exists( '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;
public function __construct( $args ) {
$plugin_dir_array = explode( '/', $args['plugin_basename'] );
$this->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
function bws_show_demo_button( $form_info ) {
if ( ! ( is_multisite() && is_network_admin() ) ) {
if ( empty( $this->bws_demo_options ) ) {
$button_title = __( 'Install Demo Data', $this->bws_plugin_text_domain );
$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 );
<button class="button" name="bws_handle_demo" value="<?php echo $value; ?>"><?php echo $button_title; ?></button>
<div class="bws_info"><?php echo $form_info; ?></div>
* Display page for confirmation action to install demo data
function bws_demo_confirm() {
if ( 'install' == $_POST['bws_handle_demo'] ) {
$button_title = __( 'Yes, install demo data', $this->bws_plugin_text_domain );
$label = __( 'Are you sure you want to install demo data?', $this->bws_plugin_text_domain );
$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 );
<p><?php echo $label; ?></p>
<form method="post" action="">
<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' ); ?>
* @param bool $install_callback
* @param bool $remove_callback
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 );
* @param bool|string $callback
* @return array $message message about the result of the query
function bws_install_demo_data( $callback = false ) {
'distant_attachments' => NULL,
'distant_attachments_metadata' => NULL,
$page_id = $posttype_post_id = $post_id = '';
@include_once( $this->bws_demo_folder . 'demo-data.php' );
$received_demo_data = bws_demo_data_array();
if ( empty( $received_demo_data ) ) {
$message['error'] = __( 'Can not get demo data.', $this->bws_plugin_text_domain );
$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 );
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'] ) );
$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 ( ! empty( $demo_data['posts'] ) ) {
$wp_upload_dir = wp_upload_dir();
$attachments_folder = $this->bws_demo_folder . 'images';
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 );
$term_id = wp_insert_term(
$term_value_single, /* the term. */
$taxonomy_name, /* the taxonomy. */
if ( is_wp_error( $term_id ) ) {
$term_IDs[ $taxonomy_name ][ $term_key ] = $term_id['term_id'];
$term_IDs_new[ $taxonomy_name ][ $term_key ] = $term_id['term_id'];
$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;
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}/', '<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'] ) {
$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 ];
$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 ( is_wp_error( $post_id ) || 0 == $post_id ) {
} elseif ( ! empty( $post_id ) ) {
$this->bws_demo_options['posts'][ $post_id ] = get_post_modified_time( 'U', false, $post_id, false );
$featured_attach_id = '';
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;
$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_status' => 'inherit'
/* insert attschment in to database */
$attach_id = wp_insert_attachment( $attachment_data, $destination, $post_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 );
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'] );
'name' => $data['title'],
* Check for download errors
* if there are error unlink the temp file name
if ( is_wp_error( $tmp ) ) {
@unlink( $file_array[ 'tmp_name' ] );
$attach_id = media_handle_sideload( $file_array, $post_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 );
/* 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_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 );
add_option( $this->bws_plugin_prefix . 'demo_options', $this->bws_demo_options );
$message['done'] = __( 'Demo data successfully installed.', $this->bws_plugin_text_domain );
if ( ! empty( $posttype_post_id ) ) {
$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>';
if ( ! empty( $page_id ) ) {
$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>';
if ( ! empty( $demo_data['options'] ) )
$message['options'] = $demo_data['options'];
if ( $callback && function_exists( $callback ) )
call_user_func( $callback );
$message['error'] = __( 'Demo data installation proceeded with some errors.', $this->bws_plugin_text_domain );
$message['error'] = __( 'Post data is missing.', $this->bws_plugin_text_domain );
* Change url for distant attachments
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://' ) );
* Replace metadata to default for images after saving ( to prevent editing image )
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'] ) ) {
@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();
'width' => $imagesize[0],
'height' => $imagesize[1],
'file' => $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['url'],
* Change url for distant attachments
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 array $message message about the result of the query
function bws_remove_demo_data( $callback = false ) {
if ( empty( $this->bws_demo_options ) ) {
$message['error'] = __( 'Demo data have been already removed.', $this->bws_plugin_text_domain );
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 ( ! 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 ( ! 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 );
if ( ! empty( $this->bws_demo_options['attachments'] ) ) {
foreach ( $this->bws_demo_options['attachments'] as $post_id ) {
$done = wp_delete_attachment( $post_id, true );