Wp面包店使用类的嵌套元素

时间:2018-02-21 10:44:59

标签: php wordpress visual-composer

我使用嵌套元素wp面包店。我正在尝试使用类和公共功能。

但无法让它发挥作用。它没有注册。我认为它可能是一个问题,因为我使用了课程。请找出什么是错的。我不需要一个解决方案。我知道如何使用成员函数来处理它。但我需要使用成员函数

来做
                <?php
            /*
             *  Element Description: Featured Block
             */
                //featured block container
                if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
                    class protechsaasFeaturedBlock extends WPBakeryShortCodesContainer {

                        // Element Init
                        function __construct() {
                            add_action( 'init', array( $this, 'protechsaas_featured_block_mapping' ) );
                            add_shortcode( 'feature_container', array( $this, 'protechsaas_featured_block_html' ) );
                        }
                        // Element Mapping
                        public function protechsaas_featured_block_mapping() {

                            // Stop all if VC is not enabled
                            if ( !defined( 'WPB_VC_VERSION' ) ) {
                                return;
                            }

                             //Register "container" content element. It will hold all your inner (child) content elements
                            vc_map( array(
                                "name" => __("Feature Block", "protechsaas"),
                                "base" => "feature_container",
                                "as_parent" => array('only' => 'feature'), // Use only|except attributes to limit child shortcodes (separate multiple values with comma)
                                "content_element" => true,
                                "show_settings_on_create" => false,
                                "is_container" => true,
                                "params" => array(
                                    // add params same as with any other content element
                                    array(
                                        "type" => "textfield",
                                        "heading" => __("Block Title", "protechsaas"),
                                        'holder' => 'h2',
                                        'class' => 'sub-title-class',
                                        "param_name" => "subtitle",
                                        "description" => __("add the title for your features block", "protechsaas")
                                    ),
                                    array(
                                        "type" => "textfield",
                                        "heading" => __("Title", "protechsaas"),
                                        'holder' => 'h2',
                                        'class' => 'title-class',
                                        "param_name" => "title",
                                        "description" => __("add the main title for your features block", "protechsaas")
                                    ),
                                    array(
                                        "type" => "textarea",
                                        "heading" => __("Content", "protechsaas"),
                                        'holder' => 'p',
                                        'class' => 'content-class',
                                        "param_name" => "content",
                                        "description" => __("add the main content for your features block", "protechsaas")
                                    ),
                                    array(
                                        'type' => 'dropdown',
                                        'holder' => 'div',
                                        'class' => 'style-class',
                                        'heading' => __( 'Block Style', 'protechsaas' ),
                                        'param_name' => 'blockstyle',
                                        'value' => array(
                                                    '1'   => 'Feature  white bg ',
                                                    '2'   => 'Feature  grey bg',
                                                    '3'   => 'Feature  icon bg none ',
                                                  ),
                                        'description' => __( 'choose you features block style', 'protechsaas' ),
                                        'admin_label' => true,
                                        'weight' => 0,
                                    ),
                                ),
                                "js_view" => 'VcColumnView'
                            ) );                             

                        }
                        // Element HTML
                        public function protechsaas_featured_block_html( $atts ,$features = null ) {

                            // Params extraction
                            extract(
                                shortcode_atts(
                                    array(
                                        'subtitle' => '',
                                        'title' => '',
                                        'content' => '',
                                        'blockstyle' => '',
                                    ), 
                                    $atts
                                )
                            );

                            switch ($blockstyle) {
                         case '1':
                            $html = '
                                    <section class="client-speak our-features padding-lg">
                                        <div class="container">
                                            <div class="row justify-content-center head-block">
                                                <div class="col-md-10"> <span>'.$subtitle.'</span>
                                                    <h2>'.$title.'</h2>
                                                    <p class="hidden-xs">'.$content.'</p>
                                                </div>
                                            </div>
                                            <ul class="row features-listing">
                                                '.do_shortcode($features).'
                                            </ul>
                                        </div>
                                    </section>'; 
                            break;
                         case '2':
                            $html = '
                                    <section class="client-speak our-features padding-lg bg-white">
                                        <div class="container">
                                            <div class="row justify-content-center head-block">
                                                <div class="col-md-10"> <span>'.$subtitle.'</span>
                                                    <h2>'.$title.'</h2>
                                                    <p class="hidden-xs">'.$content.'</p>
                                                </div>
                                            </div>
                                            <ul class="row features-listing ico-bg">
                                            '.do_shortcode($features).'
                                            </ul>
                                        </div>
                                    </section>'; 
                            break;
                         case '3':
                            $html = '
                                   <section class="client-speak our-features padding-lg">
                                        <div class="container">
                                            <div class="row justify-content-center head-block">
                                                <div class="col-md-10"> <span>'.$subtitle.'</span>
                                                    <h2>'.$title.'</h2>
                                                    <p class="hidden-xs">'.$content.'</p>
                                                </div>
                                            </div>
                                            <ul class="row features-listing bg-none">
                                                '.do_shortcode($features).'
                                            </ul>
                                        </div>
                                    </section> '; 
                            break;


                         default:
                            $html = '
                                    <section class="client-speak our-features padding-lg">
                                        <div class="container">
                                            <div class="row justify-content-center head-block">
                                                <div class="col-md-10"> <span>'.$subtitle.'</span>
                                                    <h2>'.$title.'</h2>
                                                    <p class="hidden-xs">'.$content.'</p>
                                                </div>
                                            </div>
                                            <ul class="row features-listing">
                                                '.do_shortcode($features).'
                                            </ul>
                                        </div>
                                    </section>'; 
                             break;
                    }
                            return $html;

                        }


                    }
                }

                //feature element
                if ( class_exists( 'WPBakeryShortCode' ) ) {
                    class protechsaasFeature extends WPBakeryShortCode {
                        // Element Init
                        function __construct() {
                            add_action( 'init', array( $this, 'protechsaas_feature_mapping' ) );
                            add_shortcode( 'protechsaas_feature', array( $this, 'protechsaas_feature_html' ) );
                        }
                        // Element Mapping
                        public function protechsaas_feature_mapping() {

                            // Stop all if VC is not enabled
                            if ( !defined( 'WPB_VC_VERSION' ) ) {
                                return;
                            }

                            vc_map( array(
                                "name" => __("Feature", "protechsaas"),
                                "base" => "feature",
                                "content_element" => true,
                                "as_child" => array('only' => 'feature_container'), // Use only|except attributes to limit parent (separate multiple values with comma)
                                "params" => array(
                                    // add params same as with any other content element
                                    array(
                                        "type" => "textfield",
                                        "heading" => __("title", "protechsaas"),
                                        "param_name" => "title",
                                        "description" => __("add the  title for your feature.", "protechsaas")
                                    ),
                                    array(
                                        "type" => "textarea",
                                        "heading" => __("Content", "protechsaas"),
                                        'holder' => 'p',
                                        'class' => 'content-class',
                                        "param_name" => "content",
                                        "description" => __("add the  content for your feature", "protechsaas")
                                    ),
                                    array(
                                        'type' => 'attach_image',
                                        'holder'=> 'div',
                                        'class' => '',
                                        'heading' => __('Icon image', 'protechsaas'),
                                        'param_name' => 'iconimage',
                                        'description' => __('Choose an image for icon if ypu want use your own icons else keep it blank and choose icon from next field', 'protechsaas'),
                                    ),
                                    array(
                                        'type' => 'dropdown',
                                        'holder' => 'div',
                                        'class' => 'style-class',
                                        'heading' => __( 'Saas Icons', 'protechsaas' ),
                                        'param_name' => 'icon',
                                        'value' => array(
                                                    'icon-analytics'   => 'analytics ',
                                                    'icon-responsive'   => 'responsive',
                                                    'icon-support'   => 'support',
                                                    'icon-settings'   => 'settings',
                                                    'icon-file' => 'file',
                                                    'icon-graphic' => 'graphic',
                                                  ),
                                        'description' => __( 'choose you features block style', 'protechsaas' ),
                                        'admin_label' => true,
                                        'weight' => 0,
                                    ),
                                )
                            ) );                           

                        }
                        // Element HTML
                        public function protechsaas_feature_html( $atts ) {

                            // Params extraction
                            extract(
                                shortcode_atts(
                                    array(
                                        'iconimage'   => '',
                                        'title' => '',
                                        'content' => '',
                                        'icon' => '',
                                    ), 
                                    $atts
                                )
                            );

                            if($iconimage != null){
                                $bg=wp_get_attachment_image_src($iconimage,'full');
                                $iconcontent = '<img src="'.$bg[0].'" alt="icon" class="img-fluid"/>';
                            }
                            else {
                                $iconcontent ='<span class="'.$icon.'"></span>';
                            }
                            $html='
                                <li class="col-md-4">
                                    <div class="inner"> <span class="icon">'.$iconcontent.'</span>
                                        <h3>'.$title.'</h3>
                                        <p>'.$content.'</p>
                                    </div>
                                </li>
                            ';
                            return $html;

                        }

                    }
                }

            // Element Class Init
            new protechsaasFeaturedBlock();

            new protechsaasFeature();

            ?>

