Redux_Filesystem

The evolve Redux Filesystem class.

Defined (1)

The class is defined in the following location(s).

/library/admin/redux-framework/inc/class.redux_filesystem.php  
  1. class Redux_Filesystem { 
  2.  
  3. /** 
  4. * Instance of this class. 
  5. * @since 1.0.0 
  6. * @var object 
  7. */ 
  8. protected static $instance = null; 
  9.  
  10. protected static $direct = null; 
  11.  
  12. private $creds = array(); 
  13.  
  14. public $fs_object = null; 
  15.  
  16. public $parent = null; 
  17.  
  18. public function __construct() { 
  19. $this->parent->admin_notices[] = array( 
  20. 'type' => 'error',  
  21. 'msg' => '<strong>' . __( 'File Permission Issues', 'evolve' ) . '</strong><br/>' . sprintf( __( 'We were unable to modify required files. Please check your permissions, or modify your wp-config.php file to contain your FTP login credentials as <a href="%s" target="_blank">outlined here</a>.', 'evolve' ), 'https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants' ),  
  22. 'id' => 'redux-wp-login',  
  23. 'dismiss' => false,  
  24. ); 
  25.  
  26. /** 
  27. * Return an instance of this class. 
  28. * @since 1.0.0 
  29. * @return object A single instance of this class. 
  30. */ 
  31. public static function get_instance( $parent = null ) { 
  32.  
  33. // If the single instance hasn't been set, set it now. 
  34. if ( null == self::$instance ) { 
  35. self::$instance = new self; 
  36.  
  37. if ( $parent !== null ) { 
  38. self::$instance->parent = $parent; 
  39.  
  40. return self::$instance; 
  41.  
  42. public function ftp_form() { 
  43. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) { 
  44. echo '<div class="wrap"><div class="error"><p>'; 
  45. echo '<strong>' . __( 'File Permission Issues', 'evolve' ) . '</strong><br/>' . sprintf( __( 'We were unable to modify required files. Please ensure that <code>%1s</code> has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as <a href="%2s" target="_blank">outlined here</a>.', 'evolve' ), Redux_Helpers::cleanFilePath( trailingslashit( WP_CONTENT_DIR ) ) . '/uploads/', 'https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants' ); 
  46. echo '</p></div><h2></h2>' . '</div>'; 
  47.  
  48. function filesystem_init( $form_url, $method = '', $context = false, $fields = null ) { 
  49. global $wp_filesystem; 
  50.  
  51. if ( ! empty( $this->creds ) ) { 
  52. return true; 
  53.  
  54. ob_start(); 
  55.  
  56. /** first attempt to get credentials */ 
  57. if ( false === ( $this->creds = request_filesystem_credentials( $form_url, $method, false, $context ) ) ) { 
  58. $this->creds = array(); 
  59. $this->parent->ftp_form = ob_get_contents(); 
  60. ob_end_clean(); 
  61.  
  62. /** 
  63. * if we comes here - we don't have credentials 
  64. * so the request for them is displaying 
  65. * no need for further processing 
  66. **/ 
  67.  
  68. return false; 
  69.  
  70. /** now we got some credentials - try to use them*/ 
  71. if ( ! WP_Filesystem( $this->creds ) ) { 
  72. $this->creds = array(); 
  73. /** incorrect connection data - ask for credentials again, now with error message */ 
  74. request_filesystem_credentials( $form_url, '', true, $context ); 
  75. $this->parent->ftp_form = ob_get_contents(); 
  76. ob_end_clean(); 
  77.  
  78. return false; 
  79.  
  80. return true; 
  81.  
  82.  
  83. public static function load_direct() { 
  84. if ( self::$direct === null ) { 
  85. require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php'; 
  86. require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php'; 
  87. self::$direct = new WP_Filesystem_Direct( array() ); 
  88.  
  89. public function execute( $action, $file = '', $params = '' ) { 
  90.  
  91. if ( empty( $this->parent->args ) ) { 
  92. return; 
  93.  
  94. if ( ! empty ( $params ) ) { 
  95. extract( $params ); 
  96.  
  97. if ( ! is_dir( ReduxFramework::$_upload_dir ) ) { 
  98. wp_mkdir_p( ReduxFramework::$_upload_dir ); 
  99.  
  100. // Setup the filesystem with creds 
  101. require_once ABSPATH . '/wp-admin/includes/template.php'; 
  102.  
  103. require_once ABSPATH . '/wp-includes/pluggable.php'; 
  104. require_once ABSPATH . '/wp-admin/includes/file.php'; 
  105.  
  106. if ( $this->parent->args['menu_type'] == 'submenu' ) { 
  107. $page_parent = $this->parent->args['page_parent']; 
  108. $base = $page_parent . '?page=' . $this->parent->args['page_slug']; 
  109. } else { 
  110. $base = 'admin.php?page=' . $this->parent->args['page_slug']; 
  111.  
  112. $url = wp_nonce_url( $base, 'redux-options' ); 
  113.  
  114. $this->filesystem_init( $url, 'direct', dirname( $file ) ); 
  115.  
  116. return $this->do_action( $action, $file, $params ); 
  117.  
  118. public function do_action( $action, $file = '', $params = '' ) { 
  119.  
  120. if ( ! empty ( $params ) ) { 
  121. extract( $params ); 
  122.  
  123. global $wp_filesystem; 
  124.  
  125. if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) { 
  126. if ( defined( 'FS_CHMOD_FILE' ) ) { 
  127. $chmod = FS_CHMOD_FILE; 
  128. } else { 
  129. $chmod = 0644; 
  130. $res = false; 
  131. if ( ! isset( $recursive ) ) { 
  132. $recursive = false; 
  133.  
  134. //$target_dir = $wp_filesystem->find_folder( dirname( $file ) ); 
  135.  
  136. // Do unique stuff 
  137. if ( $action == 'mkdir' ) { 
  138.  
  139. if ( defined( 'FS_CHMOD_DIR' ) ) { 
  140. $chmod = FS_CHMOD_DIR; 
  141. } else { 
  142. $chmod = 0755; 
  143. $res = $wp_filesystem->mkdir( $file ); 
  144. if ( ! $res ) { 
  145. wp_mkdir_p( $file ); 
  146.  
  147. $res = file_exists( $file ); 
  148. if ( ! $res ) { 
  149. mkdir( $file, $chmod, true ); 
  150. $res = file_exists( $file ); 
  151. } elseif ( $action == 'rmdir' ) { 
  152. $res = $wp_filesystem->rmdir( $file, $recursive ); 
  153. } elseif ( $action == 'copy' && ! isset( $this->filesystem->killswitch ) ) { 
  154. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) { 
  155. $res = copy( $file, $destination ); 
  156. if ( $res ) { 
  157. chmod( $destination, $chmod ); 
  158. } else { 
  159. $res = $wp_filesystem->copy( $file, $destination, $overwrite, $chmod ); 
  160. } elseif ( $action == 'move' && ! isset( $this->filesystem->killswitch ) ) { 
  161. $res = $wp_filesystem->copy( $file, $destination, $overwrite ); 
  162. } elseif ( $action == 'delete' ) { 
  163. $res = $wp_filesystem->delete( $file, $recursive ); 
  164. } elseif ( $action == 'rmdir' ) { 
  165. $res = $wp_filesystem->rmdir( $file, $recursive ); 
  166. } elseif ( $action == 'dirlist' ) { 
  167. if ( ! isset( $include_hidden ) ) { 
  168. $include_hidden = true; 
  169. $res = $wp_filesystem->dirlist( $file, $include_hidden, $recursive ); 
  170. } elseif ( $action == 'put_contents' && ! isset( $this->filesystem->killswitch ) ) { 
  171. // Write a string to a file 
  172. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) { 
  173. self::load_direct(); 
  174. $res = self::$direct->put_contents( $file, $content, $chmod ); 
  175. } else { 
  176. $res = $wp_filesystem->put_contents( $file, $content, $chmod ); 
  177. } elseif ( $action == 'chown' ) { 
  178. // Changes file owner 
  179. if ( isset( $owner ) && ! empty( $owner ) ) { 
  180. $res = $wp_filesystem->chmod( $file, $chmod, $recursive ); 
  181. } elseif ( $action == 'owner' ) { 
  182. // Gets file owner 
  183. $res = $wp_filesystem->owner( $file ); 
  184. } elseif ( $action == 'chmod' ) { 
  185.  
  186. if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) { 
  187. $chmod = false; 
  188.  
  189. $res = $wp_filesystem->chmod( $file, $chmod, $recursive ); 
  190.  
  191. } elseif ( $action == 'get_contents' ) { 
  192. // Reads entire file into a string 
  193. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) { 
  194. self::load_direct(); 
  195. $res = self::$direct->get_contents( $file ); 
  196. } else { 
  197. $res = $wp_filesystem->get_contents( $file ); 
  198. } elseif ( $action == 'get_contents_array' ) { 
  199. // Reads entire file into an array 
  200. $res = $wp_filesystem->get_contents_array( $file ); 
  201. } elseif ( $action == 'object' ) { 
  202. $res = $wp_filesystem; 
  203. } elseif ( $action == 'unzip' ) { 
  204. $unzipfile = unzip_file( $file, $destination ); 
  205. if ( $unzipfile ) { 
  206. $res = true; 
  207.  
  208. if ( ! $res ) { 
  209. if ($action == 'dirlist') { 
  210. if (empty($res) || $res == false || $res == '' ) { 
  211. return; 
  212.  
  213. if (is_array($res) && empty($res)) { 
  214. return; 
  215.  
  216. if (!is_array($res)) { 
  217. if (count(glob("$file*")) == 0) { 
  218. return; 
  219.  
  220. $this->killswitch = true; 
  221. $this->parent->admin_notices[] = array( 
  222. 'type' => 'error',  
  223. 'msg' => '<strong>' . __( 'File Permission Issues', 'evolve' ) . '</strong><br/>' . sprintf( __( 'We were unable to modify required files. Please ensure that <code>%1s</code> has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as <a href="%2s" target="_blank">outlined here</a>.', 'evolve' ), Redux_Helpers::cleanFilePath( trailingslashit( WP_CONTENT_DIR ) ) . '/uploads/', 'https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants' ),  
  224. 'id' => 'redux-wp-login',  
  225. 'dismiss' => false,  
  226. ); 
  227. //add_action( "redux/page/{$this->parent->args['opt_name']}/form/before", array( 
  228. // $this,  
  229. // 'ftp_form' 
  230. //) ); 
  231.  
  232. return $res;