/**
  * Shortcode render.
  * 
  * @since 1.0.0
  * @access public
  * @return json
  */
 public function shortcode_render()
 {
     // Check ajax referer
     check_ajax_referer($this->action_nonce, $this->field_nonce);
     $type = isset($_POST['type']) ? $_POST['type'] : '';
     $method = isset($_POST['method']) ? $_POST['method'] : 'add';
     $mode = isset($_POST['mode']) ? $_POST['mode'] : 'frontend';
     $data = array();
     switch ($method) {
         case 'duplicate':
             global $TF_Layout;
             if ('module' == $type) {
                 global $tf_modules, $tf_styles;
                 $module_instance = $tf_modules->get_module($_POST['module']);
                 $atts = isset($_POST['shortcode_params']) ? $_POST['shortcode_params'] : '';
                 $content = isset($_POST['shortcode_content']) ? wp_kses_stripslashes($_POST['shortcode_content']) : '';
                 $template_id = (int) $_POST['template_id'];
                 $data_styling = isset($_POST['data_styling']) ? json_decode(stripslashes($_POST['data_styling']), true) : array();
                 $styles = array();
                 $atts['sc_id'] = TF_Model::generate_block_id();
                 // generate new sc_id
                 if (is_array($data_styling) && count($data_styling) > 0) {
                     $data_styling['ID'] = $atts['sc_id'];
                     $styles[$atts['sc_id']] = array('module' => $data_styling['module']);
                     if (isset($data_styling['settings']) && count($data_styling['settings']) > 0) {
                         foreach ($data_styling['settings'] as $fields) {
                             $setting_key = '';
                             foreach ($fields as $key => $val) {
                                 if ('SettingKey' == $key) {
                                     $setting_key = $val;
                                 } else {
                                     $styles[$atts['sc_id']]['settings'][$setting_key][$key] = stripslashes_deep($val);
                                 }
                             }
                         }
                     }
                 }
                 $render_style = '';
                 if (count($styles) > 0) {
                     $render_style = '<style type="text/css" id="tf-template-temp-' . $atts['sc_id'] . '-css">' . $tf_styles->generate_css($styles) . '</style>';
                 }
                 if (isset($atts['editable_markup'])) {
                     unset($atts['editable_markup']);
                 }
                 if (get_magic_quotes_gpc()) {
                     $atts = stripslashes_deep($atts);
                 }
                 $shortcode_string = $module_instance->to_shortcode($atts, $content);
                 global $post;
                 $post = get_post($template_id);
                 setup_postdata($post);
                 $shortcode = $TF_Layout->render($shortcode_string);
                 $data = array('module' => sanitize_text_field($_POST['module']), 'content' => tf_escape_atts($content), 'atts' => $atts, 'caption' => $module_instance->name, 'element' => $shortcode, 'styles' => $render_style, 'model' => $data_styling);
             } else {
                 if ('row' == $type) {
                     global $tf_editor_ui;
                     $row_data = isset($_POST['row_data']) ? stripslashes_deep($_POST['row_data']) : array();
                     tf_recursive_unset($row_data, 'sc_id');
                     tf_recursive_unset($row_data, 'editable_markup');
                     $shortcode = TF_Model::array_to_shortcode(array($row_data));
                     $tf_editor_ui->force_editable_shortcode($mode);
                     $data = $TF_Layout->render($shortcode);
                 }
             }
             break;
     }
     wp_send_json_success($data);
 }
 public function shortcode_handler($atts, $content, $code)
 {
     global $TF;
     if ($TF->in_template_part && !TF_Model::is_template_page()) {
         if (is_array($this->category)) {
             $cat = array_intersect($this->category, array('archive', 'page', 'single'));
             if (!empty($cat)) {
                 $cat = current($cat);
             }
         } else {
             $cat = $this->category;
         }
         if (in_array($cat, array('archive', 'page', 'single')) && !is_admin()) {
             if ($cat == 'page' && !is_page()) {
                 return FALSE;
             } elseif ($cat == 'single' && !is_single()) {
                 return FALSE;
             } elseif ($cat == 'archive' && !is_search() && !is_tax() && !is_archive() && !is_post_type_archive()) {
                 return FALSE;
             }
         }
     }
     $atts = apply_filters('tf_shortcode_atts', $atts, $code, $this);
     /**
      * Put module wrapper markup here directly instead using apply_filters()
      * since this wrapper markup is required for each modules.
      */
     if (!isset($atts['sc_id'])) {
         $atts['sc_id'] = TF_Model::generate_block_id();
     }
     $classes = apply_filters('tf_module_classes', array('tf_module_wrapper', 'tf_module_block', 'tf_module_' . $this->slug, 'tf_module_block_' . $atts['sc_id']), $atts);
     $wrapper_atts = apply_filters('tf_module_wrapper_atts', array('class' => $classes), $atts, $content, $this);
     $wrapper_atts['class'] = implode(' ', $wrapper_atts['class']);
     $atts_output = '';
     foreach ($wrapper_atts as $key => $value) {
         $atts_output .= sprintf(' %s="%s"', $key, esc_attr($value));
     }
     $pre = sprintf('<div %s>', $atts_output);
     $after = '</div>';
     $output = $pre . $this->render_shortcode($atts, $content) . $after;
     return apply_filters('tf_shortcode_module_render', $output, $this->slug, $atts, $content);
 }
 public function shortcode_atts_generate_uid($out)
 {
     if (!isset($out['sc_id'])) {
         $out['sc_id'] = TF_Model::generate_block_id();
     }
     return $out;
 }
