rearrange some things for layouts

This commit is contained in:
John Bintz 2009-07-23 15:12:13 -04:00
parent 99ee2c9ebb
commit 7f4188929c
8 changed files with 97 additions and 66 deletions

View File

@ -443,20 +443,6 @@ class ComicPressAddonCore extends ComicPressAddon {
} }
} }
function _glob($pattern) { return glob($pattern); }
function _file_get_contents($file) { return file_get_contents($file); }
function get_layout_choices() {
$layouts = array();
foreach ($this->_glob(get_template_directory() . '/layouts/*') as $file) {
$content = $this->_file_get_contents($file);
if (preg_match('#/\*.*Layout Name: ([^\n]+).*\*/#s', $content, $matches) > 0) {
$layouts[pathinfo($file, PATHINFO_BASENAME)] = $matches[1];
}
}
return $layouts;
}
function create_layout_options($layouts, $current_layout) { function create_layout_options($layouts, $current_layout) {
$output = array(); $output = array();
foreach ($layouts as $layout_filename => $name) { foreach ($layouts as $layout_filename => $name) {

View File

@ -15,7 +15,7 @@
<th scope="row" valign="top"><?php _e('Layout', 'comicpress') ?></th> <th scope="row" valign="top"><?php _e('Layout', 'comicpress') ?></th>
<td> <td>
<select name="cp[layout]"> <select name="cp[layout]">
<?php echo $this->create_layout_options($this->get_layout_choices(), $this->comicpress->comicpress_options['layout']) ?> <?php echo $this->create_layout_options($this->comicpress->get_layout_choices(), $this->comicpress->comicpress_options['layout']) ?>
</select> </select>
</td> </td>
</tr> </tr>

View File

@ -15,6 +15,9 @@ 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->expects($this->once())->method('get_layout_choices')->will($this->returnValue(array()));
ob_start(); ob_start();
$this->core->render_admin(); $this->core->render_admin();
$source = ob_get_clean(); $source = ob_get_clean();
@ -385,52 +388,6 @@ class CoreTest extends PHPUnit_Framework_TestCase {
} }
} }
} }
function providerTestGetLayoutChoices() {
return array(
array(
array(),
array()
),
array(
array(
'layout.php' => <<<FILE
Test
FILE
),
array()
),
array(
array(
'layout.php' => <<<FILE
/*
Layout Name: Test
*/
FILE
),
array('layout.php' => 'Test')
),
);
}
/**
* @dataProvider providerTestGetLayoutChoices
*/
function testGetLayoutChoices($files, $expected_results) {
$core = $this->getMock('ComicPressAddonCore', array('_glob', '_file_get_contents'));
_set_template_directory('/test');
$file_names = array();
foreach (array_keys($files) as $file) { $file_names[] = '/test/layouts/' . $file; }
$core->expects($this->once())->method('_glob')->with('/test/layouts/*')->will($this->returnValue($file_names));
foreach ($files as $file => $contents) {
$core->expects($this->once())->method('_file_get_contents')->with('/test/layouts/' . $file)->will($this->returnValue($contents));
}
$this->assertEquals($expected_results, $core->get_layout_choices());
}
} }
?> ?>

View File

@ -355,6 +355,20 @@ class ComicPress {
return $posts; return $posts;
} }
function _glob($pattern) { return glob($pattern); }
function _file_get_contents($file) { return file_get_contents($file); }
function get_layout_choices() {
$layouts = array();
foreach ($this->_glob(get_template_directory() . '/layouts/*') as $file) {
$content = $this->_file_get_contents($file);
if (preg_match('#/\*.*Layout Name: ([^\n]+).*\*/#s', $content, $matches) > 0) {
$layouts[pathinfo($file, PATHINFO_BASENAME)] = $matches[1];
}
}
return $layouts;
}
} }
?> ?>

View File

@ -177,9 +177,6 @@ function the_transcript($displaymode = 'raw') {
// Register Sidebar and Define Widgets // Register Sidebar and Define Widgets
if ( function_exists('register_sidebar') )
register_sidebar();
function widget_comicpress_latest_comics() { ?> function widget_comicpress_latest_comics() { ?>
<li> <li>
<h2>Latest Comics</h2> <h2>Latest Comics</h2>

View File

@ -2,6 +2,11 @@
/* /*
Layout Name: Classic Layout Name: Classic
*/ */
if (function_exists('register_sidebar')) {
register_sidebar('right');
}
?> ?>
<?php get_header() ?> <?php get_header() ?>
@ -12,6 +17,6 @@ Layout Name: Classic
<?php echo $content ?> <?php echo $content ?>
</div> </div>
<?php get_sidebar() ?> <?php get_sidebar('right') ?>
<?php get_footer() ?> <?php get_footer() ?>

25
layouts/three-column.inc Normal file
View File

@ -0,0 +1,25 @@
<?php
/*
Layout Name: Classic
*/
if (function_exists('register_sidebar')) {
register_sidebar('left');
register_sidebar('right');
}
?>
<?php get_header() ?>
<?php echo $comic ?>
<?php get_sidebar('left') ?>
<div id="content" class="narrowcolumn">
<?php echo $content ?>
</div>
<?php get_sidebar('right') ?>
<?php get_footer() ?>

View File

@ -171,6 +171,53 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected, $comic_posts["show_${show}"], $show); $this->assertEquals($expected, $comic_posts["show_${show}"], $show);
} }
} }
function providerTestGetLayoutChoices() {
return array(
array(
array(),
array()
),
array(
array(
'layout.php' => <<<FILE
Test
FILE
),
array()
),
array(
array(
'layout.php' => <<<FILE
/*
Layout Name: Test
*/
FILE
),
array('layout.php' => 'Test')
),
);
}
/**
* @dataProvider providerTestGetLayoutChoices
*/
function testGetLayoutChoices($files, $expected_results) {
$cp = $this->getMock('ComicPress', array('_glob', '_file_get_contents'));
_set_template_directory('/test');
$file_names = array();
foreach (array_keys($files) as $file) { $file_names[] = '/test/layouts/' . $file; }
$cp->expects($this->once())->method('_glob')->with('/test/layouts/*')->will($this->returnValue($file_names));
foreach ($files as $file => $contents) {
$cp->expects($this->once())->method('_file_get_contents')->with('/test/layouts/' . $file)->will($this->returnValue($contents));
}
$this->assertEquals($expected_results, $cp->get_layout_choices());
}
} }
?> ?>