Skip to content

Commit

Permalink
Add utm source as class property
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedmonkey committed Apr 28, 2020
1 parent 0ee8f0c commit 8984ed7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
19 changes: 13 additions & 6 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,11 +177,9 @@ public function get_caption() {
return '';
}

$utm_source = get_plugin_instance()->settings->get_credentials()['utmSource'];

$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
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

0 comments on commit 8984ed7

Please sign in to comment.