/themes/_elise/eliseCore/ReduxCore/inc/class.redux_filesystem.php

https://gitlab.com/mattswann/launch-housing · PHP · 215 lines · 164 code · 35 blank · 16 comment · 63 complexity · a1f87f2974c62f769a9d82afa1eabb51 MD5 · raw file

  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. if ( ! class_exists( 'Redux_Filesystem' ) ) {
  6. class Redux_Filesystem {
  7. private $parent = null;
  8. private $creds = array();
  9. public $fs_object = null;
  10. public function __construct( $parent ) {
  11. $parent->filesystem = $this;
  12. $this->parent = $parent;
  13. }
  14. public function ftp_form() {
  15. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
  16. echo '<div class="wrap"><div class="error"><p>';
  17. echo __( 'Unable to modify required files. Please ensure that', 'redux-framework' );
  18. echo ' <code>' . Redux_Helpers::cleanFilePath( trailingslashit( WP_CONTENT_DIR ) ) . '/uploads/</code> ';
  19. echo __( 'has the proper read/write permissions or enter your FTP information below.', 'redux-framework' );
  20. echo '</p></div><h2></h2>' . $this->parent->ftp_form . '</div>';
  21. }
  22. }
  23. function filesystem_init( $form_url, $method = '', $context = false, $fields = null ) {
  24. global $wp_filesystem;
  25. if ( ! empty( $this->creds ) ) {
  26. return true;
  27. }
  28. ob_start();
  29. /* first attempt to get credentials */
  30. if ( false === ( $this->creds = request_filesystem_credentials( $form_url, $method, false, $context ) ) ) {
  31. $this->creds = array();
  32. $this->parent->ftp_form = ob_get_contents();
  33. ob_end_clean();
  34. /**
  35. * if we comes here - we don't have credentials
  36. * so the request for them is displaying
  37. * no need for further processing
  38. **/
  39. return false;
  40. }
  41. /* now we got some credentials - try to use them*/
  42. if ( ! WP_Filesystem( $this->creds ) ) {
  43. $this->creds = array();
  44. /* incorrect connection data - ask for credentials again, now with error message */
  45. request_filesystem_credentials( $form_url, '', true, $context );
  46. $this->parent->ftp_form = ob_get_contents();
  47. ob_end_clean();
  48. return false;
  49. }
  50. return true;
  51. }
  52. public function execute( $action, $file = '', $params = '' ) {
  53. if ( empty( $this->parent->args ) ) {
  54. return;
  55. }
  56. if ( ! empty ( $params ) ) {
  57. extract( $params );
  58. }
  59. // Setup the filesystem with creds
  60. require_once( ABSPATH . '/wp-admin/includes/template.php' );
  61. require_once( ABSPATH . '/wp-admin/includes/file.php' );
  62. if ( $this->parent->args['menu_type'] == 'submenu' ) {
  63. $page_parent = $this->parent->args['page_parent'];
  64. $base = $page_parent . '?page=' . $this->parent->args['page_slug'];
  65. } else {
  66. $base = 'admin.php?page=' . $this->parent->args['page_slug'];
  67. }
  68. $url = wp_nonce_url( $base, 'redux-options' );
  69. $this->filesystem_init( $url, 'direct', dirname( $file ) );
  70. return $this->do_action( $action, $file, $params );
  71. }
  72. public function do_action( $action, $file = '', $params = '' ) {
  73. if ( ! empty ( $params ) ) {
  74. extract( $params );
  75. }
  76. global $wp_filesystem;
  77. if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) {
  78. if ( defined( 'FS_CHMOD_FILE' ) ) {
  79. $chmod = FS_CHMOD_FILE;
  80. } else {
  81. $chmod = 0644;
  82. }
  83. }
  84. $res = false;
  85. if ( ! isset( $recursive ) ) {
  86. $recursive = false;
  87. }
  88. //$target_dir = $wp_filesystem->find_folder( dirname( $file ) );
  89. // Do unique stuff
  90. if ( $action == 'mkdir' ) {
  91. if ( defined( 'FS_CHMOD_DIR' ) ) {
  92. $chmod = FS_CHMOD_DIR;
  93. } else {
  94. $chmod = 0755;
  95. }
  96. $res = $wp_filesystem->mkdir( $file );
  97. if ( ! $res ) {
  98. wp_mkdir_p( $file );
  99. $res = file_exists( $file );
  100. if ( ! $res ) {
  101. mkdir( $file, $chmod, true );
  102. $res = file_exists( $file );
  103. }
  104. }
  105. } elseif ( $action == 'rmdir' ) {
  106. $res = $wp_filesystem->rmdir( $file, $recursive );
  107. } elseif ( $action == 'copy' && ! isset( $this->filesystem->killswitch ) ) {
  108. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
  109. $res = copy( $file, $destination );
  110. if ( $res ) {
  111. chmod( $destination, $chmod );
  112. }
  113. } else {
  114. $res = $wp_filesystem->copy( $file, $destination, $overwrite, $chmod );
  115. }
  116. } elseif ( $action == 'move' && ! isset( $this->filesystem->killswitch ) ) {
  117. $res = $wp_filesystem->copy( $file, $destination, $overwrite );
  118. } elseif ( $action == 'delete' ) {
  119. $res = $wp_filesystem->delete( $file, $recursive );
  120. } elseif ( $action == 'rmdir' ) {
  121. $res = $wp_filesystem->rmdir( $file, $recursive );
  122. } elseif ( $action == 'dirlist' ) {
  123. if ( ! isset( $include_hidden ) ) {
  124. $include_hidden = true;
  125. }
  126. $res = $wp_filesystem->dirlist( $file, $include_hidden, $recursive );
  127. } elseif ( $action == 'put_contents' && ! isset( $this->filesystem->killswitch ) ) {
  128. // Write a string to a file
  129. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
  130. $res = file_put_contents( $file, $content, $chmod );
  131. if ( $res ) {
  132. chmod( $file, $chmod );
  133. }
  134. } else {
  135. $res = $wp_filesystem->put_contents( $file, $content, $chmod );
  136. }
  137. } elseif ( $action == 'chown' ) {
  138. // Changes file owner
  139. if ( isset( $owner ) && ! empty( $owner ) ) {
  140. $res = $wp_filesystem->chmod( $file, $chmod, $recursive );
  141. }
  142. } elseif ( $action == 'owner' ) {
  143. // Gets file owner
  144. $res = $wp_filesystem->owner( $file );
  145. } elseif ( $action == 'chmod' ) {
  146. if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) {
  147. $chmod = false;
  148. }
  149. $res = $wp_filesystem->chmod( $file, $chmod, $recursive );
  150. } elseif ( $action == 'get_contents' ) {
  151. // Reads entire file into a string
  152. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
  153. $res = file_get_contents( $file );
  154. } else {
  155. $res = $wp_filesystem->get_contents( $file );
  156. }
  157. } elseif ( $action == 'get_contents_array' ) {
  158. // Reads entire file into an array
  159. $res = $wp_filesystem->get_contents_array( $file );
  160. } elseif ( $action == 'object' ) {
  161. $res = $wp_filesystem;
  162. } elseif ( $action == 'unzip' ) {
  163. $unzipfile = unzip_file( $file, $destination );
  164. if ( $unzipfile ) {
  165. $res = true;
  166. }
  167. }
  168. if ( isset( $res ) && ! $res ) {
  169. $this->killswitch = true;
  170. }
  171. if ( ! $res ) {
  172. add_action( "redux/page/{$this->parent->args['opt_name']}/form/before", array(
  173. $this,
  174. 'ftp_form'
  175. ) );
  176. }
  177. return $res;
  178. }
  179. }
  180. }