-
Notifications
You must be signed in to change notification settings - Fork 4
/
toschos-magic-widgets.php
159 lines (141 loc) · 3.78 KB
/
toschos-magic-widgets.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
* Plugin Name: Toscho’s Magic Widgets
* Plugin URI: http://toscho.de/2011/wordpress-magische-widgets/
* Description: Extra widgets for your HTML headers and footers.
* Version: 2014.10.25
* Author: Thomas Scholz
* Author URI: http://toscho.de
* License: GPL v2
* Textdomain: plugin_magic_widgets
* Domain Path: /languages
*/
class Toscho_Magic_Widgets
{
/**
* Prefix for the widget IDs.
* Filled by the constructor.
*
* @type string
*/
protected $prefix = '';
/**
* Handler for the action 'widgets_init'. Instantiates this class.
*
* @return void
*/
public static function init()
{
new self;
}
/**
* Constructor
*
* Registers the Unfiltered Text widget.
*/
public function __construct()
{
/** @noinspection PhpUnusedLocalVariableInspection */
$dummy = __( 'Extra widgets for your HTML headers and footers.', 'plugin_magic_widgets' );
// Uppercase letters don’t work.
$this->prefix = strtolower( __CLASS__ ) . '_';
if ( is_admin() )
$this->load_language();
// The extra widget.
$widget_class = 'Unfiltered_Text_Widget';
if ( ! class_exists( $widget_class ) ) {
/** @noinspection PhpIncludeInspection */
require_once plugin_dir_path( __FILE__ ) . "class.$widget_class.php";
}
register_widget( $widget_class );
$this->sidebar_actions();
}
/**
* Set up sidebars and add the print_widget action
*
* @uses add_action()
* @return void
*/
public function sidebar_actions()
{
$actions = array (
'wp_head' => __( 'Front End Header', 'plugin_magic_widgets' ),
'wp_footer' => __( 'Front End Footer', 'plugin_magic_widgets' ),
'admin_head' => __( 'Back End Header', 'plugin_magic_widgets' ),
'admin_footer' => __( 'Back End Footer', 'plugin_magic_widgets' ),
'login_head' => __( 'Log-in Header', 'plugin_magic_widgets' ),
'login_footer' => __( 'Log-in Footer', 'plugin_magic_widgets' ),
);
/**
* Change extra sidebar registrations.
* You must return an array.
*
* @param array $actions
*/
$actions = apply_filters( 'magic_widgets_actions', $actions );
// Register the areas and additional actions.
foreach ( $actions as $action => $name )
$this->register_action( $action, $name );
}
/**
* Register a sidebar for an action.
*
* @param string $action Action name
* @param string $name Sidebar name
* @return void
*/
private function register_action( $action, $name )
{
register_sidebar(
array (
'name' => $name,
'id' => $this->prefix . $action,
'description' => __( 'Use the Unfiltered Text widget.', 'plugin_magic_widgets' ),
// Erase all other output
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => ''
)
);
add_action( $action, array ( $this, 'print_widget' ) );
}
/**
* Output
*
* @return boolean
*/
public function print_widget()
{
/* In wp-admin/includes/widgets.php:: wp_list_widget_controls() a filter
* is set that makes all widgets render the controls instead of the
* content. We have to remove it to get the real output.
*/
if ( 'admin_footer' === current_filter() )
remove_filter(
'dynamic_sidebar_params',
'wp_list_widget_controls_dynamic_sidebar'
);
return dynamic_sidebar( $this->prefix . current_filter() );
}
/**
* Loads translation file.
*
* @return bool
*/
public function load_language()
{
$path = plugin_basename( dirname( __FILE__ ) ) . '/languages';
return load_plugin_textdomain( 'plugin_magic_widgets', FALSE, $path );
}
/**
* Remove translations from memory.
*
* @return void
*/
public function unload_language()
{
unset ( $GLOBALS['l10n']['plugin_magic_widgets'] );
}
}
add_action( 'widgets_init', array ( 'Toscho_Magic_Widgets', 'init' ), 20 );