Skip to content

Function

Handle an action.

tml_action_handler( )

Source

function tml_action_handler() {
    if ( ! tml_is_action() ) {
        return;
    }

    // Redirect to https login if forced to use SSL
    if ( force_ssl_admin() && ! is_ssl() ) {
        if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
            wp_safe_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
            exit();
        } else {
            wp_safe_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
            exit();
        }
    }

    // Redirect other actions from the login action
    $action = tml_get_request_value( 'action' );
    if ( $action ) {
        if ( tml_is_action( 'login' ) && 'login' !== $action ) {
            // Fix some alias actions
            if ( 'retrievepassword' === $action ) {
                $action = 'lostpassword';
            } elseif ( 'rp' === $action ) {
                $action = 'resetpass';
            }

            // Get the action URL
            $url = tml_get_action_url( $action );
            if ( $url ) {
                // Add the query string to the URL
                $query = wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY );
                if ( $query ) {
                    parse_str( $query, $args );
                    unset( $args['action'] );
                    $url = add_query_arg( tml_array_map_recursive( 'rawurlencode', $args ), $url );
                }
                wp_safe_redirect( $url );
                exit;
            }
        }
    }

    nocache_headers();

    // Set a test cookie to test if cookies are enabled
    $secure = ( 'https' === wp_parse_url( wp_login_url(), PHP_URL_SCHEME ) );
    setcookie( TEST_COOKIE, 'WP Cookie check', 0, COOKIEPATH, COOKIE_DOMAIN, $secure );
    if ( SITECOOKIEPATH !== COOKIEPATH ) {
        setcookie( TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN, $secure );
    }

    // Add the testcookie field to the login form
    tml_add_form_field(
        'login',
        'testcookie',
        array(
            'type'     => 'hidden',
            'value'    => 1,
            'priority' => 30,
        )
    );

    /** This action is documented in wp-login.php */
    do_action( 'login_init' );

    $action_name = tml_get_action()->get_name();

    /** This action is documented in wp-login.php */
    do_action( "login_form_{$action_name}" );

    $action_name = tml_get_action()->get_name();

    if ( tml_is_ajax_request() ) {
        /**
         * Fires when a TML action is being requested.
         *
         * The dynamic portion of the hook name, `$action_name`, refers to the action name.
         *
         * @since 7.1
         */
        do_action( "tml_action_ajax_{$action_name}" );
    } else {
        /**
         * Fires when a TML action is being requested.
         *
         * The dynamic portion of the hook name, `$action_name`, refers to the action name.
         *
         * @since 7.0.3
         */
        do_action( "tml_action_{$action_name}" );
    }
}

Uses

Changelog

VersionDescription
7.0 Introduced.