-
Notifications
You must be signed in to change notification settings - Fork 0
/
force_categories.php
executable file
·367 lines (359 loc) · 11.9 KB
/
force_categories.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
/*
Plugin Name: Force Categories
Plugin URI: http://github.com/BryanH/Force_Categories
Description: Force posts by a user to include one or more specified categories (custom taxonomies) and/or prevent that user from assigning some categories (custom taxonomies) to her posts.
Version: 0.945
Author: Bryan Hanks, PMP
Author URI: http://www.chron.com/apps/adbxh0/
License: GPLv3
*/
/*
Copyright 2010 Houston Chronicle, Inc.
Force Categories is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Force Categories is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!class_exists("ForceCategories")) {
class ForceCategories {
var $meta_sm = 'force_cats';
var $defaults = array (
'use_taxonomy' => 'subsite'
); // default values
function ForceCategories() { //constructor
$this->register_plugin();
/* Widget settings. */
$widget_ops = array (
'classname' => 'force_categories',
'description' => __('Forces user posts to include and/or exclude specified categories', 'force_categories')
);
add_action('plugins_loaded', array (
$this,
'register_plugin'
));
/*
* Set up taxonomy for niche crap - jam the round peg into the octagonal hole.
* 'cause nothing says "Success" like forcing a blog platform to be a
*/
/*
$taxonomy_name = $this->plugin->com . 'subsite';
register_taxonomy( "chubb", 'post', array (
'hierarchical' => true,
'label' => 'SubSite',
'query_var' => true,
'show_tagcloud' => false,
'rewrite' => true
));
*/
/*
* POUND that
*/
/* wp_insert_term( "Featured", $taxonomy_name, array(
'description' => 'Featured posts that will display in the "Featured"/"Spotlight" area',
'slug' => 'featured',
)
);
wp_insert_term( "0 - Home Page", $taxonomy_name, array(
'description' => 'Posts that should display on the home page',
'slug' => 'home',
)
);
* Kill some kittens
wp_insert_term( "Voices", $taxonomy_name, array(
'description' => 'Voices posts',
'slug' => 'voices',
)
);*/
}
// Generic plugin functionality by John Blackbourn
function register_plugin() {
$this->plugin = (object) array (
'dom' => strtolower(get_class($this)),
'url' => WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)),
'dir' => WP_PLUGIN_DIR . '/' . basename(dirname(__FILE__))
);
$this->settings = get_option($this->plugin->dom);
if (!$this->settings) {
add_option($this->plugin->dom, $this->defaults, true, true);
$this->settings = $this->defaults;
}
load_plugin_textdomain($this->plugin->dom, false, $this->plugin->dom);
add_action('admin_init', array (
$this,
'register_setting'
));
}
function register_setting() {
if ($callback = method_exists($this, 'sanitize'))
$callback = array (
$this,
'sanitize'
);
register_setting($this->plugin->dom, $this->plugin->dom, $callback);
}
/*
* Verify the current user can edit the user record
* Parameter: user_id - id of user
* Returns: (nothing) or exception if fail
*/
function check_edit_authorization($user_id) {
if (!current_user_can('edit_user', $user_id)) {
wp_die(__('You do not have sufficient permissions to edit this user.'));
}
}
/*
* Retrieves the value of the key from a form's values
* Updates the datastore with that key:value
* Parameter: $key - form and datastore key (must be identical)
* Returns: value passed so the intermediate variable can be updated
*/
function update_usermeta_from_post($key) {
if (true == empty ($key)) {
wp_die(__('Invalid key passed to fc_update_from_post'));
}
update_usermeta($key, $_POST[$key]);
return $_POST[$key];
}
/*
* Retrieves value from datastore. If nothing returned,
* then it returns the default (or null if no default)
* (equivalent to $foo = $bar || $default)
* Parameters: $key - datastore key
* $default - default value
* Returns: either datastore's value, or default if former is empty
*/
function get_value_or_default($key, $default = null) {
$the_data = get_option($key);
if (true == empty ($the_data)) {
$the_data = $default;
}
return $the_data;
}
/*
* Get the styles installed
*/
function fc_style_enqueue() {
wp_enqueue_script('jquery'); // Ensure jQ is active.
$siteurl = get_option('siteurl');
$url = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/stylesheets/force_cat.css';
echo "<link rel='stylesheet' type='text/css' href='$url' />\n";
}
/**
* hook for update_wpmu_options to save any sitewide settings for this plugin
* TODO: update for blog-level instead of MU
*/
function update_sitewide_options() {
if ($_POST['taxonomy_to_use']) {
update_site_option('force_categories', $_POST['taxonomy_to_use']);
}
}
/*
* Plugin options
*/
function add_sitewide_options() {
$taxonomy_to_use = get_site_option('force_categories', esc_attr($defaults['use_taxonomy']));
$taxonomies = get_taxonomies('', 'objects');
?>
<?php include(WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)) . "plugin_options.php"); ?>
<?php
}
/*
* User profile screen
*/
function fc_user_profile($user) {
// check_edit_authorization($user);
if (!current_user_can('edit_user', $user)) {
wp_die(__('You do not have sufficient permissions to edit this user.'));
}
/* Form Setup */
$musthaves = get_the_author_meta('musthave_categories', $user->ID);
if (1 > count($musthaves)) {
$musthaves = array ();
}
$musthaves_flat = implode(',', $musthaves);
$canthaves = get_the_author_meta('canthave_categories', $user->ID);
if (1 > count($canthaves)) {
$canthaves = array ();
}
$canthaves_flat = implode(',', $canthaves);
// only get cats that aren't already assigned
$categories_in_use = array_merge($musthaves, $canthaves);
$taxonomy_to_use = get_site_option('force_categories', 'not set');
if ('not set' == $taxonomy_to_use) {
wp_die(__("<h2>Taxonomy/Category to use has not been set. You must go to the super-admin options page to do this first.</h2>"));
}
$categories = array_diff(get_terms($taxonomy_to_use, 'fields=names'), $categories_in_use);
?>
<?php include(WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)) . "options.php"); ?>
<?php
}
function save_fc_options($user_id) {
/*check_edit_authorization($user_id);*/
if (!current_user_can('edit_user', $user_id)) {
wp_die(__('You do not have sufficient permissions to edit this user.'));
}
$canthaveval = array ();
$cant_explode = explode(',', $_POST['canthaveval']);
$musthaveval = array ();
$must_explode = explode(',', $_POST['musthaveval']);
if (false != $cant_explode) {
$canthaveval = $cant_explode;
}
if (false != $must_explode) {
$musthaveval = $must_explode;
}
update_user_meta($user_id, 'canthave_categories', $canthaveval);
update_user_meta($user_id, 'musthave_categories', $musthaveval);
}
/*
* Obtains the url and file location of a given CSS
* Parameter: css filename (assumes it lives in the 'stylesheets' directory under the plugin)
* Returns: array of file, url to stylesheet.
* Use: list($cssfile, $cssurl) = get_css_location('somecss.css');
*/
function get_css_location($css) {
$admin_css = '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)) . 'stylesheets/' . $css;
return array (
$this->plugin->dir . $admin_css,
$this->plugin->url . $admin_css
);
}
/*
* Force or prohibit
* TODO - finish
*/
function save_post($post_id, $post) {
// $taxonomy = get_site_option('force_categories', 'not set');
$taxonomy = 'subsite';
$author_id = $post->author;
$must_haves = get_user_meta($author_id, 'musthave_categories');
$cant_haves = get_user_meta($author_id, 'canthave_categories');
/////wp_set_post_terms($post_id, $must_haves, $taxonomy, true);
// wp_set_post_terms( $post_id, $cant_haves, $taxonomy, false);
}
/*
* Show only main well posts
*/
function show_only_main_well_posts($query) {
global $query_comment;
//if( is_home() ) { /* and not something else - featured? */
if (false == $query->query_vars['suppress_filters']) {
$query->set('taxonomy', 'subsite');
$query->set('term', 'mainwell'); /*
$query->set('subsite', 'mainwell');*/
// NOPE: $query->set( array ('subsite' =>'mainwell'));
/* NOPE:
$query->set('meta_key', 'subsite');
$query->set('meta_value', 'mainwell');
*/
$query->set('posts_per_page', 5);
// // $query->set('posts_per_page', 3);
}
$query_comment = $query;
return $query;
//}
}
function echo_query() { //print it into the footer
// wp_die("AHAHAHAHAHA");
global $query_comment;
echo "<h1>Query!</h1><pre>";
print_r($query_comment);
echo "</pre>";
}
/*
* Filter to categories
* http://stackoverflow.com/questions/1155565/query-multiple-custom-taxonomy-terms-in-wordpress-2-8/2060777#2060777
*/
function splattered_brainz($where) {
global $wp_query;
$term_item = "mainwell";
$terms[] = get_terms($wp_query->query_vars['taxonomy'], array (
'slug' => $term_item
));
// wp_die("Term size: ". count($terms) . " [{$terms[0]->term_id}]");
//next, get the id of posts with that term in that tax
foreach ($terms as $term) {
$term_ids[] = $term[0]->term_id;
}
$post_ids = get_objects_in_term($term_ids, $wp_query->query_vars['taxonomy']);
if (!is_wp_error($post_ids) && count($post_ids)) {
$new_where = " AND wp_posts.ID IN (" . implode(', ', $post_ids) . ") ";
// re-add any other query vars via concatenation on the $new_where string below here
// now, sub out the bad where with the good
$where = str_replace("AND 0", $new_where, $where);
} else {
// give up
}
return $where;
}
}
}
if (class_exists("ForceCategories")) {
$force_cats = new ForceCategories();
}
if (isset ($force_cats)) {
// TODO: Actions here
if (is_admin()) { // admin page actions
add_action('edit_user_profile', array (
& $force_cats,
'fc_user_profile'
));
add_action('show_user_profile', array (
& $force_cats,
'fc_user_profile'
));
add_action('admin_head', array (
& $force_cats,
'fc_style_enqueue'
));
add_action('personal_options_update', array (
& $force_cats,
'save_fc_options'
));
add_action('edit_user_profile_update', array (
& $force_cats,
'save_fc_options'
));
// todo: MAKE for single blog only
add_action('wpmu_options', array (
& $force_cats,
'add_sitewide_options'
));
add_action('update_wpmu_options', array (
& $force_cats,
'update_sitewide_options'
));
add_action('save_post', array (
& $force_cats,
'save_post'
));
/*add_action('pre_get_posts', array (
& $force_cats,
'show_only_main_well_posts'
));*/
} else {
// Non-Admin enqueues, actions, and filters
// Actions
add_action('wp_footer', array (
& $force_cats,
'echo_query'
));
// Filters
add_filter('pre_get_posts', array (
& $force_cats,
'show_only_main_well_posts'
));
add_filter('posts_where', array (
& $force_cats,
'splattered_brainz'
));
}
}
?>