function rocket_after_save_options($oldvalue, $value) { if (!(is_array($oldvalue) && is_array($value))) { return; } // This values do not need to clean the cache domain $removed = array('purge_cron_interval' => true, 'purge_cron_unit' => true, 'wl_plugin_name' => true, 'wl_plugin_URI' => true, 'wl_author' => true, 'wl_author_URI' => true, 'wl_description' => true, 'wl_plugin_slug' => true); // Create 2 arrays to compare $oldvalue_diff = array_diff_key($oldvalue, $removed); $value_diff = array_diff_key($value, $removed); // If it's different, clean the domain if (md5(serialize($oldvalue_diff)) !== md5(serialize($value_diff))) { // Purge all cache files rocket_clean_domain(); } // Purge all minify cache files if (!empty($_POST) && ($oldvalue['minify_css'] != $value['minify_css'] || $oldvalue['exclude_css'] != $value['exclude_css']) || (isset($oldvalue['cdn']) && !isset($value['cdn']) || !isset($oldvalue['cdn']) && isset($value['cdn']))) { rocket_clean_minify('css'); } if (!empty($_POST) && ($oldvalue['minify_js'] != $value['minify_js'] || $oldvalue['exclude_js'] != $value['exclude_js']) || (isset($oldvalue['cdn']) && !isset($value['cdn']) || !isset($oldvalue['cdn']) && isset($value['cdn']))) { rocket_clean_minify('js'); } // Update CloudFlare Development Mode if (!empty($_POST) && $oldvalue['cloudflare_devmode'] != $value['cloudflare_devmode']) { set_rocket_cloudflare_devmode((bool) $value['cloudflare_devmode']); } // Update CloudFlare settings if (!empty($_POST) && $oldvalue['cloudflare_auto_settings'] != $value['cloudflare_auto_settings']) { $cf_old_settings = explode(',', $value['cloudflare_old_settings']); // Set Cache Level to Aggressive $cf_cache_lvl = isset($cf_old_settings[0]) && $value['cloudflare_auto_settings'] == 0 ? $cf_old_settings[0] : 'agg'; set_rocket_cloudflare_cache_lvl($cf_cache_lvl); // Active Minification for HTML, CSS & JS $cf_minify = isset($cf_old_settings[1]) && $value['cloudflare_auto_settings'] == 0 ? $cf_old_settings[1] : 7; set_rocket_cloudflare_minify($cf_minify); // Deactive Rocket Loader to prevent conflicts $cf_async = isset($cf_old_settings[2]) && $value['cloudflare_auto_settings'] == 0 ? $cf_old_settings[2] : false; set_rocket_cloudflare_async($cf_async); } // Update .htaccess file rules flush_rocket_htaccess(!rocket_valid_key()); // Update config file rocket_generate_config_file(); // Set WP_CACHE constant in wp-config.php if (!defined('WP_CACHE') || !WP_CACHE) { set_rocket_wp_cache_define(true); } // Redirect on the correct page slug name to avoid false negative error message if (!empty($_POST) && $oldvalue['wl_plugin_name'] != $value['wl_plugin_name'] && isset($_POST['option_page'], $_POST['action']) && 'wp_rocket' == $_POST['option_page'] && 'update' == $_POST['action']) { add_settings_error('general', 'settings_updated', __('Settings saved.', 'rocket'), 'updated'); set_transient('settings_errors', get_settings_errors(), 30); wp_redirect(admin_url('options-general.php?page=' . sanitize_key($value['wl_plugin_name']) . '&settings-updated=true')); die; } }
function rocket_upgrader() { // Grab some infos $actual_version = get_rocket_option('version'); // You can hook the upgrader to trigger any action when WP Rocket is upgraded // first install if (!$actual_version) { do_action('wp_rocket_first_install'); } elseif (WP_ROCKET_VERSION != $actual_version) { do_action('wp_rocket_upgrade', WP_ROCKET_VERSION, $actual_version); } // If any upgrade has been done, we flush and update version # if (did_action('wp_rocket_first_install') || did_action('wp_rocket_upgrade')) { flush_rocket_htaccess(); rocket_renew_all_boxes(0, array('rocket_warning_plugin_modification')); $options = get_option(WP_ROCKET_SLUG); // do not use get_rocket_option() here $options['version'] = WP_ROCKET_VERSION; $keys = rocket_check_key('live'); if (is_array($keys)) { $options = array_merge($keys, $options); } update_option(WP_ROCKET_SLUG, $options); } else { if (empty($_POST) && rocket_valid_key()) { rocket_check_key('transient_30'); } } /** This filter is documented in inc/admin-bar.php */ if (!rocket_valid_key() && current_user_can(apply_filters('rocket_capacity', 'manage_options')) && (!isset($_GET['page']) || 'wprocket' != $_GET['page'])) { add_action('admin_notices', 'rocket_need_api_key'); } }
/** * Added or set the value of the WP_CACHE constant * * @since 2.0 * * @param bool $turn_it_on The value of WP_CACHE constant * @return void */ function set_rocket_wp_cache_define($turn_it_on) { // If WP_CACHE is already define, return to get a coffee if (!rocket_valid_key() || $turn_it_on && defined('WP_CACHE') && WP_CACHE) { return; } // Get path of the config file $config_file_path = rocket_find_wpconfig_path(); if (!$config_file_path) { return; } // Get content of the config file $config_file = file($config_file_path); // Get the value of WP_CACHE constant $turn_it_on = $turn_it_on ? 'true' : 'false'; /** * Filter allow to change the value of WP_CACHE constant * * @since 2.1 * * @param string $turn_it_on The value of WP_CACHE constant */ apply_filters('set_rocket_wp_cache_define', $turn_it_on); // Lets find out if the constant WP_CACHE is defined or not $is_wp_cache_exist = false; // Get WP_CACHE constant define $constant = "define('WP_CACHE', {$turn_it_on}); // Added by WP Rocket" . "\r\n"; foreach ($config_file as &$line) { if (!preg_match('/^define\\(\\s*\'([A-Z_]+)\',(.*)\\)/', $line, $match)) { continue; } if ($match[1] == 'WP_CACHE') { $is_wp_cache_exist = true; $line = $constant; } } unset($line); // If the constant does not exist, create it if (!$is_wp_cache_exist) { array_shift($config_file); array_unshift($config_file, "<?php\r\n", $constant); } // Insert the constant in wp-config.php file $handle = @fopen($config_file_path, 'w'); foreach ($config_file as $line) { @fwrite($handle, $line); } @fclose($handle); // Update the writing permissions of wp-config.php file $chmod = defined('FS_CHMOD_FILE') ? FS_CHMOD_FILE : 0644; @chmod($config_file_path, $chmod); }
function rocket_activation() { // Last constants define('WP_ROCKET_PLUGIN_NAME', 'WP Rocket'); define('WP_ROCKET_PLUGIN_SLUG', sanitize_key(WP_ROCKET_PLUGIN_NAME)); if (defined('SUNRISE') && SUNRISE == 'on' && function_exists('domain_mapping_siteurl')) { require WP_ROCKET_INC_PATH . 'domain-mapping.php'; } require WP_ROCKET_FUNCTIONS_PATH . 'options.php'; require WP_ROCKET_FUNCTIONS_PATH . 'files.php'; require WP_ROCKET_FUNCTIONS_PATH . 'formatting.php'; require WP_ROCKET_FUNCTIONS_PATH . 'plugins.php'; require WP_ROCKET_FUNCTIONS_PATH . 'i18n.php'; require WP_ROCKET_FUNCTIONS_PATH . 'htaccess.php'; if (rocket_valid_key()) { // Add All WP Rocket rules of the .htaccess file flush_rocket_htaccess(); // Add WP_CACHE constant in wp-config.php set_rocket_wp_cache_define(true); } // Create the cache folders (wp-rocket & min) rocket_init_cache_dir(); // Create the config folder (wp-rocket-config) rocket_init_config_dir(); // Create advanced-cache.php file rocket_generate_advanced_cache_file(); // Create config file rocket_generate_config_file(); // Update customer key & licence. wp_remote_get(WP_ROCKET_WEB_API . 'activate-licence.php', array('blocking' => false)); }
function rocket_admin_bar($wp_admin_bar) { if (!current_user_can(apply_filters('rocket_capacity', 'manage_options'))) { return; } $action = 'purge_cache'; // Parent $wp_admin_bar->add_menu(array('id' => 'wp-rocket', 'title' => WP_ROCKET_PLUGIN_NAME, 'href' => admin_url('options-general.php?page=' . WP_ROCKET_PLUGIN_SLUG))); // Settings $wp_admin_bar->add_menu(array('parent' => 'wp-rocket', 'id' => 'rocket-settings', 'title' => __('Settings', 'rocket'), 'href' => admin_url('options-general.php?page=' . WP_ROCKET_PLUGIN_SLUG))); if (rocket_valid_key()) { if (rocket_is_plugin_active('sitepress-multilingual-cms/sitepress.php')) { // Purge All $wp_admin_bar->add_menu(array('parent' => 'wp-rocket', 'id' => 'purge-all', 'title' => __('Clear cache', 'rocket'), 'href' => '#')); if ($langlinks = get_rocket_wpml_langs_for_admin_bar()) { foreach ($langlinks as $lang) { $wp_admin_bar->add_menu(array('parent' => 'purge-all', 'id' => 'purge-all-' . $lang['code'], 'title' => $lang['flag'] . ' ' . $lang['anchor'], 'href' => wp_nonce_url(admin_url('admin-post.php?action=' . $action . '&type=all&lang=' . $lang['code']), $action . '_all'))); } } } else { if (rocket_is_plugin_active('qtranslate/qtranslate.php') || rocket_is_plugin_active('polylang/polylang.php')) { // Purge All $wp_admin_bar->add_menu(array('parent' => 'wp-rocket', 'id' => 'purge-all', 'title' => __('Clear cache', 'rocket'), 'href' => '#')); // Add submenu for each active langs if (rocket_is_plugin_active('qtranslate/qtranslate.php')) { $langlinks = get_rocket_qtranslate_langs_for_admin_bar(); } else { if (rocket_is_plugin_active('polylang/polylang.php')) { $langlinks = get_rocket_polylang_langs_for_admin_bar(); } } foreach ($langlinks as $lang) { $wp_admin_bar->add_menu(array('parent' => 'purge-all', 'id' => 'purge-all-' . $lang['code'], 'title' => $lang['flag'] . ' ' . $lang['anchor'], 'href' => wp_nonce_url(admin_url('admin-post.php?action=' . $action . '&type=all&lang=' . $lang['code']), $action . '_all'))); } // Add subemnu "All langs" $wp_admin_bar->add_menu(array('parent' => 'purge-all', 'id' => 'purge-all-all', 'title' => '<div class="dashicons-before dashicons-admin-site" style="line-height:1.5"> ' . __('All languages', 'rocket') . '</div>', 'href' => wp_nonce_url(admin_url('admin-post.php?action=' . $action . '&type=all&lang=all'), $action . '_all'))); } else { // Purge All $wp_admin_bar->add_menu(array('parent' => 'wp-rocket', 'id' => 'purge-all', 'title' => __('Clear cache', 'rocket'), 'href' => wp_nonce_url(admin_url('admin-post.php?action=' . $action . '&type=all'), $action . '_all'))); } } if (is_admin()) { // Purge a post global $pagenow, $post; if ($post && 'post.php' == $pagenow && isset($_GET['action'], $_GET['post'])) { $pobject = get_post_type_object($post->post_type); $wp_admin_bar->add_menu(array('parent' => 'wp-rocket', 'id' => 'purge-post', 'title' => __('Clear this post', 'rocket'), 'href' => wp_nonce_url(admin_url('admin-post.php?action=' . $action . '&type=post-' . $post->ID), $action . '_post-' . $post->ID))); } } else { // Purge this URL (frontend) $wp_admin_bar->add_menu(array('parent' => 'wp-rocket', 'id' => 'purge-url', 'title' => __('Purge this URL', 'rocket'), 'href' => wp_nonce_url(admin_url('admin-post.php?action=' . $action . '&type=url'), $action . '_url'))); } $action = 'preload'; // Go robot gogo ! if (rocket_is_plugin_active('sitepress-multilingual-cms/sitepress.php')) { $wp_admin_bar->add_menu(array('parent' => 'wp-rocket', 'id' => 'preload-cache', 'title' => __('Preload cache', 'rocket'), 'href' => '#')); if ($langlinks = get_rocket_wpml_langs_for_admin_bar()) { foreach ($langlinks as $lang) { $wp_admin_bar->add_menu(array('parent' => 'preload-cache', 'id' => 'preload-cache-' . $lang['code'], 'title' => $lang['flag'] . ' ' . $lang['anchor'], 'href' => wp_nonce_url(admin_url('admin-post.php?action=' . $action . '&lang=' . $lang['code']), $action))); } } } else { if (rocket_is_plugin_active('qtranslate/qtranslate.php') || rocket_is_plugin_active('polylang/polylang.php')) { $wp_admin_bar->add_menu(array('parent' => 'wp-rocket', 'id' => 'preload-cache', 'title' => __('Preload cache', 'rocket'), 'href' => '#')); if (rocket_is_plugin_active('qtranslate/qtranslate.php')) { $langlinks = get_rocket_qtranslate_langs_for_admin_bar(); } else { if (rocket_is_plugin_active('polylang/polylang.php')) { $langlinks = get_rocket_polylang_langs_for_admin_bar(); } } foreach ($langlinks as $lang) { $wp_admin_bar->add_menu(array('parent' => 'preload-cache', 'id' => 'preload-cache-' . $lang['code'], 'title' => $lang['flag'] . ' ' . $lang['anchor'], 'href' => wp_nonce_url(admin_url('admin-post.php?action=' . $action . '&lang=' . $lang['code']), $action))); } $wp_admin_bar->add_menu(array('parent' => 'preload-cache', 'id' => 'preload-cache-all', 'title' => '<div class="dashicons-before dashicons-admin-site" style="line-height:1.5;"> ' . __('All languages', 'rocket') . '</div>', 'href' => wp_nonce_url(admin_url('admin-post.php?action=' . $action . '&lang=all'), $action))); } else { $wp_admin_bar->add_menu(array('parent' => 'wp-rocket', 'id' => 'preload-cache', 'title' => __('Preload cache', 'rocket'), 'href' => wp_nonce_url(admin_url('admin-post.php?action=' . $action), $action))); } } } if (!rocket_is_white_label()) { // Go to WP Rocket Documentation $wp_admin_bar->add_menu(array('parent' => 'wp-rocket', 'id' => 'docs', 'title' => __('Documentation', 'rocket'), 'href' => 'http://docs.wp-rocket.me')); // Go to WP Rocket Support $wp_admin_bar->add_menu(array('parent' => 'wp-rocket', 'id' => 'support', 'title' => __('Support', 'rocket'), 'href' => 'http://wp-rocket.me/support/')); } }
/** * Determine if the key is valid * * @since 2.2 The function do the live check and update the option */ function rocket_check_key($type = 'transient_1', $data = null) { // Recheck the license $return = rocket_valid_key(); if (!rocket_valid_key() || 'transient_1' == $type && !get_transient('rocket_check_licence_1') || 'transient_30' == $type && !get_transient('rocket_check_licence_30') || 'live' == $type) { $response = wp_remote_get(WP_ROCKET_WEB_VALID, array('timeout' => 30)); $json = !is_wp_error($response) ? json_decode($response['body']) : false; $rocket_options = array(); if ($json) { $rocket_options['consumer_key'] = $json->data->consumer_key; $rocket_options['consumer_email'] = $json->data->consumer_email; if ($json->success) { $rocket_options['secret_key'] = $json->data->secret_key; if (!get_rocket_option('license')) { $rocket_options['license'] = '1'; } if ('live' != $type) { if ('transient_1' == $type) { set_transient('rocket_check_licence_1', true, DAY_IN_SECONDS); } elseif ('transient_30' == $type) { set_transient('rocket_check_licence_30', true, DAY_IN_SECONDS * 30); } } } else { $messages = array('BAD_LICENSE' => __('Your license is not valid.', 'rocket'), 'BAD_NUMBER' => __('You cannot add more websites. Upgrade your account.', 'rocket'), 'BAD_SITE' => __('This website is not allowed.', 'rocket'), 'BAD_KEY' => __('This license key is not accepted.', 'rocket')); $rocket_options['secret_key'] = ''; add_settings_error('general', 'settings_updated', $messages[$json->data->reason], 'error'); } set_transient(WP_ROCKET_SLUG, $rocket_options); $return = (array) $rocket_options; } } return $return; }
function rocket_warning_minify_cache_dir_permissions() { /** This filter is documented in inc/admin-bar.php */ if (current_user_can(apply_filters('rocket_capacity', 'manage_options')) && !is_writable(WP_ROCKET_MINIFY_CACHE_PATH) && (get_rocket_option('minify_css', false) || get_rocket_option('minify_js', false)) && rocket_valid_key()) { $boxes = get_user_meta($GLOBALS['current_user']->ID, 'rocket_boxes', true); if (!in_array(__FUNCTION__, (array) $boxes)) { ?> <div class="error"> <a href="<?php echo wp_nonce_url(admin_url('admin-post.php?action=rocket_ignore&box=' . __FUNCTION__), 'rocket_ignore_' . __FUNCTION__); ?> " class="rkt-cross"><div class="dashicons dashicons-no"></div></a> <p><b><?php echo WP_ROCKET_PLUGIN_NAME; ?> </b>: <?php printf(__('Be careful, you don\'t have <a href="%1$s" target="_blank">writing permissions</a> on <b>%3$s</b> minified cache folder (<code>%2$s</code>). To make <b>%3$s</b> work properly, please CHMOD <code>755</code> or <code>775</code> or <code>777</code> this folder.', 'rocket'), 'http://codex.wordpress.org/Changing_File_Permissions', trim(str_replace(ABSPATH, '', WP_ROCKET_MINIFY_CACHE_PATH), '/'), WP_ROCKET_PLUGIN_NAME); ?> </p> </div> <?php } } }