first commit

This commit is contained in:
Roman Pyrih
2023-07-24 08:30:51 +02:00
commit c2e100a763
7128 changed files with 1622619 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
<?php
class Brizy_Content_ContextFactory {
/**
* @var Brizy_Content_Context
*/
static $globalContext = null;
/**
* @param $project
* @param $brizy_post
* @param $wp_post
* @param $contentHtml
*
* @return Brizy_Content_Context
*/
static public function createContext( $project, $brizy_post, $wp_post, $contentHtml, $isLoop = false ) {
$context = self::getContext( $project, $wp_post );
if ( $isLoop ) {
return apply_filters( 'brizy_loop_context_create', $context, $wp_post );
}
return apply_filters( 'brizy_context_create', $context, $wp_post );
}
static public function createEmptyContext() {
return new Brizy_Content_Context( null, null, null, null );
}
static public function getGlobalContext() {
return self::$globalContext;
}
static public function makeContextGlobal( Brizy_Content_Context $context ) {
return self::$globalContext = $context;
}
static public function clearGlobalContext() {
self::$globalContext = null;
}
/**
* @param $project
* @param $wp_post
*
* @return Brizy_Content_Context
*/
private static function getContext( $project, $wp_post ) {
$context = new Brizy_Content_Context( $project, null, $wp_post, null );
if ( $wp_post ) {
$context->setAuthor( $wp_post->post_author );
}
return $context;
}
}

View File

@@ -0,0 +1,77 @@
<?php
use BrizyPlaceholders\ContextInterface;
class Brizy_Content_Context implements ContextInterface {
private $data = array();
/**
* @param $name
* @param $arguments
*
* @return mixed|null
*/
public function __call( $name, $arguments ) {
$method = substr( $name, 0, 3 );
$key = substr( $name, 3 );
switch ( $method ) {
case 'set':
return $this->set( $key, $arguments[0] );
break;
case 'get':
return $this->get( $key );
break;
}
}
/**
* @param $name
*
* @return null|mixed
*/
protected function get( $name ) {
if ( is_null( $name ) ) {
return;
}
if ( isset( $this->data[ $name ] ) ) {
return $this->data[ $name ];
}
return null;
}
public function afterExtract($contentPlaceholders, $instancePlaceholders, $contentAfterExtractor) {
$this->setPlaceholders($contentPlaceholders);
}
/**
* @param $key
* @param $value
*
* @return null|mixed
*/
protected function set( $key, $value ) {
if ( is_null( $value ) ) {
return null;
}
return $this->data[ $key ] = $value;
}
/**
* BrizyPro_Content_Context constructor.
*
* @param $project
* @param $wp_post
*/
public function __construct( $project, $brizy_post, $wp_post, $contentHtml ) {
$this->setProject( $project );
$this->setWpPost( $wp_post );
}
}

View File

@@ -0,0 +1,29 @@
<?php
use BrizyPlaceholders\Extractor;
use BrizyPlaceholders\Replacer;
class Brizy_Content_DynamicContentProcessor implements Brizy_Editor_Content_ProcessorInterface {
/**
* @param string $content
* @param Brizy_Content_Context $context
*
* @return mixed
*/
public function process( $content, Brizy_Content_Context $context ) {
$placeholderProvider = new Brizy_Content_PlaceholderProvider( $context );
$extractor = new Extractor( $placeholderProvider );
$context->setProvider( $placeholderProvider );
list( $contentPlaceholders, $placeholderInstances, $content ) = $extractor->extract( $content );
$replacer = new Replacer( $placeholderProvider );
$content = $replacer->replaceWithExtractedData( $contentPlaceholders, $placeholderInstances, $content ,$context);
return $content;
}
}

View File

@@ -0,0 +1,62 @@
<?php
class Brizy_Content_MainProcessor {
/**
* @var Brizy_Editor_Content_ProcessorInterface[]
*/
private $processors = array();
/**
* @var Brizy_Content_Context
*/
private $context;
/**
* Brizy_Content_MainProcessor constructor.
*
* @param $context
*/
public function __construct( Brizy_Content_Context $context ) {
$this->context = $context;
$this->processors[] = new Brizy_Content_ShortcodeToPlaceholderProcessor();
$this->processors[] = new Brizy_Editor_Asset_DomainProcessor();
$this->processors[] = new Brizy_Content_DynamicContentProcessor();
$post_id = $context->getWpPost() ? $context->getWpPost()->ID : null;
$urlBuilder = new Brizy_Editor_UrlBuilder( $context->getProject(), $post_id );
$asset_storage = new Brizy_Editor_Asset_AssetProxyStorage( $urlBuilder );
$media_storage = new Brizy_Editor_Asset_MediaProxyStorage( $urlBuilder );
$this->processors[] = new Brizy_Editor_Asset_AssetProxyProcessor( $asset_storage );
$this->processors[] = new Brizy_Editor_Asset_MediaAssetProcessor( $media_storage );
$this->processors[] = new Brizy_Editor_Asset_SvgAssetProcessor( );
$this->processors = apply_filters( 'brizy_content_processors', $this->processors, $context );
}
/**
* @param $content
*
* @return string
*/
public function process( $content ) {
$processors = apply_filters( 'brizy_apply_content_processors', $this->processors );
if ( apply_filters( 'brizy_html_entity_decode', true ) ) {
$content = html_entity_decode( $content, ENT_QUOTES | ENT_HTML5, get_bloginfo( 'charset' ) );
}
foreach ( $processors as $processor ) {
$content = $processor->process( $content, $this->context );
}
return $content;
}
}

