From 2def8b9185011cade4f41b999e538cb1ada19e02 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 24 Jan 2010 20:16:13 -0500 Subject: [PATCH] initial commit --- .gitignore | 4 + Makefile | 5 + classes/BookmarkWidget.inc | 203 ++++++++++++++++++++++++++++++++++++ phpunit.xml | 5 + test/BookmarkWidgetTest.php | 56 ++++++++++ 5 files changed, 273 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 classes/BookmarkWidget.inc create mode 100644 phpunit.xml create mode 100644 test/BookmarkWidgetTest.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0189a46 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.buildpath +.project +.settings +coverage/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e573669 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +FILES := $(shell find . -name '*.php' -o -name '*.inc') + +test-coverage : $(FILES) + phpunit --coverage-html coverage --syntax-check test + diff --git a/classes/BookmarkWidget.inc b/classes/BookmarkWidget.inc new file mode 100644 index 0000000..87e2999 --- /dev/null +++ b/classes/BookmarkWidget.inc @@ -0,0 +1,203 @@ + __CLASS__, 'description' => __('Allow the user to bookmark a page and then jump to it upon return.','comicpress') ); + $this->WP_Widget(__CLASS__, __('ComicPress Bookmark','comicpress'), $widget_ops); + } + + $this->text_fields = array( + 'three-button' => array( + 'tag-page' => array( + 'label' => __('Tag page', 'comicpress'), + 'default' => __('Bookmark', 'comicpress') + ), + 'clear-tag-off' => array( + 'label' => __('Clear tag off', 'comicpress'), + 'default' => '' + ), + 'clear-tag-on' => array( + 'label' => __('Clear tag on', 'comicpress'), + 'default' => __('Clear', 'comicpress') + ), + 'goto-tag-off' => array( + 'label' => __('Goto tag off', 'comicpress'), + 'default' => '' + ), + 'goto-tag-on' => array( + 'label' => __('Goto tag on', 'comicpress'), + 'default' => __('Goto', 'comicpress') + ) + ), + 'one-button' => array( + 'bookmark-clicker-off' => array( + 'label' => __('Set bookmark', 'comicpress'), + 'default' => __('+Bookmark', 'comicpress') + ), + 'bookmark-clicker-on' => array( + 'label' => __('Use bookmark', 'comicpress'), + 'default' => __('>>Bookmark', 'comicpress') + ) + ) + ); + } + + function init() {} + + function is_active() { + add_action('wp_head', array(&$this, 'wp_head')); + add_action('template_redirect', array(&$this, 'template_redirect')); + } + + function template_redirect() { + wp_enqueue_script('prototype'); + wp_enqueue_script('cookiejar', get_template_directory_uri() . '/js/cookiejar.js', array('prototype')); + wp_enqueue_script('bookmark', get_template_directory_uri() . '/js/bookmark.js', array('prototype', 'cookiejar')); + } + + function wp_head() { ?> + + '', 'mode' => 'three-button'); + + foreach (array_values($this->text_fields) as $field => $info) { + if (isset($info['default'])) { + $defaults[$field] = $info['default']; + } + } + + $instance = array_merge($defaults, $instance); ?> +
+

+ +

+ __('Three-button mode', 'comicpress'), + 'one-button' => __('One-button mode', 'comicpress') + ) as $mode => $label) { ?> +

+ +

+
">

+ text_fields[$mode] as $name => $info) { + extract($info); + $value = empty($instance[$name]) ? $default : $instance[$name]; + ?> + +
+

+ +
+ + text_fields as $type => $fields) { + $all_text_fields = array_merge($all_text_fields, array_keys($fields)); + } + + foreach ($all_text_fields as $key) { + $instance[$key] = strip_tags($new_instance[$key]); + } + + if (isset($this->text_fields[$new_instance['mode']])) { + $instance['mode'] = $new_instance['mode']; + } else { + $instance['mode'] = array_shift(array_keys($this->text_fields)); + } + + return $instance; + } + + function widget($args, $instance) { + global $post; + extract($args, EXTR_SKIP); + + echo $before_widget; + + $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']); + if (!empty($title)) { echo $before_title . $title . $after_title; }; + + $mode = !isset($this->text_fields[$instance['mode']]) ? array_shift(array_keys($this->text_fields)) : $instance['mode']; + + $link = is_home() ? get_bloginfo('url') : get_permalink($post); + + $id = 'comic-bookmark-' . md5(rand()); + switch ($instance['mode']) { + case 'three-button': ?> +
+ + + +
+ +
+ +
+ + + + + + diff --git a/test/BookmarkWidgetTest.php b/test/BookmarkWidgetTest.php new file mode 100644 index 0000000..374a38f --- /dev/null +++ b/test/BookmarkWidgetTest.php @@ -0,0 +1,56 @@ + true, + '//input[contains(@name, "mode") and @value="one-button" and not(@checked)]' => true, + '//input[contains(@name, "title") and @value=""]' => true, + ), + ), + array( + array('title' => 'Title', 'mode' => 'three-button'), + array( + '//input[contains(@name, "mode") and @value="three-button" and @checked]' => true, + '//input[contains(@name, "mode") and @value="one-button" and not(@checked)]' => true, + '//input[contains(@name, "title") and @value="Title"]' => true, + ), + ), + array( + array('title' => 'Another Title', 'mode' => 'one-button'), + array( + '//input[contains(@name, "mode") and @value="one-button" and @checked]' => true, + '//input[contains(@name, "mode") and @value="three-button" and not(@checked)]' => true, + '//input[contains(@name, "title") and @value="Another Title"]' => true, + ), + ), + ); + } + + /** + * @dataProvider providerTestForm + */ + function testForm($instance, $expected_additional_xpath) { + $w = new ComicPressBookmarkWidget(); + + ob_start(); + $w->form($instance); + $content = ob_get_clean(); + + $this->assertTrue(($xml = _to_xml($content, true)) !== false); + + foreach ($expected_additional_xpath as $xpath => $value) { + $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); + } + } +}