function testURLToFileSystem() { $url = 'http://example.org/wp-content/uploads/2012/06/mypic.jpg'; $file = TimberURLHelper::url_to_file_system($url); $this->assertStringStartsWith(ABSPATH, $file); $this->assertStringEndsWith('/2012/06/mypic.jpg', $file); $this->assertNotContains($file, 'http://example.org'); $this->assertNotContains($file, '//'); }
/** * Deletes the auto-generated files for resize and letterboxing created by Timber * @param string $local_file ex: /var/www/wp-content/uploads/2015/my-pic.jpg * or: http://example.org/wp-content/uploads/2015/my-pic.jpg */ static function delete_generated_files($local_file) { if (TimberURLHelper::is_absolute($local_file)) { $local_file = TimberURLHelper::url_to_file_system($local_file); } $info = pathinfo($local_file); $dir = $info['dirname']; $ext = $info['extension']; $filename = $info['filename']; self::process_delete_generated_files($filename, $ext, $dir, '-[0-9999999]*', '-[0-9]*x[0-9]*-c-[a-z]*.'); self::process_delete_generated_files($filename, $ext, $dir, '-lbox-[0-9999999]*', '-lbox-[0-9]*x[0-9]*-[a-zA-Z0-9]*.'); }
/** * Deletes letterboxed versions of the supplied file name * * @param string $local_file */ static function delete_letterboxed_files($local_file) { if (TimberURLHelper::is_absolute($local_file)) { $local_file = TimberURLHelper::url_to_file_system($local_file); } $info = pathinfo($local_file); $dir = $info['dirname']; $ext = $info['extension']; $filename = $info['filename']; $searcher = '/' . $filename . '-lbox-[0-9999999]*'; foreach (glob($dir . $searcher) as $found_file) { $regexdir = str_replace('/', '\\/', $dir); $pattern = '/' . $regexdir . '\\/' . $filename . '-lbox-[0-9]*x[0-9]*-[a-zA-Z0-9]*.' . $ext . '/'; $match = preg_match($pattern, $found_file); if ($match) { unlink($found_file); } } }
static function delete_letterboxed_files_from_url($src) { $local = TimberURLHelper::url_to_file_system($src); self::delete_letterboxed_files($local); }
/** * @param int $iid */ function init($iid = false) { if (!is_numeric($iid) && is_string($iid)) { if (strstr($iid, '://')) { $this->init_with_url($iid); return; } if (strstr($iid, ABSPATH)) { $this->init_with_file_path($iid); return; } if (strstr(strtolower($iid), '.jpg')) { $this->init_with_relative_path($iid); return; } } $image_info = $this->get_image_info($iid); $this->import($image_info); $basedir = self::wp_upload_dir(); $basedir = $basedir['basedir']; if (isset($this->file)) { $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file; } else { if (isset($this->_wp_attached_file)) { $this->file = reset($this->_wp_attached_file); $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file; } else { if (isset($this->guid)) { if (TimberURLHelper::is_absolute($this->guid)) { $this->file_loc = TimberURLHelper::url_to_file_system($this->guid); } } } } if (isset($image_info['id'])) { $this->ID = $image_info['id']; } else { if (is_numeric($iid)) { $this->ID = $iid; } } if (isset($this->ID)) { $custom = get_post_custom($this->ID); foreach ($custom as $key => $value) { $this->{$key} = $value[0]; } } else { if (is_array($iid)) { TimberHelper::error_log('Not able to init in TimberImage with iid='); TimberHelper::error_log($iid); } else { TimberHelper::error_log('Not able to init in TimberImage with iid=' . $iid); } } }