Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Added support for attribution on featured image #239 #239

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 82 additions & 84 deletions includes/class-creativecommons-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public function license_block( $att_id, $fallback_title = null ) {
$attribution_url = get_post_meta( $att_id, 'attribution_url', true );
$source_work_url = get_post_meta( $att_id, 'source_work_url', true );
$extras_url = get_post_meta( $att_id, 'extra_permissions_url', true );

// Unfiltered.
$meta = wp_get_attachment_metadata( $att_id, true );
$credit = get_post_meta( $att_id, 'attribution_name', true );
Expand All @@ -434,7 +434,7 @@ public function license_block( $att_id, $fallback_title = null ) {
) {
$credit = $meta['image_meta']['credit'];
}

$title = get_the_title( $att_id );
if ( ! $title ) {
$title = $meta['image_meta']['title'];
Expand All @@ -447,14 +447,13 @@ public function license_block( $att_id, $fallback_title = null ) {
$license_url = $license_url . '/';
}
}

if ( $license_url ) {
$license_name = $this->license_name( $license_url );
$button_url = $this->license_button_url( $license_url );
}

// RDF stuff.

if ( $license_url ) {
$license_button_url = $this->license_button_url( $license_url );
$l = CreativeCommons::get_instance();
Expand All @@ -470,14 +469,14 @@ public function license_block( $att_id, $fallback_title = null ) {
$extras_url,
''
); // warning_text.

$button = CreativeCommonsButton::get_instance()->markup(
$html_rdfa,
false,
31,
false
);

$block = $button;
$block .= '<!-- RDFa! -->' . $html_rdfa . '<!-- end of RDFa! -->';
} else {
Expand All @@ -489,33 +488,35 @@ public function license_block( $att_id, $fallback_title = null ) {
}
}
}


// Added Attribution Support
if ( $attribution_url ) {
$block .= '<p><a href="' . esc_url($attribution_url) . '" target="_blank">' . esc_html($credit) . '</a></p>';
}

return $block;
}



/**
* Function: caption_block
*
* @param mixed $attr
* @param mixed $att_id
*/
public function caption_block( $attr, $att_id ) {

// This is way too simple. Improve before re-introducing the caption filter.
$caption = '<div class="cc-license-caption-wrapper cc-license-block">'
. '<div class="wp-caption-text">'
. $attr['caption']
. '</div><br />';

$caption .= $this->license_block( $att_id, $attr['caption'] );

$caption .= '</div>';

return $caption;
}



