Skip to content

Commit

Permalink
adding license meta-data json+ld markup generation
Browse files Browse the repository at this point in the history
  • Loading branch information
padams committed Sep 1, 2020
1 parent c105d11 commit 27f0bdc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
3 changes: 1 addition & 2 deletions modules/base/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class base extends photopress_module {

public function definePublicHooks() {

add_action( 'wp_enqueue_scripts', array( $this, 'photopress_base_js' ) );

add_action( 'wp_enqueue_scripts', [ $this, 'photopress_base_js' ] );
}

public function defineAdminHooks() {
Expand Down
68 changes: 64 additions & 4 deletions modules/metadata/metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public function definePublicHooks() {
add_filter( 'pre_move_uploaded_file', [ $this, 'embedLicense' ], 1, 4 );
}

add_filter( 'frame/attachment/image_markup', [ $this, 'addLicenseToImageMarkup'], 10, 2 );

// stop wordpress from stripping image meta from resized images.
add_filter ('image_strip_meta', function() {

Expand Down Expand Up @@ -92,8 +94,6 @@ public function defineAdminHooks() {
*/
public function addAttributesToImagesInContent( $content, $block ) {

photopress_util::debug('hello from attrincontent');

$allowedBlocks = ['photopress/gallery', 'core/image'];

if( ! in_array( $block['blockName'], $allowedBlocks ) ) {
Expand Down Expand Up @@ -141,7 +141,9 @@ public function addAttributesToImagesInContent( $content, $block ) {
'suppress_filters' => false,
]
);


$licensable_images = [];

foreach ( $attachments as $attachment ) {

$image_html = $selected_images[ $attachment->ID ];
Expand All @@ -158,14 +160,20 @@ public function addAttributesToImagesInContent( $content, $block ) {
$find[] = $image_html;

$replace[] = str_replace( '<img ', "<img $attributes_html", $image_html );

// add licensable image
$licensable_images[] = $attachment->ID;
}

$content = str_replace( $find, $replace, $content );

// add licensable images
$content .= $this->renderLicensingSchema( $licensable_images );

return $content;
}

function addAttributesToImages( $attr, $attachment = null ) {
public function addAttributesToImages( $attr, $attachment = null ) {

$attachment_id = intval( $attachment->ID );

Expand Down Expand Up @@ -217,6 +225,58 @@ function addAttributesToImages( $attr, $attachment = null ) {
return $attr;
}

public function addLicenseToImageMarkup( $markup, $attachment_id ) {

$markup .= $this->renderLicensingSchema( $attachment_id );

return $markup;
}

public function renderLicensingSchema( $attachment_ids ) {

$licensing_schema = $this->generateLicensingSchema( $attachment_ids );

$content = "\n";
$content .= sprintf('<script type="application/ld+json">%s</script>', json_encode( $licensing_schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) );

return $content;
}

// can take an array of Ids or a single id
public function generateLicensingSchema( $attachment_ids = [] ) {

$schema_block = [];

if ( is_array( $attachment_ids ) ) {

foreach ( $attachment_ids as $id ) {

$schema_block[] = $this->generateImageLicenseSchema( $id );
}

} else {

$schema_block = $this->generateImageLicenseSchema( $attachment_ids );
}

return $schema_block;
}

public function generateImageLicenseSchema( $attachment_id ) {

$schema = [

"@context" => "https://schema.org/",
"@type" => "ImageObject",
"contentUrl" => wp_get_attachment_url( $attachment_id ),
"license" => pp_api::getOption( 'core', 'metadata', 'web_statement_of_rights'),
"acquireLicensePage" => pp_api::getOption( 'core', 'metadata', 'licensor_url')

];

return $schema;
}

public function storeMoreMetaData( $meta, $file, $image_type, $iptc, $exif ) {

//pp_api::debug($meta);
Expand Down

0 comments on commit 27f0bdc

Please sign in to comment.