/**
  * load option grid posts
  * @param WP_Widget $t: widget object
  * @param array $instance: widget data
  */
 function do_widget_feature($t, $instance = array())
 {
     $this->widget_instance = $instance;
     //maybe update widget instance
     //$wf_name = $this->get_widget_feature_name('');
     //list registered sidebars
     $sidebars = hwawc_get_active_sidebars_select();
     $sidebar_skins = HW_AWC_Sidebar_Settings::available_widget_skins(true);
     //sidebar skins
     //saved widget configs by specifying group
     $widgetconfig_groups = AWC_WidgetFeature_saveconfig::get_widgets_settings_select(' WHERE _group="' . self::WIDGET_CONFIG_GROUP . '"');
     //HW_UI_Component::build_select_tag($sidebars, '', '');
     echo '<div class="form-wf-shortcode-params" id="' . $this->get_field_id('wfshortcode_container') . '"><fieldset><legend>Tạo shortcode widget</legend>';
     echo '<input type="hidden" class="widget_class" name="' . $this->get_field_name('widget_class') . '" id="' . $this->get_field_id('widget_class') . '" value="' . get_class($t) . '"/>';
     echo '<input type="hidden" class="widget_instance" name="' . $this->get_field_name('widget_instance') . '" id=" ' . $this->get_field_id('widget_instance') . ' " value="' . base64_encode(serialize($instance)) . '"/>';
     //apply widget to sidebar
     echo '<p><label for="' . $this->get_field_id('sidebar') . '">Chọn Sidebar</label><br/>';
     echo '<select class="hw-select sidebar" name="' . $this->get_field_name('sidebar') . '" id="' . $this->get_field_id('sidebar') . '">';
     foreach ($sidebars as $id => $name) {
         $selected = selected($this->get_field_value('sidebar'), $id, false);
         printf('<option value="%s" %s>%s</option>', $id, $selected, $name);
     }
     echo '</select></p>';
     //choose sidebar skin
     echo '<p><label for="' . $this->get_field_id('sidebar_skin') . '">Chọn giao diện Sidebar</label><br/>';
     echo HW_UI_Component::build_select_tag($sidebar_skins, null, array('name' => $this->get_field_name('sidebar_skin'), 'id' => $this->get_field_id('sidebar_skin'), 'class' => 'sidebar_skin'));
     echo '</select></p>';
     //widget config group
     echo '<p><label for="' . $this->get_field_id('config_group') . '">Chọn widget config</label><br/>';
     echo '<select class="config_group" name=" ' . $this->get_field_name('config_group') . ' " id="' . $this->get_field_id('config_group') . '">';
     foreach ($widgetconfig_groups as $id => $text) {
         $selected = selected($this->get_field_value('config_group'), $id, false);
         printf('<option value="%s" %s>%s</option>', $id, $selected, $text);
     }
     echo '</select>';
     echo '<a href="javascript:void(0)" onclick="__awc_feature_shortcode_params.refresh_saved_widgetsconfig(this,\'' . $this->get_field_id('config_group') . '\')">Refresh</a>';
     //you also should be active saveconfig feature
     if (!HW_AWC_WidgetFeatures::check_widget_feature($t, 'saveconfig')) {
         echo '<div>Bạn cũng cần kích hoạt feature "<a href="' . admin_url('options-general.php?page=' . HW_HOANGWEB_Settings::HW_SETTINGS_PAGE) . '" target="_blank">saveconfig</a>" cho widget này.</div>';
     }
     echo '</p>';
     echo '<a href="javascript:void(0)" onclick="__awc_feature_shortcode_params.generate_shortcode(this,jQuery(\'#' . $this->get_field_id('wfshortcode_container') . '\'))" class="button">Tạo shortcode</a>';
     echo '<div></div>';
     echo '<p><em>Hướng dẫn</em>: Sử dụng tính năng "Lưu cài đặt" ở tại widget này để lưu cấu hình của widget (chọn nhóm "Shortcode Widget")</p>';
     echo '</fieldset></div>';
 }
