Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
fix(image-ids): support image-attributes hook
Browse files Browse the repository at this point in the history
Also include data-attachment-id-source to debug future problems.
  • Loading branch information
luisherranz committed Sep 7, 2018
1 parent 7a24626 commit d7a7f22
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public static function setup(HTMLPurifier_HTMLDefinition $def)
$def->addAttribute('img', 'srcset', 'Text');
$def->addAttribute('img', 'sizes', 'Text');
$def->addAttribute('img', 'data-attachment-id', 'Text');
$def->addAttribute('img', 'data-attachment-id-source', 'Text');

// IFRAME
$def->addAttribute('iframe', 'allowfullscreen', 'Bool');
Expand Down
11 changes: 10 additions & 1 deletion wp-pwa.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,21 @@ function __construct()
add_action('wp_head', array($this,'amp_add_canonical'));

add_filter('wp_get_attachment_link', array( $this, 'add_id_to_gallery_images'), 10, 2);
add_filter('wp_get_attachment_image_attributes', array( $this, 'add_id_to_gallery_image_attributes'), 10, 2);
}

function add_id_to_gallery_image_attributes($attrs, $attachment) {
$attrs['data-attachment-id'] = $attachment->ID;
$attrs['data-attachment-id-source'] = 'image-attributes-hook';
return $attrs;
}

function add_id_to_gallery_images($html, $attachment_id) {
$attachment_id = intval($attachment_id);
$html = str_replace(
'<img ',
sprintf(
'<img data-attachment-id="%1$d" ',
'<img data-attachment-id="%1$d" data-attachment-id-source="attachment-link-hook"',
$attachment_id
),
$html
Expand Down Expand Up @@ -260,11 +267,13 @@ function add_image_ids($data) {
$imgIds[] = intval($dataAttachmentId);
} elseif ($wpImage && isset($wpImage[1])) {
$image->setAttribute('data-attachment-id', $wpImage[1]);
$image->setAttribute('data-attachment-id-source', 'wp-image-class');
$imgIds[] = intval($wpImage[1]);
} else {
$id = $this->get_attachment_id($image->src);
if ($id !== 0) {
$image->setAttribute('data-attachment-id', $id);
$image->setAttribute('data-attachment-id-source', 'wp-query');
$imgIds[] = intval($id);
}
}
Expand Down

0 comments on commit d7a7f22

Please sign in to comment.