class-fl-builder-filesystem.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Filesystem Class.
  4. * @since 2.0.6
  5. */
  6. class FL_Filesystem {
  7. protected static $_instance = null;
  8. public static function instance() {
  9. if ( is_null( self::$_instance ) ) {
  10. $filtered = apply_filters( 'fl_filesystem_instance', null );
  11. self::$_instance = $filtered instanceof FL_Filesystem ? $filtered : new self();
  12. }
  13. return self::$_instance;
  14. }
  15. /**
  16. * file_get_contents using wp_filesystem.
  17. * @since 2.0.6
  18. */
  19. function file_get_contents( $path ) {
  20. $wp_filesystem = $this->get_filesystem();
  21. return $wp_filesystem->get_contents( $path );
  22. }
  23. /**
  24. * is_writable using wp_filesystem.
  25. * @since 2.1.2
  26. */
  27. function is_writable( $path ) {
  28. $wp_filesystem = $this->get_filesystem();
  29. return $wp_filesystem->is_writable( $path );
  30. }
  31. /**
  32. * file_put_contents using wp_filesystem.
  33. * @since 2.0.6
  34. */
  35. function file_put_contents( $path, $contents ) {
  36. $wp_filesystem = $this->get_filesystem();
  37. return $wp_filesystem->put_contents( $path, $contents, FS_CHMOD_FILE );
  38. }
  39. /**
  40. * mkdir using wp_filesystem.
  41. * @since 2.0.6
  42. */
  43. function mkdir( $path ) {
  44. $wp_filesystem = $this->get_filesystem();
  45. return $wp_filesystem->mkdir( $path );
  46. }
  47. /**
  48. * is_dir using wp_filesystem.
  49. * @since 2.0.6
  50. */
  51. function is_dir( $path ) {
  52. $wp_filesystem = $this->get_filesystem();
  53. return $wp_filesystem->is_dir( $path );
  54. }
  55. /**
  56. * dirlist using wp_filesystem.
  57. * @since 2.0.6
  58. */
  59. function dirlist( $path ) {
  60. $wp_filesystem = $this->get_filesystem();
  61. return $wp_filesystem->dirlist( $path );
  62. }
  63. /**
  64. * move using wp_filesystem.
  65. * @since 2.0.6
  66. */
  67. function move( $old, $new ) {
  68. $wp_filesystem = $this->get_filesystem();
  69. return $wp_filesystem->move( $old, $new );
  70. }
  71. /**
  72. * rmdir using wp_filesystem.
  73. * @since 2.0.6
  74. */
  75. function rmdir( $path, $recursive = false ) {
  76. $wp_filesystem = $this->get_filesystem();
  77. return $wp_filesystem->rmdir( $path, $recursive );
  78. }
  79. /**
  80. * unlink using wp_filesystem.
  81. * @since 2.0.6
  82. */
  83. function unlink( $path ) {
  84. $wp_filesystem = $this->get_filesystem();
  85. return $wp_filesystem->delete( $path );
  86. }
  87. /**
  88. * unlink using wp_filesystem.
  89. * @since 2.0.6
  90. */
  91. function file_exists( $path ) {
  92. $wp_filesystem = $this->get_filesystem();
  93. return $wp_filesystem->exists( $path );
  94. }
  95. /**
  96. * filesize using wp_filesystem.
  97. * @since 2.0.6
  98. */
  99. function filesize( $path ) {
  100. $wp_filesystem = $this->get_filesystem();
  101. return $wp_filesystem->size( $path );
  102. }
  103. /**
  104. * Return an instance of WP_Filesystem.
  105. * @since 2.0.6
  106. */
  107. function get_filesystem() {
  108. global $wp_filesystem;
  109. if ( ! $wp_filesystem || 'direct' != $wp_filesystem->method ) {
  110. require_once ABSPATH . '/wp-admin/includes/file.php';
  111. $context = apply_filters( 'request_filesystem_credentials_context', false );
  112. add_filter( 'filesystem_method', array( $this, 'filesystem_method' ) );
  113. add_filter( 'request_filesystem_credentials', array( $this, 'request_filesystem_credentials' ) );
  114. $creds = request_filesystem_credentials( site_url(), '', true, $context, null );
  115. WP_Filesystem( $creds, $context );
  116. remove_filter( 'filesystem_method', array( $this, 'filesystem_method' ) );
  117. remove_filter( 'request_filesystem_credentials', array( $this, 'FLBuilderUtils::request_filesystem_credentials' ) );
  118. }
  119. // Set the permission constants if not already set.
  120. if ( ! defined( 'FS_CHMOD_DIR' ) ) {
  121. define( 'FS_CHMOD_DIR', 0755 );
  122. }
  123. if ( ! defined( 'FS_CHMOD_FILE' ) ) {
  124. define( 'FS_CHMOD_FILE', 0644 );
  125. }
  126. return $wp_filesystem;
  127. }
  128. /**
  129. * Sets method to direct.
  130. * @since 2.0.6
  131. */
  132. function filesystem_method() {
  133. return 'direct';
  134. }
  135. /**
  136. * Sets credentials to true.
  137. * @since 2.0.6
  138. */
  139. function request_filesystem_credentials() {
  140. return true;
  141. }
  142. }
  143. /**
  144. * Setup singleton.
  145. * @since 2.0.6
  146. */
  147. function fl_builder_filesystem() {
  148. return FL_Filesystem::instance();
  149. }