EDD_SL_Plugin_Updater.php
8 years ago
ad-ajax.php
9 years ago
ad-debug.php
8 years ago
ad-model.php
9 years ago
ad-select.php
9 years ago
ad.php
8 years ago
ad_ajax_callbacks.php
8 years ago
ad_group.php
8 years ago
ad_placements.php
8 years ago
ad_type_abstract.php
11 years ago
ad_type_content.php
8 years ago
ad_type_dummy.php
8 years ago
ad_type_group.php
8 years ago
ad_type_image.php
8 years ago
ad_type_plain.php
9 years ago
checks.php
9 years ago
display-conditions.php
8 years ago
filesystem.php
8 years ago
frontend_checks.php
8 years ago
plugin.php
8 years ago
upgrades.php
9 years ago
utils.php
9 years ago
visitor-conditions.php
8 years ago
widget.php
8 years ago
filesystem.php
123 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 | * Check https://codex.wordpress.org/Filesystem_API for info |
| 91 | * |
| 92 | * @param string existing path |
| 93 | * @return string normalized path |
| 94 | */ |
| 95 | public function normalize_path( $path ) { |
| 96 | global $wp_filesystem; |
| 97 | return str_replace( ABSPATH, $wp_filesystem->abspath(), $path ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Print the filesystem credentials modal when needed. |
| 102 | */ |
| 103 | public function print_request_filesystem_credentials_modal() { |
| 104 | $filesystem_method = get_filesystem_method(); |
| 105 | ob_start(); |
| 106 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
| 107 | ob_end_clean(); |
| 108 | $request_filesystem_credentials = ( $filesystem_method != 'direct' && ! $filesystem_credentials_are_stored ); |
| 109 | if ( ! $request_filesystem_credentials ) { |
| 110 | return; |
| 111 | } |
| 112 | ?> |
| 113 | <div id="advanced-ads-rfc-dialog" class="notification-dialog-wrap request-filesystem-credentials-dialog"> |
| 114 | <div class="notification-dialog-background"></div> |
| 115 | <div class="notification-dialog" role="dialog" aria-labelledby="request-filesystem-credentials-title" tabindex="0"> |
| 116 | <div class="request-filesystem-credentials-dialog-content"> |
| 117 | <?php request_filesystem_credentials( site_url() ); ?> |
| 118 | </div> |
| 119 | </div> |
| 120 | </div> |
| 121 | <?php |
| 122 | } |
| 123 | } |