: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* This file defines Builder Library Items designs and parts.
* Themify_Builder_Row class register post type for Library Items designs and Parts
* Custom metabox, and load Library Items designs / parts.
* @package Themify_Builder
* @subpackage Themify_Builder/classes
* The Builder Library Items class.
* This class register post type for Library Items designs and Parts
* Custom metabox, and load Library Items designs / parts
* @package Themify_Builder
* @subpackage Themify_Builder/classes
class Themify_Builder_Library_Items {
private const POST_TYPE_NAMES = array('row' => 'library_rows', 'module' => 'library_modules', 'part' =>Themify_Builder_Layouts::LAYOUT_PART_SLUG);
public static function init() {
add_action('wp_ajax_tb_save_custom_item', array(__CLASS__, 'save_custom_item_ajaxify'));
add_action('wp_ajax_tb_get_library_items', array(__CLASS__, 'list_library_items_ajax'));
add_action('wp_ajax_tb_get_library_item', array(__CLASS__, 'get_item'));
add_action('wp_ajax_tb_remove_library_item', array(__CLASS__, 'remove_library_item_ajax'));
add_action('wp_ajax_tb_layout_part_swap', array(__CLASS__, 'layout_part_edit'));
public static function save_custom_item_ajaxify() {
check_ajax_referer('tf_nonce', 'nonce');
'msg' => __('Something went wrong', 'themify')
if (!empty($_POST['item']) &¤t_user_can( 'edit_posts' )) {
$is_layout_part = !empty($_POST['item_layout_save']) && $_POST['item_layout_save'] !== 'false';
'type' => $_POST['type'],
'item' => $is_layout_part ? json_decode(stripslashes_deep($_POST['item']), true) : $_POST['item']
$user = get_current_user_id();
if (!empty($_POST['item_title_field'])) {
$title = sanitize_text_field($_POST['item_title_field']);
$title = $is_layout_part ? __('Saved Item Layout Part', 'themify') : $user . ' Saved-' . ucwords(sanitize_text_field($data['type']));
$post_type = $is_layout_part ? self::POST_TYPE_NAMES['part'] : self::POST_TYPE_NAMES[$data['type']];
$new_id = wp_insert_post(array(
'post_status' => current_user_can( 'publish_posts' ) ? 'publish' : 'draft',
'post_type' => $post_type,
'post_content' => $is_layout_part ? '' : $data['item']
$response = self::save_as_layout_part($data, $new_id);
$response['status'] = 'success';
if (!empty($_POST['usedGS'])) {
update_post_meta($new_id, 'themify_used_global_styles', $_POST['usedGS']);
$response['post_type'] = $post_type;
$response['id'] = $new_id;
$response['post_title'] = $title;
* Save the item as Layout Part.
protected static function save_as_layout_part(array $data, $new_id) {
if ($data['type'] === 'module') {
$row = array('cols' => array(0 => array('grid_class' => 'col-full', 'modules' => array($data['item']))));
ThemifyBuilder_Data_Manager::save_data(array($row), $new_id);
ThemifyBuilder_Data_Manager::save_data(array($data['item']), $new_id);
$post = get_post($new_id);
'post_name' => $post->post_name
* Get layout part module settings.
protected static function get_layout_part_model($post, string $type):array {
'mod_name' => 'layout-part',
'selected_layout_part' => $post->post_name
$output = array('cols' => array(0 => array('grid_class' => 'col-full', 'modules' => array($output))));
* Get list of Saved Layout Parts in library.
protected static function get_list(string $type = 'all') {
$vals = $type === 'all' ? self::POST_TYPE_NAMES : array(self::POST_TYPE_NAMES[$type]);
$post_type[] = "'" . esc_sql($v) . "'";
$post_type = implode(',', $post_type);
return $wpdb->get_results("SELECT post_name,post_title,post_type,id FROM {$wpdb->posts} WHERE post_type IN(" . $post_type . ") and post_status = 'publish' ORDER BY post_title ASC ", ARRAY_A);
* Get list of Saved Rows & Modules in library.
public static function list_library_items_ajax() {
check_ajax_referer('tf_nonce', 'nonce');
$part = !empty($_POST['part']) ? $_POST['part'] : 'part';
if (($part !== 'all' && !isset(self::POST_TYPE_NAMES[$part])) || !isset($_POST['bid']) || !current_user_can( 'read_post',$_POST['bid'] )) {
wp_send_json(self::get_list($part));
public static function remove_library_item_ajax() {
check_ajax_referer('tf_nonce', 'nonce');
if (!empty( $_POST['id'] ) && current_user_can( 'delete_post', $_POST['id'] ) ) {
$id = (int) $_POST['id'];
if (in_array($post->post_type, self::POST_TYPE_NAMES, true)) {
$status = self::POST_TYPE_NAMES['part'] === $post->post_type ? wp_trash_post($id) : wp_delete_post($id);
$status = is_wp_error($status) ? 0 : $post->post_name;
public static function get_item() {
check_ajax_referer('tf_nonce', 'nonce');
$msg = array('status' => false);
if(!empty($_POST['id'])){
$id = (int) $_POST['id'];
if (in_array($post->post_type, self::POST_TYPE_NAMES, true) && current_user_can( 'read_post',$id )) {
$msg['status'] = 'success';
$msg['content'] = $post->post_type === self::POST_TYPE_NAMES['part'] ? self::get_layout_part_model($post, $_POST['type']) : json_decode($post->post_content, true);
if ($post->post_type !== self::POST_TYPE_NAMES['part']) {
$usedGS = Themify_Global_Styles::used_global_styles($id);
$msg['content']['gs'] = $usedGS;
public static function layout_part_edit() {
check_ajax_referer('tf_nonce', 'nonce');
if ( ! empty( $_POST['bid'] ) && current_user_can( 'edit_post', $_POST['bid'] ) ) {
$id = (int) $_POST['bid'];
$response['builder_data'] = ThemifyBuilder_Data_Manager::get_data($id);
// Return used gs if used
$usedGS = Themify_Global_Styles::used_global_styles($id);
$response['used_gs'] = $usedGS;
$custom_css = get_post_meta($id, 'tbp_custom_css', true);
if (!empty($custom_css)) {
$response['custom_css'] = $custom_css;
echo wp_json_encode($response);