Skip to content

Improve elements to allow file and inline bindings #14490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/elements/chunks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ignore everything
*
# except .gitignore
!.gitignore
4 changes: 4 additions & 0 deletions core/elements/snippets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ignore everything
*
# except .gitignore
!.gitignore
4 changes: 4 additions & 0 deletions core/elements/temlplates/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ignore everything
*
# except .gitignore
!.gitignore
6 changes: 5 additions & 1 deletion core/model/modx/modelement.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,11 @@ public function getSourceFile(array $options = [])
}
/** @var modMediaSource $source */
if ($source = $this->getSource()) {
$this->_sourceFile = !$source->getMetaData($filename) && $this->get('source') < 1
$sourceBasePath = $source->getBasePath();
$checkFilename = strpos($filename, $sourceBasePath) === 0 ? str_replace($sourceBasePath, '', $filename) : $filename;
$metaData = $source->getMetaData($checkFilename);

$this->_sourceFile = empty($metaData) && $this->get('source') < 1
? $this->getSourcePath($options) . $filename
: $filename;
} else {
Expand Down
77 changes: 75 additions & 2 deletions core/model/modx/modparser.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,80 @@ public function processTag($tag, $processUncacheable = true) {
*/
public function getElement($class, $name) {
$realname = $this->realname($name);
if (array_key_exists($class, $this->modx->sourceCache) && array_key_exists($realname, $this->modx->sourceCache[$class])) {
$binding = $inlineContent = $content = $path = '';

if (preg_match('#^@([A-Z]+)#', $name, $matches)) {
$binding = $matches[1];
$content = substr($name, strlen($binding) + 1);
$content = ltrim($content, ' :');
}

if ($class == 'modChunk' || $class == 'modTemplate') {
// Replace inline tags in chunks
$content = str_replace(array('{{', '}}'), array('[[', ']]'), $content);
}

/** @var modElement $element */
switch ($binding) {
case 'CODE':
case 'INLINE':
if ($class == 'modChunk' || $class == 'modTemplate') {
$inlineContent = $content;
}
break;

case 'FILE':
$basePath = $this->modx->getOption('static_elements_basepath');
$filename = rtrim($basePath, '/') . '/' . trim($content, '/');
$sourceId = $this->modx->getOption('static_elements_default_mediasource');

/** @var modMediaSource $source */
if ($sourceId && $source = $this->modx->getObject('sources.modMediaSource', $sourceId)) {
$source->initialize();
$metaData = $source->getMetaData($filename);
$path = !empty($metaData) ? $filename : '';
} else {
$source = modMediaSource::getDefaultSource($this->modx);
$source->initialize();
$path = file_exists($filename) ? $filename : '';
$sourceBasePath = $source->getBasePath();
$path = strpos($path, $sourceBasePath) === 0 ? str_replace($sourceBasePath, '', $path) : $path;

}
break;

default:
break;
}
unset($content);

if (!empty($inlineContent)) {
$cache_name = $binding . '__' . md5($name);
$element = $this->modx->newObject($class, array(
'name' => $cache_name,
'snippet' => $inlineContent,
));
$element->setContent($inlineContent);
if ($element instanceof modScript) {
$element->_scriptName = $element->getScriptName() . $cache_name;
}
} elseif (!empty($path)) {
$cache_name = $binding . '__' . str_replace('/', '-', $path);
$element = $this->modx->newObject($class, array(
'name' => $cache_name,
'static' => true,
'static_file' => $path,
'source' => $sourceId,
));
$element->setCacheable(false);
if (isset($source) && $source instanceof modMediaSource) {
$element->setSource($source);
}
if ($element instanceof modScript) {
$element->_scriptName = $element->getScriptName() . $cache_name;
}

} elseif (array_key_exists($class, $this->modx->sourceCache) && array_key_exists($realname, $this->modx->sourceCache[$class])) {
/** @var modElement $element */
$element = $this->modx->newObject($class);
$element->fromArray($this->modx->sourceCache[$class][$realname]['fields'], '', true, true);
Expand All @@ -554,7 +627,7 @@ public function getElement($class, $name) {
);
}
elseif(!$element) {
$evtOutput = $this->modx->invokeEvent('OnElementNotFound', array('class' => $class, 'name' => $realname));
$evtOutput = $this->modx->invokeEvent('OnElementNotFound', array('class' => $class, 'name' => (!isset($path) ? $realname : $name)));
$element = false;
if ($evtOutput != false) {
foreach ((array) $evtOutput as $elm) {
Expand Down
4 changes: 4 additions & 0 deletions core/model/modx/modscript.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public function loadScript() {
$result = is_readable($includeFilename);
$outdated = false;
$sourceFile = $this->getSourceFile();
if ($source = $this->getSource()) {
$sourceFile = $source->getBasePath() . ltrim($sourceFile, ' /');
}

if ($this->isStatic() && $result && !empty($sourceFile) && is_readable($sourceFile)) {
$includeMTime = filemtime($includeFilename);
$sourceMTime = filemtime($sourceFile);
Expand Down
6 changes: 4 additions & 2 deletions core/model/modx/sources/modmediasource.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,10 @@ public function getObjectContents($path)

$properties = $this->getPropertyList();
$imageExtensions = array_map('trim', explode(',', $this->getOption('imageExtensions', $properties, 'jpg,jpeg,png,gif,svg')));
$separatedPath = explode(DIRECTORY_SEPARATOR, $file->getPath());
$fa = [
'name' => rtrim($path, DIRECTORY_SEPARATOR),
'basename' => array_pop(explode(DIRECTORY_SEPARATOR, $file->getPath())),
'basename' => array_pop($separatedPath),
'path' => $file->getPath(),
'size' => $file->getSize(),
'last_accessed' => $file->getTimestamp(),
Expand Down Expand Up @@ -2266,7 +2267,8 @@ protected function isFileImage($file, $image_extensions = [])
{
try {
$mime = $this->filesystem->getMimetype($file);
$ext = strtolower(array_pop(explode(DIRECTORY_SEPARATOR, trim($file, DIRECTORY_SEPARATOR))));
$separatedFile = explode(DIRECTORY_SEPARATOR, trim($file, DIRECTORY_SEPARATOR));
$ext = strtolower(array_pop($separatedFile));

return strpos($mime, 'image') === 0 || in_array($ext, array_map('strtolower', $image_extensions));
} catch (Exception $e) {
Expand Down