WP_Filesystem example

'test'        => 'update_temp_backup_writable',
        );

        if ( ! function_exists( 'WP_Filesystem' ) ) {
            require_once ABSPATH . '/wp-admin/includes/file.php';
        }

        ob_start();
        $credentials = request_filesystem_credentials( '' );
        ob_end_clean();

        if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
            $result['status']      = 'recommended';
            $result['label']       = __( 'Could not access filesystem' );
            $result['description'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
            return $result;
        }

        $wp_content = $wp_filesystem->wp_content_dir();

        if ( ! $wp_content ) {
            $result['status']      = 'critical';
            $result['label']       = __( 'Unable to locate WordPress content directory' );
            
$status['errorMessage'] = __( 'The requested theme does not exist.' );
        wp_send_json_error( $status );
    }

    // Check filesystem credentials. `delete_theme()` will bail otherwise.     $url = wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet );

    ob_start();
    $credentials = request_filesystem_credentials( $url );
    ob_end_clean();

    if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
        global $wp_filesystem;

        $status['errorCode']    = 'unable_to_connect_to_filesystem';
        $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );

        // Pass through the error from WP_Filesystem if one was raised.         if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
            $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
        }

        wp_send_json_error( $status );
    }

    public function fs_connect( $directories = array()$allow_relaxed_file_ownership = false ) {
        global $wp_filesystem;

        $credentials = $this->skin->request_filesystem_credentials( false, $directories[0]$allow_relaxed_file_ownership );
        if ( false === $credentials ) {
            return false;
        }

        if ( ! WP_Filesystem( $credentials$directories[0]$allow_relaxed_file_ownership ) ) {
            $error = true;
            if ( is_object( $wp_filesystem ) && $wp_filesystem->errors->has_errors() ) {
                $error = $wp_filesystem->errors;
            }
            // Failed to connect. Error and request again.             $this->skin->request_filesystem_credentials( $error$directories[0]$allow_relaxed_file_ownership );
            return false;
        }

        if ( ! is_object( $wp_filesystem ) ) {
            return new WP_Error( 'fs_unavailable', $this->strings['fs_unavailable'] );
        }
function _wp_delete_all_temp_backups() {
    global $wp_filesystem;

    if ( ! function_exists( 'WP_Filesystem' ) ) {
        require_once ABSPATH . '/wp-admin/includes/file.php';
    }

    ob_start();
    $credentials = request_filesystem_credentials( '' );
    ob_end_clean();

    if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
        return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
    }

    if ( ! $wp_filesystem->wp_content_dir() ) {
        return new WP_Error( 'fs_no_content_dir', __( 'Unable to locate WordPress content directory (wp-content).' ) );
    }

    $temp_backup_dir = $wp_filesystem->wp_content_dir() . 'upgrade-temp-backup/';
    $dirlist         = $wp_filesystem->dirlist( $temp_backup_dir );
    $dirlist         = $dirlist ? $dirlist : array();

    
if ( false === $credentials ) {
        if ( ! empty( $data ) ) {
            require_once ABSPATH . 'wp-admin/admin-header.php';
            echo $data;
            require_once ABSPATH . 'wp-admin/admin-footer.php';
            exit;
        }
        return;
    }

    if ( ! WP_Filesystem( $credentials ) ) {
        ob_start();
        // Failed to connect. Error and request again.         request_filesystem_credentials( $redirect, '', true );
        $data = ob_get_clean();

        if ( ! empty( $data ) ) {
            require_once ABSPATH . 'wp-admin/admin-header.php';
            echo $data;
            require_once ABSPATH . 'wp-admin/admin-footer.php';
            exit;
        }
        
global $wp_filesystem;

        require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
        $skin    = new Automatic_Upgrader_Skin();
        $success = $skin->request_filesystem_credentials( false, ABSPATH );

        if ( ! $success ) {
            return false;
        }

        WP_Filesystem();

        if ( 'direct' !== $wp_filesystem->method ) {
            return false;
        }

        // Make sure the `get_core_checksums()` function is available during our REST API call.         if ( ! function_exists( 'get_core_checksums' ) ) {
            require_once ABSPATH . 'wp-admin/includes/update.php';
        }

        $checksums = get_core_checksums( $wp_version, 'en_US' );
        
if ( false === $credentials ) {
        if ( ! empty( $data ) ) {
            require_once ABSPATH . 'wp-admin/admin-header.php';
            echo $data;
            require_once ABSPATH . 'wp-admin/admin-footer.php';
            exit;
        }
        return;
    }

    if ( ! WP_Filesystem( $credentials ) ) {
        ob_start();
        // Failed to connect. Error and request again.         request_filesystem_credentials( $url, '', true );
        $data = ob_get_clean();

        if ( ! empty( $data ) ) {
            require_once ABSPATH . 'wp-admin/admin-header.php';
            echo $data;
            require_once ABSPATH . 'wp-admin/admin-footer.php';
            exit;
        }
        
?> <div class="wrap"> <h1><?php _e( 'Update WordPress' ); ?></h1> <?php
    $credentials = request_filesystem_credentials( $url, '', false, ABSPATH, array( 'version', 'locale' )$allow_relaxed_file_ownership );
    if ( false === $credentials ) {
        echo '</div>';
        return;
    }

    if ( ! WP_Filesystem( $credentials, ABSPATH, $allow_relaxed_file_ownership ) ) {
        // Failed to connect. Error and request again.         request_filesystem_credentials( $url, '', true, ABSPATH, array( 'version', 'locale' )$allow_relaxed_file_ownership );
        echo '</div>';
        return;
    }

    if ( $wp_filesystem->errors->has_errors() ) {
        foreach ( $wp_filesystem->errors->get_error_messages() as $message ) {
            show_message( $message );
        }
        echo '</div>';
        
Home | Imprint | This part of the site doesn't use cookies.