/**
  * get active module by installer
  * @param $module
  * @param $default_module if specific module not exists, get default other module
  */
 private function get_active_module_from_installer($module = '', $default_module = 'hw-importer')
 {
     if (!$module) {
         $module = hw__req('module');
     }
     if (!count(HW_TGM_Module_Activation::get_register_modules(array('active' => 1, 'slug' => $module)))) {
         $module = $default_module;
     }
     if ($module) {
         return HW_Module_Settings_page::get_modules($module);
     }
 }
 /**
  * Generate WP-compatible $menu and $submenu arrays from a custom menu tree.
  */
 private function _build_custom_wp_menu()
 {
     $new_menu = array();
     $new_submenu = array();
     global $menu, $submenu;
     $cache_submenu = HW_HOANGWEB::get_wp_option('hw_custom_wp_submenu');
     $cache_modules_submenus = HW_HOANGWEB::get_wp_option('other_modules_submenus');
     if (empty($cache_submenu) || empty($cache_modules_submenus)) {
         #$root = new HW_Array_List_items_path($submenu, __CLASS__);
         $parent =& HW_Module_Settings_page::get_root_wp_menu_data($submenu);
         $parent1 =& HW_Widget_Features_Setting::get_root_wp_menu_data($submenu);
         $root_hoangweb =& HW_HOANGWEB_Settings::get_root_wp_menu_data($submenu);
         /*#eval('$parent = &$submenu'. $parent.';');
                     $parent[] = Array
                     (
                         'abc',
                         'manage_options',
                         'http://localhost/wp1/wp-admin/admin.php?tab=gallery&page=hw_modules_settings&module=gallery&tgmpa-tab-nonce=2ad45f5013&_wpnonce=922a2d6727',
         
                     );
                     set_transient('hw-replace-admin-submenu', $submenu);*/
         self::build_custom_wp_menu(array('menu' => &$menu, 'submenu' => &$submenu, 'root' => &$parent, 'root_widget_features' => &$parent1, 'root_hoangweb' => &$root_hoangweb));
         HW_SESSION::save_session('submenu', $submenu);
         HW_SESSION::save_session('menu', $menu);
         $this->custom_wp_menu = $menu;
         $this->custom_wp_submenu = $submenu;
     }
 }
 /**
  * desploy modules with positions
  */
 private function desploy_modules_position()
 {
     if (is_admin()) {
         return;
     }
     $modules_positions = $this->get_field_value('modules_position');
     if (is_array($modules_positions)) {
         foreach ($modules_positions as $module => $item) {
             //if($item['condition'] == 0) continue;
             $match = 1;
             //check condition before module can display
             $dynamic_settings = HW_Condition::get_active_conditions_settings();
             $condition = $item['condition'];
             //condition id
             if ($condition) {
                 if (!isset($dynamic_settings[$condition])) {
                     continue;
                 }
                 $setting = array($condition => $dynamic_settings[$condition]);
                 $setting_conditions = HW__Template_Condition::parse_template_conditions($setting);
                 #_print($setting_conditions);
                 foreach ($setting_conditions as $pages_condition) {
                     //and, or condition
                     if (isset($pages_condition) && is_array($pages_condition)) {
                         //get template alternate with AND relation
                         foreach ($pages_condition as $temp => $meet_condition) {
                             if ($meet_condition['result']) {
                                 $match = 1;
                                 //break;  //first occurence
                             } else {
                                 $match = 0;
                             }
                         }
                     }
                 }
             }
             if ($match) {
                 //add action for module position
                 $module_inst = HW_Module_Settings_page::get_modules($module);
                 if (!empty($module_inst)) {
                     HW_Module_Positions::add_position_hook($item['position'], $module_inst->get_module_content_cb());
                 }
             }
         }
     }
 }
 /**
  * setup module data
  * @param $module
  */
 protected function setup_module_data($module)
 {
     $module_inst = HW_Module_Settings_page::get_modules($module);
     #(new HW_Module())->get_config();
     if (!$module_inst) {
         //module not found
         $this->command_log(sprintf('Module %s chưa được kích hoạt.', $module));
         return;
     }
     #$module_config = $module_inst->get_config();
     $wxr_files = $module_inst->get_module_wxr_files();
     if (!isset($wxr_files['export'])) {
         return;
     }
     //config file not exists
     $file = $wxr_files['export'];
     $module_inst->setup_demo($file);
     $this->command_log('Cài đặt module ' . $module . '.');
 }
 /**
  * Create default title column along with action links of 'Install'
  * and 'Activate'.
  *
  * @since 2.2.0
  *
  * @param array $item Array of item data.
  * @return string     The action hover links.
  */
 public function column_plugin($item)
 {
     #$installed_plugins = get_plugins();
     $installed_plugins = hw_get_modules();
     // No need to display any hover links.
     if (hw_is_active_module($item['file_path'])) {
         $actions = array();
     }
     $actions = array();
     // We need to display the 'Install' hover link.
     if (!isset($item['force_activation']) || $item['force_activation'] == false) {
         if (!isset($installed_plugins[$item['file_path']])) {
             $actions = array('install' => sprintf('<a href="%1$s" title="' . esc_attr__('Kích hoạt', 'tgmpa') . ' %2$s">' . __('Kích hoạt', 'tgmpa') . '</a>', esc_url(wp_nonce_url(add_query_arg(array('page' => urlencode(HW_TGM_Module_Activation::$instance->menu), 'module' => urlencode($item['slug']), 'module_name' => urlencode($item['sanitized_plugin']), 'tgmpa-activate' => 'activate-plugin', 'tgmpa-activate-nonce' => urlencode(wp_create_nonce('tgmpa-activate'))), admin_url('admin.php')), 'tgmpa-install')), $item['sanitized_plugin']));
         } else {
             $actions = array('install' => sprintf('<a href="%1$s" title="' . esc_attr__('Hủy kích hoạt', 'tgmpa') . ' %2$s">' . __('Hủy Kích hoạt', 'tgmpa') . '</a>', esc_url(wp_nonce_url(add_query_arg(array('page' => urlencode(HW_TGM_Module_Activation::$instance->menu), 'module' => urlencode($item['slug']), 'module_name' => urlencode($item['sanitized_plugin']), 'tgmpa-deactivate' => 'deactivate-plugin', 'tgmpa-deactivate-nonce' => urlencode(wp_create_nonce('tgmpa-deactivate'))), admin_url('admin.php')), 'tgmpa-install')), $item['sanitized_plugin']));
         }
         //setting page url
         $actions['config'] = sprintf('<a href="%1$s" title="' . esc_attr__('Cấu hình', 'tgmpa') . ' %2$s">' . __('Cấu hình', 'tgmpa') . '</a>', wp_nonce_url(add_query_arg(array('tab' => HW_Module_Settings_page::valid_tab_slug($item['slug']), 'page' => urlencode(HW_Module_Settings_page::PAGE_SLUG), 'module' => urlencode($item['slug']), 'module_name' => urlencode($item['sanitized_plugin']), 'tgmpa-tab-nonce' => urlencode(wp_create_nonce('tgmpa-tab-nonce'))), admin_url('admin.php')), 'tgmpa-install'), $item['sanitized_plugin']);
     }
     // We need to display the 'Activate' hover link.
     /*elseif ( is_plugin_inactive( $item['file_path'] ) ) {
                     $actions = array(
                         'activate' => sprintf(
                             '<a href="%1$s" title="' . esc_attr__( 'Activate', 'tgmpa' ) . ' %2$s">' . __( 'Activate', 'tgmpa' ) . '</a>',
                             esc_url(
                                 add_query_arg(
                                     array(
                                         'page'                 => urlencode( HW_TGM_Module_Activation::$instance->menu ),
                                         'plugin'               => urlencode( $item['slug'] ),
                                         'plugin_name'          => urlencode( $item['sanitized_plugin'] ),
                                         'plugin_source'        => urlencode( $item['url'] ),
                                         'tgmpa-activate'       => 'activate-plugin',
                                         'tgmpa-activate-nonce' => urlencode( wp_create_nonce( 'tgmpa-activate' ) ),
                                     ),
                                     network_admin_url( 'options-general.php' )
                                 )
                             ),
                             $item['sanitized_plugin']
                         ),
                     );
                 }
     */
     return sprintf('%1$s %2$s', $item['plugin'], $this->row_actions($actions));
 }
 /**
  * get current module for this cli
  */
 protected function get_current_module()
 {
     if (empty($this->module)) {
         $class = get_called_class();
         $reflector = new ReflectionClass($class);
         $file = $reflector->getFileName();
         $split = explode(DIRECTORY_SEPARATOR, $file);
         $file = end($split);
         preg_match('#^class-cli-(.+)\\.php$#', trim($file), $arr);
         if (count($arr) > 1) {
             $module = $arr[1];
             $this->module = HW_Module_Settings_page::get_modules($module);
         }
     }
     return $this->module;
 }
    /**
     * Widget Backend
     * @param $instance
     */
    public function form($instance)
    {
        //widget title
        if (isset($instance['title'])) {
            $title = $instance['title'];
        } else {
            $title = __('');
        }
        $address = isset($instance['address']) ? $instance['address'] : '';
        //location
        $width = isset($instance['width']) ? $instance['width'] : '';
        //width
        $height = isset($instance['height']) ? $instance['height'] : '';
        //height
        $show_searchbox = isset($instance['show_searchbox']) && $instance['show_searchbox'] ? 1 : 0;
        //show search iinput box
        ?>
        <p>
            <label for="<?php 
        echo $this->get_field_id('title');
        ?>
"><?php 
        _e('Title:');
        ?>
</label><br/>
            <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('address');
        ?>
"><?php 
        _e('Vị trí:');
        ?>
</label><br/>
            <input type="text" name="<?php 
        echo $this->get_field_name('address');
        ?>
" id="<?php 
        echo $this->get_field_id('address');
        ?>
" value="<?php 
        echo $address;
        ?>
"/><br/>
            (<em>Để trống lấy mặc định bởi cài đặt module Map.</em>)<br/>
            <a href="<?php 
        echo HW_Module_Settings_page::get_module_setting_page('map');
        ?>
" target="_blank">Cấu hình gmap</a>
        </p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('width');
        ?>
"><?php 
        _e('Width (px/%):');
        ?>
</label><br/>
            <input type="text" name="<?php 
        echo $this->get_field_name('width');
        ?>
" id="<?php 
        echo $this->get_field_id('width');
        ?>
" value="<?php 
        echo $width;
        ?>
"/>(mặc định px)<br/>
            (<em>Để trống lấy mặc định bởi cài đặt module Map.</em>)
        </p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('height');
        ?>
"><?php 
        _e('Height (px/%):');
        ?>
</label><br/>
            <input type="text" name="<?php 
        echo $this->get_field_name('height');
        ?>
" id="<?php 
        echo $this->get_field_id('height');
        ?>
" value="<?php 
        echo $height;
        ?>
"/>(mặc định px)<br/>
            (<em>Để trống lấy mặc định bởi cài đặt module Map.</em>)
        </p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('show_searchbox');
        ?>
"><?php 
        _e('Hiển thị tìm kiếm:');
        ?>
</label><br/>
            <input type="checkbox" name="<?php 
        echo $this->get_field_name('show_searchbox');
        ?>
" id="<?php 
        echo $this->get_field_id('show_searchbox');
        ?>
" <?php 
        checked($show_searchbox);
        ?>
/><br/>
            (<em>Để trống lấy mặc định bởi cài đặt module Map.</em>)
        </p>
    <?php 
    }
    /**
     * @param array $instance
     * @return string|void
     */
    public function form($instance)
    {
        $instance = wp_parse_args((array) $instance, array('title' => ''));
        $title = $instance['title'];
        ?>
        <p>
            <label for="<?php 
        echo $this->get_field_id('title');
        ?>
">Tiêu đề: <input class="widefat" id="<?php 
        echo $this->get_field_id('title');
        ?>
" name="<?php 
        echo $this->get_field_name('title');
        ?>
" type="text" value="<?php 
        echo attribute_escape($title);
        ?>
" /></label>
        </p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('font_color');
        ?>
">Mã mầu chữ: <input class="widefat" id="<?php 
        echo $this->get_field_id('font_color');
        ?>
" name="<?php 
        echo $this->get_field_name('font_color');
        ?>
" type="text" value="<?php 
        echo $instance['font_color'];
        ?>
" /></label>
        </p>

        <!-- UPDATE PLAN -->
        <p>
            <label for="<?php 
        echo $this->get_field_id('count_start');
        ?>
">Thêm lượt truy cập ảo: <input class="widefat" id="<?php 
        echo $this->get_field_id('count_start');
        ?>
" name="<?php 
        echo $this->get_field_name('count_start');
        ?>
" type="text" value="<?php 
        echo $instance['count_start'];
        ?>
" /></label>
        </p>

        <p>
            <label for="<?php 
        echo $this->get_field_id('hits_start');
        ?>
">Bắt đầu từ Hits: <input class="widefat" id="<?php 
        echo $this->get_field_id('hits_start');
        ?>
" name="<?php 
        echo $this->get_field_name('hits_start');
        ?>
" type="text" value="<?php 
        echo $instance['hits_start'];
        ?>
" /></label>
        </p>
        <p><font size='2'>Nhập một con số cộng vào lượt hits hiện tại (mặc định bắt đầu từ 1)</font></p>
        <!-- END UPDATE -->
        <p><font size='3'><b>Tùy chọn hiển thị</b></font></p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('today_view');
        ?>
"><?php 
        _e('Truy cập hôm nay? ');
        ?>
<input type="checkbox" class="checkbox" <?php 
        checked($instance['today_view'], 'on');
        ?>
 id="<?php 
        echo $this->get_field_id('today_view');
        ?>
" name="<?php 
        echo $this->get_field_name('today_view');
        ?>
" /></label>
        </p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('yesterday_view');
        ?>
"><?php 
        _e('Truy cập hôm qua? ');
        ?>
<input type="checkbox" class="checkbox" <?php 
        checked($instance['yesterday_view'], 'on');
        ?>
 id="<?php 
        echo $this->get_field_id('yesterday_view');
        ?>
" name="<?php 
        echo $this->get_field_name('yesterday_view');
        ?>
" /></label>
        </p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('month_view');
        ?>
"><?php 
        _e('Theo tháng? ');
        ?>
<input type="checkbox" class="checkbox" <?php 
        checked($instance['month_view'], 'on');
        ?>
 id="<?php 
        echo $this->get_field_id('month_view');
        ?>
" name="<?php 
        echo $this->get_field_name('month_view');
        ?>
" /></label>
        </p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('year_view');
        ?>
"><?php 
        _e('Theo năm? ');
        ?>
<input type="checkbox" class="checkbox" <?php 
        checked($instance['year_view'], 'on');
        ?>
 id="<?php 
        echo $this->get_field_id('year_view');
        ?>
" name="<?php 
        echo $this->get_field_name('year_view');
        ?>
" /></label>
        </p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('total_view');
        ?>
"><?php 
        _e('Tổng views? ');
        ?>
<input type="checkbox" class="checkbox" <?php 
        checked($instance['total_view'], 'on');
        ?>
 id="<?php 
        echo $this->get_field_id('total_view');
        ?>
" name="<?php 
        echo $this->get_field_name('total_view');
        ?>
" /></label>
        </p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('hits_view');
        ?>
"><?php 
        _e('Hits hôm nay? ');
        ?>
<input type="checkbox" class="checkbox" <?php 
        checked($instance['hits_view'], 'on');
        ?>
 id="<?php 
        echo $this->get_field_id('hits_view');
        ?>
" name="<?php 
        echo $this->get_field_name('hits_view');
        ?>
" /></label>
        </p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('totalhits_view');
        ?>
"><?php 
        _e('Tổng Hits? ');
        ?>
<input type="checkbox" class="checkbox" <?php 
        checked($instance['totalhits_view'], 'on');
        ?>
 id="<?php 
        echo $this->get_field_id('totalhits_view');
        ?>
" name="<?php 
        echo $this->get_field_name('totalhits_view');
        ?>
" /></label>
        </p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('online_view');
        ?>
"><?php 
        _e('Số online? ');
        ?>
<input type="checkbox" class="checkbox" <?php 
        checked($instance['online_view'], 'on');
        ?>
 id="<?php 
        echo $this->get_field_id('online_view');
        ?>
" name="<?php 
        echo $this->get_field_name('online_view');
        ?>
" /></label>
        </p>

        <p>
            <label for="<?php 
        echo $this->get_field_id('ip_display');
        ?>
"><?php 
        _e('Hiển thị IP? ');
        ?>
<input type="checkbox" class="checkbox" <?php 
        checked($instance['ip_display'], 'on');
        ?>
 id="<?php 
        echo $this->get_field_id('ip_display');
        ?>
" name="<?php 
        echo $this->get_field_name('ip_display');
        ?>
" /></label>
        </p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('server_time');
        ?>
"><?php 
        _e('Thời gian? ');
        ?>
<input type="checkbox" class="checkbox" <?php 
        checked($instance['server_time'], 'on');
        ?>
 id="<?php 
        echo $this->get_field_id('server_time');
        ?>
" name="<?php 
        echo $this->get_field_name('server_time');
        ?>
" /></label>
        </p>

        <p>Chú ý: cấu hình hình ảnh bộ đếm <a href="<?php 
        echo HW_Module_Settings_page::get_module_setting_page('counter');
        ?>
">tại đây</a></p>

    <?php 
    }
 /**
  * list all displayable modules
  * @return array
  */
 public static function get_modules_displayable()
 {
     $data = array();
     $modules = HW_Module_Settings_page::get_modules_data();
     foreach ($modules as $module) {
         if ($module->option('enable_position')) {
             $meta = (array) $module->option('module_info');
             $data[$meta['slug']] = $meta['name'];
         }
     }
     return $data;
 }
 /**
  * get current module info
  * @param $item
  * @return array
  */
 public function get_module_info($item = '')
 {
     static $data;
     $module_slug = preg_replace('#.+?\\/#', '', str_replace('\\', '/', trim($this->_apply_plugin, '\\/')));
     if (!isset($data[$module_slug])) {
         $module = HW_Module_Settings_page::get_modules($module_slug);
         $data[$module_slug] = array('slug' => $module_slug);
         if ($module) {
             $data[$module_slug]['name'] = $module->get_module_info('name');
             $data[$module_slug]['object'] = $module;
         }
     }
     return $item ? isset($data[$module_slug][$item]) ? $data[$module_slug][$item] : '' : $data[$module_slug];
 }
 /**
  * get into module setting page
  * @param string|array $query
  * @return string
  */
 public function get_setting_page_url($query = '')
 {
     if (is_string($query) && $query) {
         parse_str($query, $query);
     }
     $args = array('tab' => HW_Module_Settings_page::valid_tab_slug($this->module_name), 'page' => urlencode(HW_Module_Settings_page::PAGE_SLUG), 'module' => urlencode($this->module_name), 'module_name' => urlencode($this->get_module_name()), 'tgmpa-tab-nonce' => urlencode(wp_create_nonce('tgmpa-tab-nonce')));
     if (is_array($query)) {
         $args = array_merge($query, $args);
     }
     return add_query_arg($args, admin_url('admin.php'));
 }