return isset($theme) && is_array($theme) ? $theme : array();
                }
            }
        }
    }
}
/**
 * filter pagenavi output
 */
add_filter('wp_pagenavi', 'hw_paginav_filter_pagenavi_output', 10, 2);
function hw_paginav_filter_pagenavi_output($html)
{
    HWPageNavi_Core::render_pagination_skin($html);
    return '<div class="hw-pagenavi-container">' . $html . '</div>';
}
if (hw_is_active_module('product')) {
}
/**
 * output wp_pagenavi content & custom by skin
 * @param $skin_file: skin file to override wp_pagenavi content
 * @param $html: default wp_pagenavi output
 */
function hw_paginav_output($skin_file, $html)
{
    if (file_exists($skin_file)) {
        return include $skin_file;
    }
}
/**
 * register plugin help
 */
 /**
  * load widget template
  * @param array $instance: widget instance (optional)	 
  * @param array $data: array of data need to be extract to current context
  */
 private function load_widget_template(&$instance = array(), $data = array())
 {
     //extract data
     if (isset($this->shared) && is_array($this->shared)) {
         extract($this->shared);
     } else {
         if (is_array($data)) {
             extract($data);
         }
     }
     //extract variables from array $args that get from 'widget' method
     if (isset($args)) {
         extract($args);
     }
     if (!is_array($instance) || !count($instance)) {
         $instance = $this->hw_parse_data();
         $instance = $instance['instance'];
     } else {
         $this->get_widget_instance($instance);
     }
     //by hoangweb.com
     if ($this->skin) {
         //load skin
         $file = $this->skin->get_skin_file(empty($instance['skin']) ? 'default' : $instance['skin']);
         if (file_exists($file)) {
             /**
              * pagination skin
              */
             $paginav = $this->skin->get_skin_instance('pagination')->get_skin_file(empty($instance['pagination_skin']) ? 'default' : $instance['pagination_skin']);
             //skin settings
             /*
             $paginav_skin_setting = isset($instance['skin_setting'])? $instance['skin_setting'] : '';
             
             $skin_setting_file = $this->skin->get_file_skin_setting(); //current skin setting
             $skin_options = $this->skin->get_file_skin_options();      //current skin options
             
             if(file_exists($skin_setting_file)) include ($skin_setting_file);
             if(file_exists($skin_options)) include ($skin_options);
             
             if(isset($theme) && isset($theme['options'])) $default_options = $theme['options'];
             if(isset($default_options) && isset($theme_options)) {
                 $paginav_skin_setting = HW_SKIN::merge_skin_options_values($paginav_skin_setting, $default_options, $theme_options);
             }
             */
             /**
              * scrollbar skin
              */
             $scrollbar = $this->skin->get_skin_instance('scrollbar')->get_skin_file(empty($instance['scrollbar_skin']) ? 'default' : $instance['scrollbar_skin']);
             //widget feature: grid_posts
             $grid_posts = HW_AWC::get_widget_feature($this, 'grid_posts');
             $awc_enable_grid_posts = false;
             //disable by default
             if ($grid_posts && HW_AWC::check_widget_feature($this, 'grid_posts')) {
                 $awc_enable_grid_posts = $grid_posts->is_active($instance);
                 if ($awc_enable_grid_posts) {
                     $awc_grid_posts_cols = $grid_posts->get_field_value('awc_grid_posts_cols');
                 }
             }
             /**
              * init some variables
              */
             $theme = array();
             //declare stylesheets file
             //content wrapper ID
             $hwtpl_wrapper_id = $this->get_holder_id($instance['widget_id']);
             $hwtpl_scrollbar_wrapper_class = '';
             //scrollbar content wrapper css class
             $hwtpl_pagination_class = '';
             //for pagination
             /**
              * load pagination component
              */
             if (file_exists($paginav)) {
                 include $paginav;
                 //valid
                 if (!isset($theme['scripts'])) {
                     $theme['scripts'] = array();
                 }
                 if (!isset($theme['styles'])) {
                     $theme['styles'] = array();
                 }
                 //migrate skin from pagination module
                 if ($instance['use_default_pagenav_skin'] && hw_is_active_module('pagination')) {
                     $theme = HWPageNavi_Core::render_pagination_skin();
                 } elseif (count($theme['styles']) || count($theme['scripts'])) {
                     $this->skin->get_skin_instance('pagination')->enqueue_files_from_skin($theme['styles'], $theme['scripts']);
                     //enqueue stuff from skin
                     /*HW_SKIN::enqueue_skin_assets(array(
                           'instance' => $this->skin,
                           'hash_skin' => $instance['skin'],
                           'skin_file' => $file,
                           'skin_settings' => $theme,
                           'skin_options' => $skin_setting
                       ));*/
                 }
                 //parse template tags
                 $hwtpl_pagination_class = isset($theme['pagination_class']) ? $theme['pagination_class'] : '';
                 $this->share_data('pagination', $theme);
             }
             //_print($this->skin->get_active_skin()); //enqueue stuff from skin
             /**
              * load scrollbar component
              */
             if (isset($instance['enable_scrollbar']) && $instance['enable_scrollbar'] && file_exists($scrollbar)) {
                 //load skin resource
                 $scrollbar_options_config = $this->skin->get_skin_instance('scrollbar')->get_file_skin_options(empty($instance['scrollbar_skin']) ? 'default' : $instance['scrollbar_skin']);
                 //theme options configuration
                 $scrollbar_theme_setting = $this->skin->get_skin_instance('scrollbar')->get_file_skin_setting();
                 //load theme-setting.php
                 //theme options
                 $scrollbar_theme_options = isset($instance['scrollbar_skin_setting']) ? $instance['scrollbar_skin_setting'] : array();
                 //scrollbar skin options
                 //reset
                 $theme['styles'] = array();
                 //declare stylesheets file
                 $theme['scripts'] = array();
                 //declare js files
                 $theme['options'] = array();
                 if (!empty($scrollbar_options_config) && file_exists($scrollbar_options_config)) {
                     include $scrollbar_options_config;
                     //include define scrollbar theme options
                 }
                 if (!empty($scrollbar_theme_setting) && file_exists($scrollbar_theme_setting)) {
                     include $scrollbar_theme_setting;
                     //scrollbar theme setting
                 }
                 if (isset($theme_options) && isset($theme)) {
                     $default = isset($theme['options']) ? $theme['options'] : array();
                     $result = HW_SKIN::get_skin_options($scrollbar_theme_options, $default, $theme_options);
                     $scrollbar_theme_options = array_merge($scrollbar_theme_options, $result);
                 }
                 /*
                 //best way but use old way as above do
                 $scrollbar_skin_setting = isset($instance['scrollbar_skin_setting'])? $instance['scrollbar_skin_setting'] : '';
                 
                                    $scrollbarskin_setting_file = $this->skin->get_skin_instance('scrollbar')->get_file_skin_setting(); //current skin setting
                                    $scrollbar_skin_options = $this->skin->get_skin_instance('scrollbar')->get_file_skin_options();      //current skin options
                 
                                    if(file_exists($scrollbarskin_setting_file)) include ($scrollbarskin_setting_file);
                                    if(file_exists($scrollbar_skin_options)) include ($scrollbar_skin_options);
                 
                                    if(isset($theme) && isset($theme['options'])) $scrollbar_default_options = $theme['options'];
                                    if(isset($scrollbar_default_options) && isset($theme_options)) {
                 $scrollbar_skin_setting = HW_SKIN::merge_skin_options_values($scrollbar_skin_setting, $scrollbar_default_options, $theme_options);
                                    }
                 */
                 //parse template tags
                 $hwtpl_scrollbar_wrapper_class = isset($theme['scrollbar_css']) ? $theme['scrollbar_css'] : '';
                 include $scrollbar;
                 //valid
                 /*if(!isset($theme['scripts'])) $theme['scripts'] = array();
                                        if(!isset($theme['styles'])) $theme['styles'] = array();
                 
                                        if(count($theme['styles']) || count($theme['scripts'])) {
                                            $this->skin->get_skin_instance('scrollbar')->enqueue_files_from_skin($theme['styles'], $theme['scripts']);
                                        }*/
                 //enqueue stuff from skin
                 HW_SKIN::enqueue_skin_assets(array('instance' => $this->skin->get_skin_instance('scrollbar'), 'hash_skin' => $instance['scrollbar_skin'], 'skin_file' => $scrollbar, 'skin_settings' => $theme, 'skin_options' => $scrollbar_theme_options));
                 $this->init_scrollbar_options($scrollbar_theme_options, $theme);
             }
             /**
              * load main skin
              */
             $options_config = $this->skin->get_file_skin_options();
             $skin_setting = $this->skin->get_file_skin_setting();
             if (file_exists($skin_setting)) {
                 include $skin_setting;
             }
             //theme options
             $skin_options = isset($instance['skin_setting']) ? $instance['skin_setting'] : array();
             $skin_options = HW_SKIN::merge_skin_options_values($skin_options, $skin_setting, $options_config);
             //params
             if (empty($instance['intermediate_image_sizes']) && !empty($instance['thumb_w']) && !empty($instance['thumb_h'])) {
                 $image_size = array($instance['thumb_w'], $instance['thumb_h']);
             } elseif (!empty($instance['intermediate_image_sizes'])) {
                 $image_size = $instance['intermediate_image_sizes'];
             } else {
                 $image_size = 'thumbnail';
             }
             //reset
             $theme['styles'] = array();
             //declare stylesheets file
             $theme['scripts'] = array();
             //declare js files
             $theme['options'] = array();
             //refresh theme options
             //widget features
             $wfeatures = $this->init_widget_features(array('theme' => $theme, 'skin_options' => $skin_options, 'instance' => $instance));
             //render skin template power by twig
             $data = array('hwtpl_scrollbar_wrapper_class', 'cat_posts', 'full_image_src', 'fancy_group', 'fancybox_g1', 'arrExlpodeFields', 'image_size', 'wfeatures', 'hwtpl_wrapper_id', 'args', 'instance', 'open_title_link', 'close_title_link');
             $data = compact($data);
             $data['context'] = $this;
             //reference to this object
             $content = $this->skin->render_skin_template($data, false);
             if ($content !== false) {
                 echo $content;
             }
             include $file;
             //valid
             /*if(!isset($theme['scripts'])) $theme['scripts'] = array();
                                if(!isset($theme['styles'])) $theme['styles'] = array();
             
                                if(count($theme['styles']) || count($theme['scripts'])) {
                                    $this->skin->enqueue_files_from_skin($theme['styles'], $theme['scripts']); //enqueue stuff from skin
                                }*/
             //enqueue stuff from skin
             HW_SKIN::enqueue_skin_assets(array('instance' => $this->skin, 'hash_skin' => $instance['skin'], 'skin_file' => $file, 'skin_settings' => $theme, 'skin_options' => $skin_options));
             //init scrolling
             //add_action('wp_footer',array(&$this, 'init_content_options'),1000);	//don't use wp hook to setup scroll
             $this->init_content_options(array('theme' => $theme, 'skin_options' => $skin_options, 'instance' => $instance));
         }
     } else {
         echo __('not found class HW_SKIN.');
     }
 }
 /**
  * process bulk actions for module/plugin
  * @param array $plugins
  * @param $action action bulk
  */
 public function process_bulk_action($plugins = array(), $action = '')
 {
     // Grab plugin data from $_POST.
     #$plugins             = isset( $_POST['plugin'] ) ? (array) $_POST['plugin'] : array();
     $plugins_to_activate = array();
     // Split plugin value into array with plugin file path, plugin source and plugin name.
     $plugins_need2activate = array();
     if (is_array($plugins)) {
         foreach ($plugins as $i => $plugin) {
             $_plugin = explode(',', $plugin);
             if (isset(HW_TGM_Module_Activation::get_instance()->plugins[$_plugin[0]])) {
                 $module_info = HW_TGM_Module_Activation::get_instance()->plugins[$_plugin[0]];
                 $active = array();
                 //local downloaded plugins
                 if (!empty($module_info['require_wp_plugin']) && file_exists(WP_PLUGIN_DIR . '/' . trim($module_info['require_wp_plugin'], '/'))) {
                     $active[] = $module_info['require_wp_plugin'];
                     $active[] = 'repo';
                     $plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . trim($module_info['require_wp_plugin'], '/'));
                     $active[] = $plugin_data['Name'];
                 }
                 if (!empty($module_info['require_module'])) {
                     //check module that require an other
                     $require_module = $module_info['require_module'];
                     if (!hw_is_active_module($require_module)) {
                         hw_inline_msg("Xin lỗi, module `{$module_info['name']}` yêu cầu cài trước module " . $require_module);
                         $plugins_to_activate[] = array($module_info['slug'], $module_info['name']);
                     }
                 }
                 if (!empty($module_info['require_wp_plugin']) && !is_plugin_active($module_info['require_wp_plugin']) && !empty($active)) {
                     $plugins_need2activate[] = $active;
                 }
             }
             $plugins_to_activate[] = $_plugin;
         }
     }
     foreach ($plugins_to_activate as $i => $array) {
         if (isset($array[1]) && !preg_match('|.php$|', $array[1])) {
             unset($plugins_to_activate[$i]);
         }
     }
     // Return early if there are no plugins to activate.
     if (empty($plugins_to_activate)) {
         return;
     }
     //activate plugins require for the your modules
     if (count($plugins_need2activate)) {
         $this->hw_activate_plugins($plugins_need2activate);
     }
     if ('tgmpa-bulk-activate' === $action) {
         // Now we are good to go - let's start activating plugins.
         hw_activate_modules($plugins_to_activate);
     } elseif ('tgmpa-bulk-deactivate' === $action) {
         //deactive module
         hw_deactivate_modules($plugins_to_activate);
     }
     // Update recently activated plugins option.
     $recent = (array) get_option('recently_activated');
     foreach ($plugins as $plugin => $time) {
         if (isset($recent[$plugin])) {
             unset($recent[$plugin]);
         }
     }
     update_option('recently_activated', $recent);
 }
 /**
  * Deactivates a plugin before it is upgraded.
  *
  * Hooked to the {@see 'upgrader_pre_install'} filter by {@see Plugin_Upgrader::upgrade()}.
  *
  * @since 2.8.0
  * @since 4.1.0 Added a return value.
  *
  * @param bool|WP_Error  $return Upgrade offer return.
  * @param array          $plugin Plugin package arguments.
  * @return bool|WP_Error The passed in $return param or {@see WP_Error}.
  */
 public function deactivate_module_before_upgrade($return, $plugin)
 {
     if (is_wp_error($return)) {
         //Bypass.
         return $return;
     }
     // When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
     if (defined('DOING_CRON') && DOING_CRON) {
         return $return;
     }
     $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
     if (empty($plugin)) {
         return new WP_Error('bad_request', $this->strings['bad_request']);
     }
     if (hw_is_active_module($plugin)) {
         //Deactivate the plugin silently, Prevent deactivation hooks from running.
         hw_deactivate_modules($plugin);
     }
     return $return;
 }
    /**
     * Widget Backend
     * @param $instance
     */
    public function form($instance)
    {
        //widget title
        if (isset($instance['title'])) {
            $title = $instance['title'];
        } else {
            $title = __('Chữ chạy', 'hoangweb');
        }
        //$library  = isset($instance['library'])? $instance['library'] : ''; //library
        $current_source = isset($instance['source']) ? $instance['source'] : 'content';
        //data source
        $data = isset($instance['data']) ? $instance['data'] : array();
        //data
        //content scrolling
        $content = isset($data['content']) ? $data['content'] : '';
        //skin
        $skin_setting = isset($instance['skin_settings']) ? $instance['skin_settings'] : '';
        $skin = isset($instance['skin']) ? $instance['skin'] : '';
        // Widget admin form
        ?>
        <p><strong>Lưu ý</strong>: nhấn nút lưu widget trong lần đầu tiên khởi tạo widget.</p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('title');
        ?>
"><?php 
        _e('Title:');
        ?>
</label>
            <input class="widefat" id="<?php 
        echo $this->get_field_id('title');
        ?>
" name="<?php 
        echo $this->get_field_name('title');
        ?>
" type="text" value="<?php 
        echo esc_attr($title);
        ?>
" />
        </p>
        <!--
        <p>
            <label for="<?php 
        //echo $this->get_field_id('library')
        ?>
"><?php 
        _e('Thư viện');
        ?>
</label><br/>
            <select name="<?php 
        //echo $this->get_field_name('library')
        ?>
" id="<?php 
        //echo $this->get_field_id('library')
        ?>
">
                <?php 
        /*foreach($this->libraries as $lib => $text) {
              $selected = ($library == $lib)? 'selected="selected"' : '';
              printf( '<option value="%s" %s>%s</option>', $lib, $selected, $text);
          }*/
        ?>

            </select>
        </p>
        -->
        <?php 
        if (!empty($this->skin)) {
            ?>
        <p>
            <label for="<?php 
            ?>
"><?php 
            _e('Thư viện');
            ?>
</label>
            <?php 
            echo $this->skin->get_skins_select_tag('skin', null, array('class' => 'widefat'), false);
            ?>
            <!-- show skin options if exists -->
            <?php 
            echo $this->skin->prepare_skin_options_fields('skin_settings', $skin_setting, $skin);
            ?>
        </p>
        <?php 
        }
        ?>
        <p>
            <label for="<?php 
        echo $this->get_field_id('source');
        ?>
"><?php 
        _e('Nguồn');
        ?>
</label><br/>
            <select name="<?php 
        echo $this->get_field_name('source');
        ?>
" id="<?php 
        echo $this->get_field_id('source');
        ?>
">
                <?php 
        foreach ($this->sources as $source => $text) {
            $selected = selected($source, $current_source, false);
            printf('<option %s value="%s">%s</option>', $selected, $source, $text);
        }
        ?>
            </select>
        </p>

        <!-- slides data -->
        <?php 
        if ($current_source == 'slides') {
            $module = HW_TGM_Module_Activation::get_modules('hw-ml-slider', OBJECT);
            if (hw_is_active_module('hw-ml-slider')) {
                $current_slider = isset($data['slider']) ? $data['slider'] : '';
                $slides = hwmq_get_all_mlsliders();
                ?>
        <p id="<?php 
                echo $this->get_field_id('source_slides');
                ?>
" style="">
            <label for="<?php 
                echo $this->get_field_id('slider');
                ?>
"><?php 
                _e('Slider');
                ?>
</label><br/>
            <select name="<?php 
                echo $this->get_field_name('data');
                ?>
[slides]" id="<?php 
                echo $this->get_field_id('slides');
                ?>
">
                <?php 
                foreach ($slides as $id => $name) {
                    $selected = selected($id, $current_slider, false);
                    printf('<option %s value="%s">%s</option>', $selected, $id, $name);
                }
                ?>
            </select>
        </p>
        <?php 
            } else {
                echo __('Vui lòng kích hoạt module ' . $module->name);
            }
        } elseif ($current_source == 'content') {
            //default
            ?>
        <!-- content text -->
        <p id="<?php 
            echo $this->get_field_id('source_content');
            ?>
" style="">
            <label for="<?php 
            echo $this->get_field_id('content');
            ?>
"><?php 
            _e('Nội dung chạy');
            ?>
</label><br/>
            <textarea class="" style="width:100%; min-height:200px" name="<?php 
            echo $this->get_field_name('data');
            ?>
[content]" id="<?php 
            echo $this->get_field_id('content');
            ?>
"><?php 
            echo isset($data['content']) ? $data['content'] : '';
            ?>
</textarea>
        </p>
            <script>
                jQuery(function($){
                    CKEDITOR.config.extraPlugins = "hw_wp_media_button";
                    //CKEDITOR.config.width = 700;
                    var editor = '<?php 
            echo $this->get_field_id('content');
            ?>
',
                        id = '<?php 
            echo $this->number;
            ?>
',
                        editor_obj = CKEDITOR.instances[editor];

                    var config = {
                        language: "vi",
                        //uiColor: "#9AB8F3",
                        width: 700,
                        extraPlugins : "hw_wp_media_button",
                        resize_enabled : true,
                        resize_minWidth: 300,
                        resize_minHeight: 300,
                        resize_maxWidth : 2000,
                        resize_maxHeight: 2000,
                        resize_dir: 'both'
                    };
                    // Replace the <textarea id="editor1"> with a CKEditor
                    // instance, using default configuration.
                    /*if (editor_obj) {
                     editor_obj.destroy(true);
                     CKEDITOR.remove(editor_obj);
                     }*/
                    if(!(!jQuery.isNumeric(id) && editor_obj)) CKEDITOR.replace( editor ,config);

                    $('#<?php 
            echo $this->get_field_id('savewidget');
            ?>
').bind('click', function(e){
                        console.log('savewidget');
                        $('#'+editor).val( CKEDITOR.instances[editor].getData());
                    });
                });

            </script>
            <?php 
        }
        ?>

    <?php 
    }
/**
 * check whether module is inactive
 * @param $name
 * @return bool
 */
function hw_is_inactive_module($name)
{
    return hw_is_active_module($name);
}
 /**
  * current module actived ?
  * @return bool
  */
 public static function is_active($module = '')
 {
     if (!$module) {
         $current = self::get();
         $module = $current->module_name;
         //parent module
     }
     return hw_is_active_module($module);
 }