<?php
/*
    Plugin Name: Visual Composer Sections
    Plugin URI: http://workingwithpixels.com/
    Description: Reuse sections you create on any Visual Composer page, post type or in any WordPress sidebar using a special widget.
    Version: 1.3
    Author: WWP
    Author URI: http://www.workingwithpixels.com/
    Copyright: WWP, 2016
*/

if (!defined('ABSPATH')) die('-1');

if(!class_exists('WWP_VC_SECTIONS'))
{
    class WWP_VC_SECTIONS
    {
        function defines()
        {
            defined('wwp_vc_sections_name')  ||  define('wwp_vc_sections_name', 'WWP');
            defined('wwp_vc_sections_dir')  ||  define('wwp_vc_sections_dir', plugin_dir_path( __FILE__ ));
            defined('wwp_vc_sections_inc')  ||  define('wwp_vc_sections_inc', wwp_vc_sections_dir . 'include/');
        }

        function __construct()
        {
            $this->defines();

            function wwp_vc_sections_init_admin_css()
            {
                wp_enqueue_style('wwp-vc-sections-admin', plugins_url( 'include/css/wwp-vc-sections-admin.css', __FILE__ ));
            }
            add_action('admin_enqueue_scripts', 'wwp_vc_sections_init_admin_css');
            
            require_once(wwp_vc_sections_inc . 'core/wwp-vc-sections-cpt.php');

            add_action('init', 'create_wwp_vc_sections');
            add_shortcode('wwp_vc_sections', 'wwp_vc_sections');

            function getSections()
            {
                $list_sections = array();

                $sections = get_posts( array(
                    "post_type" => "wwp_vc_sections",
                    "post_status" => "publish",
                    "posts_per_page" => -1
                ) );

                foreach($sections as $single_section)
                {
                    $list_sections[$single_section->post_title] = $single_section->ID;
                }

                return $list_sections;
            }

            function create_wwp_vc_sections()
            {
                if(!function_exists('vc_map'))
                {
                    return;
                }

                vc_map(array(
                    "name" => "VC Sections",
                    "base" => "wwp_vc_sections",
                    "content_element" => true,
                    "icon" => "wwp_vc_sections",
                    "show_settings_on_create" => true,
                    "description" => "Easily reuse Visual Composer sections",
                    "category" => wwp_vc_sections_name,
                    "params" => array(
                        array(
                            "heading" => "Section",
                            "type" => "dropdown",
                            "param_name" => "section_id",
                            "value" => array("Select Section" => "") + getSections(),
                            "admin_label" => true
                        ),

                        array(
                            "type" => "textfield",
                            "heading" => "Class name",
                            "param_name" => "additional_class",
                        )
                    )
                ));
            }

            function wwp_vc_sections($atts, $content = null)
            {
                extract(shortcode_atts(array(
                    "section_id" => "",
                    "additional_class" => "",
                ), $atts));

                if(!empty($section_id))
                {
                    $get_section_args = array(
                        "post_type" => "wwp_vc_sections",
                        "p" => (int)$section_id
                    );

                    $output = '';

                    $get_section = new WP_Query($get_section_args);

                    while($get_section->have_posts())
                    {
                        $get_section->the_post();

                        if($get_section->post->ID === (int)$section_id)
                        {
                            wp_enqueue_style('wwp-vc-sections', plugins_url( 'include/css/wwp-vc-sections.css', __FILE__ ), false, '1.0');

                            $output .= '<div class="wwp-vc-sections-element '.$additional_class.'">';
 
                            ob_start();

                            visual_composer()->addFrontCss();

                            echo get_the_content();

                            $output .= ob_get_clean();

                            $output .= '</div>';

                            $output = do_shortcode($output);
                        }
                    }

                    wp_reset_query();

                    return $output;

                }
            }
        }
    }

    function wwp_sections_activate()
    {
        $vc_already_compatible_with_default = vc_default_editor_post_types();
        $vc_already_compatible_with = vc_editor_post_types();

        $vc_already_compatible_with[] = 'wwp_vc_sections';
        $vc_already_compatible_with_default[] = 'wwp_vc_sections';

        vc_set_default_editor_post_types($vc_already_compatible_with_default);
        vc_editor_set_post_types($vc_already_compatible_with);
    }

    register_activation_hook( __FILE__, 'wwp_sections_activate' );
}

if(!class_exists('WWP_VC_SECTIONS_WIDGET'))
{
    class WWP_VC_SECTIONS_WIDGET extends WP_Widget
    {
        function __construct()
        {
            parent::__construct(
                "wwp_vc_sections_widget",
                "VC Sections",
                array(
                    "description" => "Allows you to add a Visual Composer section to any sidebar."
                )
            );
        }

        function widget($args, $instance)
        {
            $section_id = intval($instance['vc_section_id']);

            if(shortcode_exists('wwp_vc_sections'))
            {
                echo do_shortcode('[wwp_vc_sections section_id="'.$section_id.'"]');
            }
        }

        function form($instance)
        {
            $defaults = array('vc_section_id' => '');
            $instance = wp_parse_args( (array) $instance, $defaults );
            $section_id_select = $instance['vc_section_id'];

            $sections_args = array(
                "post_type" => "wwp_vc_sections",
                "post_status" => "publish",
                "posts_per_page" => -1
            );

            $sections = new WP_Query($sections_args);

            $output = '';

            if($sections->have_posts())
            {
                $output .= '<p><select class="widefat" name="'.$this->get_field_name('vc_section_id').'">';

                if($section_id_select == '')
                {
                    $output .= '<option selected="selected" value="">Select Section</option>';
                }

                while($sections->have_posts())
                {
                    $sections->the_post();

                    $add_section_selected = '';

                    if($section_id_select == $sections->post->ID)
                    {
                        $add_section_selected = 'selected="selected"';
                    }

                    $output .= '<option '.$add_section_selected.' value="'.$sections->post->ID.'">'.$sections->post->post_title.'</option>';
                }

                $output .= '</select></p>';
            }
            else
            {
                $output .= '<p>No sections added yet.</p>';
            }

            echo $output;
        }

        function update($new_instance, $old_instance)
        {
            $instance = $old_instance;

            $instance['vc_section_id'] = $new_instance['vc_section_id'];

            return $instance;
        }
    }

    function init_wwp_vc_sections_widget()
    {
        register_widget("wwp_vc_sections_widget");
    }
    add_action("widgets_init", "init_wwp_vc_sections_widget");
}

new WWP_VC_SECTIONS();
new WWP_VC_SECTIONS_WIDGET();