EDD_SL_Plugin_Updater.php
6 years ago
ad-ajax.php
6 years ago
ad-debug.php
6 years ago
ad-health-notices.php
6 years ago
ad-model.php
5 years ago
ad-select.php
9 years ago
ad.php
5 years ago
ad_ajax_callbacks.php
5 years ago
ad_group.php
6 years ago
ad_placements.php
5 years ago
ad_type_abstract.php
5 years ago
ad_type_content.php
5 years ago
ad_type_dummy.php
5 years ago
ad_type_group.php
6 years ago
ad_type_image.php
5 years ago
ad_type_plain.php
5 years ago
checks.php
6 years ago
compatibility.php
5 years ago
display-conditions.php
5 years ago
filesystem.php
8 years ago
frontend-notices.php
6 years ago
frontend_checks.php
5 years ago
plugin.php
5 years ago
upgrades.php
6 years ago
utils.php
6 years ago
visitor-conditions.php
6 years ago
widget.php
6 years ago
filesystem.php
164 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @since 1.7.17 |
| 4 | */ |
| 5 | class Advanced_Ads_Filesystem { |
| 6 | /** |
| 7 | * Singleton instance of the class |
| 8 | * |
| 9 | * @var Advanced_Ads_Filesystem |
| 10 | */ |
| 11 | protected static $instance; |
| 12 | |
| 13 | /** |
| 14 | * Return an instance of Advanced_Ads_Filesystem |
| 15 | * |
| 16 | * @return Advanced_Ads_Filesystem |
| 17 | */ |
| 18 | public static function get_instance() { |
| 19 | if ( null === self::$instance ) { |
| 20 | self::$instance = new self; |
| 21 | } |
| 22 | |
| 23 | return self::$instance; |
| 24 | } |
| 25 | |
| 26 | private function __construct() {} |
| 27 | |
| 28 | /** |
| 29 | * Connect to the filesystem. |
| 30 | * |
| 31 | * @param array $directories A list of directories. If any of these do |
| 32 | * not exist, a WP_Error object will be returned. |
| 33 | * @return bool|WP_Error True if able to connect, false or a WP_Error otherwise. |
| 34 | */ |
| 35 | public function fs_connect( $directories = array() ) { |
| 36 | global $wp_filesystem; |
| 37 | $directories = ( is_array( $directories ) && count( $directories ) ) ? $directories : array( WP_CONTENT_DIR ); |
| 38 | |
| 39 | // This will output a credentials form in event of failure, We don't want that, so just hide with a buffer. |
| 40 | ob_start(); |
| 41 | $credentials = request_filesystem_credentials( '', '', false, $directories[0] ); |
| 42 | ob_end_clean(); |
| 43 | |
| 44 | if ( false === $credentials ) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | if ( ! WP_Filesystem( $credentials ) ) { |
| 49 | $error = true; |
| 50 | if ( is_object( $wp_filesystem ) && $wp_filesystem->errors->get_error_code() ) { |
| 51 | $error = $wp_filesystem->errors; |
| 52 | } |
| 53 | // Failed to connect, Error and request again. |
| 54 | ob_start(); |
| 55 | request_filesystem_credentials( '', '', $error, $directories[0] ); |
| 56 | ob_end_clean(); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | if ( ! is_object( $wp_filesystem) ) { |
| 61 | return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.', 'advanced-ads' ) ); |
| 62 | } |
| 63 | |
| 64 | if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { |
| 65 | return new WP_Error( 'fs_error', __( 'Filesystem error.', 'advanced-ads' ), $wp_filesystem->errors); |
| 66 | } |
| 67 | |
| 68 | foreach ( (array) $directories as $dir ) { |
| 69 | switch ( $dir ) { |
| 70 | case ABSPATH: |
| 71 | if ( ! $wp_filesystem->abspath() ) |
| 72 | return new WP_Error( 'fs_no_root_dir', __( 'Unable to locate WordPress root directory.', 'advanced-ads' ) ); |
| 73 | break; |
| 74 | case WP_CONTENT_DIR: |
| 75 | if ( ! $wp_filesystem->wp_content_dir() ) |
| 76 | return new WP_Error( 'fs_no_content_dir', __( 'Unable to locate WordPress content directory (wp-content).', 'advanced-ads' ) ); |
| 77 | break; |
| 78 | default: |
| 79 | if ( ! $wp_filesystem->find_folder( $dir ) ) |
| 80 | return new WP_Error( 'fs_no_folder', sprintf( __( 'Unable to locate needed folder (%s).', 'advanced-ads' ) , esc_html( basename( $dir ) ) ) ); |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Replace the 'direct' absolute path with the Filesystem API path. Useful only when the 'direct' method is not used. |
| 90 | * Works only with folders. |
| 91 | * Check https://codex.wordpress.org/Filesystem_API for info |
| 92 | * |
| 93 | * @param string existing path |
| 94 | * @return string normalized path |
| 95 | */ |
| 96 | public function normalize_path( $path ) { |
| 97 | global $wp_filesystem; |
| 98 | return $wp_filesystem->find_folder( $path ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Recursive directory creation based on full path. |
| 103 | * |
| 104 | * @param string $target Full path to attempt to create. |
| 105 | * @return bool Whether the path was created. True if path already exists. |
| 106 | */ |
| 107 | public function mkdir_p( $target ) { |
| 108 | global $wp_filesystem; |
| 109 | |
| 110 | if ( $wp_filesystem instanceof WP_Filesystem_Direct ) { |
| 111 | return wp_mkdir_p( $target ); |
| 112 | } |
| 113 | |
| 114 | $target = rtrim($target, '/'); |
| 115 | if ( empty($target) ) { |
| 116 | $target = '/'; |
| 117 | } |
| 118 | |
| 119 | if ( $wp_filesystem->exists( $target ) ) { |
| 120 | return $wp_filesystem->is_dir( $target ); |
| 121 | } |
| 122 | |
| 123 | $target_parent = dirname( $target ); |
| 124 | while ( '.' != $target_parent && ! $wp_filesystem->is_dir( $target_parent ) ) { |
| 125 | $target_parent = dirname( $target_parent ); |
| 126 | } |
| 127 | |
| 128 | $folder_parts = explode( '/', substr( $target, strlen( $target_parent ) + 1 ) ); |
| 129 | for ( $i = 1, $c = count( $folder_parts ); $i <= $c; $i++ ) { |
| 130 | $dir = $target_parent . '/' . implode( '/', array_slice( $folder_parts, 0, $i ) ); |
| 131 | if ( $wp_filesystem->exists( $dir ) ) { continue; } |
| 132 | |
| 133 | if ( ! $wp_filesystem->mkdir( $dir ) ) { |
| 134 | return false; |
| 135 | } |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Print the filesystem credentials modal when needed. |
| 142 | */ |
| 143 | public function print_request_filesystem_credentials_modal() { |
| 144 | $filesystem_method = get_filesystem_method(); |
| 145 | ob_start(); |
| 146 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
| 147 | ob_end_clean(); |
| 148 | $request_filesystem_credentials = ( $filesystem_method != 'direct' && ! $filesystem_credentials_are_stored ); |
| 149 | if ( ! $request_filesystem_credentials ) { |
| 150 | return; |
| 151 | } |
| 152 | ?> |
| 153 | <div id="advanced-ads-rfc-dialog" class="notification-dialog-wrap request-filesystem-credentials-dialog"> |
| 154 | <div class="notification-dialog-background"></div> |
| 155 | <div class="notification-dialog" role="dialog" aria-labelledby="request-filesystem-credentials-title" tabindex="0"> |
| 156 | <div class="request-filesystem-credentials-dialog-content"> |
| 157 | <?php request_filesystem_credentials( site_url() ); ?> |
| 158 | </div> |
| 159 | </div> |
| 160 | </div> |
| 161 | <?php |
| 162 | } |
| 163 | } |
| 164 |