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