/**
  * fetch module info
  * @param $module
  */
 public function modules_info($module)
 {
     global $wp_xmlrpc_server;
     $_module = HW_TGM_Module_Activation::get_register_modules($module);
     if (count($_module)) {
         $_module = $_module[$module];
         $_module['download_link'] = 'http://api.hoangweb.com/modules/download/' . $module;
     }
     return $_module;
 }
 /**
  * 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);
     }
 }
 /**
  * this call by parent class
  */
 public function register_admin_menu()
 {
     $all_modules = HW_TGM_Module_Activation::get_register_modules();
     $module = $this->_option('module');
     $name = $module->module_name;
     $label = isset($all_modules[$name]) ? $all_modules[$name]['name'] : $name;
     $this->add_help_content(self::load_settings_page_hook_slug(HW_Module_Settings_page::PAGE_SLUG), $name, $label);
 }
 /**
  * prepare items
  */
 public function prepare_items()
 {
     //include( ABSPATH . 'wp-admin/includes/plugin-install.php' );
     include HW_ADMIN_INCLUDES . '/module-install.php';
     global $tabs, $tab, $paged, $type, $term;
     wp_reset_vars(array('tab'));
     $paged = $this->get_pagenum();
     $per_page = 30;
     // These are the tabs which are shown on the page
     $tabs = array();
     if ('search' == $tab) {
         $tabs['search'] = __('Search Results');
     }
     $tabs['featured'] = _x('Featured', 'Plugin Installer');
     $tabs['popular'] = _x('Popular', 'Plugin Installer');
     $tabs['recommended'] = _x('Recommended', 'Plugin Installer');
     $tabs['favorites'] = _x('Favorites', 'Plugin Installer');
     if ($tab === 'beta' || false !== strpos($GLOBALS['wp_version'], '-')) {
         $tabs['beta'] = _x('Beta Testing', 'Plugin Installer');
     }
     if (current_user_can('upload_plugins')) {
         // No longer a real tab. Here for filter compatibility.
         // Gets skipped in get_views().
         $tabs['upload'] = __('Upload Plugin');
     }
     $nonmenu_tabs = array('module-information');
     // Valid actions to perform which do not have a Menu item.
     /**
      * Filter the tabs shown on the Plugin Install screen.
      *
      * @since 2.7.0
      *
      * @param array $tabs The tabs shown on the Plugin Install screen. Defaults include 'featured', 'popular',
      *                    'recommended', 'favorites', and 'upload'.
      */
     $tabs = apply_filters('install_plugins_tabs', $tabs);
     /**
      * Filter tabs not associated with a menu item on the Plugin Install screen.
      *
      * @since 2.7.0
      *
      * @param array $nonmenu_tabs The tabs that don't have a Menu item on the Plugin Install screen.
      */
     $nonmenu_tabs = apply_filters('install_plugins_nonmenu_tabs', $nonmenu_tabs);
     // If a non-valid menu tab has been selected, And it's not a non-menu action.
     if (empty($tab) || !isset($tabs[$tab]) && !in_array($tab, (array) $nonmenu_tabs)) {
         $tab = key($tabs);
     }
     $args = array('page' => $paged, 'per_page' => $per_page, 'fields' => array('last_updated' => true, 'icons' => true, 'active_installs' => true), 'locale' => get_locale(), 'installed_plugins' => $this->get_installed_plugin_slugs());
     switch ($tab) {
         case 'search':
             $type = isset($_REQUEST['type']) ? wp_unslash($_REQUEST['type']) : 'term';
             $term = isset($_REQUEST['s']) ? wp_unslash($_REQUEST['s']) : '';
             switch ($type) {
                 case 'tag':
                     $args['tag'] = sanitize_title_with_dashes($term);
                     break;
                 case 'term':
                     $args['search'] = $term;
                     break;
                 case 'author':
                     $args['author'] = $term;
                     break;
             }
             break;
         case 'featured':
             $args['fields']['group'] = true;
             $this->orderby = 'group';
             // No break!
         // No break!
         case 'popular':
         case 'new':
         case 'beta':
         case 'recommended':
             $args['browse'] = $tab;
             break;
         case 'favorites':
             $user = isset($_GET['user']) ? wp_unslash($_GET['user']) : get_user_option('wporg_favorites');
             update_user_meta(get_current_user_id(), 'wporg_favorites', $user);
             if ($user) {
                 $args['user'] = $user;
             } else {
                 $args = false;
             }
             add_action('install_plugins_favorites', 'install_plugins_favorites_form', 9, 0);
             break;
         default:
             $args = false;
             break;
     }
     /**
      * Filter API request arguments for each Plugin Install screen tab.
      *
      * The dynamic portion of the hook name, `$tab`, refers to the plugin install tabs.
      * Default tabs include 'featured', 'popular', 'recommended', 'favorites', and 'upload'.
      *
      * @since 3.7.0
      *
      * @param array|bool $args Plugin Install API arguments.
      */
     $args = apply_filters("install_plugins_table_api_args_{$tab}", $args);
     if (!$args) {
         return;
     }
     /*$api =new WP_Error(0,'sfdf'); //plugins_api( 'query_plugins', $args );  //in development
     
     		if ( is_wp_error( $api ) ) {
     			$this->error = $api;
     			return;
     		}*/
     //$this->items = $api->plugins;
     $this->items = HW_TGM_Module_Activation::get_register_modules(array('core' => false));
     //$this->get_modules_repositories()
     if ($this->orderby) {
         uasort($this->items, array($this, 'order_callback'));
     }
     $this->set_pagination_args(array('total_items' => count($this->items), 'per_page' => $args['per_page']));
     /*if ( isset( $api->info['groups'] ) ) {
     			$this->groups = $api->info['groups'];
     		}*/
 }
 /**
  * parse theme config using wrx format
  * @param $file
  */
 public function parse($file)
 {
     $site = $menus = $assets = $libs = $modules = $plugins = $positions = $configuration = array();
     $xml = HW_WXR_Parser_SimpleXML::read_simplexml_object($file);
     $namespaces = $xml->namespaces;
     $xml = $xml->xml;
     $xml_parser = new HW_WXR_Parser();
     //::get_instance();
     $simplexml_parser = $xml_parser->simplexml_parser;
     //site meta
     if (isset($xml->site)) {
         $hw = $xml->site->children($namespaces['hw']);
         if (isset($hw->name)) {
             $site['name'] = (string) $hw->name;
         }
         if (isset($hw->description)) {
             $site['description'] = (string) $hw->description;
         }
         if (isset($hw->logo)) {
             $site['logo'] = (string) $hw->logo;
         }
         if (isset($hw->banner)) {
             $site['banner'] = (string) $hw->banner;
         }
         if (isset($hw->phone)) {
             $site['phone'] = (string) $hw->phone;
         }
         if (isset($hw->email)) {
             $site['admin_email'] = (string) $hw->email;
         }
         if (isset($hw->address)) {
             $site['address'] = (string) $hw->address;
         }
         if (isset($hw->testimonials)) {
             $site['testimonials'] = (string) $hw->testimonials;
         }
         if (!empty($hw->footer_text)) {
             $site['footer'] = (string) $hw->footer_text;
         }
     }
     //configuration
     if (isset($xml->configuration)) {
         $hw = $xml->configuration->children($namespaces['hw']);
         $configuration['sample_data'] = (string) $hw->sample_data;
         $media = $hw->media->children('hw');
         $configuration['media'] = array();
         foreach ($media as $image) {
             foreach ($image as $size) {
                 $atts = $size->attributes();
                 $configuration['media'][$size->getName()] = array('width' => (string) $atts->width, 'height' => (string) $atts->height, 'crop' => (string) $atts->crop);
             }
         }
         /*if(!empty($media->thumbnail)) $configuration['media']['thumbnail'] = (string) $media->thumbnail;
           if(!empty($media->medium)) $configuration['media']['medium'] = (string) $media->medium;
           if(!empty($media->large)) $configuration['media']['large'] = (string) $media->large;*/
         $configuration['locale'] = (string) $hw->locale;
     }
     //fetch menus
     foreach ($xml->xpath('/theme/menus/hw:menu') as $menu) {
         $atts = $menu->attribute();
         $menus[(string) $atts['slug']] = (string) $menu;
     }
     //fetch sidebars
     $sidebars = array();
     #$simplexml_parser->grab_sidebars($xml->xpath('/theme')[0], $namespaces);
     foreach ($xml->xpath('/theme/sidebars/hw:sidebar') as $sidebar_widgets) {
         $atts = $sidebar_widgets->attributes();
         $skin = (string) $atts['skin'];
         //sidebar skin
         $sidebar_name = (string) $atts['name'];
         if (!isset($sidebars[(string) $atts['name']])) {
             $sidebars[$sidebar_name] = array('skin' => $skin, 'widgets' => array(), 'params' => array());
         }
         //get widgets in sidebar
         $hw = $sidebar_widgets->children($namespaces['hw']);
         foreach ($hw->widget as $widget) {
             $name = (string) $widget->attributes()->name;
             $sidebars[$sidebar_name]['widgets'][] = $name;
         }
         //sidebar params
         $sidebars[$sidebar_name]['params'] = array('name' => $sidebar_name);
         if (!empty($hw->params)) {
             $sidebars[$sidebar_name]['params'] = array_merge($sidebars[$sidebar_name]['params'], $simplexml_parser->recursive_option_data($hw->params[0]->children())->option);
         }
     }
     //fetch assets
     foreach ($xml->xpath('/theme/assets') as $items) {
         $atts = $items->attributes();
         $page = isset($atts['page']) ? (string) $atts['page'] : '__all__';
         //group by page
         if (!isset($assets[$page])) {
             $assets[$page] = array();
         }
         foreach ($items as $item) {
             $atts = $item->attributes();
             $file = array();
             $file['type'] = !empty($atts['type']) ? (string) $atts['type'] : '';
             //dependencies
             if (!empty($atts['depends'])) {
                 $file['depends'] = explode(',', (string) $atts['depends']);
             } else {
                 $file['depends'] = false;
             }
             //handle
             if (!empty($atts['handle'])) {
                 $file['handle'] = (string) $atts['handle'];
             }
             if (!empty($atts['ver'])) {
                 $file['ver'] = (string) $atts['ver'];
             }
             //version
             $file['name'] = (string) $item;
             $assets[$page][] = $file;
         }
     }
     //fetch libs
     foreach ($xml->xpath('/theme/libs/lib') as $item) {
         $atts = $item->attributes();
         $lib = isset($atts['name']) ? (string) $atts['name'] : (string) $item;
         $libs[] = $lib;
     }
     //fetch modules
     $modules_list = $xml->xpath('/theme/modules');
     if ($modules_list) {
         $only_list = (string) $modules_list[0]->attributes()->only_list;
     } else {
         $only_list = 0;
     }
     if (!$only_list) {
         $installed_modules = HW_TGM_Module_Activation::get_register_modules(array('active' => 1, 'core' => 1), 'or');
         #HW_Logger::log_file($installed_modules);   //get installed modules, refer to core modules
         foreach ($installed_modules as $slug => $module) {
             $modules[$slug] = array('name' => $slug, 'status' => 1, 'core' => !empty($module['force_activation']), 'active' => 1);
         }
     }
     foreach ($xml->xpath('/theme/modules/module') as $item) {
         $atts = $item->attributes();
         $module['name'] = isset($atts['name']) ? (string) $atts['name'] : (string) $item;
         $module['status'] = isset($atts['status']) ? (string) $atts['status'] : 1;
         //active module as default
         $modules[$module['name']] = $module;
     }
     //fetch wp plugins
     foreach ($xml->xpath('/theme/plugins/plugin') as $item) {
         $atts = $item->attributes();
         $plugin['name'] = isset($atts['name']) ? (string) $atts['name'] : (string) $item;
         $plugin['status'] = isset($atts['status']) ? (string) $atts['status'] : 1;
         //active plugin as default
         $plugins[$plugin['name']] = $plugin;
     }
     //positions
     foreach ($xml->xpath('/theme/positions/position') as $item) {
         $atts = $item->attributes();
         //valid hook name
         $name = isset($atts['name']) ? (string) $atts['name'] : (string) $item;
         $name = strtolower(HW_Validation::valid_objname($name));
         //display text
         $text = (string) $item;
         if (empty($text) && $name) {
             $text = $name;
         }
         $positions[] = array('name' => $name, 'text' => $text);
     }
     unset($xml);
     return array('assets' => $assets, 'libs' => $libs, 'modules' => $modules, 'plugins' => $plugins, 'positions' => $positions, 'sidebars' => $sidebars, 'menus' => $menus, 'site' => $site, 'configuration' => $configuration);
 }
 /**
  * init module
  */
 public static final function init()
 {
     $class = get_called_class();
     if ($class) {
         $inst = new $class();
         if (!$inst instanceof HW_Module) {
             return;
         }
         //get file name where a Class was Defined
         $reflector = new ReflectionClass($class);
         //path variables
         $inst->option('module_path', dirname($reflector->getFileName()));
         $module_slug = basename($inst->option('module_path'));
         $inst->option('module_name', $module_slug);
         $modules = HW_TGM_Module_Activation::get_register_modules();
         if (isset($modules[$module_slug])) {
             $info = array_filter(HW_Plugins_Manager::get_module_info($reflector->getFileName()));
             //migrate module information
             if (!empty($info)) {
                 $modules[$module_slug] = array_merge($modules[$module_slug], $info);
             }
             HW_Plugins_Manager::register_module($modules[$module_slug]);
             //update module data
             $inst->option('module_info', $modules[$module_slug]);
         }
         $inst->option('module_url', HW_HOANGWEB_PLUGINS_URL . '/' . $module_slug);
         HW_Module_Settings_page::add_module($module_slug, $inst);
         //setup actions belong to current module instance
         self::setup_actions($inst);
         //occur after all modules loaded
         add_action('hw_modules_loaded', array($inst, 'modules_loaded'));
         //after the module loaded
         if (method_exists($inst, 'module_loaded')) {
             $inst->module_loaded();
         }
         //load cli class for the module
         $command = $inst->get_module_cli_path();
         if (!empty($command) && defined('WP_CLI') && WP_CLI) {
             include_once $command;
             $cli_class = 'HW_CLI_' . HW_Validation::valid_objname($module_slug);
             //command line class
             WP_CLI::add_command($module_slug, strtoupper($cli_class));
             //WP_CLI::get_command(); #wrong
             //add module commands to manager
             HW_CLI_Command_Utilities::get_instance()->register_cli_cmds($module_slug, HW_Core::get_child_class_methods($cli_class, ReflectionMethod::IS_PUBLIC));
         }
         //load oher cli class for module
         $cli_files = $inst->get_commands();
         if (is_array($cli_files)) {
             foreach ($cli_files as $cli) {
                 //not found file to process command line,
                 if (!$cli) {
                     continue;
                 }
                 //!isset($cli['file']) purpose for module->register_cli that more cli class write in one file
                 if (isset($cli['file'])) {
                     include_once $cli['file'];
                 }
                 if (class_exists($cli['class'], false)) {
                     WP_CLI::add_command($cli['command'], $cli['class']);
                     //add module commands to manager
                     HW_CLI_Command_Utilities::get_instance()->register_cli_cmds($cli['command'], HW_Core::get_child_class_methods($cli['class'], ReflectionMethod::IS_PUBLIC));
                 }
             }
         }
     }
 }