comicpress-core/functions.inc

149 lines
3.8 KiB
PHP
Raw Normal View History

2009-08-07 19:50:06 +00:00
<?php
foreach (array(
'F' => 3,
'Protect' => 0,
'Restore' => 0,
'Unprotect' => 0,
'M' => 1,
2009-11-19 12:27:10 +00:00
'EM' => 3,
'SL' => 1,
'SC' => 2,
) as $function => $param_count) {
if ($param_count == 0) {
add_action("comicpress-${function}", $function, 10);
} else {
add_filter("comicpress-${function}", $function, 10, $param_count);
}
}
2009-11-16 17:46:53 +00:00
// Global template functions for ComicPress
2009-08-07 19:50:06 +00:00
2009-11-07 22:18:11 +00:00
function F($name, $path, $override_post = null) {
global $post;
2009-11-08 01:56:49 +00:00
2009-11-07 22:18:11 +00:00
$comic_post = new ComicPressComicPost(is_null($override_post) ? $post : $override_post);
2009-11-13 00:12:30 +00:00
$comicpress = ComicPress::get_instance();
return $comicpress->find_file($name, $path, $comic_post->find_parents());
2009-11-07 22:18:11 +00:00
}
2009-11-08 01:56:49 +00:00
/**
* Protect global $post and $wp_query.
2009-11-19 12:27:10 +00:00
* @param object $use_this_post If provided, after saving the current post, set up this post for template tag use.
2009-11-08 01:56:49 +00:00
*/
2009-11-19 02:25:35 +00:00
function Protect($use_this_post = null) {
2009-11-08 01:56:49 +00:00
global $post, $wp_query, $__post, $__wp_query;
$__post = $post;
$__wp_query = $wp_query;
2009-11-19 02:25:35 +00:00
if (!is_null($use_this_post)) {
$post = $use_this_post;
setup_postdata($post);
}
2009-11-08 01:56:49 +00:00
}
2009-11-08 03:00:14 +00:00
/**
* Temporarily restore the global $post variable and set it up for use.
*/
function Restore() {
global $post, $__post;
$post = $__post;
setup_postdata($post);
}
2009-11-08 01:56:49 +00:00
/**
* Restore global $post and $wp_query.
*/
2009-11-08 03:00:14 +00:00
function Unprotect() {
2009-11-08 01:56:49 +00:00
global $post, $wp_query, $__post, $__wp_query;
$post = $__post;
$wp_query = $__wp_query;
$__post = $__wp_query = null;
}
2009-11-19 12:27:10 +00:00
/**
* Call a function on the backend specified by the provided attachment info.
* @param array $attachment_info The post attachment info from M().
* @param string $which The subattachment short name to reference.
* @param string $action The method to call on the backend.
* @return object The result from the backend.
*/
2009-11-16 17:35:48 +00:00
function EM($attachment_info, $which = 'default', $action = 'embed') {
if (substr($action, 0, 1) != '_') {
$args = func_get_args();
if (isset($attachment_info[$which])) {
if (($attachment = ComicPressBackend::generate_from_id($attachment_info[$which])) !== false) {
if (method_exists($attachment, $action)) {
return call_user_func_array(array($attachment, $action), array_merge(array($which), array_slice($args, 3)));
}
switch ($action) {
case 'object': return $attachment;
}
}
}
}
}
2009-11-19 12:27:10 +00:00
/**
* Return the raw storyline structure.
* @return array The astoryline structure from ComicPressStoryline
*/
function SL() {
$storyline = new ComicPressStoryline();
$storyline->read_from_options();
return $storyline->_structure;
2009-11-19 01:03:14 +00:00
}
2009-11-19 12:27:10 +00:00
/**
* Get a category relative to the provided category.
* If no category is provided, use the current post's category.
* Relative measures can be one of: current, previous, lext, level, parent
* If no relative measure is provided, the current category is returned.
* @param string $which The relative measure to use.
* @param object $relative_to The post object to be relative to.
* @return object|false The relative category object, or false if not found.
*/
function SC($which = 'current', $relative_to = null) {
2009-11-19 01:03:14 +00:00
global $post;
$storyline = new ComicPressStoryline();
$storyline->read_from_options();
if (is_null($relative_to)) {
if (is_object($post)) {
if (isset($post->ID)) {
$categories = wp_get_post_categories($post->ID);
if (is_array($categories)) {
$relative_to = reset($categories);
}
}
}
}
if (!is_null($relative_to)) {
if ($which == 'current') {
$result = $relative_to;
} else {
$result = $storyline->_get_field($which, $relative_to);
}
if ($result !== false) {
2009-11-19 01:03:14 +00:00
$category = get_category($result);
2009-11-19 12:07:04 +00:00
// sanity check
// @codeCoverageIgnoreStart
2009-11-19 01:03:14 +00:00
if (!empty($category)) {
return $category;
}
}
2009-11-19 12:07:04 +00:00
// @codeCoverageIgnoreEnd
2009-11-19 01:03:14 +00:00
}
return false;
2009-11-19 02:25:35 +00:00
}