Esempio n. 1
0
 function wp_cta_load_calls_to_action()
 {
     $calls_to_action = CTA_Render();
 }
        /**
         * How to display the widget on the screen.
         */
        function widget($args, $instance)
        {
            global $wp_query;
            global $post;
            if (!isset($instance['cta_ids'])) {
                return;
            }
            /* get enviroment object id if available */
            $obj_id = $wp_query->get_queried_object_id();
            $CTA_Load_Extensions = CTA_Load_Extensions();
            $this->cta_templates = $CTA_Load_Extensions->template_definitions;
            $CTA_Render = CTA_Render();
            $selected_ctas = $instance['cta_ids'];
            if (!is_array($selected_ctas)) {
                return;
            }
            $cta_ids = implode(",", $selected_ctas);
            $count = count($selected_ctas);
            $rand_key = array_rand($selected_ctas, 1);
            $cta_id = $selected_ctas[$rand_key];
            $this->cta_id = $cta_id;
            $selected_cta = $CTA_Render->prepare_cta_dataset(array($cta_id));
            if (!isset($selected_cta['templates'])) {
                return;
            }
            /* Import Correct CSS & JS from Assets folder and Enqueue */
            $loaded = array();
            foreach ($selected_cta['templates'] as $template) {
                if (in_array($template['slug'], $loaded)) {
                    continue;
                }
                $loaded[] = $template['slug'];
                $assets = $CTA_Render->get_assets($template);
                $localized_template_id = str_replace('-', '_', $template['slug']);
                foreach ($assets as $type => $file) {
                    switch ($type) {
                        case 'js':
                            foreach ($file as $js) {
                                wp_enqueue_script(md5($js), $js, array('jquery'));
                                wp_localize_script(md5($js), $localized_template_id, array('ajaxurl' => admin_url('admin-ajax.php'), 'post_id' => $obj_id));
                            }
                            break;
                        case 'css':
                            foreach ($file as $css) {
                                wp_enqueue_style(md5($css), $css);
                            }
                            break;
                    }
                }
            }
            /* Load CTA CSS Templates & Load Custom CSS & Custom JS */
            foreach ($selected_cta['meta'] as $vid => $cta) {
                $vid < 1 ? $suffix = '' : ($suffix = '-' . $vid);
                $template_slug = $selected_cta['meta'][$vid]['wp-cta-selected-template-' . $vid];
                $custom_css = get_post_meta($cta_id, 'wp-cta-custom-css' . $suffix, true);
                //echo $template_slug;
                //print_r($this->cta_templates);exit;
                $dynamic_css = $this->cta_templates[$template_slug]['css-template'];
                $dynamic_css = $CTA_Render->replace_template_variables($selected_cta, $dynamic_css, $vid);
                $css_id_preface = "#wp_cta_" . $cta_id . "_variation_" . $vid;
                $dynamic_css = $CTA_Render->parse_css_template($dynamic_css, $css_id_preface);
                $css_styleblock_class = apply_filters('wp_cta_styleblock_class', '', $cta_id, $vid);
                if (!stristr($custom_css, '<style')) {
                    $custom_css = strip_tags($custom_css);
                }
                /* Print Cusom CSS */
                echo '<style type="text/css" id="wp_cta_css_custom_' . $cta_id . '_' . $vid . '" class="wp_cta_css_' . $cta_id . ' ' . $css_styleblock_class . '">' . $custom_css . ' ' . $dynamic_css . '</style>';
                $custom_js = get_post_meta($cta_id, 'wp-cta-custom-js' . $suffix, true);
                if (!stristr($custom_css, '<script') && $custom_css) {
                    echo '<script type="text/javascript" id="wp_cta_js_custom">jQuery(document).ready(function($) {
					' . $custom_js . ' });</script>';
                } else {
                    if ($custom_js) {
                        echo $custom_js;
                    }
                }
            }
            /* get supporting widget settings */
            $selected_cta['margin-top'] = $instance['cta_margin_top'];
            $selected_cta['margin-bottom'] = $instance['cta_margin_bottom'];
            $cta_template = $CTA_Render->build_cta_content($selected_cta);
            $cta_template = do_shortcode($cta_template);
            echo $cta_template;
            $this->load_variation();
            //add_action('wp_footer', array($this,'load_variation'));
        }