/**
 * Replace builder content sc_id with new unique shortcode id.
 * 
 * @since 1.0.0
 * @param string $content 
 * @return string
 */
function tf_generate_new_shortcode_ids($content)
{
    $unique_ids = tf_get_shortcode_ids($content);
    if (count($unique_ids) > 0) {
        foreach ($unique_ids as $id) {
            $new_id = TF_Model::generate_block_id();
            $content = str_replace($id, $new_id, $content);
            usleep(300);
            //because php will not have time to generate new id and will return the same id
        }
    }
    return $content;
}
 /**
  * Fires to render row option form.
  * 
  * @since 1.0.0
  * @access public
  * @param string $method Form state method (add|edit|delete)
  */
 public function render_row_option_form($method)
 {
     $label_submit_btn = 'add' == $method ? __('Add', 'themify-flow') : __('Update', 'themify-flow');
     $data = array();
     $shortcode = isset($_POST['shortcode']) ? $_POST['shortcode'] : '';
     $atts = isset($_POST['shortcode_params']) ? $_POST['shortcode_params'] : array();
     $shortcode_id = isset($atts['sc_id']) ? $atts['sc_id'] : TF_Model::generate_block_id();
     $template_id = isset($_POST['template_id']) ? $_POST['template_id'] : 0;
     $mode = isset($_POST['mode']) ? $_POST['mode'] : 'frontend';
     // proses data here
     if ('edit' == $method) {
         $data = stripslashes_deep($atts);
     }
     echo TF_Form::open(array('id' => 'tf_row_option_form'));
     echo sprintf('<input type="hidden" name="_state" value="%s">', $method);
     echo sprintf('<input type="hidden" name="_shortcode_name" value="%s">', $shortcode);
     echo sprintf('<input type="hidden" name="_template_id" value="%d">', $template_id);
     echo sprintf('<input type="hidden" name="sc_id" value="%s">', $shortcode_id);
     echo sprintf('<input type="hidden" name="_mode" value="%s">', $mode);
     echo TF_Form::render(TF_Shortcodes::row_fields(), $data);
     echo TF_Form::submit_button($label_submit_btn);
     echo TF_Form::close();
 }