|
| 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 | +class Feedzy_Rss_Feeds_Loop_Block { |
| 13 | + |
| 14 | + /** |
| 15 | + * A reference to an instance of this class. |
| 16 | + * |
| 17 | + * @var Feedzy_Rss_Feeds_Loop_Block The one Feedzy_Rss_Feeds_Loop_Block instance. |
| 18 | + */ |
| 19 | + private static $instance; |
| 20 | + |
| 21 | + /** |
| 22 | + * Instance of Feedzy_Rss_Feeds_Admin class. |
| 23 | + * |
| 24 | + * @var Feedzy_Rss_Feeds_Admin $admin The Feedzy_Rss_Feeds_Admin instance. |
| 25 | + */ |
| 26 | + private $admin; |
| 27 | + |
| 28 | + /** |
| 29 | + * Feedzy RSS Feeds plugin version. |
| 30 | + * |
| 31 | + * @var string $version The current version of the plugin. |
| 32 | + */ |
| 33 | + protected $version; |
| 34 | + |
| 35 | + /** |
| 36 | + * Returns an instance of this class. |
| 37 | + */ |
| 38 | + public static function get_instance() { |
| 39 | + if ( null === self::$instance ) { |
| 40 | + self::$instance = new Feedzy_Rss_Feeds_Loop_Block(); |
| 41 | + } |
| 42 | + return self::$instance; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Initializes the plugin by setting filters and administration functions. |
| 47 | + */ |
| 48 | + private function __construct() { |
| 49 | + $this->version = Feedzy_Rss_Feeds::get_version(); |
| 50 | + $this->admin = Feedzy_Rss_Feeds::instance()->get_admin(); |
| 51 | + add_action( 'init', array( $this, 'register_block' ) ); |
| 52 | + add_filter( 'feedzy_loop_item', array( $this, 'apply_magic_tags' ), 10, 2 ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Register Block |
| 57 | + */ |
| 58 | + public function register_block() { |
| 59 | + $metadata_file = trailingslashit( FEEDZY_ABSPATH ) . '/build/loop/block.json'; |
| 60 | + register_block_type_from_metadata( |
| 61 | + $metadata_file, |
| 62 | + array( |
| 63 | + 'render_callback' => array( $this, 'render_callback' ), |
| 64 | + ) |
| 65 | + ); |
| 66 | + |
| 67 | + wp_set_script_translations( 'feedzy-rss-feeds-loop-editor-script', 'feedzy-rss-feeds' ); |
| 68 | + |
| 69 | + // Pass in REST URL |
| 70 | + wp_localize_script( |
| 71 | + 'feedzy-rss-feeds-loop-editor-script', |
| 72 | + 'feedzyData', |
| 73 | + array( |
| 74 | + 'imagepath' => esc_url( FEEDZY_ABSURL . 'img/' ), |
| 75 | + 'defaultImage' => esc_url( FEEDZY_ABSURL . 'img/feedzy.svg' ), |
| 76 | + 'isPro' => feedzy_is_pro(), |
| 77 | + ) |
| 78 | + ); |
| 79 | + |
| 80 | + wp_localize_script( |
| 81 | + 'feedzy-rss-feeds-loop-editor-script', |
| 82 | + 'feedzyConditionsData', |
| 83 | + apply_filters( |
| 84 | + 'feedzy_conditions_data', |
| 85 | + array( |
| 86 | + 'isPro' => feedzy_is_pro(), |
| 87 | + 'operators' => Feedzy_Rss_Feeds_Conditions::get_operators(), |
| 88 | + ) |
| 89 | + ) |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Render Callback |
| 95 | + * |
| 96 | + * @param array $attributes The block attributes. |
| 97 | + * @param string $content The block content. |
| 98 | + * @return string The block content. |
| 99 | + */ |
| 100 | + public function render_callback( $attributes, $content ) { |
| 101 | + $content = empty( $content ) ? ( $attributes['innerBlocksContent'] ?? '' ) : $content; |
| 102 | + $is_preview = isset( $attributes['innerBlocksContent'] ) && ! empty( $attributes['innerBlocksContent'] ); |
| 103 | + $feed_urls = array(); |
| 104 | + |
| 105 | + if ( isset( $attributes['feed']['type'] ) && 'group' === $attributes['feed']['type'] && isset( $attributes['feed']['source'] ) && is_numeric( $attributes['feed']['source'] ) ) { |
| 106 | + $group = $attributes['feed']['source']; |
| 107 | + $value = get_post_meta( $group, 'feedzy_category_feed', true ); |
| 108 | + $value = trim( $value ); |
| 109 | + $feed_urls = !empty( $value ) ? explode( ',', $value ) : array(); |
| 110 | + } |
| 111 | + |
| 112 | + if ( isset( $attributes['feed']['type'] ) && 'url' === $attributes['feed']['type'] && isset( $attributes['feed']['source'] ) && is_array( $attributes['feed']['source'] ) ) { |
| 113 | + $feed_urls = $attributes['feed']['source']; |
| 114 | + } |
| 115 | + |
| 116 | + if ( empty( $feed_urls ) ) { |
| 117 | + return '<div>' . esc_html__( 'No feeds to display', 'feedzy-rss-feeds' ) . '</div>'; |
| 118 | + } |
| 119 | + |
| 120 | + $column_count = isset($attributes['layout']) && isset($attributes['layout']['columnCount']) && !empty($attributes['layout']['columnCount']) ? $attributes['layout']['columnCount'] : 1; |
| 121 | + |
| 122 | + $default_query = array( |
| 123 | + 'max' => 5, |
| 124 | + 'sort' => 'default', |
| 125 | + 'refresh' => '12_hours', |
| 126 | + ); |
| 127 | + |
| 128 | + $query = isset( $attributes['query'] ) ? wp_parse_args( $attributes['query'], $default_query ) : $default_query; |
| 129 | + $filters = isset( $attributes['conditions'] ) ? $attributes['conditions'] : array(); |
| 130 | + |
| 131 | + $options = array( |
| 132 | + 'feeds' => implode( ',', $feed_urls ), |
| 133 | + 'max' => $query['max'], |
| 134 | + 'sort' => $query['sort'], |
| 135 | + 'offset' => 0, |
| 136 | + 'target' => '_blank', |
| 137 | + 'keywords_ban' => '', |
| 138 | + 'columns' => '1', |
| 139 | + 'thumb' => 'auto', |
| 140 | + 'default' => '', |
| 141 | + 'title' => '', |
| 142 | + 'meta' => 'yes', |
| 143 | + 'multiple_meta' => 'no', |
| 144 | + 'summary' => 'yes', |
| 145 | + 'summarylength' => '', |
| 146 | + 'filters' => wp_json_encode( $filters ), |
| 147 | + ); |
| 148 | + |
| 149 | + $sizes = array( |
| 150 | + 'width' => 300, |
| 151 | + 'height' => 300, |
| 152 | + ); |
| 153 | + |
| 154 | + $feed = $this->admin->fetch_feed( $feed_urls, $query['refresh'], $options ); |
| 155 | + |
| 156 | + if ( isset( $feed->error ) && ! empty( $feed->error ) ) { |
| 157 | + return '<div>' . esc_html__( 'An error occurred while fetching the feed.', 'feedzy-rss-feeds' ) . '</div>'; |
| 158 | + } |
| 159 | + |
| 160 | + $feed_items = apply_filters( 'feedzy_get_feed_array', array(), $options, $feed, implode( ',', $feed_urls ), $sizes ); |
| 161 | + |
| 162 | + if ( empty( $feed_items ) ) { |
| 163 | + return '<div>' . esc_html__( 'No items to display.', 'feedzy-rss-feeds' ) . '</div>'; |
| 164 | + } |
| 165 | + |
| 166 | + $loop = ''; |
| 167 | + |
| 168 | + foreach ($feed_items as $key => $item) { |
| 169 | + $loop .= apply_filters( 'feedzy_loop_item', $content, $item ); |
| 170 | + } |
| 171 | + |
| 172 | + return sprintf( |
| 173 | + '<div %1$s>%2$s</div>', |
| 174 | + $wrapper_attributes = get_block_wrapper_attributes( array( |
| 175 | + 'class' => 'feedzy-loop-columns-' . $column_count, |
| 176 | + ) ), |
| 177 | + $loop |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Magic Tags Replacement. |
| 183 | + * |
| 184 | + * @param string $content The content. |
| 185 | + * |
| 186 | + * @return string The content. |
| 187 | + */ |
| 188 | + public function apply_magic_tags( $content, $item ) { |
| 189 | + $pattern = '/\{\{feedzy_([^}]+)\}\}/'; |
| 190 | + $content = str_replace( |
| 191 | + array( |
| 192 | + FEEDZY_ABSURL . 'img/feedzy.svg', |
| 193 | + 'http://{{feedzy_url}}' |
| 194 | + ), |
| 195 | + array( |
| 196 | + '{{feedzy_image}}', |
| 197 | + '{{feedzy_url}}' |
| 198 | + ), |
| 199 | + $content |
| 200 | + ); |
| 201 | + |
| 202 | + return preg_replace_callback( $pattern, function( $matches ) use ( $item ) { |
| 203 | + return isset( $matches[1] ) ? $this->get_value( $matches[1], $item ) : ''; |
| 204 | + }, $content ); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Get Dynamic Value. |
| 209 | + * |
| 210 | + * @param string $key The key. |
| 211 | + * @param array $item Feed item. |
| 212 | + * |
| 213 | + * @return string The value. |
| 214 | + */ |
| 215 | + public function get_value( $key, $item ) { |
| 216 | + switch ( $key ) { |
| 217 | + case 'title': |
| 218 | + return isset( $item['item_title'] ) ? $item['item_title'] : ''; |
| 219 | + case 'url': |
| 220 | + return isset( $item['item_url'] ) ? $item['item_url'] : ''; |
| 221 | + case 'date': |
| 222 | + $item_date = isset( $item['item_date'] ) ? wp_date( get_option( 'date_format' ), $item['item_date'] ) : ''; |
| 223 | + return $item_date; |
| 224 | + case 'time': |
| 225 | + $item_date = isset( $item['item_date'] ) ? wp_date( get_option( 'time_format' ), $item['item_date'] ) : ''; |
| 226 | + return $item_date; |
| 227 | + case 'datetime': |
| 228 | + $item_date = isset( $item['item_date'] ) ? wp_date( get_option( 'date_format' ), $item['item_date'] ) : ''; |
| 229 | + $item_time = isset( $item['item_date'] ) ? wp_date( get_option( 'time_format' ), $item['item_date'] ) : ''; |
| 230 | + /* translators: 1: date, 2: time */ |
| 231 | + $datetime = sprintf( __( '%1$s at %2$s', 'feedzy-rss-feeds' ), $item_date, $item_time ); |
| 232 | + return $datetime; |
| 233 | + case 'author': |
| 234 | + if ( isset( $item['item_author'] ) && is_string( $item['item_author'] ) ) { |
| 235 | + return $item['item_author']; |
| 236 | + } elseif ( isset( $item['item_author'] ) && is_object( $item['item_author'] ) ) { |
| 237 | + return $item['item_author']->get_name(); |
| 238 | + } |
| 239 | + return ''; |
| 240 | + case 'description': |
| 241 | + return isset( $item['item_description'] ) ? $item['item_description'] : ''; |
| 242 | + case 'content': |
| 243 | + return isset( $item['item_content'] ) ? $item['item_content'] : ''; |
| 244 | + case 'meta': |
| 245 | + return isset( $item['item_meta'] ) ? $item['item_meta'] : ''; |
| 246 | + case 'categories': |
| 247 | + return isset( $item['item_categories'] ) ? $item['item_categories'] : ''; |
| 248 | + case 'image': |
| 249 | + $settings = apply_filters( 'feedzy_get_settings', array() ); |
| 250 | + if ( $settings && ! empty( $settings['general']['default-thumbnail-id'] ) ) { |
| 251 | + $default_img = wp_get_attachment_image_src( $settings['general']['default-thumbnail-id'], 'full' ); |
| 252 | + $default_img = ! empty( $default_img ) ? reset( $default_img ) : ''; |
| 253 | + } else { |
| 254 | + $default_img = FEEDZY_ABSURL . 'img/feedzy.svg'; |
| 255 | + } |
| 256 | + |
| 257 | + return isset( $item['item_img_path'] ) ? $item['item_img_path'] : $default_img; |
| 258 | + case 'media': |
| 259 | + return isset( $item['item_media']['src'] ) ? $item['item_media']['src'] : ''; |
| 260 | + case 'price': |
| 261 | + return isset( $item['item_price'] ) ? $item['item_price'] : ''; |
| 262 | + case 'source': |
| 263 | + return isset( $item['item_source'] ) ? $item['item_source'] : ''; |
| 264 | + default: |
| 265 | + return ''; |
| 266 | + } |
| 267 | + } |
| 268 | +} |
0 commit comments