Files
Testing/tbb/wp-content/plugins/feedzy-rss-feeds/includes/gutenberg/feedzy-rss-feeds-gutenberg-block.php
sean 0e8a8b3028
Some checks failed
deploy-testing / deploy (push) Has been cancelled
Initial testing snapshot
2026-11-17 16:15:01 -05:00

292 lines
8.1 KiB
PHP

<?php
/**
* Class for functionalities related to Gutenberg block.
*
* Defines the functions that need to be used for Gutenberg block,
* and REST router.
*
* @package feedzy-rss-feeds
* @subpackage feedzy-rss-feeds/includes/guteneberg
* @author Themeisle <friends@themeisle.com>
*/
class Feedzy_Rss_Feeds_Gutenberg_Block {
/**
* A reference to an instance of this class.
*
* @var Feedzy_Rss_Feeds_Gutenberg_Block The one Feedzy_Rss_Feeds_Gutenberg_Block instance.
*/
private static $instance;
/**
* Feedzy RSS Feeds plugin version.
*
* @var string $version The current version of the plugin.
*/
protected $version;
/**
* Returns an instance of this class.
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new Feedzy_Rss_Feeds_Gutenberg_Block();
}
return self::$instance;
}
/**
* Initializes the plugin by setting filters and administration functions.
*/
private function __construct() {
$this->version = Feedzy_Rss_Feeds::get_version();
// Add a filter to load functions when all plugins have been loaded
add_action( 'enqueue_block_editor_assets', array( $this, 'feedzy_gutenberg_scripts' ) );
add_action( 'rest_api_init', array( $this, 'feedzy_register_rest_route' ) );
add_action( 'init', array( $this, 'feedzy_register_block_type' ) );
}
/**
* Enqueue front end and editor JavaScript and CSS
*/
public function feedzy_gutenberg_scripts() {
if ( FEEDZY_DISABLE_CACHE_FOR_TESTING ) {
$version = filemtime( FEEDZY_ABSPATH . '/includes/gutenberg/build/block.js' );
} else {
$version = $this->version;
}
// Enqueue the bundled block JS file
wp_enqueue_script( 'feedzy-gutenberg-block-js', FEEDZY_ABSURL . 'includes/gutenberg/build/block.js', array( 'wp-i18n', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-editor', 'wp-api', 'lodash' ), $version );
// Pass in REST URL
wp_localize_script(
'feedzy-gutenberg-block-js', 'feedzyjs',
array(
'imagepath' => esc_url( FEEDZY_ABSURL . 'img/' ),
'isPro' => feedzy_is_pro(),
)
);
// Enqueue editor block styles
wp_enqueue_style( 'feedzy-gutenberg-block-css', FEEDZY_ABSURL . 'includes/gutenberg/build/block.css', '', $version );
}
/**
* Hook server side rendering into render callback
*/
public function feedzy_register_block_type() {
register_block_type(
'feedzy-rss-feeds/feedzy-block', array(
'render_callback' => array( $this, 'feedzy_gutenberg_block_callback' ),
'attributes' => array(
'feeds' => array(
'type' => 'string',
),
'max' => array(
'type' => 'string',
'default' => '5',
),
'feed_title' => array(
'type' => 'boolean',
'default' => true,
),
'refresh' => array(
'type' => 'string',
'default' => '12_hours',
),
'sort' => array(
'type' => 'string',
'default' => 'date_desc',
),
'target' => array(
'type' => 'string',
'default' => '_blank',
),
'title' => array(
'type' => 'number',
),
'meta' => array(
'type' => 'boolean',
'default' => true,
),
'summary' => array(
'type' => 'boolean',
'default' => true,
),
'summarylength' => array(
'type' => 'number',
),
'keywords_title' => array(
'type' => 'string',
),
'keywords_ban' => array(
'type' => 'string',
),
'thumb' => array(
'type' => 'string',
'default' => 'auto',
),
'default' => array(
'type' => 'object',
),
'size' => array(
'type' => 'number',
'default' => 150,
),
'price' => array(
'type' => 'boolean',
'default' => true,
),
'referral_url' => array(
'type' => 'string',
),
'columns' => array(
'type' => 'number',
'default' => 1,
),
'template' => array(
'type' => 'string',
'default' => 'default',
),
),
)
);
}
/**
* Feedzy Gutenberg Block Callback Function
*/
public function feedzy_gutenberg_block_callback( $attr ) {
$attr['default'] = ( ! empty( $attr['default'] ) ? $attr['default']['url'] : '' );
$paramss = wp_parse_args( $attr );
return feedzy_rss( $paramss );
}
/**
* Register Rest Route for Feedzy
*/
public function feedzy_register_rest_route() {
register_rest_route(
'feedzy/v1', '/feed/', array(
'methods' => 'GET',
'callback' => array( $this, 'feedzy_rest_route' ),
'args' => array(
'url' => array(
'sanitize_callback' => array( $this, 'feedzy_sanitize_feeds' ),
),
'category' => array(
'sanitize_callback' => array( $this, 'feedzy_sanitize_categories' ),
),
),
)
);
}
/**
* Output Feed JSON
*/
public function feedzy_rest_route( $data ) {
$feed = $data;
if ( ! empty( $data['url'] ) ) {
$feed = $data['url'];
} elseif ( ! empty( $data['category'] ) ) {
$feed = $data['category'];
}
$meta_args = array(
'date_format' => get_option( 'date_format' ),
'time_format' => get_option( 'time_format' ),
);
$instance = Feedzy_Rss_Feeds::instance();
$admin = $instance->get_admin();
$feed = $admin->fetch_feed( $feed, '12_hours', array( '' ) );
$feedy = array();
if ( ! $feed->init() ) {
$feedy['error'] = __( 'Invalid Feed URL', 'feedzy-rss-feeds' );
header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
return $feedy;
}
if ( ( ! $feed->get_title() ) && ( ! $feed->get_title() ) && ( ! $feed->get_title() ) ) {
$feedy['channel'] = null;
} else {
$feedy['channel'] = array(
'title' => ( ( $feed->get_title() ) ? $feed->get_title() : null ),
'description' => ( ( $feed->get_description() ) ? $feed->get_description() : null ),
'permalink' => ( ( $feed->get_permalink() ) ? $feed->get_permalink() : null ),
);
}
if ( feedzy_is_pro() ) {
$pro = new Feedzy_Rss_Feeds_Pro_Admin( $instance->get_plugin_name(), $instance->get_version() );
}
$feedy['items'] = array();
$items = $feed->get_items();
foreach ( $items as $item ) {
if ( feedzy_is_pro() ) {
$item_attrs = $pro->feedzy_pro_add_data_to_item( array(), $item );
}
array_push(
$feedy['items'], array(
'title' => ( ( $item->get_title() ) ? $item->get_title() : null ),
'link' => ( ( $item->get_permalink() ) ? $item->get_permalink() : null ),
'creator' => ( ( $item->get_author() ) ? $item->get_author()->get_name() : null ),
'pubDate' => ( ( $item->get_date() ) ? $item->get_date( 'U' ) : null ),
'date' => ( ( $item->get_date() ) ? date_i18n( $meta_args['date_format'], $item->get_date( 'U' ) ) : null ),
'time' => ( ( $item->get_date() ) ? date_i18n( $meta_args['time_format'], $item->get_date( 'U' ) ) : null ),
'description' => ( ( $item->get_description() ) ? $item->get_description() : null ),
'thumbnail' => $admin->feedzy_retrieve_image( $item ),
'price' => ( ( feedzy_is_pro() && $item_attrs['item_price'] ) ? $item_attrs['item_price'] : null ),
'media' => ( ( feedzy_is_pro() && $item_attrs['item_media'] ) ? $item_attrs['item_media'] : null ),
)
);
}
header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
$output = $feedy;
return $output;
}
/**
* Sanitize Rest API Return
*/
public function feedzy_sanitize_feeds( $input ) {
if ( sizeof( $input ) === 1 ) {
$feed = esc_url( $input[0] );
return $feed;
} else {
$feeds = array();
foreach ( $input as $item ) {
$feeds[] = esc_url( $item );
}
return $feeds;
}
}
/**
* Sanitize Rest API Return
*/
public function feedzy_sanitize_categories( $input ) {
if ( $post = get_page_by_path( $input, OBJECT, 'feedzy_categories' ) ) {
$id = $post->ID;
$value = get_post_meta( $id, 'feedzy_category_feed', true );
$value = trim( $value );
$value = explode( ',', $value );
if ( sizeof( $value ) === 1 ) {
$value = esc_url( $value[0] );
return $value;
} else {
return $value;
}
}
}
}