Skip to content

Commit

Permalink
create Openverse caption/attribution
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Dec 16, 2022
1 parent 26d2be1 commit a393ce8
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function getBlockAndPreviewFromMedia( media, mediaType ) {
// Add the common attributes between the different media types.
const attributes = {
id: media.id || undefined,
caption: media.caption || undefined,
caption: media.caption || undefined, // TODO: probably check to escape for security
};
const mediaSrc = media.url;
const alt = media.alt || undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,76 @@
*/
import { __ } from '@wordpress/i18n';
import { resolveSelect } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';

/**
* Internal dependencies
*/
import { store as coreStore } from '@wordpress/core-data';

const getExternalLink = ( url, text ) =>
`<a href="${ url }" target="_blank" rel="noreferrer noopener">${ text }</a>`;

const getOpenvereLicense = (
license,
licenseVersion = '', // unknown version.
licenseUrl
) => {
let licenseName = license.trim();
// PDM has no abbreviation
if ( license !== 'pdm' ) {
licenseName = license.toUpperCase().replace( 'SAMPLING', 'Sampling' );
}
// If version is known, append version to the name.
if ( licenseVersion ) {
licenseName += ` ${ licenseVersion }`;
}
// For licenses other than public-domain marks, prepend 'CC' to the name.
const isPublicDomainMark = [ 'pdm', 'cc0' ].includes( license );
if ( ! isPublicDomainMark ) {
licenseName = `CC ${ licenseName }`;
}
if ( !! licenseUrl ) {
license = getExternalLink(
`${ licenseUrl }?ref=openverse`,
licenseName
);
}
// TODO: add translators comment.
const markedLicence = isPublicDomainMark
? __( 'is marked with' )
: __( 'is licensed under' );
return `${ markedLicence } ${ license }`;
};

const getOpenverseCaption = ( item ) => {
const {
title = __( 'This work' ), // TODO: add translators comment.
foreign_landing_url: foreignLandingUrl,
creator,
creator_url: creatorUrl,
license,
license_version: licenseVersion,
license_url: licenseUrl,
} = item;
let _title = decodeEntities( title );
if ( !! foreignLandingUrl ) {
_title = getExternalLink( foreignLandingUrl, _title );
}
const fullLicense = getOpenvereLicense(
license,
licenseVersion,
licenseUrl
);
let _creator = decodeEntities( creator );
if ( creatorUrl ) {
_creator = getExternalLink( creatorUrl, creator );
}
_creator = ` ${ __( 'by' ) } ${ _creator }`; // TODO: add translators comment.
const caption = `"${ _title }" ${ _creator } ${ fullLicense }.`;
return caption.replace( /\s{2}/g, ' ' );
};

const coreMediaFetch = async ( query = {} ) => {
const mediaItems = await resolveSelect( coreStore ).getMediaItems( {
...query,
Expand Down Expand Up @@ -76,6 +140,7 @@ const inserterMediaCategories = [
...result,
sourceId: result.id,
id: undefined,
caption: getOpenverseCaption( result ),
} ) );
},
hasAvailableMedia: true,
Expand Down
65 changes: 65 additions & 0 deletions packages/editor/src/components/media-categories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,76 @@
*/
import { __ } from '@wordpress/i18n';
import { resolveSelect } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';

/**
* Internal dependencies
*/
import { store as coreStore } from '@wordpress/core-data';

const getExternalLink = ( url, text ) =>
`<a href="${ url }" target="_blank" rel="noreferrer noopener">${ text }</a>`;

const getOpenvereLicense = (
license,
licenseVersion = '', // unknown version.
licenseUrl
) => {
let licenseName = license.trim();
// PDM has no abbreviation
if ( license !== 'pdm' ) {
licenseName = license.toUpperCase().replace( 'SAMPLING', 'Sampling' );
}
// If version is known, append version to the name.
if ( licenseVersion ) {
licenseName += ` ${ licenseVersion }`;
}
// For licenses other than public-domain marks, prepend 'CC' to the name.
const isPublicDomainMark = [ 'pdm', 'cc0' ].includes( license );
if ( ! isPublicDomainMark ) {
licenseName = `CC ${ licenseName }`;
}
if ( !! licenseUrl ) {
license = getExternalLink(
`${ licenseUrl }?ref=openverse`,
licenseName
);
}
// TODO: add translators comment.
const markedLicence = isPublicDomainMark
? __( 'is marked with' )
: __( 'is licensed under' );
return `${ markedLicence } ${ license }`;
};

const getOpenverseCaption = ( item ) => {
const {
title = __( 'This work' ), // TODO: add translators comment.
foreign_landing_url: foreignLandingUrl,
creator,
creator_url: creatorUrl,
license,
license_version: licenseVersion,
license_url: licenseUrl,
} = item;
let _title = decodeEntities( title );
if ( !! foreignLandingUrl ) {
_title = getExternalLink( foreignLandingUrl, _title );
}
const fullLicense = getOpenvereLicense(
license,
licenseVersion,
licenseUrl
);
let _creator = decodeEntities( creator );
if ( creatorUrl ) {
_creator = getExternalLink( creatorUrl, creator );
}
_creator = ` ${ __( 'by' ) } ${ _creator }`; // TODO: add translators comment.
const caption = `"${ _title }" ${ _creator } ${ fullLicense }.`;
return caption.replace( /\s{2}/g, ' ' );
};

const coreMediaFetch = async ( query = {} ) => {
const mediaItems = await resolveSelect( coreStore ).getMediaItems( {
...query,
Expand Down Expand Up @@ -76,6 +140,7 @@ const inserterMediaCategories = [
...result,
sourceId: result.id,
id: undefined,
caption: getOpenverseCaption( result ),
} ) );
},
hasAvailableMedia: true,
Expand Down

1 comment on commit a393ce8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3713006785
📝 Reported issues:

Please sign in to comment.