Нет описания

RequirementsChecker.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace MailPoet\Config;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Util\Helpers;
  5. use MailPoet\WP\Functions as WPFunctions;
  6. use MailPoet\WP\Notice as WPNotice;
  7. class RequirementsChecker {
  8. const TEST_FOLDER_PERMISSIONS = 'TempAndCacheFolderCreation';
  9. const TEST_PDO_EXTENSION = 'PDOExtension';
  10. const TEST_XML_EXTENSION = 'XmlExtension';
  11. const TEST_VENDOR_SOURCE = 'VendorSource';
  12. public $displayErrorNotice;
  13. public $vendorClasses = [
  14. '\pQuery',
  15. '\Cron\CronExpression',
  16. '\Html2Text\Html2Text',
  17. ];
  18. public function __construct(
  19. $displayErrorNotice = true
  20. ) {
  21. $this->displayErrorNotice = $displayErrorNotice;
  22. }
  23. public function checkAllRequirements() {
  24. $availableTests = [
  25. self::TEST_PDO_EXTENSION,
  26. self::TEST_FOLDER_PERMISSIONS,
  27. self::TEST_XML_EXTENSION,
  28. self::TEST_VENDOR_SOURCE,
  29. ];
  30. $results = [];
  31. foreach ($availableTests as $test) {
  32. $callback = [$this, 'check' . $test];
  33. if (is_callable($callback)) {
  34. $results[$test] = call_user_func($callback);
  35. }
  36. }
  37. return $results;
  38. }
  39. public function checkTempAndCacheFolderCreation() {
  40. $paths = [
  41. 'temp_path' => Env::$tempPath,
  42. 'cache_path' => Env::$cachePath,
  43. ];
  44. if (!is_dir($paths['cache_path']) && !wp_mkdir_p($paths['cache_path'])) {
  45. $error = Helpers::replaceLinkTags(
  46. WPFunctions::get()->__('MailPoet requires write permissions inside the /wp-content/uploads folder. Please read our [link]instructions[/link] on how to resolve this issue.', 'mailpoet'),
  47. 'https://kb.mailpoet.com/article/152-minimum-requirements-for-mailpoet-3#folder_permissions',
  48. ['target' => '_blank']
  49. );
  50. return $this->processError($error);
  51. }
  52. foreach ($paths as $path) {
  53. $indexFile = $path . '/index.php';
  54. if (!file_exists($indexFile)) {
  55. file_put_contents(
  56. $path . '/index.php',
  57. str_replace('\n', PHP_EOL, '<?php\n\n// Silence is golden')
  58. );
  59. }
  60. }
  61. return true;
  62. }
  63. public function checkPDOExtension() {
  64. if (extension_loaded('pdo') && extension_loaded('pdo_mysql')) return true;
  65. $error = Helpers::replaceLinkTags(
  66. WPFunctions::get()->__('MailPoet requires a PDO_MYSQL PHP extension. Please read our [link]instructions[/link] on how to resolve this issue.', 'mailpoet'),
  67. 'https://kb.mailpoet.com/article/152-minimum-requirements-for-mailpoet-3#php_extension',
  68. ['target' => '_blank']
  69. );
  70. return $this->processError($error);
  71. }
  72. public function checkXmlExtension() {
  73. if (extension_loaded('xml')) return true;
  74. $error = Helpers::replaceLinkTags(
  75. WPFunctions::get()->__('MailPoet requires an XML PHP extension. Please read our [link]instructions[/link] on how to resolve this issue.', 'mailpoet'),
  76. 'https://kb.mailpoet.com/article/152-minimum-requirements-for-mailpoet-3#php_extension',
  77. ['target' => '_blank']
  78. );
  79. return $this->processError($error);
  80. }
  81. public function checkVendorSource() {
  82. foreach ($this->vendorClasses as $dependency) {
  83. $dependencyPath = $this->getDependencyPath($dependency);
  84. if (!$dependencyPath) {
  85. $error = sprintf(
  86. WPFunctions::get()->__('A MailPoet dependency (%s) does not appear to be loaded correctly, thus MailPoet will not work correctly. Please reinstall the plugin.', 'mailpoet'),
  87. $dependency
  88. );
  89. return $this->processError($error);
  90. }
  91. $pattern = '#' . preg_quote(Env::$path) . '[\\\/]#';
  92. $isLoadedByPlugin = preg_match($pattern, $dependencyPath);
  93. if (!$isLoadedByPlugin) {
  94. $error = sprintf(
  95. WPFunctions::get()->__('MailPoet has detected a dependency conflict (%s) with another plugin (%s), which may cause unexpected behavior. Please disable the offending plugin to fix this issue.', 'mailpoet'),
  96. $dependency,
  97. $dependencyPath
  98. );
  99. return $this->processError($error);
  100. }
  101. }
  102. return true;
  103. }
  104. private function getDependencyPath($namespacedClass) {
  105. try {
  106. $reflector = new \ReflectionClass($namespacedClass);
  107. return $reflector->getFileName();
  108. } catch (\ReflectionException $ex) {
  109. return false;
  110. }
  111. }
  112. public function processError($error) {
  113. if ($this->displayErrorNotice) {
  114. WPNotice::displayError($error);
  115. }
  116. return false;
  117. }
  118. }