PluginProbe
CatFolders – WordPress Media Library Folders & Categories / trunk
CatFolders – WordPress Media Library Folders & Categories vtrunk
2.5.6 2.5.5 trunk 1.6.1 1.8 1.9 2.0 2.2 2.3.1 2.3.2 2.4.4 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4
catfolders / includes / Blocks / GalleryBlock.php

GalleryBlock.php in CatFolders – WordPress Media Library Folders & Categories trunk, at includes/Blocks/GalleryBlock.php

171 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace CatFolders\Blocks;
3
4 use CatFolders\Traits\Singleton;
5
6 class GalleryBlock {
7 use Singleton;
8
9 private $script_handle = 'catfolders-image-gallery';
10
11 public function __construct() {
12 add_action( 'init', array( $this, 'create_block_catfolders_block_init' ) );
13 }
14
15 public function create_block_catfolders_block_init() {
16 wp_register_style( 'catf-photoswipe', CATF_PLUGIN_URL . 'assets/css/photoswipe/photoswipe.css', array(), CATF_VERSION );
17 wp_register_style( 'catf-photoswipe-default-skin', CATF_PLUGIN_URL . 'assets/css/photoswipe/default-skin.css', array(), CATF_VERSION );
18
19 wp_register_script( 'catf-photoswipe', CATF_PLUGIN_URL . 'assets/js/photoswipe/photoswipe.min.js', array(), CATF_VERSION, true );
20 wp_register_script( 'catf-photoswipe-ui-default', CATF_PLUGIN_URL . 'assets/js/photoswipe/photoswipe-ui-default.min.js', array(), CATF_VERSION, true );
21 wp_register_script( 'catf-gallery', CATF_PLUGIN_URL . 'assets/js/photoswipe/catf-photoswipe.js', array(), CATF_VERSION, true );
22
23 register_block_type( __DIR__ . '/build', array( 'render_callback' => array( $this, 'render_block' ) ) );
24 }
25
26 public function render_block( $attributes ) {
27 if ( empty( $attributes['folders'] ) ) {
28 return '';
29 }
30
31 if ( isset( $attributes['enableLightbox'] ) && $attributes['enableLightbox'] ) {
32 wp_enqueue_style( 'catf-photoswipe' );
33 wp_enqueue_style( 'catf-photoswipe-default-skin' );
34 wp_enqueue_script( 'catf-photoswipe' );
35 wp_enqueue_script( 'catf-photoswipe-ui-default' );
36 wp_enqueue_script( 'catf-gallery' );
37 }
38
39 wp_enqueue_style( $this->script_handle, CATF_PLUGIN_URL . 'includes/Blocks/build/style-index.css', array(), CATF_VERSION );
40
41 return $this->generate_html( $attributes );
42 }
43
44 public function get_attachments( $args ) {
45 $selectedFolders = isset( $args['folders'] ) ? array_map( 'intval', $args['folders'] ) : array();
46 if ( ! $selectedFolders ) {
47 return false;
48 }
49
50 global $wpdb;
51 $ids = $selectedFolders;
52 $where_arr[] = '`folder_id` IN (' . implode( ',', $ids ) . ')';
53 $in_not_in = $wpdb->get_col( "SELECT `post_id` FROM {$wpdb->prefix}catfolders_posts" . ' WHERE ' . implode( ' AND ', $where_arr ) );
54 if ( ! $in_not_in ) {
55 return false;
56 }
57
58 $queryArgs = array(
59 'post_type' => 'attachment',
60 'post__in' => $in_not_in,
61 'posts_per_page' => -1,
62 'orderby' => array(
63 'ID' => 'ASC',
64 ),
65 'post_status' => 'inherit',
66 );
67
68 $query = new \WP_Query( $queryArgs );
69
70 $posts = $query->get_posts();
71
72 if ( count( $posts ) < 1 ) {
73 return '';
74 }
75
76 $attachments_data = array();
77 foreach ( $posts as $post ) {
78
79 if ( ! wp_attachment_is_image( $post ) ) {
80 continue;
81 }
82 $attachment_data = array();
83 $attachment_data['id'] = $post->ID;
84
85 $imageSrc = wp_get_attachment_image_src( $post->ID, 'full' );
86 $attachment_data['src'] = $imageSrc[0];
87 $attachment_data['width'] = $imageSrc[1];
88 $attachment_data['height'] = $imageSrc[2];
89
90 $imageAlt = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
91 $imageAlt = empty( $imageAlt ) ? $post->post_title : $imageAlt;
92 $attachment_data['alt'] = $imageAlt;
93
94 $imageCaption = wp_get_attachment_caption( $post->ID );
95 $attachment_data['caption'] = $imageCaption;
96
97 $attachments_data[] = $attachment_data;
98
99 }
100
101 return $attachments_data;
102 }
103
104 public function generate_html( $attributes ) {
105 $attachments = $this->get_attachments( $attributes );
106 $html = '';
107 if ( $attachments && '' !== $attachments ) {
108 $enableLightbox = isset( $attributes['enableLightbox'] ) ? $attributes['enableLightbox'] : false;
109
110 $html .= '<div class="wp-block-catfolders-block-catfolders-gallery catf-wp-block-gallery">';
111
112 $ulClass = '';
113 $ulClass .= ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '';
114
115 if($attributes['layout'] == 'masonry') {
116 $ulClass .= ' catf-blocks-gallery-grid is-style-masonry';
117 } elseif($attributes['layout'] == 'grid') {
118 $ulClass .= ' catf-blocks-gallery-grid is-style-grid';
119 } else {
120 //flex
121 $ulClass .= 'catf-blocks-gallery-flex';
122 }
123
124 $ulClass .= ' catf-columns-' . esc_attr( $attributes['columns'] );
125
126 if ( $enableLightbox ) {
127 $ulClass .= ' catf-gallery-lightbox';
128 }
129
130 $html .= '<ul class="' . esc_attr( $ulClass ) . '">';
131 foreach ( $attachments as $attachment ) {
132 $img_attributes = 'src="' . esc_attr( $attachment['src'] ) . '" alt="' . esc_attr( $attachment['alt'] ) . '"';
133
134 // Add width and height if available
135 if ( ! empty( $attachment['width'] ) && ! empty( $attachment['height'] ) ) {
136 $img_attributes .= ' width="' . esc_attr( $attachment['width'] ) . '" height="' . esc_attr( $attachment['height'] ) . '"';
137 }
138
139 $img = '<img ' . $img_attributes . '>';
140
141 $caption = $attachment['caption'] ? '<figcaption class="wp-block-image-caption">' . esc_html( $attachment['caption'] ) . '</figcaption>' : '';
142 $li = '<li class="catf-blocks-gallery-item wp-block-image">';
143 $li .= '<figure>';
144 if ( 'attachment' === $attributes['thumbnailLink'] || 'media_file' === $attributes['thumbnailLink'] ) {
145 $thumbnailLinkTarget = in_array( $attributes['thumbnailLinkTarget'], array( '_self', '_blank' ) ) ? $attributes['thumbnailLinkTarget'] : '_self';
146 $link = 'attachment' === $attributes['thumbnailLink'] ? get_attachment_link( $attachment['id'] ) : wp_get_attachment_url( $attachment['id'] );
147 $li .= '<a href="' . esc_url( $link ) . '" target="' . esc_attr( $thumbnailLinkTarget ) . '">';
148 }
149 $li .= $img;
150
151 if ( 'attachment' === $attributes['thumbnailLink'] || 'media_file' === $attributes['thumbnailLink'] ) {
152 $li .= '</a>';
153 }
154 if( $attributes['caption'] === true ) {
155 $li .= $caption;
156 }
157
158 $li .= '</figure>';
159 $li .= '</li>';
160
161 $html .= $li;
162 }
163 $html .= '</ul>';
164 $html .= '</div>';
165 }
166 return $html;
167 }
168 }
169
170
171