| | |
| | | namespace JVBase\blocks; |
| | | |
| | | use JVBase\managers\Cache; |
| | | use JVBase\registrar\Registrar; |
| | | use JVBase\utility\Features; |
| | | use JVBase\utility\Checker; |
| | | use JVBase\forms\TaxonomySelector; |
| | | use WP_Block; |
| | | |
| | |
| | | |
| | | if (is_post_type_archive() || is_singular()) { |
| | | $content = is_singular() ? jvbNoBase($type->post_type) : jvbNoBase($type->name); |
| | | $mainConfig = JVB_CONTENT[$content]??false; |
| | | if ($mainConfig && array_key_exists('feed', $mainConfig) && array_key_exists('config', $mainConfig['feed'])){ |
| | | $config = array_merge($config, $mainConfig['feed']['config']); |
| | | |
| | | $registrar = Registrar::getInstance($content)??false; |
| | | if ($registrar) { |
| | | $config = array_merge($config, $registrar->getConfig('feed')); |
| | | } else { |
| | | $config['content'] = $content; |
| | | $config['icon'] = JVB_CONTENT[$content]['icon']??[jvbLogoIcon()]; |
| | | $config['icon'] = jvbDefaultIcon(); |
| | | } |
| | | if (is_singular()) { |
| | | $config['source'] = $type->ID; |
| | |
| | | $config['taxonomies'] = $this->getTaxonomies([$content]); |
| | | } elseif (is_tax()) { |
| | | $content = jvbNoBase($type->taxonomy); |
| | | $mainConfig = JVB_TAXONOMY[$content]??false; |
| | | if ($mainConfig) { |
| | | $config['content'] = $mainConfig['for_content']; |
| | | $config['context'] = $content; // ← ADD THIS |
| | | $config['taxonomies'] = $this->getTaxonomies($mainConfig['for_content']); |
| | | if (array_key_exists('feed', $mainConfig) && array_key_exists('config', $mainConfig['feed'])){ |
| | | $config = array_merge($config, $mainConfig['feed']['config']); |
| | | $registrar = Registrar::getInstance($content)??false; |
| | | if ($registrar) { |
| | | $config['content'] = $registrar->registrar->for; |
| | | $config['context'] = $content; |
| | | $config['taxonomies'] = $this->getTaxonomies($registrar->registrar->for); |
| | | if (!empty($registrar->getConfig('feed'))){ |
| | | $config = array_merge($config, $registrar->getConfig('feed')); |
| | | } |
| | | } |
| | | $config['source'] = $type->term_id; |
| | |
| | | |
| | | /** |
| | | * Get taxonomies for given content types |
| | | * Uses Checker instead of globals |
| | | |
| | | */ |
| | | protected function getTaxonomies(array $content): array |
| | | { |
| | | $checker = Checker::getInstance(); |
| | | |
| | | $taxonomies = []; |
| | | |
| | | foreach ($content as $contentType) { |
| | | $contentTaxonomies = $checker->getTaxonomiesForContent($contentType); |
| | | $registrar = Registrar::getInstance($contentType); |
| | | |
| | | $contentTaxonomies = $registrar->registrar->taxonomies; |
| | | $contentTaxonomies = array_filter($contentTaxonomies, function($taxonomy) { |
| | | return array_key_exists('show_feed', JVB_TAXONOMY[$taxonomy]) && JVB_TAXONOMY[$taxonomy]['show_feed']; |
| | | return Registrar::getInstance($taxonomy)->hasFeature('show_feed'); |
| | | }); |
| | | $taxonomies = array_merge($taxonomies, $contentTaxonomies); |
| | | } |
| | |
| | | <span class="label">FILTER BY:</span> |
| | | |
| | | <?php |
| | | $checker = Checker::getInstance(); |
| | | foreach ($this->config['taxonomies'] as $tax) : |
| | | $taxConfig = JVB_TAXONOMY[$tax] ?? null; |
| | | if (!$taxConfig) continue; |
| | | $registrar = Registrar::getInstance($tax)??false; |
| | | if (!$registrar) continue; |
| | | |
| | | $contentForTax = $checker->getContentForTaxonomy($tax); |
| | | $contentForTax = $registrar->registrar->for; |
| | | $hidden = empty($contentForTax) ? ' hidden' : ''; |
| | | |
| | | $taxSelector = new TaxonomySelector( |
| | | 'feed-'.$tax, |
| | | $tax, |
| | | [ |
| | | 'icon' => $taxConfig['icon']??jvbLogoIcon(), |
| | | 'icon' => $registrar->getIcon()??jvbLogoIcon(), |
| | | 'update' => '.selected-items-section .selected-items', |
| | | 'types' => $contentForTax, |
| | | 'autocomplete' => false, |
| | |
| | | <?php |
| | | $custom = []; |
| | | foreach ($this->getContent() as $content) { |
| | | $config = JVB_CONTENT[$content]??JVB_TAXONOMY[$content]??JVB_USER[$content]??false; |
| | | if ($config && array_key_exists('custom_order', $config)) { |
| | | $custom = array_merge_recursive($custom, $config['custom_order']); |
| | | $registrar = Registrar::getInstance($content)??false; |
| | | |
| | | if ($registrar && !empty($registrar->config('feed')->getCustomOrder())) { |
| | | $custom = array_merge_recursive($custom, $registrar->config('feed')->getCustomOrder()); |
| | | } |
| | | } |
| | | foreach ($custom as $slug => $conf) { |
| | |
| | | $total = count($this->getContent()) - 1; |
| | | for ($i = 1; $i <= 36; $i++) { |
| | | $rand = rand(0, $total); |
| | | $config = Features::getConfig($this->getContent()[$rand]); |
| | | $icon = jvbIcon($config['icon']??jvbLogoIcon()); |
| | | $config = Registrar::getInstance($this->getContent()[$rand]); |
| | | $icon = jvbIcon($config->getIcon??jvbLogoIcon()); |
| | | ?> |
| | | <div class="placeholder"><?=apply_filters('jvbFeedPlaceholder', $icon) ?></div> |
| | | <?php |
| | |
| | | |
| | | protected function getFavouritesButton(string $content):string |
| | | { |
| | | if (!Features::forSite()->has('favourites') && !Features::forContent($content)->has('favouritable')) { |
| | | $registrar = Registrar::getInstance($content); |
| | | if (!$registrar || !Features::forSite()->has('favourites') || !$registrar->hasFeature('favouritable')) { |
| | | return ''; |
| | | } |
| | | return '<button class="favourite" type="button" title="Add to favourites" data-action="favourite"> |
| | |
| | | } |
| | | protected function getUpvotesButton(string $content):string |
| | | { |
| | | if (!Features::forSite()->has('karma') && !Features::forContent($content)->has('karma')){ |
| | | $registrar = Registrar::getInstance($content); |
| | | if (!Features::forSite()->has('karma') || !$registrar || !$registrar->hasFeature('karma')){ |
| | | return ''; |
| | | } |
| | | return '<div class="karma row"> |
| | |
| | | } |
| | | protected function getDefaultTemplate(string $content): string |
| | | { |
| | | $config = JVB_CONTENT[$content]??[]; |
| | | $hasConfig = array_key_exists('feed', $config); |
| | | $images = ($hasConfig && array_key_exists('images', $config['feed']) ? $config['feed']['images']:['post_thumbnail']); |
| | | $images = (is_array($images)) ? $images : [$images]; |
| | | $fields = ($hasConfig && array_key_exists('fields', $config['feed']) ? $config['feed']['fields']:['post_title', 'post_date']); |
| | | $config = Registrar::getInstance($content)->getConfig('feed'); |
| | | $allFields = Registrar::getFieldsFor($content); |
| | | $images = $config['images']??['post_thumbnail']; |
| | | $fields = $config['fields']??['post_title','post_date','post_excerpt']; |
| | | $fields = array_filter($fields, function($field) use($images) { |
| | | return !in_array($field, $images); |
| | | }); |
| | | $fields = array_filter($allFields, function($field) use($fields) { |
| | | return in_array($field, $fields); |
| | | }, ARRAY_FILTER_USE_KEY); |
| | | $template = '<div class="feed item col '.$content.'">'.$this->getFavouritesButton($content).$this->getUpvotesButton($content); |
| | | |
| | | //Add all defined images, but allow for filtering |
| | |
| | | |
| | | $fieldsTemplate = ''; |
| | | |
| | | foreach ($fields as $fieldName) { |
| | | $fieldType = JVB_CONTENT[$content][$fieldName]['type']??'text'; |
| | | $fieldsTemplate .= apply_filters('jvbFeedItemField', $this->defaultFieldTemplate($fieldType, $fieldName), $content, $fieldName, $fieldType); |
| | | foreach ($fields as $fieldName => $config) { |
| | | $fieldsTemplate .= apply_filters('jvbFeedItemField', $this->defaultFieldTemplate($config['type'], $fieldName), $content, $fieldName, $config['type']); |
| | | } |
| | | $template .= '<div class="item-info">'.apply_filters('jvbFeedItemFields', $fieldsTemplate, $content, $fields).'</div>'; |
| | | $template .= '</details></div>'; |