View File

@@ -0,0 +1,110 @@
<?php
/**
* @deprecated
*
* Class Brizy_Content_PlaceholderExtractor
*/
class Brizy_Content_PlaceholderExtractor
{
/**
* @var Brizy_Content_Providers_AbstractProvider
*/
private $provider;
/**
* BrizyPro_Content_PlaceholderExtractor constructor.
*
* @param Brizy_Content_Providers_AbstractProvider $provider
*/
public function __construct($provider)
{
$this->provider = $provider;
}
private static function getPlaceholderRegexExpression()
{
return "/(?<placeholder>{{\s*(?<placeholderName>.+?)(?<attributes>(?:\s+)((?:\w+\s*=\s*(?:'|\"|\&quot;|\&apos;)(?:.[^\"']*|)(?:'|\"|\&quot;|\&apos;)\s*)*))?}}(?:(?<content>.*?){{\s*end_(\g{placeholderName})\s*}})?)/ims";;
}
public static function stripPlaceholders($content)
{
$expression = self::getPlaceholderRegexExpression();
return preg_replace($expression, '', $content);
}
/**
* @param $content
*
* @return array
*/
public function extract($content)
{
$placeholders = array();
$expression = self::getPlaceholderRegexExpression();
$matches = array();
preg_match_all($expression, $content, $matches);
if (count($matches['placeholder']) == 0) {
return array($placeholders, $content);
}
foreach ($matches['placeholder'] as $i => $name) {
$hasPlaceholder = $this->provider->getPlaceholder($matches['placeholderName'][$i]);
// ignore unknown placeholders
if (!$hasPlaceholder) {
continue;
}
$placeholder = new \BrizyPlaceholders\ContentPlaceholder(
$matches['placeholderName'][$i],
$matches['placeholder'][$i],
$this->getPlaceholderAttributes($matches['attributes'][$i]),
$matches['content'][$i]
);
$placeholders[] = $placeholder;
$pos = strpos($content, $placeholder->getPlaceholder());
$length = strlen($placeholder->getPlaceholder());
if ($pos !== false) {
$content = substr_replace($content, $placeholder->getUid(), $pos, $length);
}
}
return array($placeholders, $content);
}
/**
* Split the attributs from attribute string
*
* @param $attributeString
*
* @return array
*/
private function getPlaceholderAttributes($attributeString)
{
$attrString = trim($attributeString);
$attrMatches = array();
$attributes = array();
preg_match_all("/(\w+)\s*=\s*(?<quote>'|\"|\&quot;|\&apos;)(.*?)(\g{quote})/mi", $attrString, $attrMatches);
if (isset($attrMatches[0]) && is_array($attrMatches[0])) {
foreach ($attrMatches[1] as $i => $name) {
$attributes[$name] = $attrMatches[3][$i];
}
}
return $attributes;
}
}

View File

