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/wp-conte.../plugins/themify-.../admin
File: class-tbp-import-demo.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Handles importing sample themes.
[3] Fix | Delete
*
[4] Fix | Delete
* @link https://themify.me/
[5] Fix | Delete
* @since 1.0.0
[6] Fix | Delete
*
[7] Fix | Delete
* @package Tbp
[8] Fix | Delete
* @subpackage Tbp/admin
[9] Fix | Delete
*/
[10] Fix | Delete
/**
[11] Fix | Delete
*
[12] Fix | Delete
*
[13] Fix | Delete
* @package Tbp
[14] Fix | Delete
* @subpackage Tbp/admin
[15] Fix | Delete
* @author Themify <themify@themify.me>
[16] Fix | Delete
*/
[17] Fix | Delete
final class Tbp_Import_Demo {
[18] Fix | Delete
[19] Fix | Delete
[20] Fix | Delete
public static function run() {
[21] Fix | Delete
add_action( 'wp_ajax_tbp_import_term', array( __CLASS__, 'wp_ajax_tbp_import_term' ) );
[22] Fix | Delete
add_action( 'wp_ajax_tbp_import_post', array( __CLASS__, 'wp_ajax_tbp_import_post' ) );
[23] Fix | Delete
add_action( 'wp_ajax_tbp_import_thumbnail', array( __CLASS__, 'wp_ajax_tbp_import_thumbnail' ) );
[24] Fix | Delete
add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
[25] Fix | Delete
}
[26] Fix | Delete
[27] Fix | Delete
public static function admin_init() {
[28] Fix | Delete
if ( isset( $_REQUEST['page'] ) && !empty( $_GET['tbp_erase_demo'] ) && $_REQUEST['page'] === Tbp_Themes::$post_type ) {
[29] Fix | Delete
self::erase_demo();
[30] Fix | Delete
}
[31] Fix | Delete
}
[32] Fix | Delete
[33] Fix | Delete
public static function wp_ajax_tbp_import_term() {
[34] Fix | Delete
$term = $_POST['term'];
[35] Fix | Delete
$old_id = $term['term_id'];
[36] Fix | Delete
[37] Fix | Delete
if ( $term_id = term_exists( $term['slug'], $term['taxonomy'] ) ) {
[38] Fix | Delete
if ( is_array( $term_id ) ) $term_id = $term_id['term_id'];
[39] Fix | Delete
if ( isset( $term['term_id'] ) ) {
[40] Fix | Delete
update_term_meta( $term_id, '_tbp_demo_id', $old_id );
[41] Fix | Delete
wp_send_json_error();
[42] Fix | Delete
}
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
if ( empty( $term['parent'] ) ) {
[46] Fix | Delete
$parent = 0;
[47] Fix | Delete
} else {
[48] Fix | Delete
$new_id = self::get_term_by_old_id( $term['parent'] );
[49] Fix | Delete
$parent = term_exists( $new_id, $term['taxonomy'] );
[50] Fix | Delete
if ( is_array( $parent ) ) $parent = $parent['term_id'];
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
$id = wp_insert_term( $term['name'], $term['taxonomy'], array(
[54] Fix | Delete
'parent' => $parent,
[55] Fix | Delete
'slug' => $term['slug'],
[56] Fix | Delete
'description' => $term['description'],
[57] Fix | Delete
) );
[58] Fix | Delete
if ( ! is_wp_error( $id ) ) {
[59] Fix | Delete
[60] Fix | Delete
update_term_meta( $id['term_id'], '_tbp_demo_id', $old_id );
[61] Fix | Delete
[62] Fix | Delete
wp_send_json_success( array(
[63] Fix | Delete
'oid' => $old_id, // original ID
[64] Fix | Delete
'id' => $id['term_id'],
[65] Fix | Delete
'type' => 'term'
[66] Fix | Delete
) );
[67] Fix | Delete
} else {
[68] Fix | Delete
wp_send_json_error( $id );
[69] Fix | Delete
}
[70] Fix | Delete
die;
[71] Fix | Delete
}
[72] Fix | Delete
[73] Fix | Delete
public static function wp_ajax_tbp_import_post() {
[74] Fix | Delete
$post = $_POST['post'];
[75] Fix | Delete
$old_id = $post['ID'];
[76] Fix | Delete
[77] Fix | Delete
$post['post_author'] = (int) get_current_user_id();
[78] Fix | Delete
$post['post_status'] = 'publish';
[79] Fix | Delete
unset( $post['ID'] );
[80] Fix | Delete
[81] Fix | Delete
if ( ! post_type_exists( $post['post_type'] ) ) {
[82] Fix | Delete
wp_send_json_error();
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
/* Menu items don't have reliable post_title, skip the post_exists check */
[86] Fix | Delete
/* With tbp_template, different Themes can have duplicate templates so skip post_exists */
[87] Fix | Delete
if ( $post['post_type'] !== 'nav_menu_item' && $post['post_type'] !== 'tbp_template' ) {
[88] Fix | Delete
$post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
[89] Fix | Delete
if ( $post_exists && get_post_type( $post_exists ) === $post['post_type'] ) {
[90] Fix | Delete
update_post_meta( $post_id, '_tbp_demo_id', $old_id );
[91] Fix | Delete
wp_send_json_success( array(
[92] Fix | Delete
'oid' => $old_id, // original ID
[93] Fix | Delete
'id' => $post_exists,
[94] Fix | Delete
'type' => 'post'
[95] Fix | Delete
) );
[96] Fix | Delete
}
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
if ( $post['post_type'] === 'nav_menu_item' ) {
[100] Fix | Delete
if ( ! isset( $post['tax_input']['nav_menu'] ) || ! term_exists( $post['tax_input']['nav_menu'], 'nav_menu' ) ) {
[101] Fix | Delete
wp_send_json_error();
[102] Fix | Delete
}
[103] Fix | Delete
$_menu_item_type = $post['meta_input']['_menu_item_type'];
[104] Fix | Delete
$_menu_item_object_id = $post['meta_input']['_menu_item_object_id'];
[105] Fix | Delete
[106] Fix | Delete
if ( 'taxonomy' === $_menu_item_type ) {
[107] Fix | Delete
$new_id = self::get_term_by_old_id( intval( $_menu_item_object_id ) );
[108] Fix | Delete
if ( $new_id ) {
[109] Fix | Delete
$post['meta_input']['_menu_item_object_id'] = $new_id;
[110] Fix | Delete
}
[111] Fix | Delete
} elseif ( 'post_type' === $_menu_item_type ) {
[112] Fix | Delete
$new_id = self::get_post_by_old_id( intval( $_menu_item_object_id ) );
[113] Fix | Delete
if ( $new_id ) {
[114] Fix | Delete
$post['meta_input']['_menu_item_object_id'] = $new_id;
[115] Fix | Delete
}
[116] Fix | Delete
}
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
$post_parent = ( $post['post_type'] == 'nav_menu_item' ) ? $post['meta_input']['_menu_item_menu_item_parent'] : (int) $post['post_parent'];
[120] Fix | Delete
$post['post_parent'] = 0;
[121] Fix | Delete
if ( $post_parent ) {
[122] Fix | Delete
// if we already know the parent, map it to the new local ID
[123] Fix | Delete
$new_id = self::get_post_by_old_id( $post_parent );
[124] Fix | Delete
if ( $new_id ) {
[125] Fix | Delete
if( $post['post_type'] === 'nav_menu_item' ) {
[126] Fix | Delete
$post['meta_input']['_menu_item_menu_item_parent'] = $new_id;
[127] Fix | Delete
} else {
[128] Fix | Delete
$post['post_parent'] = $new_id;
[129] Fix | Delete
}
[130] Fix | Delete
}
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
/**
[134] Fix | Delete
* for hierarchical taxonomies, IDs must be used so wp_set_post_terms can function properly
[135] Fix | Delete
* convert term slugs to IDs for hierarchical taxonomies
[136] Fix | Delete
*/
[137] Fix | Delete
if ( ! empty( $post['tax_input'] ) ) {
[138] Fix | Delete
foreach( $post['tax_input'] as $tax => $terms ) {
[139] Fix | Delete
if ( ! taxonomy_exists( $tax ) ) {
[140] Fix | Delete
unset( $post['tax_input'][ $tax ] );
[141] Fix | Delete
continue;
[142] Fix | Delete
}
[143] Fix | Delete
if( is_taxonomy_hierarchical( $tax ) ) {
[144] Fix | Delete
$terms = explode( ', ', $terms );
[145] Fix | Delete
$post['tax_input'][ $tax ] = array_map( 'self::get_term_id_by_slug', $terms, array_fill( 0, count( $terms ), $tax ) );
[146] Fix | Delete
}
[147] Fix | Delete
}
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
$post_id = wp_insert_post( $post, true );
[151] Fix | Delete
if ( is_wp_error( $post_id ) ) {
[152] Fix | Delete
wp_send_json_error( $post_id );
[153] Fix | Delete
} else {
[154] Fix | Delete
[155] Fix | Delete
update_post_meta( $post_id, '_tbp_demo_id', $old_id );
[156] Fix | Delete
[157] Fix | Delete
// download featured image
[158] Fix | Delete
if ( isset( $post['thumb'] ) ) {
[159] Fix | Delete
$fetch = self::fetch_remote_file( $post['thumb'], $post_id );
[160] Fix | Delete
if ( ! is_wp_error( $fetch ) ) {
[161] Fix | Delete
set_post_thumbnail( $post_id, $fetch );
[162] Fix | Delete
}
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
// Home page
[166] Fix | Delete
if ( isset( $post['is_home'] ) ) {
[167] Fix | Delete
update_option( 'show_on_front', 'page' );
[168] Fix | Delete
update_option( 'page_on_front', $post_id );
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
if ( $post['post_type'] === 'tbp_theme' ) {
[172] Fix | Delete
Tbp_Utils::set_active_theme( $post_id );
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
elseif ( $post['post_type'] === 'tbp_template' ) {
[176] Fix | Delete
$theme = (int) sanitize_text_field( $_POST['theme_id'] );
[177] Fix | Delete
$theme_post = get_post( $theme );
[178] Fix | Delete
update_post_meta( $post_id, 'tbp_associated_theme', $theme_post->post_name );
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
wp_send_json_success( array(
[182] Fix | Delete
'oid' => $old_id, // original ID
[183] Fix | Delete
'id' => $post_id,
[184] Fix | Delete
'type' => 'post'
[185] Fix | Delete
) );
[186] Fix | Delete
}
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
public static function wp_ajax_tbp_import_thumbnail() {
[190] Fix | Delete
$post_id = (int) $_POST['post_id'];
[191] Fix | Delete
$url = sanitize_text_field( $_POST['thumb'] );
[192] Fix | Delete
[193] Fix | Delete
$fetch = self::fetch_remote_file( $url, $post_id );
[194] Fix | Delete
if ( is_wp_error( $fetch ) ) {
[195] Fix | Delete
wp_send_json_error( $fetch );
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
set_post_thumbnail( $post_id, $fetch );
[199] Fix | Delete
wp_send_json_success();
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
/**
[203] Fix | Delete
* Remove all demo posts, signified by the existence of _tbp_demo_id meta
[204] Fix | Delete
*
[205] Fix | Delete
* @return null
[206] Fix | Delete
*/
[207] Fix | Delete
private static function erase_demo() {
[208] Fix | Delete
$terms = get_terms( array(
[209] Fix | Delete
'taxonomy' => null,
[210] Fix | Delete
'hide_empty' => false,
[211] Fix | Delete
'number' => 0,
[212] Fix | Delete
'meta_query' => array(
[213] Fix | Delete
array(
[214] Fix | Delete
'key' => '_tbp_demo_id',
[215] Fix | Delete
'compare' => 'EXISTS'
[216] Fix | Delete
),
[217] Fix | Delete
),
[218] Fix | Delete
'suppress_filters' => true,
[219] Fix | Delete
) );
[220] Fix | Delete
foreach ( $terms as $term ) {
[221] Fix | Delete
wp_delete_term( $term->term_id, $term->taxonomy );
[222] Fix | Delete
}
[223] Fix | Delete
[224] Fix | Delete
$posts = get_posts( array(
[225] Fix | Delete
'posts_per_page' => -1,
[226] Fix | Delete
'post_type' => 'any',
[227] Fix | Delete
'posts_status' => 'any',
[228] Fix | Delete
'meta_query' => array(
[229] Fix | Delete
array(
[230] Fix | Delete
'key' => '_tbp_demo_id',
[231] Fix | Delete
'compare' => 'EXISTS'
[232] Fix | Delete
),
[233] Fix | Delete
),
[234] Fix | Delete
'suppress_filters' => true,
[235] Fix | Delete
'fields' => 'ids',
[236] Fix | Delete
) );
[237] Fix | Delete
foreach ( $posts as $post_id ) {
[238] Fix | Delete
wp_delete_post( $post_id, true );
[239] Fix | Delete
}
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
/**
[243] Fix | Delete
* Recieves a post_id from sample file and returns the same post that has been imported into the site
[244] Fix | Delete
*
[245] Fix | Delete
* @return int|bool
[246] Fix | Delete
*/
[247] Fix | Delete
private static function get_post_by_old_id( $old_id ) {
[248] Fix | Delete
global $wpdb;
[249] Fix | Delete
$result = $wpdb->get_results( $wpdb->prepare(
[250] Fix | Delete
"SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_tbp_demo_id' AND meta_value = %d LIMIT 1",
[251] Fix | Delete
$old_id
[252] Fix | Delete
), ARRAY_A );
[253] Fix | Delete
[254] Fix | Delete
if ( isset( $result[0]['post_id'] ) ) {
[255] Fix | Delete
return $result[0]['post_id'];
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
return false;
[259] Fix | Delete
}
[260] Fix | Delete
[261] Fix | Delete
private static function get_term_by_old_id( $old_id ) {
[262] Fix | Delete
global $wpdb;
[263] Fix | Delete
$result = $wpdb->get_results( $wpdb->prepare(
[264] Fix | Delete
"SELECT post_id FROM {$wpdb->termmeta} WHERE meta_key = '_tbp_demo_id' AND meta_value = %d LIMIT 1",
[265] Fix | Delete
$old_id
[266] Fix | Delete
), ARRAY_A );
[267] Fix | Delete
[268] Fix | Delete
if ( isset( $result[0]['term_id'] ) ) {
[269] Fix | Delete
return $result[0]['term_id'];
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
return false;
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
/**
[276] Fix | Delete
* Download an image from external URL and returns the file
[277] Fix | Delete
*
[278] Fix | Delete
* @param $post_id Attachments may be associated with a parent post or page.
[279] Fix | Delete
*
[280] Fix | Delete
* @return WP_Error|int ID of created attachment, or WP_Error
[281] Fix | Delete
*/
[282] Fix | Delete
private static function fetch_remote_file( $url, $post_id = null ) {
[283] Fix | Delete
// extract the file name and extension from the url
[284] Fix | Delete
$file_name = basename( $url );
[285] Fix | Delete
[286] Fix | Delete
// get placeholder file in the upload dir with a unique, sanitized filename
[287] Fix | Delete
$upload = wp_upload_bits( $file_name, 0, '' );
[288] Fix | Delete
if ( $upload['error'] )
[289] Fix | Delete
return new WP_Error( 'upload_dir_error', $upload['error'] );
[290] Fix | Delete
[291] Fix | Delete
// fetch the remote url and write it to the placeholder file
[292] Fix | Delete
$remote_response = wp_safe_remote_get( $url, array(
[293] Fix | Delete
'timeout' => 300,
[294] Fix | Delete
'stream' => true,
[295] Fix | Delete
'filename' => $upload['file'],
[296] Fix | Delete
) );
[297] Fix | Delete
[298] Fix | Delete
$headers = wp_remote_retrieve_headers( $remote_response );
[299] Fix | Delete
[300] Fix | Delete
// request failed
[301] Fix | Delete
if ( ! $headers ) {
[302] Fix | Delete
@unlink( $upload['file'] );
[303] Fix | Delete
return new WP_Error( 'import_file_error', __('Remote server did not respond', 'tbp') );
[304] Fix | Delete
}
[305] Fix | Delete
[306] Fix | Delete
$remote_response_code = wp_remote_retrieve_response_code( $remote_response );
[307] Fix | Delete
[308] Fix | Delete
// make sure the fetch was successful
[309] Fix | Delete
if ( $remote_response_code != '200' ) {
[310] Fix | Delete
@unlink( $upload['file'] );
[311] Fix | Delete
return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'tbp'), esc_html( $remote_response_code ), get_status_header_desc( $remote_response_code ) ) );
[312] Fix | Delete
}
[313] Fix | Delete
[314] Fix | Delete
$filesize = filesize( $upload['file'] );
[315] Fix | Delete
[316] Fix | Delete
if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
[317] Fix | Delete
/* note: this is intentionally disabled, if gZip is enabled, $headers['content-length'] and $filesize do not necessarily match */
[318] Fix | Delete
}
[319] Fix | Delete
[320] Fix | Delete
if ( 0 == $filesize ) {
[321] Fix | Delete
@unlink( $upload['file'] );
[322] Fix | Delete
return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'tbp') );
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
$post = array(
[326] Fix | Delete
'post_title' => '',
[327] Fix | Delete
'post_content' => '',
[328] Fix | Delete
'post_status' => 'inherit',
[329] Fix | Delete
);
[330] Fix | Delete
if ( $info = wp_check_filetype( $upload['file'] ) )
[331] Fix | Delete
$post['post_mime_type'] = $info['type'];
[332] Fix | Delete
else
[333] Fix | Delete
return new WP_Error( 'mime_type_error', __('Invalid file type', 'tbp') );
[334] Fix | Delete
[335] Fix | Delete
$attach_id = wp_insert_attachment( $post, $upload['file'], $post_id );
[336] Fix | Delete
wp_update_attachment_metadata( $attach_id, wp_generate_attachment_metadata( $attach_id, $upload['file'] ) );
[337] Fix | Delete
[338] Fix | Delete
return $attach_id;
[339] Fix | Delete
}
[340] Fix | Delete
[341] Fix | Delete
public static function get_term_id_by_slug( $slug, $tax ) {
[342] Fix | Delete
$term = get_term_by( 'slug', $slug, $tax );
[343] Fix | Delete
if( $term ) {
[344] Fix | Delete
return $term->term_id;
[345] Fix | Delete
}
[346] Fix | Delete
[347] Fix | Delete
return false;
[348] Fix | Delete
}
[349] Fix | Delete
}
[350] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function