_read_external_packages_themes(); $this->_read_theme_packages(); $this->_read_plugin_packages(); $this->_read_external_packages_plugins(); add_action('plugins_loaded', array($this,'load_packages')); add_action('icl_extra_options_' . $_GET['page'], array($this,'render_forms')); if(isset($_POST['icl_extras_submit'])){ add_action('init', array($this,'process_forms')); } if(isset($_POST['icl_packages'])){ add_action('init', array($this, 'update_enabled_packages')); } } function scan($folder, $external = false){ global $icl_extra_packages; $packages = array(); $dh = opendir($folder); $packages_type = basename($folder); if($dh){ while($f = readdir($dh)){ if(0 === strpos($f , '.')) continue; $package_main_file = $folder . '/' . $f . '/load.php'; if(file_exists($package_main_file)){ $fp = fopen($package_main_file, 'r'); // Pull only the first 8kiB of the file in. $package_info = fread( $fp, 8192 ); fclose($fp); preg_match( '|Package Name:(.*)$|mi', $package_info, $name ); preg_match( '|Package URI:(.*)$|mi', $package_info, $uri ); preg_match( '|\n[\s]*Version:(.*)|i', $package_info, $version ); if($packages_type == 'themes' || $external){ preg_match( '|Theme:(.*)|i', $package_info, $theme ); preg_match( '|Theme version:(.*)|i', $package_info, $theme_version ); } if($packages_type == 'plugins' || $external){ preg_match( '|Plugin:(.*)|i', $package_info, $plugin ); preg_match( '|Plugin version:(.*)|i', $package_info, $plugin_version ); } preg_match( '|Description:(.*)$|mi', $package_info, $description ); preg_match( '|Author:(.*)$|mi', $package_info, $author_name ); preg_match( '|Author URI:(.*)$|mi', $package_info, $author_uri ); foreach ( array( 'name', 'uri', 'version', 'theme', 'theme_version', 'plugin', 'plugin_version', 'description', 'author_name', 'author_uri' ) as $field ) { if ( !empty( ${$field} ) ) ${$field} = _cleanup_header_comment(${$field}[1]); else ${$field} = ''; } if($name && $version){ $package_data = array( 'Name' => $name, 'URI' => $uri, 'Description' => $description, 'Author' => $author_name, 'AuthorURI' => $author_uri, 'Version' => $version ); if($packages_type == 'themes' || $external){ $package_data['Theme'] = $theme; $package_data['ThemeVersion'] = $theme_version; $package_data['id'] = $theme; } if($packages_type == 'plugins' || $external){ $package_data['Plugin'] = $plugin; $package_data['PluginVersion'] = $plugin_version; $package_data['id'] = str_replace(array('/','.php'),array('-',''),$plugin); } // add the package only if the theme is active if($external && $package_data['Plugin'] && !in_array($package_data['Plugin'], get_option('active_plugins'))){ continue; } if($external && $package_data['Theme']){ if(($package_data['Theme'] != basename(get_template_directory())) && ($package_data['Theme'] != basename(get_stylesheet_directory()))){ continue; } } if($packages_type == 'themes'){ if($package_data['Theme'] != basename(get_template_directory()) && $package_data['Theme'] != basename(get_stylesheet_directory())){ continue; } }elseif($packages_type == 'plugins'){ $plugins = get_option('active_plugins'); if (function_exists('get_site_option')){ $plugins = array_merge($plugins, array_keys(get_site_option('active_sitewide_plugins',array()))); } if(!in_array($package_data['Plugin'], $plugins)){ continue; } } $package_data['path'] = $folder . '/' . $f ; $packages[$f] = $package_data; } } } closedir($dh); }else{ throw new Exception(sprintf('Can\'t open folder %s',$folder)); } return $packages; } function _read_theme_packages(){ try{ $this->packages['themes'] = array_merge((array)$this->packages['themes'], $this->scan(ICL_EXTRAS_PACKAGES_BASE_PATH . '/themes')); }catch (Exception $e){ echo $e->getMessage(); } } function _read_plugin_packages(){ try{ $this->packages['plugins'] = array_merge((array)$this->packages['plugins'], $this->scan(ICL_EXTRAS_PACKAGES_BASE_PATH . '/plugins')); }catch (Exception $e){ echo $e->getMessage(); } } function _read_external_packages_themes(){ $external = true; // read package from the current theme if(get_template_directory() != get_stylesheet_directory()){ $folder = get_stylesheet_directory() . '/' . ICL_EXTRAS_EXTERNAL_PACKAGE_FOLDER; if(@file_exists($folder) && is_dir($folder)){ $packages = $this->scan($folder, $external); if(is_array($packages) && !empty($packages)){ $this->packages['themes'] = array_merge((array)$this->packages['themes'] , $packages); } } } $folder = get_template_directory() . '/' . ICL_EXTRAS_EXTERNAL_PACKAGE_FOLDER; if(@file_exists($folder) && is_dir($folder)){ $packages = $this->scan($folder, $external); if(is_array($packages) && !empty($packages)){ $this->packages['themes'] = array_merge((array)$this->packages['themes'] , $packages); } } } function _read_external_packages_plugins(){ $plugins = get_option('active_plugins'); if (function_exists('get_site_option')){ $plugins = array_merge($plugins, array_keys(get_site_option('active_sitewide_plugins',array()))); } if(is_array($plugins) && !empty($plugins)){ foreach($plugins as $plugin){ $exp = explode('/', $plugin); $plugin_folder = WP_PLUGIN_DIR . '/' . $exp[0] . '/' . ICL_EXTRAS_EXTERNAL_PACKAGE_FOLDER; if(@file_exists($plugin_folder) && is_dir($plugin_folder)){ $packages = $this->scan($plugin_folder, $external); if(is_array($packages) && !empty($packages)){ $this->packages['plugins'] = array_merge((array)$this->packages['plugins'] , $packages); } } } } } function get_packages(){ return $this->packages; } function load_package($type, $name){ try{ $bootstrap = $this->packages[$type][$name]['path'] . '/load.php'; if(@file_exists($bootstrap)){ @include $bootstrap; }else{ global $sitepress; $enabled = $this->get_enabled_packages(); unset($enabled[$type][$name]); $this->packages_enabled[$type][$name] = 0; $iclsettings['packages_enabled'] = $this->packages_enabled; $sitepress->save_settings($iclsettings); } }catch(Exception $e){ echo $e->getMessage(); } } function load_packages(){ global $sitepress_settings; $enabled = $this->get_enabled_packages(); foreach($this->packages as $type => $packages){ foreach($packages as $package => $package_data){ // auto-enable if(!isset($enabled[$type][$package]) && !isset($sitepress_settings['upgrade_flags']['1.5'])){ global $sitepress; $enabled[$type][$package] = 1; $this->packages_enabled[$type][$package] = 1; $iclsettings['packages_enabled'] = $this->packages_enabled; $sitepress->save_settings($iclsettings); } if($enabled[$type][$package]){ $this->load_package($type, $package); } } } } function get_enabled_packages(){ global $sitepress_settings; if(!isset($this->packages_enabled)){ $this->packages_enabled = $sitepress_settings['packages_enabled']; } return $this->packages_enabled; } function update_enabled_packages(){ global $sitepress, $sitepress_settings; $type = key($_POST['icl_packages']); $updated_packages = $_POST['icl_packages'][$type]; $enabled_packages = $this->get_enabled_packages(); if(!is_array($enabled_packages[$type])){ $enabled_packages[$type] = array(); } $iclsettings['packages_enabled'][$type] = array_merge((array)$enabled_packages[$type], (array)$updated_packages); $sitepress->save_settings($iclsettings); $this->packages_enabled[$type] = $iclsettings['packages_enabled'][$type]; $sitepress_settings['packages_enabled'][$type] = $iclsettings['packages_enabled'][$type]; } private function _render_form_title($group_name){ echo '