diff --git a/classes/ComicPressTagBuilder.inc b/classes/ComicPressTagBuilder.inc index a3234e4..ee024c4 100644 --- a/classes/ComicPressTagBuilder.inc +++ b/classes/ComicPressTagBuilder.inc @@ -2,7 +2,6 @@ require_once('ComicPressStoryline.inc'); -// TODO Loop preservation class ComicPressTagBuilderFactory { private $storyline, $dbi; @@ -45,6 +44,45 @@ class ComicPressTagBuilderFactory { $comicpress = ComicPress::get_instance(); return $comicpress->find_file($name, $path, $categories); } + + /** + * Protect global $post and $wp_query. + * @param object $use_this_post If provided, after saving the current post, set up this post for template tag use. + */ + function protect($use_this_post = null) { + global $post, $wp_query; + + $this->_post = $post; + $this->_wp_query = $wp_query; + + if (!is_null($use_this_post)) { + $post = $use_this_post; + setup_postdata($post); + } + } + + /** + * Temporarily restore the global $post variable and set it up for use. + */ + function restore() { + global $post; + + $post = $this->_post; + setup_postdata($post); + } + + /** + * Restore global $post and $wp_query. + */ + function unprotect() { + global $post, $wp_query; + + $post = $this->_post; + $wp_query = $this->_wp_query; + + unset($this->_post); + unset($this->_wp_query); + } } class ComicPressTagBuilder { diff --git a/comicpress-core.php b/comicpress-core.php index 7eb5450..56508b3 100644 --- a/comicpress-core.php +++ b/comicpress-core.php @@ -32,7 +32,7 @@ add_action('init', '__comicpress_init'); function __comicpress_init() { $classes_search = array( - '/', '/classes/', '/classes/backends/' + '/classes/', '/classes/backends/' ); foreach ($classes_search as $path) { diff --git a/functions.inc b/functions.inc deleted file mode 100644 index bdf076e..0000000 --- a/functions.inc +++ /dev/null @@ -1,51 +0,0 @@ - 0, - 'Restore' => 0, - 'Unprotect' => 0, -) as $function => $param_count) { - if ($param_count == 0) { - add_action("comicpress-${function}", $function, 10); - } else { - add_filter("comicpress-${function}", $function, 10, $param_count); - } -} - -/** - * Protect global $post and $wp_query. - * @param object $use_this_post If provided, after saving the current post, set up this post for template tag use. - */ -function Protect($use_this_post = null) { - global $post, $wp_query, $__post, $__wp_query; - - $__post = $post; - $__wp_query = $wp_query; - - if (!is_null($use_this_post)) { - $post = $use_this_post; - setup_postdata($post); - } -} - -/** - * Temporarily restore the global $post variable and set it up for use. - */ -function Restore() { - global $post, $__post; - - $post = $__post; - setup_postdata($post); -} - -/** - * Restore global $post and $wp_query. - */ -function Unprotect() { - global $post, $wp_query, $__post, $__wp_query; - - $post = $__post; - $wp_query = $__wp_query; - - $__post = $__wp_query = null; -} diff --git a/test/ComicPressTagBuilderTest.php b/test/ComicPressTagBuilderTest.php index bfd19a9..462a37e 100644 --- a/test/ComicPressTagBuilderTest.php +++ b/test/ComicPressTagBuilderTest.php @@ -507,8 +507,7 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase { } function testFindFilePassthru() { - $dbi = $this->getMock('ComicPressDBInterface'); - $core = new ComicPressTagBuilderFactory($dbi); + $core = new ComicPressTagBuilderFactory(); $comicpress = $this->getMock('ComicPress', array('find_file')); $comicpress->expects($this->once()) @@ -522,4 +521,59 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase { ComicPress::get_instance(true); } + + function providerTestProtect() { + return array( + array(null, 'test'), + array('test3', 'test3') + ); + } + + /** + * @dataProvider providerTestProtect + */ + function testProtect($post_to_use, $expected_post) { + global $post, $wp_query; + + $post = "test"; + $wp_query = "test2"; + + $core = new ComicPressTagBuilderFactory(); + + $core->protect($post_to_use); + + $this->assertEquals($core->_post, 'test'); + $this->assertEquals($expected_post, $post); + $this->assertEquals($wp_query, $core->_wp_query); + } + + function testRestore() { + global $post; + + $core = new ComicPressTagBuilderFactory(); + + $post = 'not'; + $core->_post = 'test'; + + $core->restore(); + + $this->assertEquals($core->_post, $post); + } + + function testUnprotect() { + global $post, $wp_query; + + $core = new ComicPressTagBuilderFactory(); + + $core->_post = $core->_wp_query = 'test'; + $post = $wp_query = 'not'; + + $core->unprotect(); + + $this->assertEquals('test', $post); + $this->assertEquals('test', $wp_query); + + $this->assertTrue(!isset($core->_post)); + $this->assertTrue(!isset($core->_wp_query)); + } } diff --git a/test/FunctionsTest.php b/test/FunctionsTest.php deleted file mode 100644 index 0a6ed60..0000000 --- a/test/FunctionsTest.php +++ /dev/null @@ -1,63 +0,0 @@ -assertEquals($__post, 'test'); - $this->assertEquals($expected_post, $post); - $this->assertEquals($wp_query, $__wp_query); - } - - function testRestore() { - global $post, $__post; - - $post = 'not'; - $__post = 'test'; - - Restore(); - - $this->assertEquals($__post, $post); - } - - function testUnprotect() { - global $post, $__post, $wp_query, $__wp_query; - - $__post = $__wp_query = 'test'; - $post = $wp_query = 'not'; - - Unprotect(); - - $this->assertEquals('test', $post); - $this->assertEquals('test', $wp_query); - - $this->assertTrue(is_null($__post)); - $this->assertTrue(is_null($__wp_query)); - } -}