2009-12-02 02:07:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ComicPressMediaHandling {
|
2009-12-02 02:37:28 +00:00
|
|
|
var $root_filter = '%wordpress%/%type-folder%/';
|
2009-12-05 02:25:30 +00:00
|
|
|
var $default_filter = '%wordpress%/%type-folder%/{date}*.*';
|
|
|
|
var $default_filename_filter = '%date-Y-m-d%';
|
2009-12-02 02:14:54 +00:00
|
|
|
|
2009-12-02 02:07:10 +00:00
|
|
|
function _bundle_global_variables() {
|
|
|
|
global $comic_folder, $archive_comic_folder, $rss_comic_folder, $mini_comic_folder;
|
|
|
|
|
|
|
|
$bundle = array();
|
|
|
|
foreach (array('comic', 'archive', 'rss', 'mini') as $which) {
|
|
|
|
switch ($which) {
|
|
|
|
case 'comic':
|
|
|
|
$bundle['comic'] = $comic_folder;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$bundle[$which] = ${"${which}_comic_folder"};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $bundle;
|
|
|
|
}
|
2009-12-02 02:14:54 +00:00
|
|
|
|
|
|
|
function _get_filter($filter_to_use = null) {
|
|
|
|
global $comic_filename_filters;
|
|
|
|
if (!is_null($filter_to_use)) {
|
|
|
|
if (is_string($filter_to_use)) {
|
|
|
|
if (isset($comic_filename_filters[$filter_to_use])) {
|
2009-12-02 02:37:28 +00:00
|
|
|
return $this->_convert_to_percent_filter($comic_filename_filters[$filter_to_use]);
|
2009-12-02 02:14:54 +00:00
|
|
|
}
|
2010-01-20 12:46:40 +00:00
|
|
|
$options = get_option('comicpress_options');
|
2010-01-24 00:25:24 +00:00
|
|
|
// if (isset($options['comic_filename_filters'][$filter_to_use])) {
|
|
|
|
// return $this->_convert_to_percent_filter($options['comic_filename_filters'][$filter_to_use]);
|
|
|
|
// }
|
2009-12-02 02:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-05 02:25:30 +00:00
|
|
|
$filter = $this->default_filter;
|
|
|
|
if ($result = get_option('comicpress-manager-cpm-date-format')) {
|
|
|
|
$filter = str_replace('{date}', "%date-${result}%", $filter);
|
|
|
|
} else {
|
|
|
|
$filter = str_replace('{date}', $this->default_filename_filter, $filter);
|
|
|
|
}
|
|
|
|
return $filter;
|
2009-12-02 02:14:54 +00:00
|
|
|
}
|
|
|
|
|
2009-12-02 02:37:28 +00:00
|
|
|
function _convert_to_percent_filter($old) {
|
|
|
|
if (strpos(strtolower($old), '%wordpress%') !== 0) {
|
|
|
|
$old = str_replace('{date}', '%date-Y-m-d%', $old);
|
|
|
|
return $this->root_filter . $old;
|
|
|
|
}
|
|
|
|
return $old;
|
|
|
|
}
|
|
|
|
|
2009-12-02 02:53:46 +00:00
|
|
|
function _expand_filter($filter, $type_folder, $override_post = null) {
|
2009-12-02 02:37:28 +00:00
|
|
|
global $post;
|
|
|
|
$this->post_to_use = !is_null($override_post) ? $override_post : $post;
|
2009-12-02 02:53:46 +00:00
|
|
|
$this->type_folder = $type_folder;
|
2009-12-02 02:37:28 +00:00
|
|
|
|
|
|
|
$result = preg_replace_callback('#%([a-z0-9-]+)%#i', array(&$this, '_expand_filter_callback'), $filter);
|
|
|
|
$result = str_replace('.', '\.', $result);
|
|
|
|
$result = str_replace('*', '.*', $result);
|
|
|
|
|
|
|
|
unset($this->post_to_use);
|
2009-12-02 02:53:46 +00:00
|
|
|
unset($this->type_folder);
|
2009-12-02 02:37:28 +00:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2009-12-05 02:03:09 +00:00
|
|
|
function _get_regex_dirname($input) {
|
|
|
|
return dirname($this->_resolve_regex_path($input));
|
|
|
|
}
|
|
|
|
|
|
|
|
function _get_regex_filename($input) {
|
|
|
|
$input = preg_replace('#\\\(?![.])#', '/', $input);
|
2009-12-05 20:00:34 +00:00
|
|
|
return preg_replace('#^.*\/([^\/]+)$#', '$1', $input);
|
2009-12-05 02:03:09 +00:00
|
|
|
}
|
|
|
|
|
2009-12-03 03:40:08 +00:00
|
|
|
function _resolve_regex_path($input) {
|
|
|
|
$input = str_replace('\.', '.', $input);
|
2009-12-03 23:40:21 +00:00
|
|
|
$input = str_replace('\\', '/', $input);
|
2009-12-03 03:40:08 +00:00
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
2009-12-02 03:13:21 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2009-12-02 02:40:40 +00:00
|
|
|
function _abspath() {
|
2009-12-25 20:43:32 +00:00
|
|
|
return trailingslashit($this->_resolve_regex_path(ABSPATH));
|
2009-12-02 02:40:40 +00:00
|
|
|
}
|
2009-12-02 03:13:21 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2009-12-02 02:37:28 +00:00
|
|
|
|
|
|
|
function _expand_filter_callback($matches) {
|
|
|
|
$value = '';
|
|
|
|
switch (strtolower($matches[1])) {
|
|
|
|
case 'wordpress':
|
2009-12-04 16:35:08 +00:00
|
|
|
$value = untrailingslashit($this->_abspath());
|
2009-12-02 02:37:28 +00:00
|
|
|
break;
|
|
|
|
case 'type-folder':
|
2009-12-02 02:53:46 +00:00
|
|
|
$value = $this->type_folder;
|
2009-12-02 02:37:28 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (preg_match('#^date-(.*)$#', $matches[1], $date_matches) > 0) {
|
2009-12-03 12:12:14 +00:00
|
|
|
if (isset($this->post_to_use)) {
|
|
|
|
$value = date($date_matches[1], strtotime($this->post_to_use->post_date));
|
|
|
|
break;
|
|
|
|
}
|
2009-12-02 02:37:28 +00:00
|
|
|
}
|
|
|
|
$value = $matches[0];
|
|
|
|
break;
|
|
|
|
}
|
2009-12-02 02:53:46 +00:00
|
|
|
return apply_filters('comicpress_expand_filter_callback', $value, $matches);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _read_directory($pattern) {
|
2009-12-05 02:03:09 +00:00
|
|
|
$dirname = $this->_get_regex_dirname($pattern);
|
2009-12-02 02:53:46 +00:00
|
|
|
$results = false;
|
|
|
|
if (is_dir($dirname)) {
|
|
|
|
$results = array();
|
|
|
|
if (($dh = opendir($dirname)) !== false) {
|
2009-12-05 02:03:09 +00:00
|
|
|
$filename_pattern = str_replace('#', '\#', $this->_get_regex_filename($pattern));
|
2009-12-02 02:53:46 +00:00
|
|
|
while (($file = readdir($dh)) !== false) {
|
|
|
|
$target = $dirname . '/' . $file;
|
|
|
|
if (is_file($target)) {
|
2009-12-02 03:27:54 +00:00
|
|
|
if (preg_match("#^${filename_pattern}$#", $file) > 0) {
|
2009-12-02 02:53:46 +00:00
|
|
|
$results[] = $target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-02 03:27:54 +00:00
|
|
|
closedir($dh);
|
2009-12-02 02:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $results;
|
2009-12-02 02:37:28 +00:00
|
|
|
}
|
2009-12-02 02:14:54 +00:00
|
|
|
|
2009-12-03 12:12:14 +00:00
|
|
|
function _check_post_meta_data($post_to_use, $type) {
|
|
|
|
if ($result = get_post_meta($post_to_use->ID, "backend_url_${type}", true)) {
|
|
|
|
if (is_string($result)) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result = get_post_meta($post_to_use->ID, "backend_url_images", true)) {
|
2009-12-03 23:43:07 +00:00
|
|
|
$types = false;
|
2009-12-03 12:12:14 +00:00
|
|
|
if (is_string($result)) {
|
|
|
|
parse_str($result, $types);
|
2009-12-03 23:43:07 +00:00
|
|
|
}
|
|
|
|
if (is_array($result)) {
|
|
|
|
$types = $result;
|
|
|
|
}
|
|
|
|
if (is_array($types)) {
|
2009-12-03 12:12:14 +00:00
|
|
|
if (isset($types[$type])) {
|
|
|
|
return $types[$type];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result = get_post_meta($post_to_use->ID, "backend_url", true)) {
|
|
|
|
if (is_string($result)) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _ensure_valid_uri($uri, $type) {
|
|
|
|
if (!empty($uri)) {
|
|
|
|
if (substr($uri, 0, 1) == '/') {
|
|
|
|
return $uri;
|
|
|
|
} else {
|
|
|
|
if (preg_match('#^[a-z]+://#', $uri) > 0) {
|
|
|
|
return $uri;
|
|
|
|
} else {
|
|
|
|
$bundle = $this->_bundle_global_variables();
|
|
|
|
if (isset($bundle[$type])) {
|
|
|
|
$this->type_folder = $bundle[$type];
|
|
|
|
} else {
|
|
|
|
$this->type_folder = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$uri = preg_replace_callback('#%([a-z0-9-]+)%#i', array(&$this, '_expand_filter_callback'), $uri);
|
|
|
|
|
2009-12-05 03:59:08 +00:00
|
|
|
return $uri;
|
2009-12-03 12:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-04 16:35:08 +00:00
|
|
|
|
2009-12-02 03:27:54 +00:00
|
|
|
/**
|
|
|
|
* Get the comic path.
|
|
|
|
* @param string $type The type to retrieve.
|
|
|
|
* @param object $override_post The post to use in place of the Loop post.
|
|
|
|
* @param string $filter The filter to use.
|
|
|
|
* @param boolean $multi If true, return all matching files.
|
|
|
|
* @return string|array|boolean A single comic URI relative to the WordPress base, multiple comic URIs, or false if an error occurred.
|
|
|
|
*/
|
2009-12-02 02:37:28 +00:00
|
|
|
function get_comic_path($type = 'comic', $override_post = null, $filter = 'default', $multi = false) {
|
2009-12-02 02:53:46 +00:00
|
|
|
global $post;
|
|
|
|
$post_to_use = !is_null($override_post) ? $override_post : $post;
|
|
|
|
|
2009-12-04 00:12:34 +00:00
|
|
|
if ($uri = $this->_check_post_meta_data($post_to_use, $type)) {
|
2009-12-05 03:59:08 +00:00
|
|
|
if ($result = $this->_ensure_valid_uri($uri, $type)) {
|
|
|
|
return preg_replace('#^/#', '', $result);
|
2009-12-03 12:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-02 03:27:54 +00:00
|
|
|
$filter = $this->_get_filter($filter);
|
2009-12-02 02:53:46 +00:00
|
|
|
$globals = $this->_bundle_global_variables();
|
|
|
|
|
|
|
|
if (isset($globals[$type])) {
|
|
|
|
$filter = $this->_expand_filter($filter, $globals[$type], $post_to_use);
|
2009-12-02 03:13:21 +00:00
|
|
|
if (is_array($results = $this->_read_directory($filter))) {
|
|
|
|
if (($pre_handle = apply_filters('comicpress_pre_handle_comic_path_results', false, $results, $type, $post_to_use)) !== false) {
|
|
|
|
return $pre_handle;
|
|
|
|
}
|
2009-12-02 03:27:54 +00:00
|
|
|
$new_results = array();
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$new_results[] = str_replace($this->_abspath(), '', $result);
|
|
|
|
}
|
|
|
|
if ($multi) {
|
|
|
|
return $new_results;
|
|
|
|
} else {
|
|
|
|
return reset($new_results);
|
|
|
|
}
|
2009-12-02 03:13:21 +00:00
|
|
|
}
|
2009-12-02 02:53:46 +00:00
|
|
|
}
|
2009-12-02 03:27:54 +00:00
|
|
|
return false;
|
2009-12-02 02:14:54 +00:00
|
|
|
}
|
2009-12-02 02:07:10 +00:00
|
|
|
}
|
2009-12-05 01:36:39 +00:00
|
|
|
|