working on new backend code
This commit is contained in:
parent
ead478fcec
commit
4cfacec35f
@ -13,9 +13,16 @@ class ComicPress {
|
||||
'mini_dimensions' => '100x',
|
||||
'helpers' => array(),
|
||||
'addons' => array(),
|
||||
'storyline_order' => ''
|
||||
'storyline_order' => '',
|
||||
'subattachment_types' => array(
|
||||
'rss' => 'RSS',
|
||||
'archive' => 'Archive',
|
||||
'mini' => 'Mini Thumb'
|
||||
)
|
||||
);
|
||||
|
||||
var $backends = array('ComicPressBackendAttachment');
|
||||
|
||||
function &get_instance() {
|
||||
static $instance;
|
||||
|
||||
@ -58,6 +65,7 @@ class ComicPress {
|
||||
}
|
||||
|
||||
add_filter('intermediate_image_sizes', array(&$this, 'intermediate_image_sizes'));
|
||||
add_filter('editor_max_image_size', array(&$this, 'editor_max_image_size'), 10, 2);
|
||||
|
||||
foreach (array('comic', 'rss', 'archive', 'mini') as $size) {
|
||||
list($w, $h) = explode("x", $this->comicpress_options["${size}_dimensions"]);
|
||||
@ -71,6 +79,14 @@ class ComicPress {
|
||||
return array_merge($sizes, array('comic', 'rss', 'archive', 'mini'));
|
||||
}
|
||||
|
||||
function editor_max_image_size($current_max, $size) {
|
||||
if (isset($this->comicpress_options["${size}_dimensions"])) {
|
||||
list($width, $height) = explode('x', $this->comicpress_options["${size}_dimensions"]);
|
||||
$current_max = array(intval($width), intval($height));
|
||||
}
|
||||
return $current_max;
|
||||
}
|
||||
|
||||
function announce_activated_helpers() {
|
||||
echo "<center>[ <strong>Activated ComicPress helpers:</strong> " . implode(", ", array_keys($this->comicpress_options['helpers'])) . " ]</center>";
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ class ComicPressAdmin {
|
||||
wp_enqueue_script('cp-admin', get_template_directory_uri() . '/js/Storyline.js', array('prototype', 'scriptaculous'));
|
||||
}
|
||||
|
||||
if (strpos($pagenow, "media-upload") === 0) {
|
||||
if (strpos($pagenow, "-upload") !== false) {
|
||||
wp_enqueue_script('cp-media', get_template_directory_uri() . '/js/MediaUpload.js', array('prototype'));
|
||||
}
|
||||
}
|
||||
@ -176,28 +176,26 @@ class ComicPressAdmin {
|
||||
$comic_post = new ComicPressComicPost(get_post($uploading_iframe_ID));
|
||||
$ordering = $comic_post->normalize_comic_image_ordering();
|
||||
|
||||
if (is_array($ordering)) {
|
||||
$nonce = wp_create_nonce('comicpress');
|
||||
$zoom_level = 40;
|
||||
$current_user = wp_get_current_user();
|
||||
if (!empty($current_user)) {
|
||||
$comicpress_meta = get_usermeta($current_user->ID, 'comicpress-settings');
|
||||
if (is_array($comicpress_meta)) {
|
||||
if (isset($comicpress_meta['zoom_level'])) {
|
||||
$zoom_level = floor($comicpress_meta['zoom_level']);
|
||||
}
|
||||
$nonce = wp_create_nonce('comicpress');
|
||||
$zoom_level = 40;
|
||||
$current_user = wp_get_current_user();
|
||||
if (!empty($current_user)) {
|
||||
$comicpress_meta = get_usermeta($current_user->ID, 'comicpress-settings');
|
||||
if (is_array($comicpress_meta)) {
|
||||
if (isset($comicpress_meta['zoom_level'])) {
|
||||
$zoom_level = floor($comicpress_meta['zoom_level']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// from wp-admin/includes/media.php O_o
|
||||
$media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID";
|
||||
$image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&type=image&TB_iframe=true");
|
||||
// from wp-admin/includes/media.php O_o
|
||||
$media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID";
|
||||
$image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&type=image&TB_iframe=true");
|
||||
|
||||
if ($is_ajax === true) {
|
||||
include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc');
|
||||
} else {
|
||||
include(dirname(__FILE__) . '/partials/_comic-image-ordering.inc');
|
||||
}
|
||||
if ($is_ajax === true) {
|
||||
include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc');
|
||||
} else {
|
||||
include(dirname(__FILE__) . '/partials/_comic-image-ordering.inc');
|
||||
}
|
||||
}
|
||||
|
||||
|
17
classes/ComicPressBackend.inc
Normal file
17
classes/ComicPressBackend.inc
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
class ComicPressBackend {
|
||||
function _embed_image($size) {
|
||||
$extras = array();
|
||||
if (($dims = $this->dims($size)) !== false) {
|
||||
$extras = array_merge($extras, $dims);
|
||||
}
|
||||
foreach ($extras as $field => $value) {
|
||||
$extras[] = "${field}=\"${value}\"";
|
||||
unset($extras[$field]);
|
||||
}
|
||||
|
||||
$output = sprintf('<img src="%s" alt="%s" title="%s" %s />', $this->url(), $this->alt(), $this->title(), implode(" ", $extras));
|
||||
return apply_filters('comicpress_embed_image', $output, $this);
|
||||
}
|
||||
}
|
@ -8,17 +8,15 @@ class ComicPressComicPost {
|
||||
if (!is_null($post)) { $this->post = $post; }
|
||||
}
|
||||
|
||||
function get_comic_image_attachments() {
|
||||
if (is_null($this->attachments)) {
|
||||
$this->attachments = get_children(array(
|
||||
'post_parent' => $this->post->ID,
|
||||
'post_type' => 'attachment',
|
||||
'post_mime_type' => 'image'
|
||||
));
|
||||
}
|
||||
return $this->attachments;
|
||||
function get_attachments() {
|
||||
$comicpress = ComicPress::get_instance();
|
||||
$attachments = array();
|
||||
foreach ($comicpress->backends as $backend) {
|
||||
$attachments = array_merge($attachments, call_user_func(array($backend, 'generate_from_post'), $this->post));
|
||||
}
|
||||
return $attachments;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Display all the attached images.
|
||||
*/
|
||||
@ -33,19 +31,19 @@ class ComicPressComicPost {
|
||||
if (get_post_meta($attachment_id, "comic_image_type", true) == $type) {
|
||||
$attachment = get_post($attachment_id);
|
||||
$title = (!empty($attachment->post_excerpt) ? $attachment->post_excerpt : $attachment->post_title);
|
||||
|
||||
|
||||
$url = wp_get_attachment_url($attachment->ID, '');
|
||||
$sizes = image_downsize($attachment->ID, $size_type);
|
||||
if ($sizes) {
|
||||
$url = $sizes[0];
|
||||
}
|
||||
|
||||
$output[] = apply_filters('comicpress_attached_image',
|
||||
|
||||
$output[] = apply_filters('comicpress_attached_image',
|
||||
sprintf($format, $this->get_comic_img_tag($url, $size_type, array('title' => $title))),
|
||||
$attachment_id,
|
||||
$i++);
|
||||
$found = true;
|
||||
|
||||
|
||||
if (!is_null($limit)) {
|
||||
if (--$limit == 0) { break; }
|
||||
}
|
||||
@ -58,7 +56,7 @@ class ComicPressComicPost {
|
||||
|
||||
return $found;
|
||||
}
|
||||
|
||||
|
||||
function _display_type($types, $format, $single = false) {
|
||||
$target_type = reset($types);
|
||||
foreach ($types as $type) {
|
||||
@ -67,7 +65,7 @@ class ComicPressComicPost {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function display_comics($format) { $this->_display_type(array('comic'), $format); }
|
||||
function display_archive($format) { $this->_display_type(array('archive'. 'comic'), $format, true); }
|
||||
function display_rss($format) { $this->_display_type(array('rss'. 'comic'), $format); }
|
||||
@ -77,7 +75,7 @@ class ComicPressComicPost {
|
||||
*/
|
||||
function get_comic_img_tag($url, $type, $additional_parameters = array()) {
|
||||
$dimensions = array();
|
||||
|
||||
|
||||
if (isset($this->comicpress->comicpress_options["${type}_dimensions"])) {
|
||||
$parts = explode("x", $this->comicpress->comicpress_options["${type}_dimensions"]);
|
||||
switch (count($parts)) {
|
||||
@ -86,7 +84,7 @@ class ComicPressComicPost {
|
||||
}
|
||||
$dimensions = compact('width', 'height');
|
||||
}
|
||||
|
||||
|
||||
$output = '<img src="' . $url . '" ';
|
||||
foreach (array('width', 'height') as $field) {
|
||||
if (!empty($dimensions[$field])) {
|
||||
@ -95,11 +93,11 @@ class ComicPressComicPost {
|
||||
}
|
||||
if (is_array($additional_parameters)) {
|
||||
foreach ($additional_parameters as $parameter => $value) {
|
||||
$output .= $parameter . '="' . $value . '" ';
|
||||
$output .= $parameter . '="' . $value . '" ';
|
||||
}
|
||||
}
|
||||
$output .= "/>";
|
||||
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
@ -107,62 +105,58 @@ class ComicPressComicPost {
|
||||
* Normalize the ordering of comic images in this post.
|
||||
* If images have beed added or removed, intelligently update the metadata.
|
||||
*/
|
||||
function normalize_comic_image_ordering() {
|
||||
if (is_array($this->get_comic_image_attachments())) {
|
||||
$ordering_by_type = array();
|
||||
$ordering_types = get_post_meta($this->post->ID, 'comic_ordering', true);
|
||||
|
||||
$comic_image_ordering = array();
|
||||
$found_post_ids = array();
|
||||
if (!empty($ordering_types)) {
|
||||
foreach ($ordering_types as $type => $post_ids) {
|
||||
$comic_image_ordering[$type] = array();
|
||||
foreach ($post_ids as $ordering_post_id) {
|
||||
foreach ($this->get_comic_image_attachments() as $attachment) {
|
||||
if (!isset($found_post_ids[$attachment->ID])) {
|
||||
if ($attachment->ID == $ordering_post_id) {
|
||||
$comic_image_ordering[$type][] = $attachment->ID;
|
||||
$found_post_ids[$ordering_post_id] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function normalize_ordering() {
|
||||
$attachments = $this->get_attachments();
|
||||
if (is_array($attachments)) {
|
||||
$new_ordering = array();
|
||||
$current_ordering = get_post_meta($this->post->ID, 'image-ordering', true);
|
||||
if (!is_array($current_ordering)) { $current_ordering = array(); }
|
||||
|
||||
$all_current_ids = array();
|
||||
foreach ($current_ordering as $key => $children) {
|
||||
$all_current_ids[$key] = true;
|
||||
if (is_array($children)) {
|
||||
foreach ($children as $type => $kids) {
|
||||
$all_current_ids = array_merge($all_current_ids, $kids);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$remaining_posts_to_sort = array();
|
||||
foreach ($this->get_comic_image_attachments() as $attachment) {
|
||||
$comic_image_type = get_post_meta($attachment->ID, 'comic_image_type', true);
|
||||
|
||||
if (!empty($comic_image_type)) {
|
||||
if (!isset($found_post_ids[$attachment->ID])) {
|
||||
if (!isset($remaining_posts_to_sort[$comic_image_type])) {
|
||||
$remaining_posts_to_sort[$comic_image_type] = array();
|
||||
}
|
||||
$remaining_posts_to_sort[$comic_image_type][] = $attachment->ID;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($remaining_posts_to_sort as $type => $posts) {
|
||||
usort($remaining_posts_to_sort[$type], array(&$this, 'sort_remaining_comic_images'));
|
||||
}
|
||||
|
||||
foreach ($remaining_posts_to_sort as $type => $posts) {
|
||||
if (!isset($comic_image_ordering[$type])) { $comic_image_ordering[$type] = array(); }
|
||||
if (is_array($comic_image_ordering[$type])) {
|
||||
$comic_image_ordering[$type] = array_merge($comic_image_ordering[$type], $posts);
|
||||
} else {
|
||||
$comic_image_ordering[$type] = $posts;
|
||||
}
|
||||
$all_current_ids = array_keys($all_current_ids);
|
||||
|
||||
$attachment_ids = array();
|
||||
foreach ($attachments as $attachment) { $attachment_ids[] = $attachment->id; }
|
||||
|
||||
$new_attachments = array_diff($attachment_ids, $all_current_ids);
|
||||
$missing_attachments = array_diff($all_current_ids, $attachment_ids);
|
||||
|
||||
foreach ($new_attachments as $attachment_id) {
|
||||
$current_ordering[$attachment_id] = true;
|
||||
}
|
||||
|
||||
update_post_meta($this->post->ID, 'comic_ordering', $comic_image_ordering);
|
||||
return $comic_image_ordering;
|
||||
foreach ($missing_attachments as $attachment_id) {
|
||||
if (isset($current_ordering[$attachment_id])) {
|
||||
unset($current_ordering[$attachment_id]);
|
||||
} else {
|
||||
foreach ($current_ordering as $key => $children) {
|
||||
foreach ($children as $type => $kids) {
|
||||
if (isset($kids[$attachment_id])) { unset($kids[$attachment_id]); }
|
||||
$children[$type] = $kids;
|
||||
}
|
||||
if (empty($children[$type])) {
|
||||
$current_ordering[$key] = true;
|
||||
} else {
|
||||
$current_ordering[$key] = $children;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
update_post_meta($this->post->ID, 'image-ordering', $current_ordering);
|
||||
return $current_ordering;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sort the remaining comic images by file date.
|
||||
* @param object $a
|
||||
@ -172,7 +166,7 @@ class ComicPressComicPost {
|
||||
function sort_remaining_comic_images($a, $b) {
|
||||
$a_date = isset($a->post_date) ? $a->post_date : 0;
|
||||
$b_date = isset($b->post_date) ? $b->post_date : 0;
|
||||
return $a_date - $b_date;
|
||||
return $a_date - $b_date;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -182,7 +176,7 @@ class ComicPressComicPost {
|
||||
function change_comic_image_ordering($requested_new_order) {
|
||||
$orderings = get_post_meta($this->post->ID, 'comic_ordering', true);
|
||||
if (!is_array($orderings)) { $orderings = array(); }
|
||||
|
||||
|
||||
$new_order = array();
|
||||
|
||||
$requested_new_order = (array)$requested_new_order;
|
||||
@ -193,7 +187,7 @@ class ComicPressComicPost {
|
||||
$position = 0;
|
||||
foreach ($requested_new_order[$type] as $id) {
|
||||
if (!isset($sort_by_position[$position])) {
|
||||
$sort_by_position[$position] = array();
|
||||
$sort_by_position[$position] = array();
|
||||
}
|
||||
$sort_by_position[$position][] = $id;
|
||||
$position++;
|
||||
@ -213,7 +207,7 @@ class ComicPressComicPost {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
update_post_meta($this->post->ID, 'comic_ordering', $new_order);
|
||||
}
|
||||
|
||||
@ -228,16 +222,16 @@ class ComicPressComicPost {
|
||||
$category_parent = $category->parent;
|
||||
if ($category_parent != 0) {
|
||||
$post_categories[] = $category_parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while ($category_parent != 0);
|
||||
|
||||
|
||||
foreach ($post_categories as $category_id) {
|
||||
$category = get_category($category_id);
|
||||
$parents[$category_id] = $category->slug;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $parents;
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,64 @@
|
||||
<?php
|
||||
|
||||
class ComicPressBackendAttachment {
|
||||
function ComicPressBackendAttachment($attachment_id) {
|
||||
$this->attachment_id = $attachment_id;
|
||||
require_once(dirname(__FILE__) . '/../ComicPressBackend.inc');
|
||||
|
||||
class ComicPressBackendAttachment extends ComicPressBackend {
|
||||
var $root_id = 'attachment';
|
||||
|
||||
function generate_from_post($post) {
|
||||
$result = array();
|
||||
if (is_object($post)) {
|
||||
if (isset($post->ID)) {
|
||||
$children = get_children(array(
|
||||
'post_parent' => $post->ID,
|
||||
'post_type' => 'attachment',
|
||||
'post_mime_type' => 'image'
|
||||
));
|
||||
|
||||
if (!empty($children)) {
|
||||
foreach ($children as $child) {
|
||||
$result[] = new ComicPressBackendAttachment($child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
function ComicPressBackendAttachment($attachment) {
|
||||
$this->attachment = $attachment;
|
||||
$this->id = sprintf('%s-%d', $this->root_id, $this->attachment->ID);
|
||||
$this->type = get_post_meta($this->attachment->ID, 'comic_image_type', true);
|
||||
}
|
||||
|
||||
function get_url() {
|
||||
return wp_get_attachment_url($this->attachment_id);
|
||||
function dims($size = 'comic') {
|
||||
$metadata = image_downsize($this->attachment->ID, $size);
|
||||
if (!empty($metadata)) {
|
||||
if (is_array($metadata)) {
|
||||
return array_combine(array('width', 'height'), array_slice($metadata, 1, 2));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function get_info() {
|
||||
return wp_get_attachment_metadata($this->attachment_id);
|
||||
function url($size = 'comic') {
|
||||
$metadata = image_downsize($this->attachment->ID, $size);
|
||||
if (!empty($metadata)) {
|
||||
if (is_array($metadata)) {
|
||||
return $metadata[0];
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function embed($size = 'comic') {
|
||||
return $this->_embed_image($size);
|
||||
}
|
||||
|
||||
function alt() { return $this->attachment->post_title; }
|
||||
function title() { return $this->attachment->post_excerpt; }
|
||||
|
||||
function get_info() { return array(); }
|
||||
}
|
||||
|
||||
?>
|
@ -9,29 +9,31 @@
|
||||
<?php _e('Click the Refesh button underneath the zoom slider if you\'ve changed the images attached to this post.', 'comicpress') ?>
|
||||
|
||||
</em></p>
|
||||
<?php foreach ($ordering as $type => $attachment_ids) { ?>
|
||||
<h3><?php echo $this->comic_image_types[$type] ?></h3>
|
||||
<div class="comic-ordering" id="comic-ordering-<?php echo $type ?>">
|
||||
<?php foreach ($attachment_ids as $attachment_id) {
|
||||
$backend = new ComicPressBackendAttachment($attachment_id);
|
||||
$info = $backend->get_info(); ?>
|
||||
<div class="cp-comic-attachment" id="attachment_<?php echo $attachment_id ?>">
|
||||
<img src="<?php echo $backend->get_url() ?>" border="0" height="<?php echo $zoom_level ?>" />
|
||||
<div>
|
||||
<?php if (isset($info['file'])) { ?>
|
||||
<p><strong><?php echo basename($info['file']) ?></strong></p>
|
||||
<?php } ?>
|
||||
<?php if (isset($info['width']) && isset($info['height'])) { ?>
|
||||
<p>
|
||||
<?php _e('Size:', 'comicpress') ?>
|
||||
<?php printf('%dx%d', $info['width'], $info['height'] ) ?>
|
||||
</p>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<br style="clear: both" />
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php if (!empty($ordering)) { ?>
|
||||
<?php foreach ($ordering as $type => $attachment_ids) { ?>
|
||||
<h3><?php echo $this->comic_image_types[$type] ?></h3>
|
||||
<div class="comic-ordering" id="comic-ordering-<?php echo $type ?>">
|
||||
<?php foreach ($attachment_ids as $attachment_id) {
|
||||
$backend = new ComicPressBackendAttachment($attachment_id);
|
||||
$info = $backend->get_info(); ?>
|
||||
<div class="cp-comic-attachment" id="attachment_<?php echo $attachment_id ?>">
|
||||
<img src="<?php echo $backend->url() ?>" border="0" height="<?php echo $zoom_level ?>" />
|
||||
<div>
|
||||
<?php if (isset($info['file'])) { ?>
|
||||
<p><strong><?php echo basename($info['file']) ?></strong></p>
|
||||
<?php } ?>
|
||||
<?php if (isset($info['width']) && isset($info['height'])) { ?>
|
||||
<p>
|
||||
<?php _e('Size:', 'comicpress') ?>
|
||||
<?php printf('%dx%d', $info['width'], $info['height'] ) ?>
|
||||
</p>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<br style="clear: both" />
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<?php } ?>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
@ -144,6 +144,14 @@ function RT($which, $restrictions = null, $override_post = null) {
|
||||
return $post;
|
||||
}
|
||||
|
||||
function M($override_post = null) {
|
||||
global $post;
|
||||
$post_to_use = !is_null($override_post) ? $override_post : $post;
|
||||
|
||||
$comic_post = new ComicPressComicPost($post_to_use);
|
||||
return $comic_post->get_attachments();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the list of Storyline categories.
|
||||
*/
|
||||
|
@ -11,6 +11,11 @@
|
||||
RT('previous', array('root_of' => '__post')); the_title(); echo '<br />';
|
||||
RT('previous'); the_title(); echo '<br />';
|
||||
Restore(); the_title(); echo '<br />';
|
||||
|
||||
foreach (M() as $image) {
|
||||
echo $image->embed();
|
||||
}
|
||||
|
||||
RT('next'); the_title(); echo '<br />';
|
||||
RT('next', array('root_of' => '__post')); the_title(); echo '<br />';
|
||||
RT('next', 'from_post'); the_title(); echo '<br />';
|
||||
|
@ -20,7 +20,7 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @dataProvider providerTestGenerateComicTag
|
||||
*/
|
||||
@ -30,7 +30,7 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
|
||||
'comic_dimensions' => $dimensions
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
$source = $this->p->get_comic_img_tag("test.gif", "comic");
|
||||
|
||||
if (count($parts = explode("x", $dimensions)) == 2) {
|
||||
@ -46,83 +46,54 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function testNormalizeComicImageOrdering() {
|
||||
$p = $this->getMock('ComicPressComicPost', array('get_comic_image_attachments'));
|
||||
|
||||
$comic_attachments = array(
|
||||
array(
|
||||
'ID' => 2,
|
||||
'post_parent' => 1,
|
||||
'post_title' => 'Comic one',
|
||||
'post_meta' => array(
|
||||
'comic_image_type' => 'comic'
|
||||
),
|
||||
'post_date' => 1
|
||||
),
|
||||
array(
|
||||
'ID' => 3,
|
||||
'post_parent' => 1,
|
||||
'post_title' => 'Comic two',
|
||||
'post_meta' => array(
|
||||
'comic_image_type' => 'comic'
|
||||
),
|
||||
'post_date' => 2
|
||||
),
|
||||
array(
|
||||
'ID' => 4,
|
||||
'post_parent' => 1,
|
||||
'post_title' => 'Comic three',
|
||||
'post_meta' => array(
|
||||
'comic_image_type' => 'rss'
|
||||
),
|
||||
'post_date' => 4
|
||||
),
|
||||
array(
|
||||
'ID' => 5,
|
||||
'post_parent' => 1,
|
||||
'post_title' => 'Comic four',
|
||||
'post_meta' => array(
|
||||
'comic_image_type' => 'rss'
|
||||
),
|
||||
'post_date' => 3
|
||||
),
|
||||
);
|
||||
|
||||
$attachments = array();
|
||||
foreach ($comic_attachments as $attachment_info) {
|
||||
$attachment = (object)array();
|
||||
foreach ($attachment_info as $field => $value) {
|
||||
switch ($field) {
|
||||
case "post_meta":
|
||||
foreach ($value as $meta => $meta_value) {
|
||||
update_post_meta($attachment_info['ID'], $meta, $meta_value);
|
||||
}
|
||||
break;
|
||||
case "post_date":
|
||||
$attachment->{$field} = date("r", $value);
|
||||
break;
|
||||
default:
|
||||
$attachment->{$field} = $value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$attachments[] = $attachment;
|
||||
}
|
||||
|
||||
$p->expects($this->any())->method('get_comic_image_attachments')->will($this->returnValue($attachments));
|
||||
|
||||
wp_insert_post((object)array('ID' => 1));
|
||||
update_post_meta(1, 'comic_ordering', array('comic' => array(3)));
|
||||
|
||||
$p->post = (object)array('ID' => 1);
|
||||
|
||||
$result = $p->normalize_comic_image_ordering();
|
||||
|
||||
$this->assertEquals(array('comic' => array(3,2), 'rss' => array(5,4)), $result);
|
||||
$this->assertEquals(array('comic' => array(3,2), 'rss' => array(5,4)), get_post_meta(1, 'comic_ordering', true));
|
||||
|
||||
function providerTestNormalizeOrdering() {
|
||||
return array(
|
||||
array(
|
||||
array('attachment-1'),
|
||||
array(),
|
||||
array('attachment-1' => true)
|
||||
),
|
||||
array(
|
||||
array('attachment-1'),
|
||||
array('attachment-1' => true, 'attachment-2' => true),
|
||||
array('attachment-1' => true)
|
||||
),
|
||||
array(
|
||||
array('attachment-1'),
|
||||
array('attachment-1' => array('rss' => array('attachment-2' => true))),
|
||||
array('attachment-1' => true)
|
||||
),
|
||||
array(
|
||||
array('attachment-1', 'attachment-2', 'attachment-3'),
|
||||
array('attachment-1' => array('rss' => array('attachment-2' => true))),
|
||||
array('attachment-1' => array('rss' => array('attachment-2' => true)), 'attachment-3' => true)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @dataProvider providerTestNormalizeOrdering
|
||||
*/
|
||||
function testNormalizeOrdering($attachments, $current_meta, $expected_result) {
|
||||
$p = $this->getMock('ComicPressComicPost', array('get_attachments'));
|
||||
|
||||
$attachment_objects = array();
|
||||
foreach ($attachments as $attachment) {
|
||||
$attachment_objects[] = (object)array('id' => $attachment);
|
||||
}
|
||||
|
||||
$p->expects($this->any())->method('get_attachments')->will($this->returnValue($attachment_objects));
|
||||
|
||||
wp_insert_post((object)array('ID' => 1));
|
||||
update_post_meta(1, 'image-ordering', $current_meta);
|
||||
|
||||
$p->post = (object)array('ID' => 1);
|
||||
|
||||
$this->assertEquals($expected_result, $p->normalize_ordering());
|
||||
$this->assertEquals($expected_result, get_post_meta(1, 'image-ordering', true));
|
||||
}
|
||||
|
||||
function providerTestChangeComicImageOrdering() {
|
||||
return array(
|
||||
array(
|
||||
@ -148,17 +119,17 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @dataProvider providerTestChangeComicImageOrdering
|
||||
* @covers ComicPressComicPost::change_comic_image_ordering
|
||||
*/
|
||||
function testChangeComicImageOrdering($current_ordering, $revised_ordering, $expected_result) {
|
||||
update_post_meta(1, 'comic_ordering', $current_ordering);
|
||||
|
||||
|
||||
$this->p->post = (object)array('ID' => 1);
|
||||
$this->p->change_comic_image_ordering($revised_ordering);
|
||||
|
||||
|
||||
$this->assertEquals($expected_result, get_post_meta(1, 'comic_ordering', true));
|
||||
}
|
||||
|
||||
@ -190,7 +161,7 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @dataProvider providerTestFindParents
|
||||
*/
|
||||
@ -199,11 +170,11 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
|
||||
add_category(2, (object)array('slug' => 'comic', 'parent' => 1));
|
||||
add_category(3, (object)array('slug' => 'part-1', 'parent' => 2));
|
||||
add_category(4, (object)array('slug' => 'blog', 'parent' => 1));
|
||||
|
||||
|
||||
wp_set_post_categories(1, $post_categories);
|
||||
|
||||
|
||||
$this->p->post = (object)array('ID' => 1);
|
||||
|
||||
|
||||
$this->assertEquals($expected_result, $this->p->find_parents());
|
||||
}
|
||||
}
|
||||
|
39
test/backends/ComicPressBackendAttachmentTest.php
Normal file
39
test/backends/ComicPressBackendAttachmentTest.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
require_once('PHPUnit/Framework.php');
|
||||
require_once('MockPress/mockpress.php');
|
||||
require_once('backends/ComicPressBackendAttachment.inc');
|
||||
|
||||
class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase {
|
||||
function setUp() {
|
||||
_reset_wp();
|
||||
}
|
||||
|
||||
function providerTestGenerateFromPost() {
|
||||
return array(
|
||||
array(array(), false),
|
||||
array(array((object)array('ID' => 1)), array('attachment-1'))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerTestGenerateFromPost
|
||||
*/
|
||||
function testGenerateFromPost($get_children_response, $expected_ids) {
|
||||
_set_get_children(array(
|
||||
'post_parent' => 1,
|
||||
'post_type' => 'attachment',
|
||||
'post_mime_type' => 'image'
|
||||
), $get_children_response);
|
||||
|
||||
$results = ComicPressBackendAttachment::generate_from_post((object)array('ID' => 1));
|
||||
if ($expected_ids === false) {
|
||||
$this->assertTrue(empty($results));
|
||||
} else {
|
||||
$this->assertEquals(count($expected_ids), count($results));
|
||||
foreach ($results as $result) {
|
||||
$this->assertTrue(in_array($result->id, $expected_ids));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user