Skip to content

Commit

Permalink
- close #21 - enable Unsplash parser
Browse files Browse the repository at this point in the history
  • Loading branch information
r-brown committed Feb 28, 2016
1 parent 5ce1cf4 commit df02aa1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions credit-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
require_once(plugin_dir_path(__FILE__) . '/php/parser/flickr.php');
require_once(plugin_dir_path(__FILE__) . '/php/parser/freeimages.php');
require_once(plugin_dir_path(__FILE__) . '/php/parser/shutterstock.php');
require_once(plugin_dir_path(__FILE__) . '/php/parser/unsplash.php');

// Register hooks that are fired when the plugin is activated, deactivated, and uninstalled, respectively.
register_activation_hook(__FILE__, array('Credit_Tracker', 'activate'));
Expand Down
22 changes: 22 additions & 0 deletions options.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,11 @@ function credittracker_get_sources_array()
'caption' => 'Wikimedia',
'copyright' => 'credittracker_get_wikimedia_copyright',
'retriever' => 'credittracker_get_wikimedia_metadata'
),
'unsplash' => array(
'caption' => 'Unsplash',
'copyright' => 'credittracker_get_unsplash_copyright',
'retriever' => 'credittracker_get_unsplash_metadata'
)
);
return $sources;
Expand Down Expand Up @@ -840,4 +845,21 @@ function credittracker_get_wikimedia_metadata($number)
return $item;
}

/**
* Unsplash: copyright
*/
function credittracker_get_unsplash_copyright()
{
return CTUnsplash::COPYRIGHT;
}

/**
* Unsplash: metadata
*/
function credittracker_get_unsplash_metadata($number)
{
$parser = new CTUnsplash();
return $parser->execute($number);
}

?>
49 changes: 49 additions & 0 deletions php/parser/Unsplash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* Unsplash parser
*
* @package parser
* @author Labs64 <[email protected]>
**/
class CTUnsplash extends CTParser
{

const COPYRIGHT = '&copy; %author% - unsplash.com';

const BASE_URL = 'https://unsplash.com/photos/';

protected function parse($number)
{
$url = self::BASE_URL . $number;

$item = parent::parse($number);
$item['source'] = 'Unsplash';
$item['publisher'] = 'Unsplash';
$item['license'] = 'Creative Commons Zero (CC0 1.0)';
$item['link'] = $url;

$doc = new DOMDocument();
$response = $this->curl($url);
$html = @$doc->loadHTML(wp_remote_retrieve_body($response));
if ($html) {
$xpath = new DOMXPath($doc);

$tags = $xpath->query("*/meta[@property='og:url']");
if (!is_null($tags) && $tags->length > 0) {
$item['link'] = $tags->item(0)->getAttribute('content');
}

$tags = $xpath->query("*/meta[@property='og:title']");
if (!is_null($tags) && $tags->length > 0) {
$author = $tags->item(0)->getAttribute('content');
$author = str_replace("Photo by ", "", $author);
$author = str_replace(" | Unsplash", "", $author);
$item['author'] = $author;
}
}

return $item;
}

}
4 changes: 2 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== Plugin Name ===
Contributors: labs64
Tags: credit, attribution, legal, copyright, owner, author, media library, media, image, photo, license, royalty-free, RF, Creative Commons, stock, attachment, flickr, fotolia, bildnachweis, impressum, imprint, microdata, NetLicensing
Tags: credit, attribution, legal, copyright, owner, author, media library, media, image, photo, license, royalty-free, RF, Creative Commons, stock, attachment, flickr, fotolia, unsplash, bildnachweis, impressum, imprint, microdata, NetLicensing
Requires at least: 3.5.1
Tested up to: 4.4.2
Stable tag: 1.1.8
Expand Down Expand Up @@ -143,7 +143,7 @@ Yes you can! Join in on our [GitHub repository](https://github.com/Labs64/credit
== Changelog ==

= 1.1.8 =
* *TODO*
* Enable Unsplash parser

= 1.1.7 =
* Improve custom source handling
Expand Down

0 comments on commit df02aa1

Please sign in to comment.