Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/portfoli.../includes/demo-dat...
File: class-bws-demo-data.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Load demo data
[3] Fix | Delete
* @version 1.0.4
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
if ( ! class_exists( 'Bws_Demo_Data' ) ) {
[7] Fix | Delete
class Bws_Demo_Data {
[8] Fix | Delete
private $bws_plugin_text_domain, $bws_plugin_prefix, $bws_plugin_page, $bws_plugin_name, $bws_plugin_basename, $bws_demo_options, $bws_demo_folder;
[9] Fix | Delete
[10] Fix | Delete
public function __construct( $args ) {
[11] Fix | Delete
$plugin_dir_array = explode( '/', $args['plugin_basename'] );
[12] Fix | Delete
$this->bws_plugin_basename = $args['plugin_basename'];
[13] Fix | Delete
$this->bws_plugin_prefix = $args['plugin_prefix'];
[14] Fix | Delete
$this->bws_plugin_name = $args['plugin_name'];
[15] Fix | Delete
$this->bws_plugin_page = $args['plugin_page'];
[16] Fix | Delete
$this->bws_demo_folder = $args['demo_folder'];
[17] Fix | Delete
$this->bws_plugin_text_domain = $plugin_dir_array[0];
[18] Fix | Delete
$this->bws_demo_options = get_option( $this->bws_plugin_prefix . 'demo_options' );
[19] Fix | Delete
}
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Display "Install demo data" or "Uninstal demo data" buttons
[23] Fix | Delete
* @return void
[24] Fix | Delete
*/
[25] Fix | Delete
function bws_show_demo_button( $form_info ) {
[26] Fix | Delete
if ( ! ( is_multisite() && is_network_admin() ) ) {
[27] Fix | Delete
if ( empty( $this->bws_demo_options ) ) {
[28] Fix | Delete
$value = 'install';
[29] Fix | Delete
$button_title = __( 'Install Demo Data', $this->bws_plugin_text_domain );
[30] Fix | Delete
} else {
[31] Fix | Delete
$value = 'remove';
[32] Fix | Delete
$button_title = __( 'Remove Demo Data', $this->bws_plugin_text_domain );
[33] Fix | Delete
$form_info = __( 'Delete demo data and restore previous plugin settings.', $this->bws_plugin_text_domain );
[34] Fix | Delete
} ?>
[35] Fix | Delete
<button class="button" name="bws_handle_demo" value="<?php echo $value; ?>"><?php echo $button_title; ?></button>
[36] Fix | Delete
<div class="bws_info"><?php echo $form_info; ?></div>
[37] Fix | Delete
<?php }
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* Display page for confirmation action to install demo data
[42] Fix | Delete
* @return void
[43] Fix | Delete
*/
[44] Fix | Delete
function bws_demo_confirm() {
[45] Fix | Delete
if ( 'install' == $_POST['bws_handle_demo'] ) {
[46] Fix | Delete
$button_title = __( 'Yes, install demo data', $this->bws_plugin_text_domain );
[47] Fix | Delete
$label = __( 'Are you sure you want to install demo data?', $this->bws_plugin_text_domain );
[48] Fix | Delete
} else {
[49] Fix | Delete
$button_title = __( 'Yes, remove demo data', $this->bws_plugin_text_domain );
[50] Fix | Delete
$label = __( 'Are you sure you want to remove demo data?', $this->bws_plugin_text_domain );
[51] Fix | Delete
} ?>
[52] Fix | Delete
<div>
[53] Fix | Delete
<p><?php echo $label; ?></p>
[54] Fix | Delete
<form method="post" action="">
[55] Fix | Delete
<p>
[56] Fix | Delete
<button class="button button-primary" name="bws_<?php echo $_POST['bws_handle_demo']; ?>_demo_confirm" value="true"><?php echo $button_title; ?></button>
[57] Fix | Delete
<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>
[58] Fix | Delete
<?php wp_nonce_field( $this->bws_plugin_basename, 'bws_settings_nonce_name' ); ?>
[59] Fix | Delete
</p>
[60] Fix | Delete
</form>
[61] Fix | Delete
</div>
[62] Fix | Delete
<?php }
[63] Fix | Delete
[64] Fix | Delete
/**
[65] Fix | Delete
* @param bool $install_callback
[66] Fix | Delete
* @param bool $remove_callback
[67] Fix | Delete
*
[68] Fix | Delete
* @return array
[69] Fix | Delete
*/
[70] Fix | Delete
function bws_handle_demo_data( $install_callback = false, $remove_callback = false ) {
[71] Fix | Delete
if ( isset( $_POST['bws_install_demo_confirm'] ) && check_admin_referer( $this->bws_plugin_basename, 'bws_settings_nonce_name' ) ) {
[72] Fix | Delete
return $this->bws_install_demo_data( $install_callback );
[73] Fix | Delete
} elseif ( isset( $_POST['bws_remove_demo_confirm'] ) && check_admin_referer( $this->bws_plugin_basename, 'bws_settings_nonce_name' ) ) {
[74] Fix | Delete
return $this->bws_remove_demo_data( $remove_callback );
[75] Fix | Delete
} else {
[76] Fix | Delete
return false;
[77] Fix | Delete
}
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
/**
[81] Fix | Delete
* Load demo data
[82] Fix | Delete
*
[83] Fix | Delete
* @param bool|string $callback
[84] Fix | Delete
*
[85] Fix | Delete
* @return array $message message about the result of the query
[86] Fix | Delete
*/
[87] Fix | Delete
function bws_install_demo_data( $callback = false ) {
[88] Fix | Delete
global $wpdb;
[89] Fix | Delete
/* get demo data*/
[90] Fix | Delete
$message = array(
[91] Fix | Delete
'error' => NULL,
[92] Fix | Delete
'done' => NULL,
[93] Fix | Delete
'options' => NULL
[94] Fix | Delete
);
[95] Fix | Delete
$demo_data = array(
[96] Fix | Delete
'posts' => NULL,
[97] Fix | Delete
'attachments' => NULL,
[98] Fix | Delete
'distant_attachments' => NULL,
[99] Fix | Delete
'distant_attachments_metadata' => NULL,
[100] Fix | Delete
'terms' => NULL,
[101] Fix | Delete
'options' => NULL
[102] Fix | Delete
);
[103] Fix | Delete
$error = 0;
[104] Fix | Delete
$page_id = $posttype_post_id = $post_id = '';
[105] Fix | Delete
/* get demo data */
[106] Fix | Delete
@include_once( $this->bws_demo_folder . 'demo-data.php' );
[107] Fix | Delete
$received_demo_data = bws_demo_data_array();
[108] Fix | Delete
[109] Fix | Delete
/*
[110] Fix | Delete
* load demo data
[111] Fix | Delete
*/
[112] Fix | Delete
if ( empty( $received_demo_data ) ) {
[113] Fix | Delete
$message['error'] = __( 'Can not get demo data.', $this->bws_plugin_text_domain );
[114] Fix | Delete
} else {
[115] Fix | Delete
$demo_data = array_merge( $demo_data, $received_demo_data );
[116] Fix | Delete
/*
[117] Fix | Delete
* check if demo options already loaded
[118] Fix | Delete
*/
[119] Fix | Delete
if ( ! empty( $this->bws_demo_options ) ) {
[120] Fix | Delete
$message['error'] = __( 'Demo settings are already installed.', $this->bws_plugin_text_domain );
[121] Fix | Delete
return $message;
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
/*
[125] Fix | Delete
* load demo options
[126] Fix | Delete
*/
[127] Fix | Delete
if ( ! empty( $demo_data['options'] ) ) {
[128] Fix | Delete
$plugin_options = get_option( $this->bws_plugin_prefix . 'options' );
[129] Fix | Delete
/* remember old plugin options */
[130] Fix | Delete
if ( ! empty( $plugin_options ) ) {
[131] Fix | Delete
$this->bws_demo_options['options'] = $plugin_options;
[132] Fix | Delete
$demo_data['options']['display_demo_notice'] = 0;
[133] Fix | Delete
update_option( $this->bws_plugin_prefix . 'options', array_merge( $plugin_options, $demo_data['options'] ) );
[134] Fix | Delete
}
[135] Fix | Delete
} else {
[136] Fix | Delete
/* remove demo notice */
[137] Fix | Delete
$plugin_options = get_option( $this->bws_plugin_prefix . 'options' );
[138] Fix | Delete
if ( 0 != $plugin_options['display_demo_notice'] ) {
[139] Fix | Delete
$plugin_options['display_demo_notice'] = 0;
[140] Fix | Delete
update_option( $this->bws_plugin_prefix . 'options', $plugin_options );
[141] Fix | Delete
}
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
/*
[145] Fix | Delete
* load demo posts
[146] Fix | Delete
*/
[147] Fix | Delete
if ( ! empty( $demo_data['posts'] ) ) {
[148] Fix | Delete
$wp_upload_dir = wp_upload_dir();
[149] Fix | Delete
$attachments_folder = $this->bws_demo_folder . 'images';
[150] Fix | Delete
/*
[151] Fix | Delete
* load demo terms
[152] Fix | Delete
*/
[153] Fix | Delete
if ( ! empty( $demo_data['terms'] ) ) {
[154] Fix | Delete
foreach ( $demo_data['terms'] as $taxonomy_name => $terms_values_array ) {
[155] Fix | Delete
foreach ( $terms_values_array as $term_key => $term_value_single ) {
[156] Fix | Delete
$term_exists = term_exists( $term_key, $taxonomy_name );
[157] Fix | Delete
if ( ! $term_exists ) {
[158] Fix | Delete
$term_id = wp_insert_term(
[159] Fix | Delete
$term_value_single, /* the term. */
[160] Fix | Delete
$taxonomy_name, /* the taxonomy. */
[161] Fix | Delete
array(
[162] Fix | Delete
'slug' => $term_key
[163] Fix | Delete
)
[164] Fix | Delete
);
[165] Fix | Delete
if ( is_wp_error( $term_id ) ) {
[166] Fix | Delete
$error ++;
[167] Fix | Delete
} else {
[168] Fix | Delete
$term_IDs[ $taxonomy_name ][ $term_key ] = $term_id['term_id'];
[169] Fix | Delete
$term_IDs_new[ $taxonomy_name ][ $term_key ] = $term_id['term_id'];
[170] Fix | Delete
}
[171] Fix | Delete
} else {
[172] Fix | Delete
$term_IDs[ $taxonomy_name ][ $term_key ] = $term_exists['term_id'];
[173] Fix | Delete
}
[174] Fix | Delete
}
[175] Fix | Delete
}
[176] Fix | Delete
if ( ! empty( $term_IDs_new ) ) {
[177] Fix | Delete
$this->bws_demo_options['terms'] = isset( $this->bws_demo_options['terms'] ) ? array_merge( $this->bws_demo_options['terms'], $term_IDs_new ) : $term_IDs_new;
[178] Fix | Delete
}
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
/*
[182] Fix | Delete
* load demo posts
[183] Fix | Delete
*/
[184] Fix | Delete
foreach ( $demo_data['posts'] as $post ) {
[185] Fix | Delete
if ( preg_match( '/{last_post_id}/', $post['post_content'] ) && ! empty( $post_id ) ) {
[186] Fix | Delete
$post['post_content'] = preg_replace( '/{last_post_id}/', $post_id, $post['post_content'] );
[187] Fix | Delete
}
[188] Fix | Delete
if ( preg_match( '/{template_page}/', $post['post_content'] ) ) {
[189] Fix | Delete
if ( empty( $page_id ) && ! empty( $page_template ) ) {
[190] Fix | Delete
$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" ) );
[191] Fix | Delete
}
[192] Fix | Delete
if ( ! empty( $page_id ) ) {
[193] Fix | Delete
$post['post_content'] = preg_replace( '/{template_page}/', '<a href="' . get_permalink( $page_id ) . '">' . get_the_title( $page_id ) . '</a>', $post['post_content'] );
[194] Fix | Delete
}
[195] Fix | Delete
}
[196] Fix | Delete
/* insert current post */
[197] Fix | Delete
$post_id = wp_insert_post( $post );
[198] Fix | Delete
if ( 'post' == $post['post_type'] ) {
[199] Fix | Delete
$posttype_post_id = $post_id;
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
/* add taxonomy for posttype */
[203] Fix | Delete
if ( 'post' != $post['post_type'] && 'page' != $post['post_type'] && ! empty( $term_IDs ) ) {
[204] Fix | Delete
foreach ( $term_IDs as $taxonomy_name => $term_array ) {
[205] Fix | Delete
if ( isset( $post['terms'][ $taxonomy_name ] ) ) {
[206] Fix | Delete
$selected_terms = $post['terms'][ $taxonomy_name ];
[207] Fix | Delete
} else {
[208] Fix | Delete
$selected_terms = array();
[209] Fix | Delete
$selected_terms[] = intval( $term_array[ array_rand( $term_array ) ] );
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
if ( ! wp_set_object_terms( $post_id, $selected_terms, $taxonomy_name, false ) )
[213] Fix | Delete
$error ++;
[214] Fix | Delete
}
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
$attach_id = 0;
[218] Fix | Delete
[219] Fix | Delete
if ( is_wp_error( $post_id ) || 0 == $post_id ) {
[220] Fix | Delete
$error ++;
[221] Fix | Delete
} elseif ( ! empty( $post_id ) ) {
[222] Fix | Delete
/* remember post ID */
[223] Fix | Delete
$this->bws_demo_options['posts'][ $post_id ] = get_post_modified_time( 'U', false, $post_id, false );
[224] Fix | Delete
[225] Fix | Delete
$featured_attach_id = '';
[226] Fix | Delete
/*
[227] Fix | Delete
* load post attachments
[228] Fix | Delete
*/
[229] Fix | Delete
if ( ! empty( $post['attachments_folder'] ) ) {
[230] Fix | Delete
$attachments_list = @scandir( $attachments_folder . '/' . $post['attachments_folder'] );
[231] Fix | Delete
if ( 2 < count( $attachments_list ) ) {
[232] Fix | Delete
foreach ( $attachments_list as $attachment ) {
[233] Fix | Delete
$file = $attachments_folder . '/' . $post['attachments_folder'] . '/' . $attachment;
[234] Fix | Delete
/* insert current attachment */
[235] Fix | Delete
/* Check if file is image */
[236] Fix | Delete
$file_data = @getimagesize( $file );
[237] Fix | Delete
$bws_is_image = ! ( $file_data || in_array( $file_data[2], array( 1, 2, 3 ) ) ) ? false : true;
[238] Fix | Delete
if ( $bws_is_image ) {
[239] Fix | Delete
[240] Fix | Delete
$destination = $wp_upload_dir['path'] . '/' . $this->bws_plugin_prefix . 'demo_' . $attachment; /* path to new file */
[241] Fix | Delete
$wp_filetype = wp_check_filetype( $file, null ); /* Mime-type */
[242] Fix | Delete
[243] Fix | Delete
if ( copy( $file, $destination ) ) { /* if attachment copied */
[244] Fix | Delete
[245] Fix | Delete
$attachment_data = array(
[246] Fix | Delete
'post_mime_type' => $wp_filetype['type'],
[247] Fix | Delete
'post_title' => $attachment,
[248] Fix | Delete
'post_content' => '',
[249] Fix | Delete
'post_status' => 'inherit'
[250] Fix | Delete
);
[251] Fix | Delete
[252] Fix | Delete
/* insert attschment in to database */
[253] Fix | Delete
$attach_id = wp_insert_attachment( $attachment_data, $destination, $post_id );
[254] Fix | Delete
if ( 0 != $attach_id ) {
[255] Fix | Delete
if ( empty( $featured_attach_id ) )
[256] Fix | Delete
$featured_attach_id = $attach_id;
[257] Fix | Delete
/* remember attachment ID */
[258] Fix | Delete
$this->bws_demo_options['attachments'][] = $attach_id;
[259] Fix | Delete
[260] Fix | Delete
/* insert attachment metadata */
[261] Fix | Delete
$attach_data = wp_generate_attachment_metadata( $attach_id, $destination );
[262] Fix | Delete
wp_update_attachment_metadata( $attach_id, $attach_data );
[263] Fix | Delete
/* insert additional metadata */
[264] Fix | Delete
if ( isset( $demo_data['attachments'][ $attachment ] ) ) {
[265] Fix | Delete
foreach ( $demo_data['attachments'][ $attachment ] as $meta_key => $meta_value ) {
[266] Fix | Delete
if ( '{get_lorem_ipsum}' == $meta_value )
[267] Fix | Delete
$meta_value = $this->bws_get_lorem_ipsum();
[268] Fix | Delete
add_post_meta( $attach_id, $meta_key, $meta_value );
[269] Fix | Delete
}
[270] Fix | Delete
}
[271] Fix | Delete
} else {
[272] Fix | Delete
$error ++;
[273] Fix | Delete
}
[274] Fix | Delete
} else {
[275] Fix | Delete
$error ++;
[276] Fix | Delete
}
[277] Fix | Delete
}
[278] Fix | Delete
}
[279] Fix | Delete
}
[280] Fix | Delete
}
[281] Fix | Delete
[282] Fix | Delete
/*
[283] Fix | Delete
* load post attachments
[284] Fix | Delete
*/
[285] Fix | Delete
if ( ! empty( $post['distant_attachments'] ) ) {
[286] Fix | Delete
foreach ( $post['distant_attachments'] as $attachment_name ) {
[287] Fix | Delete
if ( isset( $demo_data['distant_attachments_metadata'][ $attachment_name ] ) ) {
[288] Fix | Delete
$data = $demo_data['distant_attachments_metadata'][ $attachment_name ];
[289] Fix | Delete
[290] Fix | Delete
$tmp = download_url( $data['url'] );
[291] Fix | Delete
[292] Fix | Delete
$file_array = array(
[293] Fix | Delete
'name' => $data['title'],
[294] Fix | Delete
'tmp_name' => $tmp
[295] Fix | Delete
);
[296] Fix | Delete
[297] Fix | Delete
/**
[298] Fix | Delete
* Check for download errors
[299] Fix | Delete
* if there are error unlink the temp file name
[300] Fix | Delete
*/
[301] Fix | Delete
if ( is_wp_error( $tmp ) ) {
[302] Fix | Delete
@unlink( $file_array[ 'tmp_name' ] );
[303] Fix | Delete
continue;
[304] Fix | Delete
}
[305] Fix | Delete
[306] Fix | Delete
$attach_id = media_handle_sideload( $file_array, $post_id );
[307] Fix | Delete
[308] Fix | Delete
if ( 0 != $attach_id ) {
[309] Fix | Delete
if ( empty( $featured_attach_id ) ) {
[310] Fix | Delete
$featured_attach_id = $attach_id;
[311] Fix | Delete
}
[312] Fix | Delete
/* remember attachment ID */
[313] Fix | Delete
$this->bws_demo_options['distant_attachments'][ $attachment_name ] = $attach_id;
[314] Fix | Delete
[315] Fix | Delete
/* insert additional metadata */
[316] Fix | Delete
if ( isset( $demo_data['distant_attachments'][ $attachment_name ] ) ) {
[317] Fix | Delete
foreach ( $demo_data['distant_attachments'][ $attachment_name ] as $meta_key => $meta_value ) {
[318] Fix | Delete
if ( '{get_lorem_ipsum}' == $meta_value ) {
[319] Fix | Delete
$meta_value = $this->bws_get_lorem_ipsum();
[320] Fix | Delete
}
[321] Fix | Delete
add_post_meta( $attach_id, $meta_key, $meta_value );
[322] Fix | Delete
}
[323] Fix | Delete
}
[324] Fix | Delete
} else {
[325] Fix | Delete
$error ++;
[326] Fix | Delete
}
[327] Fix | Delete
} else {
[328] Fix | Delete
$error ++;
[329] Fix | Delete
}
[330] Fix | Delete
}
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
/* insert additional post meta */
[334] Fix | Delete
if ( isset( $post['post_meta'] ) && ! empty( $post['post_meta'] ) ) {
[335] Fix | Delete
foreach ( $post['post_meta'] as $meta_key => $meta_value ) {
[336] Fix | Delete
add_post_meta( $post_id, $meta_key, $meta_value );
[337] Fix | Delete
}
[338] Fix | Delete
}
[339] Fix | Delete
/* set template for post type "page" */
[340] Fix | Delete
if ( ! empty( $post['page_template'] ) ) {
[341] Fix | Delete
update_post_meta( $post_id, '_wp_page_template', $post['page_template'] );
[342] Fix | Delete
$page_id = $post_id;
[343] Fix | Delete
$page_template = $post['page_template'];
[344] Fix | Delete
}
[345] Fix | Delete
/* first inserted image is thummbnail for post */
[346] Fix | Delete
if ( ! empty( $featured_attach_id ) )
[347] Fix | Delete
update_post_meta( $post_id, '_thumbnail_id', $featured_attach_id );
[348] Fix | Delete
}
[349] Fix | Delete
}
[350] Fix | Delete
[351] Fix | Delete
/*
[352] Fix | Delete
* Save demo options
[353] Fix | Delete
*/
[354] Fix | Delete
add_option( $this->bws_plugin_prefix . 'demo_options', $this->bws_demo_options );
[355] Fix | Delete
[356] Fix | Delete
if ( 0 == $error ) {
[357] Fix | Delete
$message['done'] = __( 'Demo data successfully installed.', $this->bws_plugin_text_domain );
[358] Fix | Delete
if ( ! empty( $posttype_post_id ) ) {
[359] Fix | Delete
$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>';
[360] Fix | Delete
}
[361] Fix | Delete
if ( ! empty( $page_id ) ) {
[362] Fix | Delete
$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>';
[363] Fix | Delete
}
[364] Fix | Delete
[365] Fix | Delete
if ( ! empty( $demo_data['options'] ) )
[366] Fix | Delete
$message['options'] = $demo_data['options'];
[367] Fix | Delete
[368] Fix | Delete
if ( $callback && function_exists( $callback ) )
[369] Fix | Delete
call_user_func( $callback );
[370] Fix | Delete
} else {
[371] Fix | Delete
$message['error'] = __( 'Demo data installation proceeded with some errors.', $this->bws_plugin_text_domain );
[372] Fix | Delete
}
[373] Fix | Delete
} else {
[374] Fix | Delete
$message['error'] = __( 'Post data is missing.', $this->bws_plugin_text_domain );
[375] Fix | Delete
}
[376] Fix | Delete
}
[377] Fix | Delete
return $message;
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
/**
[381] Fix | Delete
* Change url for distant attachments
[382] Fix | Delete
* @return $url string
[383] Fix | Delete
*/
[384] Fix | Delete
function bws_wp_get_attachment_url( $url, $id ) {
[385] Fix | Delete
if ( ! empty( $this->bws_demo_options['distant_attachments'] ) && in_array( $id, $this->bws_demo_options['distant_attachments'] ) ) {
[386] Fix | Delete
$url = substr( $url, strpos( $url, 'https://' ) );
[387] Fix | Delete
}
[388] Fix | Delete
return $url;
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
/**
[392] Fix | Delete
* Replace metadata to default for images after saving ( to prevent editing image )
[393] Fix | Delete
* @return $data array()
[394] Fix | Delete
*/
[395] Fix | Delete
function bws_wp_update_attachment_metadata( $data, $id ) {
[396] Fix | Delete
if ( ! empty( $data ) && ! empty( $this->bws_demo_options['distant_attachments'] ) && $attachment_name = array_search( $id, $this->bws_demo_options['distant_attachments'] ) ) {
[397] Fix | Delete
/* get demo data */
[398] Fix | Delete
@include_once( $this->bws_demo_folder . 'demo-data.php' );
[399] Fix | Delete
$received_demo_data = bws_demo_data_array();
[400] Fix | Delete
[401] Fix | Delete
if ( isset( $received_demo_data['distant_attachments_metadata'][ $attachment_name ] ) ) {
[402] Fix | Delete
[403] Fix | Delete
/* insert attachment metadata */
[404] Fix | Delete
$imagesize = @getimagesize( $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['url'] );
[405] Fix | Delete
$sizes = ( isset( $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['sizes'] ) ) ? $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['sizes'] : array();
[406] Fix | Delete
$data = array(
[407] Fix | Delete
'width' => $imagesize[0],
[408] Fix | Delete
'height' => $imagesize[1],
[409] Fix | Delete
'file' => $received_demo_data['distant_attachments_metadata'][ $attachment_name ]['url'],
[410] Fix | Delete
'sizes' => $sizes
[411] Fix | Delete
);
[412] Fix | Delete
}
[413] Fix | Delete
}
[414] Fix | Delete
return $data;
[415] Fix | Delete
}
[416] Fix | Delete
[417] Fix | Delete
/**
[418] Fix | Delete
* Change url for distant attachments
[419] Fix | Delete
* @return $url string
[420] Fix | Delete
*/
[421] Fix | Delete
function bws_wp_get_attachment_image_attributes( $attr, $attachment, $size = false ) {
[422] Fix | Delete
if ( ! empty( $attr['srcset'] ) && ! empty( $this->bws_demo_options['distant_attachments'] ) && in_array( $attachment->ID, $this->bws_demo_options['distant_attachments'] ) ) {
[423] Fix | Delete
$srcset = explode( ', ', $attr['srcset'] );
[424] Fix | Delete
foreach ( $srcset as $key => $value ) {
[425] Fix | Delete
$srcset[ $key ] = substr( $value, strpos( $value, 'https://' ) );
[426] Fix | Delete
}
[427] Fix | Delete
$attr['srcset'] = implode( ', ', $srcset );
[428] Fix | Delete
}
[429] Fix | Delete
return $attr;
[430] Fix | Delete
}
[431] Fix | Delete
[432] Fix | Delete
/**
[433] Fix | Delete
* Remove demo data
[434] Fix | Delete
*
[435] Fix | Delete
* @param $callback
[436] Fix | Delete
*
[437] Fix | Delete
* @return array $message message about the result of the query
[438] Fix | Delete
*/
[439] Fix | Delete
function bws_remove_demo_data( $callback = false ) {
[440] Fix | Delete
$error = 0;
[441] Fix | Delete
$message = array(
[442] Fix | Delete
'error' => null,
[443] Fix | Delete
'done' => null,
[444] Fix | Delete
'options' => null
[445] Fix | Delete
);
[446] Fix | Delete
[447] Fix | Delete
if ( empty( $this->bws_demo_options ) ) {
[448] Fix | Delete
$message['error'] = __( 'Demo data have been already removed.', $this->bws_plugin_text_domain );
[449] Fix | Delete
} else {
[450] Fix | Delete
[451] Fix | Delete
/*
[452] Fix | Delete
* Restore plugin options
[453] Fix | Delete
*/
[454] Fix | Delete
if ( ! empty( $this->bws_demo_options['options'] ) ) {
[455] Fix | Delete
$this->bws_demo_options['options']['display_demo_notice'] = 0;
[456] Fix | Delete
update_option( $this->bws_plugin_prefix . 'options', $this->bws_demo_options['options'] );
[457] Fix | Delete
if ( $callback && function_exists( $callback ) )
[458] Fix | Delete
call_user_func( $callback );
[459] Fix | Delete
}
[460] Fix | Delete
$done = $this->bws_delete_demo_option();
[461] Fix | Delete
if ( ! $done ) {
[462] Fix | Delete
$error ++;
[463] Fix | Delete
}
[464] Fix | Delete
[465] Fix | Delete
/*
[466] Fix | Delete
* Delete all posts
[467] Fix | Delete
*/
[468] Fix | Delete
if ( ! empty( $this->bws_demo_options['posts'] ) ) {
[469] Fix | Delete
foreach ( $this->bws_demo_options['posts'] as $post_id => $last_modified ) {
[470] Fix | Delete
/* delete only not modified posts */
[471] Fix | Delete
if ( get_post_modified_time( 'U', false, $post_id, false ) == $last_modified ) {
[472] Fix | Delete
$done = wp_delete_post( $post_id, true );
[473] Fix | Delete
if ( ! $done ) {
[474] Fix | Delete
$error ++;
[475] Fix | Delete
}
[476] Fix | Delete
}
[477] Fix | Delete
}
[478] Fix | Delete
}
[479] Fix | Delete
[480] Fix | Delete
/* Delete terms */
[481] Fix | Delete
if ( ! empty( $this->bws_demo_options['terms'] ) ) {
[482] Fix | Delete
foreach ( $this->bws_demo_options['terms'] as $taxonomy_name => $terms_values_array ) {
[483] Fix | Delete
foreach ( $terms_values_array as $term_id ) {
[484] Fix | Delete
wp_delete_term( $term_id, $taxonomy_name );
[485] Fix | Delete
}
[486] Fix | Delete
}
[487] Fix | Delete
}
[488] Fix | Delete
[489] Fix | Delete
/*
[490] Fix | Delete
* Delete all attachments
[491] Fix | Delete
*/
[492] Fix | Delete
if ( ! empty( $this->bws_demo_options['attachments'] ) ) {
[493] Fix | Delete
foreach ( $this->bws_demo_options['attachments'] as $post_id ) {
[494] Fix | Delete
$done = wp_delete_attachment( $post_id, true );
[495] Fix | Delete
if ( ! $done ) {
[496] Fix | Delete
$error ++;
[497] Fix | Delete
}
[498] Fix | Delete
}
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function