abilities
4 weeks ago
admin
4 weeks ago
compatibility
4 weeks ago
extensions
4 weeks ago
foopluginbase
4 weeks ago
public
4 weeks ago
thumbs
4 weeks ago
asset-manifest.php
4 weeks ago
class-attachment-filters.php
4 weeks ago
class-command-palette.php
4 weeks ago
class-foogallery-animated-gif-support.php
4 weeks ago
class-foogallery-attachment-custom-class.php
4 weeks ago
class-foogallery-attachment-filename.php
4 weeks ago
class-foogallery-attachment-type.php
4 weeks ago
class-foogallery-attachment.php
4 weeks ago
class-foogallery-cache.php
4 weeks ago
class-foogallery-common-fields.php
4 weeks ago
class-foogallery-crop-position.php
4 weeks ago
class-foogallery-datasource-media_library.php
4 weeks ago
class-foogallery-debug.php
4 weeks ago
class-foogallery-delayed-runtime-loader.php
4 weeks ago
class-foogallery-extensions-compatibility.php
4 weeks ago
class-foogallery-force-https.php
4 weeks ago
class-foogallery-lazyload.php
4 weeks ago
class-foogallery-license-constant-handler.php
4 weeks ago
class-foogallery-lightbox.php
4 weeks ago
class-foogallery-paging.php
4 weeks ago
class-foogallery-password-protect.php
4 weeks ago
class-foogallery-sitemaps.php
4 weeks ago
class-foogallery-widget.php
4 weeks ago
class-foogallery.php
4 weeks ago
class-gallery-advanced-settings.php
4 weeks ago
class-il8n.php
4 weeks ago
class-override-thumbnail.php
4 weeks ago
class-posttypes.php
4 weeks ago
class-previews.php
4 weeks ago
class-retina.php
4 weeks ago
class-thumbnail-dimensions.php
4 weeks ago
class-thumbnails.php
4 weeks ago
class-version-check.php
4 weeks ago
constants.php
4 weeks ago
functions.php
4 weeks ago
gallery-management-functions.php
4 weeks ago
includes.php
4 weeks ago
index.php
4 weeks ago
render-functions.php
4 weeks ago
class-foogallery-attachment-type.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists('FooGallery_Attachment_Type') ) { |
| 8 | |
| 9 | class FooGallery_Attachment_Type { |
| 10 | |
| 11 | function __construct() { |
| 12 | //determine the type of the attachment |
| 13 | add_action( 'foogallery_attachment_instance_after_load', array( $this, 'determine_type' ), 99, 2 ); |
| 14 | |
| 15 | //add attachment field for custom type |
| 16 | add_filter( 'foogallery_attachment_custom_fields', array( $this, 'add_override_type_field' ), 50 ); |
| 17 | |
| 18 | //add attributes to front-end anchor |
| 19 | add_filter( 'foogallery_attachment_html_link_attributes', array( $this, 'alter_link_attributes' ), 30, 3 ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Determines the type of the item |
| 24 | * |
| 25 | * @param $foogallery_attachment FooGalleryAttachment |
| 26 | * @param $post WP_Post |
| 27 | */ |
| 28 | function determine_type( $foogallery_attachment, $post ) { |
| 29 | if ( isset( $foogallery_attachment->is_video ) && $foogallery_attachment->is_video === true ) { |
| 30 | $foogallery_attachment->type = 'video'; |
| 31 | |
| 32 | //check if we have set the type to embed |
| 33 | if ( isset( $foogallery_attachment->is_embed ) && $foogallery_attachment->is_embed === true ) { |
| 34 | $foogallery_attachment->type = 'embed'; |
| 35 | } |
| 36 | } else { |
| 37 | $link = foogallery_gallery_template_setting( 'thumbnail_link', 'image' ); |
| 38 | if ( 'page' === $link || ( 'parent_post' === $link && ! empty( $foogallery_attachment->parent_post_url ) ) || ( 'custom' === $link && ! empty( $foogallery_attachment->custom_url ) ) ) { |
| 39 | // let's check if the custom URL is an image. |
| 40 | if ( 'custom' === $link && $this->check_maybe_image( $foogallery_attachment->custom_url ) ) { |
| 41 | $foogallery_attachment->type = 'image'; |
| 42 | } else { |
| 43 | $foogallery_attachment->type = 'iframe'; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // check if we have overridden the type for the attachment |
| 49 | $override_type = get_post_meta( $foogallery_attachment->ID, '_foogallery_override_type', true ); |
| 50 | if ( ! empty( $override_type ) ) { |
| 51 | $foogallery_attachment->type = sanitize_title( $override_type ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Checks to see if the url is maybe an image based on the extension |
| 57 | * |
| 58 | * @param string $url The url to check. |
| 59 | * |
| 60 | * @return false |
| 61 | */ |
| 62 | private function check_maybe_image( $url ) { |
| 63 | $known_image_extensions = array( 'gif', 'jpg', 'jpeg', 'png', 'tiff', 'tif', 'bmp', 'svg', 'webp' ); |
| 64 | $url_extension = pathinfo( $url, PATHINFO_EXTENSION ); |
| 65 | return in_array( $url_extension, $known_image_extensions, true ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Adds a override type field to the attachments |
| 70 | * |
| 71 | * @param $fields array |
| 72 | * |
| 73 | * @return array |
| 74 | */ |
| 75 | function add_override_type_field( $fields ) { |
| 76 | $fields['foogallery_override_type'] = array( |
| 77 | 'label' => __( 'Override Type', 'foogallery' ), |
| 78 | 'input' => 'text', |
| 79 | 'helps' => __( 'Override the type of the attachment used by lightbox', 'foogallery' ), |
| 80 | 'exclusions' => array( 'audio', 'video' ), |
| 81 | ); |
| 82 | |
| 83 | return $fields; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * @uses "foogallery_attachment_html_link_attributes" filter |
| 89 | * |
| 90 | * @param $attr |
| 91 | * @param $args |
| 92 | * @param object|FooGalleryAttachment $attachment |
| 93 | * |
| 94 | * @return mixed |
| 95 | */ |
| 96 | public function alter_link_attributes( $attr, $args, $attachment ) { |
| 97 | $attr['data-type'] = $attachment->type; |
| 98 | |
| 99 | return $attr; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 |