Skip to content

Commit

Permalink
Merge 7cb29e8 into 7bf793d
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedmonkey committed Aug 6, 2020
2 parents 7bf793d + 7cb29e8 commit b3630a6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
30 changes: 24 additions & 6 deletions php/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,14 +444,13 @@ public function get_access_token( $code ) {
* @return mixed
*/
public function get_client_id( $access_token ) {
$url = get_home_url( null, '/' );
$name = get_bloginfo( 'name' );
$response = wp_remote_post(
$site_data = $this->get_site_data();
$response = wp_remote_post(
'https://api.unsplash.com/clients',
[
'body' => [
'name' => $name,
'description' => sprintf( 'Wordpress Oauth Client application for: %1$s - %2$s', $name, $url ),
'name' => $site_data['name'],
'description' => sprintf( 'Wordpress Oauth Client application for: %1$s - %2$s', $site_data['name'], $site_data['url'] ),
],
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
Expand Down Expand Up @@ -504,7 +503,8 @@ public function access_key_render() {
*/
public function get_credentials() {
$options = get_option( 'unsplash_settings' );
$site_name_slug = sanitize_title_with_dashes( get_bloginfo( 'name' ) );
$site_data = $this->get_site_data();
$site_name_slug = sanitize_title_with_dashes( $site_data['name'] );

$credentials = [
'applicationId' => ! empty( $options['access_key'] ) ? $this->decrypt( $options['access_key'] ) : getenv( 'UNSPLASH_ACCESS_KEY' ),
Expand All @@ -521,4 +521,22 @@ public function get_credentials() {

return $credentials;
}

/**
* Get site name and url. If site name is empty, fallback to domain with dashes.
*
* @return array
*/
public function get_site_data() {
$data = [
'url' => get_home_url( null, '/' ),
'name' => get_bloginfo( 'name' ),
];
if ( ! $data['name'] && $data['url'] ) {
$url = wp_parse_url( $data['url'] );
$data['name'] = sanitize_title_with_dashes( $url['host'] );
}

return $data;
}
}
1 change: 1 addition & 0 deletions tests/phpunit/php/class-test-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ public function test_access_key_render() {
* Test get_credentials.
*
* @covers ::get_credentials()
* @covers ::get_site_data()
*/
public function test_get_credentials() {
$credentials = $this->settings->get_credentials();
Expand Down

0 comments on commit b3630a6

Please sign in to comment.