Няма описание

ActionScheduler_Compatibility.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Class ActionScheduler_Compatibility
  4. */
  5. class ActionScheduler_Compatibility {
  6. /**
  7. * Converts a shorthand byte value to an integer byte value.
  8. *
  9. * Wrapper for wp_convert_hr_to_bytes(), moved to load.php in WordPress 4.6 from media.php
  10. *
  11. * @link https://secure.php.net/manual/en/function.ini-get.php
  12. * @link https://secure.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
  13. *
  14. * @param string $value A (PHP ini) byte value, either shorthand or ordinary.
  15. * @return int An integer byte value.
  16. */
  17. public static function convert_hr_to_bytes( $value ) {
  18. if ( function_exists( 'wp_convert_hr_to_bytes' ) ) {
  19. return wp_convert_hr_to_bytes( $value );
  20. }
  21. $value = strtolower( trim( $value ) );
  22. $bytes = (int) $value;
  23. if ( false !== strpos( $value, 'g' ) ) {
  24. $bytes *= GB_IN_BYTES;
  25. } elseif ( false !== strpos( $value, 'm' ) ) {
  26. $bytes *= MB_IN_BYTES;
  27. } elseif ( false !== strpos( $value, 'k' ) ) {
  28. $bytes *= KB_IN_BYTES;
  29. }
  30. // Deal with large (float) values which run into the maximum integer size.
  31. return min( $bytes, PHP_INT_MAX );
  32. }
  33. /**
  34. * Attempts to raise the PHP memory limit for memory intensive processes.
  35. *
  36. * Only allows raising the existing limit and prevents lowering it.
  37. *
  38. * Wrapper for wp_raise_memory_limit(), added in WordPress v4.6.0
  39. *
  40. * @return bool|int|string The limit that was set or false on failure.
  41. */
  42. public static function raise_memory_limit() {
  43. if ( function_exists( 'wp_raise_memory_limit' ) ) {
  44. return wp_raise_memory_limit( 'admin' );
  45. }
  46. $current_limit = @ini_get( 'memory_limit' );
  47. $current_limit_int = self::convert_hr_to_bytes( $current_limit );
  48. if ( -1 === $current_limit_int ) {
  49. return false;
  50. }
  51. $wp_max_limit = WP_MAX_MEMORY_LIMIT;
  52. $wp_max_limit_int = self::convert_hr_to_bytes( $wp_max_limit );
  53. $filtered_limit = apply_filters( 'admin_memory_limit', $wp_max_limit );
  54. $filtered_limit_int = self::convert_hr_to_bytes( $filtered_limit );
  55. if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
  56. if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) {
  57. return $filtered_limit;
  58. } else {
  59. return false;
  60. }
  61. } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
  62. if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) {
  63. return $wp_max_limit;
  64. } else {
  65. return false;
  66. }
  67. }
  68. return false;
  69. }
  70. /**
  71. * Attempts to raise the PHP timeout for time intensive processes.
  72. *
  73. * Only allows raising the existing limit and prevents lowering it. Wrapper for wc_set_time_limit(), when available.
  74. *
  75. * @param int The time limit in seconds.
  76. */
  77. public static function raise_time_limit( $limit = 0 ) {
  78. if ( $limit < ini_get( 'max_execution_time' ) ) {
  79. return;
  80. }
  81. if ( function_exists( 'wc_set_time_limit' ) ) {
  82. wc_set_time_limit( $limit );
  83. } elseif ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
  84. @set_time_limit( $limit );
  85. }
  86. }
  87. }