140 lines
3.8 KiB
PHP
140 lines
3.8 KiB
PHP
<?php
|
|
/**
|
|
* core/template/filters.php
|
|
*
|
|
* This class is for managing template filters.
|
|
*
|
|
* @version 3.0
|
|
* @author Joey Kimsey <Joey@thetempusproject.com>
|
|
* @link https://TheTempusProject.com/Core
|
|
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
|
*/
|
|
namespace TheTempusProject\Houdini\Classes;
|
|
|
|
use TheTempusProject\Canary\Bin\Canary as Debug;
|
|
|
|
class Filters {
|
|
public static $filters = [
|
|
'comments' => [
|
|
'name' => 'comments',
|
|
'match' => '#/\*.*?\*/#s',
|
|
'replace' => '',
|
|
'enabled' => true,
|
|
],
|
|
'commentsTwo' => [
|
|
'name' => 'commentsTwo',
|
|
'match' => '#(?<!:)//.*#',
|
|
'replace' => '',
|
|
'enabled' => true,
|
|
],
|
|
'formComponents' => [
|
|
'name' => 'formComponents',
|
|
'match' => '#{CHECKED:(.*?)=(.*?)}#s',
|
|
'replace' => '',
|
|
'enabled' => true,
|
|
],
|
|
];
|
|
|
|
/**
|
|
* Iterates through the filters list on $data. Leaving only the internal
|
|
* contents of enabled filters and removing all traces of disabled filters.
|
|
*
|
|
* @param {string} [$data]
|
|
* @return {string}
|
|
*/
|
|
public static function apply( $data, $force = false ) {
|
|
if ( empty( self::$filters ) ) {
|
|
return $data;
|
|
}
|
|
foreach ( self::$filters as $pattern ) {
|
|
if ( $pattern['enabled'] || false !== $force ) {
|
|
$data = trim( preg_replace( $pattern['match'], $pattern['replace'], $data ) );
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public static function applyOne( $name, $data, $force = false ) {
|
|
if ( empty( self::$filters ) || empty( self::$filters[$name] ) ) {
|
|
return $data;
|
|
}
|
|
if ( self::$filters[$name]['enabled'] || false !== $force ) {
|
|
$data = trim(
|
|
preg_replace(
|
|
self::$filters[$name]['match'],
|
|
self::$filters[$name]['replace'],
|
|
$data
|
|
)
|
|
);
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* Adds a filter.
|
|
*
|
|
* @param {string} [$filterName]
|
|
* @param {string} [$match]
|
|
* @param {string} [$replace]
|
|
* @param {bool} [$enabled] - Whether the filter should be enabled or disabled.
|
|
*/
|
|
public static function add( $filterName, $match, $replace, $enabled = false ) {
|
|
if ( isset( self::$filters[$filterName] ) ) {
|
|
Debug::error( "Filter already exists: $filterName" );
|
|
return;
|
|
}
|
|
self::$filters[$filterName] = [
|
|
'name' => $filterName,
|
|
'match' => $match,
|
|
'replace' => $replace,
|
|
'enabled' => $enabled,
|
|
];
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* Removes a filter.
|
|
*
|
|
* @param {string} [$filterName]
|
|
* @return {bool}
|
|
*/
|
|
public function remove( $filterName ) {
|
|
if ( !isset( self::$filters[$filterName] ) ) {
|
|
Debug::error( "Filter does not exist: $filterName" );
|
|
return false;
|
|
}
|
|
unset( self::$filters[$filterName] );
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Enable a filter.
|
|
*
|
|
* @param {string} [$filterName]
|
|
* @return {bool}
|
|
*/
|
|
public function enable( $filterName ) {
|
|
if ( !isset( self::$filters[$filterName] ) ) {
|
|
Debug::error( "Filter does not exist: $filterName" );
|
|
return false;
|
|
}
|
|
self::$filters[$filterName]['enabled'] = true;
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Disables a filter.
|
|
*
|
|
* @param {string} [$filterName]
|
|
* @return {bool}
|
|
*/
|
|
public function disable( $filterName ) {
|
|
if ( !isset( self::$filters[$filterName] ) ) {
|
|
Debug::error( "Filter does not exist: $filterName" );
|
|
return false;
|
|
}
|
|
self::$filters[$filterName]['enabled'] = false;
|
|
return true;
|
|
}
|
|
}
|