comicpress-core/classes/ComicPress.inc

301 lines
8.4 KiB
PHP
Raw Normal View History

2009-07-08 23:51:02 +00:00
<?php
2009-11-07 22:13:13 +00:00
require_once(dirname(__FILE__) . '/ComicPressComicPost.inc');
2009-07-13 23:14:17 +00:00
/**
* The core functions of ComicPress.
*/
2009-07-08 23:51:02 +00:00
class ComicPress {
var $comicpress_options = array(
2009-11-12 12:56:32 +00:00
'image_types' => array(
'comic' => array(
'default' => true,
'name' => 'Comic',
'dimensions' => '760x'
),
'rss' => array(
'default' => false,
'name' => 'RSS',
'dimensions' => '350x'
),
'archive' => array(
'default' => false,
'name' => 'Archive',
'dimensions' => '125x'
),
'mini' => array(
'default' => false,
'name' => 'Minithumb',
'dimensions' => '100x'
),
),
2009-08-06 02:29:54 +00:00
'helpers' => array(),
2009-11-09 03:11:26 +00:00
'storyline_order' => '',
2010-01-31 21:40:27 +00:00
'enabled_backends' => null,
'category_groupings' => array()
2009-07-08 23:51:02 +00:00
);
2009-11-07 22:52:21 +00:00
2009-11-12 03:31:36 +00:00
var $backends = array();
2009-11-09 03:11:26 +00:00
2009-11-25 19:22:09 +00:00
function &get_instance($force = false) {
2009-11-01 18:15:59 +00:00
static $instance;
2009-11-07 22:52:21 +00:00
2009-11-25 19:22:09 +00:00
if (is_object($force)) {
$instance = array($force);
}
if (!$instance || ($force === true)) {
2009-11-01 19:04:15 +00:00
$instance = array(new ComicPress());
2009-11-01 18:15:59 +00:00
}
2009-11-07 22:52:21 +00:00
2009-11-01 19:04:15 +00:00
return $instance[0];
2009-11-01 18:15:59 +00:00
}
2009-11-07 22:52:21 +00:00
2009-07-13 23:14:13 +00:00
/**
* Load ComicPress options.
*/
2009-07-08 23:51:02 +00:00
function load() {
$result = get_option('comicpress-options');
if (is_array($result)) {
2009-11-12 12:56:32 +00:00
$this->comicpress_options = $this->_array_merge_replace_recursive($this->comicpress_options, $result);
2009-07-08 23:51:02 +00:00
}
}
2009-11-07 22:52:21 +00:00
2009-11-12 12:56:32 +00:00
function _array_merge_replace_recursive() {
$args = func_get_args();
foreach ($args as $arg) {
if (!is_array($arg)) { return end($args); }
}
$all_keys = array();
$result = array();
foreach ($args as $arg) {
$all_keys = array_merge($all_keys, array_keys($arg));
}
foreach ($all_keys as $key) {
$parts_to_merge = array();
foreach ($args as $arg) {
if (isset($arg[$key])) { $parts_to_merge[] = $arg[$key]; }
}
$result[$key] = call_user_func_array(array($this, '_array_merge_replace_recursive'), $parts_to_merge);
}
return $result;
}
2009-07-13 23:14:13 +00:00
/**
* Save ComicPress options.
*/
2009-07-08 23:51:02 +00:00
function save() {
if (is_array($this->comicpress_options)) {
update_option('comicpress-options', $this->comicpress_options);
}
}
2009-11-12 12:59:59 +00:00
// WordPress Filters
function intermediate_image_sizes($sizes) {
return array_merge($sizes, array_keys($this->comicpress_options['image_types']));
}
function editor_max_image_size($current_max, $size) {
2009-11-19 02:25:35 +00:00
if (is_string($size)) {
if (isset($this->comicpress_options['image_types'])) {
if (isset($this->comicpress_options['image_types'][$size])) {
if (isset($this->comicpress_options['image_types'][$size]['dimensions'])) {
list($width, $height) = explode('x', $this->comicpress_options['image_types'][$size]['dimensions']);
$current_max = array(intval($width), intval($height));
}
2009-11-16 17:35:48 +00:00
}
}
2009-11-12 12:59:59 +00:00
}
return $current_max;
}
2009-11-12 17:53:40 +00:00
function normalize_image_size_options() {
$protected_options = apply_filters('comicpress_protected_image_size_options', array('thumbnail', 'medium', 'large'));
2009-11-16 17:35:48 +00:00
foreach (wp_load_alloptions() as $option => $value) {
2009-11-12 17:53:40 +00:00
if (strpos($option, '_size_w') !== false) {
$size = str_replace('_size_w', '', $option);
if (!in_array($size, $protected_options)) {
foreach (array('_size_w', '_size_h', '_crop') as $suffix) {
delete_option("${size}${suffix}");
}
}
}
}
if (isset($this->comicpress_options['image_types'])) {
foreach ($this->comicpress_options['image_types'] as $type => $info) {
if (isset($info['dimensions'])) {
list($width, $height) = explode('x', $info['dimensions']);
2009-11-16 17:35:48 +00:00
foreach (array('_size_w', '_size_h', '_crop') as $suffix) {
delete_option("${type}${suffix}");
}
2009-11-12 17:53:40 +00:00
update_option("${type}_size_w", intval($width));
update_option("${type}_size_h", intval($height));
update_option("${type}_crop", 0);
}
}
}
}
// @codeCoverageIgnoreStart
2010-01-25 00:24:57 +00:00
function _get_declared_classes() { return get_declared_classes(); }
// @codeCoverageIgnoreEnd
2009-11-12 03:31:36 +00:00
2009-07-13 23:14:13 +00:00
/**
* Initialize the class.
*/
2009-07-08 23:51:02 +00:00
function init() {
$this->load();
2009-11-07 22:52:21 +00:00
add_filter('intermediate_image_sizes', array(&$this, 'intermediate_image_sizes'));
2009-11-09 03:11:26 +00:00
add_filter('editor_max_image_size', array(&$this, 'editor_max_image_size'), 10, 2);
2009-11-07 22:52:21 +00:00
2009-11-16 17:35:48 +00:00
$this->normalize_image_size_options();
2009-11-12 03:31:36 +00:00
2010-01-28 01:21:53 +00:00
$this->reset_backend_admin();
2010-01-25 00:24:57 +00:00
foreach ($this->normalize_active_backends() as $class) {
$this->backends[] = new $class();
2010-01-28 01:21:53 +00:00
$admin_class = preg_replace('#Factory$#', 'Admin', $class);
if (class_exists($admin_class)) {
if (method_exists($admin_class, 'actions')) {
foreach (call_user_func(array($admin_class, 'actions')) as $options) {
call_user_func_array('add_action', $options);
}
}
}
2010-01-25 00:24:57 +00:00
}
}
2010-01-28 01:21:53 +00:00
function reset_backend_admin() {
foreach ($this->_get_declared_classes() as $class) {
if (preg_match('#^ComicPressBackend(.*)Admin$#', $class)) {
if (method_exists($class, 'actions')) {
foreach (call_user_func(array($class, 'actions')) as $options) {
call_user_func_array('remove_action', $options);
}
}
}
}
}
2010-01-25 00:24:57 +00:00
function normalize_active_backends() {
2010-01-28 01:21:53 +00:00
if (!is_array($this->comicpress_options['enabled_backends'])) {
$this->comicpress_options['enabled_backends'] = $this->get_valid_backends();
2010-01-26 04:00:40 +00:00
}
2010-01-28 01:21:53 +00:00
$this->comicpress_options['enabled_backends'] = array_intersect($this->get_valid_backends(), $this->comicpress_options['enabled_backends']);
2010-01-26 04:00:40 +00:00
2010-01-28 01:21:53 +00:00
return $this->comicpress_options['enabled_backends'];
2010-01-26 04:00:40 +00:00
}
function get_valid_backends() {
2010-01-25 00:24:57 +00:00
$available_backends = array();
foreach ($this->_get_declared_classes() as $class) {
if (preg_match('#^ComicPressBackend(.*)Factory$#', $class, $matches) > 0) {
$available_backends[] = $class;
}
}
2010-01-26 04:00:40 +00:00
return $available_backends;
2010-01-25 00:24:57 +00:00
}
// @codeCoverageIgnoreStart
function announce_activated_helpers() {
echo "<center>[ <strong>Activated ComicPress helpers:</strong> " . implode(", ", array_keys($this->comicpress_options['helpers'])) . " ]</center>";
}
2009-07-19 23:20:23 +00:00
/**
* Gather blog posts for the given index page.
*/
function get_index_blog_posts_query() {
2009-07-19 23:20:23 +00:00
global $post, $paged;
2009-11-07 22:52:21 +00:00
2009-07-19 23:20:23 +00:00
$t = $post;
2009-11-07 22:52:21 +00:00
2009-07-19 23:20:23 +00:00
$wp_query = new WP_Query();
$wp_query->query(
2009-11-07 22:52:21 +00:00
'showposts=' .
2009-07-19 23:20:23 +00:00
(int)$this->comicpress_options['blogpost_count'] .
2009-11-07 22:52:21 +00:00
'&cat=-' .
$this->comicpress_options['comic_category_id'] .
2009-07-19 23:20:23 +00:00
'&paged=' .
$paged
);
2009-11-07 22:52:21 +00:00
return $wp_query;
2009-07-19 23:20:23 +00:00
}
2009-11-07 22:52:21 +00:00
// @codeCoverageIgnoreEnd
2009-11-07 21:38:22 +00:00
/**
* Search a path for directories named after the slugs provided.
* @param array $categories A list of category slugs going from child -> parent -> root.
* @param string $path The path to search.
* @return array All matching paths.
*/
2009-11-07 21:36:57 +00:00
function category_search($categories, $path) {
$path = trailingslashit($path);
$all_categories = array_reverse($categories);
$all_paths = array();
while (count($all_categories) > 0) {
$target = $path . implode('/', $all_categories);
if (is_dir($target)) { $all_paths[] = $target; }
array_pop($all_categories);
}
return array_reverse($all_paths);
}
2009-11-07 22:52:21 +00:00
2009-11-07 22:00:14 +00:00
/**
* Recursively search a particular template directory for a specified file, limiting the search to the provided categories.
* @param string $name The name of the file with no path information.
* @param string $path The path within the template directories to start the search.
* @param attay $categories The child -> root category slugs to search within the specified path. If not provided, retrieve from current post.
* @return string|boolean The path to the file, for false if not found.
*/
2009-11-07 22:35:51 +00:00
function find_file($name, $path = '', $categories = null) {
2009-11-07 22:13:13 +00:00
global $post;
if (!is_array($categories)) {
$comic_post = new ComicPressComicPost($post);
$categories = $comic_post->find_parents();
}
2009-11-07 22:52:21 +00:00
2009-11-07 22:00:14 +00:00
foreach (array(get_stylesheet_directory(), get_template_directory()) as $dir) {
$dir = trailingslashit($dir) . $path;
$dirs_to_search = $this->category_search($categories, $dir);
$dirs_to_search[] = $dir;
2009-11-07 22:52:21 +00:00
2009-11-07 22:00:14 +00:00
foreach ($dirs_to_search as $category_path) {
$target = trailingslashit($category_path) . $name;
if (file_exists($target)) {
return $target;
}
}
}
return false;
}
2009-11-16 17:35:48 +00:00
function get_default_image_type() {
if (isset($this->comicpress_options['image_types'])) {
if (is_array($this->comicpress_options['image_types'])) {
foreach ($this->comicpress_options['image_types'] as $type => $properties) {
2009-12-09 13:21:15 +00:00
if (isset($properties['default'])) {
if ($properties['default'] === true) {
return $type;
}
2009-11-16 17:35:48 +00:00
}
}
}
}
return false;
}
2009-07-08 23:51:02 +00:00
}
2010-01-30 16:11:07 +00:00
class ComicPressException extends Exception {}