comicpress-core/classes/backends/ComicPressBackendFilesystem...

453 lines
14 KiB
PHP
Raw Normal View History

2009-11-24 12:52:53 +00:00
<?php
require_once(dirname(__file__) . '/../ComicPressBackend.inc');
class ComicPressBackendFilesystem extends ComicPressBackend {
var $search_string = '';
var $id, $files_by_type = array(), $file_urls_by_type = array();
var $source_name;
function __construct() {
parent::__construct();
$this->source_name = __('Filesystem', 'comicpress');
}
2010-03-18 03:28:52 +00:00
function _getimagesize($file) {
if (file_exists($file)) {
return getimagesize($file);
} else {
return array(0, 0);
}
}
2010-02-02 03:41:56 +00:00
function dims($size = null) {
$dims = array();
2010-02-02 03:41:56 +00:00
if ($result = $this->_getimagesize($this->files_by_type[$this->ensure_type($size)])) {
$dims = array_combine(array('width', 'height'), array_slice($result, 0, 2));
}
return $dims;
}
function url($size = null) {
return $this->file_urls_by_type[$this->ensure_type($size)];
}
function file($size = null) { return $this->files_by_type[$this->ensure_type($size)]; }
2010-02-11 23:02:23 +00:00
function alt() { return $this->alt_text; }
function title() { return $this->title_text; }
2009-12-06 21:17:09 +00:00
}
class ComicPressBackendFilesystemFactory {
2010-01-28 01:21:53 +00:00
function __construct() {
$this->description = __('Uses files on the filesystem as comic images, similar to ComicPress Legacy.', 'comicpress');
}
2009-12-06 21:17:09 +00:00
function generate_from_id($id) {
if (preg_match('#^filesystem-([0-9]+)-(.*)$#', $id, $matches) > 0) {
list($all, $post_id, $root) = $matches;
if (($result = get_post_meta($post_id, 'backend_filesystem_files_by_type', true)) !== false) {
if (is_array($result)) {
if (isset($result[0][$root])) {
$return = new ComicPressBackendFilesystem();
$return->id = $id;
$return->root = $root;
foreach (array('files_by_type', 'file_urls_by_type') as $index => $name) {
$return->{$name} = $result[$index][$root];
}
if (($result = get_post_meta($post_id, 'backend_filesystem_image_meta', true)) !== false) {
if (is_array($result)) {
if (isset($result[$root])) {
foreach ($result[$root] as $key => $value) {
$return->{$key} = $value;
}
}
}
}
return $return;
}
2009-12-06 21:17:09 +00:00
}
}
}
return false;
}
2010-01-01 21:35:48 +00:00
function _get_search_pattern() {
return $this->_get_pattern('search_pattern');
}
function _get_url_pattern() {
return $this->_get_pattern('url_pattern');
}
function _get_pattern($which) {
2009-12-06 21:17:09 +00:00
$comicpress = ComicPress::get_instance();
if (isset(
$comicpress->comicpress_options['backend_options']['filesystem'][$which]
2009-12-06 21:17:09 +00:00
)) {
return (string)$comicpress->comicpress_options['backend_options']['filesystem'][$which];
2009-12-06 21:17:09 +00:00
}
2010-01-01 21:35:48 +00:00
return '';
}
function generate_from_post($post) {
$return = array();
$comicpress = ComicPress::get_instance();
$this->search_string = $this->_get_search_pattern();
2010-01-01 21:35:48 +00:00
2009-12-06 21:17:09 +00:00
if (isset($comicpress->comicpress_options['image_types'])) {
$files = array();
$all_patterns = array();
foreach (array_keys($comicpress->comicpress_options['image_types']) as $type) {
$patterns = $this->process_search_string($post, $type);
if (!empty($patterns)) {
$result = $this->find_matching_files($patterns);
if (!empty($result)) {
$files[$type] = $result;
}
}
$all_patterns = array_merge($all_patterns, $patterns);
}
2010-02-16 03:06:59 +00:00
2009-12-06 21:17:09 +00:00
if (($filename_pattern = $this->has_common_filename_pattern($all_patterns)) !== false) {
if (!empty($files)) {
$grouped_by_root = $this->group_by_root($filename_pattern, $files);
$urls_by_root = $this->get_urls_for_post_roots($grouped_by_root, $post);
$image_meta_by_root = array();
update_post_meta($post->ID, 'backend_filesystem_files_by_type', array($grouped_by_root, $urls_by_root));
2009-12-06 21:17:09 +00:00
foreach ($grouped_by_root as $root => $files_for_root) {
$fs = new ComicPressBackendFilesystem();
$fs->id = 'filesystem-' . $post->ID . '-' . $root;
$fs->files_by_type = $files_for_root;
$fs->file_urls_by_type = $urls_by_root[$root];
2009-12-06 21:17:09 +00:00
$return[] = $fs;
$image_meta_by_root[$root] = array(
'alt_text' => '',
'title_text' => ''
);
}
if (!($check = get_post_meta($post->ID, 'backend_filesystem_image_meta', true))) {
update_post_meta($post->ID, 'backend_filesystem_image_meta', array($image_meta_by_root));
2009-12-06 21:17:09 +00:00
}
}
}
}
return $return;
}
2009-11-24 12:52:53 +00:00
function process_search_string($post, $type, $filename = null) {
2009-11-24 12:52:53 +00:00
$this->_searches = array($this->search_string);
$this->_filename = $filename;
2009-11-24 12:52:53 +00:00
do {
$any_found = false;
for ($i = 0; $i < count($this->_searches); ++$i) {
$search = $this->_searches[$i];
if (preg_match('#%([a-z0-9-]+?)%#i', $search, $matches) > 0) {
$found = false;
$parts = explode('-', $matches[1]);
foreach (array(
'_replace_' . strtolower(str_replace('-', '_', $matches[1])) => null,
'_replace_' . strtolower($parts[0]) => implode('-', array_slice($parts, 1))
) as $method => $additional) {
if (method_exists($this, $method)) {
$any_found = true;
$found = true;
$result = $this->{$method}($post, $type, $additional);
if ($result !== false) {
$this->_searches[$i] = str_replace($matches[0], $result, $search);
break;
} else {
// array state change, start over
break;
}
2009-11-24 12:52:53 +00:00
}
}
if (!$found) {
2009-11-24 12:52:53 +00:00
$this->_searches[$i] = str_replace($matches[0], '', $search);
}
}
}
2009-11-24 23:49:32 +00:00
}
// @codeCoverageIgnoreStart
while ($any_found);
// @codeCoverageIgnoreEnd
2009-11-24 12:52:53 +00:00
return $this->_searches;
}
2009-11-24 23:49:32 +00:00
// @codeCoverageIgnoreStart
function _replace_wordpress($post, $type) { return untrailingslashit(ABSPATH); }
2009-11-24 23:49:32 +00:00
// @codeCoverageIgnoreEnd
function _replace_wordpress_url($post, $type) { return untrailingslashit(get_option('home')); }
function _replace_filename($post, $type) { return $this->_filename; }
2009-11-24 12:52:53 +00:00
function _replace_type($post, $type) { return $type; }
function _replace_title($post, $type) { return "*"; }
2010-01-24 18:38:07 +00:00
function _replace_upload_path($post, $type) { return get_option('upload_path'); }
2010-01-01 21:35:48 +00:00
function _replace_type_folder($post, $type) {
$comicpress = ComicPress::get_instance();
if (isset($comicpress->comicpress_options['backend_options']['filesystem']['folders'][$type])) {
return $comicpress->comicpress_options['backend_options']['filesystem']['folders'][$type];
}
return '';
2010-01-01 21:35:48 +00:00
}
function _replace_date($post, $type, $additional) {
return date($additional, strtotime($post->post_date));
}
2009-11-24 12:52:53 +00:00
function _replace_categories($post, $type) {
if (count($post_categories = wp_get_post_categories($post->ID)) == 1) {
$current_parent = reset($post_categories);
$all_slugs = array();
do {
if ($keep_searching = ($current_parent != 0)) {
$category = get_category($current_parent);
if (!empty($category)) {
array_unshift($all_slugs, $category->slug);
$current_parent = $category->parent;
} else {
2009-11-24 23:49:32 +00:00
$all_slugs = array();
2009-11-24 12:52:53 +00:00
break;
}
}
2009-11-24 23:49:32 +00:00
}
// @codeCoverageIgnoreStart
while ($keep_searching);
// @codeCoverageIgnoreEnd
2009-11-24 12:52:53 +00:00
$new_searches = array();
$slug_count = count($all_slugs);
foreach ($this->_searches as $search) {
if ($slug_count == 0) {
$new_searches[] = preg_replace('#%categories%#i', '', $search);
} else {
for ($i = 0; $i < $slug_count; ++$i) {
$new_searches[] = preg_replace('#%categories%#i', implode('/', array_slice($all_slugs, 0, $slug_count - $i)), $search);
}
}
}
$this->_searches = $new_searches;
}
return false;
}
2009-11-25 00:19:53 +00:00
2009-12-06 18:53:11 +00:00
2009-11-25 00:19:53 +00:00
function find_matching_files($patterns) {
$matches = array();
2009-11-25 00:19:53 +00:00
foreach ($patterns as $pattern) {
2009-12-06 18:53:11 +00:00
$dir = $this->get_regex_dirname($pattern);
2009-11-25 00:19:53 +00:00
if (is_dir($dir)) {
2009-12-06 18:53:11 +00:00
$pattern = $this->get_regex_filename($pattern);
2009-11-25 00:19:53 +00:00
if (($dh = opendir($dir)) !== false) {
while (($file = readdir($dh)) !== false) {
$target = $dir . '/' . $file;
if (is_file($target) || is_link($target)) {
if (preg_match('#' . $pattern. '#', $file) > 0) {
$matches[] = $target;
2009-11-25 00:19:53 +00:00
}
}
}
closedir($dh);
}
}
}
return $matches;
2009-11-25 00:19:53 +00:00
}
2009-11-25 00:33:16 +00:00
2009-12-06 21:17:09 +00:00
function get_regex_dirname($input) {
return dirname($this->resolve_regex_path($input));
}
2009-11-25 00:33:16 +00:00
2009-12-06 21:17:09 +00:00
function get_regex_filename($input) {
$input = preg_replace('#\\\(?![.])#', '/', $input);
$input = preg_replace('#^.*\/([^\/]+)$#', '$1', $input);
$input = preg_replace('#(?<![.])\*#', '.*', $input);
return $input;
}
2009-11-25 00:33:16 +00:00
2009-12-06 21:17:09 +00:00
function resolve_regex_path($input) {
$input = str_replace('\.', '.', $input);
$input = str_replace('\\', '/', $input);
return $input;
2009-11-25 00:33:16 +00:00
}
2009-11-25 03:15:34 +00:00
function has_common_filename_pattern($patterns) {
$filename_patterns = array_unique(array_map('basename', $patterns));
return (count($filename_patterns) == 1) ? reset($filename_patterns) : false;
}
2009-11-25 03:15:34 +00:00
function group_by_root($filename_pattern, $all_files) {
$roots = array();
$filename_pattern = str_replace('*', '(.*)', basename($filename_pattern));
foreach ($all_files as $type => $files) {
foreach ($files as $file) {
$filename = basename($file);
if (preg_match('#^' . $filename_pattern . '$#', $filename, $matches) > 0) {
$filename = $matches[1];
}
if (!isset($roots[$filename])) { $roots[$filename] = array(); }
$roots[$filename][$type] = $file;
}
}
return $roots;
}
function get_urls_for_post_roots($roots, $post) {
$urls = array();
$this->search_string = $this->_get_url_pattern();
foreach ($roots as $root => $files) {
$urls[$root] = array();
foreach ($files as $type => $file) {
$urls[$root][$type] = reset($this->process_search_string($post, $type, basename($file)));
}
}
return $urls;
}
2009-11-25 03:15:34 +00:00
}
2010-01-01 21:35:48 +00:00
// @codeCoverageIgnoreStart
class ComicPressBackendFilesystemAdmin {
function options_admin() {
$factory = new ComicPressBackendFilesystemFactory();
$filesystem_pattern = $factory->_get_search_pattern();
$url_pattern = $factory->_get_url_pattern();
2010-01-01 21:35:48 +00:00
include('partials/backend-filesystem/options-admin.inc');
}
function image_type_holder($type) {
$comicpress = ComicPress::get_instance();
$path = '';
if (
isset($comicpress->comicpress_options['backend_options']['filesystem']['folders'][$type])
) {
$path = $comicpress->comicpress_options['backend_options']['filesystem']['folders'][$type];
}
include('partials/backend-filesystem/image-type-holder.inc');
}
// @codeCoverageIgnoreEnd
2010-01-01 21:35:48 +00:00
function handle_update_comicpress_options($info) {
2010-02-12 23:08:17 +00:00
if (is_array($info)) {
if (isset($info['backend_options']['filesystem'])) {
if (is_array($info['backend_options']['filesystem'])) {
$info = $info['backend_options']['filesystem'];
$comicpress = ComicPress::get_instance();
if (!isset($comicpress->comicpress_options['backend_options'])) {
$comicpress->comicpress_options['backend_options'] = array();
}
if (!isset($comicpress->comicpress_options['backend_options']['filesystem'])) {
$comicpress->comicpress_options['backend_options']['filesystem'] = array();
}
2010-01-01 21:35:48 +00:00
2010-02-12 23:08:17 +00:00
foreach (array('folders', 'search_pattern', 'url_pattern') as $valid_field) {
if (isset($info[$valid_field])) {
if (is_array($info[$valid_field])) {
$comicpress->comicpress_options['backend_options']['filesystem'][$valid_field] = array();
foreach ($info[$valid_field] as $field => $value) {
$comicpress->comicpress_options['backend_options']['filesystem'][$valid_field][$field] = strip_tags($value);
}
} else {
$comicpress->comicpress_options['backend_options']['filesystem'][$valid_field] = strip_tags($info[$valid_field]);
}
}
2010-01-01 21:35:48 +00:00
}
2010-02-12 23:08:17 +00:00
$comicpress->save();
2010-01-01 21:35:48 +00:00
}
}
}
}
2010-01-28 01:21:53 +00:00
function save_post($post_id) {
if (isset($_POST['cp'])) {
$info = $_POST['cp'];
2010-02-12 21:55:11 +00:00
if (is_array($info)) {
if (isset($info['attachments'])) {
if (is_array($info['attachments'])) {
$image_meta_updates = array();
foreach ($info['attachments'] as $id => $properties) {
if (is_array($properties)) {
if ($backend = ComicPressBackend::generate_from_id($id)) {
if (is_a($backend, 'ComicPressBackendFilesystem')) {
$image_meta_updates[$backend->root] = array();
foreach (array('alt_text', 'title_text') as $field) {
if (isset($properties[$field])) {
$image_meta_updates[$backend->root][$field] = strip_tags($properties[$field]);
} else {
$image_meta_updates[$backend->root][$field] = '';
}
}
}
}
}
}
2010-02-12 21:55:11 +00:00
update_post_meta($post_id, 'backend_filesystem_image_meta', $image_meta_updates);
}
}
}
}
}
// @codeCoverageIgnoreStart
function comic_ordering_holder($backend) {
if (is_a($backend, 'ComicPressBackendFilesystem')) {
?>
<table class="widefat">
<tr>
<th scope="row">Title (hover) text:</th>
<td>
<input type="text" name="cp[attachments][<?php echo esc_attr($backend->id) ?>][title_text]" value="<?php echo esc_attr($backend->title()) ?>" style="width:100%" />
</td>
</tr>
<tr>
<th scope="row">Alt (accessibility) text:</th>
<td>
<input type="text" name="cp[attachments][<?php echo esc_attr($backend->id) ?>][alt_text]" value="<?php echo esc_attr($backend->alt()) ?>" style="width:100%" />
</td>
</tr>
</table>
<?php
}
}
2010-01-28 01:21:53 +00:00
function actions() {
return array(
array('comicpress-options-admin', array('ComicPressBackendFilesystemAdmin', 'options_admin')),
array('comicpress-image-type-holder', array('ComicPressBackendFilesystemAdmin', 'image_type_holder'), 10, 1),
array('comicpress-handle_update_comicpress_options', array('ComicPressBackendFilesystemAdmin', 'handle_update_comicpress_options'), 10, 1),
array('comicpress-comic-ordering-holder', array('ComicPressBackendFilesystemAdmin', 'comic_ordering_holder'), 10, 1),
array('save_post', array('ComicPressBackendFilesystemAdmin', 'save_post'), 10, 1)
2010-01-28 01:21:53 +00:00
);
}
2010-01-01 21:35:48 +00:00
}
// @codeCoverageIgnoreEnd