bunch of fixes, partials editing

This commit is contained in:
John Bintz 2009-08-05 22:29:54 -04:00
parent 89c1b216ee
commit 4fddaccb24
7 changed files with 217 additions and 23 deletions

View File

@ -100,7 +100,8 @@ class ComicPressAddonCore extends ComicPressAddon {
* Set up the admin interface and meta boxes. * Set up the admin interface and meta boxes.
*/ */
function setup_admin_interface() { function setup_admin_interface() {
add_theme_page(__("ComicPress Core", 'comicpress'), __('ComicPress Core', 'comicpress'), 'edit_themes', basename(__FILE__), array(&$this, 'render_admin')); add_theme_page(__("ComicPress Core", 'comicpress'), __('ComicPress Core', 'comicpress'), 'edit_themes', 'comicpress/render_admin', array(&$this, 'render_admin'));
add_theme_page(__("Edit Partials", 'comicpress'), __('Edit Partials', 'comicpress'), 'edit_themes', 'comicpress/edit_partials', array(&$this, 'render_edit_partials'));
if (isset($_REQUEST['post'])) { if (isset($_REQUEST['post'])) {
add_meta_box("comic-image-ordering", __("Comic Image Ordering", 'comicpress'), array(&$this, 'render_comic_image_ordering'), "post", "normal", "low"); add_meta_box("comic-image-ordering", __("Comic Image Ordering", 'comicpress'), array(&$this, 'render_comic_image_ordering'), "post", "normal", "low");
@ -203,6 +204,12 @@ class ComicPressAddonCore extends ComicPressAddon {
include(dirname(__FILE__) . '/partials/options-admin.inc'); include(dirname(__FILE__) . '/partials/options-admin.inc');
} }
function render_edit_partials() {
$nonce = wp_create_nonce('comicpress');
include(dirname(__FILE__) . '/partials/edit-partials.inc');
}
/** /**
* Render the comic image ordering interface. * Render the comic image ordering interface.
*/ */
@ -477,6 +484,17 @@ class ComicPressAddonCore extends ComicPressAddon {
} }
} }
function handle_update_override_partial($info) {
switch ($info['action']) {
case __('Update partial', 'comicpress'):
$this->comicpress->comicpress_options['override_partials'][$info['partial']] = $info['code'];
break;
case __('Delete override partial', 'comicpress'):
unset($this->comicpress->comicpress_options['override_partials'][$info['partial']]);
break;
}
}
/** /**
* Handle an update. * Handle an update.
*/ */
@ -503,6 +521,12 @@ class ComicPressAddonCore extends ComicPressAddon {
} }
$this->handle_update_comic_ordering(); $this->handle_update_comic_ordering();
} else if (isset($_POST['cp']['partial'])) {
$this->handle_update_override_partial($_POST['cp']);
$this->info(sprintf(__("Partial %s updated.", 'comicpress'), $_POST['cp']['partial']));
$this->comicpress->save();
$this->comicpress->init();
} else { } else {
//coming from us //coming from us
$this->handle_update_comicpress_options(); $this->handle_update_comicpress_options();
@ -513,7 +537,7 @@ class ComicPressAddonCore extends ComicPressAddon {
$this->comicpress->save(); $this->comicpress->save();
$this->info("ComicPress configuration updated."); $this->info(__("ComicPress configuration updated.", 'comicpress'));
$this->comicpress->init(); $this->comicpress->init();
} }

View File

