Dynamic Widgets - Version 1.0

Version Description

This is the first version. No upgrades available yet.

Download this release

Release Info

Developer qurl
Plugin Icon wp plugin Dynamic Widgets
Version 1.0
Comparing to
See all releases

Version 1.0

dynamic-widgets.php ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Plugin Name: Dynamic Widgets
4
+ * Plugin URI: http://www.qurl.nl/2010/01/dynamic-widgets-1-0/
5
+ * Description: Dynamic Widgets gives you more control over your widgets. It lets you dynamicly place widgets on pages by excluding or including rules for the homepage, single posts, pages, categories and archives.
6
+ * Author: Jacco
7
+ * Version: 1.0
8
+ * Author URI: http://www.qurl.nl/
9
+ * Tags: widget, dynamic, sidebar, custom
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * Released under the GPL v.2, http://www.gnu.org/copyleft/gpl.html
17
+ *
18
+ * @version $Id: dynamic-widgets.php 194733 2010-01-17 12:29:21Z qurl $
19
+ */
20
+
21
+ // Constants
22
+ define('DW_DB_TABLE', 'dynamic_widgets');
23
+ define('DW_LIST_LIMIT', 20);
24
+ define('DW_VERSION', '1.0');
25
+
26
+ // Functions
27
+ function dynwid_add_admin_menu() {
28
+ add_submenu_page('themes.php', 'Dynamic Widgets', 'Dynamic Widgets', 'switch_themes', 'dynwid-config', 'dynwid_admin_page');
29
+ }
30
+
31
+ function dynwid_add_plugin_actions($all) {
32
+ $links = array();
33
+ $links[ ] = '<a href="themes.php?page=dynwid-config">' . __('Settings') . '</a>';
34
+
35
+ return array_merge($links, $all);
36
+ }
37
+
38
+ function dynwid_admin_dump(){
39
+ require_once('dynwid_admin_dump.php');
40
+ die();
41
+ }
42
+
43
+ function dynwid_admin_page() {
44
+ require_once('dynwid_admin.php');
45
+ }
46
+
47
+ function dynwid_init() {
48
+ if ( is_admin() ) {
49
+ global $_POST, $wpdb;
50
+ if ( $_POST['dynwid_save'] == 'yes' ) {
51
+ require_once('dynwid_admin_save.php');
52
+ }
53
+
54
+ add_action('admin_menu', 'dynwid_add_admin_menu');
55
+ add_action('plugin_action_links_' . plugin_basename(__FILE__), 'dynwid_add_plugin_actions');
56
+ } else {
57
+ add_action('wp_head', 'dynwid_worker');
58
+ }
59
+ }
60
+
61
+ function dynwid_install() {
62
+ global $wpdb;
63
+ $dbtable = $wpdb->prefix . DW_DB_TABLE;
64
+
65
+ $query = "CREATE TABLE IF NOT EXISTS " . $dbtable . " (
66
+ id int(11) NOT NULL auto_increment,
67
+ widget_id varchar(40) NOT NULL,
68
+ maintype varchar(20) NOT NULL,
69
+ `name` varchar(40) NOT NULL,
70
+ `value` smallint(1) NOT NULL default '1',
71
+ PRIMARY KEY (id),
72
+ KEY widget_id (widget_id,maintype)
73
+ );";
74
+ $wpdb->query($query);
75
+
76
+ // Version check
77
+ /* $version = get_option('dynwid_version');
78
+ if ( version_compare($version, DW_VERSION, '<') ) {
79
+
80
+ } */
81
+ update_option('dynwid_version', DW_VERSION);
82
+ }
83
+
84
+ function dynwid_uninstall() {
85
+ global $wpdb;
86
+ $dbtable = $wpdb->prefix . DW_DB_TABLE;
87
+
88
+ // Housekeeping
89
+ delete_option('dynwid_version');
90
+ $query = "DROP TABLE " . $dbtable;
91
+ $wpdb->query($query);
92
+
93
+ // Redirect to plugins page for deactivation
94
+ wp_redirect( get_option('siteurl') . '/wp-admin/plugins.php' );
95
+ die();
96
+ }
97
+
98
+ function dynwid_worker() {
99
+ require_once('dynwid_worker.php');
100
+ }
101
+
102
+ // Hooks
103
+ add_action('admin_action_dynwid_dump', 'dynwid_admin_dump');
104
+ add_action('admin_action_dynwid_uninstall', 'dynwid_uninstall');
105
+ add_action('init', 'dynwid_init');
106
+ register_activation_hook(__FILE__, 'dynwid_install');
107
+ ?>
dynwid_admin.php ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * dynwid_admin.php - Startpage for admin
4
+ *
5
+ * @version $Id: dynwid_admin.php 194733 2010-01-17 12:29:21Z qurl $
6
+ */
7
+
8
+ require_once('dynwid_class.php');
9
+ if (! isset($DW) ) {
10
+ $DW = new dynWid();
11
+ }
12
+ ?>
13
+
14
+ <div class="wrap">
15
+ <h2>Dynamic Widgets</h2>
16
+
17
+ <?php
18
+ // Actions
19
+ switch ( $_GET['action'] ) {
20
+ case 'edit':
21
+ require_once('dynwid_admin_edit.php');
22
+ break;
23
+
24
+ default:
25
+ // Special case: Reset action needs to go back to overview.
26
+ if ( $_GET['action'] == 'reset' ) {
27
+ check_admin_referer('plugin-name-action_reset_' . $_GET['id']);
28
+ $DW->resetOptions($_GET['id']);
29
+ ?>
30
+ <div class="updated fade" id="message">
31
+ <p>
32
+ <strong>Widget options have been reset to default.</strong><br />
33
+ </p>
34
+ </div>
35
+ <?php } ?>
36
+
37
+ <style type="text/css">
38
+ .helpbox {
39
+ -moz-border-radius-topleft : 6px;
40
+ -moz-border-radius-topright : 6px;
41
+ -moz-border-radius-bottomleft : 6px;
42
+ -moz-border-radius-bottomright : 6px;
43
+ border-style : solid;
44
+ border-width : 1px;
45
+ border-color : #E3E3E3;
46
+ padding : 5px;
47
+ background-color : white;
48
+ width : 98%;
49
+ }
50
+ </style>
51
+
52
+ <div class="helpbox">
53
+ <strong>Static / Dynamic</strong><br />
54
+ When a widget is <em>Static</em>, the widget uses the WordPress default. In other words, it's shown everywhere.<br />
55
+ A widget is <em>Dynamic</em> when there are options set. E.g. not showing on the front page.<br />
56
+ <br />
57
+
58
+ <strong>Reset</strong><br />
59
+ Reset makes the widget return to <em>Static</em>.<br />
60
+ </div>
61
+
62
+ <?php
63
+ foreach ( $DW->sidebars as $sidebar_id => $widgets ) {
64
+ if ( count($widgets) > 0 ) {
65
+ if ( $sidebar_id == 'wp_inactive_widgets' ) {
66
+ $name = 'Inactive Widgets';
67
+ } else {
68
+ $name = $DW->getName($sidebar_id, 'S');
69
+ }
70
+ ?>
71
+
72
+ <div class="postbox-container" style="width:48%;margin-top:10px;margin-right:10px;">
73
+ <table cellspacing="0" class="widefat fixed">
74
+ <thead>
75
+ <tr>
76
+ <th class="managage-column" scope="col"><?php echo $name; ?></th>
77
+ <th style="width:70px">&nbsp;</th>
78
+ </tr>
79
+ </thead>
80
+
81
+ <tbody class="list:link-cat" id="<?php echo str_replace('-', '_', $sidebar_id); ?>">
82
+ <?php foreach ( $widgets as $widget_id ) { ?>
83
+ <tr>
84
+ <td class="name">
85
+ <p class="row-title"><?php echo $DW->getName($widget_id); ?></p>
86
+ <div class="row-actions">
87
+ <span class="edit">
88
+ <a title="Edit this widget options" href="themes.php?page=dynwid-config&amp;action=edit&amp;id=<?php echo $widget_id; ?>">Edit</a>
89
+ </span>
90
+ <?php if ( $DW->hasOptions($widget_id) ) { ?>
91
+ <span class="delete">
92
+ <?php $href = wp_nonce_url('themes.php?page=dynwid-config&amp;action=reset&amp;id=' . $widget_id, 'plugin-name-action_reset_' . $widget_id); ?>
93
+ | <a class="submitdelete" title="Reset widget to Static" onclick="if ( confirm('You are about to reset this widget \'<?php echo strip_tags($DW->getName($widget_id)); ?>\'\n \'Cancel\' to stop, \'OK\' to reset.') ) { return true;}return false;" href="<?php echo $href; ?>">Reset</a>
94
+ </span>
95
+ <?php } ?>
96
+ </div>
97
+ </td>
98
+ <td>
99
+ <?php echo ( $DW->hasOptions($widget_id) ) ? 'Dynamic' : 'Static'; ?>
100
+ </td>
101
+ </tr>
102
+ <?php } // END foreach $widgets ?>
103
+ </tbody>
104
+ </table>
105
+ </div>
106
+ <?php
107
+ } // END count $widgets
108
+ } // END foreach $sidebars
109
+ ?>
110
+
111
+ <div class="clear"><br /><br /></div>
112
+
113
+ <a href="#" onclick="document.getElementById('un').style.display='inline'; return false;">Advanced &gt;</a>
114
+ <div id="un" style="display:none">
115
+ <br />
116
+ For debugging purposes it is possible you're asked to create a dump. Click the 'Create dump' button and save the text file.
117
+ <br /><br />
118
+ <div id="dump">
119
+ <form action="" method="get">
120
+ <input type="hidden" name="action" value="dynwid_dump" />
121
+ <input class="button-primary" type="submit" value="Create dump" />
122
+ </form>
123
+ </div>
124
+
125
+ <br /><br />
126
+
127
+ If you deceide not to use this plugin anymore (sorry to hear that!). You can cleanup all settings and data related to this plugin by clicking on the 'Uninstall' button. This process is irreversible! After the cleanup, you can deactivate the plugin.
128
+ <br /><br />
129
+ <div id="uninstall">
130
+ <form action="" method="get">
131
+ <input type="hidden" name="action" value="dynwid_uninstall" />
132
+ <input class="button-primary" type="submit" value="Uninstall" onclick="if ( confirm('Are you sure you want to uninstall Dynamic Widgets?') ) { return true; } return false;" />
133
+ </form>
134
+ </div>
135
+ </div>
136
+
137
+ <?php } // END switch ?>
138
+
139
+ </div> <!-- /wrap //-->
dynwid_admin_dump.php ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * dynwid_admin_dump.php
4
+ *
5
+ * @version $Id: dynwid_admin_dump.php 194733 2010-01-17 12:29:21Z qurl $
6
+ */
7
+
8
+ require_once('dynwid_class.php');
9
+ if (! isset($DW) ) {
10
+ $DW = new dynWid();
11
+ }
12
+
13
+ header('Content-Description: File Transfer');
14
+ header('Content-Disposition: attachment; filename=dynwid_dump_' . date('Ymd') . '.txt' );
15
+ header('Content-Type: text/plain');
16
+
17
+ $DW->dump();
18
+ ?>
dynwid_admin_edit.php ADDED
@@ -0,0 +1,263 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * dynwid_admin_edit.php - Options settinfs
4
+ *
5
+ * @version $Id: dynwid_admin_edit.php 194733 2010-01-17 12:29:21Z qurl $
6
+ */
7
+
8
+ // Front Page
9
+ $frontpage_yes_selected = 'checked="true"';
10
+ $opt_frontpage = $DW->getOptions($_GET['id'], 'front-page');
11
+ if ( count($opt_frontpage) > 0 ) {
12
+ $frontpage_condition = $opt_frontpage[0]['value'];
13
+ if ( $frontpage_condition == '0' ) {
14
+ $frontpage_no_selected = $frontpage_yes_selected;
15
+ unset($frontpage_yes_selected);
16
+ }
17
+ }
18
+
19
+ // Single Post
20
+ $single_yes_selected = 'checked="true"';
21
+ $opt_single = $DW->getOptions($_GET['id'], 'single');
22
+ if ( count($opt_single) > 0 ) {
23
+ $single_condition = $opt_single[0]['value'];
24
+ if ( $single_condition == '0' ) {
25
+ $single_no_selected = $single_yes_selected;
26
+ unset($single_yes_selected);
27
+ }
28
+ }
29
+
30
+ // -- Author
31
+ $opt_single_author = $DW->getOptions($_GET['id'], 'single-author');
32
+ if ( count($opt_single_author) > 0 ) {
33
+ $single_author_act = array();
34
+ foreach ( $opt_single_author as $single_author_condition ) {
35
+ $single_author_act[ ] = $single_author_condition['name'];
36
+ }
37
+ }
38
+
39
+ $authors = get_users_of_blog();
40
+ if ( count($authors) > DW_LIST_LIMIT ) {
41
+ $single_author_condition_select_style = 'style="overflow:auto;height:240px;"';
42
+ }
43
+
44
+ // -- Category
45
+ $opt_single_category = $DW->getOptions($_GET['id'], 'single-category');
46
+ if ( count($opt_single_category) > 0 ) {
47
+ $single_category_act = array();
48
+ foreach ( $opt_single_category as $single_category_condition ) {
49
+ $single_category_act[ ] = $single_category_condition['name'];
50
+ }
51
+ }
52
+
53
+ // Pages
54
+ $page_yes_selected = 'checked="true"';
55
+ $opt_page = $DW->getOptions($_GET['id'], 'page');
56
+ if ( count($opt_page) > 0 ) {
57
+ $page_act = array();
58
+ foreach ( $opt_page as $page_condition ) {
59
+ if ( $page_condition['name'] == 'default' || empty($page_condition['name']) ) {
60
+ $page_default = $page_condition['value'];
61
+ } else {
62
+ $page_act[ ] = $page_condition['name'];
63
+ }
64
+ }
65
+
66
+ if ( $page_default == '0' ) {
67
+ $page_no_selected = $page_yes_selected;
68
+ unset($page_yes_selected);
69
+ }
70
+ }
71
+
72
+ $pages = get_pages();
73
+ if ( count($pages) > DW_LIST_LIMIT ) {
74
+ $page_condition_select_style = 'style="overflow:auto;height:240px;"';
75
+ }
76
+
77
+ // Categories
78
+ $category_yes_selected = 'checked="true"';
79
+ $opt_category = $DW->getOptions($_GET['id'], 'category');
80
+ if ( count($opt_category) > 0 ) {
81
+ $category_act = array();
82
+ foreach ( $opt_category as $category_condition ) {
83
+ if ( $category_condition['name'] == 'default' || empty($category_condition['name']) ) {
84
+ $category_default = $category_condition['value'];
85
+ } else {
86
+ $category_act[ ] = $category_condition['name'];
87
+ }
88
+ }
89
+
90
+ if ( $category_default == '0' ) {
91
+ $category_no_selected = $category_yes_selected;
92
+ unset($category_yes_selected);
93
+ }
94
+ }
95
+
96
+ $category = get_categories( array('hide_empty' => FALSE) );
97
+ if ( count($category) > DW_LIST_LIMIT ) {
98
+ $category_condition_select_style = 'style="overflow:auto;height:240px;"';
99
+ }
100
+
101
+ // Archives
102
+ $archive_yes_selected = 'checked="true"';
103
+ $opt_archive = $DW->getOptions($_GET['id'], 'archive');
104
+ if ( count($opt_archive) > 0 ) {
105
+ $archive_condition = $opt_archive[0]['value'];
106
+ if ( $archive_condition == '0' ) {
107
+ $archive_no_selected = $archive_yes_selected;
108
+ unset($archive_yes_selected);
109
+ }
110
+ }
111
+ ?>
112
+
113
+
114
+ <div id="adv"></div>
115
+
116
+ <style type="text/css">
117
+ label {
118
+ cursor : default;
119
+ }
120
+
121
+ .condition-select {
122
+ width : 300px;
123
+ -moz-border-radius-topleft : 6px;
124
+ -moz-border-radius-topright : 6px;
125
+ -moz-border-radius-bottomleft : 6px;
126
+ -moz-border-radius-bottomright : 6px;
127
+ border-style : solid;
128
+ border-width : 1px;
129
+ border-color : #E3E3E3;
130
+ padding : 5px;
131
+ }
132
+
133
+ .infotext {
134
+ width : 98%;
135
+ display : none;
136
+ }
137
+ </style>
138
+
139
+ <script type="text/javascript">
140
+ function tglinfo(id) {
141
+ var element = document.getElementById(id);
142
+ var display = 'd_';
143
+ display = display.concat(id);
144
+
145
+ if ( window[display] ) {
146
+ element.style.display = 'none';
147
+ window[display] = false;
148
+ } else {
149
+ element.style.display = 'inline';
150
+ window[display] = true;
151
+ }
152
+ }
153
+ var d_single = false;
154
+ var d_archive = false;
155
+ </script>
156
+
157
+ <?php if ( $_POST['dynwid_save'] == 'yes' ) { ?>
158
+ <div class="updated fade" id="message">
159
+ <p>
160
+ <strong>Widget options saved.</strong> <a href="themes.php?page=dynwid-config">Return</a> to Dynamic Widgets overview.
161
+ </p>
162
+ </div>
163
+ <?php } ?>
164
+
165
+ <?php if ( $_GET['work'] == 'none' ) { ?>
166
+ <div class="error" id="message">
167
+ <p>Dynamic does not mean static hiding of a widget. Hint: <a href="widgets.php">Remove</a> the widget from the sidebar.</p>
168
+ </div>
169
+ <?php } ?>
170
+
171
+ <h3>Edit options for <em><?php echo $DW->getName($_GET['id']); ?></em> Widget</h3>
172
+
173
+ <form action="<?php echo attribute_escape($_SERVER['REQUEST_URI']); ?>" method="post">
174
+ <?php wp_nonce_field('plugin-name-action_edit_' . $_GET['id']); ?>
175
+ <input type="hidden" name="dynwid_save" value="yes" />
176
+ <input type="hidden" name="widget_id" value="<?php echo $_GET['id']; ?>" />
177
+
178
+ <b>Front Page</b><br />
179
+ Show widget on the front page?<br />
180
+ <input type="radio" name="front-page" value="yes" id="front-page-yes" <?php echo $frontpage_yes_selected; ?> /> <label for="front-page-yes">Yes</label>
181
+ <input type="radio" name="front-page" value="no" id="front-page-no" <?php echo $frontpage_no_selected; ?> /> <label for="front-page-no">No</label>
182
+
183
+ <br /><br />
184
+
185
+ <b>Single Posts</b> <img src="<?php echo $DW->plugin_url; ?>img/info.gif" alt="info" title="Click to toggle info" onclick="tglinfo('single')"><br />
186
+ Show widget on single posts?<br />
187
+ <div id="i_single">
188
+ <div id="single" class="infotext">
189
+ When you use an author <b>AND</b> a category exception, both rules in the condition must be met. Otherwise the exception won't work.
190
+ If you want to use the rules in an OR condition. Add the same widget again and apply the other rule to that.
191
+ </div>
192
+ </div>
193
+ <input type="radio" name="single" value="yes" id="single-yes" <?php echo $single_yes_selected; ?> /> <label for="single-yes">Yes</label>
194
+ <input type="radio" name="single" value="no" id="single-no" <?php echo $single_no_selected; ?> /> <label for="single-no">No</label><br />
195
+ <table border="0" cellspacing="0" cellpadding="0">
196
+ <tr>
197
+ <td valign="top">
198
+ Except the posts by author:
199
+ <div id="single-author-select" class="condition-select" <?php echo $single_author_condition_select_style; ?>>
200
+ <?php foreach ( $authors as $author ) { ?>
201
+ <input type="checkbox" id="single_author_act_<?php echo $author->ID; ?>" name="single_author_act[]" value="<?php echo $author->ID; ?>" <?php if ( count($single_author_act) > 0 && in_array($author->ID,$single_author_act) ) { echo 'checked="true"'; } ?> /> <label for="single_author_act_<?php echo $author->ID; ?>"><?php echo $author->display_name; ?></label><br />
202
+ <?php } ?>
203
+ </div>
204
+ </td>
205
+ <td style="width:10px"></td>
206
+ <td valign="top">
207
+ Except the posts in category:
208
+ <div id="single-category-select" class="condition-select" <?php echo $category_condition_select_style; ?>>
209
+ <?php foreach ( $category as $cat ) { ?>
210
+ <input type="checkbox" id="single_cat_act_<?php echo $cat->cat_ID; ?>" name="single_category_act[]" value="<?php echo $cat->cat_ID; ?>" <?php if ( count($single_category_act) > 0 && in_array($cat->cat_ID,$single_category_act) ) { echo 'checked="true"'; } ?> /> <label for="single_cat_act_<?php echo $cat->cat_ID; ?>"><?php echo $cat->name; ?></label><br />
211
+ <?php } ?>
212
+ </div>
213
+ </td>
214
+ </tr>
215
+ </table>
216
+
217
+ <br /><br />
218
+
219
+ <b>Pages</b><br />
220
+ Show widget on pages?<br />
221
+ <input type="radio" name="page" value="yes" id="page-yes" <?php echo $page_yes_selected; ?> /> <label for="page-yes">Yes</label>
222
+ <input type="radio" name="page" value="no" id="page-no" <?php echo $page_no_selected; ?> /> <label for="page-no">No</label><br />
223
+ Except the pages:<br />
224
+ <div id="page-select" class="condition-select" <?php echo $page_condition_select_style; ?>>
225
+ <?php foreach ( $pages as $page ) { ?>
226
+ <input type="checkbox" id="page_act_<?php echo $page->ID; ?>" name="page_act[]" value="<?php echo $page->ID; ?>" <?php if ( count($page_act) > 0 && in_array($page->ID,$page_act) ) { echo 'checked="true"'; } ?> /> <label for="page_act_<?php echo $page->ID; ?>"><?php echo $page->post_title; ?></label><br />
227
+ <?php } ?>
228
+ </div>
229
+
230
+ <br />
231
+
232
+ <b>Category Pages</b><br />
233
+ Show widget on category pages?<br />
234
+ <input type="radio" name="category" value="yes" id="category-yes" <?php echo $category_yes_selected; ?> /> <label for="category-yes">Yes</label>
235
+ <input type="radio" name="category" value="no" id="category-no" <?php echo $category_no_selected; ?> /> <label for="category-no">No</label><br />
236
+ Except the categories:<br />
237
+ <div id="category-select" class="condition-select" <?php echo $category_condition_select_style; ?>>
238
+ <?php foreach ( $category as $cat ) { ?>
239
+ <input type="checkbox" id="cat_act_<?php echo $cat->cat_ID; ?>" name="category_act[]" value="<?php echo $cat->cat_ID; ?>" <?php if ( count($category_act) > 0 && in_array($cat->cat_ID,$category_act) ) { echo 'checked="true"'; } ?> /> <label for="cat_act_<?php echo $cat->cat_ID; ?>"><?php echo $cat->name; ?></label><br />
240
+ <?php } ?>
241
+ </div>
242
+
243
+ <br />
244
+
245
+ <b>Archive Pages</b> <img src="<?php echo $DW->plugin_url; ?>img/info.gif" alt="info" title="Click to toggle info" onclick="tglinfo('archive')"><br />
246
+ Show widget on archive pages?<br />
247
+ <div id="i_archive">
248
+ <div id="archive" class="infotext">
249
+ This option does not include category pages.
250
+ </div>
251
+ </div>
252
+ <input type="radio" name="archive" value="yes" id="archive-yes" <?php echo $archive_yes_selected; ?> /> <label for="archive-yes">Yes</label>
253
+ <input type="radio" name="archive" value="no" id="archive-no" <?php echo $archive_no_selected; ?> /> <label for="archive-no">No</label>
254
+
255
+ <br /><br />
256
+
257
+ <input class="button-primary" type="submit" value="Save" />
258
+ </form>
259
+
260
+ <form action="themes.php" method="get">
261
+ <input type="hidden" name="page" value="dynwid-config" />
262
+ <input class="button-secondary" type="submit" value="Cancel" style="position:relative;top:-23px;left:80px;" />
263
+ </form>
dynwid_admin_save.php ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * dynwid_admin_save.php - Saving options to the database
4
+ *
5
+ * @version $Id: dynwid_admin_save.php 194733 2010-01-17 12:29:21Z qurl $
6
+ */
7
+
8
+ // Security - nonce
9
+ check_admin_referer('plugin-name-action_edit_' . $_POST['widget_id']);
10
+
11
+ require_once('dynwid_class.php');
12
+ if (! isset($DW) ) {
13
+ $DW = new dynWid();
14
+ }
15
+
16
+ // Checking basic stuff
17
+ $fields = array( 'front-page', 'single', 'page', 'category', 'archive' );
18
+ $work = FALSE;
19
+ foreach ( $fields as $field ) {
20
+ if ( $_POST[$field] == 'yes' ) {
21
+ $work = TRUE;
22
+ break;
23
+ }
24
+ }
25
+ if (! $work ) {
26
+ $fields = array( 'single_actor_act', 'single_category_act', 'page_act', 'category_act' );
27
+ foreach ( $fields as $field ) {
28
+ if ( count($_POST[$field]) > 0 ) {
29
+ $work = TRUE;
30
+ break;
31
+ }
32
+ }
33
+ }
34
+ if (! $work ) {
35
+ wp_redirect( get_option('siteurl') . $_SERVER['REQUEST_URI'] . '&work=none' );
36
+ die();
37
+ }
38
+
39
+ // Checking already set options
40
+ if ( $DW->hasOptions($_POST['widget_id']) ) {
41
+ $DW->resetOptions($_POST['widget_id']);
42
+ }
43
+
44
+ // Front Page
45
+ if ( $_POST['front-page'] == 'no' ) {
46
+ $DW->addSingleOption($_POST['widget_id'], 'front-page');
47
+ }
48
+
49
+ // Single Post
50
+ if ( $_POST['single'] == 'no' ) {
51
+ $DW->addSingleOption($_POST['widget_id'], 'single');
52
+ }
53
+
54
+ // -- Author
55
+ if ( count($_POST['single_author_act']) > 0 ) {
56
+ if ( $_POST['single'] == 'yes' ) {
57
+ $DW->addSingleOption($_POST['widget_id'], 'single', '1');
58
+ }
59
+ $DW->addMultiOption($_POST['widget_id'], 'single-author', $_POST['single'], $_POST['single_author_act']);
60
+ }
61
+
62
+ // -- Category
63
+ if ( count($_POST['single_category_act']) > 0 ) {
64
+ if ( $_POST['single'] == 'yes' && count($_POST['single_author_act']) == 0 ) {
65
+ $DW->addSingleOption($_POST['widget_id'], 'single', '1');
66
+ }
67
+ $DW->addMultiOption($_POST['widget_id'], 'single-category', $_POST['single'], $_POST['single_category_act']);
68
+ }
69
+
70
+ // Pages
71
+ if ( count($_POST['page_act']) > 0 ) {
72
+ $DW->addMultiOption($_POST['widget_id'], 'page', $_POST['page'], $_POST['page_act']);
73
+ } else if ( $_POST['page'] == 'no' ) {
74
+ $DW->addSingleOption($_POST['widget_id'], 'page');
75
+ }
76
+
77
+ // Categories
78
+ if ( count($_POST['category_act']) > 0 ) {
79
+ $DW->addMultiOption($_POST['widget_id'], 'category', $_POST['category'], $_POST['category_act']);
80
+ } else if ( $_POST['category'] == 'no' ) {
81
+ $DW->addSingleOption($_POST['widget_id'], 'category');
82
+ }
83
+
84
+ // Archive
85
+ if ( $_POST['archive'] == 'no' ) {
86
+ $DW->addSingleOption($_POST['widget_id'], 'archive');
87
+ }
88
+ ?>
dynwid_class.php ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?
2
+ /**
3
+ * dynwid_class.php - Dynamic Widgets Class
4
+ *
5
+ * @version $Id: dynwid_class.php 194733 2010-01-17 12:29:21Z qurl $
6
+ */
7
+
8
+ class dynWid {
9
+ private $dbtable;
10
+ public $dynwid_list;
11
+ private $firstmessage;
12
+ private $registered_sidebars;
13
+ public $registered_widgets;
14
+ public $sidebars;
15
+ public $plugin_url;
16
+ private $wpdb;
17
+
18
+ public function __construct() {
19
+ global $wp_registered_sidebars, $wp_registered_widgets, $wpdb;
20
+ // $wp_registered_widget_controls
21
+
22
+ $this->firstmessage = TRUE;
23
+ $this->registered_sidebars = $wp_registered_sidebars;
24
+ $this->registered_widgets = &$wp_registered_widgets;
25
+ $this->sidebars = wp_get_sidebars_widgets();
26
+ $this->plugin_url = WP_PLUGIN_URL . '/' . str_replace( basename( __FILE__), '', plugin_basename(__FILE__) );
27
+
28
+ $this->wpdb = $wpdb;
29
+ $this->dbtable = $this->wpdb->prefix . DW_DB_TABLE;
30
+
31
+ $this->createList();
32
+ }
33
+
34
+ public function addMultiOption($widget_id, $maintype, $default, $act) {
35
+ if ( $default == 'no' ) {
36
+ $opt_default = '0';
37
+ $opt_act = '1';
38
+ } else {
39
+ $opt_default = '1';
40
+ $opt_act = '0';
41
+ }
42
+
43
+ $query = "INSERT INTO " . $this->dbtable . "
44
+ (widget_id,maintype,name,value)
45
+ VALUES
46
+ ('" . $widget_id . "', '" . $maintype . "', 'default', '" . $opt_default . "')";
47
+ $this->wpdb->query($query);
48
+ foreach ( $act as $option ) {
49
+ $query = "INSERT INTO " . $this->dbtable . "
50
+ (widget_id,maintype,name,value)
51
+ VALUES
52
+ ('" . $widget_id . "', '" . $maintype . "', '" . $option . "', '" . $opt_act . "')";
53
+ $this->wpdb->query($query);
54
+ }
55
+ }
56
+
57
+ public function addSingleOption($widget_id, $maintype, $value = '0') {
58
+ $query = "INSERT INTO " . $this->dbtable . "
59
+ (widget_id,maintype,value)
60
+ VALUES
61
+ ('" . $widget_id . "', '" . $maintype . "', '" . $value . "')";
62
+ $this->wpdb->query($query);
63
+ }
64
+
65
+ private function createList() {
66
+ $this->dynwid_list = array();
67
+
68
+ foreach ( $this->sidebars as $sidebar_id => $widgets ) {
69
+ if ( count($widgets) > 0 ) {
70
+ foreach ( $widgets as $widget_id ) {
71
+ if ( $this->hasOptions($widget_id) ) {
72
+ $this->dynwid_list[ ] = $widget_id;
73
+ }
74
+ }
75
+ }
76
+ }
77
+ }
78
+
79
+ public function detectPage() {
80
+ if ( is_front_page() ) {
81
+ return 'front-page';
82
+ } else if ( is_single() ) {
83
+ return 'single';
84
+ } else if ( is_page() ) {
85
+ return 'page';
86
+ } else if ( is_category() ) {
87
+ return 'category';
88
+ } else if ( is_archive() && ! is_category() ) {
89
+ return 'archive';
90
+ } else {
91
+ return 'undef';
92
+ }
93
+ }
94
+
95
+ public function dump() {
96
+ global $wp_version;
97
+
98
+ echo "wp version: " . $wp_version . "\n";
99
+ echo "dw version: " . DW_VERSION . "\n";
100
+ echo "php version: " . phpversion() . "\n";
101
+
102
+ echo "\n";
103
+ echo "list: \n";
104
+ print_r($this->dynwid_list);
105
+
106
+ echo "wp_registered_widgets: \n";
107
+ print_r($this->registered_widgets);
108
+
109
+ echo "options: \n";
110
+ print_r( $this->getOptions('%', NULL) );
111
+ }
112
+
113
+ public function getName($id, $type = 'W') {
114
+ switch ( $type ) {
115
+ case 'S':
116
+ $lookup = $this->registered_sidebars;
117
+ break;
118
+
119
+ default:
120
+ $lookup = $this->registered_widgets;
121
+ // end default
122
+ }
123
+
124
+ $name = $lookup[$id]['name'];
125
+
126
+ if ( $type == 'W' ) {
127
+ // Retrieve optional set title
128
+ $number = $lookup[$id]['params'][0]['number'];
129
+ $option_name = $lookup[$id]['callback'][0]->option_name;
130
+ $option = get_option($option_name);
131
+ if (! empty($option[$number]['title']) ) {
132
+ $name .= ': <span class="in-widget-title">' . $option[$number]['title'] . '</span>';
133
+ }
134
+ }
135
+
136
+ return $name;
137
+ }
138
+
139
+ public function getOptions($widget_id, $maintype) {
140
+ $opt = array();
141
+
142
+ $query = "SELECT widget_id, maintype, name, value FROM " . $this->dbtable . "
143
+ WHERE widget_id LIKE '" . $widget_id . "' AND maintype LIKE '" . $maintype . "%' ORDER BY name";
144
+ $results = $this->wpdb->get_results($query);
145
+
146
+ foreach ( $results as $myrow ) {
147
+ $opt[ ] = array('widget_id' => $myrow->widget_id, 'maintype' => $myrow->maintype, 'name' => $myrow->name, 'value' => $myrow->value);
148
+ }
149
+
150
+ return $opt;
151
+ }
152
+
153
+ public function hasOptions($widget_id) {
154
+ $query = 'SELECT COUNT(1) AS total FROM ' . $this->dbtable . ' WHERE widget_id = \'' . $widget_id . '\'';
155
+ $count = $this->wpdb->get_var($this->wpdb->prepare($query));
156
+
157
+ if ( $count > 0 ) {
158
+ return TRUE;
159
+ } else {
160
+ return FALSE;
161
+ }
162
+ }
163
+
164
+ public function message($text) {
165
+ if ( $this->firstmessage ) {
166
+ echo "\n";
167
+ $this->firstmessage = FALSE;
168
+ }
169
+ echo '<!-- ' . $text . ' //-->';
170
+ echo "\n";
171
+ }
172
+
173
+ public function resetOptions($widget_id) {
174
+ $query = "DELETE FROM " . $this->dbtable . " WHERE widget_id = '" . $widget_id . "'";
175
+ $this->wpdb->query($query);
176
+ }
177
+ }
178
+ ?>
dynwid_worker.php ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * dynwid_worker.php - The worker does the actual work.
4
+ *
5
+ * @version $Id: dynwid_worker.php 194733 2010-01-17 12:29:21Z qurl $
6
+ */
7
+
8
+ require_once('dynwid_class.php');
9
+ if (! isset($DW) ) {
10
+ $DW = new dynWid();
11
+ }
12
+ $DW->message('Dynamic Widgets INIT');
13
+
14
+ $whereami = $DW->detectPage();
15
+
16
+ foreach ( $DW->sidebars as $sidebar_id => $widgets ) {
17
+ // Only processing active sidebars with widgets
18
+ if ( $sidebar_id != 'wp_inactive_widgets' && count($widgets) > 0 ) {
19
+ foreach ( $widgets as $widget_id ) {
20
+ // Check if the widget has options set
21
+ if ( in_array($widget_id,$DW->dynwid_list) ) {
22
+ $act = array();
23
+ $opt = $DW->getOptions($widget_id, $whereami);
24
+ $display = TRUE;
25
+
26
+ foreach ( $opt as $condition ) {
27
+ if ( empty($condition['name']) && $condition['value'] == '0' ) {
28
+ $DW->message('Default for ' . $widget_id . ' set to FALSE (rule D1)');
29
+ $display = FALSE;
30
+ break;
31
+ } else {
32
+ // Get default value
33
+ if ( $condition['name'] == 'default' ) {
34
+ $default = $condition['value'];
35
+ } else {
36
+ $act[ ] = $condition['name'];
37
+ }
38
+
39
+ if ( $default == '0' ) {
40
+ $DW->message('Default for ' . $widget_id . ' set to FALSE (rule D2)');
41
+ $display = FALSE;
42
+ $other = TRUE;
43
+ } else {
44
+ $DW->message('Default for ' . $widget_id . ' set to TRUE (rule D3)');
45
+ $other = FALSE;
46
+ }
47
+ }
48
+ }
49
+
50
+ // Act the condition(s) when there are options set
51
+ if ( count($opt) > 0 ) {
52
+ switch ( $whereami ) {
53
+ case 'single':
54
+ global $post;
55
+
56
+ $act_author = array();
57
+ $act_category = array();
58
+ $post_category = array();
59
+
60
+ // Get the categories from the post
61
+ $categories = get_the_category();
62
+ foreach ( $categories as $category ) {
63
+ $post_category[ ] = $category->cat_ID;
64
+ }
65
+
66
+ // Split out the conditions
67
+ foreach ( $opt as $condition ) {
68
+ if ( $condition['maintype'] == 'single-author' && $condition['name'] != 'default' ) {
69
+ $act_author[ ] = $condition['name'];
70
+ } else if ( $condition['maintype'] == 'single-category' && $condition['name'] != 'default' ) {
71
+ $act_category[ ] = $condition['name'];
72
+ }
73
+ }
74
+
75
+ if (! $display ) {
76
+ $other = TRUE;
77
+ } else {
78
+ $other = FALSE;
79
+ }
80
+
81
+ if ( count($act_author) > 0 && count($act_category) > 0 ) {
82
+ // Use of array_intersect to be sure one value in both arrays returns true
83
+ if ( in_array($post->post_author,$act_author) && (bool) array_intersect($post_category, $act_category) ) {
84
+ $display = $other;
85
+ $DW->message('Exception triggered for ' . $widget_id . ' (rule ES1)');
86
+ }
87
+ } else if ( count($act_author) > 0 && count($act_category == 0) ) {
88
+ if ( in_array($post->post_author,$act_author) ) {
89
+ $display = $other;
90
+ $DW->message('Exception triggered for ' . $widget_id . ' (rule ES2)');
91
+ }
92
+ } else if ( count($act_author) == 0 && count($act_category) > 0 ) {
93
+ if ( (bool) array_intersect($post_category, $act_category) ) {
94
+ $display = $other;
95
+ $DW->message('Exception triggered for ' . $widget_id . ' (rule ES3)');
96
+ }
97
+ }
98
+ break;
99
+
100
+ case 'page':
101
+ if ( is_page($act) ) {
102
+ $display = $other;
103
+ $DW->message('Exception triggered for ' . $widget_id . ' (rule EP1)');
104
+ }
105
+ break;
106
+
107
+ case 'category':
108
+ if ( is_category($act) ) {
109
+ $display = $other;
110
+ $DW->message('Exception triggered for ' . $widget_id . ' (rule EC1)');
111
+ }
112
+ break;
113
+ }
114
+ }
115
+
116
+ if (! $display ) {
117
+ $DW->message('Removed ' . $widget_id . ' from display');
118
+ unset($DW->registered_widgets[$widget_id]);
119
+ }
120
+ }
121
+ } // END foreach $widgets
122
+ }
123
+ } // END foreach $sidebars
124
+
125
+ $DW->message('Dynamic Widgets END');
126
+ ?>
img/info.gif ADDED
Binary file
readme.txt ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Plugin Name ===
2
+ Contributors: Jacco
3
+ Donate link: http://www.qurl.nl/
4
+ Tags: widget, dynamic, sidebar, custom
5
+ Requires at least: 2.9.1
6
+ Tested up to: 2.9.1
7
+ Stable tag: 1.0
8
+
9
+ Dynamic Widgets gives you more control over your widgets. It lets you dynamicly place widgets on WordPress pages.
10
+
11
+ == Description ==
12
+
13
+ Dynamic Widgets gives you more control over your widgets. It lets you dynamicly place widgets on WordPress pages by excluding or including rules for the homepage, single posts, pages, categories and archives.
14
+
15
+ * Default widget display setting is supported for:
16
+ - Front page
17
+ - Single post pages
18
+ - Pages
19
+ - Category pages
20
+ - Archive pages
21
+ * Exceptions can be created for:
22
+ - Single post pages on Author and/or Category
23
+ - Pages on Page Title
24
+ - Category pages on Category
25
+
26
+ == Upgrade Notice ==
27
+
28
+ This is the first version. No upgrades available yet.
29
+
30
+ == Installation ==
31
+
32
+ Installation of this plugin is fairly easy:
33
+
34
+ 1. Unpack `dynamic-widgets.zip`
35
+ 2. Upload the whole directory and everything underneath to the `/wp-content/plugins/` directory.
36
+ 3. Activate the plugin through the 'Plugins' menu in WordPress.
37
+ 4. Visit the Dynamic Widgets Configuration page (settings link).
38
+ 5. Edit the desired widgets.
39
+
40
+ == Frequently Asked Questions ==
41
+
42
+ For the latest FAQ, please visit the [online FAQ](http://www.qurl.nl/faq/).
43
+
44
+ = You asked me to create a dump. How do I do that? =
45
+
46
+ * Click at the bottom of the Widgets Overview page on the 'Advanced >' link.
47
+ * Now a button 'Create dump' appears a bit below.
48
+ * Click that button.
49
+ * Save the text file.
50
+
51
+ = How do I completely remove Dynamic Widgets? =
52
+
53
+ * Click at the bottom of the Widgets Overview page on the 'Advanced >' link.
54
+ * Now a button 'Uninstall' appears a bit below.
55
+ * Click that button.
56
+ * Confirm you really want to uninstall the plugin. After the cleanup, you'll be redirected to the plugin page.
57
+ * Deactivate Dynamic Widgets.
58
+ * Remove the directory 'dynamic-widgets' underneath to the `/wp-content/plugins/` directory.
59
+
60
+ == Changelog ==
61
+
62
+ See Release notes
63
+
64
+ == Release notes ==
65
+
66
+ = Version 1.0 =
67
+ * This is the first stable release. However, the road ahead might be a bumpy. Fasten your seatbelt.
68
+ * Default widget display setting is supported for:
69
+ - Front page
70
+ - Single post pages
71
+ - Pages
72
+ - Category pages
73
+ - Archive pages
74
+ * Exceptions can be created for:
75
+ - Single post pages on Author and/or Category
76
+ - Pages on Page Title
77
+ - Category pages on Category
78
+
79
+ == Screenshots ==
80
+
81
+ 1. Widgets overview page
82
+ 2. Widget Options page
screenshot-1.jpg ADDED
Binary file
screenshot-2.jpg ADDED
Binary file