Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

jsarnowski / jsarnowski/generatepress-premium   php

Repository URL to install this package:

Version: 2.2.2 

/ class-block-elements.php

<?php
/**
 * This file handles the dynamic aspects of Block Elements.
 *
 * @package GP Premium
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // No direct access, please.
}

/**
 * Dynamic blocks.
 */
class GeneratePress_Block_Elements {
	/**
	 * Instance.
	 *
	 * @access private
	 * @var object Instance
	 * @since 2.0.0
	 */
	private static $instance;

	/**
	 * Initiator.
	 *
	 * @return object initialized object of class.
	 */
	public static function get_instance() {
		if ( ! isset( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Build it.
	 */
	public function __construct() {
		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_assets' ), 8 );

		if ( version_compare( $GLOBALS['wp_version'], '5.8', '<' ) ) {
			add_filter( 'block_categories', array( $this, 'add_block_category' ) );
		} else {
			add_filter( 'block_categories_all', array( $this, 'add_block_category' ) );
		}

		add_action( 'init', array( $this, 'register_dynamic_blocks' ) );
		add_action( 'init', array( $this, 'register_meta' ) );
		add_filter( 'render_block', array( $this, 'render_blocks' ), 10, 2 );
		add_filter( 'generateblocks_background_image_url', array( $this, 'set_background_image_url' ), 10, 2 );
		add_filter( 'generateblocks_attr_container', array( $this, 'set_container_attributes' ), 10, 2 );
		add_filter( 'generateblocks_defaults', array( $this, 'set_defaults' ) );
		add_action( 'generateblocks_block_css_data', array( $this, 'generate_css' ), 10, 7 );
		add_filter( 'generateblocks_attr_container', array( $this, 'set_dynamic_container_url' ), 15, 2 );
		add_filter( 'generateblocks_attr_container-link', array( $this, 'set_dynamic_container_url' ), 15, 2 );
		add_action( 'wp_enqueue_scripts', array( $this, 'frontend_css' ), 100 );
	}

	/**
	 * Enqueue assets.
	 */
	public function enqueue_assets() {
		if ( 'gp_elements' !== get_post_type() ) {
			return;
		}

		wp_enqueue_script(
			'gp-premium-block-elements',
			GP_PREMIUM_DIR_URL . 'dist/block-elements.js',
			array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ),
			filemtime( GP_PREMIUM_DIR_PATH . 'dist/block-elements.js' ),
			true
		);

		wp_set_script_translations( 'gp-premium-block-elements', 'gp-premium', GP_PREMIUM_DIR_PATH . 'langs' );

		$taxonomies = get_taxonomies(
			array(
				'public'   => true,
			)
		);

		$parent_elements = get_posts(
			array(
				'post_type'     => 'gp_elements',
				'post_parent'   => 0,
				'no_found_rows' => true,
				'post_status'   => 'publish',
				'numberposts'   => 100,
				'fields'        => 'ids',
				'exclude'       => array( get_the_ID() ),
				'meta_query' => array(
					array(
						'key' => '_generate_block_type',
						'value' => 'content-template',
						'compare' => '=',
					),
				),
			)
		);

		$parent_elements_data = array();

		foreach ( (array) $parent_elements as $element ) {
			$parent_elements_data[] = array(
				'label' => get_the_title( $element ),
				'id' => $element,
			);
		}

		$image_sizes = get_intermediate_image_sizes();
		$image_sizes = array_diff( $image_sizes, array( '1536x1536', '2048x2048' ) );
		$image_sizes[] = 'full';

		$containerWidth = function_exists( 'generate_get_option' ) ? generate_get_option( 'container_width' ) : 1100;
		$rightSidebarWidth = apply_filters( 'generate_right_sidebar_width', '25' );
		$leftSidebarWidth = apply_filters( 'generate_left_sidebar_width', '25' );

		$containerWidth = floatval( $containerWidth );
		$leftSidebarWidth = '0.' . $leftSidebarWidth;
		$rightSidebarWidth = '0.' . $rightSidebarWidth;

		$leftSidebarWidth = $containerWidth - ( $containerWidth * $leftSidebarWidth );
		$rightSidebarWidth = $containerWidth - ( $containerWidth * $rightSidebarWidth );

		$leftSidebarWidth = $containerWidth - $leftSidebarWidth;
		$rightSidebarWidth = $containerWidth - $rightSidebarWidth;

		$contentWidth = $containerWidth - $rightSidebarWidth;

		wp_localize_script(
			'gp-premium-block-elements',
			'gpPremiumBlockElements',
			array(
				'isBlockElement' => 'gp_elements' === get_post_type(),
				'taxonomies' => $taxonomies,
				'rightSidebarWidth' => $rightSidebarWidth,
				'leftSidebarWidth' => $leftSidebarWidth,
				'contentWidth' => $contentWidth,
				'hooks' => GeneratePress_Elements_Helper::get_available_hooks(),
				'excerptLength' => apply_filters( 'excerpt_length', 55 ), // phpcs:ignore -- Core filter.
				'isGenerateBlocksActive' => function_exists( 'generateblocks_load_plugin_textdomain' ),
				'isGenerateBlocksInstalled' => file_exists( WP_PLUGIN_DIR . '/generateblocks/plugin.php' ) ? true : false,
				'isGenerateBlocksProActive' => function_exists( 'generateblocks_pro_init' ),
				'installLink' => wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=generateblocks' ), 'install-plugin_generateblocks' ),
				'activateLink' => wp_nonce_url( 'plugins.php?action=activate&amp;plugin=generateblocks/plugin.php&amp;plugin_status=all&amp;paged=1&amp;s', 'activate-plugin_generateblocks/plugin.php' ),
				'imageSizes' => $image_sizes,
				'imageSizeDimensions' => $this->get_image_Sizes(),
				'featuredImagePlaceholder' => GP_PREMIUM_DIR_URL . 'elements/assets/admin/featured-image-placeholder.png',
				'authorImagePlaceholder' => GP_PREMIUM_DIR_URL . 'elements/assets/admin/author-image-placeholder.png',
				'bgImageFallback' => GP_PREMIUM_DIR_URL . 'elements/assets/admin/background-image-fallback.jpg',
				'templateImageUrl' => 'https://gpsites.co/files/element-library',
				'parentElements' => $parent_elements_data,
			)
		);

		wp_enqueue_style(
			'gp-premium-block-elements',
			GP_PREMIUM_DIR_URL . 'dist/block-elements.css',
			array( 'wp-edit-blocks' ),
			filemtime( GP_PREMIUM_DIR_PATH . 'dist/block-elements.css' )
		);
	}

	/**
	 * Add our block category.
	 *
	 * @param array $categories The existing categories.
	 */
	public function add_block_category( $categories ) {
		return array_merge(
			array(
				array(
					'slug'  => 'generatepress',
					'title' => __( 'GeneratePress', 'gp-premium' ),
				),
			),
			$categories
		);
	}

	/**
	 * Register our dynamic blocks.
	 */
	public function register_dynamic_blocks() {
		if ( ! function_exists( 'register_block_type' ) ) {
			return;
		}

		register_block_type(
			'generatepress/dynamic-content',
			array(
				'render_callback' => array( $this, 'do_dynamic_content_block' ),
				'attributes' => array(
					'contentType' => array(
						'type' => 'string',
						'default' => '',
					),
					'excerptLength' => array(
						'type' => 'number',
						'default' => apply_filters( 'excerpt_length', 55 ), // phpcs:ignore -- Core filter.
					),
					'useThemeMoreLink' => array(
						'type' => 'boolean',
						'defaut' => true,
					),
					'customMoreLink' => array(
						'type' => 'string',
						'default' => '',
					),
				),
			)
		);

		register_block_type(
			'generatepress/dynamic-image',
			array(
				'render_callback' => array( $this, 'do_dynamic_image_block' ),
				'attributes' => array(
					'imageType' => array(
						'type' => 'string',
						'default' => '',
					),
					'imageSource' => array(
						'type' => 'string',
						'default' => 'current-post',
					),
					'customField' => array(
						'type' => 'string',
						'default' => '',
					),
					'gpDynamicSourceInSameTerm' => array(
						'type' => 'boolean',
						'default' => false,
					),
					'gpDynamicSourceInSameTermTaxonomy' => array(
						'tyoe' => 'string',
						'default' => 'category',
					),
					'imageSize' => array(
						'type' => 'string',
						'default' => 'full',
					),
					'linkTo' => array(
						'type' => 'string',
						'default' => '',
					),
					'linkToCustomField' => array(
						'type' => 'string',
						'default' => '',
					),
					'imageWidth' => array(
						'type' => 'number',
						'default' => null,
					),
					'imageHeight' => array(
						'type' => 'number',
						'default' => null,
					),
					'avatarSize' => array(
						'type' => 'number',
						'default' => 30,
					),
					'avatarRounded' => array(
						'type' => 'boolean',
						'default' => false,
					),
				),
			)
		);
	}

	/**
	 * Do our dynamic content block.
	 *
	 * @param array $attributes The attributes from this block.
	 */
	public function do_dynamic_content_block( $attributes ) {
		if ( empty( $attributes['contentType'] ) ) {
			return;
		}

		if ( 'post-content' === $attributes['contentType'] ) {
			return $this->do_content_block();
		}

		if ( 'post-excerpt' === $attributes['contentType'] ) {
			return $this->do_excerpt_block( $attributes );
		}

		if ( 'term-description' === $attributes['contentType'] ) {
			return sprintf(
				'<div class="dynamic-term-description">%s</div>',
				term_description()
			);
		}

		if ( 'author-description' === $attributes['contentType'] ) {
			return sprintf(
				'<div class="dynamic-author-description">%s</div>',
				get_the_author_meta( 'description' )
			);
		}
	}

	/**
	 * Build our content block.
	 */
	public function do_content_block() {
		if ( 'gp_elements' !== get_post_type() && ! is_admin() ) {
			return sprintf(
				'<div class="dynamic-entry-content">%s</div>',
				apply_filters( 'the_content', str_replace( ']]>', ']]&gt;', get_the_content() ) ) // phpcs:ignore -- Core filter.
			);
		}
	}

	/**
	 * Build our excerpt block.
	 *
	 * @param array $attributes The block attributes.
	 */
	public function do_excerpt_block( $attributes ) {
		if ( version_compare( PHP_VERSION, '5.6', '>=' ) ) {
			$filter_excerpt_length = function( $length ) use ( $attributes ) {
				return isset( $attributes['excerptLength'] ) ? $attributes['excerptLength'] : $length;
			};

			add_filter(
				'excerpt_length',
				$filter_excerpt_length,
				100
			);

			if ( isset( $attributes['useThemeMoreLink'] ) && ! $attributes['useThemeMoreLink'] ) {
				$filter_more_text = function() use ( $attributes ) {
					if ( empty( $attributes['customMoreLink'] ) ) {
						return ' ...';
					}

					return apply_filters(
						'generate_excerpt_block_more_output',
Loading ...