: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* This file provide a class for Builder Import Export.
* a class to perform builder import/export operation
* @package Themify_Builder
* @subpackage Themify_Builder/classes
* The Builder Import Export class.
* This is used to provide a hook and ajax action to perform Import Export for Builder.
* @package Themify_Builder
* @subpackage Themify_Builder/classes
final class Themify_Builder_Import_Export {
public static function init() {
if (defined('DOING_AJAX')) {
add_action('wp_ajax_builder_import_submit', array(__CLASS__, 'builder_import_submit_ajaxify'), 10);
add_action('wp_ajax_builder_import', array(__CLASS__, 'builder_import_ajaxify'), 10);
add_action('wp_ajax_builder_prepare_export', array(__CLASS__, 'builder_export_ajaxify'), 10);
* Prepare Builder data for export
public static function prepare_builder_data($builder_data) {
if (!empty($builder_data) && is_array($builder_data)) {
foreach ($builder_data as &$row) {
if (isset($row['styling']) && !empty($row['styling']['background_slider'])) {
$row['styling']['background_slider'] = self::replace_with_image_path($row['styling']['background_slider']);
if (!empty($row['cols'])) {
foreach ($row['cols'] as &$col) {
if (isset($col['styling']) && !empty($col['styling']['background_slider'])) {
$col['styling']['background_slider'] = self::replace_with_image_path($col['styling']['background_slider']);
if (!empty($col['modules'])) {
foreach ($col['modules'] as &$mod) {
if (isset($mod['mod_name']) && $mod['mod_name'] === 'gallery' && !empty($mod['mod_settings']['shortcode_gallery'])) {
$mod['mod_settings']['shortcode_gallery'] = self::replace_with_image_path($mod['mod_settings']['shortcode_gallery']);
if (!empty($mod['cols'])) {
foreach ($mod['cols'] as &$sub_col) {
if (isset($sub_col['styling']) && !empty($sub_col['styling']['background_slider'])) {
$sub_col['styling']['background_slider'] = self::replace_with_image_path($sub_col['styling']['background_slider']);
if (!empty($sub_col['modules'])) {
foreach ($sub_col['modules'] as &$sub_module) {
if (isset($sub_module['mod_name']) && $sub_module['mod_name'] === 'gallery' && !empty($sub_module['mod_settings']['shortcode_gallery'])) {
$sub_module['mod_settings']['shortcode_gallery'] = self::replace_with_image_path($sub_module['mod_settings']['shortcode_gallery']);
public static function builder_export_ajaxify() {
check_ajax_referer('tf_nonce', 'nonce');
if (!empty($_POST['data']) && current_user_can( 'edit_posts' ) ) {
$shortcodes = json_decode(stripslashes_deep($_POST['data']), true);
foreach ($shortcodes as $k => $sh) {
$res[$k] = self::replace_with_image_path($sh);
public static function replace_export(array $builder_data, $post_id) {//deprecated will be removed
foreach ($builder_data as &$row) {
if (!empty($row['styling']['background_slider'])) {
$row['styling']['background_slider'] = self::replace_ids_image_path($row['styling']['background_slider'], $post_id);
if (!empty($row['cols'])) {
foreach ($row['cols'] as &$col) {
if (!empty($col['styling']['background_slider'])) {
$col['styling']['background_slider'] = self::replace_ids_image_path($col['styling']['background_slider'], $post_id);
if (!empty($col['modules'])) {
foreach ($col['modules'] as &$mod) {
if (isset($mod['mod_name']) && $mod['mod_name'] === 'gallery' && !empty($mod['mod_settings']['shortcode_gallery'])) {
$mod['mod_settings']['shortcode_gallery'] = self::replace_ids_image_path($mod['mod_settings']['shortcode_gallery'], $post_id);
if (!empty($mod['cols'])) {
foreach ($mod['cols'] as &$sub_col) {
if (!empty($sub_col['styling']['background_slider'])) {
$sub_col['styling']['background_slider'] = self::replace_ids_image_path($sub_col['styling']['background_slider'], $post_id);
if (!empty($sub_col['modules'])) {
foreach ($sub_col['modules'] as &$sub_module) {
if (isset($sub_module['mod_name']) && $sub_module['mod_name'] === 'gallery' && !empty($sub_module['mod_settings']['shortcode_gallery'])) {
$sub_module['mod_settings']['shortcode_gallery'] = self::replace_ids_image_path($sub_module['mod_settings']['shortcode_gallery'], $post_id);
* Replace shortcode gallery with image path
* @param string $shortcode
private static function replace_with_image_path(string $shortcode):string {
$images = themify_get_gallery_shortcode($shortcode);
preg_match('/\[gallery.*ids=.(.*).\]/im', $shortcode, $ids);
$ids = trim($ids[1], '\\');
foreach ($images as $img) {
$path[] = wp_get_attachment_image_url($img->ID, 'full');
$path = implode(',', $path);
$shortcode = str_replace('[gallery', '[gallery path="' . $path . '" ', $shortcode);
* Get attachment ID by URL.
private static function get_attachment_id_by_url(string $url) {
// Split the $url into two parts with the wp-content directory as the separator
$parsed_url = explode(parse_url(WP_CONTENT_URL, PHP_URL_PATH), $url);
// Get the host of the current site and the host of the $url, ignoring www
$this_host = str_ireplace('www.', '', parse_url(home_url(), PHP_URL_HOST));
$file_host = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
// Return nothing if there aren't any $url parts or if the current host and $url host do not match
if (empty($parsed_url[1]) || ( $this_host !== $file_host )) {
// Now we're going to quickly search the DB for any attachment GUID with a partial path match
// Example: /uploads/2013/05/test-image.jpg
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->prefix}posts WHERE post_type='attachment' AND guid RLIKE '%s' LIMIT 1", $parsed_url[1]));
return $attachment ? $attachment[0] : false;
* Replace image path if it doesn't exist and replace with the new ids
* @param string $shortcode
private static function replace_ids_image_path(string $shortcode, $post_id = false) {//deprecated will be removed
preg_match('/\[gallery.*path.*?=.*?[\'"](.+?)[\'"].*?\]/im', $shortcode, $path);
$path = trim($path[1], '\\');
$path = trim($path, '"');
$image_path = explode(',', $path);
if (!empty($image_path)) {
$attachment_id = array();
$wp_upload_dir = themify_upload_dir();
require_once( ABSPATH . 'wp-admin/includes/image.php' );
foreach ($image_path as $img) {
$img_id = self::get_attachment_id_by_url($img);
// extract the file name and extension from the url
$file_name = basename($img);
// get placeholder file in the upload dir with a unique, sanitized filename
$upload = wp_upload_bits($file_name, NULL, '');
// fetch the remote url and write it to the placeholder file
$response = $request->request($img, array('sslverify' => false));
// request failed and make sure the fetch was successful
if (!$response || is_wp_error($response) || wp_remote_retrieve_response_code($response) != '200') {
$access_type = get_filesystem_method();
if ($access_type === 'direct') {
$creds = request_filesystem_credentials(site_url() . '/wp-admin/', '', false, false, array());
if (!WP_Filesystem($creds)) {
$wp_filesystem->put_contents($upload['file'], wp_remote_retrieve_body($response));
$filetype = wp_check_filetype($file_name, null);
'guid' => $wp_upload_dir['url'] . '/' . $file_name,
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $file_name),
'post_status' => 'inherit'
$img_id = wp_insert_attachment($attachment, $upload['file'], 369);
$attach_data = wp_generate_attachment_metadata($img_id, $upload['file']);
wp_update_attachment_metadata($img_id, $attach_data);
$attachment_id[] = $img_id;
$shortcode = str_replace('path="' . $path . '"', '', $shortcode);
if (!empty($attachment_id)) {
$attachment_id = implode(',', $attachment_id);
preg_match('/\[gallery.*ids.*?=.*?[\'"](.+?)[\'"].*?\]/i', $shortcode, $ids);
$ids = trim($ids[1], '\\');
$shortcode = str_replace('ids="' . $ids . '"', 'ids="' . $attachment_id . '"', $shortcode);
* Builder Import Lightbox
public static function builder_import_ajaxify():void {
check_ajax_referer('tf_nonce', 'nonce');
if ( ! empty( $_POST['bid'] ) && current_user_can( 'edit_post', $_POST['bid'] ) ) {
$data = '<div class="tb_import_result">';
$page = ! empty( $_POST['page'] ) ? (int) $_POST['page'] : 1;
'post_type' => array_keys( Themify_Builder_Model::get_public_post_types() + [ 'page' => 0 ] ),
'post_status' => 'publish',
'posts_per_page' => $limit,
'ignore_sticky_posts' => true,
'cache_results' => false,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'lazy_load_term_meta' => false,
'key' => ThemifyBuilder_Data_Manager::META_KEY,
add_filter( 'posts_where', [ __CLASS__, 'posts_where' ] );
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$post_type_object = get_post_type_object( get_post_type() );
$data .= '<li><a href="#" data-id="' . get_the_ID() . '">' . get_the_title() . '<span>' . $post_type_object->labels->singular_name . '</span></a></li>';
$data .= '<div class="tb_pagination tf_textc">' . paginate_links( array(
'total' => ceil( $query->found_posts / $limit ),
* Search by post title only
* Used by builder_import_ajaxify()
public static function posts_where(string $where ):string {
$s = ! empty( $_POST['s'] ) ? sanitize_text_field( $_POST['s'] ) : null;
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $wpdb->esc_like( $s ) ) . '%\'';
public static function builder_import_submit_ajaxify() {
check_ajax_referer('tf_nonce', 'nonce');
if ( ! empty( $_POST['data'] ) && ! empty( $_POST['bid'] ) && current_user_can( 'edit_post', $_POST['bid'] ) ) {
$imports = json_decode(stripslashes($_POST['data']), true);
if (!empty($imports) && is_array($imports)) {
$builderData = $used_gs = array();
foreach ($imports as $post_id) {
$data = ThemifyBuilder_Data_Manager::get_data($post_id);
$used_gs = array_merge($used_gs, Themify_Global_Styles::used_global_styles($post_id));
$custom_css .= get_post_meta($post_id, 'tbp_custom_css', true);
if (!empty($builderData)) {
foreach ($builderData as $meta) {
$result = array_merge($result, (array) $meta);
$response['builder_data'] = $result;
$response['used_gs'] = array_keys(array_flip($used_gs));
if (!empty($custom_css)) {
$response['custom_css'] = $custom_css;