コード例 #2
0
    /**
     * extra fields to any widget
     * @param object $t: widget class object
     * @param $return
     * @param array $instance: widget instance
     */
    public function _hw_awc_in_widget_form($t, $return, $instance)
    {
        /*begin custom sidebar format*/
        static $sw_list;
        if (!$sw_list) {
            $sw_list = get_option('sidebars_widgets');
        }
        //get all widgets
        global $wp_registered_sidebars;
        foreach ($sw_list as $sidebar => $widgets) {
            //get sidebar where widget nested on it
            if (is_array($widgets) && in_array($t->id, $widgets)) {
                //make sure you ignore the key 'array_version'
                $found_sidebar = true;
                break;
            }
        }
        if ($sidebar == 'array_version' || !isset($found_sidebar) || !isset($wp_registered_sidebars[$sidebar])) {
            echo '<div style="background:#fafafa;border:1px solid #dadada;padding:5px;"><p>Nhấn nút Lưu để hiển thị công cụ tùy chỉnh widget này hoặc nếu không thấy thì xóa widget và thêm lại.</p></div>';
            return;
        }
        //first check
        if (isset($wp_registered_sidebars[$sidebar]['old_id'])) {
            $old_sidebar = $wp_registered_sidebars[$sidebar]['old_id'];
        } else {
            $old_sidebar = $sidebar;
        }
        if (!HW_AWC_Sidebar_Settings::check_valid_sidebar_name($old_sidebar)) {
            echo "<div class='hwawc-message'>Do Sidebar cũ có ID ({$old_sidebar}) không hợp lệ, nên tạo mới sidebar với tên: ({$sidebar})";
            echo '<br/>Hiển thị sidebar:<code>dynamic_sidebar("' . $sidebar . '")</code>';
            echo '</div>';
            // return;  //fixed from callback '_hw_awc_init'
        }
        $params = $wp_registered_sidebars[$sidebar];
        //get sidebar param where widget nested on it
        //create feature tog
        if (class_exists('HW_ButtonToggle_widget')) {
            $btn_tog = new HW_ButtonToggle_widget($t, $instance);
        }
        //instance HW_SKIN for widget box title
        $skin = new HW_SKIN($t, HW_AWC_PATH, 'hw_awc_skins', 'hwawc-skin.php', 'skins');
        $skin->plugin_url = HW_AWC_URL;
        $skin->external_callback = false;
        $skin->init();
        //seem no longer use
        ?>
        <?php 
        if (isset($btn_tog)) {
            $btn_tog->set_button_toggle_start_wrapper('Tùy chỉnh widget');
        }
        ?>
        <div style="background:#fafafa;border:1px solid #dadada;padding:5px;">
            <!-- show widget ID on each widget in admin. -->
            <p>
            <div id="<?php 
        echo $t->get_field_id('show_widget_id');
        ?>
" style="padding:5px;background:pink;border:1px solid red;" name="<?php 
        echo $t->get_field_name('show_widget_id');
        ?>
" >
                ID: <strong><?php 
        echo $t->id;
        ?>
</strong><br/>
                <?php 
        if (is_plugin_active('amr-shortcode-any-widget/amr-shortcode-any-widget.php')) {
            echo 'Hiển thị nội dung widget này bởi shortcode:<br/>';
            echo '<code>[do_widget id=' . $t->id . ']</code>';
            echo '<a href="' . HW_AWC_URL . '/docs/amr-shortcode-any-widget.html' . '" target="_blank">Hướng dẫn</a>';
        } else {
            echo hw_install_plugin_link('amr-shortcode-any-widget', 'Nhúng widget này bằng shortcode');
        }
        //display widget content with feature "shortcode_params"
        if (!HW_AWC_WidgetFeatures::check_widget_feature($t, 'shortcode_params') && class_exists('HW_HOANGWEB_Settings')) {
            echo '<p>Kích hoạt feature "<a target="_blank" href="' . admin_url('options-general.php?page=' . HW_HOANGWEB_Settings::HW_SETTINGS_PAGE) . '">Tạo shortcode widget</a>" để hiển thị nội dung của widget này.</p>';
        }
        ?>
            </div>
            </p>
            <!-- widget features -->
            <?php 
        $this->load_widget_features($t, $instance);
        ?>
            <p>

            </p>
            <hr/>
            <!-- sidebar custom after_title & before_title -->
            <?php 
        if (isset($params['before_title'])) {
            ?>

                <p>
                    <label for="<?php 
            echo $t->get_field_id('awc_clear_widget_title');
            ?>
"><strong><?php 
            _e('Xóa tiêu đề widget');
            ?>
</strong></label><br/>
                    <input type="checkbox" name="<?php 
            echo $t->get_field_name('awc_clear_widget_title');
            ?>
" id="<?php 
            echo $t->get_field_id('awc_clear_widget_title');
            ?>
" <?php 
            checked(isset($instance['awc_clear_widget_title']) && $instance['awc_clear_widget_title'] ? 1 : 0);
            ?>
/><?php 
            _e('Xóa before_title & after_title');
            ?>
                    <br/>
                    <span style="font-size:12px">Nếu xóa tiêu đề (xóa before_title & after_title) thì check vào đây. <i>(Tùy chọn này sẽ xóa thẻ HTML có trong before_title & after_title và cả before_widget,after_widget nếu cần thiết để có thể hiển thị tốt widget mà không có title.)</i></span>
                    <br/>

                </p><hr/>

                <p>
                    <label><strong><?php 
            _e('Sửa before_title');
            ?>
</strong></label><br/>
                    <em>Hiện tại: (<font color="blue"><?php 
            echo htmlentities($params['before_title']);
            ?>
</font>)</em><br/>
                    <em><?php 
            if (!preg_match('#%\\d\\$s#', $params['before_title'])) {
                echo '<font color="red">Cảnh báo: before_title chưa sẵn sàng.</font>';
            }
            ?>
</em><br/>
                    <input <?php 
            ?>
 type="text" name="<?php 
            echo $t->get_field_name('format_before_title');
            ?>
" id="<?php 
            echo $t->get_field_id('format_before_title');
            ?>
" value="<?php 
            echo isset($instance['format_before_title']) ? $instance['format_before_title'] : '';
            ?>
"><br/>
                    <span style="font-size:12px"><i>(mỗi ID or chuỗi class ngăn cách bởi '|')</i></span>
                    <br/>
                    <span><em>ie: before_title=>"<?php 
            echo htmlentities('<h2 id="%1$s" class="%2$s" style="%3$s">');
            ?>
"</em></span>
                </p><hr/>
            <?php 
        }
        ?>
            <p>
                <label><strong><?php 
        _e('Sửa after_title');
        ?>
</strong></label><br/>
                <em>Hiện tại: (<font color="blue"><?php 
        echo htmlentities($params['after_title']);
        ?>
</font>)</em><br/>
                <em><?php 
        if (!preg_match('#%\\d\\$s#', $params['after_title'])) {
            echo '<font color="red">Cảnh báo: after_title chưa sẵn sàng.</font>';
        }
        ?>
</em><br/>
                <input <?php 
        ?>
 type="text" name="<?php 
        echo $t->get_field_name('format_after_title');
        ?>
" id="<?php 
        echo $t->get_field_id('format_after_title');
        ?>
" value="<?php 
        echo isset($instance['format_after_title']) ? $instance['format_after_title'] : '';
        ?>
"><br/>
                <span style="font-size:12px"><i>(mỗi ID or chuỗi class ngăn cách bởi '|')</i></span>
                <br/>
                <span><em>ie: after_title=>"<?php 
        echo htmlentities('</h2><div id="%1$s" class="%2$s" style="%3$s">');
        ?>
"</em></span>
            </p><hr/>
            <p>
                <label><strong><?php 
        _e('Sửa before_widget');
        ?>
</strong></label><br/>
                <em>Thêm class theo thứ tự bằng cách chèn tag (*1,*2,..) vào tham số before_widget: <br/>(Hiện tại:<font color="blue"><?php 
        echo htmlentities($params['before_widget']);
        ?>
</font>)</em><br/>
                <em><?php 
        if (!preg_match('#\\*\\d+#', $params['before_widget'])) {
            echo '<font color="red">Cảnh báo: before_widget chưa sẵn sàng.</font>';
        }
        ?>
</em>
                <input type="text" name="<?php 
        echo $t->get_field_name('preclasses_before_widget');
        ?>
" id="<?php 
        echo $t->get_field_id('preclasses_before_widget');
        ?>
" value="<?php 
        echo isset($instance['preclasses_before_widget']) ? $instance['preclasses_before_widget'] : '';
        ?>
"><br/>
                <span style="font-size:12px"><i>(mỗi chuỗi class tương ứng với *1|*2.. ngăn cách bởi '|'. VD: class1 class2|class3 class4 class5)</i></span>
            </p>
            <hr/>
            <p>
                <label for="<?php 
        echo $t->get_field_id('widget_skin');
        ?>
"><strong><?php 
        _e('Giao diện widget');
        ?>
</strong></label>
            <div>
                <select name="<?php 
        echo $t->get_field_name('widget_skin');
        ?>
" id="<?php 
        echo $t->get_field_id('widget_skin');
        ?>
">
                    <?php 
        foreach (HW_AWC_Sidebar_Settings::available_widget_skins() as $name => $field) {
            ?>
                        <option <?php 
            selected(isset($instance['widget_skin']) && $name == $instance['widget_skin'] ? 1 : 0);
            ?>
 value="<?php 
            echo $name;
            ?>
"><?php 
            echo isset($field['title']) ? $field['title'] : $name;
            ?>
</option>
                    <?php 
        }
        ?>
                </select><br/>
                <span>Quản lý skins, nhấn <a target="_blank" href="<?php 
        echo HW_AWC_Sidebar_Settings::get_edit_sidebar_setting_page_link($sidebar);
        ?>
">vào đây</a>.</span>
            </div>
            </p>
            <p>
                <strong>Mầu sắc</strong><br/>
                <em>Lưu ý: TH nếu xóa mầu và khi load lại trang cần xóa lại vì mầu khởi đầu là #FFFFFF.<br/> Nếu không xuất hiện hộp chọn mầu thì tự điền mã mầu và lưu lại để khởi tạo lại trình chọn mã mầu.</em>
            </p>
            <p>
                <input type="text" size="5" class="color {hash:true}" name="<?php 
        echo $t->get_field_name('bgcolor_widget');
        ?>
" id="<?php 
        echo $t->get_field_id('bgcolor_widget');
        ?>
" value="<?php 
        echo isset($instance['bgcolor_widget']) ? $instance['bgcolor_widget'] : '';
        ?>
"/>
                <label for="<?php 
        echo $t->get_field_id('bgcolor_widget');
        ?>
">Mầu nền widget</label>
                <a class="clear-jscolor button" href="javascript:void(0)">Xóa</a>
            </p>
            <p>
                <input type="text" size="5" class="color {hash:true}" name="<?php 
        echo $t->get_field_name('bgcolor_title');
        ?>
" id="<?php 
        echo $t->get_field_id('bgcolor_title');
        ?>
" value="<?php 
        echo isset($instance['bgcolor_title']) ? $instance['bgcolor_title'] : '';
        ?>
"/>
                <label for="<?php 
        echo $t->get_field_id('bgcolor_title');
        ?>
">Mầu nền tiêu đề</label>
                <a class="clear-jscolor button" href="javascript:void(0)" onclick="">Xóa</a>
            </p>
            <p>
                <input type="text" size="5" class="color {hash:true}" name="<?php 
        echo $t->get_field_name('bgcolor_box');
        ?>
" id="<?php 
        echo $t->get_field_id('bgcolor_box');
        ?>
" value="<?php 
        echo isset($instance['bgcolor_box']) ? $instance['bgcolor_box'] : '';
        ?>
"/>
                <label for="<?php 
        echo $t->get_field_id('bgcolor_box');
        ?>
">Mầu nền nội dung</label>
                <a class="clear-jscolor button" href="javascript:void(0)">Xóa</a>
            </p>

            <!-- bg title -->
            <p>
                <label><strong><?php 
        _e('Ảnh nền cho tiêu đề widget');
        ?>
</strong></label>
            <div>
                <?php 
        if (strpos($params['before_title'], '{css_title}') === false) {
            echo '<font color="red">Warning: không tìm thấy {css_title} tag trong tham số <em>before_title</em>. Để sử dụng trường này bạn cần thêm tag này vào tham số <em>before_title</em> cho sidebar hiện tại.</font>';
        }
        ?>
                <?php 
        if (isset($instance['bgimg_title'])) {
            ?>
                    <img class="user-preview-image" style="max-width:100%;" src="<?php 
            echo $instance['bgimg_title'];
            ?>
"><!-- display image -->
                <?php 
        }
        ?>
                <input type="hidden" name="<?php 
        echo $t->get_field_name('bgimg_title');
        ?>
" id="<?php 
        echo $t->get_field_id('bgimg_title');
        ?>
" value="<?php 
        echo isset($instance['bgimg_title']) ? $instance['bgimg_title'] : '';
        ?>
" class="regular-text" /><!-- show image url -->
                <input type='button' class="button-primary" value="Upload" id="<?php 
        echo $t->get_field_id('hw_awc_uploadimage');
        ?>
"/><br /><!-- upload button -->
                <script type="text/javascript">
                    jQuery(function( $ ) {
                        if(typeof hw_awc_btn_upload_image == 'function') {
                            hw_awc_btn_upload_image($( '#<?php 
        echo $t->get_field_id('hw_awc_uploadimage');
        ?>
' ), $( '#<?php 
        echo $t->get_field_id('bgimg_title');
        ?>
' ));
                        }

                    });
                </script>
                <p>

                    <input type="checkbox" name="<?php 
        echo $t->get_field_name('bgimg_title_css');
        ?>
" id="<?php 
        echo $t->get_field_id('bgimg_title_css');
        ?>
" onlick="this.value = this.checked" <?php 
        checked(isset($instance['bgimg_title_css']) ? 1 : 0);
        ?>
/>
                    <label for="<?php 
        echo $t->get_field_id('bgimg_title_css');
        ?>
"><?php 
        _e('Hiển thị ảnh nền cho tiêu đề widget');
        ?>
</label>
                </p>
            </div>
            </p>
            <!-- bg box -->
            <p>
                <label for="<?php 
        echo $t->get_field_id('hw_awc_upload_bgimg_box');
        ?>
"><strong><?php 
        _e('Ảnh nền cho nội dung widget');
        ?>
</strong></label><br/>
                <?php 
        if (strpos($params['after_title'], '{css_box}') === false && strpos($params['before_widget'], '{css_box}') === false) {
            echo '<div><font color="red">Warning: không tìm thấy {css_box} tag trong tham số <em>after_title</em> hoặc <em>before_widget</em>. Để sử dụng trường này bạn cần thêm tag này cho sidebar hiện tại.</font></div>';
        }
        ?>
                <?php 
        if (isset($instance['bgimg_box'])) {
            ?>
                    <img class="user-preview-image" style="max-width:100%;" src="<?php 
            echo $instance['bgimg_box'];
            ?>
"><!-- display image -->
                <?php 
        }
        ?>
                <input type="hidden" name="<?php 
        echo $t->get_field_name('bgimg_box');
        ?>
" id="<?php 
        echo $t->get_field_id('bgimg_box');
        ?>
"
                       value="<?php 
        echo isset($instance['bgimg_box']) ? $instance['bgimg_box'] : '';
        ?>
" class="regular-text" /><!-- show image url -->
                <input type='button' class="button-primary" value="Upload" id="<?php 
        echo $t->get_field_id('hw_awc_upload_bgimg_box');
        ?>
"/><br /><!-- upload button -->
            </p>
            <script type="text/javascript">
                jQuery(function( $ ) {
                    if(typeof hw_awc_btn_upload_image == 'function') {
                        hw_awc_btn_upload_image($( '#<?php 
        echo $t->get_field_id('hw_awc_upload_bgimg_box');
        ?>
' ),$('#<?php 
        echo $t->get_field_id('bgimg_box');
        ?>
'));
                    }

                });
                jQuery(function($){
                    //empty color input
                    var empty_color_input = function(obj){
                        $(obj).attr('value','').css({background:'#fff'});
                        $(obj).empty();
                        //remove color from input
                        //HW_AWC.jscolor_set(obj,'');//wrong it will reset to white color
                    };
                    //init jscolor when ajax response for all input field has 'color' class
                    $('input.color').each(function(i,obj){
                        var color = $(obj).val(),
                            clear_btn = $($(obj).next().next());

                        $(obj).data('clear_btn', clear_btn);
                        //bind clear color button
                        if(!$(obj).data('clear_color_btn')){
                            $(obj).data({
                                clear_color_btn:function(){
                                    empty_color_input(obj);
                                    //remove color from input
                                    //HW_AWC.jscolor_set(obj,'');//wrong it will reset to white color
                                }
                            });
                        }
                        clear_btn.bind('click',$(obj).data('clear_color_btn'));
                        //update color
                        if(color && $(obj).attr('value')){
                            HW_AWC.jscolor_set(obj,color);  //resume color in input tag
                        }
                        else {
                            setTimeout(function(){
                                empty_color_input(obj); //clear default white color
                                $(obj).data('clear_btn').trigger('click');
                            },1000);
                        }

                    });
                });

            </script>
        </div>
        <?php 
        if (isset($btn_tog)) {
            $btn_tog->set_button_toggle_end_wrapper();
        }
        //close feature tog
    }
コード例 #3
0
 /**
  * @hook hw_sidebars_widgets
  * @param $sidebars
  */
 public static function _sidebars_widgets($sidebars)
 {
     if (is_admin()) {
         return $sidebars;
     }
     global $wp_registered_widgets;
     foreach ($sidebars as $s => $sidebar) {
         if ($s == 'wp_inactive_widgets' || strpos($s, 'orphaned_widgets') === 0 || empty($sidebar)) {
             continue;
         }
         foreach ($sidebar as $w => $widget) {
             // $widget is the id of the widget
             if (!isset($wp_registered_widgets[$widget])) {
                 continue;
             }
             if (isset(self::$checked[$widget])) {
                 $show = self::$checked[$widget];
             } else {
                 $widget_info = HW_AWC::get_widget_data($widget);
                 $opts = $wp_registered_widgets[$widget];
                 $widget_obj = isset($widget_info['widget']) ? $widget_info['widget'] : null;
                 $id_base = isset($widget_info['id_base']) ? $widget_info['id_base'] : '';
                 #$id_base = is_array( $opts['callback'] ) ? $opts['callback'][0]->id_base : $opts['callback'];
                 if (!$id_base) {
                     continue;
                 }
                 $feature = HW_AWC::get_widget_feature($widget, self::_static_option('feature_name'));
                 #
                 if (!$feature || HW_AWC_WidgetFeatures::check_widget_feature($widget, self::_static_option('feature_name') && !$feature->is_active())) {
                     continue;
                 }
                 #$instance = get_option( 'widget_' . $id_base );
                 $instance = $widget_info['instance'];
                 if (!$instance || !is_array($instance)) {
                     continue;
                 }
                 //from 'display widgets' plugin
                 if (isset($instance['_multiwidget']) && $instance['_multiwidget']) {
                     $number = $opts['params'][0]['number'];
                     if (!isset($instance[$number])) {
                         continue;
                     }
                     $instance = $instance[$number];
                     unset($number);
                 }
                 unset($opts);
                 $show = self::show_widget($feature, $instance);
                 self::$checked[$widget] = $show ? true : false;
             }
             if (!$show) {
                 unset($sidebars[$s][$w]);
                 #$this->hide_list[] = "['$s'][$w]";
             }
             unset($widget);
         }
         unset($sidebar);
     }
     return $sidebars;
 }