full-size-image-links.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /*
  3. Plugin Name: Full Size Image Links
  4. Plugin URI: https://meowapps.com
  5. Description: Link to full-size media files from galleries. Blatently ripped from Gallery Custom Links by Jordy Meow
  6. Version: 1.0.0
  7. Author: Zach Isbach, Jordy Meow
  8. Author URI: https://meowapps.com
  9. Text Domain: full-size-image-links
  10. Domain Path: /languages
  11. Licensed under the MIT license:
  12. http://www.opensource.org/licenses/mit-license.php
  13. */
  14. require_once 'vendor/autoload.php';
  15. use DiDom\Document;
  16. use DiDom\Element;
  17. new Full_Size_Image_Links();
  18. class Full_Size_Image_Links {
  19. public function __construct() {
  20. add_filter( 'the_content', array( $this, 'linkify' ), 20 );
  21. }
  22. function get_pathinfo_from_image_src( $image_src ) {
  23. $uploads = wp_upload_dir();
  24. $uploads_url = trailingslashit( $uploads['baseurl'] );
  25. if ( strpos( $image_src, $uploads_url ) === 0 )
  26. return ltrim( substr( $image_src, strlen( $uploads_url ) ), '/');
  27. else if ( strpos( $image_src, wp_make_link_relative( $uploads_url ) ) === 0 )
  28. return ltrim( substr( $image_src, strlen( wp_make_link_relative( $uploads_url ) ) ), '/');
  29. $img_info = parse_url( $image_src );
  30. return ltrim( $img_info['path'], '/' );
  31. }
  32. function resolve_image_id( $url ) {
  33. global $wpdb;
  34. $pattern = '/[_-]\d+x\d+(?=\.[a-z]{3,4}$)/';
  35. $url = preg_replace( $pattern, '', $url );
  36. $url = $this->get_pathinfo_from_image_src( $url );
  37. $query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid LIKE '%s'", '%' . $url . '%' );
  38. $attachment = $wpdb->get_col( $query );
  39. return empty( $attachment ) ? null : $attachment[0];
  40. }
  41. function linkify( $buffer ) {
  42. if (!isset( $buffer ) || trim( $buffer ) === '' )
  43. return $buffer;
  44. $html = new Document();
  45. $html->preserveWhiteSpace();
  46. $html->loadHtml( $buffer, 0 );
  47. $hasChanges = false;
  48. $classes = apply_filters( 'gallery_custom_links_classes', array( '.gallery', '.wp-block-gallery' ) );
  49. foreach ( $classes as $class ) {
  50. foreach ( $html->find( $class . ' img' ) as $element )
  51. $hasChanges = $this->linkify_element( $element ) || $hasChanges;
  52. }
  53. return $hasChanges ? $html->html() : $buffer;
  54. }
  55. function linkify_element( $element ) {
  56. $classes = $element->attr('class');
  57. $mediaId = null;
  58. // Check if the wp-image-xxx class exists
  59. if ( preg_match( '/wp-image-([0-9]{1,10})/i', $classes, $matches ) )
  60. $mediaId = $matches[1];
  61. // Otherwise, resolve the ID from the URL
  62. else {
  63. $url = $element->attr('src');
  64. $mediaId = $this->resolve_image_id( $url );
  65. }
  66. if ( $mediaId ) {
  67. $url = wp_get_attachment_url($mediaId);
  68. $parent = $element->parent();
  69. // Modify an existing link tag
  70. $potentialLinkNode = $parent;
  71. $maxDepth = 10;
  72. do {
  73. if ( $potentialLinkNode->tag === 'a' ) {
  74. $potentialLinkNode->attr( 'href', $url );
  75. $class = $potentialLinkNode->attr( 'class' );
  76. $potentialLinkNode->attr( 'class', $class );
  77. return true;
  78. }
  79. if ( method_exists( $potentialLinkNode, 'parent' ) )
  80. $potentialLinkNode = $potentialLinkNode->parent();
  81. else
  82. break;
  83. }
  84. while ( $potentialLinkNode && $maxDepth-- >= 0 );
  85. // Insert a new link tag
  86. if ( $parent->tag === 'figure' )
  87. $parent = $parent->parent();
  88. $a = new Element('a');
  89. $a->attr( 'href', $url );
  90. $a->appendChild( $parent->children() );
  91. foreach( $parent->children() as $img ) {
  92. $img->remove();
  93. }
  94. $parent->appendChild( $a );
  95. return true;
  96. }
  97. return false;
  98. }
  99. }
  100. ?>