Changeset 592307
- Timestamp:
- 08/30/2012 09:55:24 AM (14 years ago)
- Location:
- selective-reading/trunk
- Files:
-
- 2 edited
-
readme.txt (modified) (2 diffs)
-
selective-reading.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
selective-reading/trunk/readme.txt
r567050 r592307 49 49 == Changelog == 50 50 51 = 0.3 = 52 * Fixed problem with UTF-8 characters displaying incorrectly in category titles. 53 51 54 = 0.2 = 52 55 * Fixes minor issue with extraneous html and body tags being generated around categories widget … … 62 65 * Initial version 63 66 67 = 0.3 = 68 * Fixed problem with UTF-8 characters displaying incorrectly in category titles. 69 -
selective-reading/trunk/selective-reading.php
r567050 r592307 2 2 /** 3 3 * @package Selective_reading 4 * @version 0. 14 * @version 0.3 5 5 */ 6 6 /* … … 9 9 Description: Simple plugin to allow website visitors to deselect categories they don't want to see. Works for unregistered users (using cookies). 10 10 Author: Andrew Moss 11 Version: 0. 111 Version: 0.3 12 12 Author URI: http://www.amoss.me.uk/ 13 13 */ … … 16 16 class SelectiveReading { 17 17 18 private static $cookieKey = 'wp-selective-reading-';18 private static $cookieKey = 'wp-selective-reading-'; 19 19 20 /** 21 * Add show/hide javascript links to the categories list, based on the state of the cookie 22 */ 23 public static function edit_categories_list($content) { 24 $dom = new DOMDocument(); 25 $dom->loadHTML($content); 26 27 // Track if any categories have been hidden 28 $anyHidden = false; 29 30 $listItems = $dom->getElementsByTagName('li'); 31 foreach($listItems as $item) { 32 // Find the category ID from the class of the list element 33 preg_match( '%(\d+)$%', $item->getAttribute('class'), $matches ); 34 $categoryID = $matches[1]; 35 36 // Create the link to show or hide the element 37 $currentState = SelectiveReading::get_state($categoryID); 38 $anyHidden = $anyHidden || !$currentState; 39 40 $showHideLink = $dom->createElement( 'a', $currentState ? '(hide)' : '(show)' ); 41 $showHideLink->setAttribute( 'onclick', 'wp_selective_reading_set_category_state(' . $categoryID . ', ' . ($currentState ? '0' : '1') .');' ); 42 $showHideLink->setAttribute( 'title', ($currentState ? 'Hide' : 'Show') . ' posts from this category.' ); 43 $showHideLink->setAttribute( 'class', 'wp-selective-reading-toggle-' . $categoryID . ' wp-selective-reading-link' ); 44 45 $item->insertBefore( $showHideLink, $item->childNodes->item(2) ); 46 } 47 48 // Add 'show all' link only if one or more categories are hidden 49 if( $anyHidden ) { 50 $showAllItem = $dom->createElement( 'li', '' ); 51 $showAllItem->setAttribute( 'class', 'cat-item wp-selective-reading-link' ); 52 $showAllLink = $dom->createElement( 'a', '(show all)' ); 53 $showAllLink->setAttribute( 'title', 'Show posts from all categories.' ); 54 $showAllLink->setAttribute( 'onclick', 'wp_selective_reading_clear_cookies();' ); 55 $showAllItem->appendChild( $showAllLink ); 56 $dom->appendChild( $showAllItem ); 57 } 58 59 // http://www.php.net/manual/en/domdocument.savehtml.php 60 $content = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML())); 61 return $content; 62 } 20 /** 21 * Add show/hide javascript links to the categories list, based on the state of the cookie 22 */ 23 public static function edit_categories_list($content) { 24 $dom = new DOMDocument(); 25 // Thanks to http://www.php.net/manual/en/domdocument.loadhtml.php#74777 for this UTF-8 fix 26 $encodedContent = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8"); 27 $dom->loadHTML($encodedContent); 28 29 // Track if any categories have been hidden 30 $anyHidden = false; 31 32 $listItems = $dom->getElementsByTagName('li'); 33 foreach($listItems as $item) { 34 // Find the category ID from the class of the list element 35 preg_match( '%(\d+)$%', $item->getAttribute('class'), $matches ); 36 $categoryID = $matches[1]; 37 38 // Create the link to show or hide the element 39 $currentState = SelectiveReading::get_state($categoryID); 40 $anyHidden = $anyHidden || !$currentState; 41 42 $showHideLink = $dom->createElement( 'a', $currentState ? '(hide)' : '(show)' ); 43 $showHideLink->setAttribute( 'onclick', 'wp_selective_reading_set_category_state(' . $categoryID . ', ' . ($currentState ? '0' : '1') .');' ); 44 $showHideLink->setAttribute( 'title', ($currentState ? 'Hide' : 'Show') . ' posts from this category.' ); 45 $showHideLink->setAttribute( 'class', 'wp-selective-reading-toggle-' . $categoryID . ' wp-selective-reading-link' ); 46 47 $item->insertBefore( $showHideLink, $item->childNodes->item(2) ); 48 } 49 50 // Add 'show all' link only if one or more categories are hidden 51 if( $anyHidden ) { 52 $showAllItem = $dom->createElement( 'li', '' ); 53 $showAllItem->setAttribute( 'class', 'cat-item wp-selective-reading-link' ); 54 $showAllLink = $dom->createElement( 'a', '(show all)' ); 55 $showAllLink->setAttribute( 'title', 'Show posts from all categories.' ); 56 $showAllLink->setAttribute( 'onclick', 'wp_selective_reading_clear_cookies();' ); 57 $showAllItem->appendChild( $showAllLink ); 58 $dom->appendChild( $showAllItem ); 59 } 60 61 // http://www.php.net/manual/en/domdocument.savehtml.php 62 $content = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML())); 63 return $content; 64 } 63 65 64 /**65 * Hides posts from disabled categories from being displayed when posts are listed on the main page66 */67 public static function edit_displayed_categories( $query ) {68 if( $query->is_home() && $query->is_main_query() ) {69 $query->set( 'cat', SelectiveReading::get_disabled_categories_str() );70 }71 }72 73 public static function enqueue_scripts() {74 wp_enqueue_script( 'selective-reading', plugins_url() . '/selective-reading/selective-reading.js' );75 }76 77 public static function enqueue_styles() {78 wp_enqueue_style( 'selective-reading', plugins_url() . '/selective-reading/selective-reading.css' );79 }80 81 /**82 * Checks which categories are disabled in the cookie and returns the appropriate exclusion string83 */84 private static function get_disabled_categories_str() {85 $disabledCategories = '';86 $firstMatch = true;87 foreach( $_COOKIE as $key => $val ) {88 if( preg_match( '%^' . SelectiveReading::$cookieKey . '(\d+)$%', $key, $matches ) ) {89 $categoryID = $matches[1];90 if( $val === '0' ) {91 if( $firstMatch ) {92 $firstMatch = false;93 } else {94 $disabledCategories .= ',';95 }96 $disabledCategories .= '-' . $categoryID;97 }98 }99 }100 return $disabledCategories;101 }102 103 /**104 * Set the enabled or disabled state for a category in a cookie105 * true = enabled106 */107 private static function set_cookie($categoryID, $state) {108 $expiry = time()+2629743; // ~1 month from now109 return setcookie(SelectiveReading::$cookieKey . $categoryID, $state ? '1' : '0', $expiry, "/");110 }111 112 /**113 * Returns the enabled or disabled state of a category, defaulting to enabled114 * Take into account parents' states115 */116 private static function get_state($categoryID) {117 // State in cookie, defaulting to enabled if unset118 $cookieState = array_key_exists( SelectiveReading::$cookieKey . $categoryID, $_COOKIE ) ? $_COOKIE[SelectiveReading::$cookieKey . $categoryID] : true;66 /** 67 * Hides posts from disabled categories from being displayed when posts are listed on the main page 68 */ 69 public static function edit_displayed_categories( $query ) { 70 if( $query->is_home() && $query->is_main_query() ) { 71 $query->set( 'cat', SelectiveReading::get_disabled_categories_str() ); 72 } 73 } 74 75 public static function enqueue_scripts() { 76 wp_enqueue_script( 'selective-reading', plugins_url() . '/selective-reading/selective-reading.js' ); 77 } 78 79 public static function enqueue_styles() { 80 wp_enqueue_style( 'selective-reading', plugins_url() . '/selective-reading/selective-reading.css' ); 81 } 82 83 /** 84 * Checks which categories are disabled in the cookie and returns the appropriate exclusion string 85 */ 86 private static function get_disabled_categories_str() { 87 $disabledCategories = ''; 88 $firstMatch = true; 89 foreach( $_COOKIE as $key => $val ) { 90 if( preg_match( '%^' . SelectiveReading::$cookieKey . '(\d+)$%', $key, $matches ) ) { 91 $categoryID = $matches[1]; 92 if( $val === '0' ) { 93 if( $firstMatch ) { 94 $firstMatch = false; 95 } else { 96 $disabledCategories .= ','; 97 } 98 $disabledCategories .= '-' . $categoryID; 99 } 100 } 101 } 102 return $disabledCategories; 103 } 104 105 /** 106 * Set the enabled or disabled state for a category in a cookie 107 * true = enabled 108 */ 109 private static function set_cookie($categoryID, $state) { 110 $expiry = time()+2629743; // ~1 month from now 111 return setcookie(SelectiveReading::$cookieKey . $categoryID, $state ? '1' : '0', $expiry, "/"); 112 } 113 114 /** 115 * Returns the enabled or disabled state of a category, defaulting to enabled 116 * Take into account parents' states 117 */ 118 private static function get_state($categoryID) { 119 // State in cookie, defaulting to enabled if unset 120 $cookieState = array_key_exists( SelectiveReading::$cookieKey . $categoryID, $_COOKIE ) ? $_COOKIE[SelectiveReading::$cookieKey . $categoryID] : true; 119 121 120 // Check if any parent categories are hidden121 $ancestors = get_ancestors( $categoryID, 'category' );122 $parentState = true;123 foreach( $ancestors as $ancestorID ) {124 $parentState = $parentState && (array_key_exists( SelectiveReading::$cookieKey . $ancestorID, $_COOKIE ) ? $_COOKIE[SelectiveReading::$cookieKey . $ancestorID] : true);125 }126 127 // If parent state is hidden and we have tried to show this category, reset it to hidden128 if( !$parentState ) {129 //SelectiveReading::set_cookie( $categoryID, false );130 }131 132 return ($parentState && $cookieState);133 }122 // Check if any parent categories are hidden 123 $ancestors = get_ancestors( $categoryID, 'category' ); 124 $parentState = true; 125 foreach( $ancestors as $ancestorID ) { 126 $parentState = $parentState && (array_key_exists( SelectiveReading::$cookieKey . $ancestorID, $_COOKIE ) ? $_COOKIE[SelectiveReading::$cookieKey . $ancestorID] : true); 127 } 128 129 // If parent state is hidden and we have tried to show this category, reset it to hidden 130 if( !$parentState ) { 131 //SelectiveReading::set_cookie( $categoryID, false ); 132 } 133 134 return ($parentState && $cookieState); 135 } 134 136 } 135 137 136 138 // Don't use the plugin in the admin panel 137 139 if( !is_admin() ) { 138 add_action( 'wp_enqueue_scripts', 'SelectiveReading::enqueue_scripts' );139 add_action( 'wp_enqueue_scripts', 'SelectiveReading::enqueue_styles' );140 add_filter( 'wp_list_categories', 'SelectiveReading::edit_categories_list' );141 add_action( 'pre_get_posts', 'SelectiveReading::edit_displayed_categories' );140 add_action( 'wp_enqueue_scripts', 'SelectiveReading::enqueue_scripts' ); 141 add_action( 'wp_enqueue_scripts', 'SelectiveReading::enqueue_styles' ); 142 add_filter( 'wp_list_categories', 'SelectiveReading::edit_categories_list' ); 143 add_action( 'pre_get_posts', 'SelectiveReading::edit_displayed_categories' ); 142 144 } 143 145 ?>
Note: See TracChangeset
for help on using the changeset viewer.