@ -0,0 +1,81 @@
<div class="wrap">
<h2>Edit partials</h2>
<style type="text/css">
#partial-list-holder {
width: 200px;
float: left;
display: inline;
}
#partial-list-holder a {
font-size: 11px
}
#partial-editor {
margin-left: 210px;
}
#partial-editor h3 {
margin: 0 0 10px
}
</style>
<div id="partial-list-holder">
<?php
$valid_partials = array();
foreach (glob(get_template_directory() . DIRECTORY_SEPARATOR . 'partials' . DIRECTORY_SEPARATOR . '*.inc') as $partial) {
if (preg_match('#(partials.*)\.inc$#', $partial, $matches) > 0) {
$valid_partials[] = $matches[1];
}
}
$selected = reset($valid_partials);
if (isset($_REQUEST['cp']['partial'])) {
if (in_array($_REQUEST['cp']['partial'], $valid_partials)) {
$selected = $_REQUEST['cp']['partial'];
}
}
echo '<ul>';
foreach ($valid_partials as $partial_name) {
echo '<li>';
if ($partial_name == $selected) { echo '<strong>'; }
echo '<a href="' . add_query_arg('cp[partial]', $partial_name) . '">' . $partial_name . '</a>';
if ($partial_name == $selected) { echo '</strong>'; }
echo '</li>';
}
echo '</ul>';
$is_original = false;
if ($_REQUEST['cp']['action'] == __('Delete override partial', 'comicpress')) {
unset($_REQUEST['cp']['code']);
}
if (isset($_REQUEST['cp']['code'])) {
$partial_code = htmlentities($_REQUEST['cp']['code']);
} else {
if (isset($this->comicpress->comicpress_options['override_partials'][$selected])) {
$partial_code = htmlentities($this->comicpress->comicpress_options['override_partials'][$selected]);
} else {
$is_original = true;
$partial_code = htmlentities(file_get_contents(get_template_directory() . DIRECTORY_SEPARATOR . $selected . '.inc'));
}
}
?>
</div>
<div id="partial-editor">
<h3><?php printf(__('Editing %s', 'comicpress'), $selected) ?></h3>
<?php if ($is_original) { ?>
<p>(<em><?php _e('currently editing default partial', 'comicpress') ?></em>)</p>
<?php } ?>
<form method="post">
<input type="hidden" name="cp[_nonce]" value="<?php echo $nonce ?>" />
<input type="hidden" name="cp[partial]" value="<?php echo $selected ?>" />
<textarea name="cp[code]" rows="20" style="width: 100%"><?php echo $partial_code ?></textarea>
<input type="submit" class="button" name="cp[action]" value="<?php _e('Update partial', 'comicpress') ?>" />
<input type="submit" class="button" name="cp[action]" value="<?php _e('Delete override partial', 'comicpress') ?>" onclick="return confirm('<?php _e('Are you sure?', 'comicpress') ?>')" />
</form>
</div>
<br style="clear: both" />
</div>

View File

@ -15,7 +15,7 @@ class CoreTest extends PHPUnit_Framework_TestCase {
function testShowOptionsPage() { function testShowOptionsPage() {
$nonce = wp_create_nonce('comicpress'); $nonce = wp_create_nonce('comicpress');
$this->core->comicpress = $this->getMock('ComicPress'); $this->core->comicpress = $this->getMock('ComicPress', array('get_layout_choices'));
$this->core->comicpress->expects($this->once())->method('get_layout_choices')->will($this->returnValue(array())); $this->core->comicpress->expects($this->once())->method('get_layout_choices')->will($this->returnValue(array()));
ob_start(); ob_start();
@ -388,6 +388,38 @@ class CoreTest extends PHPUnit_Framework_TestCase {
} }
} }
} }
function providerTestHandleUpdateOverridePartial() {
return array(
array(
'hello',
'Update partial'
),
array(
'meow',
'Delete override partial'
),
);
}
/**
* @dataProvider providerTestHandleUpdateOverridePartial
*/
function testHandleUpdateOverridePartial($code, $action) {
$this->core->comicpress = (object)array(
'comicpress_options' => array(
'override_partials' => array(
'index' => '$hiss;'
)
)
);
$this->core->handle_update_override_partial(array_merge(compact('code', 'action'), array('partial' => 'index')));
if ($result && $action == "Update partial") {
$this->assertEquals($code, $this->core->comicpress->comicpress_options['override_partials']['index']);
}
}
} }
?> ?>

View File

@ -15,7 +15,8 @@ class ComicPress {
'comic_space' => 'comic_only', 'comic_space' => 'comic_only',
'category_usage' => 'storyline', 'category_usage' => 'storyline',
'layout' => 'classic.inc', 'layout' => 'classic.inc',
'helpers' => array() 'helpers' => array(),
'override_partials' => array()
); );
var $additional_stylesheets = array(); var $additional_stylesheets = array();
@ -159,7 +160,7 @@ class ComicPress {
<?php } <?php }
function comicpress_partial($content, $target) { function comicpress_partial($content, $target) {
return '<div class="partial-helper">' . substr(realpath($target), strlen(realpath(get_template_directory())) + 1) . '</div>' . $content; return '<div class="partial-helper">' . str_replace(get_template_directory() . DIRECTORY_SEPARATOR, '', $target) . '</div>' . $content;
} }
/** /**
@ -461,6 +462,19 @@ class ComicPress {
return false; return false;
} }
function get_options_partial($partials) {
foreach ($partials as $partial) {
foreach ($this->partial_paths as $path) {
$target = str_replace(get_template_directory() . DIRECTORY_SEPARATOR, '', $path) . DIRECTORY_SEPARATOR . $partial;
if (isset($this->comicpress_options['override_partials'][$target])) {
return array($target, $this->comicpress_options['override_partials'][$target]);
}
}
}
return false;
}
/** /**
* Gather blog posts for the given index page. * Gather blog posts for the given index page.
*/ */

View File

