Enclavely / tailor

Build beautiful page layouts quickly and easily using your favourite WordPress theme.
https://www.tailorwp.com
GNU General Public License v3.0
1.05k stars 102 forks source link

Custom element disappeared after refreshing the preview #138

Open reefki opened 7 years ago

reefki commented 7 years ago

Everything was fine when adding my custom element into the canvas and hit the save button, I could see my custom element in the front end. However, if I refresh the preview window it gets disappeared while it's still visible on the front end and WordPress content editor. Here is what I see in the content editor:

screen shot 2017-08-13 at 23 06 33

This is happened after updating Tailor plugin from 1.7.4 to the latest version 1.8.0, I was never facing this problem on previously.

Below is my custom element codes:

if (! function_exists('my_custom_element_shortcode')) {
    function my_custom_element_shortcode($atts, $content = null, $tag) {
        $atts = shortcode_atts(array('text' => ''), $atts, $tag);

        $html_atts = tailor_get_attributes(array(
            'class' => array('tailor-element', 'tailor-custom-element', 'my-custom-element'),
        ));

        ob_start();

        tailor_partial('custom', 'element');

        $content = ob_get_clean();
        $wrapper = "<div {$html_atts}>%s</div>";

        return sprintf($wrapper, $content);
    }

    add_shortcode('my_custom_element', 'my_custom_element_shortcode');
}
if (! function_exists('register_custom_tailor_elements')) {
    function register_custom_tailor_elements($manager) {
        $manager->add_element('my_custom_element', array(
            'label'       => esc_html__('Custom Element', 'text-domain'),
            'description' => esc_html__('Element description', 'text-domain'),
            'type'        => 'content',
        ));
    }

    add_filter('tailor_register_elements', 'register_custom_tailor_elements');
}
if (class_exists('Tailor_Element') && ! class_exists('My_Custom_Element_Element'))
{
    class My_Custom_Element_Element extends Tailor_Element
    {
        protected function register_controls()
        {
            $this->add_section('general', array(
                'title'    => esc_html__('General', 'text-domain'),
                'priority' => 10,
            ));

            $this->add_setting('text', array(
                'sanitize_callback' => 'tailor_sanitize_text',
            ));

            $this->add_control('text', array(
                'section'  => 'general',
                'label'    => esc_html__('Text', 'text-domain'),
                'type'     => 'text',
                'priority' => 10,
            ));
        }

        public function generate_css($atts = array())
        {
             $css_rules = array();
         $excluded_settings = array();
             $css_rules = tailor_css_presets($css_rules, $atts, $excluded_settings);

         return $css_rules;
        }
    }
}
conwaydev commented 7 years ago

Same here, I proposed https://github.com/Enclavely/tailor/pull/139, but I don't know if thats how @Enclavelyio wants to go about it, but it fixed our scenario which seems like the same as yours

reefki commented 7 years ago

@conwaydev Nice! It seems to be worked for me, but let see what @Enclavelyio will do to fix it.

reefki commented 7 years ago

@conwaydev I don't think that it happened only to custom namespaced elements, I tried to install tailor official sample extension which has tailor_ prefix on all custom elements and yes, they also fail! Similar to mine, they rendered at first but disappeared after refreshing the window.

I think it's a critical bug and @Enclavelyio or @andrew-worsfold should take a look at this.

reefki commented 7 years ago

@conwaydev Just pulled a new changes #140 the problems is only on stripping out tailor_ prefix when creating the regex and append it when parsing post content.

conwaydev commented 7 years ago

Nice, closed mine, yours works for us!

reefki commented 7 years ago

Looks like the issue has been resolved in the new release.

reefki commented 7 years ago

Not really though, updated to the 1.8.1 custom elements still can't be extracted from post content, I don't know why my commit it's worked on 1.8.0, probably some other changes affecting to it. But after some digging, I found that generate_element_regex is fired at default priority that make it possible to be fired before custom elements are registered.

So I pull another commit #143 to make sure it's fired at the very end.

conwaydev commented 7 years ago

Yeah same for us thanks @reefki

mihaijoldis commented 6 years ago

Latest release and have the same issue with elements going missing after saving and reopening the editor. Found this issue and applied commit #143 and its now working again.

arashohadi commented 6 years ago

I can confirm the same problem. The commit in #143 fixes the problem.