Skip to content

Commit

Permalink
Merge pull request #85 from xwp/enhancement/utm-source-slug
Browse files Browse the repository at this point in the history
Set UTM source to slug of site name
  • Loading branch information
spacedmonkey committed Apr 28, 2020
2 parents 85909ca + 8984ed7 commit 4b78088
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 56 deletions.
21 changes: 13 additions & 8 deletions php/class-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class Image {
* @var array
*/
protected $process_data = [];

/**
* UTM source.
*
* @var string
*/
protected $utm_source = '';
/**
* Hardcoded file ext.
*/
Expand All @@ -39,10 +46,12 @@ class Image {
/**
* Image constructor.
*
* @param array $image Unsplash image array.
* @param array $image Unsplash image array.
* @param string $utm_source (Optional) UTM source.
*/
public function __construct( array $image = [] ) {
$this->image = $image;
public function __construct( array $image = [], $utm_source = '' ) {
$this->image = $image;
$this->utm_source = $utm_source;
$this->process_fields();
}

Expand Down Expand Up @@ -168,13 +177,9 @@ public function get_caption() {
return '';
}

$options = get_option( 'unsplash_settings' );
$default_utm = ( getenv( 'UNSPLASH_UTM_SOURCE' ) ) ? getenv( 'UNSPLASH_UTM_SOURCE' ) : 'WordPress-XWP';
$utm_source = ! empty( $options['utm_source'] ) ? $options['utm_source'] : $default_utm;

$url = add_query_arg(
[
'utm_source' => $utm_source,
'utm_source' => $this->utm_source,
'utm_medium' => 'referral',
],
'https://unsplash.com/'
Expand Down
4 changes: 3 additions & 1 deletion php/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ public function enqueue_media_scripts() {
* @return array
*/
public function wp_prepare_attachment_for_js( array $photo ) {
$image = new Image( $photo );
$credentials = $this->settings->get_credentials();
$utm_source = $credentials['utmSource'];
$image = new Image( $photo, $utm_source );

$response = [
'id' => isset( $photo['id'] ) ? $photo['id'] : null,
Expand Down
7 changes: 4 additions & 3 deletions php/class-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,10 @@ public function get_import( $request ) {
$this->http_client_init();
$photo = Photo::find( $id );
$photo->download();
$results = $photo->toArray();

$image = new Image( $results );
$results = $photo->toArray();
$credentials = $this->plugin->settings->get_credentials();
$utm_source = $credentials['utmSource'];
$image = new Image( $results, $utm_source );
$importer = new Import( $id, $image );
$attachment_id = $importer->process();
if ( is_wp_error( $attachment_id ) ) {
Expand Down
26 changes: 4 additions & 22 deletions php/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,6 @@ public function add_settings() {
'unsplash',
'unsplash_section'
);

add_settings_field(
'utm_source',
__( 'UTM Source', 'unsplash' ),
[ $this, 'utm_source_render' ],
'unsplash',
'unsplash_section'
);
}

/**
Expand Down Expand Up @@ -270,30 +262,21 @@ public function secret_key_render() {
<?php
}

/**
* Renders the UTM Source Key.
*/
public function utm_source_render() {
$options = get_option( 'unsplash_settings' );
?>
<input type='text' class="widefat" name='unsplash_settings[utm_source]' value='<?php echo esc_attr( isset( $options['utm_source'] ) ? $options['utm_source'] : '' ); ?>'>
<?php
}

/**
* Format the API credentials in an array and filter.
*
* @return mixed|void
*/
public function get_credentials() {
$options = get_option( 'unsplash_settings' );
$default_utm = ( getenv( 'UNSPLASH_UTM_SOURCE' ) ) ? getenv( 'UNSPLASH_UTM_SOURCE' ) : 'WordPress-XWP';
$options = get_option( 'unsplash_settings' );
$site_name_slug = sanitize_title_with_dashes( get_bloginfo( 'name' ) );

$credentials = [
'applicationId' => ! empty( $options['access_key'] ) ? $this->decrypt( $options['access_key'] ) : getenv( 'UNSPLASH_ACCESS_KEY' ),
'secret' => ! empty( $options['secret_key'] ) ? $this->decrypt( $options['secret_key'] ) : getenv( 'UNSPLASH_SECRET_KEY' ),
'utmSource' => ! empty( $options['utm_source'] ) ? $options['utm_source'] : $default_utm,
'utmSource' => getenv( 'UNSPLASH_UTM_SOURCE' ) ? getenv( 'UNSPLASH_UTM_SOURCE' ) : $site_name_slug,
];

/**
* Filter API credentials.
*
Expand All @@ -304,5 +287,4 @@ public function get_credentials() {

return $credentials;
}

}
10 changes: 6 additions & 4 deletions tests/phpunit/php/class-test-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ public function test_get_image_field() {
* @covers \Unsplash\Image::get_caption()
*/
public function test_get_caption() {
$image = new Image( $this->get_data() );
$image = new Image( $this->get_data(), 'test_utm_source' );
$this->assertRegexp( '/Harley-Davidson/', $image->get_caption() );
$this->assertRegexp( '/https:\/\/unsplash.com/', $image->get_caption() );
$this->assertRegexp( '/https:\/\/www.unpslash.com\/harleydavidson/', $image->get_caption() );
$this->assertContains( 'test_utm_source', $image->get_caption() );
}
/**
* Test get captionl.
Expand All @@ -135,7 +136,7 @@ public function test_get_caption() {
* @covers \Unsplash\Image::get_caption()
*/
public function test_no_get_caption() {
$image = new Image( [] );
$image = new Image( [], 'test_utm_source' );
$this->assertSame( '', $image->get_caption() );
}

Expand All @@ -146,7 +147,7 @@ public function test_no_get_caption() {
* @covers \Unsplash\Image::get_caption()
*/
public function test_no_get_caption_1() {
$image = new Image( [] );
$image = new Image( [], 'test_utm_source' );
$this->assertSame( '', $image->get_caption() );
}

Expand All @@ -165,7 +166,8 @@ public function test_no_get_caption_2() {
'html' => '',
],
],
]
],
'test_utm_source'
);
$this->assertSame( '', $image->get_caption() );
}
Expand Down
26 changes: 8 additions & 18 deletions tests/phpunit/php/class-test-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,6 @@ public function test_secret_key_render() {
$this->assertContains( $expected, $input );
}

/**
* Test utm_source_render.
*
* @covers ::utm_source_render()
*/
public function test_utm_source_render() {
ob_start();
$this->settings->utm_source_render();
$input = ob_get_clean();

$expected = "\t\t<input type='text' class=\"widefat\" name='unsplash_settings[utm_source]' value=''>\n\t\t";

$this->assertContains( $expected, $input );
}

/**
*
* Test get_credentials.
Expand All @@ -241,8 +226,13 @@ public function test_utm_source_render() {
*/
public function test_get_credentials() {
$credentials = $this->settings->get_credentials();
$this->assertArrayHasKey( 'applicationId', $credentials );
$this->assertArrayHasKey( 'secret', $credentials );
$this->assertArrayHasKey( 'utmSource', $credentials );

$this->assertEquals( [ 'applicationId', 'secret', 'utmSource' ], array_keys( $credentials ) );

// Test UTM source.
$expected_utm = sanitize_title_with_dashes( get_bloginfo( 'name' ) );
$actual_utm = $credentials['utmSource'];

$this->assertEquals( $expected_utm, $actual_utm );
}
}

0 comments on commit 4b78088

Please sign in to comment.