Create New Item
×
Item Type
File
Folder
Item Name
×
Search file in folder and subfolders...
File Manager
/
wp-content
/
plugins
/
wp-rocket
/
inc
/
functions
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
<?php use WP_Rocket\Admin\Options; use WP_Rocket\Admin\Options_Data; use WP_Rocket\Logger\Logger; defined('ABSPATH') || exit; function get_rocket_option($option, $default = false) { $options_api = new Options('wp_rocket_'); $options = new Options_Data($options_api->get('settings', [])); return $options->get($option, $default); } function update_rocket_option($key, $value) { $options_api = new Options('wp_rocket_'); $options = new Options_Data($options_api->get('settings', [])); $options->set($key, $value); $options_api->set('settings', $options->get_options()); } function rocket_is_plugin_active($plugin) { return in_array($plugin, (array) get_option('active_plugins', []), true) || rocket_is_plugin_active_for_network($plugin); } function rocket_is_plugin_active_for_network($plugin) { if (!is_multisite()) { return false; } $plugins = get_site_option('active_sitewide_plugins'); return isset($plugins[$plugin]); } function is_rocket_post_excluded_option($option) { global $post; if (!is_object($post)) { return false; } if (is_home()) { $post_id = get_queried_object_id(); } if (is_singular() && isset($post)) { $post_id = $post->ID; } return isset($post_id) ? get_post_meta($post_id, '_rocket_exclude_' . $option, true) : false; } function is_rocket_cache_mobile() { return get_rocket_option('cache_mobile', false); } function is_rocket_generate_caching_mobile_files() { return get_rocket_option('cache_mobile', false) && get_rocket_option('do_caching_mobile_files', false); } function rocket_get_dns_prefetch_domains() { $domains = (array) get_rocket_option('dns_prefetch'); return apply_filters('rocket_dns_prefetch', $domains); } function rocket_get_ignored_parameters() { $params = ['utm_source' => 1, 'utm_medium' => 1, 'utm_campaign' => 1, 'utm_expid' => 1, 'utm_term' => 1, 'utm_content' => 1, 'mtm_source' => 1, 'mtm_medium' => 1, 'mtm_campaign' => 1, 'mtm_keyword' => 1, 'mtm_cid' => 1, 'mtm_content' => 1, 'pk_source' => 1, 'pk_medium' => 1, 'pk_campaign' => 1, 'pk_keyword' => 1, 'pk_cid' => 1, 'pk_content' => 1, 'fb_action_ids' => 1, 'fb_action_types' => 1, 'fb_source' => 1, 'fbclid' => 1, 'campaignid' => 1, 'adgroupid' => 1, 'adid' => 1, 'gclid' => 1, 'age-verified' => 1, 'ao_noptimize' => 1, 'usqp' => 1, 'cn-reloaded' => 1, '_ga' => 1, 'sscid' => 1, 'gclsrc' => 1, '_gl' => 1, 'mc_cid' => 1, 'mc_eid' => 1, '_bta_tid' => 1, '_bta_c' => 1, 'trk_contact' => 1, 'trk_msg' => 1, 'trk_module' => 1, 'trk_sid' => 1, 'gdfms' => 1, 'gdftrk' => 1, 'gdffi' => 1, '_ke' => 1, 'redirect_log_mongo_id' => 1, 'redirect_mongo_id' => 1, 'sb_referer_host' => 1, 'mkwid' => 1, 'pcrid' => 1, 'ef_id' => 1, 's_kwcid' => 1, 'msclkid' => 1, 'dm_i' => 1, 'epik' => 1, 'pp' => 1]; return apply_filters('rocket_cache_ignored_parameters', $params); } function get_rocket_cache_reject_uri($force = false) { static $uris; global $wp_rewrite; if ($force) { $uris = null; } if ($uris) { return $uris; } $uris = (array) get_rocket_option('cache_reject_uri', []); $home_root = rocket_get_home_dirname(); $home_root_escaped = preg_quote($home_root, '/'); $home_root_len = strlen($home_root); if ('' !== $home_root && $uris) { foreach ($uris as $i => $uri) { if (!preg_match('/' . $home_root_escaped . '\\(?\\//', $uri)) { unset($uris[$i]); continue; } $uris[$i] = substr($uri, $home_root_len); } } $uris[] = '/(.+/)?' . $wp_rewrite->feed_base . '/?.+/?'; $uris[] = '/(?:.+/)?embed/'; $uris = apply_filters('rocket_cache_reject_uri', $uris); $uris = array_filter($uris); if (!$uris) { return ''; } if ('' !== $home_root) { foreach ($uris as $i => $uri) { if (preg_match('/' . $home_root_escaped . '\\(?\\//', $uri)) { $uris[$i] = substr($uri, $home_root_len); } } } $uris = implode('|', $uris); if ('' !== $home_root) { $uris = $home_root . '(' . $uris . ')'; } return $uris; } function get_rocket_cache_reject_cookies() { $logged_in_cookie = explode(COOKIEHASH, LOGGED_IN_COOKIE); $logged_in_cookie = array_map('preg_quote', $logged_in_cookie); $logged_in_cookie = implode('.+', $logged_in_cookie); $cookies = get_rocket_option('cache_reject_cookies', []); $cookies[] = $logged_in_cookie; $cookies[] = 'wp-postpass_'; $cookies[] = 'wptouch_switch_toggle'; $cookies[] = 'comment_author_'; $cookies[] = 'comment_author_email_'; $cookies = (array) apply_filters('rocket_cache_reject_cookies', $cookies); $cookies = array_filter($cookies); $cookies = array_flip(array_flip($cookies)); return implode('|', $cookies); } function get_rocket_cache_mandatory_cookies() { $cookies = []; $cookies = (array) apply_filters('rocket_cache_mandatory_cookies', $cookies); $cookies = array_filter($cookies); $cookies = array_flip(array_flip($cookies)); return implode('|', $cookies); } function get_rocket_cache_dynamic_cookies() { $cookies = []; $cookies = (array) apply_filters('rocket_cache_dynamic_cookies', $cookies); $cookies = array_filter($cookies); $cookies = array_unique($cookies); return $cookies; } function get_rocket_cache_reject_ua() { $ua = get_rocket_option('cache_reject_ua', []); $ua[] = 'facebookexternalhit'; $ua = (array) apply_filters('rocket_cache_reject_ua', $ua); $ua = array_filter($ua); $ua = array_flip(array_flip($ua)); $ua = implode('|', $ua); return str_replace([' ', '\\\\ '], '\\ ', $ua); } function get_rocket_cache_query_string() { $query_strings = get_rocket_option('cache_query_strings', []); $query_strings = (array) apply_filters('rocket_cache_query_strings', $query_strings); $query_strings = array_filter($query_strings); $query_strings = array_flip(array_flip($query_strings)); return $query_strings; } function rocket_valid_key() { return true; $rocket_secret_key = get_rocket_option('secret_key'); if (!$rocket_secret_key) { return false; } $valid_details = 8 === strlen(get_rocket_option('consumer_key')) && hash_equals($rocket_secret_key, hash('crc32', get_rocket_option('consumer_email'))); if (!$valid_details) { set_transient('rocket_check_key_errors', [__('The provided license data are not valid.', 'rocket') . ' <br>' . sprintf(__('To resolve, please %1$scontact support%2$s.', 'rocket'), '<a href="https://wp-rocket.me/support/" rel="noopener noreferrer" target=_"blank">', '</a>')]); return $valid_details; } delete_transient('rocket_check_key_errors'); return $valid_details; } function rocket_check_key() { $return = rocket_valid_key(); if ($return) { rocket_delete_licence_data_file(); return $return; } Logger::info('LICENSE VALIDATION PROCESS STARTED.', ['license validation process']); $response = wp_remote_get(rocket_get_constant('WP_ROCKET_WEB_VALID'), ['timeout' => 30]); if (is_wp_error($response)) { Logger::error('License validation failed.', ['license validation process', 'request_error' => $response->get_error_messages()]); set_transient('rocket_check_key_errors', $response->get_error_messages()); return $return; } $body = wp_remote_retrieve_body($response); $json = json_decode($body); if (null === $json) { if ('' === $body) { Logger::error('License validation failed. No body available in response.', ['license validation process']); $message = __('License validation failed. Our server could not resolve the request from your website.', 'rocket') . '<br>' . sprintf(__('Try clicking %1$sSave Changes%2$s below. If the error persists, follow %3$sthis guide%4$s.', 'rocket'), '<em>', '</em>', '<a href="https://docs.wp-rocket.me/article/100-resolving-problems-with-license-validation#general">', '</a>'); set_transient('rocket_check_key_errors', [$message]); return $return; } Logger::error('License validation failed.', ['license validation process', 'response_body' => $body]); if ('NULLED' === $body) { $message = __('License validation failed. You may be using a nulled version of the plugin. Please do the following:', 'rocket') . '<ul><li>' . sprintf(__('Login to your WP Rocket %1$saccount%2$s', 'rocket'), '<a href="https://wp-rocket.me/account/" rel="noopener noreferrer" target=_"blank">', '</a>') . '</li><li>' . __('Download the zip file', 'rocket') . '<li></li>' . __('Reinstall', 'rocket') . '</li></ul>' . sprintf(__('If you do not have a WP Rocket account, please %1$spurchase a license%2$s.', 'rocket'), '<a href="https://wp-rocket.me/" rel="noopener noreferrer" target="_blank">', '</a>'); set_transient('rocket_check_key_errors', [$message]); return $return; } if ('BAD_USER' === $body) { $message = __('License validation failed. This user account does not exist in our database.', 'rocket') . '<br>' . sprintf(__('To resolve, please contact support.', 'rocket'), '<a href="https://wp-rocket.me/support/" rel="noopener noreferrer" target=_"blank">', '</a>'); set_transient('rocket_check_key_errors', [$message]); return $return; } if ('USER_BLOCKED' === $body) { $message = __('License validation failed. This user account is blocked.', 'rocket') . '<br>' . sprintf(__('Please see %1$sthis guide%2$s for more info.', 'rocket'), '<a href="https://docs.wp-rocket.me/article/100-resolving-problems-with-license-validation#errors" rel="noopener noreferrer" target=_"blank">', '</a>'); set_transient('rocket_check_key_errors', [$message]); return $return; } $message = __('License validation failed. Our server could not resolve the request from your website.', 'rocket') . '<br>' . sprintf(__('Try clicking %1$sSave Changes%2$s below. If the error persists, follow %3$sthis guide%4$s.', 'rocket'), '<em>', '</em>', '<a href="https://docs.wp-rocket.me/article/100-resolving-problems-with-license-validation#general" rel="noopener noreferrer" target=_"blank">', '</a>'); set_transient('rocket_check_key_errors', [$message]); return $return; } $rocket_options = []; $rocket_options['consumer_key'] = $json->data->consumer_key; $rocket_options['consumer_email'] = $json->data->consumer_email; if (!$json->success) { $messages = ['BAD_LICENSE' => __('Your license is not valid.', 'rocket') . '<br>' . sprintf(__('Make sure you have an active %1$sWP Rocket license%2$s.', 'rocket'), '<a href="https://wp-rocket.me/" rel="noopener noreferrer" target="_blank">', '</a>'), 'BAD_NUMBER' => __('You have added as many sites as your current license allows.', 'rocket') . '<br>' . sprintf(__('Upgrade your %1$saccount%2$s or %3$stransfer your license%2$s to this domain.', 'rocket'), '<a href="https://wp-rocket.me/account/" rel="noopener noreferrer" target=_"blank">', '</a>', '<a href="https://docs.wp-rocket.me/article/28-transfering-your-license-to-another-site" rel="noopener noreferrer" target=_"blank">'), 'BAD_SITE' => __('This website is not allowed.', 'rocket') . '<br>' . sprintf(__('Please %1$scontact support%2$s.', 'rocket'), '<a href="https://wp-rocket.me/support/" rel="noopener noreferrer" target=_"blank">', '</a>'), 'BAD_KEY' => __('This license key is not recognized.', 'rocket') . '<ul><li>' . sprintf(__('Login to your WP Rocket %1$saccount%2$s', 'rocket'), '<a href="https://wp-rocket.me/account/" rel="noopener noreferrer" target=_"blank">', '</a>') . '</li><li>' . __('Download the zip file', 'rocket') . '<li></li>' . __('Reinstall', 'rocket') . '</li></ul>' . sprintf(__('If the issue persists, please %1$scontact support%2$s.', 'rocket'), '<a href="https://wp-rocket.me/support/" rel="noopener noreferrer" target=_"blank">', '</a>')]; $rocket_options['secret_key'] = ''; set_transient('rocket_check_key_errors', [sprintf(__('License validation failed: %s', 'rocket'), $messages[$json->data->reason])]); Logger::error('License validation failed.', ['license validation process', 'response_error' => $json->data->reason]); set_transient(rocket_get_constant('WP_ROCKET_SLUG'), $rocket_options); return $rocket_options; } $rocket_options['secret_key'] = $json->data->secret_key; if (!get_rocket_option('license')) { $rocket_options['license'] = '1'; } Logger::info('License validation successful.', ['license validation process']); set_transient(rocket_get_constant('WP_ROCKET_SLUG'), $rocket_options); delete_transient('rocket_check_key_errors'); rocket_delete_licence_data_file(); return $rocket_options; } function rocket_delete_licence_data_file() { if (is_multisite()) { return; } $rocket_path = rocket_get_constant('WP_ROCKET_PATH'); if (!rocket_direct_filesystem()->exists($rocket_path . 'licence-data.php')) { return; } rocket_direct_filesystem()->delete($rocket_path . 'licence-data.php'); } function rocket_is_subfolder_install() { global $wpdb; static $subfolder_install; if (isset($subfolder_install)) { return $subfolder_install; } if (is_multisite()) { $subfolder_install = !is_subdomain_install(); } elseif (!is_null($wpdb->sitemeta)) { $subfolder_install = !$wpdb->get_var("SELECT meta_value FROM {$wpdb->sitemeta} WHERE site_id = 1 AND meta_key = 'subdomain_install'"); } else { $subfolder_install = false; } return $subfolder_install; } function rocket_get_home_dirname() { static $home_root; if (isset($home_root)) { return $home_root; } $home_root = wp_parse_url(rocket_get_main_home_url()); if (!empty($home_root['path'])) { $home_root = '/' . trim($home_root['path'], '/'); $home_root = rtrim($home_root, '/'); } else { $home_root = ''; } return $home_root; } function rocket_get_main_home_url() { static $root_url; if (isset($root_url)) { return $root_url; } if (!is_multisite() || is_main_site()) { $root_url = rocket_get_home_url('/'); return $root_url; } $current_network = get_network(); if ($current_network) { $root_url = set_url_scheme('https://' . $current_network->domain . $current_network->path); $root_url = trailingslashit($root_url); } else { $root_url = rocket_get_home_url('/'); } return $root_url; }