@@ -0,0 +1,136 @@
<?php
use BrizyPlaceholders\PlaceholderInterface;
use BrizyPlaceholders\RegistryInterface;
class Brizy_Content_PlaceholderProvider implements RegistryInterface
{
/**
* @var array of implements Brizy_Editor_Content_PlaceholdersProviderInterface
*/
protected $providers = array();
/**
* @var array
*/
static private $cache_grouped_placeholders = null;
static private $cache_all_placeholders = null;
/**
* BrizyPro_Content_ProviderPlaceholders constructor.
*
* $context: for back compatibility
*
* @param Brizy_Content_Context $context
*/
public function __construct($context = null)
{
$this->providers[] = new Brizy_Content_Providers_FreeProvider();
$this->providers = apply_filters('brizy_providers', $this->providers, null);
}
/**
* @return array
*/
public function getPlaceholders()
{
$out = array();
if (self::$cache_all_placeholders) {
return self::$cache_all_placeholders;
}
foreach ($this->providers as $provider) {
$out = array_merge($out, $provider->getPlaceholders());
}
self::$cache_all_placeholders = $out;
return $out;
}
/**
* @return array
*/
public function getGroupedPlaceholders()
{
if (self::$cache_grouped_placeholders) {
return self::$cache_grouped_placeholders;
}
$result = array();
foreach ($this->providers as $provider) {
foreach ($provider->getPlaceholders() as $placeholder) {
if($placeholder->getGroup())
{
$result[ $placeholder->getGroup() ][] = $placeholder;
}
}
}
return apply_filters('brizy_placeholders', self::$cache_grouped_placeholders = $result);
}
public function getGroupedPlaceholdersForApiResponse() {
$groups = $this->getGroupedPlaceholders();
$result = [];
foreach ( $groups as $group => $entries ) {
$result[ $group ] = array_map( function ( $entry ) {
$placeholder = [
'placeholder' => '{{' . $entry->getPlaceholder() . '}}',
'label' => $entry->getLabel(),
'display' => $entry->getDisplay()
];
return apply_filters( 'editor_placeholder_data', $placeholder, $entry );
}, $entries );
}
return $result;
}
/**
* @param $name
* @return \BrizyPlaceholders\PlaceholderInterface
*/
// public function getPlaceholder($name)
// {
// return $this->getPlaceholderSupportingName($name);
// }
/**
* @inheritDoc
*/
// public function getPlaceholdersByGroup($groupName)
// {
// $getGroupedPlaceholders = $this->getGroupedPlaceholders();
//
// if (isset($getGroupedPlaceholders[$groupName])) {
// return $getGroupedPlaceholders[$groupName];
// }
// }
/**
* @inheritDoc
*/
public function getPlaceholderSupportingName($name)
{
foreach ($this->providers as $provider) {
if($instance = $provider->getPlaceholderSupportingName($name)) {
return $instance;
}
}
}
public function registerPlaceholder(PlaceholderInterface $instance)
{
throw new Exception('Try to use a specific registry to register the placeholder');
}
}

View File

@@ -0,0 +1,67 @@
<?php
/**
* @deprecated
*
* Class Brizy_Content_PlaceholderReplacer
*/
class Brizy_Content_PlaceholderReplacer {
/**
* @var Brizy_Content_Providers_AbstractProvider
*/
private $placeholderProvider;
/**
* Brizy_Content_PlaceholderReplacer constructor.
*
* @param $context
* @param $placeholderProvider
*/
public function __construct( $context, $placeholderProvider ) {
$this->placeholderProvider = $placeholderProvider;
}
/**
* @param $placeholders
* @param $content
* @param $context
*
* @return string|string[]
*/
public function getContent( $placeholders, $content, $context= null) {
$toReplace = array();
$toReplaceWithValues = array();
if(!$context)
{
$context = Brizy_Content_ContextFactory::createEmptyContext();
}
if ( $placeholders ) {
foreach ( $placeholders as $contentPlaceholder ) {
try {
$placeholder = $this->placeholderProvider->getPlaceholder( $contentPlaceholder->getName() );
if ( $placeholder ) {
$toReplace[] = $contentPlaceholder->getUid();
$toReplaceWithValues[] = $placeholder->getValue( $context , $contentPlaceholder );
} else {
$toReplace[] = $contentPlaceholder->getPlaceholder();
$toReplaceWithValues[] = '';
}
} catch ( Exception $e ) {
continue;
}
}
}
$content = str_replace( $toReplace, $toReplaceWithValues, $content );
return $content;
}
}

View File

@@ -0,0 +1,28 @@
<?php
class Brizy_Content_PlaceholderWpProvider extends Brizy_Content_PlaceholderProvider {
const PLACEHOLDERS = [
'brizy_dc_image_alt'
];
/**
* @return array
*/
public function getAllPlaceholders() {
$out = [];
foreach ( $this->providers as $provider ) {
$placeholders = $provider->getAllPlaceholders();
foreach ( $placeholders as $placeholder ) {
if ( in_array( $placeholder->getPlaceholder(), self::PLACEHOLDERS ) ) {
$out[] = $placeholder;
}
}
}
return $out;
}
}

View File

@@ -0,0 +1,159 @@
<?php
use \BrizyPlaceholders\ContentPlaceholder;
use BrizyPlaceholders\ContextInterface;
use \BrizyPlaceholders\PlaceholderInterface;
abstract class Brizy_Content_Placeholders_Abstract extends Brizy_Admin_Serializable implements PlaceholderInterface
{
const DISPLAY_INLINE = 'inline';
const DISPLAY_BLOCK = 'block';
/**
* @return string
*/
protected $label;
/**
* @return string
*/
protected $placeholder;
/**
* @var string
*/
protected $display = self::DISPLAY_INLINE;
protected $group = '';
/**
* It should return an unique identifier of the placeholder
*
* @return mixed
*/
public function getUid()
{
return md5(microtime());
}
public function support($placeholderName) {
return $this->getPlaceholder()==$placeholderName;
}
public function shouldFallbackValue($value, ContextInterface $context, ContentPlaceholder $placeholder)
{
return empty($value);
}
public function getFallbackValue(ContextInterface $context, ContentPlaceholder $placeholder)
{
$attributes = $placeholder->getAttributes();
return isset($attributes[PlaceholderInterface::FALLBACK_KEY]) ? $attributes[PlaceholderInterface::FALLBACK_KEY] : '';
}
/**
* @return mixed
*/
public function getLabel() {
return $this->label;
}
/**
* @param mixed $label
*
* @return Brizy_Content_Placeholders_Abstract
*/
public function setLabel( $label ) {
$this->label = $label;
return $this;
}
/**
* @return string
*/
public function getDisplay() {
return $this->display;
}
/**
* @param string $display
*
* @return Brizy_Content_Placeholders_Abstract
*/
public function setDisplay( $display ) {
$this->display = $display;
return $this;
}
/**
* @return string
*/
public function getGroup()
{
return $this->group;
}
/**
* @param string $group
* @return Brizy_Content_Placeholders_Abstract
*/
public function setGroup($group)
{
$this->group = $group;
return $this;
}
/**
* @return mixed
*/
public function getPlaceholder() {
return $this->placeholder;
}
/**
* @param mixed $placeholder
*
* @return Brizy_Content_Placeholders_Abstract
*/
public function setPlaceholder( $placeholder ) {
$this->placeholder = $placeholder;
return $this;
}
/**
* @return array
*/
public function convertToOptionValue() {
return array(
'label' => $this->getLabel(),
'placeholder' => $this->getPlaceholder(),
'display' => $this->getDisplay(),
);
}
public function jsonSerialize() {
return array(
'label' => $this->getLabel(),
'placeholder' => $this->getReplacePlaceholder(),
'display' => $this->getDisplay(),
);
}
/**
* @return string
*/
public function getReplacePlaceholder() {
$placeholder = $this->getPlaceholder();
if ( ! empty( $placeholder ) ) {
return "{{" . $placeholder . "}}";
}
return "";
}
}

View File

@@ -0,0 +1,13 @@
<?php
class Brizy_Content_Placeholders_ImageAltAttribute extends Brizy_Content_Placeholders_ImageAttribute {
/**
* @param $attachmentId
*
* @return string
*/
protected function getAttributeValue( $attachmentId ) {
return get_post_meta( $attachmentId, '_wp_attachment_image_alt', true );
}
}

View File

@@ -0,0 +1,101 @@
<?php
abstract class Brizy_Content_Placeholders_ImageAttribute extends Brizy_Content_Placeholders_Simple {
/**
* @return string|callable
*/
protected $value;
/**
* @param $attachmentId
*
* @return mixed
*/
abstract protected function getAttributeValue( $attachmentId );
/**
* Brizy_Editor_Content_GenericPlaceHolder constructor.
*
* @param string $label
* @param string $placeholder
* @param string|array $value
*/
public function __construct( $label, $placeholder ) {
parent::__construct( $label, $placeholder, function ( Brizy_Content_Context $context, \BrizyPlaceholders\ContentPlaceholder $contentPlaceholder ) {
$attributes = $contentPlaceholder->getAttributes();
$attachmentId = null;
if ( isset( $attributes['uid'] ) ) {
$attachmentId = $this->getAttachmentIdByByUid( $attributes['uid'], $context );
} elseif ( isset( $attributes['placeholder'] ) ) {
$attachmentId = $this->getAttachmentIdByPlaceholderName( $attributes['placeholder'], $context, $contentPlaceholder );
}
if ( $attachmentId ) {
return $this->getAttributeValue( $attachmentId );
}
return '';
} );
}
/**
* @param $placeholderName
* @param Brizy_Content_Context $context
* @param \BrizyPlaceholders\ContentPlaceholder $contentPlaceholder
*
* @return int|mixed|null|string
*/
protected function getAttachmentIdByPlaceholderName( $placeholderName, Brizy_Content_Context $context, \BrizyPlaceholders\ContentPlaceholder $contentPlaceholder ) {
$provider = $context->getProvider();
if ( ! $provider ) {
return 0;
}
$placeholder = $provider->getPlaceholderSupportingName( $placeholderName );
if ( ! $placeholder ) {
return 0;
}
if ( $placeholder instanceof BrizyPro_Content_Placeholders_Image ) {
$attachmentId = $placeholder->getAttachmentId( $context, $contentPlaceholder );
} else {
$attachmentId = $placeholder->getValue( $context, $contentPlaceholder );
}
return $attachmentId;
}
/**
* @param $uid
* @param Brizy_Content_Context $context
*
* @return null|string
*/
protected function getAttachmentIdByByUid( $uid, Brizy_Content_Context $context ) {
global $wpdb;
$pt = $wpdb->posts;
$mt = $wpdb->postmeta;
$attachmentId = $wpdb->get_var( $wpdb->prepare(
"SELECT
{$pt}.ID
FROM {$pt}
INNER JOIN {$mt} ON ( {$pt}.ID = {$mt}.post_id )
WHERE
( {$mt}.meta_key = 'brizy_attachment_uid'
AND {$mt}.meta_value = %s )
AND {$pt}.post_type = 'attachment'
ORDER BY {$pt}.post_date DESC",
$uid
) );
return $attachmentId;
}
}

View File

@@ -0,0 +1,17 @@
<?php
use \BrizyPlaceholders\ContentPlaceholder;
trait Brizy_Content_Placeholders_ImageAttributesAware {
/**
* It should return a string containigng image attributes
* Ex: alt="image alt attribute" title="Image title"
*
* @param Brizy_Content_Context $context
* @param ContentPlaceholder $contentPlaceholder
*
* @return
*/
abstract public function getAttachmentId( Brizy_Content_Context $context, ContentPlaceholder $contentPlaceholder );
}

View File

@@ -0,0 +1,15 @@
<?php
class Brizy_Content_Placeholders_ImageTitleAttribute extends Brizy_Content_Placeholders_ImageAttribute {
/**
* @param $attachmentId
*
* @return mixed|string
*/
protected function getAttributeValue( $attachmentId ) {
$post = get_post( $attachmentId );
return $post->post_title;
}
}

View File

@@ -0,0 +1,34 @@
<?php
use BrizyPlaceholders\ContentPlaceholder;
class Brizy_Content_Placeholders_Permalink extends Brizy_Content_Placeholders_Simple {
/**
* Brizy_Content_Placeholders_Simple constructor.
*
* @param $label
* @param $placeholder
* @param $value
* @param string $display
*/
public function __construct() {
parent::__construct( 'Permalink', 'brizy_dc_permalink', null );
}
/**
* @param ContentPlaceholder $contentPlaceholder
* @param Brizy_Content_Context $context
*
* @return mixed|string
*/
public function getValue(\BrizyPlaceholders\ContextInterface $context, ContentPlaceholder $contentPlaceholder) {
$attributes = $contentPlaceholder->getAttributes();
if ( isset( $attributes['post_id'] ) && (int) $attributes['post_id'] > 0 ) {
return get_permalink( (int) $attributes['post_id'] );
}
return '';
}
}

View File

@@ -0,0 +1,48 @@
<?php
use BrizyPlaceholders\ContentPlaceholder;
use BrizyPlaceholders\ContextInterface;
class Brizy_Content_Placeholders_Simple extends Brizy_Content_Placeholders_Abstract
{
/**
* @return string|callable
*/
protected $value;
/**
* Brizy_Content_Placeholders_Simple constructor.
*
* @param $label
* @param $placeholder
* @param $value
* @param string $display
*/
public function __construct($label, $placeholder, $value, $group = null, $display = Brizy_Content_Placeholders_Abstract::DISPLAY_INLINE)
{
$this->setLabel($label);
$this->setPlaceholder($placeholder);
$this->setDisplay($display);
$this->setGroup($group);
$this->value = $value;
}
/**
* @param ContextInterface $context
* @param ContentPlaceholder $placeholder
*
* @return mixed
*/
public function getValue(ContextInterface $context, ContentPlaceholder $placeholder)
{
$method = $this->value;
if (is_object($method) && ($method instanceof Closure)) {
return call_user_func($method, $context, $placeholder);
}
return $this->value;
}
}

View File

@@ -0,0 +1,67 @@
<?php
use BrizyPlaceholders\ContentPlaceholder;
class Brizy_Content_Placeholders_UniquePageUrl extends Brizy_Content_Placeholders_Simple {
/**
* Brizy_Content_Placeholders_Simple constructor.
*
* @param $label
* @param $placeholder
* @param $value
* @param string $display
*/
public function __construct( $label, $placeholder = 'brizy_dc_current_page_unique_url', $value = null, $display = Brizy_Content_Placeholders_Abstract::DISPLAY_INLINE ) {
$this->setLabel( $label );
$this->setPlaceholder( $placeholder );
$this->setDisplay( $display );
$this->value = null;
}
/**
* @param ContentPlaceholder $contentPlaceholder
* @param Brizy_Content_Context $context
*
* @return mixed|string
*/
public function getValue(\BrizyPlaceholders\ContextInterface $context, ContentPlaceholder $contentPlaceholder) {
global $wp;
$url = home_url( add_query_arg(array(), $wp->request) );
$closure = function () {
return false;
};
add_filter( 'pre_term_link', $closure );
$object = get_queried_object();
if(is_archive()) {
$url = add_query_arg($wp->query_vars, home_url() );
}
if ( $object instanceof WP_User ) {
$file = home_url( '/' );
$url = $file . '?author=' . $object->ID;
}
if ( $object instanceof WP_Post ) {
$url = $object->guid;
}
if ( $object instanceof WP_Term ) {
$url = get_term_link( $object );
}
remove_filter( 'pre_term_link', $closure );
return $url;
}
}

View File

@@ -0,0 +1,45 @@
<?php
use \BrizyPlaceholders\Registry;
abstract class Brizy_Content_Providers_AbstractProvider extends Registry implements Brizy_Content_Providers_Interface {
/**
* @return array
*/
protected function getDefaultGroupPlaceholders() {
return array(
self::CONFIG_KEY_TEXT =>array(),
self::CONFIG_KEY_IMAGE =>array(),
self::CONFIG_KEY_LINK =>array(),
self::CONFIG_KEY_OEMBED =>array(),
self::CONFIG_KEY_VIDEO =>array(),
self::CONFIG_KEY_SNDCLOUD =>array(),
);
}
/**
* @param $handle string
* @param $deps array
*
* @return bool
*/
protected function setScriptDependency( $handle, $deps ) {
global $wp_scripts;
$script = $wp_scripts->query( $handle );
if ( ! $script ) {
return false;
}
foreach ( $deps as $dep ) {
if ( ! in_array( $dep, $script->deps ) ) {
$script->deps[] = $dep;
}
}
return true;
}
}

View File

@@ -0,0 +1,358 @@
<?php
use BrizyPlaceholders\ContentPlaceholder;
use \BrizyPlaceholders\Registry;
class Brizy_Content_Providers_FreeProvider extends Brizy_Content_Providers_AbstractProvider
{
public function __construct() {
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple( 'Internal Display Block By User Role', 'display_by_roles', function ( Brizy_Content_Context $context, ContentPlaceholder $contentPlaceholder ) {
$attrs = $contentPlaceholder->getAttributes();
if ( ! empty( $attrs['roles'] ) ) {
$roles = explode( ',', $attrs['roles'] );
$userRoles = (array) wp_get_current_user()->roles;
if ( in_array( 'logged', $roles ) && is_user_logged_in() ) {
$userRoles[] = 'logged';
}
if ( Brizy_Editor_User::is_user_allowed() ) {
if ( ! empty( $_GET['role'] ) ) {
if ( $_GET['role'] === 'default' ) {
$roles[] = 'default';
$userRoles[] = 'default';
} else {
$userRoles = [];
if ( $_GET['role'] == 'not_logged' ) {
if ( in_array( 'not_logged', $roles ) ) {
$roles[] = 'default';
$userRoles[] = 'default';
}
} else {
$userRoles[] = $_GET['role'];
}
}
}
}
if ( in_array( 'not_logged', $roles ) ) {
$roles = array_diff( $roles, [ 'not_logged' ] );
if ( is_user_logged_in() ) {
if ( ! array_intersect( $roles, $userRoles ) ) {
return '';
}
}
} else {
if ( ! array_intersect( $roles, $userRoles ) ) {
return '';
}
}
}
$replacer = new \BrizyPlaceholders\Replacer( $context->getProvider() );
return $replacer->replacePlaceholders( $contentPlaceholder->getContent(), $context );
} ) );
$this->registerPlaceholder( new Brizy_Content_Placeholders_ImageTitleAttribute('Internal Title Attributes', 'brizy_dc_image_title') );
$this->registerPlaceholder( new Brizy_Content_Placeholders_ImageAltAttribute('Internal Alt Attributes', 'brizy_dc_image_alt') );
$this->registerPlaceholder( new Brizy_Content_Placeholders_UniquePageUrl('Uniquer page url', 'brizy_dc_current_page_unique_url') );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple('WP Language', 'brizy_dc_page_language', get_locale()) );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple('Ajax Url', 'brizy_dc_ajax_url', admin_url('admin-ajax.php')) );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Permalink() );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple('', 'editor_sidebar', function ($context, $contentPlaceholder) {
$attrs = $contentPlaceholder->getAttributes();
$id = isset($attrs['id']) ? $attrs['id'] : null;
if ($id) {
ob_start();
dynamic_sidebar($id);
return ob_get_clean();
}
return '';
}) );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple('', 'editor_navigation', function ($context, $contentPlaceholder) {
$attrs = $contentPlaceholder->getAttributes();
return $attrs['name'] ? wp_nav_menu(array('menu' => $attrs['name'], 'echo' => false)) : '';
}) );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple('', 'editor_post_field', function ($context, $contentPlaceholder) {
$attrs = $contentPlaceholder->getAttributes();
$post = ($context = Brizy_Content_ContextFactory::getGlobalContext()) ? $context->getWpPost() : get_post();
if (!$post || !isset($attrs['property'])) {
return '';
}
return $this->filterData($attrs['property'], $post);
}) );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple('', 'editor_post_info', function () {
$post = ($context = Brizy_Content_ContextFactory::getGlobalContext()) ? $context->getWpPost() : get_post();
if ( $post ) {
$twig = Brizy_TwigEngine::instance( BRIZY_PLUGIN_PATH . '/public/views' );
$commentsCount = get_approved_comments( $post->ID, [ 'count' => true ] );
$params = [];
$params['author'] = get_the_author_meta( 'display_name', $post->post_author );
$params['date'] = get_the_date( '', $post );
$params['time'] = get_the_time( '', $post );
$params['comments'] = sprintf( _n( '%s comment', '%s comments', $commentsCount, 'brizy' ), number_format_i18n( $commentsCount ) );
return $twig->render('post-info.html.twig', $params);
}
return '';
}) );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple('', 'editor_posts', function ($context, $contentPlaceholder) {
$atts = $contentPlaceholder->getAttributes();
// shortcode to use in page: {{editor_posts posts_per_page="5" category="1,2" orderby="date" order="DESC" columns="1" display_date="1" display_author="1"}}
// this array is used as default values for displayPosts
$extra_atts = array(
"columns" => 1,
"display_date" => 1,
"display_author" => 1,
);
$extra_atts = array_merge($extra_atts, $atts);
$posts = $this->getPosts($atts);
return $this->displayPosts($posts, $extra_atts);
}) );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple('Product Page', 'editor_product_page', function ($context, $contentPlaceholder) {
$atts = $contentPlaceholder->getAttributes();
// if ( ! empty( $atts['id'] ) ) {
// $product_data = get_post( $atts['id'] );
// $product = ! empty( $product_data ) && in_array( $product_data->post_type, [ 'product', 'product_variation' ] ) ? wc_setup_product_data( $product_data ) : false;
// }
if (empty($atts['id']) && current_user_can('manage_options')) {
return __('Please set a valid product', 'brizy');
}
$this->setScriptDependency('brizy-preview', ['zoom', 'photoswipe', 'flexslider', 'wc-single-product']);
// Avoid infinite loop. There's a call of the function the_content() in the woocommerce/single-product/tabs/description.php
remove_filter('the_content', [Brizy_Admin_Templates::instance(), 'filterPageContent'], -12000);
$html = do_shortcode('[product_page id="' . $atts['id'] . '"]');
add_filter('the_content', [Brizy_Admin_Templates::instance(), 'filterPageContent'], -12000);
return $html;
}) );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple('Products Page', 'editor_product_products', function ($context, $contentPlaceholder) {
$atts = $contentPlaceholder->getAttributes();
$shortcodeAttributes = [];
if (isset($atts['limit'])) {
$shortcodeAttributes[] = sprintf("limit=\"%d\"", (int)$atts['limit']);
}
if (isset($atts['columns'])) {
$shortcodeAttributes[] = sprintf("columns=\"%d\"", (int)$atts['columns']);
}
if (isset($atts['category'])) {
$shortcodeAttributes[] = sprintf("category=\"%s\"", $atts['category']);
}
if (isset($atts['orderby'])) {
$shortcodeAttributes[] = sprintf("orderby=\"%s\"", $atts['orderby']);
}
if (isset($atts['order'])) {
$shortcodeAttributes[] = sprintf("order=\"%s\"", $atts['order']);
}
$shortcodeAttributes = implode(' ', $shortcodeAttributes);
return do_shortcode('[products ' . $shortcodeAttributes . ' ]');
}) );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple('', 'editor_product_default_cart', function () {
return do_shortcode('[woocommerce_cart]');
}) );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple('', 'editor_product_checkout', function () {
return do_shortcode('[woocommerce_checkout]');
}) );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple('', 'editor_product_my_account', function () {
return do_shortcode('[woocommerce_my_account]');
}) );
$this->registerPlaceholder( new Brizy_Content_Placeholders_Simple('', 'editor_product_order_tracking', function () {
return do_shortcode('[woocommerce_order_tracking]');
}) );
}
private function filterData($property, $post)
{
switch ($property) {
case 'post_title':
return get_the_title($post);
case 'post_excerpt':
return get_the_excerpt($post);
case 'post_content':
return get_the_content($post);
case 'post_password':
return '';
default:
return $post->{$property};
}
}
/**
* It rewrite the wodpress function wp_trim_excerpt.
* The only thing we do is exclude the appling of the hook the_content.
* Further information read the description of the function getValue of this class.
*
* @param string $text
* @param null $post
*
* @return string
*/
public static function wp_trim_excerpt($text = '', $post = null)
{
global $pages;
// not sure why this is null (this happens on author pages.. maybe there are more)
//
if (is_null($pages))
$pages = [];
$raw_excerpt = $text;
if ('' == $text) {
$post = get_post($post);
$text = get_the_content('', false, $post);
$text = strip_shortcodes($text);
$text = excerpt_remove_blocks($text);
/** This filter is documented in wp-includes/post-template.php */
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]&gt;', $text);
/**
* Filters the number of words in an excerpt.
*
* @param int $number The number of words. Default 55.
*
* @since 2.7.0
*
*/
$excerpt_length = apply_filters('excerpt_length', 55);
/**
* Filters the string in the "more" link displayed after a trimmed excerpt.
*
* @param string $more_string The string shown within the more link.
*
* @since 2.9.0
*
*/
$excerpt_more = apply_filters('excerpt_more', ' ' . '[&hellip;]');
$text = wp_trim_words($text, $excerpt_length, $excerpt_more);
}
/**
* Filters the trimmed excerpt string.
*
* @param string $text The trimmed text.
* @param string $raw_excerpt The text prior to trimming.
*
* @since 2.8.0
*
*/
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
private function getPosts($atts)
{
// here are default posts arguments: https://codex.wordpress.org/Template_Tags/get_posts
// maybe here we need to change some attributes, unset or add something before make query
$posts = get_posts($atts);
return $posts;
}
private function displayPosts($posts, $extra_atts)
{
ob_start();
$thumbnail_size = ''; // possible sizes: thumbnail, medium, medium_large, large
if ((int)$extra_atts['columns'] > 1) {
$thumbnail_size = 'large';
}
foreach ($posts as $post) { ?>
<article class="brz-article">
<h2><a href="<?php echo get_permalink($post->ID); ?>"><?php echo get_the_title($post); ?></a></h2>
<div class="brz-post-thumbnail">
<?php echo get_the_post_thumbnail($post, $thumbnail_size); ?>
</div>
<div class="brz-post-description">
<?php echo $this->getPostExcerpt($post); ?>
</div>
<?php if ($extra_atts['display_date']) { ?>
<div class="brz-post-date">
<?php echo get_the_date("", $post); ?>
</div>
<?php } ?>
<?php if ($extra_atts['display_author']) { ?>
<div class="brz-post-author">
<a rel="author" href="<?php echo get_author_posts_url($post->post_author); ?>"><span
itemprop="name"><?php echo get_the_author_meta('display_name', $post->post_author); ?></span></a>
</div>
<?php } ?>
</article>
<?php
}
return ob_get_clean();
}
private function getPostExcerpt($post)
{
if (!empty($post->post_excerpt)) {
// if !empty excerpt
return $post->post_excerpt;
}
$the_excerpt = strip_tags(strip_shortcodes($post->post_content)); // Strips tags and shortcodes
$excerpt_length = 50; // Sets excerpt length by word count, default in WP is 55
$words = explode(' ', $the_excerpt, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
$the_excerpt = implode(' ', $words); // put in excerpt only the number of word that is set in $excerpt_length
}
return $the_excerpt;
}
}

View File

@@ -0,0 +1,11 @@
<?php
interface Brizy_Content_Providers_Interface {
const CONFIG_KEY_TEXT = 'richText';
const CONFIG_KEY_IMAGE = 'image';
const CONFIG_KEY_LINK = 'link';
const CONFIG_KEY_OEMBED = 'oembed';
const CONFIG_KEY_VIDEO = 'video';
const CONFIG_KEY_SNDCLOUD = 'sndcloud';
}

View File

@@ -0,0 +1,26 @@
<?php
class Brizy_Content_ShortcodeToPlaceholderProcessor implements Brizy_Editor_Content_ProcessorInterface {
/**
* @param string $content
* @param Brizy_Content_Context $context
*
* @return mixed|string
*/
public function process( $content, Brizy_Content_Context $context ) {
// Transform [brizy_shortcode_name attr="val"] to {{editor_shortcode_name attr='val'}}
$regex = '@(\[)(brizy_)(.*?)(\])@';
$content = preg_replace_callback( $regex, function ( $matches ) {
$shortcode = $matches[0];
$shortcode = str_replace( [ '[brizy', ']' ], [ '{{editor', '}}' ], $shortcode );
$shortcode = str_replace( '"', "'", $shortcode );
return $shortcode;
}, $content );
return $content;
}
}