3 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,花了一些时间来解决。我们不想破坏我们的命名空间环境,并保持我们的类名一致。

还有一个额外的未记录属性,您可以将其传递给vc_map函数,即unserialize(file_get_contents('data.file'));

因此您的代码应为:

php_class_name

这也适用于名称空间:

vc_map(array(
  'name' => __('Feature', 'protechsaas'),
  'php_class_name' => 'protechsaasFeaturedBlock',
));

if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
  class protechsaasFeaturedBlock extends WPBakeryShortCodesContainer {
      // Your code here
  }
}

答案 1 :(得分:1)

据我所知,这没有记录,但如果你要扩展WPBakeryShortCodesContainer类,你的类名必​​须以WPBakeryShortCode_为前缀,后缀必须与你的短代码名称相匹配'正在使用。可能还有其他限制,我没有对它进行过广泛的测试。

因此,如果你在vc_map()使用:

"base" => "feature_container",

然后将您的班级声明更改为:

class WPBakeryShortCode_feature_container extends WPBakeryShortCodesContainer

我知道这没有意义,但经过大量测试后,这是我的代码失败的原因。我以为是因为我将它宣布为一个类,而不是直接调用vc_map()

你甚至不需要声明一个短代码才能工作,所以我猜WPBakeryShortCodesContainer类中的某些内容实际上是从base参数注册了短代码,如果那里没有东西的话?

是的,如果你想让你的名字空间保持清洁/一致,那么这个巨型很糟糕¯\ _(ツ)_ /¯

答案 2 :(得分:0)

我将其复制为嵌套元素的示例。我发现您的代码中有错字。这行:

add_shortcode( 'protechsaas_feature', array( $this, 'protechsaas_feature_html' ) );

功能名称为feature,而不是protechsaas_feature。定义如下:

"base" => "feature",

因此您的代码应变为:

add_shortcode( 'feature', array( $this, 'protechsaas_feature_html' ) );