/**
  * Puts the current sitemap output in cache
  *
  * @return bool|string bool cache file could not be written or read
  *                     string cache file's modification timestamp
  * @since 1.3.0
  * @access private
  */
 private function _cache_sitemap()
 {
     if ('yes' != $this->options['enable_cache'] || $this->_debug) {
         // cache is not enabled or debug is enabled
         return false;
     }
     if (!@is_writable($this->cache_directory)) {
         $this->log_error(sprintf(__('Cache directory <strong>%s</strong> is not writable, ' . 'no cache file was created.', $this->domain), $this->cache_directory));
         return false;
     }
     $lastmod = $this->cache->write_cache($this->output);
     if (!$lastmod) {
         $this->log_error(sprintf(__('Could not write sitemap file to cache directory <strong>%s</strong>', $this->domain), $this->cache_directory));
         return false;
     }
     return $lastmod;
 }
 function load_module($module, $sub_module)
 {
     // Assuming we fail, ugh!
     $success = false;
     // Remember to use $wpdb->prepare or $wpdb->escape when developing module
     $module = stripslashes($module);
     $sub_module = stripslashes($sub_module);
     // Check whether or not splitting is enabled and the sub_module has a 'part' part,
     // if so, remove it and pass a parameter to let the module knows that it must produce a split sitemap
     $module_part = 0;
     $module_virtual = '';
     if ('yes' == $this->options['enable_sitemap_split_post'] && preg_match('/part([0-9]+)$/i', $sub_module, $matches)) {
         $module_virtual = str_replace($matches[0], '', $sub_module);
         $module_virtual = rtrim($module_virtual, '_');
         $module_part = (int) $matches[1];
     }
     $true_sub_module = $sub_module;
     $pre_module = $module;
     $pre_module .= !empty($sub_module) ? '_' . $sub_module : '';
     // @since 1.0.1 - Redirect to correct domain, with or without www
     $this->canonical_redirect($pre_module);
     // Allowed modules
     $allowed_modules = $this->allowed_modules();
     $this->build_requested_modules($allowed_modules);
     // $this->convert_label($sub_module, $module);
     if ('sitemapindex' != $module && isset($allowed_modules[$module])) {
         if (!empty($sub_module)) {
             if (in_array($sub_module, $allowed_modules[$module]) || empty($module_virtual) && !empty($module_part) && in_array($module, $allowed_modules[$module]) || !empty($module_virtual) && in_array($module_virtual, $allowed_modules[$module])) {
                 $module_key = $module . '_' . $sub_module;
             } else {
                 $module_key = '';
             }
         } else {
             $module_key = $module;
         }
         $module_name = str_replace($sub_module, $true_sub_module, $module_key);
     } else {
         if ('sitemapindex' == $module) {
             $module_key = 'sitemapindex';
             $module_name = 'sitemapindex';
         }
     }
     // Pass the real sub_module back
     $sub_module = !empty($module_part) ? $module_virtual : $sub_module;
     if (empty($module_key)) {
         $this->elog(sprintf(__('Requested module (<em>%s</em>) not found or not allowed.', 'bwp-simple-gxs'), $pre_module), true, 404);
         $this->commit_logs();
         // If debugging is not enabled, redirect to homepage
         wp_redirect(home_url());
         exit;
     }
     // @since 1.0.1 - Start counting correct build time and queries
     timer_start();
     $this->build_stats['query'] = get_num_queries();
     // Don't let other instrusive plugins mess up our permalnks - @since 1.1.4
     remove_filter('post_link', 'syndication_permalink', 1, 3);
     remove_filter('page_link', 'suffusion_unlink_page', 10, 2);
     // If cache is enabled, we check the cache first
     if ('yes' == $this->options['enable_cache']) {
         require_once dirname(__FILE__) . '/class-bwp-gxs-cache.php';
         $bwp_gxs_cache = new BWP_GXS_CACHE(array('module' => $module_key, 'module_name' => $module_name));
         // If cache is ok, output the cached sitemap, only if debug is off
         if ('yes' != $this->options['enable_debug'] && true == $bwp_gxs_cache->has_cache) {
             $this->send_header($bwp_gxs_cache->get_header());
             $file = $bwp_gxs_cache->get_cache_file();
             // Decompress the gz file only if the server or script is not already gzipping, and gzip is enabled
             // This is to avoid double compression
             if ('yes' == $this->options['enable_gzip'] && !self::is_gzipped()) {
                 readfile($file);
             } else {
                 readgzfile($file);
             }
             // Get from cache successfully
             $this->slog(sprintf(__('Successfully served a cached version of <em>%s.xml</em>.', 'bwp-simple-gxs'), $module_name), true);
             $this->commit_logs();
             exit;
         }
     }
     // If the user uses a custom module dir, also check that dir for usable module files
     $custom_module_dir = !empty($this->options['input_alt_module_dir']) && $this->options_default['input_alt_module_dir'] != $this->options['input_alt_module_dir'] ? trailingslashit($this->options['input_alt_module_dir']) : false;
     $custom_module_dir = trailingslashit(apply_filters('bwp_gxs_module_dir', $custom_module_dir));
     // Begin loading modules
     require_once dirname(__FILE__) . '/class-bwp-gxs-module.php';
     if ('sitemapindex' != $module && isset($allowed_modules[$module])) {
         $sub_loaded = $mapped_sub_loaded = false;
         if (!empty($sub_module) && in_array($sub_module, $allowed_modules[$module])) {
             // Try to load the mapped sub-module first
             if (!empty($this->module_map[$sub_module])) {
                 $module_file = $module . '_' . $this->module_map[$sub_module] . '.php';
                 $path_custom = $custom_module_dir ? $this->uni_path_sep($custom_module_dir . $module_file) : '';
                 $path = $this->uni_path_sep($this->module_directory . $module_file);
                 if (!empty($path_custom) && @file_exists($path_custom)) {
                     $module_key = $module . '_' . $this->module_map[$sub_module];
                     include_once $path_custom;
                     $mapped_sub_loaded = true;
                     $this->nlog(sprintf(__('Loaded a custom sub-module file: <strong>%s</strong>.', 'bwp-simple-gxs'), $module_file));
                 } else {
                     if (@file_exists($path)) {
                         $module_key = $module . '_' . $this->module_map[$sub_module];
                         include_once $path;
                         $mapped_sub_loaded = true;
                     } else {
                         // Don't fire a wp_die
                         $this->nlog(sprintf(__('Mapped sub-module file: <strong>%s</strong> is not available in both default and custom module directory. The plugin will now try loading the requested sub-module instead.', 'bwp-simple-gxs'), $module_file));
                     }
                 }
             }
             if (false == $mapped_sub_loaded) {
                 $module_file = $module . '_' . $sub_module . '.php';
                 $module_key = $module . '_' . $sub_module;
                 $path_custom = $custom_module_dir ? $this->uni_path_sep($custom_module_dir . $module_file) : '';
                 $path = $this->uni_path_sep($this->module_directory . $module_file);
                 if (!empty($path_custom) && @file_exists($path_custom)) {
                     include_once $path_custom;
                     $sub_loaded = true;
                     $this->nlog(sprintf(__('Loaded a custom sub-module file: <strong>%s</strong>.', 'bwp-simple-gxs'), $module_file));
                 } else {
                     if (@file_exists($path)) {
                         include_once $path;
                         $sub_loaded = true;
                     } else {
                         // Don't fire a wp_die
                         $this->nlog(sprintf(__('Sub-module file: <strong>%s</strong> is not available in both default and custom module directory. The plugin will now try loading the parent module instead.', 'bwp-simple-gxs'), $module_file));
                     }
                 }
             }
         }
         if (false == $sub_loaded && false == $mapped_sub_loaded) {
             // Try loading the module
             $module_file = $module . '.php';
             $module_key = $module;
             $path_custom = $custom_module_dir ? $this->uni_path_sep($custom_module_dir . $module_file) : '';
             $path = $this->uni_path_sep($this->module_directory . $module_file);
             if (!empty($path_custom) && @file_exists($path_custom)) {
                 include_once $path_custom;
                 $this->nlog(sprintf(__('Loaded a custom module file: <strong>%s</strong>.', 'bwp-simple-gxs'), $module_file));
             } else {
                 if (@file_exists($path)) {
                     include_once $path;
                 } else {
                     $error_log = sprintf(__('Could not load module file: <strong>%s</strong> in both default and custom module directory. Please recheck if that module file exists.', 'bwp-simple-gxs'), $module_file);
                     $this->elog($error_log, true);
                 }
             }
         }
         $this->module_data = array('module' => $module, 'sub_module' => $sub_module, 'module_key' => $module_key, 'module_name' => $module_name, 'module_part' => $module_part);
         if (class_exists('BWP_GXS_MODULE_' . $module_key)) {
             $class_name = 'BWP_GXS_MODULE_' . $module_key;
             $the_module = new $class_name();
             if ('url' == $the_module->type) {
                 $success = $this->generate_sitemap($the_module->data);
             } else {
                 if ('news' == $the_module->type) {
                     $success = $this->generate_news_sitemap($the_module->data);
                 } else {
                     $success = $this->generate_sitemap_index($the_module->data);
                 }
             }
             unset($the_module->data);
         } else {
             $this->elog(sprintf(__('There is no class named <strong>%s</strong> in the module file <strong>%s</strong>.', 'bwp-simple-gxs'), 'BWP_GXS_MODULE_' . strtoupper($module_key), $module_file), true);
         }
     } else {
         if ('sitemapindex' == $module) {
             $module_file = 'sitemapindex.php';
             $path_custom = $custom_module_dir ? $this->uni_path_sep($custom_module_dir . $module_file) : '';
             if (!empty($path_custom) && @file_exists($path_custom)) {
                 include_once $path_custom;
                 $this->nlog(sprintf(__('Loaded a custom sitemapindex module file: <strong>%s</strong>.', 'bwp-simple-gxs'), $module_file));
             } else {
                 include_once dirname(__FILE__) . '/modules/sitemapindex.php';
             }
             if (class_exists('BWP_GXS_MODULE_INDEX')) {
                 $the_module = new BWP_GXS_MODULE_INDEX($this->requested_modules);
                 $success = $this->generate_sitemap_index($the_module->data);
                 unset($the_module->data);
             }
         }
     }
     // Output succeeded
     if (true == $success) {
         // Output additional stats
         if ('yes' == $this->options['enable_stats']) {
             $this->sitemap_stats();
         }
         // Now cache the sitemap if we have to
         if ('yes' == $this->options['enable_cache'] && true == $bwp_gxs_cache->cache_ok) {
             $bwp_gxs_cache->write_cache();
         }
         // Output the requested sitemap
         $this->output_sitemap();
         $this->slog(sprintf(__('Successfully generated <em>%s.xml</em> using module <em>%s</em>.', 'bwp-simple-gxs'), $module_name, $module_file), true);
         $this->smlog($module_name);
         $this->commit_logs();
         exit;
     } else {
         $this->commit_logs();
     }
 }