mercredi 8 juin 2016

Visual composer extended parameter type not saving

Currently we are constructing a website for a customer who needs to be able to modify most of his website without any knowledge of coding, so we figured visual composer was the way to go. However we need a functionality that isn't included in VC. We made a custom post type named "referenties" in Wordpress and wish to be able to select these posts via checkboxes. The selected(checked) posts then are displayed on the frontend. I created a plugin that extends VC but I'm running into a problem.

Because it's going to be multiple checkboxes that are based on the custom post type, I started creating a custom parameter type. I am able to display the posts of the custom post type in the backend.

However, whenever I select any or all of these checkboxes the value is not saved and can't be shown on the frontend.

I've had contact with the makers of VC, and they told me I was missing the "'save_always' => true" parameter. I've added it but it's still not saving the values, other than that they just redirected me to some of their online "developer how-to's", which aren't that much help to me now.

Clearly I'm missing something as to why it's not saving, any and all help would be greatly appreciated.

Below is my full plugin:

    /*
    Plugin Name: Visual Composer - Image Post Slider

    */


    */

    // don't load directly
    if (!defined('ABSPATH')) die('-1');


    // set css for vc field
    function add_backend_css(){
        wp_enqueue_style( 'tfd_checkbox_dropdown',plugins_url().'/tfd_vc_img_posts_slider/assets/css/tfd_checkbox_dropdown.css');
    }
    add_action('admin_enqueue_scripts', "add_backend_css");


    //Add backend vc field
    vc_add_shortcode_param( 'checkbox_drop', 'tfd_checkbox_dropdown', plugins_url().'/tfd_vc_img_posts_slider/assets/js/tfd_vc_ips_form_handler.js' );
    function tfd_checkbox_dropdown( $settings, $value ) {

        // get cutom post type
        $type = 'referenties';
        $args = array(
            'post_type' => $type,
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'caller_get_posts'=> 1
        );

        $my_query = null;
        $my_query = new WP_Query($args);




        //for every post make a checkbox so user can select what posts to display
        if( $my_query->have_posts() ) {
            while ($my_query->have_posts()) : $my_query->the_post(); 

                $tfd_checkbox_drop_form .= '
                    <li><input type="checkbox" name="'.esc_attr( $settings['param_name'] ).'" 
                     class="wpb_vc_param_value wpb-textinput'.
                        esc_attr( $settings['param_name'] ) . ' ' .
                        esc_attr( $settings['type'] ) . '_field checkbox"'.
                    '" value="'.get_the_ID().'" />'.get_the_title().'</li>';    

            endwhile;
        }
        //wp_reset_query();  // Restore global post data stomped by the_post().



        //return the field
        return $tfd_checkbox_drop_form;
    }



    //Start class for VC extention
    class TFDipsExtendVC {
        function __construct() {
            // We safely integrate with VC with this hook
            add_action( 'init', array( $this, 'tfd_ips_integrateWithVC' ) );

            // Use this when creating a shortcode addon
            add_shortcode( 'tfd-img-post-slider', array( $this, 'TFDImgPostSlider' ) );

            // Register CSS and JS
            add_action( 'wp_enqueue_scripts', array( $this, 'loadCssAndJs' ) );
        }

        public function tfd_ips_integrateWithVC() {
            // Check if Visual Composer is installed
            if ( ! defined( 'WPB_VC_VERSION' ) ) {
                // Display notice that Visual Compser is required
                add_action('admin_notices', array( $this, 'showVcVersionNotice' ));
                return;
            }


            vc_map( 
                array(
                    "name" => __("Afbeelding & Posts Slider", 'vc_extend'),
                    "description" => __("Slider voor afbeeldingen en berichten", 'vc_extend'),
                    "base" => "tfd-img-post-slider",
                    "class" => "",
                    "controls" => "full",
                    "icon" => plugins_url('assets/tfd.png', __FILE__), 
                    "category" => __('Content', 'js_composer'),

                    "params" => array(

                        array(
                            'type' => 'attach_images',
                            'heading' => __( 'Images', 'js_composer' ),
                            'param_name' => 'images',
                            'value' => '',
                            'save_always' => true,
                            'description' => __( 'Selecteer afbeeldingen van de media bibliotheek.', 'js_composer' ),
                        ),

                        //add the new selection fields to the form
                         array(
                            "type" => "checkbox_drop",
                            "holder" => "div",
                            "heading" => __("Selecteer de referenties", "js_composer"),
                            "param_name" => "tfd_checkbox_drop",
                            'save_always' => true,
                            "description" => __( "Selecteer welke referenties u wilt tonen", 'js_composer' ),
                        ),


                    )
                ) 
            );
        }

        /*
        Shortcode logic how it should be rendered
        */
        public function TFDImgPostSlider( $atts, $content = null ) {

            $atts = vc_map_get_attributes( 'tfd_ips_integrateWithVC', $atts );



            $output = '
            <div class="img-post-slider">
                <ul>';

            print_r($tfd_checkbox_drop);
            $tfd_checkbox_drop = explode(",", $tfd_checkbox_drop, 4);



            $images = explode(",", $images);
            $i = 0;

            //for each image get the fields for the corresponding custom post type
            foreach ( $images as $attach_id ){

                if ( $attach_id > 0 ) {
                    $post_thumbnail = wpb_getImageBySize( array(
                        'attach_id' => $attach_id,
                        'thumb_size' => $img_size,
                    ) );
                };

                $rating_full = '';
                $quote = get_field( "quote", $tfd_checkbox_drop[$i]);
                $rating_nr = get_field( "beoordeling", $tfd_checkbox_drop[$i]);
                $name = get_field( "naam", $tfd_checkbox_drop[$i] );
                $company = get_field( "bedrijfsnaam", $tfd_checkbox_drop[$i] );
                $categories = get_the_terms($tfd_checkbox_drop[$i], 'doelgroep');
                $category = $categories[0]->name;

                //code to generate a star rating
                for($j = 0; $j < $rating_nr; $j++){
                    $rating_full .= '<div class="ipslider star_rating_filled">star-</div>';
                }
                if($rating_nr < 5){
                    for($k = $rating_nr; $k < 5; $k++){
                        $rating_full .= '<div class="ipslider star_rating_clear">-star</div>';
                    }
                }

                //Output all elements
                $output .= '
                    <li>
                        <img src="'.$post_thumbnail['p_img_large'][0].'"/>
                        <div class="ipslider review '.$category.'">
                            <div class="ipslider review_quote">'.$quote.'</div>
                            <div class="ipslider rating">'.$rating_full.'</div>
                            <div class="ipslider review_author">
                                <div class="ipslider author_name">'.$name.'</div>
                                <div class="ipslider author_company">'.$company.'</div>
                            </div>
                        </div>
                    </li>';


                //$output .= '';
                $i++;
            };

            $output .=
                '</ul>
            </div>';



            return $output;
        }

        /*
        Load plugin css and javascript files which you may need on front end of your site
        */
        public function loadCssAndJs() {

            wp_enqueue_script( 'vc_extend_js', '/wp-content/plugins/tfd_vc_img_posts_slider/assets/js/vc_extend.js');
            wp_enqueue_script( 'unslider_min_js', '/wp-content/plugins/tfd_vc_img_posts_slider/assets/js/unslider-master/src/js/unslider.js');
            wp_enqueue_style( 'vc_extend_style', '/wp-content/plugins/tfd_vc_img_posts_slider/assets/js/unslider-master/dist/css/unslider.css' );

        }

        /*
        Show notice if your plugin is activated but Visual Composer is not
        */
        public function showVcVersionNotice() {
            $plugin_data = get_plugin_data(__FILE__);
            echo '
            <div class="updated">
                <p>'.sprintf(__('<strong>%s</strong> requires <strong><a href="#" target="_blank">Visual Composer</a></strong> plugin to be installed and activated on your site.', 'vc_extend'), $plugin_data['Name']).'</p>
            </div>';
        }
    }
    // Finally initialize code
    new TFDipsExtendVC();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire