├── assets ├── banner-772x250.png └── banner-772x250.psd ├── languages ├── plugin_magic_widgets-de_DE.mo └── plugin_magic_widgets-de_DE.po ├── changelog.md ├── readme.txt ├── readme.md ├── toschos-magic-widgets.php └── class.Unfiltered_Text_Widget.php /assets/banner-772x250.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thefuxia/WP-Magic-Widgets/HEAD/assets/banner-772x250.png -------------------------------------------------------------------------------- /assets/banner-772x250.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thefuxia/WP-Magic-Widgets/HEAD/assets/banner-772x250.psd -------------------------------------------------------------------------------- /languages/plugin_magic_widgets-de_DE.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thefuxia/WP-Magic-Widgets/HEAD/languages/plugin_magic_widgets-de_DE.mo -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## Current, not versioned 4 | 5 | * Added inline documentation for hooks. 6 | * Fixed some code inspection warnings. 7 | * Added a `readme.md` for GitHub with developer instructions. 8 | 9 | ## Version 2014.03.22 10 | 11 | * Fixed labels for visibility options. 12 | * Use fixed font for the textarea. 13 | * Added a title to identify the widgets easier. The title will not be printed 14 | in sidebars, it is for internal use only. 15 | * Added @dnaber-de as contributor. Thanks! 16 | 17 | ## Version 2013.06.02 18 | 19 | * Removed secondary plugin. 20 | * Moved widget class to a separate file, so you can use it without activating 21 | the main plugin. 22 | * Made the plugin translatable. 23 | * Added German translation. 24 | * Added extendable visibility options. 25 | * Switched to date based version numbers. 26 | 27 | ## Version 1.2 28 | 29 | * Fixed a problem with the version numbers. Updates should now work better. -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Magic Widgets === 2 | Author URI: http://toscho.de 3 | Plugin URI: http://toscho.de/2011/wordpress-plugin-magische-widgets/ 4 | Contributors: toscho, dnaber-de 5 | Author: toscho 6 | Tags: widget, admin, sidebar 7 | Requires at least: 3.0 8 | Tested up to: 3.9 9 | Stable tag: 2014.03.22 10 | Version: 2014.03.22 11 | License: GPLv2 or later 12 | 13 | Assigns widgets to action hooks. 14 | 15 | == Description == 16 | 17 | Defines sidebar areas in `wp_head`, `wp_footer`, `admin_head` and `admin_footer`. You may extend the list. 18 | 19 | Additionally, the plugin creates a new widget, called *Unfiltered Text*. Very similar to the regular text widget, but it doesn’t insert any extra markup. 20 | 21 | Send me your bug reports and suggestions via my [contact page](http://toscho.de/kontakt/) or per bugtracker at the [public repository at GitHub](https://github.com/toscho/WP-Magic-Widgets). 22 | 23 | [Beschreibung auf Deutsch](http://toscho.de/2011/wordpress-plugin-magische-widgets/) 24 | 25 | == Installation == 26 | 27 | Upload the directory to your plugin directory. 28 | Activate the plugin through the 'Plugins' menu in WordPress. 29 | 30 | == Changelog == 31 | 32 | Version 2014.03.22 33 | 34 | * Fixed labels for visibility options. 35 | * Use fixed font for the textarea. 36 | * Added a title to identify the widgets easier. The title will not be printed in sidebars, it is for internal use only. 37 | * Added @dnaber-de as contributor. Thanks! 38 | 39 | Version 2013.06.02 40 | 41 | * Removed secondary plugin. 42 | * Moved widget class to a separate file, so you can use it without activating the main plugin. 43 | * Made the plugin translatable. 44 | * Added German translation. 45 | * Added extendable visibility options. 46 | 47 | Version 1.2 48 | 49 | * Fixed a problem with the version numbers. Updates should now work better. -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Magic Widgets 2 | 3 | A WordPress plugin that assigns widgets to action hooks. 4 | 5 | It defines sidebar areas in `wp_head`, `wp_footer`, `admin_head` and `admin_footer`. 6 | You can change the list, add your own areas or remove the default areas. 7 | 8 | Additionally, the plugin creates a new widget, called *Unfiltered Text*. 9 | It is very similar to the regular *Text* widget, but it doesn’t insert any extra markup. 10 | 11 | If you want to help: we need more translations. 12 | 13 | ## Hooks 14 | 15 | There is one hook in the main plugin class: 16 | 17 | ```php 18 | $actions = array ( 19 | 'wp_head' => __( 'Front End Header', 'plugin_magic_widgets' ), 20 | 'wp_footer' => __( 'Front End Footer', 'plugin_magic_widgets' ), 21 | 'admin_head' => __( 'Back End Header', 'plugin_magic_widgets' ), 22 | 'admin_footer' => __( 'Back End Footer', 'plugin_magic_widgets' ), 23 | 'login_head' => __( 'Log-in Header', 'plugin_magic_widgets' ), 24 | 'login_footer' => __( 'Log-in Footer', 'plugin_magic_widgets' ), 25 | ); 26 | 27 | $actions = apply_filters( 'magic_widgets_actions', $actions ); 28 | ``` 29 | 30 | You can add your own sidebar areas here. 31 | 32 | The widget class `Unfiltered_Text_Widget` offers more hooks: 33 | 34 | ```php 35 | apply_filters( 'tmw_visibility_options', $options ) 36 | ``` 37 | `$options` is a list of visibility selections: 38 | 39 | ```php 40 | $options = array ( 41 | 'all' => __( 'All', 'plugin_magic_widgets' ), 42 | 'members' => __( 'Members only', 'plugin_magic_widgets' ), 43 | 'anonymous' => __( 'Anonymous visitors only', 'plugin_magic_widgets' ) 44 | ); 45 | ``` 46 | 47 | You can add new options for particular roles, languages, visitors with comment 48 | cookies … be creative. 49 | 50 | Then you have to hook into the output handler: 51 | 52 | ```php 53 | do_action( 'tmw_show_widget', $instance, $args ); 54 | ``` 55 | 56 | `$instance['visibility']` will tell you what visibility the user has selected. 57 | This action fires for custom visibility selections only. 58 | 59 | There are two other output actions with the same arguments: 60 | 61 | ```php 62 | do_action( 'tmw_before_show_widget', $instance, $args ); 63 | do_action( 'tmw_after_show_widget', $instance, $args ); 64 | ``` 65 | 66 | Both run on every output, no matter what the `visibility` is. 67 | 68 | [Beschreibung auf Deutsch](http://toscho.de/2011/wordpress-plugin-magische-widgets/) -------------------------------------------------------------------------------- /languages/plugin_magic_widgets-de_DE.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: Toscho’s Magic Widgets v2014.03.23\n" 4 | "Report-Msgid-Bugs-To: \n" 5 | "POT-Creation-Date: \n" 6 | "PO-Revision-Date: 2014-10-25 20:53:24+0000\n" 7 | "Last-Translator: toscho \n" 8 | "Language-Team: \n" 9 | "MIME-Version: 1.0\n" 10 | "Content-Type: text/plain; charset=UTF-8\n" 11 | "Content-Transfer-Encoding: 8bit\n" 12 | "Plural-Forms: nplurals=2; plural=n != 1;\n" 13 | "X-Generator: CSL v1.x\n" 14 | "X-Poedit-Language: German\n" 15 | "X-Poedit-Country: GERMANY\n" 16 | "X-Poedit-SourceCharset: utf-8\n" 17 | "X-Poedit-KeywordsList: __;_e;__ngettext:1,2;_n:1,2;__ngettext_noop:1,2;_n_noop:1,2;_c,_nc:4c,1,2;_x:1,2c;_ex:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;\n" 18 | "X-Poedit-Basepath: ../\n" 19 | "X-Poedit-Bookmarks: \n" 20 | "X-Poedit-SearchPath-0: .\n" 21 | "X-Textdomain-Support: yes" 22 | 23 | #: class.Unfiltered_Text_Widget.php:18 24 | #@ plugin_magic_widgets 25 | msgid "Unfiltered Text" 26 | msgstr "Ungefilterter Text" 27 | 28 | #: class.Unfiltered_Text_Widget.php:20 29 | #@ plugin_magic_widgets 30 | msgid "Pure Markup" 31 | msgstr "Reines Markup" 32 | 33 | #: class.Unfiltered_Text_Widget.php:183 34 | #@ plugin_magic_widgets 35 | msgid "Visibility" 36 | msgstr "Sichtbarkeit" 37 | 38 | #: class.Unfiltered_Text_Widget.php:214 39 | #@ plugin_magic_widgets 40 | msgid "All" 41 | msgstr "Alle" 42 | 43 | #: class.Unfiltered_Text_Widget.php:215 44 | #@ plugin_magic_widgets 45 | msgid "Members only" 46 | msgstr "Nur Mitglieder" 47 | 48 | #: class.Unfiltered_Text_Widget.php:216 49 | #@ plugin_magic_widgets 50 | msgid "Anonymous visitors only" 51 | msgstr "Nur anonyme Besucher" 52 | 53 | #: toschos-magic-widgets.php:77 54 | #@ plugin_magic_widgets 55 | msgid "Front End Header" 56 | msgstr "Frontend Kopfteil" 57 | 58 | #: toschos-magic-widgets.php:78 59 | #@ plugin_magic_widgets 60 | msgid "Front End Footer" 61 | msgstr "Frontend Fußteil" 62 | 63 | #: toschos-magic-widgets.php:79 64 | #@ plugin_magic_widgets 65 | msgid "Back End Header" 66 | msgstr "Backend Kopfteil" 67 | 68 | #: toschos-magic-widgets.php:80 69 | #@ plugin_magic_widgets 70 | msgid "Back End Footer" 71 | msgstr "Backend Fußteil" 72 | 73 | #: toschos-magic-widgets.php:48 74 | #@ plugin_magic_widgets 75 | msgid "Extra widgets for your HTML headers and footers." 76 | msgstr "Extrawidgets für den HTML-Kopf- und Fußteil." 77 | 78 | #: toschos-magic-widgets.php:111 79 | #@ plugin_magic_widgets 80 | msgid "Use the Unfiltered Text widget." 81 | msgstr "Benutze das Widget Ungefilterter Text." 82 | 83 | #: class.Unfiltered_Text_Widget.php:144 84 | #@ default 85 | msgid "Title:" 86 | msgstr "" 87 | 88 | #: toschos-magic-widgets.php:81 89 | #@ plugin_magic_widgets 90 | msgid "Log-in Header" 91 | msgstr "Anmeldung Kopfteil" 92 | 93 | #: toschos-magic-widgets.php:82 94 | #@ plugin_magic_widgets 95 | msgid "Log-in Footer" 96 | msgstr "Anmeldung Fußteil" 97 | 98 | -------------------------------------------------------------------------------- /toschos-magic-widgets.php: -------------------------------------------------------------------------------- 1 | prefix = strtolower( __CLASS__ ) . '_'; 45 | 46 | if ( is_admin() ) 47 | $this->load_language(); 48 | 49 | // The extra widget. 50 | $widget_class = 'Unfiltered_Text_Widget'; 51 | 52 | if ( ! class_exists( $widget_class ) ) { 53 | /** @noinspection PhpIncludeInspection */ 54 | require_once plugin_dir_path( __FILE__ ) . "class.$widget_class.php"; 55 | } 56 | 57 | register_widget( $widget_class ); 58 | 59 | $this->sidebar_actions(); 60 | } 61 | 62 | /** 63 | * Set up sidebars and add the print_widget action 64 | * 65 | * @uses add_action() 66 | * @return void 67 | */ 68 | public function sidebar_actions() 69 | { 70 | $actions = array ( 71 | 'wp_head' => __( 'Front End Header', 'plugin_magic_widgets' ), 72 | 'wp_footer' => __( 'Front End Footer', 'plugin_magic_widgets' ), 73 | 'admin_head' => __( 'Back End Header', 'plugin_magic_widgets' ), 74 | 'admin_footer' => __( 'Back End Footer', 'plugin_magic_widgets' ), 75 | 'login_head' => __( 'Log-in Header', 'plugin_magic_widgets' ), 76 | 'login_footer' => __( 'Log-in Footer', 'plugin_magic_widgets' ), 77 | ); 78 | 79 | /** 80 | * Change extra sidebar registrations. 81 | * You must return an array. 82 | * 83 | * @param array $actions 84 | */ 85 | $actions = apply_filters( 'magic_widgets_actions', $actions ); 86 | 87 | // Register the areas and additional actions. 88 | foreach ( $actions as $action => $name ) 89 | $this->register_action( $action, $name ); 90 | } 91 | 92 | /** 93 | * Register a sidebar for an action. 94 | * 95 | * @param string $action Action name 96 | * @param string $name Sidebar name 97 | * @return void 98 | */ 99 | private function register_action( $action, $name ) 100 | { 101 | register_sidebar( 102 | array ( 103 | 'name' => $name, 104 | 'id' => $this->prefix . $action, 105 | 'description' => __( 'Use the Unfiltered Text widget.', 'plugin_magic_widgets' ), 106 | // Erase all other output 107 | 'before_widget' => '', 108 | 'after_widget' => '', 109 | 'before_title' => '', 110 | 'after_title' => '' 111 | ) 112 | ); 113 | 114 | add_action( $action, array ( $this, 'print_widget' ) ); 115 | } 116 | 117 | /** 118 | * Output 119 | * 120 | * @return boolean 121 | */ 122 | public function print_widget() 123 | { 124 | /* In wp-admin/includes/widgets.php:: wp_list_widget_controls() a filter 125 | * is set that makes all widgets render the controls instead of the 126 | * content. We have to remove it to get the real output. 127 | */ 128 | if ( 'admin_footer' === current_filter() ) 129 | remove_filter( 130 | 'dynamic_sidebar_params', 131 | 'wp_list_widget_controls_dynamic_sidebar' 132 | ); 133 | 134 | return dynamic_sidebar( $this->prefix . current_filter() ); 135 | } 136 | 137 | /** 138 | * Loads translation file. 139 | * 140 | * @return bool 141 | */ 142 | public function load_language() 143 | { 144 | $path = plugin_basename( dirname( __FILE__ ) ) . '/languages'; 145 | return load_plugin_textdomain( 'plugin_magic_widgets', FALSE, $path ); 146 | } 147 | 148 | /** 149 | * Remove translations from memory. 150 | * 151 | * @return void 152 | */ 153 | public function unload_language() 154 | { 155 | unset ( $GLOBALS['l10n']['plugin_magic_widgets'] ); 156 | } 157 | } 158 | 159 | add_action( 'widgets_init', array ( 'Toscho_Magic_Widgets', 'init' ), 20 ); -------------------------------------------------------------------------------- /class.Unfiltered_Text_Widget.php: -------------------------------------------------------------------------------- 1 | __( 'Pure Markup', 'plugin_magic_widgets' ) 21 | ), 22 | array ( 23 | 'width' => 300, 24 | 'height' => 150 25 | ) 26 | ); 27 | } 28 | 29 | /** 30 | * Front end output 31 | * 32 | * @param array $args 33 | * @param array $instance 34 | * @return void 35 | */ 36 | public function widget( $args, $instance ) 37 | { 38 | /** 39 | * Create output before widget output. 40 | * 41 | * @param array $instance Current widget data, 'text' and 'title'. 42 | * @param array $args Sidebar registration args, 'before' and so on. 43 | */ 44 | do_action( 'tmw_before_show_widget', $instance, $args ); 45 | 46 | if ( empty ( $instance[ 'visibility' ] ) ) { 47 | print $instance[ 'text' ]; 48 | 49 | return; 50 | } 51 | 52 | $user_logged_in = is_user_logged_in(); 53 | 54 | switch ( $instance[ 'visibility' ] ) 55 | { 56 | case 'all': 57 | print $instance['text']; 58 | break; 59 | 60 | case 'members': 61 | print ( $user_logged_in ? $instance[ 'text' ] : '' ); 62 | break; 63 | 64 | case 'anonymous': 65 | print ( $user_logged_in ? '' : $instance[ 'text' ] ); 66 | break; 67 | 68 | default: // custom visibility option 69 | 70 | /** 71 | * Print custom content. 72 | * 73 | * @param array $instance Current widget data, 'text', 'visibility' and 'title'. 74 | * @param array $args Sidebar registration args, 'before' and so on. 75 | */ 76 | do_action( 'tmw_show_widget', $instance, $args ); 77 | } 78 | 79 | /** 80 | * Create output after widget output. 81 | * 82 | * @param array $instance Current widget data, 'text' and 'title'. 83 | * @param array $args Sidebar registration args, 'before' and so on. 84 | */ 85 | do_action( 'tmw_after_show_widget', $instance, $args ); 86 | } 87 | 88 | /** 89 | * Prepares the content 90 | * 91 | * @param array $new_instance New content 92 | * @param array $old_instance Old content 93 | * @return array New content 94 | */ 95 | public function update( $new_instance, $old_instance ) 96 | { 97 | $visibility = $this->get_visibility_options(); 98 | 99 | if ( empty ( $new_instance[ 'visibility' ] ) 100 | or ! isset ( $visibility[ $new_instance[ 'visibility' ] ] ) 101 | ) 102 | $new_instance[ 'visibility' ] = $this->get_default_visibility(); 103 | 104 | $new_instance[ 'title' ] = esc_attr( $new_instance[ 'title' ] ); 105 | 106 | return $new_instance; 107 | } 108 | 109 | /** 110 | * Backend form 111 | * 112 | * @param array $instance 113 | * @return void 114 | */ 115 | public function form( $instance ) 116 | { 117 | $instance = wp_parse_args( 118 | $instance, 119 | array( 120 | 'text' => '', 121 | 'title' => '', 122 | 'visibility' => $this->get_default_visibility() 123 | ) 124 | ); 125 | 126 | print $this->get_title( $instance[ 'title' ] ); 127 | $text = format_to_edit( $instance[ 'text' ] ); 128 | print $this->get_textarea( $text, 'text' ); 129 | print $this->get_visibility_html( $instance[ 'visibility' ], 'visibility' ); 130 | } 131 | 132 | /** 133 | * Add a title to identify the widget in the widget manager. 134 | * 135 | * @ticket https://github.com/toscho/WP-Magic-Widgets/issues/3 136 | * @param string $title 137 | * @return string 138 | */ 139 | protected function get_title( $title ) 140 | { 141 | $title = esc_attr( $title ); 142 | $id = $this->get_field_id( 'title' ); 143 | $name = $this->get_field_name( 'title' ); 144 | $label = __( 'Title:' ); 145 | 146 | return "

147 | 150 |

"; 151 | } 152 | 153 | /** 154 | * Create the textarea for the main content 155 | * 156 | * @param string $content 157 | * @param string $name 158 | * @return string 159 | */ 160 | protected function get_textarea( $content, $name ) 161 | { 162 | return sprintf( 163 | '

164 | 165 |

', 166 | $this->get_field_id( $name ), 167 | $this->get_field_name( $name ), 168 | $content 169 | ); 170 | } 171 | 172 | /** 173 | * Render visibility radio buttons 174 | * 175 | * @param string $current 176 | * @param string $name 177 | * @return string 178 | */ 179 | protected function get_visibility_html( $current, $name ) 180 | { 181 | $options = $this->get_visibility_options(); 182 | $out = '
183 | ' . __( 'Visibility', 'plugin_magic_widgets' ) .' 184 |
"; 203 | } 204 | 205 | /** 206 | * Default options for widget visibility 207 | * 208 | * @uses apply_filters tmw_visibility_options 209 | * @return array 210 | */ 211 | protected function get_visibility_options() 212 | { 213 | $options = array ( 214 | 'all' => __( 'All', 'plugin_magic_widgets' ), 215 | 'members' => __( 'Members only', 'plugin_magic_widgets' ), 216 | 'anonymous' => __( 'Anonymous visitors only', 'plugin_magic_widgets' ) 217 | ); 218 | 219 | /** 220 | * Add custom visibility options, or remove existing ones. 221 | * You *must* return an array. 222 | * 223 | * @param array $options 224 | */ 225 | return apply_filters( 'tmw_visibility_options', $options ); 226 | } 227 | 228 | /** 229 | * Get the first visibility options key as default 230 | * 231 | * @return string 232 | */ 233 | protected function get_default_visibility() 234 | { 235 | $options = $this->get_visibility_options(); 236 | return key( $options ); 237 | } 238 | } 239 | --------------------------------------------------------------------------------