| | |
| | | class CustomBlocks |
| | | { |
| | | protected CacheManager $cache; |
| | | protected CacheManager $imgCache; |
| | | public function __construct() |
| | | { |
| | | $this->cache = CacheManager::for('blocks', WEEK_IN_SECONDS); |
| | | $this->imgCache = CacheManager::for('images', WEEK_IN_SECONDS); |
| | | |
| | | |
| | | add_filter('render_block', [$this, 'render'], 999, 3); |
| | | |
| | | |
| | |
| | | if (array_key_exists('useFeaturedImage', $attrs)) { |
| | | global $post; |
| | | $ID = get_post_thumbnail_id($post->ID); |
| | | } else { |
| | | } else if (array_key_exists('id', $attrs)) { |
| | | $ID = (int)$attrs['id']; |
| | | } |
| | | |
| | |
| | | protected function render_core_template_part(array $block, string $content):string |
| | | { |
| | | |
| | | $check = ['header', 'footer']; |
| | | $isHeaderTemplate = ( |
| | | (array_key_exists('slug', $block['attrs']) && str_contains(strtolower($block['attrs']['slug']), 'header')) || |
| | | (array_key_exists('tagName', $block['attrs']) && str_contains(strtolower($block['attrs']['tagName']), 'header')) |
| | |
| | | ) ? 'footer' : false; |
| | | |
| | | |
| | | if ($isHeaderTemplate || $isFooterTemplate) { |
| | | if (($isHeaderTemplate || $isFooterTemplate)) { |
| | | |
| | | $tag = $isHeaderTemplate ?: $isFooterTemplate ?: 'div'; |
| | | |
| | | $breadcrumbs = $themeSwitch = $afterHeader = $beforeHeader = $footerText= ''; |
| | |
| | | } |
| | | $footerText = jvbRandomFooterText(); |
| | | } |
| | | // jvbDump($beforeHeader,'beforeHeader'); |
| | | // jvbDump('<'.$tag.$this->getClassesAndStyles($block['attrs']).'>','tag'); |
| | | // jvbDump($themeSwitch,'themeSwitch'); |
| | | // jvbDump($this->inside($block, $tag, $content),'inside'); |
| | | // jvbDump($footerText,'footerText'); |
| | | // jvbDump($afterHeader, 'afterheader'); |
| | | // jvbDump($breadcrumbs, 'breadcrumbs'); |
| | | |
| | | return $beforeHeader.'<'.$tag.$this->getClassesAndStyles($block['attrs']).'>'. |
| | | $themeSwitch . |
| | | $this->inside($block, $tag, $content). |
| | | $footerText.'</'.$tag.'>'.$afterHeader.$breadcrumbs; |
| | | } |
| | | |
| | | return $content; |
| | | } |
| | | //core_term_description |
| | |
| | | protected function localize_feedblock():void |
| | | { |
| | | wp_localize_script('jvb-feed-view-script', 'feedSettings', [ |
| | | 'apiUrl' => rest_url('jvb/v1/'), |
| | | 'nonce' => wp_create_nonce('wp_rest'), |
| | | 'currentUser' => is_user_logged_in() ? [ |
| | | 'id' => get_current_user_id() |
| | | ] : null, |
| | |
| | | $tag = (str_contains($tag, ' class')) ? strtok($tag, ' class') : $tag; |
| | | $tag = trim($tag); |
| | | } |
| | | |
| | | if (!str_starts_with($o, '<'.$tag)) { |
| | | return $o; |
| | | } |
| | | |
| | | $len = strlen('</'.$tag.'>'); |
| | | |
| | |
| | | int $ID = 0, |
| | | string $start = 'tiny', |
| | | string $replace = 'large', |
| | | string $postSlug = '' |
| | | ?string $postSlug = null |
| | | ):string { |
| | | $image = $this->image($ID, $start, $replace); |
| | | if ($ID === 0) { |
| | | $ID = $this->imageID($ID); |
| | | } |
| | | if ($postSlug==='') { |
| | | global $post; |
| | | $postSlug = $post->post_name; |
| | | } |
| | | if (!$ID) { |
| | | return ''; |
| | | } |
| | | $img = get_post($ID); |
| | | if (!$img) { |
| | | return ''; |
| | | } |
| | | $imgSlug = $img->post_name; |
| | | |
| | | $img = '<a class="open-gallery" target="_blank" rel="nofollow" data-opens="gallery-'.$postSlug.'" data-focus="'.$postSlug.'-'.$imgSlug.'">'.$image; |
| | | $image = jvbFormatImage($ID, $start, $replace, true, $postSlug); |
| | | |
| | | if ($close) { |
| | | $img .= '</a>'; |
| | | return $image; |
| | | } |
| | | return $img; |
| | | $len = strlen('</a>'); |
| | | return substr_replace($image, '', -$len, $len); |
| | | } |
| | | public function image(string $ID = '', string $start = 'tiny', string $replace = 'large'):string |
| | | { |
| | |
| | | if ($ID === 0 || $ID === false) { |
| | | return ''; |
| | | } |
| | | |
| | | $img = wp_get_attachment_image_src($ID, $start); |
| | | if (!$img) return ''; |
| | | $img = $img[0]; |
| | | |
| | | $data = $this->gallerySizes($ID, $replace); |
| | | |
| | | $alt = get_post_meta($ID, '_wp_attachment_image_alt', true); |
| | | $alt = ($alt=='')? '' : ' alt="'.$alt.'" '; |
| | | |
| | | return '<img width="100%" height="auto" src="' . |
| | | $img . '"' .$alt . $data . ' loading="lazy" decoding="async">'; |
| | | } |
| | | public function gallerySizes(int $ID, string $replace = 'medium'):string |
| | | { |
| | | if (!wp_get_attachment_image_src($ID)) { |
| | | return ''; |
| | | } |
| | | if ($replace == 'large') { |
| | | return 'data-small="'. |
| | | wp_get_attachment_image_src($ID, 'large')[0].'" data-medium="'. |
| | | wp_get_attachment_image_src($ID, 'full')[0].'" data-full="'. |
| | | wp_get_attachment_image_src($ID, 'full')[0].'"'; |
| | | } elseif ($replace == 'medium') { |
| | | return 'data-small="'. |
| | | wp_get_attachment_image_src($ID, 'large')[0].'" data-medium="'. |
| | | wp_get_attachment_image_src($ID, 'large')[0].'" data-full="'. |
| | | wp_get_attachment_image_src($ID, 'large')[0].'"'; |
| | | } elseif ($replace == 'thumbnail') { |
| | | return 'data-small="'. |
| | | wp_get_attachment_image_src($ID, 'medium')[0].'" data-medium="'. |
| | | wp_get_attachment_image_src($ID, 'medium')[0].'" data-full="'. |
| | | wp_get_attachment_image_src($ID, 'medium')[0].'"'; |
| | | } |
| | | return ''; |
| | | return jvbFormatImage($ID, $start, $replace, false); |
| | | } |
| | | public function sanitizeBlockName(array $block):string |
| | | { |
| | |
| | | return $styles; |
| | | } |
| | | |
| | | public function getGallerySizes(int $ID, string $replace):string |
| | | { |
| | | if (!wp_get_attachment_image_src($ID)) { |
| | | return ''; |
| | | } |
| | | if (!has_image_size($replace)) { |
| | | $replace = 'large'; |
| | | } |
| | | |
| | | if ($replace == 'large') { |
| | | return 'data-small="' . |
| | | wp_get_attachment_image_src($ID)[0] . '" data-medium="' . |
| | | wp_get_attachment_image_src($ID, 'large')[0] . '" data-full="' . |
| | | wp_get_attachment_image_src($ID, 'full')[0] . '"'; |
| | | } elseif ($replace == 'medium') { |
| | | return 'data-small="'. |
| | | wp_get_attachment_image_src($ID, 'large')[0].'" data-medium="'. |
| | | wp_get_attachment_image_src($ID, 'large')[0].'" data-full="'. |
| | | wp_get_attachment_image_src($ID, 'large')[0].'"'; |
| | | } elseif ($replace == 'thumbnail') { |
| | | return 'data-small="'. |
| | | wp_get_attachment_image_src($ID, 'medium')[0].'" data-medium="'. |
| | | wp_get_attachment_image_src($ID, 'medium')[0].'" data-full="'. |
| | | wp_get_attachment_image_src($ID, 'medium')[0].'"'; |
| | | } |
| | | return ''; |
| | | } |
| | | |
| | | public function formatImage(int $ID = 0, string $start = 'tiny', string $replace = 'large'):string |
| | | { |
| | | if ($ID === 0) { |
| | |
| | | if ($ID === 0) { |
| | | return ''; |
| | | } |
| | | |
| | | return $this->imgCache->remember( |
| | | ['ID' => $ID, 'start' => $start, 'replace' => $replace], |
| | | function() use ($ID, $start, $replace) { |
| | | $img = wp_get_attachment_image_src($ID, $start); |
| | | if (!$img) { |
| | | return''; |
| | | } |
| | | $img = $img[0]; |
| | | |
| | | $data = $this->getGallerySizes($ID, $replace); |
| | | |
| | | |
| | | |
| | | $alt = get_post_meta($ID, '_wp_attachment_image_alt', true); |
| | | $alt = ($alt=='')? '' : ' alt="'.$alt.'" '; |
| | | return '<img width="300px" height="300px" src="'.$img.'"'.$alt.$data.' loading="lazy" decoding="async">'; |
| | | } |
| | | ); |
| | | return jvbFormatImage($ID, $start, $replace); |
| | | } |
| | | |
| | | } |