false, 'html' => '

'. esc_html__('Load failed!', 'wpmf') .'

')); } $request_body = file_get_contents('php://input'); $data = json_decode($request_body, true); if (empty($data['items']) && empty($data['gallery_folder_id'])) { $html = '
'; wp_send_json(array('status' => false, 'html' => $html)); } $html = do_shortcode('[wpmf_gallery is_divi="1" include="'. esc_attr($data['items']) .'" display="' . esc_attr($data['display']) . '" columns="' . esc_attr($data['columns']) . '" size="' . esc_attr($data['size']) . '" targetsize="' . esc_attr($data['targetsize']) . '" link="' . esc_attr($data['link']) . '" wpmf_orderby="' . esc_attr($data['orderby']) . '" wpmf_order="' . esc_attr($data['order']) . '" gutterwidth="' . esc_attr($data['gutterwidth']) . '" border_width="' . esc_attr($data['border_width']) . '" border_style="' . esc_attr($data['border_style']) . '" border_color="' . esc_attr($data['border_color']) . '" img_shadow="' . esc_attr($data['img_shadow']) . '" img_border_radius="' . esc_attr($data['border_radius']) . '" wpmf_autoinsert="' . esc_attr($data['wpmf_autoinsert']) . '" wpmf_folder_id="' . esc_attr($data['wpmf_folder_id']) . '"]'); wp_send_json(array('status' => true, 'html' => $html)); } /** * Load elementor widget * * @return void */ public function loadElementorWidget() { require_once(WP_MEDIA_FOLDER_PLUGIN_DIR . 'class/elementor-widgets/class-gallery-elementor-widget.php'); if (defined('ELEMENTOR_VERSION') && version_compare(ELEMENTOR_VERSION, '3.5', '<')) { \Elementor\Plugin::instance()->widgets_manager->register_widget_type(new \WpmfGalleryElementorWidget()); } else { \Elementor\Plugin::instance()->widgets_manager->register(new \WpmfGalleryElementorWidget()); } } /** * Override the attributes on the image * * @param array $attributes Image attributes * * @return mixed */ public function jetpackLazyImagesNewAttributes($attributes) { if (isset($attributes) && isset($attributes['data-type']) && $attributes['data-type'] === 'wpmfgalleryimg') { $attributes['src'] = $attributes['data-lazy-src']; unset($attributes['data-lazy-src']); } return $attributes; } /** * Compatible with elementor plugin * * @param array $schema Params of gallery * * @return mixed */ public function mediaGalleryInstanceSchema($schema) { $schema['display'] = array( 'type' => 'string', 'enum' => array( 'default', 'masonry', 'portfolio', 'slider', ), 'default' => 'default' ); $schema['wpmf_autoinsert'] = array( 'type' => 'string', 'enum' => array(0, 1), 'default' => 0 ); $schema['wpmf_orderby'] = array( 'type' => 'string', 'enum' => array( 'post__in', 'rand', 'title', 'date' ), 'default' => 'post__in' ); $schema['wpmf_order'] = array( 'type' => 'string', 'enum' => array( 'ASC', 'DESC' ), 'default' => 'ASC' ); return $schema; } /** * Includes styles and some scripts * * @return void */ public function galleryScripts() { $is_builder = (function_exists('fusion_is_preview_frame') && fusion_is_preview_frame()) || (function_exists('fusion_is_builder_frame') && fusion_is_builder_frame()); if (!$is_builder) { wp_register_style( 'wpmf-gallery-popup-style', plugins_url('/assets/css/display-gallery/magnific-popup.css', dirname(__FILE__)), array(), '0.9.9' ); wp_register_style( 'wpmf-gallery-style', plugins_url('/assets/css/display-gallery/style-display-gallery.css', dirname(__FILE__)), array(), WPMF_VERSION ); wp_register_script( 'wordpresscanvas-imagesloaded', plugins_url('/assets/js/display-gallery/imagesloaded.pkgd.min.js', dirname(__FILE__)), array('jquery'), '3.1.5', true ); wp_register_script( 'wpmf-gallery-popup', plugins_url('/assets/js/display-gallery/jquery.magnific-popup.min.js', dirname(__FILE__)), array('jquery'), '0.9.9', true ); wp_register_style( 'wpmf-slick-style', WPMF_PLUGIN_URL . 'assets/js/slick/slick.css', array(), WPMF_VERSION ); wp_register_style( 'wpmf-slick-theme-style', WPMF_PLUGIN_URL . 'assets/js/slick/slick-theme.css', array(), WPMF_VERSION ); wp_register_script( 'wpmf-slick-script', WPMF_PLUGIN_URL . 'assets/js/slick/slick.min.js', array('jquery'), WPMF_VERSION, true ); wp_register_script( 'wpmf-gallery', plugins_url('assets/js/display-gallery/site_gallery.js', dirname(__FILE__)), array('jquery', 'wordpresscanvas-imagesloaded'), WPMF_VERSION, true ); wp_localize_script( 'wpmf-gallery', 'wpmfggr', $this->localizeScript() ); } } /** * Enqueue script styles by editor * * @param string $editor Editor name * * @return void */ public function enqueueScript($editor = '') { if ($editor === 'divi' || $editor === 'avada') { wp_enqueue_style('wpmf-slick-style'); wp_enqueue_style('wpmf-slick-theme-style'); wp_enqueue_script('wpmf-slick-script'); wp_enqueue_style('wpmf-gallery-style'); wp_enqueue_script('wordpresscanvas-imagesloaded'); wp_enqueue_script('jquery-masonry'); } } /** * Localize a script. * Works only if the script has already been added. * * @return array */ public function localizeScript() { $option_usegellery_lightbox = get_option('wpmf_usegellery_lightbox'); $option_current_theme = get_option('current_theme'); $slider_animation = get_option('wpmf_slider_animation'); $smush_settings = get_option('wp-smush-settings'); return array( 'wpmf_lightbox_gallery' => (int)$option_usegellery_lightbox, 'wpmf_current_theme' => $option_current_theme, 'slider_animation' => $slider_animation, 'smush_lazyload' => (is_plugin_active('wp-smushit/wp-smush.php') && isset($smush_settings) && isset($smush_settings['lazy_load']) && $smush_settings['lazy_load']) ? true : false, 'img_url' => WPMF_PLUGIN_URL . 'assets/images/', ); } /** * Includes some scripts * * @return void */ public function galleryEnqueueAdminScripts() { global $pagenow; if ($pagenow !== 'upload.php') { wp_enqueue_script( 'wpmf-gallery-admin-js', plugins_url('assets/js/display-gallery/admin_gallery.js', dirname(__FILE__)), array('jquery'), WPMF_VERSION, true ); } } /** * Render gallery by shortcode * * @param array $attr All attribute in shortcode * * @return string */ public function gallery($attr) { if (!is_array($attr)) { return ''; } wp_enqueue_style( 'wpmf-material-icon', 'https://fonts.googleapis.com/css?family=Material+Icons|Material+Icons+Outlined' ); $post = get_post(); static $instance = 0; $instance++; $gallery_configs = wpmfGetOption('gallery_settings'); $caption_lightbox = wpmfGetOption('caption_lightbox_gallery'); if (isset($gallery_configs['theme']['slider_theme']['auto_animation'])) { $autoplay = $gallery_configs['theme']['slider_theme']['auto_animation']; } else { $autoplay = 0; } if (isset($gallery_configs['theme']['slider_theme']['duration'])) { $duration = $gallery_configs['theme']['slider_theme']['duration']; } else { $duration = 4000; } if (empty($attr['crop_image'])) { $attr['crop_image'] = 1; } $attrs = shortcode_atts(array( 'order' => 'ASC', 'orderby' => 'menu_order ID', 'id' => $post ? $post->ID : 0, 'aspect_ratio' => 'default', 'columns' => 3, 'gutterwidth' => 5, 'link' => 'post', 'size' => 'thumbnail', 'targetsize' => 'large', 'display' => 'default', 'wpmf_orderby' => 'post__in', 'wpmf_order' => 'ASC', 'customlink' => 0, 'bottomspace' => 'default', 'hidecontrols' => 'false', 'class' => '', 'include' => '', 'exclude' => '', 'wpmf_folder_id' => array(), 'wpmf_autoinsert' => '', 'include_children' => 0, 'img_border_radius' => 0, 'border_width' => 0, 'border_color' => 'transparent', 'border_style' => 'solid', 'img_shadow' => '', 'autoplay' => $autoplay, 'duration' => $duration, 'is_divi' => 0, 'limit' => -1, 'crop_image' => 1, 'align' => 'none' ), $attr, 'gallery'); foreach ($attrs as $attr_key => $attr_value) { ${$attr_key} = $attr_value; } if (!is_array($wpmf_folder_id)) { $wpmf_folder_id = explode(',', $wpmf_folder_id); } foreach ($wpmf_folder_id as $folderIndex => $folder_id) { if ((int)$folder_id === 0) { unset($wpmf_folder_id[$folderIndex]); } } $custom_class = trim($class); if (isset($wpmf_autoinsert) && ((int)$wpmf_autoinsert === 1 || $wpmf_autoinsert === 'on') && !empty($wpmf_folder_id)) { if ($wpmf_orderby === 'post__in') { $wpmf_orderby = 'meta_value_num'; $wpmf_order = 'ASC'; $meta_key = 'wpmf_order'; } $args = array( 'posts_per_page' => $limit, 'post_status' => 'any', 'post_type' => 'attachment', 'post_mime_type' => array('image/jpeg', 'image/jpg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon', 'image/webp', 'image/avif', 'application/pdf'), 'order' => $wpmf_order, 'orderby' => $wpmf_orderby, 'wpmf_gallery' => 1 ); if (!empty($meta_key)) { $args['meta_key'] = $meta_key; } $args['tax_query'] = array( array( 'taxonomy' => WPMF_TAXO, 'field' => 'term_id', 'terms' => $wpmf_folder_id, 'operator' => 'IN', 'include_children' => ((int) $include_children === 1) ) ); /** * Filter gallery query argument to get images. * * @param array Gallery query arguments * * @return array */ $args = apply_filters('wpmf_gallery_query_args', $args); $query = new WP_Query($args); $_attachments = $query->get_posts(); $gallery_items = array(); $last_folder_parent = $this->getFolderParent($wpmf_folder_id[0]); foreach ($_attachments as $key => $val) { if ($last_folder_parent->slug !== 'nextcloud' || !strpos($val->post_mime_type, 'avif')) { // hide avif in nextcloud folder $gallery_items[$val->ID] = $_attachments[$key]; } } } else { $args = array( 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => array('image/jpeg', 'image/jpg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon', 'image/webp', 'image/avif', 'application/pdf'), 'order' => $wpmf_order, 'orderby' => $wpmf_orderby, 'wpmf_gallery' => 1 ); if (!empty($meta_key)) { $args['meta_key'] = $meta_key; } //var_dump($args);die; if (!empty($include)) { $args['include'] = $include; $_attachments = get_posts($args); $gallery_items = array(); foreach ($_attachments as $key => $val) { $gallery_items[$val->ID] = $_attachments[$key]; } } elseif (!empty($exclude)) { $args['exclude'] = $exclude; $args['post_parent'] = (int) $id; $gallery_items = get_children($args); } else { $args['post_parent'] = (int) $id; $gallery_items = get_children($args); } } if (is_feed()) { $output = "\n"; foreach ($gallery_items as $att_id => $attachment) { $output .= wp_get_attachment_link($att_id, $size, true) . "\n"; } return $output; } $columns = intval($columns); $selector = 'gallery-' . $instance; $size_class = sanitize_html_class($size); /** * Filter to enable/disable download image on gallery * * @param boolean Enable/disable download image * * @return boolean */ $enable_download = apply_filters('wpmf_gallery_enable_download', false); $class = array(); $class[] = 'wpmf-gallery'; if ($link === 'file' || $link === 'none' || $link === 'post') { $customlink = false; } else { $customlink = true; } if (!empty($custom_class)) { $class[] = esc_attr($custom_class); } if (!$customlink) { $class[] = 'gallery-link-' . $link; } if ($link === 'file') { wp_enqueue_script('wpmf-gallery-popup'); } wp_enqueue_script('jquery'); wp_enqueue_style('wpmf-gallery-style'); wp_enqueue_style('wpmf-gallery-popup-style'); switch ($display) { case 'slider': require(WP_MEDIA_FOLDER_PLUGIN_DIR . 'themes-gallery/gallery-slider.php'); break; case 'masonry': require(WP_MEDIA_FOLDER_PLUGIN_DIR . 'themes-gallery/gallery-mansory.php'); break; case 'portfolio': require(WP_MEDIA_FOLDER_PLUGIN_DIR . 'themes-gallery/gallery-portfolio.php'); break; default: require(WP_MEDIA_FOLDER_PLUGIN_DIR . 'themes-gallery/gallery-default.php'); break; } return $output; } /** * Get vimeo video ID from URL * * @param string $url URl of video * * @return mixed|string */ public function getVimeoVideoIdFromUrl($url = '') { $regs = array(); $id = ''; $vimeo_pattern = '%^https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)(?:[?]?.*)$%im'; if (preg_match($vimeo_pattern, $url, $regs)) { $id = $regs[3]; } return $id; } /** * Display gallery on frontend * * @param string $blank The current output * @param array $attr Attributes of the gallery shortcode. * * @return string $output The gallery output. Default empty. */ public function galleryDefaultShortcode($blank, $attr) { $output = $this->gallery($attr); return $output; } /** * Display gallery from folder on frontend * * @param array $attr Attributes of the gallery shortcode. * * @return string $output The gallery output. Default empty. */ public function galleryShortcode($attr) { $output = $this->gallery($attr); return $output; } /** * Get attachment download link * * @param integer $attachment_id Attachment ID * * @return false|string|string[] */ public function wpmfGalleryGetDownloadLink($attachment_id) { $drive_type = get_post_meta($attachment_id, 'wpmf_drive_type', true); if (empty($drive_type)) { $download_link = wp_get_attachment_image_url($attachment_id, 'full'); $type = 'local'; } else { $drive_id = get_post_meta($attachment_id, 'wpmf_drive_id', true); switch ($drive_type) { case 'onedrive': $download_link = admin_url('admin-ajax.php') . '?action=wpmf_onedrive_download&id=' . urlencode($drive_id) . '&link=true&dl=1'; break; case 'onedrive_business': $download_link = admin_url('admin-ajax.php') . '?action=wpmf_onedrive_business_download&id=' . urlencode($drive_id) . '&link=true&dl=1'; break; case 'google_drive': $download_link = admin_url('admin-ajax.php') . '?action=wpmf-download-file&id=' . urlencode($drive_id) . '&dl=1'; break; case 'dropbox': $download_link = admin_url('admin-ajax.php') . '?action=wpmf-dbxdownload-file&id=' . urlencode($drive_id) . '&link=true&dl=1'; break; default: $download_link = wp_get_attachment_image_url($attachment_id, 'full'); } $download_link = str_replace('&', '&', $download_link); $download_link = str_replace('&', '&', $download_link); $type = 'cloud'; } return array('download_link' => $download_link, 'type' => $type); } /** * Generate html attachment link * * @param integer $id Id of image * @param string $size Size of image * @param boolean $permalink Permalink of current image * @param string $targetsize Optional. Image size. Accepts any valid image size, or an array of width * @param boolean $customlink Custom link URL * @param string $target Target of link * @param string $pos Possition * * @return mixed|string */ public function getAttachmentLink( $id = 0, $size = 'thumbnail', $permalink = false, $targetsize = 'large', $customlink = false, $target = '_self', $pos = 0 ) { $id = intval($id); $_post = get_post($id); $url = wp_get_attachment_url($_post->ID); if (empty($_post) || ('attachment' !== $_post->post_type) || !$url) { return __('Missing Attachment', 'wpmf'); } $lb = 0; $url = get_post_meta($_post->ID, _WPMF_GALLERY_PREFIX . 'custom_image_link', true); if ($url === '') { if ($permalink) { if ($_post->post_mime_type === 'application/pdf') { $target = '_blank'; } } elseif ($targetsize) { $lb = 1; if ($_post->post_mime_type === 'application/pdf') { $target = '_blank'; $lb = 0; } } } $caption_lightbox = wpmfGetOption('caption_lightbox_gallery'); if (!empty($caption_lightbox) && $_post->post_excerpt !== '') { $title = $_post->post_excerpt; } else { $title = $_post->post_title; } $alt_post = get_post_meta($_post->ID, '_wp_attachment_image_alt', true); if ($alt_post === '') { $alt_post = $_post->post_title; } if ($size && 'none' !== $size) { $drive_id = get_post_meta($id, 'wpmf_drive_id', true); if (!empty($drive_id)) { $text = ''. esc_attr($alt_post) .''; } else { $text = ''. esc_attr($alt_post) .''; } } else { $text = ''; } if (trim($text) === '') { $text = $_post->post_title; } $current_theme = get_option('current_theme'); if (isset($current_theme) && $current_theme === 'Gleam') { $tclass = 'fancybox'; } else { $tclass = ''; } $remote_video = get_post_meta($id, 'wpmf_remote_video_link', true); if (empty($remote_video)) { $class = $tclass . ' not_video'; } else { $class = $tclass . ' isvideo'; } $lightbox_urls = $this->getLightboxUrl($id, $targetsize); $url = $lightbox_urls['url']; if (defined('WPMF_DISABLE_GALLERY_TITLE')) { return '' . $text . ''; } else { return '' . $text . ''; } } /** * Display settings gallery when custom gallery in back-end * * @return void */ public function galleryPrintMediaTemplates() { if (isset($_GET['action']) && $_GET['action'] === 'elementor') { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No action, nonce is not required return; } $cf = wpmfGetOption('gallery_settings'); $display_types = array( 'default' => __('Default', 'wpmf'), 'masonry' => __('Masonry', 'wpmf'), 'portfolio' => __('Portfolio', 'wpmf'), 'slider' => __('Slider', 'wpmf'), ); ?> __('Image gallery link to', 'wpmf'), 'input' => 'html', 'html' => ' ' ); $target_value = get_post_meta($post->ID, '_gallery_link_target', true); $form_fields['gallery_link_target'] = array( 'label' => __('Link target', 'wpmf'), 'input' => 'html', 'html' => ' ' ); return $form_fields; } /** * Save custom field for attachment * Based on /wp-admin/includes/media.php * * @param array $post An array of post data. * @param array $attachment An array of attachment metadata. * * @return mixed $post */ public function galleryAttachmentFieldsToSave($post, $attachment) { if (isset($attachment['wpmf_gallery_custom_image_link'])) { update_post_meta( $post['ID'], _WPMF_GALLERY_PREFIX . 'custom_image_link', esc_url_raw($attachment['wpmf_gallery_custom_image_link']) ); } if (isset($attachment['gallery_link_target'])) { update_post_meta($post['ID'], '_gallery_link_target', $attachment['gallery_link_target']); } return $post; } /** * Enqueue styles and scripts for gutenberg * * @return void */ public function addEditorAssets() { wp_register_script( 'wordpresscanvas-imagesloaded', WPMF_PLUGIN_URL . 'assets/js/display-gallery/imagesloaded.pkgd.min.js', array('jquery'), '3.1.5', true ); wp_enqueue_script('jquery-masonry'); global $pagenow; $deps = (isset($pagenow) && $pagenow === 'widgets.php') ? array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-data', 'wp-block-editor', 'lodash', 'wordpresscanvas-imagesloaded') : array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-data', 'wp-editor', 'lodash', 'wordpresscanvas-imagesloaded'); wp_enqueue_script( 'wpmf_gallery_blocks', WPMF_PLUGIN_URL . 'assets/js/blocks/gallery/block.js', $deps, WPMF_VERSION ); wp_enqueue_style( 'wpmf-slick-style', WPMF_PLUGIN_URL . 'assets/js/slick/slick.css', array(), WPMF_VERSION ); wp_enqueue_style( 'wpmf-slick-theme-style', WPMF_PLUGIN_URL . 'assets/js/slick/slick-theme.css', array(), WPMF_VERSION ); wp_enqueue_script( 'wpmf-slick-script', WPMF_PLUGIN_URL . 'assets/js/slick/slick.min.js', array('jquery'), WPMF_VERSION, true ); $sizes = apply_filters('image_size_names_choose', array( 'thumbnail' => __('Thumbnail', 'wpmf'), 'medium' => __('Medium', 'wpmf'), 'large' => __('Large', 'wpmf'), 'full' => __('Full Size', 'wpmf'), )); $sizes_value = json_decode(get_option('wpmf_gallery_image_size_value')); if (!empty($sizes_value)) { foreach ($sizes as $k => $size) { if (!in_array($k, $sizes_value)) { unset($sizes[$k]); } } } $gallery_configs = wpmfGetOption('gallery_settings'); $params = array( 'l18n' => array( 'block_gallery_title' => __('WP Media Folder Gallery', 'wpmf'), 'no_post_found' => __('No post found', 'wpmf'), 'select_label' => __('Select a News Block', 'wpmf') ), 'vars' => array( 'sizes' => $sizes, 'gallery_configs' => $gallery_configs, 'wpmf_nonce' => wp_create_nonce('wpmf_nonce'), 'ajaxurl' => admin_url('admin-ajax.php'), 'block_cover' => WPMF_PLUGIN_URL .'assets/js/blocks/gallery/preview.png' ) ); wp_localize_script('wpmf_gallery_blocks', 'wpmf_blocks', $params); } /** * Update image infos * * @return void */ public function galleryBlockUpdateImageInfos() { if (empty($_REQUEST['wpmf_nonce']) || !wp_verify_nonce($_REQUEST['wpmf_nonce'], 'wpmf_nonce')) { die(); } if (isset($_REQUEST['id']) && isset($_REQUEST['title']) && isset($_REQUEST['caption']) && isset($_REQUEST['custom_link'])) { if (!current_user_can('edit_post', (int)$_REQUEST['id'])) { wp_send_json(array('status' => false, 'msg' => esc_html__('You not have a permission to update image information!', 'wpmf'))); } $my_post = array( 'ID' => (int)$_REQUEST['id'], 'post_title' => $_REQUEST['title'], 'post_excerpt' => $_REQUEST['caption'], ); $post_id = wp_update_post($my_post); update_post_meta($post_id, _WPMF_GALLERY_PREFIX . 'custom_image_link', $_REQUEST['custom_link']); update_post_meta($post_id, '_gallery_link_target', $_REQUEST['link_target']); if (is_wp_error($post_id)) { wp_send_json(array( 'status' => false )); } else { wp_send_json(array( 'status' => true, 'infos' => array('title' => $_REQUEST['title'], 'caption' => $_REQUEST['caption'], 'custom_link' => $_REQUEST['custom_link'], 'link_target' => $_REQUEST['link_target']) )); } } } /** * Load image title * * @return void */ public function galleryBlockLoadImageInfos() { if (empty($_REQUEST['wpmf_nonce']) || !wp_verify_nonce($_REQUEST['wpmf_nonce'], 'wpmf_nonce')) { die(); } $titles = array(); $captions = array(); $custom_links = array(); $link_targets = array(); if (isset($_REQUEST['ids'])) { $ids = explode(',', $_REQUEST['ids']); if (!empty($ids)) { foreach ($ids as $id) { $image = get_post($id); if (empty($image)) { continue; } $url = get_post_meta($id, _WPMF_GALLERY_PREFIX . 'custom_image_link', true); $target = get_post_meta($id, '_gallery_link_target', true); $titles[$id] = $image->post_title; $captions[$id] = $image->post_excerpt; $custom_links[$id] = $url; $link_targets[$id] = $target; } } } wp_send_json(array( 'status' => true, 'titles' => $titles, 'captions' => $captions, 'custom_links' => $custom_links, 'link_targets' => $link_targets )); } /** * Get image from folder * * @return void */ public function getImagesFromFolder() { if (empty($_REQUEST['wpmf_nonce']) || !wp_verify_nonce($_REQUEST['wpmf_nonce'], 'wpmf_nonce')) { die(); } $folders = explode(',', $_REQUEST['ids']); $orderby = isset($_REQUEST['orderby']) ? $_REQUEST['orderby'] : 'title'; $order = isset($_REQUEST['order']) ? $_REQUEST['order'] : 'ASC'; $args = array( 'posts_per_page' => -1, 'post_type' => 'attachment', 'post_status' => 'any', 'orderby' => $orderby, 'order' => $order, 'tax_query' => array( array( 'taxonomy' => WPMF_TAXO, 'field' => 'term_id', 'terms' => $folders, 'operator' => 'IN', 'include_children' => false ) ) ); $query = new WP_Query($args); $attachments = $query->get_posts(); $last_folder_parent = $this->getFolderParent($folders[0]); $list_images = array(); foreach ($attachments as $attachment) { if (strpos($attachment->post_mime_type, 'image') !== false && ($last_folder_parent->slug !== 'nextcloud' || !strpos($attachment->post_mime_type, 'avif'))) { $custom_link = get_post_meta($attachment->ID, _WPMF_GALLERY_PREFIX . 'custom_image_link', true); $target = get_post_meta($attachment->ID, '_gallery_link_target', true); $details = array( 'id' => $attachment->ID, 'title' => $attachment->post_title, 'url' => wp_get_attachment_url($attachment->ID), 'caption' => $attachment->post_excerpt, 'custom_link' => $custom_link, 'link_target' => $target ); $metadata = wp_get_attachment_metadata($attachment->ID); if (!empty($metadata['sizes'])) { $sizes = $metadata['sizes']; foreach ($sizes as $size => &$value) { $url = wp_get_attachment_image_src($attachment->ID, $size); $value['url'] = $url[0]; } $details['sizes'] = $sizes; } $list_images[] = $details; } } wp_send_json(array('status' => true, 'images' => $list_images)); } /** * Get last folder parent * * @param integer $folder_id Folder ID * * @return object */ public function getFolderParent($folder_id) { if ($folder_id) { $term = get_term($folder_id, 'wpmf-category'); if ($term && $term->parent) { $parent = $this->getFolderParent($term->parent); } else { return $term; } } return $parent; } /** * Get lightbox URL and type * * @param integer $attachmentID Attachment ID * @param string $targetsize Lightbox size * * @return array */ public function getLightboxUrl($attachmentID, $targetsize) { $type = 'image'; $item_url = get_post_meta($attachmentID, _WPMF_GALLERY_PREFIX . 'custom_image_link', true); if ($item_url === '') { if ($targetsize) { $attachment = get_post($attachmentID); if ($attachment->post_mime_type === 'application/pdf') { $item_url = wp_get_attachment_url($attachmentID); } else { $item_url = wp_get_attachment_image_url($attachmentID, $targetsize); } } else { $item_url = wp_get_attachment_image_url($attachmentID, $targetsize); } } $remote_video = get_post_meta($attachmentID, 'wpmf_remote_video_link', true); $url = (!empty($remote_video)) ? $remote_video : $item_url; if ((!empty($remote_video)) && strpos($url, 'vimeo') !== false) { $vimeo_id = WpmfHelper::getVimeoVideoIdFromUrl($url); $url = 'https://player.vimeo.com/video/' . $vimeo_id; $type = 'iframe'; } if ((!empty($remote_video)) && (strpos($remote_video, 'youtube') !== false || strpos($remote_video, 'youtu.be') !== false)) { if (strpos($url, '/embed/') === false) { $parts = parse_url($url); if ($parts['host'] === 'youtu.be') { $youtube_id = trim($parts['path'], '/'); } else { parse_str($parts['query'], $query); $youtube_id = $query['v']; } $url = 'http://www.youtube.com/watch?v=' . $youtube_id; } else { $url = $remote_video; } $type = 'iframe'; } if ((!empty($remote_video)) && (strpos($url, 'dailymotion') !== false)) { $type = 'iframe'; $id = strtok(basename($url), '_'); $url = 'https://dailymotion.com/embed/video/' . $id; } if (!empty($remote_video)) { if (strpos($url, '.mp4') !== false || strpos($url, '.mov') !== false || strpos($url, '.flv') !== false) { $type = 'iframe'; } } if ((!empty($remote_video)) && (strpos($url, 'wistia') !== false)) { $type = 'iframe'; } if ((!empty($remote_video)) && (strpos($url, 'facebook') !== false)) { $type = 'iframe'; $url = 'https://www.facebook.com/plugins/video.php?height=314&href='. urlencode($url) .'&show_text=false&width=560'; } if ((!empty($remote_video)) && (strpos($url, 'twitch') !== false)) { $type = 'iframe'; $parts = parse_url($url); if (strpos($parts['path'], '/video') !== false) { $twitch_id = str_replace('/videos/', '', $parts['path']); $url = 'https://player.twitch.tv/?video='. $twitch_id .'&parent=' . $_SERVER['SERVER_NAME']; } else { $twitch_id = trim($parts['path'], '/'); $url = 'https://player.twitch.tv/?channel='. $twitch_id .'&parent=' . $_SERVER['SERVER_NAME']; } } return array('type' => $type, 'url' => $url); } }