/wp-content/plugins/w3-total-cache/lib/W3/DbCacheAdminEnvironment.php

https://gitlab.com/juanito.abelo/nlmobile · PHP · 158 lines · 88 code · 18 blank · 52 comment · 18 complexity · a5dd7b6f03fa13a05e7e1954fa9dcadc MD5 · raw file

  1. <?php
  2. /**
  3. * W3 PgCache plugin - administrative interface
  4. */
  5. if (!defined('W3TC')) {
  6. die();
  7. }
  8. /**
  9. * Class W3_DbCacheAdminEnvironment
  10. */
  11. class W3_DbCacheAdminEnvironment {
  12. /*
  13. * Fixes environment in each wp-admin request
  14. * @param W3_Config $config
  15. * @param bool $force_all_checks
  16. *
  17. * @throws SelfTestExceptions
  18. **/
  19. public function fix_on_wpadmin_request($config, $force_all_checks) {
  20. $exs = new SelfTestExceptions();
  21. try {
  22. if ($config->get_boolean('dbcache.enabled'))
  23. $this->create_addin();
  24. else
  25. $this->delete_addin();
  26. } catch (FilesystemOperationException $ex) {
  27. $exs->push($ex);
  28. }
  29. if (count($exs->exceptions()) > 0)
  30. throw $exs;
  31. }
  32. /**
  33. * Fixes environment once event occurs
  34. * @throws SelfTestExceptions
  35. **/
  36. public function fix_on_event($config, $event, $old_config = null) {
  37. if ($config->get_boolean('dbcache.enabled') &&
  38. $config->get_string('dbcache.engine') == 'file') {
  39. if (!wp_next_scheduled('w3_dbcache_cleanup')) {
  40. wp_schedule_event(time(),
  41. 'w3_dbcache_cleanup', 'w3_dbcache_cleanup');
  42. }
  43. } else {
  44. $this->unschedule();
  45. }
  46. }
  47. /**
  48. * Fixes environment after plugin deactivation
  49. * @throws SelfTestExceptions
  50. * @return array
  51. */
  52. public function fix_after_deactivation() {
  53. $exs = new SelfTestExceptions();
  54. try {
  55. $this->delete_addin();
  56. } catch (FilesystemOperationException $ex) {
  57. $exs->push($ex);
  58. }
  59. $this->unschedule();
  60. if (count($exs->exceptions()) > 0)
  61. throw $exs;
  62. }
  63. /**
  64. * Returns required rules for module
  65. * @var W3_Config $config
  66. * @return array
  67. */
  68. function get_required_rules($config) {
  69. return null;
  70. }
  71. /**
  72. * scheduling stuff
  73. **/
  74. private function unschedule() {
  75. if (wp_next_scheduled('w3_dbcache_cleanup')) {
  76. wp_clear_scheduled_hook('w3_dbcache_cleanup');
  77. }
  78. }
  79. /**
  80. * Creates add-in
  81. * @throws FilesystemOperationException
  82. */
  83. private function create_addin() {
  84. $src = W3TC_INSTALL_FILE_DB;
  85. $dst = W3TC_ADDIN_FILE_DB;
  86. if ($this->db_installed()) {
  87. if ($this->is_dbcache_add_in()) {
  88. $script_data = @file_get_contents($dst);
  89. if ($script_data == @file_get_contents($src))
  90. return;
  91. } elseif (!$this->db_check_old_add_in()) {
  92. w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/other.php');
  93. w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/ui.php');
  94. if (isset($_GET['page']))
  95. $url = 'admin.php?page=' . $_GET['page'] . '&amp;';
  96. else
  97. $url = basename(w3_remove_query($_SERVER['REQUEST_URI'])) . '?page=w3tc_dashboard&amp;';
  98. $remove_url = w3_admin_url($url . 'w3tc_default_remove_add_in=dbcache');
  99. throw new FilesystemOperationException(
  100. sprintf(__('The Database add-in file db.php is not a W3 Total Cache drop-in.
  101. Remove it or disable Database Caching. %s', 'w3-total-cache'),
  102. w3tc_button_link(__('Remove it for me', 'w3-total-cache'), wp_nonce_url($remove_url,'w3tc'))));
  103. }
  104. }
  105. w3_wp_copy_file($src, $dst);
  106. }
  107. /**
  108. * Deletes add-in
  109. * @throws FilesystemOperationException
  110. */
  111. private function delete_addin() {
  112. if ($this->is_dbcache_add_in())
  113. w3_wp_delete_file(W3TC_ADDIN_FILE_DB);
  114. }
  115. /**
  116. * Returns true if db.php is installed
  117. *
  118. * @return boolean
  119. */
  120. public function db_installed() {
  121. return file_exists(W3TC_ADDIN_FILE_DB);
  122. }
  123. /**
  124. * Returns true if db.php is old version.
  125. * @return boolean
  126. */
  127. public function db_check_old_add_in() {
  128. return (($script_data = @file_get_contents(W3TC_ADDIN_FILE_DB))
  129. && strstr($script_data, '& w3_instance') !== false);
  130. }
  131. /**
  132. * Checks if db.php is W3TC drop in
  133. *
  134. * @return boolean
  135. */
  136. public function is_dbcache_add_in() {
  137. return (($script_data = @file_get_contents(W3TC_ADDIN_FILE_DB))
  138. && strstr($script_data, 'W3 Total Cache Database module') !== false);
  139. }
  140. }