Skip to content

Commit

Permalink
Merge pull request #208 from xwp/fix/empty-name
Browse files Browse the repository at this point in the history
Name sure site name isn't empty.
  • Loading branch information
spacedmonkey committed Aug 7, 2020
2 parents 7bf793d + 9e6d777 commit 7a26345
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
32 changes: 26 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,24 @@ 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'] ) {
$url = wp_parse_url( $data['url'] );
if ( $url && array_key_exists( 'host', $url ) ) {
$data['name'] = sanitize_title_with_dashes( $url['host'] );
}
}

return $data;
}
}
20 changes: 20 additions & 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 All @@ -717,6 +718,25 @@ public function test_get_credentials() {
$this->assertEquals( $expected_utm, $actual_utm );
}

/**
*
* Test get_site_data.
*
* @covers ::get_site_data()
*/
public function test_get_site_data() {
add_filter( 'pre_option_blogname', '__return_empty_string' );
$site_data = $this->settings->get_site_data();

$this->assertEquals( [ 'url', 'name' ], array_keys( $site_data ) );

$url = wp_parse_url( get_home_url( null, '/' ) );
$name = sanitize_title_with_dashes( $url['host'] );

$this->assertEquals( $site_data['name'], $name );
remove_filter( 'pre_option_blogname', '__return_empty_string' );
}

/**
* Fake success.
*
Expand Down

0 comments on commit 7a26345

Please sign in to comment.