a crapload of changes

This commit is contained in:
John Bintz 2009-11-16 12:35:48 -05:00
parent c7233f0395
commit ef82b9c861
13 changed files with 291 additions and 127 deletions

View File

@ -95,18 +95,20 @@ class ComicPress {
}
function editor_max_image_size($current_max, $size) {
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));
}
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));
}
}
}
return $current_max;
}
function normalize_image_size_options() {
$protected_options = apply_filters('comicpress_protected_image_size_options', array('thumbnail', 'medium', 'large'));
foreach (get_alloptions() as $option => $value) {
foreach (wp_load_alloptions() as $option => $value) {
if (strpos($option, '_size_w') !== false) {
$size = str_replace('_size_w', '', $option);
if (!in_array($size, $protected_options)) {
@ -121,6 +123,9 @@ class ComicPress {
foreach ($this->comicpress_options['image_types'] as $type => $info) {
if (isset($info['dimensions'])) {
list($width, $height) = explode('x', $info['dimensions']);
foreach (array('_size_w', '_size_h', '_crop') as $suffix) {
delete_option("${type}${suffix}");
}
update_option("${type}_size_w", intval($width));
update_option("${type}_size_h", intval($height));
update_option("${type}_crop", 0);
@ -137,21 +142,10 @@ class ComicPress {
function init() {
$this->load();
if (current_user_can('edit_themes')) {
if (!empty($this->comicpress_options['helpers'])) {
add_action('wp_footer', array(&$this, 'announce_activated_helpers'));
}
}
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"]);
update_option("${size}_size_w", $w);
update_option("${size}_size_h", $h);
update_option("${size}_crop", 0);
}
$this->normalize_image_size_options();
foreach (get_declared_classes() as $class) {
if (preg_match('#^ComicPressBackend.+$#', $class) > 0) {
@ -232,6 +226,19 @@ class ComicPress {
}
return false;
}
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) {
if ($properties['default'] === true) {
return $type;
}
}
}
}
return false;
}
}
?>

View File

@ -103,13 +103,17 @@ class ComicPressAdmin {
function setup_comic_metadata_buttons($form_fields, $post) {
global $pagenow;
$comicpress_management = get_post_meta($post->ID, 'comicpress_management', true);
$comicpress_info = get_post_meta($post->ID, 'comicpress', true);
$is_managed = false;
if (isset($comicpress_info['managed'])) {
$is_managed = $comicpress_info['managed'];
}
if ($pagenow !== "media.php") {
$form_fields['auto_attach'] = array(
'label' => __("Let ComicPress Manage?", 'comicpress'),
'input' => 'html',
'html' => '<label><input type="checkbox" name="attachments[' . $post->ID . '][comicpress_management]" value="yes" ' . ($comicpress_management ? 'checked="checked"' : '') . '/> '
'html' => '<label><input type="checkbox" name="attachments[' . $post->ID . '][comicpress_management]" value="yes" ' . ($is_managed ? 'checked="checked"' : '') . '/> '
. __('Let ComicPress treat this image as a comic media file', 'comicpress')
. '</label>'
. '<input type="hidden" name="cp[_nonce]" value="' . wp_create_nonce('comicpress') . '" />'
@ -250,8 +254,8 @@ class ComicPressAdmin {
}
}
update_post_meta($post_id, 'comicpress', array(
'managed' => isset($settings['comicpress_management']
)));
'managed' => isset($settings['comicpress_management'])
));
}
}
@ -290,6 +294,7 @@ class ComicPressAdmin {
$this->comicpress->comicpress_options['image_types'][$new_type] = $new_value;
}
}
$this->comicpress->normalize_image_size_options();
}
if (is_null($defined_default)) {
$defined_default = array_keys($this->comicpress->comicpress_options['image_types']);

View File

@ -11,7 +11,9 @@ class ComicPressBackend {
}
foreach ($extras as $field => $value) {
$extras[] = "${field}=\"${value}\" ";
if (!empty($value)) {
$extras[] = "${field}=\"${value}\" ";
}
unset($extras[$field]);
}

View File

@ -9,7 +9,6 @@ class ComicPressComicPost {
}
/**
* TODO normalize the attachments
* @return array The raw list of attached files for this post.
*/
function get_attachments() {
@ -164,16 +163,33 @@ class ComicPressComicPost {
* @return array The simplified, normalized attachments.
*/
function get_attachments_with_children($skip_checks = false) {
$attachments_with_children = array();
foreach ($this->normalize_ordering($skip_checks) as $id => $info) {
if ($info['enabled']) {
$attachment = array('id' => $id);
if (isset($info['children'])) {
$attachment['children'] = $info['children'];
$comicpress = ComicPress::get_instance();
$default_type = $comicpress->get_default_image_type();
$normalized_children = $this->normalize_ordering($skip_checks);
$remove_from_list = array();
foreach ($normalized_children as $id => $info) {
if (isset($info['children'])) {
$remove_from_list = array_merge($remove_from_list, array_values($info['children']));
}
}
$attachments_with_children = array();
foreach ($normalized_children as $id => $info) {
if (!in_array($id, $remove_from_list)) {
if ($info['enabled']) {
$attachment = array('default' => $id);
if (is_string($default_type)) { $attachment[$default_type] = $id; }
if (isset($info['children'])) {
$attachment = array_merge($attachment, $info['children']);
$remove_from_list = array_merge($remove_from_list, array_values($info['children']));
}
$attachments_with_children[] = $attachment;
}
$attachments_with_children[] = $attachment;
}
}
return $attachments_with_children;
}
}

View File

@ -11,84 +11,94 @@ class ComicPressBackendAttachment extends ComicPressBackend {
if (is_object($post)) {
if (isset($post->ID)) {
$children = get_children(array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
if (!empty($children)) {
foreach ($children as $child) {
$meta = get_post_meta($child->ID, 'comicpress', true);
if (isset($meta['managed'])) {
if ($meta['managed']) {
$result[] = new ComicPressBackendAttachment($child);
}
}
}
}
if (!empty($children)) {
foreach ($children as $child) {
$meta = get_post_meta($child->ID, 'comicpress', true);
if (isset($meta['managed'])) {
if ($meta['managed']) {
$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 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 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 dims($size = 'comic') {
$comicpress = ComicPress::get_instance();
function url($size = 'comic') {
$metadata = image_downsize($this->attachment->ID, $size);
if (!empty($metadata)) {
if (is_array($metadata)) {
return $metadata[0];
}
}
return false;
}
$dims = array();
if (isset($comicpress->comicpress_options['image_types'])) {
if (isset($comicpress->comicpress_options['image_types'][$size])) {
if (isset($comicpress->comicpress_options['image_types'][$size]['dimensions'])) {
$dims = array_combine(array('width', 'height'), explode("x", $comicpress->comicpress_options['image_types'][$size]['dimensions']));
}
}
}
// @codeCoverageIgnoreStart
function embed($size = 'comic') { return $this->_embed_image($size); }
function file($size = 'comic') { return $this->attachment->guid; }
function alt() { return $this->attachment->post_title; }
function title() { return $this->attachment->post_excerpt; }
// @codeCoverageIgnoreEnd
return $dims;
}
function get_info($size = 'comic') {
$info = array();
foreach (array('dims', 'url', 'file') as $field) {
$result = $this->{$field}($size);
if (is_array($result)) {
$info = array_merge($info, $result);
} else {
$info[$field] = $result;
}
}
return $info;
}
function url($size = 'comic') {
$metadata = image_downsize($this->attachment->ID, $size);
if (!empty($metadata)) {
if (is_array($metadata)) {
return $metadata[0];
}
}
return false;
}
function generate_from_id($id) {
if (strpos($id, 'attachment-') === 0) {
$id = str_replace('attachment-', '', $id);
if (is_numeric($id)) {
$post = get_post($id);
if (!empty($post)) {
return new ComicPressBackendAttachment($post);
}
}
}
return false;
}
// @codeCoverageIgnoreStart
function embed($size = 'comic') { return $this->_embed_image($size); }
function file($size = 'comic') { return $this->attachment->guid; }
function alt() { return $this->attachment->post_title; }
function title() { return $this->attachment->post_excerpt; }
// @codeCoverageIgnoreEnd
function get_info($size = 'comic') {
$info = array();
foreach (array('dims', 'url', 'file') as $field) {
$result = $this->{$field}($size);
if (is_array($result)) {
$info = array_merge($info, $result);
} else {
$info[$field] = $result;
}
}
return $info;
}
function generate_from_id($id) {
if (strpos($id, 'attachment-') === 0) {
$id = str_replace('attachment-', '', $id);
if (is_numeric($id)) {
$post = get_post($id);
if (!empty($post)) {
$meta = get_post_meta($post->ID, 'comicpress', true);
if (isset($meta['managed'])) {
if ($meta['managed']) {
return new ComicPressBackendAttachment($post);
}
}
}
}
}
return false;
}
}
?>
?>

View File

@ -174,6 +174,24 @@ function M($override_post = null) {
return $__attachments;
}
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;
}
}
}
}
}
/**
* Display the list of Storyline categories.
*/

View File

@ -58,17 +58,15 @@ ComicImageOrdering.build_dropdowns = function() {
ComicImageOrdering.build_response = function() {
var output = [];
$('comic-ordering').select('.cp-comic-attachment').each(function(att) {
if (att.visible) {
var data = {};
data.id = att.id.replace(/^attachment_/,'');
data.enabled = att.select('input[type=checkbox]').pop().checked;
data.children = {};
att.select('select').each(function(sel) {
var type = sel.name.replace(/^.*\[([^\]]+)\]$/, '$1');
data.children[type] = $F(sel);
});
output.push(data);
}
var data = {};
data.id = att.id.replace(/^attachment_/,'');
data.enabled = att.select('input[type=checkbox]').pop().checked;
data.children = {};
att.select('select').each(function(sel) {
var type = sel.name.replace(/^.*\[([^\]]+)\]$/, '$1');
data.children[type] = $F(sel);
});
output.push(data);
});
$('cp-comic-order').value = Object.toJSON(output);
};

View File

@ -13,10 +13,10 @@
echo 'Current: '; Restore(); the_title(); echo '<br />';
foreach (M() as $image) {
echo 'Default: '; echo $image->embed();
echo 'Comic: '; echo $image->embed('comic');
echo 'RSS: '; echo $image->embed('rss');
echo 'Archive: '; echo $image->embed('archive');
echo 'Default: '; echo EM($image);
echo 'Comic: '; echo EM($image, 'comic');
echo 'RSS: '; echo EM($image, 'rss');
echo 'Archive: '; echo EM($image, 'archive');
}
echo 'Chronologically next: '; RT('next'); the_title(); echo '<br />';

View File

@ -3,6 +3,7 @@
require_once('PHPUnit/Framework.php');
require_once('MockPress/mockpress.php');
require_once('ComicPressAdmin.inc');
require_once('ComicPress.inc');
class ComicPressAdminTest extends PHPUnit_Framework_TestCase {
function setUp() {

View File

@ -195,17 +195,19 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
'test-4' => array('enabled' => false),
),
array(
array('id' => 'test-1'),
array('id' => 'test-2')
array('comic' => 'test-1', 'default' => 'test-1'),
array('comic' => 'test-2', 'default' => 'test-2')
)
),
array(
array(
'test-1' => array('enabled' => true, 'children' => array('rss' => 'test-3')),
'test-2' => array('enabled' => false, 'children' => array('rss' => 'test-4')),
'test-3' => array('enabled' => true),
'test-4' => array('enabled' => true),
),
array(
array('id' => 'test-1', 'children' => array('rss' => 'test-3')),
array('comic' => 'test-1', 'rss' => 'test-3', 'default' => 'test-1'),
)
),
@ -216,6 +218,12 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
* @dataProvider providerTestGetAttachmentsWithChildren
*/
function testGetAttachmentsWithChildren($normalized_ordering, $expected_result) {
$comicpress = ComicPress::get_instance();
$comicpress->comicpress_options['image_types'] = array(
'comic' => array('default' => true),
'rss' => array('default' => false)
);
$p = $this->getMock('ComicPressComicPost', array('normalize_ordering'));
$p->post = (object)array('ID' => 1);

View File

@ -284,6 +284,34 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
$this->assertTrue($value === get_option($option));
}
}
function providerTestGetDefaultImageType() {
return array(
array(false, false),
array(array(), false),
array(
array(
'comic' => array('default' => false)
),
false
),
array(
array(
'comic' => array('default' => true)
),
'comic'
),
);
}
/**
* @dataProvider providerTestGetDefaultImageType
*/
function testGetDefaultImageType($image_types, $expected_result) {
$this->cp->comicpress_options['image_types'] = $image_types;
$this->assertEquals($expected_result, $this->cp->get_default_image_type());
}
}
?>

View File

@ -27,6 +27,7 @@ class FunctionsTest extends PHPUnit_Framework_TestCase {
$backend->expects($this->once())->method('generate_from_post')->with($post_to_test)->will($this->returnValue(array('test-1', 'test-2', 'test-3')));
$comicpress = ComicPress::get_instance();
$comicpress->backends = array($backend);
$comicpress->comicpress_options['image_types'] = array();
update_post_meta($post_to_test->ID, 'image-ordering', array(
'test-1' => array('enabled' => true, 'children' => array('rss' => 'test-2'))
@ -34,7 +35,9 @@ class FunctionsTest extends PHPUnit_Framework_TestCase {
$result = M($post_to_use);
$this->assertEquals(array('test-1' => array('rss' => 'test-2')), $result);
$this->assertEquals(array(
array('default' => 'test-1', 'rss' => 'test-2')
), $result);
$this->assertEquals($result, $__attachments);
$this->assertEquals(array(
'test-1' => array('enabled' => true, 'children' => array('rss' => 'test-2'))
@ -110,4 +113,62 @@ class FunctionsTest extends PHPUnit_Framework_TestCase {
function testPrepR($restrictions, $expected_result) {
$this->assertEquals($expected_result, __prep_R($restrictions, (object)array('ID' => 1)));
}
function providerTestEM() {
return array(
array(array(), 'embed', 'default', false, false),
array(
array('default' => 'test-1'),
'embed',
'default',
'test-1',
'embed'
),
array(
array('default' => 'test-1'),
'cats',
'default',
'test-1',
false
),
array(
array('default' => 'test-1'),
'embed',
'comic',
false,
false
),
array(
array('default' => 'test-1', 'comic' => 'test-2'),
'embed',
'comic',
'test-2',
'embed'
),
);
}
/**
* @dataProvider providerTestEM
*/
function testEM($info, $action, $which, $will_get_id, $expected_result) {
$backend = $this->getMock('ComicPressFakeBackend', array('generate_from_id', 'embed', 'url'));
if (is_string($will_get_id)) {
$backend->expects($this->once())->method('generate_from_id')->with($will_get_id)->will($this->returnValue($backend));
if (method_exists($backend, $action)) {
$backend->expects($this->once())->method($action)->will($this->returnValue($expected_result));
} else {
$backend->expects($this->never())->method($action);
}
} else {
$backend->expects($this->never())->method('generate_from_id');
}
$comicpress = ComicPress::get_instance();
$comicpress->backends = array($backend);
$this->assertEquals($expected_result, EM($info, $which, $action));
}
}

View File

@ -45,18 +45,24 @@ class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase {
function providerTestDims() {
return array(
array(false, false),
array(true, false),
array(array(), false),
array(array('url', 300, 200, false), array('width' => 300, 'height' => 200))
array(false, array()),
array(true, array()),
array(array(), array()),
array(array('dimensions' => '300x200'), array('width' => 300, 'height' => 200))
);
}
/**
* @dataProvider providerTestDims
*/
function testDims($image_downsize_result, $expected_result) {
_set_image_downsize_result(1, 'comic', $image_downsize_result);
function testDims($image_options, $expected_result) {
$comicpress = ComicPress::get_instance();
$comicpress->comicpress_options = array(
'image_types' => array(
'comic' => $image_options
)
);
$this->assertEquals($expected_result, $this->ba->dims('comic'));
}
@ -82,8 +88,9 @@ class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase {
array(null, false),
array(1, false),
array('attachment-1', true),
array('attachment-2', false)
);
array('attachment-2', false),
array('attachment-3', false),
);
}
/**
@ -91,6 +98,9 @@ class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase {
*/
function testGenerateFromID($id, $is_successful) {
wp_insert_post(array('ID' => 1));
wp_insert_post(array('ID' => 3));
update_post_meta(1, 'comicpress', array('managed' => true));
if ($is_successful) {
$return = new ComicPressBackendAttachment((object)array('ID' => 1));
@ -115,4 +125,4 @@ class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase {
'file' => '/root/file.jpg'
), $ba->get_info());
}
}
}