PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/admin/options.php

https://gitlab.com/BenjaminHolm/wp-rocket
PHP | 301 lines | 199 code | 40 blank | 62 comment | 89 complexity | 9ba8961bc74813ea35b8e2a5106453dd MD5 | raw file
  1. <?php
  2. use WP_Rocket\Logger\Logger;
  3. defined( 'ABSPATH' ) || die( 'Cheatin&#8217; uh?' );
  4. /**
  5. * When our settings are saved: purge, flush, preload!
  6. *
  7. * @since 1.0
  8. *
  9. * When the settins menu is hidden, redirect on the main settings page to avoid the same thing
  10. * (Only when a form is sent from our options page )
  11. *
  12. * @since 2.1
  13. *
  14. * @param array $oldvalue An array of previous values for the settings.
  15. * @param array $value An array of submitted values for the settings.
  16. */
  17. function rocket_after_save_options( $oldvalue, $value ) {
  18. if ( ! ( is_array( $oldvalue ) && is_array( $value ) ) ) {
  19. return;
  20. }
  21. // These values do not need to clean the cache domain.
  22. $removed = [
  23. 'cache_mobile' => true,
  24. 'purge_cron_interval' => true,
  25. 'purge_cron_unit' => true,
  26. 'sitemap_preload' => true,
  27. 'sitemaps' => true,
  28. 'database_revisions' => true,
  29. 'database_auto_drafts' => true,
  30. 'database_trashed_posts' => true,
  31. 'database_spam_comments' => true,
  32. 'database_trashed_comments' => true,
  33. 'database_expired_transients' => true,
  34. 'database_all_transients' => true,
  35. 'database_optimize_tables' => true,
  36. 'schedule_automatic_cleanup' => true,
  37. 'automatic_cleanup_frequency' => true,
  38. 'do_cloudflare' => true,
  39. 'cloudflare_email' => true,
  40. 'cloudflare_api_key' => true,
  41. 'cloudflare_zone_id' => true,
  42. 'cloudflare_devmode' => true,
  43. 'cloudflare_auto_settings' => true,
  44. 'cloudflare_old_settings' => true,
  45. 'heartbeat_admin_behavior' => true,
  46. 'heartbeat_editor_behavior' => true,
  47. 'varnish_auto_purge' => true,
  48. 'do_beta' => true,
  49. 'analytics_enabled' => true,
  50. 'sucury_waf_cache_sync' => true,
  51. 'sucury_waf_api_key' => true,
  52. ];
  53. // Create 2 arrays to compare.
  54. $oldvalue_diff = array_diff_key( $oldvalue, $removed );
  55. $value_diff = array_diff_key( $value, $removed );
  56. // If it's different, clean the domain.
  57. if ( md5( wp_json_encode( $oldvalue_diff ) ) !== md5( wp_json_encode( $value_diff ) ) ) {
  58. // Purge all cache files.
  59. rocket_clean_domain();
  60. wp_remote_get(
  61. home_url(),
  62. [
  63. 'timeout' => 0.01,
  64. 'blocking' => false,
  65. 'user-agent' => 'WP Rocket/Homepage Preload',
  66. 'sslverify' => apply_filters( 'https_local_ssl_verify', true ),
  67. ]
  68. );
  69. }
  70. // Purge all minify cache files.
  71. 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'] ) ) ) {
  72. rocket_clean_minify( 'css' );
  73. }
  74. 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'] ) ) ) {
  75. rocket_clean_minify( 'js' );
  76. }
  77. // Purge all cache busting files.
  78. if ( ! empty( $_POST ) && ( $oldvalue['remove_query_strings'] !== $value['remove_query_strings'] ) ) {
  79. rocket_clean_cache_busting();
  80. }
  81. // Update CloudFlare Development Mode.
  82. $cloudflare_update_result = array();
  83. if ( ! empty( $_POST ) && isset( $oldvalue['cloudflare_devmode'], $value['cloudflare_devmode'] ) && (int) $oldvalue['cloudflare_devmode'] !== (int) $value['cloudflare_devmode'] ) {
  84. $cloudflare_dev_mode_return = set_rocket_cloudflare_devmode( $value['cloudflare_devmode'] );
  85. if ( is_wp_error( $cloudflare_dev_mode_return ) ) {
  86. $cloudflare_update_result[] = array(
  87. 'result' => 'error',
  88. // translators: %s is the message returned by the CloudFlare API.
  89. 'message' => sprintf( __( 'Cloudflare development mode error: %s', 'rocket' ), $cloudflare_dev_mode_return->get_error_message() ),
  90. );
  91. } else {
  92. $cloudflare_update_result[] = array(
  93. 'result' => 'success',
  94. // translators: %s is the message returned by the CloudFlare API.
  95. 'message' => sprintf( __( 'Cloudflare development mode %s', 'rocket' ), $cloudflare_dev_mode_return ),
  96. );
  97. }
  98. }
  99. // Update CloudFlare settings.
  100. if ( ! empty( $_POST ) && ! empty( $value['do_cloudflare'] ) && isset( $oldvalue['cloudflare_auto_settings'], $value['cloudflare_auto_settings'] ) && (int) $oldvalue['cloudflare_auto_settings'] !== (int) $value['cloudflare_auto_settings'] ) {
  101. $cf_old_settings = explode( ',', $value['cloudflare_old_settings'] );
  102. // Set Cache Level to Aggressive.
  103. $cf_cache_level = ( isset( $cf_old_settings[0] ) && 0 === $value['cloudflare_auto_settings'] ) ? $cf_old_settings[0] : 'aggressive';
  104. $cf_cache_level_return = set_rocket_cloudflare_cache_level( $cf_cache_level );
  105. if ( is_wp_error( $cf_cache_level_return ) ) {
  106. $cloudflare_update_result[] = array(
  107. 'result' => 'error',
  108. // translators: %s is the message returned by the CloudFlare API.
  109. 'message' => sprintf( __( 'Cloudflare cache level error: %s', 'rocket' ), $cf_cache_level_return->get_error_message() ),
  110. );
  111. } else {
  112. if ( 'aggressive' === $cf_cache_level_return ) {
  113. $cf_cache_level_return = _x( 'Standard', 'Cloudflare caching level', 'rocket' );
  114. }
  115. $cloudflare_update_result[] = array(
  116. 'result' => 'success',
  117. // translators: %s is the caching level returned by the CloudFlare API.
  118. 'message' => sprintf( __( 'Cloudflare cache level set to %s', 'rocket' ), $cf_cache_level_return ),
  119. );
  120. }
  121. // Active Minification for HTML, CSS & JS.
  122. $cf_minify = ( isset( $cf_old_settings[1] ) && 0 === $value['cloudflare_auto_settings'] ) ? $cf_old_settings[1] : 'on';
  123. $cf_minify_return = set_rocket_cloudflare_minify( $cf_minify );
  124. if ( is_wp_error( $cf_minify_return ) ) {
  125. $cloudflare_update_result[] = array(
  126. 'result' => 'error',
  127. // translators: %s is the message returned by the CloudFlare API.
  128. 'message' => sprintf( __( 'Cloudflare minification error: %s', 'rocket' ), $cf_minify_return->get_error_message() ),
  129. );
  130. } else {
  131. $cloudflare_update_result[] = array(
  132. 'result' => 'success',
  133. // translators: %s is the message returned by the CloudFlare API.
  134. 'message' => sprintf( __( 'Cloudflare minification %s', 'rocket' ), $cf_minify_return ),
  135. );
  136. }
  137. // Deactivate Rocket Loader to prevent conflicts.
  138. $cf_rocket_loader = ( isset( $cf_old_settings[2] ) && 0 === $value['cloudflare_auto_settings'] ) ? $cf_old_settings[2] : 'off';
  139. $cf_rocket_loader_return = set_rocket_cloudflare_rocket_loader( $cf_rocket_loader );
  140. if ( is_wp_error( $cf_rocket_loader_return ) ) {
  141. $cloudflare_update_result[] = array(
  142. 'result' => 'error',
  143. // translators: %s is the message returned by the CloudFlare API.
  144. 'message' => sprintf( __( 'Cloudflare rocket loader error: %s', 'rocket' ), $cf_rocket_loader_return->get_error_message() ),
  145. );
  146. } else {
  147. $cloudflare_update_result[] = array(
  148. 'result' => 'success',
  149. // translators: %s is the message returned by the CloudFlare API.
  150. 'message' => sprintf( __( 'Cloudflare rocket loader %s', 'rocket' ), $cf_rocket_loader_return ),
  151. );
  152. }
  153. // Set Browser cache to 1 month.
  154. $cf_browser_cache_ttl = ( isset( $cf_old_settings[3] ) && 0 === $value['cloudflare_auto_settings'] ) ? $cf_old_settings[3] : '2678400';
  155. $cf_browser_cache_return = set_rocket_cloudflare_browser_cache_ttl( $cf_browser_cache_ttl );
  156. if ( is_wp_error( $cf_browser_cache_return ) ) {
  157. $cloudflare_update_result[] = array(
  158. 'result' => 'error',
  159. // translators: %s is the message returned by the CloudFlare API.
  160. 'message' => sprintf( __( 'Cloudflare browser cache error: %s', 'rocket' ), $cf_browser_cache_return->get_error_message() ),
  161. );
  162. } else {
  163. $cloudflare_update_result[] = array(
  164. 'result' => 'success',
  165. // translators: %s is the message returned by the CloudFlare API.
  166. 'message' => sprintf( __( 'Cloudflare browser cache set to %s seconds', 'rocket' ), $cf_browser_cache_return ),
  167. );
  168. }
  169. }
  170. if ( (bool) $cloudflare_update_result ) {
  171. set_transient( $GLOBALS['current_user']->ID . '_cloudflare_update_settings', $cloudflare_update_result );
  172. }
  173. // Regenerate advanced-cache.php file.
  174. if ( ! empty( $_POST ) && ( ( isset( $oldvalue['do_caching_mobile_files'] ) && ! isset( $value['do_caching_mobile_files'] ) ) || ( ! isset( $oldvalue['do_caching_mobile_files'] ) && isset( $value['do_caching_mobile_files'] ) ) || ( isset( $oldvalue['do_caching_mobile_files'], $value['do_caching_mobile_files'] ) ) && $oldvalue['do_caching_mobile_files'] !== $value['do_caching_mobile_files'] ) ) {
  175. rocket_generate_advanced_cache_file();
  176. }
  177. // Update .htaccess file rules.
  178. flush_rocket_htaccess( ! rocket_valid_key() );
  179. // Update config file.
  180. rocket_generate_config_file();
  181. // Set WP_CACHE constant in wp-config.php.
  182. if ( ! defined( 'WP_CACHE' ) || ! WP_CACHE ) {
  183. set_rocket_wp_cache_define( true );
  184. }
  185. if ( isset( $oldvalue['analytics_enabled'], $value['analytics_enabled'] ) && $oldvalue['analytics_enabled'] !== $value['analytics_enabled'] && 1 === (int) $value['analytics_enabled'] ) {
  186. set_transient( 'rocket_analytics_optin', 1 );
  187. }
  188. }
  189. add_action( 'update_option_' . WP_ROCKET_SLUG, 'rocket_after_save_options', 10, 2 );
  190. /**
  191. * When purge settings are saved we change the scheduled purge
  192. *
  193. * @since 1.0
  194. *
  195. * @param array $newvalue An array of submitted options values.
  196. * @param array $oldvalue An array of previous options values.
  197. * @return array Updated submitted options values.
  198. */
  199. function rocket_pre_main_option( $newvalue, $oldvalue ) {
  200. if ( ( isset( $newvalue['purge_cron_interval'], $oldvalue['purge_cron_interval'] ) && $newvalue['purge_cron_interval'] !== $oldvalue['purge_cron_interval'] ) || ( isset( $newvalue['purge_cron_unit'], $oldvalue['purge_cron_unit'] ) && $newvalue['purge_cron_unit'] !== $oldvalue['purge_cron_unit'] ) ) {
  201. // Clear WP Rocket cron.
  202. if ( wp_next_scheduled( 'rocket_purge_time_event' ) ) {
  203. wp_clear_scheduled_hook( 'rocket_purge_time_event' );
  204. }
  205. }
  206. // Clear WP Rocket database optimize cron if the setting has been modified.
  207. if ( ( isset( $newvalue['schedule_automatic_cleanup'], $oldvalue['schedule_automatic_cleanup'] ) && $newvalue['schedule_automatic_cleanup'] !== $oldvalue['schedule_automatic_cleanup'] ) || ( ( isset( $newvalue['automatic_cleanup_frequency'], $oldvalue['automatic_cleanup_frequency'] ) && $newvalue['automatic_cleanup_frequency'] !== $oldvalue['automatic_cleanup_frequency'] ) ) ) {
  208. if ( wp_next_scheduled( 'rocket_database_optimization_time_event' ) ) {
  209. wp_clear_scheduled_hook( 'rocket_database_optimization_time_event' );
  210. }
  211. }
  212. // Regenerate the minify key if CSS files have been modified.
  213. if ( ( isset( $newvalue['minify_css'], $oldvalue['minify_css'] ) && $newvalue['minify_css'] !== $oldvalue['minify_css'] )
  214. || ( isset( $newvalue['exclude_css'], $oldvalue['exclude_css'] ) && $newvalue['exclude_css'] !== $oldvalue['exclude_css'] )
  215. || ( isset( $oldvalue['cdn'] ) && ! isset( $newvalue['cdn'] ) || ! isset( $oldvalue['cdn'] ) && isset( $newvalue['cdn'] ) )
  216. ) {
  217. $newvalue['minify_css_key'] = create_rocket_uniqid();
  218. }
  219. // Regenerate the minify key if JS files have been modified.
  220. if ( ( isset( $newvalue['minify_js'], $oldvalue['minify_js'] ) && $newvalue['minify_js'] !== $oldvalue['minify_js'] )
  221. || ( isset( $newvalue['exclude_js'], $oldvalue['exclude_js'] ) && $newvalue['exclude_js'] !== $oldvalue['exclude_js'] )
  222. || ( isset( $oldvalue['cdn'] ) && ! isset( $newvalue['cdn'] ) || ! isset( $oldvalue['cdn'] ) && isset( $newvalue['cdn'] ) )
  223. ) {
  224. $newvalue['minify_js_key'] = create_rocket_uniqid();
  225. }
  226. // Save old CloudFlare settings.
  227. if ( ( isset( $newvalue['cloudflare_auto_settings'], $oldvalue['cloudflare_auto_settings'] ) && $newvalue['cloudflare_auto_settings'] !== $oldvalue['cloudflare_auto_settings'] && 1 === $newvalue['cloudflare_auto_settings'] ) && 0 < (int) get_rocket_option( 'do_cloudflare' ) ) {
  228. $cf_settings = get_rocket_cloudflare_settings();
  229. $newvalue['cloudflare_old_settings'] = ( ! is_wp_error( $cf_settings ) ) ? implode( ',', array_filter( $cf_settings ) ) : '';
  230. }
  231. // Checked the SSL option if the whole website is on SSL.
  232. if ( rocket_is_ssl_website() ) {
  233. $newvalue['cache_ssl'] = 1;
  234. }
  235. if ( ! defined( 'WP_ROCKET_ADVANCED_CACHE' ) ) {
  236. rocket_generate_advanced_cache_file();
  237. }
  238. $keys = get_transient( WP_ROCKET_SLUG );
  239. if ( $keys ) {
  240. delete_transient( WP_ROCKET_SLUG );
  241. $newvalue = array_merge( $newvalue, $keys );
  242. }
  243. return $newvalue;
  244. }
  245. add_filter( 'pre_update_option_' . WP_ROCKET_SLUG, 'rocket_pre_main_option', 10, 2 );
  246. /**
  247. * Auto-activate the SSL cache if the website URL is updated with https protocol
  248. *
  249. * @since 2.7
  250. *
  251. * @param array $old_value An array of previous options values.
  252. * @param array $value An array of submitted options values.
  253. */
  254. function rocket_update_ssl_option_after_save_home_url( $old_value, $value ) {
  255. if ( $old_value === $value ) {
  256. return;
  257. }
  258. $scheme = rocket_extract_url_component( $value, PHP_URL_SCHEME );
  259. update_rocket_option( 'cache_ssl', 'https' === $scheme ? 1 : 0 );
  260. }
  261. add_action( 'update_option_home', 'rocket_update_ssl_option_after_save_home_url', 10, 2 );