|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class for functionalities related to Loop block. |
| 4 | + * |
| 5 | + * Defines the functions that need to be used for Loop block, |
| 6 | + * and REST router. |
| 7 | + * |
| 8 | + * @package feedzy-rss-feeds |
| 9 | + * @subpackage feedzy-rss-feeds/includes/guteneberg |
| 10 | + * @author Themeisle <[email protected]> |
| 11 | + * |
| 12 | + * @since 5.1.0 |
| 13 | + */ |
| 14 | +class Feedzy_Rss_Feeds_Tag_Block { |
| 15 | + |
| 16 | + /** |
| 17 | + * A reference to an instance of this class. |
| 18 | + * |
| 19 | + * @var Feedzy_Rss_Feeds_Tag_Block The one Feedzy_Rss_Feeds_Loop_Block instance. |
| 20 | + */ |
| 21 | + private static $instance; |
| 22 | + |
| 23 | + /** |
| 24 | + * Instance of Feedzy_Rss_Feeds_Admin class. |
| 25 | + * |
| 26 | + * @var Feedzy_Rss_Feeds_Admin $admin The Feedzy_Rss_Feeds_Admin instance. |
| 27 | + */ |
| 28 | + private $admin; |
| 29 | + |
| 30 | + /** |
| 31 | + * Feedzy RSS Feeds plugin version. |
| 32 | + * |
| 33 | + * @var string $version The current version of the plugin. |
| 34 | + */ |
| 35 | + protected $version; |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns an instance of this class. |
| 39 | + */ |
| 40 | + public static function get_instance() { |
| 41 | + if ( null === self::$instance ) { |
| 42 | + self::$instance = new Feedzy_Rss_Feeds_Tag_Block(); |
| 43 | + } |
| 44 | + return self::$instance; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Initializes the plugin by setting filters and administration functions. |
| 49 | + */ |
| 50 | + private function __construct() { |
| 51 | + $this->version = Feedzy_Rss_Feeds::get_version(); |
| 52 | + $this->admin = Feedzy_Rss_Feeds::instance()->get_admin(); |
| 53 | + add_action( 'init', array( $this, 'register_block' ) ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Register Block |
| 58 | + */ |
| 59 | + public function register_block() { |
| 60 | + $metadata_file = trailingslashit( FEEDZY_ABSPATH ) . '/build/tag/block.json'; |
| 61 | + register_block_type_from_metadata( |
| 62 | + $metadata_file, |
| 63 | + array( |
| 64 | + 'render_callback' => array( $this, 'render_callback' ), |
| 65 | + ) |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Render Callback |
| 71 | + * |
| 72 | + * @param array $attributes The block attributes. |
| 73 | + * @param string $content The block content. |
| 74 | + * @param WP_Block $block The block instance. |
| 75 | + * @return string The block content. |
| 76 | + */ |
| 77 | + public function render_callback( $attributes, $content, $block ) { |
| 78 | + if ( |
| 79 | + ! isset( $attributes['tag'] ) || empty( $attributes['tag'] ) || |
| 80 | + ! isset( $block->context ) || empty( $block->context ) || |
| 81 | + ! isset( $block->context['feedzy-rss-feeds/feedItem'] ) || empty( $block->context['feedzy-rss-feeds/feedItem'] ) |
| 82 | + ) { |
| 83 | + return ''; |
| 84 | + } |
| 85 | + |
| 86 | + $feed_item = $block->context['feedzy-rss-feeds/feedItem']; |
| 87 | + |
| 88 | + if ( ! isset( $feed_item[ $attributes['tag'] ] ) ) { |
| 89 | + return ''; |
| 90 | + } |
| 91 | + |
| 92 | + return wp_kses_post( $feed_item[ $attributes['tag'] ] ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Magic Tags Replacement. |
| 97 | + * |
| 98 | + * @param string $content The content. |
| 99 | + * |
| 100 | + * @return string The content. |
| 101 | + */ |
| 102 | + public function apply_magic_tags( $content, $item ) { |
| 103 | + $pattern = '/\{\{feedzy_([^}]+)\}\}/'; |
| 104 | + $content = str_replace( |
| 105 | + array( |
| 106 | + FEEDZY_ABSURL . 'img/feedzy.svg', |
| 107 | + 'http://{{feedzy_url}}' |
| 108 | + ), |
| 109 | + array( |
| 110 | + '{{feedzy_image}}', |
| 111 | + '{{feedzy_url}}' |
| 112 | + ), |
| 113 | + $content |
| 114 | + ); |
| 115 | + |
| 116 | + return preg_replace_callback( $pattern, function( $matches ) use ( $item ) { |
| 117 | + return isset( $matches[1] ) ? $this->get_value( $matches[1], $item ) : ''; |
| 118 | + }, $content ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Get Dynamic Value. |
| 123 | + * |
| 124 | + * @param string $key The key. |
| 125 | + * @param array $item Feed item. |
| 126 | + * |
| 127 | + * @return string The value. |
| 128 | + */ |
| 129 | + public function get_value( $key, $item ) { |
| 130 | + switch ( $key ) { |
| 131 | + case 'title': |
| 132 | + return isset( $item['item_title'] ) ? $item['item_title'] : ''; |
| 133 | + case 'url': |
| 134 | + return isset( $item['item_url'] ) ? $item['item_url'] : ''; |
| 135 | + case 'date': |
| 136 | + $item_date = isset( $item['item_date'] ) ? wp_date( get_option( 'date_format' ), $item['item_date'] ) : ''; |
| 137 | + return $item_date; |
| 138 | + case 'time': |
| 139 | + $item_date = isset( $item['item_date'] ) ? wp_date( get_option( 'time_format' ), $item['item_date'] ) : ''; |
| 140 | + return $item_date; |
| 141 | + case 'datetime': |
| 142 | + $item_date = isset( $item['item_date'] ) ? wp_date( get_option( 'date_format' ), $item['item_date'] ) : ''; |
| 143 | + $item_time = isset( $item['item_date'] ) ? wp_date( get_option( 'time_format' ), $item['item_date'] ) : ''; |
| 144 | + /* translators: 1: date, 2: time */ |
| 145 | + $datetime = sprintf( __( '%1$s at %2$s', 'feedzy-rss-feeds' ), $item_date, $item_time ); |
| 146 | + return $datetime; |
| 147 | + case 'author': |
| 148 | + if ( isset( $item['item_author'] ) && is_string( $item['item_author'] ) ) { |
| 149 | + return $item['item_author']; |
| 150 | + } elseif ( isset( $item['item_author'] ) && is_object( $item['item_author'] ) ) { |
| 151 | + return $item['item_author']->get_name(); |
| 152 | + } |
| 153 | + return ''; |
| 154 | + case 'description': |
| 155 | + return isset( $item['item_description'] ) ? $item['item_description'] : ''; |
| 156 | + case 'content': |
| 157 | + return isset( $item['item_content'] ) ? $item['item_content'] : ''; |
| 158 | + case 'meta': |
| 159 | + return isset( $item['item_meta'] ) ? $item['item_meta'] : ''; |
| 160 | + case 'categories': |
| 161 | + return isset( $item['item_categories'] ) ? $item['item_categories'] : ''; |
| 162 | + case 'image': |
| 163 | + $settings = apply_filters( 'feedzy_get_settings', array() ); |
| 164 | + if ( $settings && ! empty( $settings['general']['default-thumbnail-id'] ) ) { |
| 165 | + $default_img = wp_get_attachment_image_src( $settings['general']['default-thumbnail-id'], 'full' ); |
| 166 | + $default_img = ! empty( $default_img ) ? reset( $default_img ) : ''; |
| 167 | + } else { |
| 168 | + $default_img = FEEDZY_ABSURL . 'img/feedzy.svg'; |
| 169 | + } |
| 170 | + |
| 171 | + return isset( $item['item_img_path'] ) ? $item['item_img_path'] : $default_img; |
| 172 | + case 'media': |
| 173 | + return isset( $item['item_media']['src'] ) ? $item['item_media']['src'] : ''; |
| 174 | + case 'price': |
| 175 | + return isset( $item['item_price'] ) ? $item['item_price'] : ''; |
| 176 | + case 'source': |
| 177 | + return isset( $item['item_source'] ) ? $item['item_source'] : ''; |
| 178 | + default: |
| 179 | + return ''; |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments