first commit
This commit is contained in:
29
wp-content/plugins/brizy/editor/asset/abstract-storage.php
Normal file
29
wp-content/plugins/brizy/editor/asset/abstract-storage.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
|
||||
abstract class Brizy_Editor_Asset_AbstractStorage extends Brizy_Editor_Asset_StaticFile {
|
||||
|
||||
/**
|
||||
* @var Brizy_Editor_UrlBuilder
|
||||
*/
|
||||
protected $url_builder;
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Asset_AbstractStorage constructor.
|
||||
*
|
||||
* @param $url_builder
|
||||
*/
|
||||
public function __construct( $url_builder ) {
|
||||
$this->url_builder = $url_builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the asset and store it somewhere in uploads and return the new local url.
|
||||
*
|
||||
* @param $asset_url
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function store( $asset_url );
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Brizy_Editor_Asset_AssetProxyProcessor implements Brizy_Editor_Content_ProcessorInterface {
|
||||
|
||||
/**
|
||||
* @var Brizy_Editor_Asset_Storage
|
||||
*/
|
||||
private $storage;
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Asset_HtmlAssetProcessor constructor.
|
||||
*
|
||||
* @param Brizy_Editor_Asset_AbstractStorage $storage
|
||||
*/
|
||||
public function __construct( $storage ) {
|
||||
$this->storage = $storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @param Brizy_Content_Context $context
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function process( $content, Brizy_Content_Context $context ) {
|
||||
|
||||
preg_match_all( '/"(.[^"]*(?:\?|&|&)brizy=(.[^"]*))"/im', $content, $matches );
|
||||
|
||||
if ( ! isset( $matches[2] ) ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
foreach ( $matches[2] as $i => $url ) {
|
||||
$url = urldecode($url);
|
||||
$hash_matches = array();
|
||||
preg_match( "/^.[^#]*(#.*)$/", $url, $hash_matches );
|
||||
|
||||
$url = preg_replace( "/^(.[^#]*)#.*$/", '$1', $url );
|
||||
|
||||
if ( $url ) {
|
||||
// store and replace $url
|
||||
$new_url = $this->storage->store( $url );
|
||||
|
||||
if ( $new_url == $url ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( isset( $hash_matches[1] ) && $hash_matches[1] != '' ) {
|
||||
$new_url .= $hash_matches[1];
|
||||
}
|
||||
|
||||
$content = str_replace( $matches[1][ $i ], $new_url, $content );
|
||||
}
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
class Brizy_Editor_Asset_AssetProxyStorage extends Brizy_Editor_Asset_AbstractStorage {
|
||||
|
||||
|
||||
/**
|
||||
* Get the asset and store it somewhere in uploads and return the new local url.
|
||||
*
|
||||
* @param $asset_url
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function store( $asset_url ) {
|
||||
|
||||
//$asset_url = html_entity_decode( $asset_url );
|
||||
$new_url = $this->url_builder->page_upload_url( "assets/icons/" . basename( $asset_url ) );
|
||||
$new_path = $this->url_builder->page_upload_path( "assets/icons/" . basename( $asset_url ) );
|
||||
$external_url = $this->url_builder->external_asset_url( $asset_url );
|
||||
|
||||
|
||||
if ( $this->store_file( $external_url, $new_path ) ) {
|
||||
$asset_url = $new_url;
|
||||
}
|
||||
|
||||
return $asset_url;
|
||||
}
|
||||
|
||||
}
|
||||
28
wp-content/plugins/brizy/editor/asset/attachment-aware.php
Normal file
28
wp-content/plugins/brizy/editor/asset/attachment-aware.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
|
||||
trait Brizy_Editor_Asset_AttachmentAware {
|
||||
/**
|
||||
* @param $media_name
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
private function getAttachmentByMediaName( $media_name ) {
|
||||
|
||||
global $wpdb;
|
||||
|
||||
return $wpdb->get_var( $wpdb->prepare(
|
||||
"SELECT
|
||||
p.ID
|
||||
FROM {$wpdb->posts} p
|
||||
LEFT JOIN {$wpdb->postmeta} m ON ( p.ID = m.post_id )
|
||||
WHERE
|
||||
( m.meta_key = 'brizy_attachment_uid' AND m.meta_value = %s )
|
||||
AND p.post_type = 'attachment'
|
||||
AND p.post_status = 'inherit'
|
||||
GROUP BY p.ID
|
||||
ORDER BY p.post_date DESC",
|
||||
$media_name
|
||||
) );
|
||||
}
|
||||
}
|
||||
96
wp-content/plugins/brizy/editor/asset/cleaner.php
Normal file
96
wp-content/plugins/brizy/editor/asset/cleaner.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
class Brizy_Editor_Asset_Cleaner {
|
||||
const CLEAN_FILES_CRON_KEY = 'brizy-asset-clean-files';
|
||||
const CLEAN_EMPTY_DIRS_CRON_KEY = 'brizy-asset-clean-dirs';
|
||||
const FILE_LIFE_TIME = 2592000; // 30 days
|
||||
|
||||
public static function _init() {
|
||||
static $instance;
|
||||
|
||||
if ( ! $instance ) {
|
||||
$instance = new self();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Brizy_Admin_Cloud_Cron constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
add_action( self::CLEAN_FILES_CRON_KEY, array( $this, 'clean_files' ) );
|
||||
add_action( self::CLEAN_EMPTY_DIRS_CRON_KEY, array( $this, 'clean_empty_dirs' ) );
|
||||
add_filter( 'cron_schedules', [ $this, 'cron_schedules' ] );
|
||||
|
||||
if ( ! wp_next_scheduled( self::CLEAN_FILES_CRON_KEY ) ) {
|
||||
wp_schedule_event( time(), 'ten_minutes', self::CLEAN_FILES_CRON_KEY );
|
||||
}
|
||||
|
||||
if ( ! wp_next_scheduled( self::CLEAN_EMPTY_DIRS_CRON_KEY ) ) {
|
||||
wp_schedule_event( time(), 'daily', self::CLEAN_EMPTY_DIRS_CRON_KEY );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove images older than 30 days
|
||||
*/
|
||||
public function clean_files() {
|
||||
$wp_filesystem = new WP_Filesystem_Direct( null );
|
||||
$now = time();
|
||||
|
||||
foreach ( glob( $this->get_upload_dir() . '*/assets/images/{*/*.*,*/*/*.*}', GLOB_BRACE ) as $img ) {
|
||||
if ( $now - filemtime( $img ) >= self::FILE_LIFE_TIME ) {
|
||||
$wp_filesystem->delete( $img );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove empty folders
|
||||
*/
|
||||
public function clean_empty_dirs() {
|
||||
|
||||
$wp_filesystem = new WP_Filesystem_Direct( null );
|
||||
|
||||
foreach ( glob( $this->get_upload_dir() . '*/assets/images/*', GLOB_ONLYDIR ) as $dir ) {
|
||||
$this->rm_empty_dir( $dir, $wp_filesystem );
|
||||
}
|
||||
}
|
||||
|
||||
private function rm_empty_dir( $path, $wp_filesystem ) {
|
||||
|
||||
$empty = true;
|
||||
|
||||
foreach ( glob( $path . "/*" ) as $file ) {
|
||||
if ( is_dir( $file ) ) {
|
||||
if ( ! $this->rm_empty_dir( $file, $wp_filesystem ) ) {
|
||||
$empty = false;
|
||||
}
|
||||
} else {
|
||||
$empty = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $empty ) {
|
||||
$wp_filesystem->delete( $path, false, 'd' );
|
||||
}
|
||||
|
||||
return $empty;
|
||||
}
|
||||
|
||||
public function cron_schedules( $schedules ) {
|
||||
|
||||
$schedules['ten_minutes'] = array(
|
||||
'interval' => 600,
|
||||
'display' => esc_html__( 'Every Ten Minutes', 'brizy' ),
|
||||
);
|
||||
|
||||
return $schedules;
|
||||
}
|
||||
|
||||
public function get_upload_dir() {
|
||||
$urlBuilder = new Brizy_Editor_UrlBuilder( Brizy_Editor_Project::get() );
|
||||
return $urlBuilder->upload_path( 'brizy/' );
|
||||
}
|
||||
}
|
||||
245
wp-content/plugins/brizy/editor/asset/crop/cropper.php
Normal file
245
wp-content/plugins/brizy/editor/asset/crop/cropper.php
Normal file
@@ -0,0 +1,245 @@
|
||||
<?php
|
||||
|
||||
class Brizy_Editor_Asset_Crop_Cropper {
|
||||
|
||||
const BASIC_CROP_TYPE = 1;
|
||||
const ADVANCED_CROP_TYPE = 2;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $services;
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Asset_Crop_Cropper constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
$this->services = array(
|
||||
//'Brizy_Editor_Asset_Crop_ExternalService',
|
||||
'Brizy_Editor_Asset_Crop_WordpressService'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $target
|
||||
* @param $callback
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function serviceLoop( $source, $target, $callback ) {
|
||||
foreach ( $this->services as $serviceClass ) {
|
||||
try {
|
||||
/**
|
||||
* @var Brizy_Editor_Asset_Crop_ServiceInterface $service ;
|
||||
*/
|
||||
$service = new $serviceClass( $source, $target );
|
||||
if ( $callback( $service ) ) {
|
||||
return true;
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $target
|
||||
* @param $offsetX
|
||||
* @param $offsetY
|
||||
* @param $width
|
||||
* @param $height
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function serviceCrop( $source, $target, $offsetX, $offsetY, $width, $height ) {
|
||||
|
||||
return $this->serviceLoop( $source, $target, function ( Brizy_Editor_Asset_Crop_ServiceInterface $service ) use ( $offsetX, $offsetY, $width, $height ) {
|
||||
$result = $service->crop( $offsetX, $offsetY, $width, $height );
|
||||
|
||||
if ( $result ) {
|
||||
$result = $service->saveTargetImage();
|
||||
}
|
||||
|
||||
return $result;
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $target
|
||||
* @param $width
|
||||
* @param $height
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function serviceResize( $source, $target, $width, $height ) {
|
||||
return $this->serviceLoop( $source, $target, function ( Brizy_Editor_Asset_Crop_ServiceInterface $service ) use ( $width, $height ) {
|
||||
$result = $service->resize( $width, $height );
|
||||
|
||||
if ( $result ) {
|
||||
$result = $service->saveTargetImage();
|
||||
}
|
||||
|
||||
return $result;
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $target
|
||||
* @param $filterOptions
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function internalCrop( $source, $target, $filterOptions ) {
|
||||
if ( $filterOptions['format'] == "gif" ) {
|
||||
// do not resize
|
||||
return false;
|
||||
} else {
|
||||
list( $imgWidth, $imgHeight ) = $filterOptions['originalSize'];
|
||||
if ( $filterOptions['is_advanced'] === false ) {
|
||||
list( $requestedImgWidth, $requestedImgHeight ) = array_values( $filterOptions['requestedData'] );
|
||||
if ( $requestedImgWidth > $imgWidth && ( $requestedImgHeight == "any" || $requestedImgHeight == "*" ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->serviceResize( $source, $target, $requestedImgWidth, $requestedImgHeight );
|
||||
}
|
||||
|
||||
list( $requestedImgWidth, $requestedImgHeight, $requestedOffsetX, $requestedOffsetY, $containerWidth, $containerHeight ) = array_values( $filterOptions['requestedData'] );
|
||||
|
||||
if ( ( $containerWidth > $imgWidth || $containerHeight > $imgHeight ) || $requestedImgWidth > $imgWidth && $requestedImgHeight > $imgHeight ) {
|
||||
|
||||
$newOffsetX = $imgWidth * $requestedOffsetX / $requestedImgWidth;
|
||||
$newOffsetY = $imgHeight * $requestedOffsetY / $requestedImgHeight;
|
||||
|
||||
$newImgWidth = $imgWidth * $containerWidth / $requestedImgWidth;
|
||||
$newImgHeight = $imgHeight * $containerHeight / $requestedImgHeight;
|
||||
|
||||
return $this->serviceCrop( $source, $target, $newOffsetX, $newOffsetY, $newImgWidth, $newImgHeight );
|
||||
|
||||
} else {
|
||||
|
||||
return $this->serviceLoop( $source, $target, function ( Brizy_Editor_Asset_Crop_ServiceInterface $service ) use ( $source, $target, $requestedImgWidth, $requestedImgHeight, $requestedOffsetX, $requestedOffsetY, $containerWidth, $containerHeight ) {
|
||||
|
||||
$result = $service->resize( $requestedImgWidth, null );
|
||||
|
||||
$imageSizeAfterResize = $service->getSize();
|
||||
|
||||
$blackStripOnX = ( $containerWidth + $requestedOffsetX ) - $requestedImgWidth;
|
||||
$blackStripOnY = ( $containerHeight + $requestedOffsetY ) - $requestedImgHeight;
|
||||
|
||||
$requestedImgWidth = min( $imageSizeAfterResize['width'], $requestedImgWidth );
|
||||
$requestedImgHeight = min( $imageSizeAfterResize['height'], $requestedImgHeight );
|
||||
|
||||
// calculated the crop over boundary values
|
||||
$containerWidthStripDelta = $blackStripOnX > 0 ? $blackStripOnX : 0;
|
||||
$containerHeightStripDelta = $blackStripOnY > 0 ? $blackStripOnY : 0;
|
||||
|
||||
// make sure the requested crop offset and size does now go over image boundaries
|
||||
$requestedOffsetX = ( $blackStripOnX ) > 0 ? ( $requestedOffsetX - $containerWidthStripDelta ) : $requestedOffsetX;
|
||||
$requestedOffsetY = ( $blackStripOnY ) > 0 ? ( $requestedOffsetY - $containerHeightStripDelta ) : $requestedOffsetY;
|
||||
|
||||
// avoid the case when the image height or with is less that 1px (GD problem)
|
||||
$containerWidth = $containerWidth > $requestedImgWidth ? $requestedImgWidth : $containerWidth;
|
||||
$containerHeight = $containerHeight > $requestedImgHeight ? $requestedImgHeight : $containerHeight;
|
||||
|
||||
if ( $result && $service->crop( $requestedOffsetX, $requestedOffsetY, $containerWidth, $containerHeight ) ) {
|
||||
$result = $service->saveTargetImage();
|
||||
|
||||
if ( ! $result ) {
|
||||
@unlink( $target );
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
return false;
|
||||
} );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $target
|
||||
* @param $filter
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function crop( $source, $target, $filter, $originalSizes ) {
|
||||
|
||||
try {
|
||||
wp_raise_memory_limit( 'image' );
|
||||
|
||||
return $this->internalCrop( $source, $target, $this->getFilterOptions( $source, $filter, $originalSizes ) );
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->error( $e->getMessage(), [ $e ] );
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $filter
|
||||
*
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getFilterOptions( $source, $filter, $originalSizes ) {
|
||||
|
||||
parse_str( strtolower( $filter ), $output );
|
||||
$configuration = array();
|
||||
$configuration['format'] = pathinfo( basename( $source ), PATHINFO_EXTENSION );
|
||||
$configuration['originalSize'] = $originalSizes;
|
||||
|
||||
$cropType = $this->getCropType( $filter );
|
||||
|
||||
switch ( $cropType ) {
|
||||
case self::BASIC_CROP_TYPE:
|
||||
$configuration['requestedData']['imageWidth'] = (int) $output['iw'];
|
||||
$configuration['requestedData']['imageHeight'] = (int) $output['ih'];
|
||||
$configuration['is_advanced'] = false;
|
||||
break;
|
||||
case self::ADVANCED_CROP_TYPE:
|
||||
$configuration['requestedData']['imageWidth'] = (int) $output['iw'];
|
||||
$configuration['requestedData']['imageHeight'] = (int) $output['ih'];
|
||||
$configuration['requestedData']['offsetX'] = (int) $output['ox'];
|
||||
$configuration['requestedData']['offsetY'] = (int) $output['oy'];
|
||||
$configuration['requestedData']['cropWidth'] = (int) $output['cw'];
|
||||
$configuration['requestedData']['cropHeight'] = (int) $output['ch'];
|
||||
$configuration['is_advanced'] = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return $configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filter
|
||||
*
|
||||
* @return int|null
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getCropType( $filter ) {
|
||||
$regExAdvanced = "/^iW=[0-9]{1,4}&iH=[0-9]{1,4}&oX=[0-9]{1,4}&oY=[0-9]{1,4}&cW=[0-9]{1,4}&cH=[0-9]{1,4}$/is";
|
||||
$regExBasic = "/^iW=[0-9]{1,4}&iH=([0-9]{1,4}|any|\*{1})$/is";
|
||||
|
||||
if ( preg_match( $regExBasic, $filter ) ) {
|
||||
$cropType = self::BASIC_CROP_TYPE;
|
||||
} elseif ( preg_match( $regExAdvanced, $filter ) ) {
|
||||
$cropType = self::ADVANCED_CROP_TYPE;
|
||||
} else {
|
||||
throw new Exception( "Invalid size format." );
|
||||
}
|
||||
|
||||
return $cropType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: alex
|
||||
* Date: 4/4/19
|
||||
* Time: 3:51 PM
|
||||
*/
|
||||
|
||||
class Brizy_Editor_Asset_Crop_ExternalService implements Brizy_Editor_Asset_Crop_ServiceInterface {
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $targetPath;
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Asset_Crop_WordpressService constructor.
|
||||
*
|
||||
* @param $sourcePath
|
||||
* @param $targetPath
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct( $sourcePath, $targetPath ) {
|
||||
|
||||
if ( ! file_exists( $sourcePath ) ) {
|
||||
throw new Exception( 'Unable to crop media. Source file not found.' );
|
||||
}
|
||||
|
||||
$this->targetPath = $targetPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offsetX
|
||||
* @param int $offsetY
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function crop( $offsetX, $offsetY, $width, $height ) {
|
||||
throw new Exception( 'Not implemented' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function resize( $width, $height ) {
|
||||
throw new Exception( 'Not implemented' );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function saveTargetImage() {
|
||||
throw new Exception( 'Not implemented' );
|
||||
}
|
||||
|
||||
public function getSize() {
|
||||
throw new Exception( 'Not implemented' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
interface Brizy_Editor_Asset_Crop_ServiceInterface {
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Asset_Crop_ServiceInterface constructor.
|
||||
*
|
||||
* @param string $sourcePath File paths
|
||||
* @param string $targetPath File paths
|
||||
*/
|
||||
public function __construct( $sourcePath, $targetPath );
|
||||
|
||||
/**
|
||||
* @param int $offsetX
|
||||
* @param int $offsetY
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function crop( $offsetX, $offsetY, $width, $height );
|
||||
|
||||
/**
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function resize( $width, $height );
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSize();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function saveTargetImage();
|
||||
}
|
||||
105
wp-content/plugins/brizy/editor/asset/crop/wordpress-service.php
Normal file
105
wp-content/plugins/brizy/editor/asset/crop/wordpress-service.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: alex
|
||||
* Date: 4/4/19
|
||||
* Time: 3:51 PM
|
||||
*/
|
||||
|
||||
class Brizy_Editor_Asset_Crop_WordpressService implements Brizy_Editor_Asset_Crop_ServiceInterface {
|
||||
|
||||
/**
|
||||
* @var WP_Image_Editor
|
||||
*/
|
||||
private $imageEditor;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $targetPath;
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Asset_Crop_WordpressService constructor.
|
||||
*
|
||||
* @param $sourcePath
|
||||
* @param $targetPath
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct( $sourcePath, $targetPath ) {
|
||||
|
||||
if ( ! file_exists( $sourcePath ) ) {
|
||||
throw new Exception( 'Unable to crop media. Source file not found.' );
|
||||
}
|
||||
|
||||
$this->targetPath = $targetPath;
|
||||
$this->imageEditor = wp_get_image_editor( $sourcePath );
|
||||
|
||||
if ( $this->imageEditor instanceof WP_Error ) {
|
||||
Brizy_Logger::instance()->error( $this->imageEditor->get_error_message(), array( $this->imageEditor ) );
|
||||
throw new Exception( "Unable to obtain the image editor" );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offsetX
|
||||
* @param int $offsetY
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function crop( $offsetX, $offsetY, $width, $height ) {
|
||||
try {
|
||||
$this->imageEditor->crop( $offsetX, $offsetY, $width, $height );
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->error( $e->getMessage(), [ $e ] );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public function getSize() {
|
||||
try {
|
||||
return $this->imageEditor->get_size();
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->error( $e->getMessage(), [ $e ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function resize( $width, $height ) {
|
||||
|
||||
try {
|
||||
$this->imageEditor->resize( $width, $height );
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->error( $e->getMessage(), [ $e ] );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function saveTargetImage() {
|
||||
$result = $this->imageEditor->save( $this->targetPath );
|
||||
|
||||
if ( $result instanceof WP_Error ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Brizy_Editor_Asset_CssAssetProcessor implements Brizy_Editor_Content_ProcessorInterface {
|
||||
/**
|
||||
* @var Brizy_Editor_Asset_Storage
|
||||
*/
|
||||
private $storage;
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Asset_HtmlAssetProcessor constructor.
|
||||
*
|
||||
* @param Brizy_Editor_Asset_Storage $storage
|
||||
*/
|
||||
public function __construct( Brizy_Editor_Asset_Storage $storage ) {
|
||||
$this->storage = $storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find and cache all assets and replace the urls with new local ones.
|
||||
*
|
||||
* The css assets must be ignored as we have a special processor this.
|
||||
*
|
||||
* @param $content
|
||||
*
|
||||
* @param Brizy_Content_Context $context
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function process( $content, Brizy_Content_Context $context ) {
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
21
wp-content/plugins/brizy/editor/asset/domain-processor.php
Normal file
21
wp-content/plugins/brizy/editor/asset/domain-processor.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Brizy_Editor_Asset_DomainProcessor implements Brizy_Editor_Content_ProcessorInterface {
|
||||
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @param Brizy_Content_Context $context
|
||||
*
|
||||
* @return mixed|null|string|string[]
|
||||
*/
|
||||
public function process( $content, Brizy_Content_Context $context ) {
|
||||
|
||||
$url = home_url();
|
||||
|
||||
$content = Brizy_SiteUrlReplacer::restoreSiteUrl( $content, $url );
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
113
wp-content/plugins/brizy/editor/asset/media-asset-processor.php
Normal file
113
wp-content/plugins/brizy/editor/asset/media-asset-processor.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Brizy_Editor_Asset_MediaAssetProcessor implements Brizy_Editor_Content_ProcessorInterface {
|
||||
|
||||
/**
|
||||
* @var Brizy_Editor_Asset_Storage
|
||||
*/
|
||||
private $storage;
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Asset_HtmlAssetProcessor constructor.
|
||||
*
|
||||
* @param Brizy_Editor_Asset_AbstractStorage $storage
|
||||
*/
|
||||
public function __construct( $storage ) {
|
||||
$this->storage = $storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find and cache all assets and replace the urls with new local ones.
|
||||
*
|
||||
* @param string $content
|
||||
* @param Brizy_Content_Context $context
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function process( $content, Brizy_Content_Context $context ) {
|
||||
return $this->process_external_asset_urls( $content, $context );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @param Brizy_Content_Context $context
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function process_external_asset_urls( $content, Brizy_Content_Context $context ) {
|
||||
|
||||
$site_url = str_replace( array( 'http://', 'https://' ), '', home_url() );
|
||||
$site_url = str_replace( array( '/', '.' ), array( '\/', '\.' ), $site_url );
|
||||
|
||||
$project = $context->getProject();
|
||||
|
||||
$uidKey = Brizy_Editor::prefix( Brizy_Public_CropProxy::ENDPOINT );
|
||||
$postIdKey = Brizy_Editor::prefix( Brizy_Public_CropProxy::ENDPOINT_POST );
|
||||
$sizeKey = Brizy_Editor::prefix( Brizy_Public_CropProxy::ENDPOINT_FILTER );
|
||||
|
||||
preg_match_all( '/(http|https):\/\/' . $site_url . '\/?(\?' . $uidKey . '=(.[^"\',\s)]*))/im', $content, $matches );
|
||||
|
||||
if ( empty( $matches[0] ) ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
$uids = [];
|
||||
|
||||
foreach ( $matches[0] as $url ) {
|
||||
try {
|
||||
$args = $this->getQueryArgs( $url );
|
||||
$uid = $args[ $uidKey ];
|
||||
|
||||
if ( ! is_numeric( $uid ) && ! in_array( $uid, $uids ) ) {
|
||||
$uids[] = $uid;
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$mediaCache = new Brizy_Editor_CropCacheMedia( $project );
|
||||
|
||||
$mediaCache->cacheImgs( $uids );
|
||||
|
||||
foreach ( $matches[0] as $url ) {
|
||||
|
||||
try {
|
||||
$args = $this->getQueryArgs( $url );
|
||||
$postId = null;
|
||||
|
||||
if ( ! empty( $args[ $postIdKey ] ) && $postId = $args[ $postIdKey ] ) {
|
||||
$postId = wp_is_post_revision( $postId ) ? wp_get_post_parent_id( $postId ) : $postId;
|
||||
}
|
||||
|
||||
$croppedUrl = $mediaCache->tryOptimizedPath( $args[ $uidKey ], $args[ $sizeKey ], $postId );
|
||||
$content = str_replace( $url, $croppedUrl, $content );
|
||||
} catch ( Exception $e ) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getQueryArgs( $url ) {
|
||||
$endpoint = Brizy_Editor::prefix( Brizy_Public_CropProxy::ENDPOINT );
|
||||
$parsedUrl = parse_url( html_entity_decode( $url ) );
|
||||
|
||||
if ( empty( $parsedUrl['query'] ) ) {
|
||||
throw new Exception( 'The query does not exists.' );
|
||||
}
|
||||
|
||||
parse_str( $parsedUrl['query'], $args );
|
||||
|
||||
if ( empty( $args[ $endpoint ] ) ) {
|
||||
throw new Exception( 'Invalid query.' );
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Brizy_Editor_Asset_MediaProxyStorage extends Brizy_Editor_Asset_AbstractStorage {
|
||||
|
||||
/**
|
||||
* Get the asset and store it somewhere in uploads and return the new local url.
|
||||
*
|
||||
* @param $asset_url
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function store( $asset_url ) {
|
||||
|
||||
$asset_url = html_entity_decode( $asset_url );
|
||||
$sufix_url = $this->getAssetPart( $asset_url, $this->config['urls']['image'] );
|
||||
$new_url = $this->url_builder->page_upload_url( "assets/images/".$sufix_url );
|
||||
$new_path = $this->url_builder->page_upload_path( "assets/images/".$sufix_url );
|
||||
$external_url = $this->url_builder->external_media_url( $sufix_url );
|
||||
|
||||
if ( $this->store_file( $external_url, $new_path ) ) {
|
||||
$asset_url = $new_url;
|
||||
}
|
||||
|
||||
return $asset_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param $prefix
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAssetPart( $url, $prefix ) {
|
||||
return str_replace( $prefix, '', $url );
|
||||
}
|
||||
|
||||
}
|
||||
30
wp-content/plugins/brizy/editor/asset/media.php
Normal file
30
wp-content/plugins/brizy/editor/asset/media.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php if ( ! defined( 'ABSPATH' ) ) {
|
||||
die( 'Direct access forbidden.' );
|
||||
}
|
||||
|
||||
class Brizy_Editor_Asset_Media {
|
||||
|
||||
private $url;
|
||||
private $path;
|
||||
|
||||
public function __construct( $url, $path ) {
|
||||
$this->path = $path;
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
public function get_path() {
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
public function get_url() {
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
public function get_name() {
|
||||
return basename( $this->get_path() );
|
||||
}
|
||||
|
||||
public function get_type() {
|
||||
return pathinfo( $this->get_path(), PATHINFO_EXTENSION );
|
||||
}
|
||||
}
|
||||
249
wp-content/plugins/brizy/editor/asset/static-file-trait.php
Normal file
249
wp-content/plugins/brizy/editor/asset/static-file-trait.php
Normal file
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
|
||||
trait Brizy_Editor_Asset_StaticFileTrait
|
||||
{
|
||||
|
||||
public static function get_asset_content($asset_source)
|
||||
{
|
||||
$http = new WP_Http();
|
||||
$wp_response = null;
|
||||
if (is_string($asset_source)) {
|
||||
$wp_response = $http->request($asset_source, array('timeout' => 30));
|
||||
} else {
|
||||
foreach ($asset_source as $url) {
|
||||
$wp_response = $http->request($url, array('timeout' => 30));
|
||||
|
||||
if (is_wp_error($wp_response)) {
|
||||
Brizy_Logger::instance()->error('Unable to get media content', array('exception' => $wp_response));
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$code = wp_remote_retrieve_response_code($wp_response);
|
||||
|
||||
if (is_wp_error($wp_response) || !($code >= 200 && $code < 300)) {
|
||||
Brizy_Logger::instance()->error('Unable to get media content', array('exception' => $wp_response));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$content = wp_remote_retrieve_body($wp_response);
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $asset_source
|
||||
* @param $asset_path
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function store_file($asset_source, $asset_path)
|
||||
{
|
||||
|
||||
if (file_exists($asset_path)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
// check destination dir
|
||||
$dir_path = dirname($asset_path);
|
||||
|
||||
if (!file_exists($dir_path)) {
|
||||
if (!file_exists($dir_path) && !mkdir($dir_path, 0755, true) && !is_dir($dir_path)) {
|
||||
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir_path));
|
||||
}
|
||||
}
|
||||
|
||||
$content = self::get_asset_content($asset_source);
|
||||
|
||||
if ($content !== false) {
|
||||
file_put_contents($asset_path, $content);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
// clean up
|
||||
if ($asset_path) {
|
||||
@unlink($asset_path);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function create_attachment($madia_name, $absolute_asset_path, $relative_asset_path, $post_id = null, $uid = null)
|
||||
{
|
||||
return self::createMediaAttachment($absolute_asset_path, $relative_asset_path, $post_id, $uid);
|
||||
}
|
||||
|
||||
public static function createMediaAttachment($absolute_asset_path, $relative_asset_path, $post_id = null, $uid = null)
|
||||
{
|
||||
$filetype = wp_check_filetype($absolute_asset_path);
|
||||
|
||||
$upload_path = wp_upload_dir();
|
||||
|
||||
$attachment = array(
|
||||
'guid' => $upload_path['baseurl'] . "/" . $relative_asset_path,
|
||||
'post_mime_type' => $filetype['type'],
|
||||
'post_title' => basename($absolute_asset_path),
|
||||
'post_content' => '',
|
||||
'post_status' => 'inherit'
|
||||
);
|
||||
|
||||
$attachment_id = wp_insert_attachment($attachment, $relative_asset_path, $post_id);
|
||||
|
||||
if (is_wp_error($attachment_id) || $attachment_id === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
update_post_meta($attachment_id, 'brizy_attachment_uid', $uid ? $uid : md5($attachment_id . time()));
|
||||
|
||||
if (!function_exists('wp_generate_attachment_metadata')) {
|
||||
include_once ABSPATH . "/wp-admin/includes/image.php";
|
||||
}
|
||||
|
||||
$attach_data = wp_generate_attachment_metadata($attachment_id, $absolute_asset_path);
|
||||
wp_update_attachment_metadata($attachment_id, $attach_data);
|
||||
|
||||
return $attachment_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filename
|
||||
* @param array $headers
|
||||
*/
|
||||
public function send_file($filename, $headers = array())
|
||||
{
|
||||
if (file_exists($filename)) {
|
||||
|
||||
$defaultHeaders = array(
|
||||
'Content-Type' => self::get_mime($filename, 1),
|
||||
'Cache-Control' => 'max-age=600'
|
||||
);
|
||||
|
||||
$content = file_get_contents($filename);
|
||||
|
||||
// send headers
|
||||
$headers = array_merge($defaultHeaders, $headers);
|
||||
|
||||
foreach ($headers as $key => $val) {
|
||||
if (is_array($val)) {
|
||||
$val = implode(', ', $val);
|
||||
}
|
||||
|
||||
header("{$key}: {$val}");
|
||||
}
|
||||
// send file content
|
||||
echo $content;
|
||||
exit;
|
||||
} else {
|
||||
global $wp_query;
|
||||
$wp_query->set_404();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filename
|
||||
* @param int $mode
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
public static function get_mime($filename, $mode = 0)
|
||||
{
|
||||
|
||||
// mode 0 = full check
|
||||
// mode 1 = extension check only
|
||||
|
||||
$mime_types = array(
|
||||
|
||||
'txt' => 'text/plain',
|
||||
'htm' => 'text/html',
|
||||
'html' => 'text/html',
|
||||
'php' => 'text/html',
|
||||
'css' => 'text/css',
|
||||
'js' => 'application/javascript',
|
||||
'json' => 'application/json',
|
||||
'xml' => 'application/xml',
|
||||
'swf' => 'application/x-shockwave-flash',
|
||||
'flv' => 'video/x-flv',
|
||||
|
||||
// images
|
||||
'png' => 'image/png',
|
||||
'jpe' => 'image/jpeg',
|
||||
'jpeg' => 'image/jpeg',
|
||||
'jpg' => 'image/jpeg',
|
||||
'gif' => 'image/gif',
|
||||
'bmp' => 'image/bmp',
|
||||
'ico' => 'image/vnd.microsoft.icon',
|
||||
'tiff' => 'image/tiff',
|
||||
'tif' => 'image/tiff',
|
||||
'svg' => 'image/svg+xml',
|
||||
'svgz' => 'image/svg+xml',
|
||||
'webp' => 'image/webp',
|
||||
|
||||
// archives
|
||||
'zip' => 'application/zip',
|
||||
'rar' => 'application/x-rar-compressed',
|
||||
'exe' => 'application/x-msdownload',
|
||||
'msi' => 'application/x-msdownload',
|
||||
'cab' => 'application/vnd.ms-cab-compressed',
|
||||
|
||||
// audio/video
|
||||
'mp3' => 'audio/mpeg',
|
||||
'qt' => 'video/quicktime',
|
||||
'mov' => 'video/quicktime',
|
||||
|
||||
// adobe
|
||||
'pdf' => 'application/pdf',
|
||||
'psd' => 'image/vnd.adobe.photoshop',
|
||||
'ai' => 'application/postscript',
|
||||
'eps' => 'application/postscript',
|
||||
'ps' => 'application/postscript',
|
||||
|
||||
// ms office
|
||||
'doc' => 'application/msword',
|
||||
'rtf' => 'application/rtf',
|
||||
'xls' => 'application/vnd.ms-excel',
|
||||
'ppt' => 'application/vnd.ms-powerpoint',
|
||||
'docx' => 'application/msword',
|
||||
'xlsx' => 'application/vnd.ms-excel',
|
||||
'pptx' => 'application/vnd.ms-powerpoint',
|
||||
|
||||
|
||||
// open office
|
||||
'odt' => 'application/vnd.oasis.opendocument.text',
|
||||
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
|
||||
);
|
||||
|
||||
$array = explode('.', $filename);
|
||||
$str = end($array);
|
||||
$ext = strtolower($str);
|
||||
|
||||
if (function_exists('mime_content_type') && $mode == 0) {
|
||||
$mimetype = mime_content_type($filename);
|
||||
|
||||
return $mimetype;
|
||||
|
||||
} elseif (function_exists('finfo_open') && $mode == 0) {
|
||||
$finfo = finfo_open(FILEINFO_MIME);
|
||||
$mimetype = finfo_file($finfo, $filename);
|
||||
finfo_close($finfo);
|
||||
|
||||
return $mimetype;
|
||||
} elseif (array_key_exists($ext, $mime_types)) {
|
||||
return $mime_types[$ext];
|
||||
} else {
|
||||
return 'application/octet-stream';
|
||||
}
|
||||
}
|
||||
}
|
||||
6
wp-content/plugins/brizy/editor/asset/static-file.php
Normal file
6
wp-content/plugins/brizy/editor/asset/static-file.php
Normal file
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
abstract class Brizy_Editor_Asset_StaticFile {
|
||||
|
||||
use Brizy_Editor_Asset_StaticFileTrait;
|
||||
}
|
||||
95
wp-content/plugins/brizy/editor/asset/storage.php
Normal file
95
wp-content/plugins/brizy/editor/asset/storage.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Brizy_Editor_Asset_Storage extends Brizy_Editor_Asset_AbstractStorage {
|
||||
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Asset_Storage constructor.
|
||||
*
|
||||
* @param $url_builder
|
||||
* @param $config
|
||||
*/
|
||||
public function __construct( $url_builder, $config ) {
|
||||
parent::__construct( $url_builder );
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the asset and store it somewhere in uploads and return the new local url.
|
||||
*
|
||||
* @param $asset_url
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function store( $asset_url ) {
|
||||
|
||||
$asset_url = html_entity_decode( $asset_url );
|
||||
|
||||
if ( $this->isEditorUrl( $asset_url ) ) {
|
||||
$sufix_url = $this->getAssetPart( $asset_url, $this->config['urls']['assets'] );
|
||||
$new_path = $this->url_builder->editor_asset_path( $sufix_url );
|
||||
$new_url = $this->url_builder->upload_url( $new_path );
|
||||
|
||||
if ( $this->store_file( $asset_url, $new_path ) ) {
|
||||
$asset_url = $new_url;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $this->isStaticUrl( $asset_url ) ) {
|
||||
$sufix_url = $this->getAssetPart( $asset_url, $this->config['urls']['static'] );
|
||||
$new_path = $this->url_builder->page_upload_path( $sufix_url );
|
||||
$new_url = $this->url_builder->page_upload_url( $sufix_url );
|
||||
|
||||
if ( $this->store_file( $asset_url, $new_path ) ) {
|
||||
$asset_url = $new_url;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $this->isMediaUrl( $asset_url ) ) {
|
||||
$sufix_url = $this->getAssetPart( $asset_url, $this->config['urls']['image'] );
|
||||
$new_path = $this->url_builder->media_asset_path( $sufix_url );
|
||||
$new_url = $this->url_builder->media_asset_url( $sufix_url );
|
||||
|
||||
if ( $this->store_file( $asset_url, $new_path ) ) {
|
||||
$asset_url = $new_url;
|
||||
}
|
||||
}
|
||||
|
||||
return $asset_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isStaticUrl( $url ) {
|
||||
return strpos( $url, $this->config['urls']['static'] ) === 0;
|
||||
}
|
||||
|
||||
public function getAssetPart( $url, $prefix ) {
|
||||
return str_replace( $prefix, '', $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isEditorUrl( $url ) {
|
||||
return strpos( $url, $this->config['urls']['assets'] ) === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isMediaUrl( $url ) {
|
||||
return strpos( $url, $this->config['urls']['image'] ) === 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Brizy_Editor_Asset_SvgAssetProcessor implements Brizy_Editor_Content_ProcessorInterface {
|
||||
|
||||
|
||||
/**
|
||||
* Find and cache all assets and replace the urls with new local ones.
|
||||
*
|
||||
* @param string $content
|
||||
* @param Brizy_Content_Context $context
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function process( $content, Brizy_Content_Context $context ) {
|
||||
|
||||
$content = $this->process_external_asset_urls( $content, $context );
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @param Brizy_Content_Context $context
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function process_external_asset_urls( $content, Brizy_Content_Context $context ) {
|
||||
|
||||
$site_url = str_replace( array( 'http://', 'https://' ), '', home_url() );
|
||||
$site_url = str_replace( array( '/', '.' ), array( '\/', '\.' ), $site_url );
|
||||
|
||||
//preg_match_all( '/' . $site_url . '\/?(\?' . Brizy_Public_CropProxy::ENDPOINT . '=(.[^"\',\s)]*))/im', $content, $matches );
|
||||
$brizy_attachment = Brizy_Editor::prefix('_attachment');
|
||||
preg_match_all( '/(http|https):\/\/' . $site_url . '\/?(\?'.$brizy_attachment.'=(.[^"\',\s)]*))/im', $content, $matches );
|
||||
|
||||
if ( ! isset( $matches[0] ) || count( $matches[0] ) == 0 ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
foreach ( $matches[0] as $i => $url ) {
|
||||
|
||||
$parsed_url = parse_url( html_entity_decode( $matches[0][ $i ] ) );
|
||||
|
||||
if ( ! isset( $parsed_url['query'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
parse_str( $parsed_url['query'], $params );
|
||||
|
||||
if ( ! isset( $params[$brizy_attachment] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$media_path = $this->get_attachment_file_by_uid( $params[$brizy_attachment] );
|
||||
|
||||
if ( ! $media_path ) {
|
||||
return $content;
|
||||
}
|
||||
$content = str_replace( $matches[0][ $i ], $media_path, $content );
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
private function get_attachment_file_by_uid( $attachmentUId ) {
|
||||
if ( ! is_numeric( $attachmentUId ) ) {
|
||||
global $wpdb;
|
||||
|
||||
$posts_table = $wpdb->posts;
|
||||
$meta_table = $wpdb->postmeta;
|
||||
$attachment = $wpdb->get_var( $wpdb->prepare(
|
||||
"SELECT
|
||||
{$posts_table}.ID
|
||||
FROM {$posts_table}
|
||||
INNER JOIN {$meta_table} ON ( {$posts_table}.ID = {$meta_table}.post_id )
|
||||
WHERE
|
||||
{$meta_table}.meta_key = 'brizy_attachment_uid'
|
||||
AND {$meta_table}.meta_value = %s
|
||||
AND {$posts_table}.post_type = 'attachment'
|
||||
GROUP BY {$posts_table}.ID
|
||||
ORDER BY {$posts_table}.post_date DESC",
|
||||
$attachmentUId
|
||||
) );
|
||||
|
||||
|
||||
if ( ! $attachment ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
return wp_get_attachment_url( (int)$attachment );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user