@ -18,6 +18,12 @@ function __comicpress_init() {
$comicpress->init(); $comicpress->init();
$addons = array(); $addons = array();
if (get_magic_quotes_gpc()) {
$_POST = stripslashes_deep($_POST);
$_GET = stripslashes_deep($_GET);
$_REQUEST = stripslashes_deep($_REQUEST);
}
if (is_dir($addons_dir = (dirname(__FILE__) . '/addons'))) { if (is_dir($addons_dir = (dirname(__FILE__) . '/addons'))) {
$entries = glob($addons_dir . '/*'); $entries = glob($addons_dir . '/*');
if (is_array($entries)) { if (is_array($entries)) {
@ -28,24 +34,26 @@ function __comicpress_init() {
require_once($entry . "/${classname}.inc"); require_once($entry . "/${classname}.inc");
$classname = "ComicPressAddon${classname}"; $classname = "ComicPressAddon${classname}";
if (class_exists($classname)) { if (class_exists($classname)) {
$addons[] = new $classname(); $addon =& new $classname();
end($addons)->init(&$comicpress);
$addon->init(&$comicpress);
if (current_user_can('edit_posts')) { if (current_user_can('edit_posts')) {
if (is_array($_REQUEST['cp'])) { if (is_array($_REQUEST['cp'])) {
if (isset($_REQUEST['cp']['_nonce'])) { if (isset($_REQUEST['cp']['_nonce'])) {
if (wp_verify_nonce($_REQUEST['cp']['_nonce'], 'comicpress')) { if (wp_verify_nonce($_REQUEST['cp']['_nonce'], 'comicpress')) {
if (method_exists(end($addons), 'handle_update')) { if (method_exists($addon, 'handle_update')) {
end($addons)->handle_update(); $addon->handle_update();
} }
} }
} }
} }
if (is_admin()) { if (is_admin()) {
add_action('admin_notices', array(end($addons), 'display_messages')); add_action('admin_notices', array(&$addon, 'display_messages'));
} else { } else {
add_action('wp_head', array(end($addons), 'display_messages')); add_action('wp_head', array(&$addon, 'display_messages'));
} }
} }
$addons[] = $addon;
} }
} }
} }
@ -83,16 +91,23 @@ function comicpress_get_header() {
function include_partial($partials = '') { function include_partial($partials = '') {
global $comicpress, $post, $nav_comics; global $comicpress, $post, $nav_comics;
if (!is_array($partials)) { if (!is_array($partials)) { $partials = func_get_args(); }
$partials = func_get_args();
$content = $target = null;
if (($result = $comicpress->get_options_partial($partials)) !== false) {
list($target, $code) = $result;
ob_start(); eval('?>' . $code . '<?'); $content = ob_get_clean();
} else {
$target = $comicpress->get_partial_path($partials);
if ($target !== false) {
ob_start(); include($target); $content = ob_get_clean();
}
} }
$target = $comicpress->get_partial_path($partials); if (!empty($target) && !empty($content)) {
echo apply_filters("comicpress_partial", $content, $target);
if ($target !== false) {
ob_start();
include($target);
echo apply_filters("comicpress_partial", ob_get_clean(), $target);
} }
} }

View File

@ -4,7 +4,7 @@ Theme URI: http://comicpress.org
Description: Publish a comic with WordPress. Development version. <a href="http://comicpress.org">Visit the ComicPress Website.</a> Description: Publish a comic with WordPress. Development version. <a href="http://comicpress.org">Visit the ComicPress Website.</a>
Author: Tyler Martin, John Bintz, Philip Hofer Author: Tyler Martin, John Bintz, Philip Hofer
Author URI: http://comicpres.org/ Author URI: http://comicpres.org/
Version: 2.8-git Version: 2.999-git
. .
The CSS, XHTML and design is released under GPL v3: The CSS, XHTML and design is released under GPL v3:
http://www.opensource.org/licenses/gpl-3.0.html http://www.opensource.org/licenses/gpl-3.0.html

View File

@ -423,6 +423,34 @@ FILE
$this->assertEquals(array('/subthemes/test-one'), $cp->partial_paths); $this->assertEquals(array('/subthemes/test-one'), $cp->partial_paths);
} }
function providerTestGetOverridePartials() {
return array(
array(
array('partials'),
array('index'),
array('partials/index'),
array('partials/index', true)
),
array(
array('partials'),
array('index'),
array('partials/single'),
false
)
);
}
/**
* @dataProvider providerTestGetOverridePartials
*/
function testGetOverridePartials($partial_paths, $requested_partials, $override_partials, $expected_result) {
$this->cp->partial_paths = $partial_paths;
foreach ($override_partials as $partial) {
$this->cp->comicpress_options['override_partials'][$partial] = true;
}
$this->assertEquals($expected_result, $this->cp->get_options_partial($requested_partials));
}
} }
?> ?>