Skip to content

Commit eb00d3a

Browse files
author
Damyon Wiese
committed
Properly delete images when they are deleted in an admin setting.
1 parent f9d6559 commit eb00d3a

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

lib.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,34 @@ function theme_photo_update_settings_images($settingname) {
8383
// This is the component name the setting is stored in.
8484
$component = 'theme_photo';
8585

86+
8687
// This is the value of the admin setting which is the filename of the uploaded file.
8788
$filename = get_config($component, $settingname);
8889
// We extract the file extension because we want to preserve it.
8990
$extension = substr($filename, strrpos($filename, '.') + 1);
9091

9192
// This is the path in the moodle internal file system.
9293
$fullpath = "/{$syscontext->id}/{$component}/{$settingname}/0{$filename}";
94+
95+
// This location matches the searched for location in theme_config::resolve_image_location.
96+
$pathname = $CFG->dataroot . '/pix_plugins/theme/photo/' . $settingname . '.' . $extension;
97+
98+
// This pattern matches any previous files with maybe different file extensions.
99+
$pathpattern = $CFG->dataroot . '/pix_plugins/theme/photo/' . $settingname . '.*';
100+
101+
// Make sure this dir exists.
102+
@mkdir($CFG->dataroot . '/pix_plugins/theme/photo/', $CFG->directorypermissions, true);
103+
104+
// Delete any existing files for this setting.
105+
foreach (glob($pathpattern) as $filename) {
106+
@unlink($filename);
107+
}
108+
93109
// Get an instance of the moodle file storage.
94110
$fs = get_file_storage();
95111
// This is an efficient way to get a file if we know the exact path.
96112
if ($file = $fs->get_file_by_hash(sha1($fullpath))) {
97113
// We got the stored file - copy it to dataroot.
98-
// This location matches the searched for location in theme_config::resolve_image_location.
99-
$pathname = $CFG->dataroot . '/pix_plugins/theme/photo/' . $settingname . '.' . $extension;
100-
101-
// This pattern matches any previous files with maybe different file extensions.
102-
$pathpattern = $CFG->dataroot . '/pix_plugins/theme/photo/' . $settingname . '.*';
103-
104-
// Make sure this dir exists.
105-
@mkdir($CFG->dataroot . '/pix_plugins/theme/photo/', $CFG->directorypermissions, true);
106-
107-
// Delete any existing files for this setting.
108-
foreach (glob($pathpattern) as $filename) {
109-
@unlink($filename);
110-
}
111-
112-
// Copy the current file to this location.
113114
$file->copy_content_to($pathname);
114115
}
115116

version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
defined('MOODLE_INTERNAL') || die();
2727

2828
// This is the version of the plugin.
29-
$plugin->version = 2017010300;
29+
$plugin->version = 2017022000;
3030

3131
// This is the version of Moodle this plugin requires.
3232
$plugin->requires = 2016112900.00;
@@ -44,5 +44,5 @@
4444
$plugin->maturity = MATURITY_STABLE;
4545

4646
// This is the named version.
47-
$plugin->release = 1.2;
47+
$plugin->release = 1.3;
4848

0 commit comments

Comments
 (0)