Skip to content

Commit

Permalink
FIX pick up resampled images and attach shortcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed May 27, 2021
1 parent 5ecd860 commit 7ff1c14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/Extensions/TaskerSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,16 @@ public function requireDefaultRecords()
$upgrader = Injector::inst()->create($className);

try {
$upgrader->run(new HTTPRequest('GET', '/', [
'quiet' => true
]));

// update schema
DB::query(sprintf(
'UPDATE SiteConfig SET SchemaVersion = %s',
$latestSchema
));

$upgrader->run(new HTTPRequest('GET', '/', [
'quiet' => true
]));

DB::alteration_message('[Tasker] Upgraded project to schema '. $latestSchema, 'created');
} catch (Exception $e) {
DB::alteration_message('[Tasker] Error upgrading project to schema: '. $e->getMessage(), 'error');
Expand Down
43 changes: 20 additions & 23 deletions src/Traits/TaskHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,11 @@ public function runInsertOrUpdate($table, $id, $fields)
*/
public function updateFilePathLinks($table, $column)
{
$records = DB::query("SELECT ID, $column FROM $table WHERE $column LIKE '%assets/%'");
$records = DB::query("SELECT ID, $column FROM $table WHERE $column LIKE '%_resampled/%'")->map();

foreach ($records as $record) {
$id = $record['ID'];
$content = $record[$column];
$this->echoWarning(count($records) . ' to be updated with _resampled text');

foreach ($records as $id => $content) {
$html = new HTML5();
$needsWrite = false;

Expand All @@ -290,25 +289,23 @@ public function updateFilePathLinks($table, $column)
$shortcodeId = $img->getAttribute('data-id');

if (!$shortcodeId && !$shortcode && $src) {
if (substr($src, 0, 8) === "\/assets\/" || substr($src, 0,7) === 'assets/') {
if (strpos($src, '_resampled/')) {
if (strpos($src, '_resampled/')) {
// remove /_resampled/
$correctedFileName = preg_replace('/_resampled\/([A-Z])\w+[-|\/]/', '', $src);

$parts = explode('/', $src);

$file = File::get()->filter([
'Filename:PartialMatch:nocase' => $parts[count($parts) -1]
])->first();
} else {
$file = File::get()->filter('Filename:PartialMatch:nocase', $src)->first();
}
$file = File::get()->filter([
'Filename:PartialMatch:nocase' => $correctedFileName
])->first();
} else {
$file = File::get()->filter('Filename:PartialMatch:nocase', $src)->first();
}

// find the correct ID for the file and attach it to the image if we can.
if ($file) {
$img->setAttribute('data-shortcode', 'image');
$img->setAttribute('data-id', $file->ID);
// find the correct ID for the file and attach it to the image if we can.
if ($file) {
$img->setAttribute('data-shortcode', 'image');
$img->setAttribute('data-id', $file->ID);

$needsWrite = true;
}
$needsWrite = true;
}
} else if (strpos($src, '_resampled/') && $shortcodeId) {
$file = File::get()->byId($shortcodeId);
Expand All @@ -324,6 +321,8 @@ public function updateFilePathLinks($table, $column)
}

if ($needsWrite) {
$this->echoProgress();

DB::prepared_query("UPDATE \"$table\" SET \"$column\" = ? WHERE \"ID\" = ?", [
$html->saveHTML($doc),
$id
Expand All @@ -333,10 +332,8 @@ public function updateFilePathLinks($table, $column)
$this->echoSuccess("Updated {$table}.{$column} ". $id);
}
} else {

$this->echoProgress();
}

$this->echoProgress();
}

// if a live table exists, then also update that directly
Expand Down

0 comments on commit 7ff1c14

Please sign in to comment.