firetreedesign / ccbpress-core

Introducing the easiest way to display information from Church Community Builder (CCB) on your church WordPress site.
https://churchdataconnect.com/
GNU General Public License v2.0
2 stars 0 forks source link

Add settings for sites behind Basic Authentication #48

Open danielmilner opened 5 years ago

danielmilner commented 5 years ago

It is fairly common for sites that are in development to be behind Basic Authentication. In these instances, WP Cron jobs and Admin Ajax requests will fail. We should add a setting to add the Basic Auth username and password to requests.

danielmilner commented 5 years ago

Code samples for adding Basic Auth to both Admin Ajax and Cron requests.

Admin Ajax

add_filter( 'http_request_args', 'http_request_args_basic_auth', 10, 2 );
function http_request_args_basic_auth( $args, $url ) {
    if ( strpos( $url, admin_url( 'admin-ajax.php' ) ) === 0 ) {
        $args['headers']['Authorization'] = 'Basic ' . base64_encode( USERNAME . ':' . PASSWORD );
    }

    return $args;
}

Cron

add_filter( 'cron_request', 'cron_request_basic_auth' );
function cron_request_basic_auth( $cron_request ) {
    $cron_request['args']['headers']['Authorization'] = 'Basic ' . base64_encode( USERNAME . ':' . PASSWORD );
    return $cron_request;
}