request_filesystem_credentials() – Displays a form to the user to request for their FTP/SSH details in order to connect to the filesystem.

You appear to be a bot. Output may be restricted

Description

Displays a form to the user to request for their FTP/SSH details in order to connect to the filesystem.

All chosen/entered details are saved, excluding the password. Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467) to specify an alternate FTP/SSH port. Plugins may override this form by returning true|false via the request_filesystem_credentials filter.

Usage

$bool|array = request_filesystem_credentials( $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership );

Parameters

$form_post
( string ) required – The URL to post the form to.
$type
( string ) optional – Optional. Chosen type of filesystem. Default empty.
$error
( bool|WP_Error ) optional – Optional. Whether the current request has failed to connect, or an error object. Default false.
$context
( string ) optional – Optional. Full path to the directory that is tested for being writable. Default empty.
$extra_fields
( array ) optional – Optional. Extra POST fields to be checked for inclusion in the post. Default null.
$allow_relaxed_file_ownership
( bool ) optional – Optional. Whether to allow Group/World writable. Default false.

Returns

bool|array True if no filesystem credentials are required, false if they are required but have not been provided, array of credentials if they are required and have been provided.

Source

File name: wordpress/wp-admin/includes/file.php
Lines:

1 to 100 of 292
function request_filesystem_credentials( $form_post, $type = '', $error = false, $context = '', $extra_fields = null, $allow_relaxed_file_ownership = false ) {
  global $pagenow;

  
/**
 * Filters the filesystem credentials.
 *
 * Returning anything other than an empty string will effectively short-circuit
 * output of the filesystem credentials form, returning that value instead.
 *
 * A filter should return true if no filesystem credentials are required, false if they are required but have not been
 * provided, or an array of credentials if they are required and have been provided.
 *
 * @since 2.5.0
 * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
 *
 * @param mixed         $credentials                  Credentials to return instead. Default empty string.
 * @param string        $form_post                    The URL to post the form to.
 * @param string        $type                         Chosen type of filesystem.
 * @param bool|WP_Error $error                        Whether the current request has failed to connect,
 *                                                    or an error object.
 * @param string        $context                      Full path to the directory that is tested for
 *                                                    being writable.
 * @param array         $extra_fields                 Extra POST fields.
 * @param bool          $allow_relaxed_file_ownership Whether to allow Group/World writable.
 */
  $req_cred = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership );

  if ( '' !== $req_cred ) {
    return $req_cred;
  }

  if ( empty( $type ) ) {
    $type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership );
  }

  if ( 'direct' === $type ) {
    return true;
  }

  if ( is_null( $extra_fields ) ) {
    $extra_fields = array( 'version', 'locale' );
  }

  $credentials = get_option(
    'ftp_credentials',
    array(
      'hostname' => '',
      'username' => '',
    )
  );

  $submitted_form = wp_unslash( $_POST );

  // Verify nonce, or unset submitted form field values on failure.
  if ( ! isset( $_POST['_fs_nonce'] ) || ! wp_verify_nonce( $_POST['_fs_nonce'], 'filesystem-credentials' ) ) {
    unset(
      $submitted_form['hostname'],
      $submitted_form['username'],
      $submitted_form['password'],
      $submitted_form['public_key'],
      $submitted_form['private_key'],
      $submitted_form['connection_type']
    );
  }

  $ftp_constants = array(
    'hostname'    => 'FTP_HOST',
    'username'    => 'FTP_USER',
    'password'    => 'FTP_PASS',
    'public_key'  => 'FTP_PUBKEY',
    'private_key' => 'FTP_PRIKEY',
  );

  // If defined, set it to that. Else, if POST'd, set it to that. If not, set it to an empty string.
  // Otherwise, keep it as it previously was (saved details in option).
  foreach ( $ftp_constants as $key => $constant ) {
    if ( defined( $constant ) ) {
      $credentials[ $key ] = constant( $constant );
    } elseif ( ! empty( $submitted_form[ $key ] ) ) {
      $credentials[ $key ] = $submitted_form[ $key ];
    } elseif ( ! isset( $credentials[ $key ] ) ) {
      $credentials[ $key ] = '';
    }
  }

  // Sanitize the hostname, some people might pass in odd data.
  $credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); // Strip any schemes off.

  if ( strpos( $credentials['hostname'], ':' ) ) {
    list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 );
    if ( ! is_numeric( $credentials['port'] ) ) {
      unset( $credentials['port'] );
    }
  } else {
    unset( $credentials['port'] );
  }

  if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' === FS_METHOD ) ) {
    $credentials['connection_type'] = 'ssh';
  } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' === $type ) { // Only the FTP Extension understands SSL.
 

 View on GitHub View on Trac