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/themify-.../classes
File: class-themify-builder-import-export.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* This file provide a class for Builder Import Export.
[3] Fix | Delete
*
[4] Fix | Delete
* a class to perform builder import/export operation
[5] Fix | Delete
*
[6] Fix | Delete
*
[7] Fix | Delete
* @package Themify_Builder
[8] Fix | Delete
* @subpackage Themify_Builder/classes
[9] Fix | Delete
*/
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* The Builder Import Export class.
[13] Fix | Delete
*
[14] Fix | Delete
* This is used to provide a hook and ajax action to perform Import Export for Builder.
[15] Fix | Delete
*
[16] Fix | Delete
*
[17] Fix | Delete
* @package Themify_Builder
[18] Fix | Delete
* @subpackage Themify_Builder/classes
[19] Fix | Delete
* @author Themify
[20] Fix | Delete
*/
[21] Fix | Delete
final class Themify_Builder_Import_Export {
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Class constructor.
[25] Fix | Delete
*
[26] Fix | Delete
* @access public
[27] Fix | Delete
*/
[28] Fix | Delete
public static function init() {
[29] Fix | Delete
if (defined('DOING_AJAX')) {
[30] Fix | Delete
add_action('wp_ajax_builder_import_submit', array(__CLASS__, 'builder_import_submit_ajaxify'), 10);
[31] Fix | Delete
add_action('wp_ajax_builder_import', array(__CLASS__, 'builder_import_ajaxify'), 10);
[32] Fix | Delete
add_action('wp_ajax_builder_prepare_export', array(__CLASS__, 'builder_export_ajaxify'), 10);
[33] Fix | Delete
}
[34] Fix | Delete
}
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* Prepare Builder data for export
[38] Fix | Delete
*
[39] Fix | Delete
* @access public
[40] Fix | Delete
*/
[41] Fix | Delete
public static function prepare_builder_data($builder_data) {
[42] Fix | Delete
if (!empty($builder_data) && is_array($builder_data)) {
[43] Fix | Delete
foreach ($builder_data as &$row) {
[44] Fix | Delete
if (isset($row['styling']) && !empty($row['styling']['background_slider'])) {
[45] Fix | Delete
$row['styling']['background_slider'] = self::replace_with_image_path($row['styling']['background_slider']);
[46] Fix | Delete
}
[47] Fix | Delete
if (!empty($row['cols'])) {
[48] Fix | Delete
foreach ($row['cols'] as &$col) {
[49] Fix | Delete
if (isset($col['styling']) && !empty($col['styling']['background_slider'])) {
[50] Fix | Delete
$col['styling']['background_slider'] = self::replace_with_image_path($col['styling']['background_slider']);
[51] Fix | Delete
}
[52] Fix | Delete
if (!empty($col['modules'])) {
[53] Fix | Delete
foreach ($col['modules'] as &$mod) {
[54] Fix | Delete
if (isset($mod['mod_name']) && $mod['mod_name'] === 'gallery' && !empty($mod['mod_settings']['shortcode_gallery'])) {
[55] Fix | Delete
$mod['mod_settings']['shortcode_gallery'] = self::replace_with_image_path($mod['mod_settings']['shortcode_gallery']);
[56] Fix | Delete
}
[57] Fix | Delete
// Check for Sub-rows
[58] Fix | Delete
if (!empty($mod['cols'])) {
[59] Fix | Delete
foreach ($mod['cols'] as &$sub_col) {
[60] Fix | Delete
if (isset($sub_col['styling']) && !empty($sub_col['styling']['background_slider'])) {
[61] Fix | Delete
$sub_col['styling']['background_slider'] = self::replace_with_image_path($sub_col['styling']['background_slider']);
[62] Fix | Delete
}
[63] Fix | Delete
if (!empty($sub_col['modules'])) {
[64] Fix | Delete
foreach ($sub_col['modules'] as &$sub_module) {
[65] Fix | Delete
if (isset($sub_module['mod_name']) && $sub_module['mod_name'] === 'gallery' && !empty($sub_module['mod_settings']['shortcode_gallery'])) {
[66] Fix | Delete
$sub_module['mod_settings']['shortcode_gallery'] = self::replace_with_image_path($sub_module['mod_settings']['shortcode_gallery']);
[67] Fix | Delete
}
[68] Fix | Delete
}
[69] Fix | Delete
}
[70] Fix | Delete
}
[71] Fix | Delete
}
[72] Fix | Delete
}
[73] Fix | Delete
}
[74] Fix | Delete
}
[75] Fix | Delete
}
[76] Fix | Delete
}
[77] Fix | Delete
}
[78] Fix | Delete
return $builder_data;
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* Perform export file.
[83] Fix | Delete
*
[84] Fix | Delete
* @access public
[85] Fix | Delete
*/
[86] Fix | Delete
public static function builder_export_ajaxify() {
[87] Fix | Delete
check_ajax_referer('tf_nonce', 'nonce');
[88] Fix | Delete
if (!empty($_POST['data']) && current_user_can( 'edit_posts' ) ) {
[89] Fix | Delete
$shortcodes = json_decode(stripslashes_deep($_POST['data']), true);
[90] Fix | Delete
$res = array();
[91] Fix | Delete
foreach ($shortcodes as $k => $sh) {
[92] Fix | Delete
$res[$k] = self::replace_with_image_path($sh);
[93] Fix | Delete
}
[94] Fix | Delete
wp_send_json($res);
[95] Fix | Delete
}
[96] Fix | Delete
wp_die();
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
public static function replace_export(array $builder_data, $post_id) {//deprecated will be removed
[100] Fix | Delete
foreach ($builder_data as &$row) {
[101] Fix | Delete
if (!empty($row['styling']['background_slider'])) {
[102] Fix | Delete
$row['styling']['background_slider'] = self::replace_ids_image_path($row['styling']['background_slider'], $post_id);
[103] Fix | Delete
}
[104] Fix | Delete
if (!empty($row['cols'])) {
[105] Fix | Delete
foreach ($row['cols'] as &$col) {
[106] Fix | Delete
if (!empty($col['styling']['background_slider'])) {
[107] Fix | Delete
$col['styling']['background_slider'] = self::replace_ids_image_path($col['styling']['background_slider'], $post_id);
[108] Fix | Delete
}
[109] Fix | Delete
if (!empty($col['modules'])) {
[110] Fix | Delete
foreach ($col['modules'] as &$mod) {
[111] Fix | Delete
if (isset($mod['mod_name']) && $mod['mod_name'] === 'gallery' && !empty($mod['mod_settings']['shortcode_gallery'])) {
[112] Fix | Delete
$mod['mod_settings']['shortcode_gallery'] = self::replace_ids_image_path($mod['mod_settings']['shortcode_gallery'], $post_id);
[113] Fix | Delete
}
[114] Fix | Delete
// Check for Sub-rows
[115] Fix | Delete
if (!empty($mod['cols'])) {
[116] Fix | Delete
foreach ($mod['cols'] as &$sub_col) {
[117] Fix | Delete
if (!empty($sub_col['styling']['background_slider'])) {
[118] Fix | Delete
$sub_col['styling']['background_slider'] = self::replace_ids_image_path($sub_col['styling']['background_slider'], $post_id);
[119] Fix | Delete
}
[120] Fix | Delete
if (!empty($sub_col['modules'])) {
[121] Fix | Delete
foreach ($sub_col['modules'] as &$sub_module) {
[122] Fix | Delete
if (isset($sub_module['mod_name']) && $sub_module['mod_name'] === 'gallery' && !empty($sub_module['mod_settings']['shortcode_gallery'])) {
[123] Fix | Delete
$sub_module['mod_settings']['shortcode_gallery'] = self::replace_ids_image_path($sub_module['mod_settings']['shortcode_gallery'], $post_id);
[124] Fix | Delete
}
[125] Fix | Delete
}
[126] Fix | Delete
}
[127] Fix | Delete
}
[128] Fix | Delete
}
[129] Fix | Delete
}
[130] Fix | Delete
}
[131] Fix | Delete
}
[132] Fix | Delete
}
[133] Fix | Delete
}
[134] Fix | Delete
return $builder_data;
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Replace shortcode gallery with image path
[139] Fix | Delete
*
[140] Fix | Delete
* @access public
[141] Fix | Delete
* @param string $shortcode
[142] Fix | Delete
* @return string
[143] Fix | Delete
*/
[144] Fix | Delete
private static function replace_with_image_path(string $shortcode):string {
[145] Fix | Delete
$images = themify_get_gallery_shortcode($shortcode);
[146] Fix | Delete
if (!empty($images)) {
[147] Fix | Delete
preg_match('/\[gallery.*ids=.(.*).\]/im', $shortcode, $ids);
[148] Fix | Delete
$ids = trim($ids[1], '\\');
[149] Fix | Delete
$ids = trim($ids, '"');
[150] Fix | Delete
$path = array();
[151] Fix | Delete
foreach ($images as $img) {
[152] Fix | Delete
$path[] = wp_get_attachment_image_url($img->ID, 'full');
[153] Fix | Delete
}
[154] Fix | Delete
if (!empty($path)) {
[155] Fix | Delete
$path = implode(',', $path);
[156] Fix | Delete
$shortcode = str_replace('[gallery', '[gallery path="' . $path . '" ', $shortcode);
[157] Fix | Delete
}
[158] Fix | Delete
}
[159] Fix | Delete
return $shortcode;
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
/**
[163] Fix | Delete
* Get attachment ID by URL.
[164] Fix | Delete
*
[165] Fix | Delete
* @access public
[166] Fix | Delete
* @param string $url
[167] Fix | Delete
* @return string
[168] Fix | Delete
*/
[169] Fix | Delete
private static function get_attachment_id_by_url(string $url) {
[170] Fix | Delete
// Split the $url into two parts with the wp-content directory as the separator
[171] Fix | Delete
$parsed_url = explode(parse_url(WP_CONTENT_URL, PHP_URL_PATH), $url);
[172] Fix | Delete
// Get the host of the current site and the host of the $url, ignoring www
[173] Fix | Delete
$this_host = str_ireplace('www.', '', parse_url(home_url(), PHP_URL_HOST));
[174] Fix | Delete
$file_host = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
[175] Fix | Delete
// Return nothing if there aren't any $url parts or if the current host and $url host do not match
[176] Fix | Delete
[177] Fix | Delete
if (empty($parsed_url[1]) || ( $this_host !== $file_host )) {
[178] Fix | Delete
return false;
[179] Fix | Delete
}
[180] Fix | Delete
// Now we're going to quickly search the DB for any attachment GUID with a partial path match
[181] Fix | Delete
// Example: /uploads/2013/05/test-image.jpg
[182] Fix | Delete
global $wpdb;
[183] Fix | Delete
$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]));
[184] Fix | Delete
return $attachment ? $attachment[0] : false;
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* Replace image path if it doesn't exist and replace with the new ids
[189] Fix | Delete
*
[190] Fix | Delete
* @access public
[191] Fix | Delete
* @param string $shortcode
[192] Fix | Delete
* @param int $post_id
[193] Fix | Delete
* @return string
[194] Fix | Delete
*/
[195] Fix | Delete
private static function replace_ids_image_path(string $shortcode, $post_id = false) {//deprecated will be removed
[196] Fix | Delete
preg_match('/\[gallery.*path.*?=.*?[\'"](.+?)[\'"].*?\]/im', $shortcode, $path);
[197] Fix | Delete
if (!empty($path[1])) {
[198] Fix | Delete
$path = trim($path[1], '\\');
[199] Fix | Delete
$path = trim($path, '"');
[200] Fix | Delete
$image_path = explode(',', $path);
[201] Fix | Delete
if (!empty($image_path)) {
[202] Fix | Delete
$attachment_id = array();
[203] Fix | Delete
$wp_upload_dir = themify_upload_dir();
[204] Fix | Delete
require_once( ABSPATH . 'wp-admin/includes/image.php' );
[205] Fix | Delete
foreach ($image_path as $img) {
[206] Fix | Delete
[207] Fix | Delete
$img_id = self::get_attachment_id_by_url($img);
[208] Fix | Delete
if (!$img_id) {
[209] Fix | Delete
// extract the file name and extension from the url
[210] Fix | Delete
$file_name = basename($img);
[211] Fix | Delete
[212] Fix | Delete
// get placeholder file in the upload dir with a unique, sanitized filename
[213] Fix | Delete
$upload = wp_upload_bits($file_name, NULL, '');
[214] Fix | Delete
if ($upload['error']) {
[215] Fix | Delete
continue;
[216] Fix | Delete
}
[217] Fix | Delete
// fetch the remote url and write it to the placeholder file
[218] Fix | Delete
$request = new WP_Http;
[219] Fix | Delete
$response = $request->request($img, array('sslverify' => false));
[220] Fix | Delete
[221] Fix | Delete
// request failed and make sure the fetch was successful
[222] Fix | Delete
if (!$response || is_wp_error($response) || wp_remote_retrieve_response_code($response) != '200') {
[223] Fix | Delete
continue;
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
$access_type = get_filesystem_method();
[227] Fix | Delete
[228] Fix | Delete
if ($access_type === 'direct') {
[229] Fix | Delete
$creds = request_filesystem_credentials(site_url() . '/wp-admin/', '', false, false, array());
[230] Fix | Delete
[231] Fix | Delete
if (!WP_Filesystem($creds)) {
[232] Fix | Delete
continue;
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
global $wp_filesystem;
[236] Fix | Delete
$wp_filesystem->put_contents($upload['file'], wp_remote_retrieve_body($response));
[237] Fix | Delete
} else {
[238] Fix | Delete
continue;
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
clearstatcache();
[242] Fix | Delete
$filetype = wp_check_filetype($file_name, null);
[243] Fix | Delete
$attachment = array(
[244] Fix | Delete
'guid' => $wp_upload_dir['url'] . '/' . $file_name,
[245] Fix | Delete
'post_mime_type' => $filetype['type'],
[246] Fix | Delete
'post_title' => preg_replace('/\.[^.]+$/', '', $file_name),
[247] Fix | Delete
'post_content' => '',
[248] Fix | Delete
'post_status' => 'inherit'
[249] Fix | Delete
);
[250] Fix | Delete
[251] Fix | Delete
$img_id = wp_insert_attachment($attachment, $upload['file'], 369);
[252] Fix | Delete
if ($img_id) {
[253] Fix | Delete
$attach_data = wp_generate_attachment_metadata($img_id, $upload['file']);
[254] Fix | Delete
wp_update_attachment_metadata($img_id, $attach_data);
[255] Fix | Delete
}
[256] Fix | Delete
}
[257] Fix | Delete
if ($img_id) {
[258] Fix | Delete
$attachment_id[] = $img_id;
[259] Fix | Delete
}
[260] Fix | Delete
}
[261] Fix | Delete
}
[262] Fix | Delete
$shortcode = str_replace('path="' . $path . '"', '', $shortcode);
[263] Fix | Delete
if (!empty($attachment_id)) {
[264] Fix | Delete
$attachment_id = implode(',', $attachment_id);
[265] Fix | Delete
preg_match('/\[gallery.*ids.*?=.*?[\'"](.+?)[\'"].*?\]/i', $shortcode, $ids);
[266] Fix | Delete
$ids = trim($ids[1], '\\');
[267] Fix | Delete
$ids = trim($ids, '"');
[268] Fix | Delete
$shortcode = str_replace('ids="' . $ids . '"', 'ids="' . $attachment_id . '"', $shortcode);
[269] Fix | Delete
}
[270] Fix | Delete
}
[271] Fix | Delete
return $shortcode;
[272] Fix | Delete
}
[273] Fix | Delete
[274] Fix | Delete
/**
[275] Fix | Delete
* Builder Import Lightbox
[276] Fix | Delete
*/
[277] Fix | Delete
public static function builder_import_ajaxify():void {
[278] Fix | Delete
check_ajax_referer('tf_nonce', 'nonce');
[279] Fix | Delete
if ( ! empty( $_POST['bid'] ) && current_user_can( 'edit_post', $_POST['bid'] ) ) {
[280] Fix | Delete
$data = '<div class="tb_import_result">';
[281] Fix | Delete
$page = ! empty( $_POST['page'] ) ? (int) $_POST['page'] : 1;
[282] Fix | Delete
$limit = 20;
[283] Fix | Delete
$args = array(
[284] Fix | Delete
'fields' => 'ids',
[285] Fix | Delete
'post_type' => array_keys( Themify_Builder_Model::get_public_post_types() + [ 'page' => 0 ] ),
[286] Fix | Delete
'post_status' => 'publish',
[287] Fix | Delete
'posts_per_page' => $limit,
[288] Fix | Delete
'paged' => $page,
[289] Fix | Delete
'ignore_sticky_posts' => true,
[290] Fix | Delete
'cache_results' => false,
[291] Fix | Delete
'update_post_meta_cache' => false,
[292] Fix | Delete
'update_post_term_cache' => false,
[293] Fix | Delete
'lazy_load_term_meta' => false,
[294] Fix | Delete
'orderby' => 'none',
[295] Fix | Delete
'order' => 'ASC',
[296] Fix | Delete
'meta_query' => array(
[297] Fix | Delete
array(
[298] Fix | Delete
'key' => ThemifyBuilder_Data_Manager::META_KEY,
[299] Fix | Delete
'value' => '',
[300] Fix | Delete
'compare' => '!='
[301] Fix | Delete
)
[302] Fix | Delete
)
[303] Fix | Delete
);
[304] Fix | Delete
add_filter( 'posts_where', [ __CLASS__, 'posts_where' ] );
[305] Fix | Delete
$query = new WP_Query( $args );
[306] Fix | Delete
if ( $query->have_posts() ) {
[307] Fix | Delete
$data .= '<ul>';
[308] Fix | Delete
while ( $query->have_posts() ) {
[309] Fix | Delete
$query->the_post();
[310] Fix | Delete
$post_type_object = get_post_type_object( get_post_type() );
[311] Fix | Delete
$data .= '<li><a href="#" data-id="' . get_the_ID() . '">' . get_the_title() . '<span>' . $post_type_object->labels->singular_name . '</span></a></li>';
[312] Fix | Delete
}
[313] Fix | Delete
$data .= '</ul>';
[314] Fix | Delete
$data .= '<div class="tb_pagination tf_textc">' . paginate_links( array(
[315] Fix | Delete
'base' => '',
[316] Fix | Delete
'format' => '?',
[317] Fix | Delete
'total' => ceil( $query->found_posts / $limit ),
[318] Fix | Delete
'current' => $page,
[319] Fix | Delete
'prev_next' => false
[320] Fix | Delete
) ) . '</div>';
[321] Fix | Delete
}
[322] Fix | Delete
$data .= '</div>';
[323] Fix | Delete
[324] Fix | Delete
die($data);
[325] Fix | Delete
}
[326] Fix | Delete
wp_die();
[327] Fix | Delete
}
[328] Fix | Delete
[329] Fix | Delete
/**
[330] Fix | Delete
* Search by post title only
[331] Fix | Delete
* Used by builder_import_ajaxify()
[332] Fix | Delete
*/
[333] Fix | Delete
public static function posts_where(string $where ):string {
[334] Fix | Delete
$s = ! empty( $_POST['s'] ) ? sanitize_text_field( $_POST['s'] ) : null;
[335] Fix | Delete
if ( $s ) {
[336] Fix | Delete
global $wpdb;
[337] Fix | Delete
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $wpdb->esc_like( $s ) ) . '%\'';
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
return $where;
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
/**
[344] Fix | Delete
* Process import builder
[345] Fix | Delete
*/
[346] Fix | Delete
public static function builder_import_submit_ajaxify() {
[347] Fix | Delete
check_ajax_referer('tf_nonce', 'nonce');
[348] Fix | Delete
$response = array();
[349] Fix | Delete
if ( ! empty( $_POST['data'] ) && ! empty( $_POST['bid'] ) && current_user_can( 'edit_post', $_POST['bid'] ) ) {
[350] Fix | Delete
$imports = json_decode(stripslashes($_POST['data']), true);
[351] Fix | Delete
if (!empty($imports) && is_array($imports)) {
[352] Fix | Delete
$custom_css = '';
[353] Fix | Delete
$builderData = $used_gs = array();
[354] Fix | Delete
[355] Fix | Delete
foreach ($imports as $post_id) {
[356] Fix | Delete
if (!empty($post_id)) {
[357] Fix | Delete
$data = ThemifyBuilder_Data_Manager::get_data($post_id);
[358] Fix | Delete
if (!empty($data)) {
[359] Fix | Delete
$builderData[] = $data;
[360] Fix | Delete
// Used GS
[361] Fix | Delete
$used_gs = array_merge($used_gs, Themify_Global_Styles::used_global_styles($post_id));
[362] Fix | Delete
$custom_css .= get_post_meta($post_id, 'tbp_custom_css', true);
[363] Fix | Delete
}
[364] Fix | Delete
}
[365] Fix | Delete
}
[366] Fix | Delete
if (!empty($builderData)) {
[367] Fix | Delete
$result = array();
[368] Fix | Delete
foreach ($builderData as $meta) {
[369] Fix | Delete
$result = array_merge($result, (array) $meta);
[370] Fix | Delete
}
[371] Fix | Delete
$response['builder_data'] = $result;
[372] Fix | Delete
if (!empty($used_gs)) {
[373] Fix | Delete
$response['used_gs'] = array_keys(array_flip($used_gs));
[374] Fix | Delete
}
[375] Fix | Delete
if (!empty($custom_css)) {
[376] Fix | Delete
$response['custom_css'] = $custom_css;
[377] Fix | Delete
}
[378] Fix | Delete
}
[379] Fix | Delete
}
[380] Fix | Delete
}
[381] Fix | Delete
wp_send_json($response);
[382] Fix | Delete
}
[383] Fix | Delete
}
[384] Fix | Delete
[385] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function