array( $this, 'render_block' ) ) ); } public function render_block( $attributes ) { if ( empty( $attributes['folders'] ) ) { return ''; } if ( isset( $attributes['enableLightbox'] ) && $attributes['enableLightbox'] ) { wp_enqueue_style( 'catf-photoswipe' ); wp_enqueue_style( 'catf-photoswipe-default-skin' ); wp_enqueue_script( 'catf-photoswipe' ); wp_enqueue_script( 'catf-photoswipe-ui-default' ); wp_enqueue_script( 'catf-gallery' ); } wp_enqueue_style( $this->script_handle, CATF_PLUGIN_URL . 'includes/Blocks/build/style-index.css', array(), CATF_VERSION ); return $this->generate_html( $attributes ); } public function get_attachments( $args ) { $selectedFolders = isset( $args['folders'] ) ? array_map( 'intval', $args['folders'] ) : array(); if ( ! $selectedFolders ) { return false; } global $wpdb; $ids = $selectedFolders; $where_arr[] = '`folder_id` IN (' . implode( ',', $ids ) . ')'; $in_not_in = $wpdb->get_col( "SELECT `post_id` FROM {$wpdb->prefix}catfolders_posts" . ' WHERE ' . implode( ' AND ', $where_arr ) ); if ( ! $in_not_in ) { return false; } $queryArgs = array( 'post_type' => 'attachment', 'post__in' => $in_not_in, 'posts_per_page' => -1, 'orderby' => array( 'ID' => 'ASC', ), 'post_status' => 'inherit', ); $query = new \WP_Query( $queryArgs ); $posts = $query->get_posts(); if ( count( $posts ) < 1 ) { return ''; } $attachments_data = array(); foreach ( $posts as $post ) { if ( ! wp_attachment_is_image( $post ) ) { continue; } $attachment_data = array(); $attachment_data['id'] = $post->ID; $imageSrc = wp_get_attachment_image_src( $post->ID, 'full' ); $attachment_data['src'] = $imageSrc[0]; $attachment_data['width'] = $imageSrc[1]; $attachment_data['height'] = $imageSrc[2]; $imageAlt = get_post_meta( $post->ID, '_wp_attachment_image_alt', true ); $imageAlt = empty( $imageAlt ) ? $post->post_title : $imageAlt; $attachment_data['alt'] = $imageAlt; $imageCaption = wp_get_attachment_caption( $post->ID ); $attachment_data['caption'] = $imageCaption; $attachments_data[] = $attachment_data; } return $attachments_data; } public function generate_html( $attributes ) { $attachments = $this->get_attachments( $attributes ); $html = ''; if ( $attachments && '' !== $attachments ) { $enableLightbox = isset( $attributes['enableLightbox'] ) ? $attributes['enableLightbox'] : false; $html .= '