/**
* Function: image_url_to_postid
*
Expand All @@ -541,15 +542,14 @@ public function image_url_to_postid( $image_url ) {
}
return $att_id;
}

/**
* Function: license_shortcode
*
* @param mixed $atts
* @param mixed $content
*/
public function license_shortcode( $atts, $content = null ) {

if ( $content !== null ) {
// TODO: Profile replacing this with parsing html and walking the DOM.
$match_count = preg_match(
Expand All @@ -569,7 +569,7 @@ public function license_shortcode( $atts, $content = null ) {
}
return do_shortcode( $content );
}

/**
* Function: captioned_image
*
Expand All @@ -579,24 +579,23 @@ public function license_shortcode( $atts, $content = null ) {
* robust. So we do not and will not do that.
*/
public function captioned_image( $empty, $attr, $content ) {
$args = shortcode_atts(
array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => '',
'title' => '',
),
$attr
);

$args = shortcode_atts(
array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => '',
'title' => '',
),
$attr
);
if ( isset( $attr['id'] )
&& isset( $attr['license'] )
) {
// Extract attachment $post->ID.
preg_match( '/\d+/', $attr['id'], $att_id );
if ( $att_id ) {

// We *should* handle this based on the shortcode code's behaviour.
// if ((intval($width) > 1) && $caption) {.
$result = '<div ' /*. $id*/ . 'class="cc-caption wp-caption '
Expand All @@ -612,7 +611,7 @@ public function captioned_image( $empty, $attr, $content ) {
}
return $result;
}

/**
* Function: init
*
Expand All @@ -625,84 +624,83 @@ public function init() {
0,
2
);

add_filter(
'attachment_fields_to_edit',
array( $this, 'add_image_license_metadata' ),
10,
2
);

add_filter(
'attachment_fields_to_save',
array( $this, 'save_image_license_metadata' ),
10,
2
);

}

// We really need to improve our emulation of the caption shortcode's
// output, and make sure our css fits it better, before adding this
// back in.

/*
* add_filter(
* 'img_caption_shortcode',
* array($this, 'captioned_image'),
* 10,
* 3
* );
*/

if (get_option("enable_attribution_box")) {
*/
if ( get_option("enable_attribution_box") ) {
add_filter( 'the_content', array( $this, 'add_attribution_boxes' ));
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_attribution_boxes' ));
}

add_shortcode(
'license',
array( $this, 'license_shortcode' )
);
}

public function enqueue_attribution_boxes() {
wp_enqueue_style('cc-attribution-box', plugins_url('css/cc-attribution-box.css', dirname(__FILE__)));
}

public function add_attribution_boxes( $content ) {
return preg_replace_callback(
'/<img\s[^>]*class="wp-image-(\d+)[^>]*>/',
function ($matches) {
return
'<div class="cc-attribution-box-container">'
. $matches[0]
. '<div class="cc-attribution-box"> '
. $this->simple_license_block($matches[1])
. '</div></div>';
},
$content
);
}

public function simple_license_block($att_id) {
/* TODO: this should just be replaced by just using $this->license_block.
* Need to make sure that code can be safely fixed for this purpose,
*/
$license_url = get_post_meta( $att_id, 'license_url', true );
$attribution_url = get_post_meta( $att_id, 'attribution_url', true );
$title = get_the_title( $att_id );
$credit = get_post_meta( $att_id, 'attribution_name', true );

$license_name = $this->license_name( $license_url );
$button_url = $this->license_button_url( $license_url );

if (!$button_url) return '';

$lazy = function_exists('wp_lazy_loading_enabled') && wp_lazy_loading_enabled('img', 'simple_license_block');
$loading_type = $lazy ? 'lazy' : 'eager'; // 'eager' is the browser default
return "<div>" . esc_html($credit) . "</div>"
."<a target='_blank' href='" . esc_url($attribution_url) . "'>$title</a>"
. "<a class='cc-attribution-box-license' target='_blank' href='" . esc_url($license_url) . "' title='" . esc_attr($license_name) . "' rel='license'>"
. "<img src='" . esc_url($button_url) . "' alt='" . esc_attr($license_name) . " . loading='" . $loading_type . "'></a>";
}

}

public function enqueue_attribution_boxes() {
wp_enqueue_style('cc-attribution-box', plugins_url('css/cc-attribution-box.css', dirname(__FILE__)));
}

public function add_attribution_boxes( $content ) {
return preg_replace_callback(
'/<img\s[^>]*class="wp-image-(\d+)[^>]*>/',
function ($matches) {
return
'<div class="cc-attribution-box-container">'
. $matches[0]
. '<div class="cc-attribution-box"> '
. $this->simple_license_block($matches[1])
. '</div></div>';
},
$content
);
}

public function simple_license_block($att_id) {
/* TODO: this should just be replaced by just using $this->license_block.
* Need to make sure that code can be safely fixed for this purpose,
*/
$license_url = get_post_meta( $att_id, 'license_url', true );
$attribution_url = get_post_meta( $att_id, 'attribution_url', true );
$title = get_the_title( $att_id );
$credit = get_post_meta( $att_id, 'attribution_name', true );

$license_name = $this->license_name( $license_url );
$button_url = $this->license_button_url( $license_url );

if (!$button_url) return '';

$lazy = function_exists('wp_lazy_loading_enabled') && wp_lazy_loading_enabled('img', 'simple_license_block');
$loading_type = $lazy ? 'lazy' : 'eager'; // 'eager' is the browser default
return "<div>" . esc_html($credit) . "</div>"
."<a target='_blank' href='" . esc_url($attribution_url) . "'>$title</a>"
. "<a class='cc-attribution-box-license' target='_blank' href='" . esc_url($license_url) . "' title='" . esc_attr($license_name) . "' rel='license'>"
. "<img src='" . esc_url($button_url) . "' alt='" . esc_attr($license_name) . "' loading='" . $loading_type . "'></a>";
}
}
Loading