/** * Normalizes file name for minify * * Relative to document root! * * @param string $file * @return string */ function w3_normalize_file_minify2($file) { $file = w3_remove_query($file); $file = w3_normalize_file_minify($file); $file = w3_translate_file($file); return $file; }
/** * Imports library * * @param integer $limit * @param integer $offset * @param integer $count * @param integer $total * @param array $results * @return boolean */ function import_library($limit, $offset, &$count, &$total, &$results) { global $wpdb; $count = 0; $total = 0; $results = array(); $upload_info = w3_upload_info(); $uploads_use_yearmonth_folders = get_option('uploads_use_yearmonth_folders'); $document_root = w3_get_document_root(); @set_time_limit($this->_config->get_integer('timelimit.cdn_import')); if ($upload_info) { /** * Search for posts with links or images */ $sql = sprintf('SELECT ID, post_content, post_date FROM %sposts WHERE post_status = "publish" AND (post_type = "post" OR post_type = "page") AND (post_content LIKE "%%src=%%" OR post_content LIKE "%%href=%%") ', $wpdb->prefix); if ($limit) { $sql .= sprintf(' LIMIT %d', $limit); if ($offset) { $sql .= sprintf(' OFFSET %d', $offset); } } $posts = $wpdb->get_results($sql); if ($posts) { $count = count($posts); $total = $this->get_import_posts_count(); $regexp = '~(' . $this->get_regexp_by_mask($this->_config->get_string('cdn.import.files')) . ')$~'; $import_external = $this->_config->get_boolean('cdn.import.external'); foreach ($posts as $post) { $matches = null; $replaced = array(); $attachments = array(); $post_content = $post->post_content; /** * Search for all link and image sources */ if (preg_match_all('~(href|src)=[\'"]?([^\'"<>\\s]+)[\'"]?~', $post_content, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { list($search, $attribute, $origin) = $match; /** * Check if $search is already replaced */ if (isset($replaced[$search])) { continue; } $error = ''; $result = false; $src = w3_normalize_file_minify($origin); $dst = ''; /** * Check if file exists in the library */ if (stristr($origin, $upload_info['baseurl']) === false) { /** * Check file extension */ $check_src = $src; if (w3_is_url($check_src)) { $qpos = strpos($check_src, '?'); if ($qpos !== false) { $check_src = substr($check_src, 0, $qpos); } } if (preg_match($regexp, $check_src)) { /** * Check for already uploaded attachment */ if (isset($attachments[$src])) { list($dst, $dst_url) = $attachments[$src]; $result = true; } else { if ($uploads_use_yearmonth_folders) { $upload_subdir = date('Y/m', strtotime($post->post_date)); $upload_dir = sprintf('%s/%s', $upload_info['basedir'], $upload_subdir); $upload_url = sprintf('%s/%s', $upload_info['baseurl'], $upload_subdir); } else { $upload_subdir = ''; $upload_dir = $upload_info['basedir']; $upload_url = $upload_info['baseurl']; } $src_filename = pathinfo($src, PATHINFO_FILENAME); $src_extension = pathinfo($src, PATHINFO_EXTENSION); /** * Get available filename */ for ($i = 0;; $i++) { $dst = sprintf('%s/%s%s%s', $upload_dir, $src_filename, $i ? $i : '', $src_extension ? '.' . $src_extension : ''); if (!file_exists($dst)) { break; } } $dst_basename = basename($dst); $dst_url = sprintf('%s/%s', $upload_url, $dst_basename); $dst_path = ltrim(str_replace($document_root, '', w3_path($dst)), '/'); if ($upload_subdir) { w3_mkdir($upload_subdir, 0777, $upload_info['basedir']); } $download_result = false; /** * Check if file is remote URL */ if (w3_is_url($src)) { /** * Download file */ if ($import_external) { $download_result = w3_download($src, $dst); if (!$download_result) { $error = 'Unable to download file'; } } else { $error = 'External file import is disabled'; } } else { /** * Otherwise copy file from local path */ $src_path = $document_root . '/' . urldecode($src); if (file_exists($src_path)) { $download_result = @copy($src_path, $dst); if (!$download_result) { $error = 'Unable to copy file'; } } else { $error = 'Source file doesn\'t exists'; } } /** * Check if download or copy was successful */ if ($download_result) { w3_require_once(W3TC_INC_DIR . '/functions/mime.php'); $title = $dst_basename; $guid = ltrim($upload_info['baseurlpath'] . $title, ','); $mime_type = w3_get_mime_type($dst); $GLOBALS['wp_rewrite'] = new WP_Rewrite(); /** * Insert attachment */ $id = wp_insert_attachment(array('post_mime_type' => $mime_type, 'guid' => $guid, 'post_title' => $title, 'post_content' => '', 'post_parent' => $post->ID), $dst); if (!is_wp_error($id)) { /** * Generate attachment metadata and upload to CDN */ require_once ABSPATH . 'wp-admin/includes/image.php'; wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $dst)); $attachments[$src] = array($dst, $dst_url); $result = true; } else { $error = 'Unable to insert attachment'; } } } /** * If attachment was successfully created then replace links */ if ($result) { $replace = sprintf('%s="%s"', $attribute, $dst_url); // replace $search with $replace $post_content = str_replace($search, $replace, $post_content); $replaced[$search] = $replace; $error = 'OK'; } } else { $error = 'File type rejected'; } } else { $error = 'File already exists in the media library'; } /** * Add new entry to the log file */ $results[] = array('src' => $src, 'dst' => $dst_path, 'result' => $result, 'error' => $error); } } /** * If post content was chenged then update DB */ if ($post_content != $post->post_content) { wp_update_post(array('ID' => $post->ID, 'post_content' => $post_content)); } } } } }
/** * Options save action * * @return void */ function action_save_options() { if (!current_user_can('manage_options')) { wp_die(__('You do not have the rights to perform this action.', 'w3-total-cache')); } /** * Redirect params */ $params = array(); /** * Store error message regarding permalink not enabled */ $redirect_permalink_error = ''; /** * Read config * We should use new instance of WP_Config object here */ $config = new W3_Config(); $this->read_request($config); $config_admin = new W3_ConfigAdmin(); $this->read_request($config_admin); if ($this->_page == 'w3tc_dashboard') { if (W3_Request::get_boolean('maxcdn')) { $config->set('cdn.enabled', true); $config->set('cdn.engine', 'maxcdn'); } } /** * General tab */ if ($this->_page == 'w3tc_general') { $file_nfs = W3_Request::get_boolean('file_nfs'); $file_locking = W3_Request::get_boolean('file_locking'); $config->set('pgcache.file.nfs', $file_nfs); $config->set('minify.file.nfs', $file_nfs); $config->set('dbcache.file.locking', $file_locking); $config->set('objectcache.file.locking', $file_locking); $config->set('pgcache.file.locking', $file_locking); $config->set('minify.file.locking', $file_locking); if (is_network_admin()) { if ($this->_config->get_boolean('common.force_master') !== $config->get_boolean('common.force_master') || !w3_force_master() && $this->_config->get_boolean('common.force_master') && $config->get_boolean('common.force_master') || w3_force_master() && !$this->_config->get_boolean('common.force_master') && !$config->get_boolean('common.force_master')) { @unlink(W3TC_CACHE_BLOGMAP_FILENAME); $blogmap_dir = dirname(W3TC_CACHE_BLOGMAP_FILENAME) . '/' . basename(W3TC_CACHE_BLOGMAP_FILENAME, '.php') . '/'; if (@is_dir($blogmap_dir)) { w3_rmdir($blogmap_dir); } } if ($config->get_boolean('common.force_master')) { $config_admin->set('common.visible_by_master_only', true); } } /** * Check permalinks for page cache */ if ($config->get_boolean('pgcache.enabled') && $config->get_string('pgcache.engine') == 'file_generic' && !get_option('permalink_structure')) { $config->set('pgcache.enabled', false); $redirect_permalink_error = 'fancy_permalinks_disabled_pgcache'; } /** * Get New Relic application id */ if ($config->get_boolean('newrelic.enabled')) { $method = W3_Request::get_string('application_id_method'); $newrelic_prefix = ''; if (w3_is_network() && w3_get_blog_id() != 0) { $newrelic_prefix = $this->_config->get_string('newrelic.appname_prefix'); } if (($newrelic_api_key = $config->get_string('newrelic.api_key')) && !$config->get_string('newrelic.account_id')) { $nerser = w3_instance('W3_NewRelicService'); $account_id = $nerser->get_account_id($newrelic_api_key); $config->set('newrelic.account_id', $account_id); } if ($method == 'dropdown' && $config->get_string('newrelic.application_id')) { $application_id = $config->get_string('newrelic.application_id'); if ($config->get_string('newrelic.api_key') && $config->get_string('newrelic.account_id')) { w3_require_once(W3TC_LIB_W3_DIR . '/NewRelicService.php'); $nerser = new W3_NewRelicService($config->get_string('newrelic.api_key'), $config->get_string('newrelic.account_id')); $appname = $nerser->get_application_name($application_id); $config->set('newrelic.appname', $appname); } } else { if ($method == 'manual' && $config->get_string('newrelic.appname')) { if ($newrelic_prefix != '' && strpos($config->get_string('newrelic.appname'), $newrelic_prefix) === false) { $application_name = $newrelic_prefix . $config->get_string('newrelic.appname'); $config->set('newrelic.appname', $application_name); } else { $application_name = $config->get_string('newrelic.appname'); } if ($config->get_string('newrelic.api_key') && $config->get_string('newrelic.account_id')) { w3_require_once(W3TC_LIB_W3_DIR . '/NewRelicService.php'); $nerser = new W3_NewRelicService($config->get_string('newrelic.api_key'), $config->get_string('newrelic.account_id')); $application_id = $nerser->get_application_id($application_name); if ($application_id) { $config->set('newrelic.application_id', $application_id); } } } } } if ($config->get_boolean('minify.enabled') && !$this->_config->get_boolean('minify.enabled') || $config->get_boolean('minify.enabled') && $config->get_boolean('browsercache.enabled') && !$this->_config->get_boolean('browsercache.enabled') || $config->get_boolean('minify.enabled') && $config->get_boolean('minify.auto') && !$this->_config->get_boolean('minify.auto') || $config->get_boolean('minify.enabled') && $config->get_string('minify.engine') != $this->_config->get_string('minify.engine')) { delete_transient('w3tc_minify_tested_filename_length'); } if (!w3_is_pro($this->_config)) { delete_transient('w3tc_license_status'); } } /** * Minify tab */ if ($this->_page == 'w3tc_minify' && !$this->_config->get_boolean('minify.auto')) { $js_groups = array(); $css_groups = array(); $js_files = W3_Request::get_array('js_files'); $css_files = W3_Request::get_array('css_files'); foreach ($js_files as $theme => $templates) { foreach ($templates as $template => $locations) { foreach ((array) $locations as $location => $types) { foreach ((array) $types as $files) { foreach ((array) $files as $file) { if (!empty($file)) { $js_groups[$theme][$template][$location]['files'][] = w3_normalize_file_minify($file); } } } } } } foreach ($css_files as $theme => $templates) { foreach ($templates as $template => $locations) { foreach ((array) $locations as $location => $files) { foreach ((array) $files as $file) { if (!empty($file)) { $css_groups[$theme][$template][$location]['files'][] = w3_normalize_file_minify($file); } } } } } $config->set('minify.js.groups', $js_groups); $config->set('minify.css.groups', $css_groups); $js_theme = W3_Request::get_string('js_theme'); $css_theme = W3_Request::get_string('css_theme'); $params = array_merge($params, array('js_theme' => $js_theme, 'css_theme' => $css_theme)); } if ($this->_page == 'w3tc_minify') { if ($config->get_integer('minify.auto.filename_length') > 246) { $config->set('minify.auto.filename_length', 246); } delete_transient('w3tc_minify_tested_filename_length'); } /** * Browser Cache tab */ if ($this->_page == 'w3tc_browsercache') { if ($config->get_boolean('browsercache.enabled') && $config->get_boolean('browsercache.no404wp') && !get_option('permalink_structure')) { $config->set('browsercache.no404wp', false); $redirect_permalink_error = 'fancy_permalinks_disabled_browsercache'; } $config->set('browsercache.timestamp', time()); if (in_array($engine = $this->_config->get_string('cdn.engine'), array('netdna', 'maxcdn'))) { w3_require_once(W3TC_LIB_NETDNA_DIR . '/NetDNA.php'); $keys = explode('+', $this->_config->get_string('cdn.' . $engine . '.authorization_key')); if (sizeof($keys) == 3) { list($alias, $consumerkey, $consumersecret) = $keys; try { $api = new NetDNA($alias, $consumerkey, $consumersecret); $disable_cooker_header = $config->get_boolean('browsercache.other.nocookies') || $config->get_boolean('browsercache.cssjs.nocookies'); $api->update_pull_zone($this->_config->get_string('cdn.' . $engine . '.zone_id'), array('ignore_setcookie_header' => $disable_cooker_header)); } catch (Exception $ex) { } } } } /** * Mobile tab */ if ($this->_page == 'w3tc_mobile') { $groups = W3_Request::get_array('mobile_groups'); $mobile_groups = array(); $cached_mobile_groups = array(); foreach ($groups as $group => $group_config) { $group = strtolower($group); $group = preg_replace('~[^0-9a-z_]+~', '_', $group); $group = trim($group, '_'); if ($group) { $theme = isset($group_config['theme']) ? trim($group_config['theme']) : 'default'; $enabled = isset($group_config['enabled']) ? (bool) $group_config['enabled'] : true; $redirect = isset($group_config['redirect']) ? trim($group_config['redirect']) : ''; $agents = isset($group_config['agents']) ? explode("\r\n", trim($group_config['agents'])) : array(); $mobile_groups[$group] = array('theme' => $theme, 'enabled' => $enabled, 'redirect' => $redirect, 'agents' => $agents); $cached_mobile_groups[$group] = $agents; } } /** * Allow plugins modify WPSC mobile groups */ $cached_mobile_groups = apply_filters('cached_mobile_groups', $cached_mobile_groups); /** * Merge existent and delete removed groups */ foreach ($mobile_groups as $group => $group_config) { if (isset($cached_mobile_groups[$group])) { $mobile_groups[$group]['agents'] = (array) $cached_mobile_groups[$group]; } else { unset($mobile_groups[$group]); } } /** * Add new groups */ foreach ($cached_mobile_groups as $group => $agents) { if (!isset($mobile_groups[$group])) { $mobile_groups[$group] = array('theme' => '', 'enabled' => true, 'redirect' => '', 'agents' => $agents); } } /** * Allow plugins modify W3TC mobile groups */ $mobile_groups = apply_filters('w3tc_mobile_groups', $mobile_groups); /** * Sanitize mobile groups */ foreach ($mobile_groups as $group => $group_config) { $mobile_groups[$group] = array_merge(array('theme' => '', 'enabled' => true, 'redirect' => '', 'agents' => array()), $group_config); $mobile_groups[$group]['agents'] = array_unique($mobile_groups[$group]['agents']); $mobile_groups[$group]['agents'] = array_map('strtolower', $mobile_groups[$group]['agents']); sort($mobile_groups[$group]['agents']); } $enable_mobile = false; foreach ($mobile_groups as $group_config) { if ($group_config['enabled']) { $enable_mobile = true; break; } } $config->set('mobile.enabled', $enable_mobile); $config->set('mobile.rgroups', $mobile_groups); } /** * Referrer tab */ if ($this->_page == 'w3tc_referrer') { $groups = W3_Request::get_array('referrer_groups'); $referrer_groups = array(); foreach ($groups as $group => $group_config) { $group = strtolower($group); $group = preg_replace('~[^0-9a-z_]+~', '_', $group); $group = trim($group, '_'); if ($group) { $theme = isset($group_config['theme']) ? trim($group_config['theme']) : 'default'; $enabled = isset($group_config['enabled']) ? (bool) $group_config['enabled'] : true; $redirect = isset($group_config['redirect']) ? trim($group_config['redirect']) : ''; $referrers = isset($group_config['referrers']) ? explode("\r\n", trim($group_config['referrers'])) : array(); $referrer_groups[$group] = array('theme' => $theme, 'enabled' => $enabled, 'redirect' => $redirect, 'referrers' => $referrers); } } /** * Allow plugins modify W3TC referrer groups */ $referrer_groups = apply_filters('w3tc_referrer_groups', $referrer_groups); /** * Sanitize mobile groups */ foreach ($referrer_groups as $group => $group_config) { $referrer_groups[$group] = array_merge(array('theme' => '', 'enabled' => true, 'redirect' => '', 'referrers' => array()), $group_config); $referrer_groups[$group]['referrers'] = array_unique($referrer_groups[$group]['referrers']); $referrer_groups[$group]['referrers'] = array_map('strtolower', $referrer_groups[$group]['referrers']); sort($referrer_groups[$group]['referrers']); } $enable_referrer = false; foreach ($referrer_groups as $group_config) { if ($group_config['enabled']) { $enable_referrer = true; break; } } $config->set('referrer.enabled', $enable_referrer); $config->set('referrer.rgroups', $referrer_groups); } /** * CDN tab */ if ($this->_page == 'w3tc_cdn') { $cdn_cnames = W3_Request::get_array('cdn_cnames'); $cdn_domains = array(); foreach ($cdn_cnames as $cdn_cname) { $cdn_cname = trim($cdn_cname); /** * Auto expand wildcard domain to 10 subdomains */ $matches = null; if (preg_match('~^\\*\\.(.*)$~', $cdn_cname, $matches)) { $cdn_domains = array(); for ($i = 1; $i <= 10; $i++) { $cdn_domains[] = sprintf('cdn%d.%s', $i, $matches[1]); } break; } if ($cdn_cname) { $cdn_domains[] = $cdn_cname; } } switch ($this->_config->get_string('cdn.engine')) { case 'ftp': $config->set('cdn.ftp.domain', $cdn_domains); break; case 's3': $config->set('cdn.s3.cname', $cdn_domains); break; case 'cf': $config->set('cdn.cf.cname', $cdn_domains); break; case 'cf2': $config->set('cdn.cf2.cname', $cdn_domains); break; case 'rscf': $config->set('cdn.rscf.cname', $cdn_domains); break; case 'azure': $config->set('cdn.azure.cname', $cdn_domains); break; case 'mirror': $config->set('cdn.mirror.domain', $cdn_domains); break; case 'maxcdn': $config->set('cdn.maxcdn.domain', $cdn_domains); break; case 'netdna': $config->set('cdn.netdna.domain', $cdn_domains); break; case 'cotendo': $config->set('cdn.cotendo.domain', $cdn_domains); break; case 'edgecast': $config->set('cdn.edgecast.domain', $cdn_domains); break; case 'att': $config->set('cdn.att.domain', $cdn_domains); break; case 'akamai': $config->set('cdn.akamai.domain', $cdn_domains); break; } } w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/extensions.php'); w3_extensions_admin_init(); $all_extensions = w3_get_extensions($config); $old_extensions = $this->_config->get_array('extensions.settings', array()); foreach ($all_extensions as $extension => $descriptor) { $extension_values = W3_Request::get_as_array('extensions.settings.'); $extension_keys = array(); $extension_settings = array(); $tmp_grp = str_replace('.', '_', $extension) . '_'; foreach ($extension_values as $key => $value) { if (strpos($key, $tmp_grp) !== false) { $extension_settings[str_replace($tmp_grp, '', $key)] = $value; } } if ($extension_settings) { $old_extension_settings = isset($old_extensions[$extension]) ? $old_extensions[$extension] : array(); if (!isset($old_extensions[$extension])) { $old_extensions[$extension] = array(); } $extension_keys[$extension] = apply_filters("w3tc_save_extension_settings-{$extension}", $extension_settings, $old_extension_settings); $new_settings = array_merge($old_extensions, $extension_keys); $config->set("extensions.settings", $new_settings); $old_extensions = $config->get_array('extensions.settings', array()); } } //CloudFront does not support expires header. So disable it when its used if ($config->get_string('cdn.engine') == 'cf2') { $config->set('browsercache.cssjs.expires', false); $config->set('browsercache.html.expires', false); $config->set('browsercache.other.expires', false); } $config = apply_filters('w3tc_save_options', $config, $this->_config, $config_admin); $config = apply_filters("w3tc_save_options-{$this->_page}", $config, $this->_config, $config_admin); do_action('w3tc_saving_options', $config, $this->_config, $config_admin); do_action("w3tc_saving_options-{$this->_page}", $config, $this->_config, $config_admin); w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/admin.php'); w3_config_save($this->_config, $config, $config_admin); switch ($this->_page) { case 'w3tc_cdn': /** * Handle Set Cookie Domain */ $set_cookie_domain_old = W3_Request::get_boolean('set_cookie_domain_old'); $set_cookie_domain_new = W3_Request::get_boolean('set_cookie_domain_new'); if ($set_cookie_domain_old != $set_cookie_domain_new) { if ($set_cookie_domain_new) { if (!$this->enable_cookie_domain()) { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'enable_cookie_domain'))); } } else { if (!$this->disable_cookie_domain()) { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'disable_cookie_domain'))); } } } break; case 'w3tc_general': break; } $notes[] = 'config_save'; if ($redirect_permalink_error) { w3_admin_redirect(array('w3tc_error' => $redirect_permalink_error, 'w3tc_note' => 'config_save')); } w3_admin_redirect_with_custom_messages($params, null, $notes, true); }
/** * Exports min files to CDN * * @return array */ function get_files_minify() { $files = array(); if (W3TC_PHP5 && $this->_config->get_boolean('minify.rewrite') && (!$this->_config->get_boolean('minify.auto') || w3_is_cdn_mirror($this->_config->get_string('cdn.engine')))) { require_once W3TC_INC_DIR . '/functions/http.php'; $minify =& w3_instance('W3_Plugin_Minify'); $document_root = w3_get_document_root(); $site_root = w3_get_site_root(); $minify_root = w3_path(W3TC_CACHE_FILE_MINIFY_DIR); $minify_path = ltrim(str_replace($site_root, rtrim(w3_get_site_path(), '/'), $minify_root), '/'); $urls = $minify->get_urls(); if ($this->_config->get_string('minify.engine') == 'file') { foreach ($urls as $url) { w3_http_get($url); } $files = $this->search_files($minify_root, $minify_path, '*.css;*.js'); } else { foreach ($urls as $url) { $file = w3_normalize_file_minify($url); $file = w3_translate_file($file); if (!w3_is_url($file)) { $file = $document_root . '/' . $file; $file = ltrim(str_replace($minify_root, '', $file), '/'); $dir = dirname($file); if ($dir) { w3_mkdir($dir, 0777, $minify_root); } if (w3_download($url, $minify_root . '/' . $file) !== false) { $files[] = $minify_path . '/' . $file; } } } } } return $files; }
/** * Options save action * * @return void */ function options_save() { require_once W3TC_LIB_W3_DIR . '/Request.php'; /** * Redirect params */ $params = array(); /** * Read config * We should use new instance of WP_Config object here */ $config =& new W3_Config(); $config->read_request(); /** * General tab */ if ($this->_page == 'w3tc_general') { $file_locking = W3_Request::get_boolean('file_locking'); $config->set('dbcache.file.locking', $file_locking); $config->set('objectcache.file.locking', $file_locking); $config->set('pgcache.file.locking', $file_locking); $config->set('minify.file.locking', $file_locking); /** * Check permalinks for page cache */ if ($config->get_boolean('pgcache.enabled') && $config->get_string('pgcache.engine') == 'file_generic' && !get_option('permalink_structure')) { $this->redirect(array('w3tc_error' => 'fancy_permalinks_disabled_pgcache')); } } /** * Minify tab */ if ($this->_page == 'w3tc_minify' && !$this->_config->get_boolean('minify.auto')) { $js_groups = array(); $css_groups = array(); $js_files = W3_Request::get_array('js_files'); $css_files = W3_Request::get_array('css_files'); foreach ($js_files as $theme => $templates) { foreach ($templates as $template => $locations) { foreach ((array) $locations as $location => $files) { switch ($location) { case 'include': $js_groups[$theme][$template][$location]['blocking'] = true; break; case 'include-nb': $js_groups[$theme][$template][$location]['blocking'] = false; break; case 'include-body': $js_groups[$theme][$template][$location]['blocking'] = true; break; case 'include-body-nb': $js_groups[$theme][$template][$location]['blocking'] = false; break; case 'include-footer': $js_groups[$theme][$template][$location]['blocking'] = true; break; case 'include-footer-nb': $js_groups[$theme][$template][$location]['blocking'] = false; break; } foreach ((array) $files as $file) { if (!empty($file)) { $js_groups[$theme][$template][$location]['files'][] = w3_normalize_file_minify($file); } } } } } foreach ($css_files as $theme => $templates) { foreach ($templates as $template => $locations) { foreach ((array) $locations as $location => $files) { foreach ((array) $files as $file) { if (!empty($file)) { $css_groups[$theme][$template][$location]['files'][] = w3_normalize_file_minify($file); } } } } } $config->set('minify.js.groups', $js_groups); $config->set('minify.css.groups', $css_groups); $js_theme = W3_Request::get_string('js_theme'); $css_theme = W3_Request::get_string('css_theme'); $params = array_merge($params, array('js_theme' => $js_theme, 'css_theme' => $css_theme)); } /** * Browser Cache tab */ if ($this->_page == 'w3tc_browsercache') { if ($config->get_boolean('browsercache.enabled') && $config->get_boolean('browsercache.no404wp') && !get_option('permalink_structure')) { $this->redirect(array('w3tc_error' => 'fancy_permalinks_disabled_browsercache')); } } /** * Mobile tab */ if ($this->_page == 'w3tc_mobile') { $groups = W3_Request::get_array('mobile_groups'); $mobile_groups = array(); $cached_mobile_groups = array(); foreach ($groups as $group => $group_config) { $group = strtolower($group); $group = preg_replace('~[^0-9a-z_]+~', '_', $group); $group = trim($group, '_'); if ($group) { $theme = isset($group_config['theme']) ? trim($group_config['theme']) : 'default'; $enabled = isset($group_config['enabled']) ? (bool) $group_config['enabled'] : true; $redirect = isset($group_config['redirect']) ? trim($group_config['redirect']) : ''; $agents = isset($group_config['agents']) ? explode("\r\n", trim($group_config['agents'])) : array(); $mobile_groups[$group] = array('theme' => $theme, 'enabled' => $enabled, 'redirect' => $redirect, 'agents' => $agents); $cached_mobile_groups[$group] = $agents; } } /** * Allow plugins modify WPSC mobile groups */ $cached_mobile_groups = apply_filters('cached_mobile_groups', $cached_mobile_groups); /** * Merge existent and delete removed groups */ foreach ($mobile_groups as $group => $group_config) { if (isset($cached_mobile_groups[$group])) { $mobile_groups[$group]['agents'] = (array) $cached_mobile_groups[$group]; } else { unset($mobile_groups[$group]); } } /** * Add new groups */ foreach ($cached_mobile_groups as $group => $agents) { if (!isset($mobile_groups[$group])) { $mobile_groups[$group] = array('theme' => '', 'enabled' => true, 'redirect' => '', 'agents' => $agents); } } /** * Allow plugins modify W3TC mobile groups */ $mobile_groups = apply_filters('w3tc_mobile_groups', $mobile_groups); /** * Sanitize mobile groups */ foreach ($mobile_groups as $group => $group_config) { $mobile_groups[$group] = array_merge(array('theme' => '', 'enabled' => true, 'redirect' => '', 'agents' => array()), $group_config); $mobile_groups[$group]['agents'] = array_unique($mobile_groups[$group]['agents']); $mobile_groups[$group]['agents'] = array_map('strtolower', $mobile_groups[$group]['agents']); sort($mobile_groups[$group]['agents']); } $config->set('mobile.rgroups', $mobile_groups); } /** * Referrer tab */ if ($this->_page == 'w3tc_referrer') { $groups = W3_Request::get_array('referrer_groups'); $referrer_groups = array(); foreach ($groups as $group => $group_config) { $group = strtolower($group); $group = preg_replace('~[^0-9a-z_]+~', '_', $group); $group = trim($group, '_'); if ($group) { $theme = isset($group_config['theme']) ? trim($group_config['theme']) : 'default'; $enabled = isset($group_config['enabled']) ? (bool) $group_config['enabled'] : true; $redirect = isset($group_config['redirect']) ? trim($group_config['redirect']) : ''; $referrers = isset($group_config['referrers']) ? explode("\r\n", trim($group_config['referrers'])) : array(); $referrer_groups[$group] = array('theme' => $theme, 'enabled' => $enabled, 'redirect' => $redirect, 'referrers' => $referrers); } } /** * Allow plugins modify W3TC referrer groups */ $referrer_groups = apply_filters('w3tc_referrer_groups', $referrer_groups); /** * Sanitize mobile groups */ foreach ($referrer_groups as $group => $group_config) { $referrer_groups[$group] = array_merge(array('theme' => '', 'enabled' => true, 'redirect' => '', 'referrers' => array()), $group_config); $referrer_groups[$group]['referrers'] = array_unique($referrer_groups[$group]['referrers']); $referrer_groups[$group]['referrers'] = array_map('strtolower', $referrer_groups[$group]['referrers']); sort($referrer_groups[$group]['referrers']); } $config->set('referrer.rgroups', $referrer_groups); } /** * CDN tab */ if ($this->_page == 'w3tc_cdn') { $cdn_cnames = W3_Request::get_array('cdn_cnames'); $cdn_domains = array(); foreach ($cdn_cnames as $cdn_cname) { $cdn_cname = trim($cdn_cname); /** * Auto expand wildcard domain to 10 subdomains */ $matches = null; if (preg_match('~^\\*\\.(.*)$~', $cdn_cname, $matches)) { $cdn_domains = array(); for ($i = 1; $i <= 10; $i++) { $cdn_domains[] = sprintf('cdn%d.%s', $i, $matches[1]); } break; } if ($cdn_cname) { $cdn_domains[] = $cdn_cname; } } switch ($this->_config->get_string('cdn.engine')) { case 'mirror': $config->set('cdn.mirror.domain', $cdn_domains); break; case 'netdna': $config->set('cdn.netdna.domain', $cdn_domains); break; case 'cotendo': $config->set('cdn.cotendo.domain', $cdn_domains); break; case 'ftp': $config->set('cdn.ftp.domain', $cdn_domains); break; case 's3': $config->set('cdn.s3.cname', $cdn_domains); break; case 'cf': $config->set('cdn.cf.cname', $cdn_domains); break; case 'cf2': $config->set('cdn.cf2.cname', $cdn_domains); break; case 'rscf': $config->set('cdn.rscf.cname', $cdn_domains); break; case 'azure': $config->set('cdn.azure.cname', $cdn_domains); break; } } if ($this->config_save($this->_config, $config)) { switch ($this->_page) { case 'w3tc_cdn': /** * Handle Set Cookie Domain */ $set_cookie_domain_old = W3_Request::get_boolean('set_cookie_domain_old'); $set_cookie_domain_new = W3_Request::get_boolean('set_cookie_domain_new'); if ($set_cookie_domain_old != $set_cookie_domain_new) { if ($set_cookie_domain_new) { if (!$this->enable_cookie_domain()) { $this->redirect(array_merge($params, array('w3tc_error' => 'enable_cookie_domain'))); } } else { if (!$this->disable_cookie_domain()) { $this->redirect(array_merge($params, array('w3tc_error' => 'disable_cookie_domain'))); } } } break; case 'w3tc_general': /** * Handle CloudFlare changes */ if ($this->_config->get_boolean('cloudflare.enabled')) { $cloudflare_seclvl_old = W3_Request::get_string('cloudflare_seclvl_old'); $cloudflare_seclvl_new = W3_Request::get_string('cloudflare_seclvl_new'); $cloudflare_devmode_old = W3_Request::get_integer('cloudflare_devmode_old'); $cloudflare_devmode_new = W3_Request::get_integer('cloudflare_devmode_new'); if ($cloudflare_seclvl_old != $cloudflare_seclvl_new || $cloudflare_devmode_old != $cloudflare_devmode_new) { require_once W3TC_LIB_W3_DIR . '/CloudFlare.php'; $w3_cloudflare =& new W3_CloudFlare(array('email' => $this->_config->get_string('cloudflare.email'), 'key' => $this->_config->get_string('cloudflare.key'), 'zone' => $this->_config->get_string('cloudflare.zone'))); @set_time_limit($this->_config->get_integer('timelimit.cloudflare_api_request')); $cloudflare_response = false; if ($cloudflare_seclvl_old != $cloudflare_seclvl_new) { $cloudflare_response = $w3_cloudflare->api_request('sec_lvl', $cloudflare_seclvl_new); if (!$cloudflare_response || $cloudflare_response->result != 'success') { $this->redirect(array_merge($params, array('w3tc_error' => 'cloudflare_api_request'))); } } if ($cloudflare_devmode_old != $cloudflare_devmode_new) { $cloudflare_response = $w3_cloudflare->api_request('devmode', $cloudflare_devmode_new); if (!$cloudflare_response || $cloudflare_response->result != 'success') { $this->redirect(array_merge($params, array('w3tc_error' => 'cloudflare_api_request'))); } } } } break; } $this->redirect(array_merge($params, array('w3tc_note' => 'config_save'))); } else { $this->redirect(array_merge($params, array('w3tc_error' => 'config_save'))); } }
/** * Options save action * * @return void */ function action_save_options() { /** * Redirect params */ $params = array(); /** * Store error message regarding permalink not enabled */ $redirect_permalink_error = ''; /** * Read config * We should use new instance of WP_Config object here */ $config = new W3_Config(); if (!$this->_config->own_config_exists()) { $config->import_legacy_config(); } $this->read_request($config); $config_admin = new W3_ConfigAdmin(); $this->read_request($config_admin); if ($this->_page == 'w3tc_dashboard') { if (W3_Request::get_boolean('maxcdn')) { $config->set('cdn.enabled', true); $config->set('cdn.engine', 'maxcdn'); } } /** * General tab */ if ($this->_page == 'w3tc_general') { $file_nfs = W3_Request::get_boolean('file_nfs'); $file_locking = W3_Request::get_boolean('file_locking'); $config->set('pgcache.file.nfs', $file_nfs); $config->set('minify.file.nfs', $file_nfs); $config->set('dbcache.file.locking', $file_locking); $config->set('objectcache.file.locking', $file_locking); $config->set('pgcache.file.locking', $file_locking); $config->set('minify.file.locking', $file_locking); if ($this->is_network_and_master()) { if ($this->_config->get_boolean('common.force_master') !== $config->get_boolean('common.force_master') || !w3_force_master() && $this->_config->get_boolean('common.force_master') && $config->get_boolean('common.force_master') || w3_force_master() && !$this->_config->get_boolean('common.force_master') && !$config->get_boolean('common.force_master')) { @unlink(W3TC_CACHE_BLOGMAP_FILENAME); $blogmap_dir = dirname(W3TC_CACHE_BLOGMAP_FILENAME) . '/' . basename(W3TC_CACHE_BLOGMAP_FILENAME, '.php') . '/'; if (@is_dir($blogmap_dir)) { w3_rmdir($blogmap_dir); } } if ($config->get_boolean('common.force_master')) { $config_admin->set('common.visible_by_master_only', true); } } /** * Check permalinks for page cache */ if ($config->get_boolean('pgcache.enabled') && $config->get_string('pgcache.engine') == 'file_generic' && !get_option('permalink_structure')) { $config->set('pgcache.enabled', false); $redirect_permalink_error = 'fancy_permalinks_disabled_pgcache'; } $w3_cloudflare = w3_instance('W3_CloudFlare'); $w3_cloudflare->reset_settings_cache(); if ($config->get_boolean('cloudflare.enabled') && $w3_cloudflare->minify_enabled() && $config->get_boolean('minify.enabled')) { $config->set('minify.enabled', false); } /** * Get New Relic application id */ if ($config->get_boolean('newrelic.enabled')) { $method = W3_Request::get_string('application_id_method'); $newrelic_prefix = ''; if (w3_is_network() && w3_get_blog_id() != 0) { $newrelic_prefix = $this->_config->get_string('newrelic.appname_prefix'); } if (($newrelic_api_key = $config->get_string('newrelic.api_key')) && !$config->get_string('newrelic.account_id')) { $nerser = w3_instance('W3_NewRelicService'); $account_id = $nerser->get_account_id($newrelic_api_key); $config->set('newrelic.account_id', $account_id); } if ($method == 'dropdown' && $config->get_string('newrelic.application_id')) { $application_id = $config->get_string('newrelic.application_id'); if ($config->get_string('newrelic.api_key') && $config->get_string('newrelic.account_id')) { w3_require_once(W3TC_LIB_W3_DIR . '/NewRelicService.php'); $nerser = new W3_NewRelicService($config->get_string('newrelic.api_key'), $config->get_string('newrelic.account_id')); $appname = $nerser->get_application_name($application_id); $config->set('newrelic.appname', $appname); } } else { if ($method == 'manual' && $config->get_string('newrelic.appname')) { if ($newrelic_prefix != '' && strpos($config->get_string('newrelic.appname'), $newrelic_prefix) === false) { $application_name = $newrelic_prefix . $config->get_string('newrelic.appname'); $config->set('newrelic.appname', $application_name); } else { $application_name = $config->get_string('newrelic.appname'); } if ($config->get_string('newrelic.api_key') && $config->get_string('newrelic.account_id')) { w3_require_once(W3TC_LIB_W3_DIR . '/NewRelicService.php'); $nerser = new W3_NewRelicService($config->get_string('newrelic.api_key'), $config->get_string('newrelic.account_id')); $application_id = $nerser->get_application_id($application_name); if ($application_id) { $config->set('newrelic.application_id', $application_id); } } } } } } /** * Minify tab */ if ($this->_page == 'w3tc_minify' && !$this->_config->get_boolean('minify.auto')) { $js_groups = array(); $css_groups = array(); $js_files = W3_Request::get_array('js_files'); $css_files = W3_Request::get_array('css_files'); foreach ($js_files as $theme => $templates) { foreach ($templates as $template => $locations) { foreach ((array) $locations as $location => $types) { foreach ((array) $types as $type => $files) { foreach ((array) $files as $file) { if (!empty($file)) { $js_groups[$theme][$template][$location]['files'][] = w3_normalize_file_minify($file); } } } } } } foreach ($css_files as $theme => $templates) { foreach ($templates as $template => $locations) { foreach ((array) $locations as $location => $files) { foreach ((array) $files as $file) { if (!empty($file)) { $css_groups[$theme][$template][$location]['files'][] = w3_normalize_file_minify($file); } } } } } $config->set('minify.js.groups', $js_groups); $config->set('minify.css.groups', $css_groups); $js_theme = W3_Request::get_string('js_theme'); $css_theme = W3_Request::get_string('css_theme'); $params = array_merge($params, array('js_theme' => $js_theme, 'css_theme' => $css_theme)); } if ($this->_page == 'w3tc_minify') { if ($config->get_integer('minify.auto.filename_length') > 246) { $config->set('minify.auto.filename_length', 246); } } delete_transient('w3tc_minify_tested_filename_length'); /** * Browser Cache tab */ if ($this->_page == 'w3tc_browsercache') { if ($config->get_boolean('browsercache.enabled') && $config->get_boolean('browsercache.no404wp') && !get_option('permalink_structure')) { $config->set('browsercache.no404wp', false); $redirect_permalink_error = 'fancy_permalinks_disabled_browsercache'; } $config->set('browsercache.timestamp', time()); } /** * Mobile tab */ if ($this->_page == 'w3tc_mobile') { $groups = W3_Request::get_array('mobile_groups'); $mobile_groups = array(); $cached_mobile_groups = array(); foreach ($groups as $group => $group_config) { $group = strtolower($group); $group = preg_replace('~[^0-9a-z_]+~', '_', $group); $group = trim($group, '_'); if ($group) { $theme = isset($group_config['theme']) ? trim($group_config['theme']) : 'default'; $enabled = isset($group_config['enabled']) ? (bool) $group_config['enabled'] : true; $redirect = isset($group_config['redirect']) ? trim($group_config['redirect']) : ''; $agents = isset($group_config['agents']) ? explode("\r\n", trim($group_config['agents'])) : array(); $mobile_groups[$group] = array('theme' => $theme, 'enabled' => $enabled, 'redirect' => $redirect, 'agents' => $agents); $cached_mobile_groups[$group] = $agents; } } /** * Allow plugins modify WPSC mobile groups */ $cached_mobile_groups = apply_filters('cached_mobile_groups', $cached_mobile_groups); /** * Merge existent and delete removed groups */ foreach ($mobile_groups as $group => $group_config) { if (isset($cached_mobile_groups[$group])) { $mobile_groups[$group]['agents'] = (array) $cached_mobile_groups[$group]; } else { unset($mobile_groups[$group]); } } /** * Add new groups */ foreach ($cached_mobile_groups as $group => $agents) { if (!isset($mobile_groups[$group])) { $mobile_groups[$group] = array('theme' => '', 'enabled' => true, 'redirect' => '', 'agents' => $agents); } } /** * Allow plugins modify W3TC mobile groups */ $mobile_groups = apply_filters('w3tc_mobile_groups', $mobile_groups); /** * Sanitize mobile groups */ foreach ($mobile_groups as $group => $group_config) { $mobile_groups[$group] = array_merge(array('theme' => '', 'enabled' => true, 'redirect' => '', 'agents' => array()), $group_config); $mobile_groups[$group]['agents'] = array_unique($mobile_groups[$group]['agents']); $mobile_groups[$group]['agents'] = array_map('strtolower', $mobile_groups[$group]['agents']); sort($mobile_groups[$group]['agents']); } $enable_mobile = false; foreach ($mobile_groups as $group => $group_config) { if ($group_config['enabled']) { $enable_mobile = true; break; } } $config->set('mobile.enabled', $enable_mobile); $config->set('mobile.rgroups', $mobile_groups); } /** * Referrer tab */ if ($this->_page == 'w3tc_referrer') { $groups = W3_Request::get_array('referrer_groups'); $referrer_groups = array(); foreach ($groups as $group => $group_config) { $group = strtolower($group); $group = preg_replace('~[^0-9a-z_]+~', '_', $group); $group = trim($group, '_'); if ($group) { $theme = isset($group_config['theme']) ? trim($group_config['theme']) : 'default'; $enabled = isset($group_config['enabled']) ? (bool) $group_config['enabled'] : true; $redirect = isset($group_config['redirect']) ? trim($group_config['redirect']) : ''; $referrers = isset($group_config['referrers']) ? explode("\r\n", trim($group_config['referrers'])) : array(); $referrer_groups[$group] = array('theme' => $theme, 'enabled' => $enabled, 'redirect' => $redirect, 'referrers' => $referrers); } } /** * Allow plugins modify W3TC referrer groups */ $referrer_groups = apply_filters('w3tc_referrer_groups', $referrer_groups); /** * Sanitize mobile groups */ foreach ($referrer_groups as $group => $group_config) { $referrer_groups[$group] = array_merge(array('theme' => '', 'enabled' => true, 'redirect' => '', 'referrers' => array()), $group_config); $referrer_groups[$group]['referrers'] = array_unique($referrer_groups[$group]['referrers']); $referrer_groups[$group]['referrers'] = array_map('strtolower', $referrer_groups[$group]['referrers']); sort($referrer_groups[$group]['referrers']); } $enable_referrer = false; foreach ($referrer_groups as $group => $group_config) { if ($group_config['enabled']) { $enable_referrer = true; break; } } $config->set('referrer.enabled', $enable_referrer); $config->set('referrer.rgroups', $referrer_groups); } /** * CDN tab */ if ($this->_page == 'w3tc_cdn') { $cdn_cnames = W3_Request::get_array('cdn_cnames'); $cdn_domains = array(); foreach ($cdn_cnames as $cdn_cname) { $cdn_cname = trim($cdn_cname); /** * Auto expand wildcard domain to 10 subdomains */ $matches = null; if (preg_match('~^\\*\\.(.*)$~', $cdn_cname, $matches)) { $cdn_domains = array(); for ($i = 1; $i <= 10; $i++) { $cdn_domains[] = sprintf('cdn%d.%s', $i, $matches[1]); } break; } if ($cdn_cname) { $cdn_domains[] = $cdn_cname; } } switch ($this->_config->get_string('cdn.engine')) { case 'ftp': $config->set('cdn.ftp.domain', $cdn_domains); break; case 's3': $config->set('cdn.s3.cname', $cdn_domains); break; case 'cf': $config->set('cdn.cf.cname', $cdn_domains); break; case 'cf2': $config->set('cdn.cf2.cname', $cdn_domains); break; case 'rscf': $config->set('cdn.rscf.cname', $cdn_domains); break; case 'azure': $config->set('cdn.azure.cname', $cdn_domains); break; case 'mirror': $config->set('cdn.mirror.domain', $cdn_domains); break; case 'maxcdn': $config->set('cdn.maxcdn.domain', $cdn_domains); break; case 'netdna': $config->set('cdn.netdna.domain', $cdn_domains); break; case 'cotendo': $config->set('cdn.cotendo.domain', $cdn_domains); break; case 'edgecast': $config->set('cdn.edgecast.domain', $cdn_domains); break; case 'att': $config->set('cdn.att.domain', $cdn_domains); break; case 'akamai': $config->set('cdn.akamai.domain', $cdn_domains); break; } } $this->config_save($config, $config_admin); switch ($this->_page) { case 'w3tc_cdn': /** * Handle Set Cookie Domain */ $set_cookie_domain_old = W3_Request::get_boolean('set_cookie_domain_old'); $set_cookie_domain_new = W3_Request::get_boolean('set_cookie_domain_new'); if ($set_cookie_domain_old != $set_cookie_domain_new) { if ($set_cookie_domain_new) { if (!$this->enable_cookie_domain()) { $this->redirect(array_merge($params, array('w3tc_error' => 'enable_cookie_domain'))); } } else { if (!$this->disable_cookie_domain()) { $this->redirect(array_merge($params, array('w3tc_error' => 'disable_cookie_domain'))); } } } break; case 'w3tc_general': /** * Handle CloudFlare changes */ if ($this->_config->get_boolean('cloudflare.enabled') && (w3_get_blog_id() == 0 || w3_get_blog_id() != 0 && !$this->is_sealed('cloudflare'))) { /** * @var $w3_cloudflare W3_CloudFlare */ $w3_cloudflare = w3_instance('W3_CloudFlare'); W3_CloudFlare::clear_last_error(''); $cf_values = W3_Request::get_as_array('cloudflare_'); if (!$w3_cloudflare->save_settings($cf_values)) { $this->redirect(array_merge($params, array('w3tc_error' => 'cloudflare_api_request'))); } } break; } $this->_notes[] = 'config_save'; if ($redirect_permalink_error) { $this->redirect(array('w3tc_error' => $redirect_permalink_error, 'w3tc_note' => 'config_save')); } $this->redirect_with_custom_messages($params); }