-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcat-templates.php
407 lines (359 loc) · 13 KB
/
cat-templates.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
/*
Plugin Name: Category Templates
Plugin URI: https://github.com/c3mdigital/Category-Templates
Description: Gives you the ability to apply different templates to your posts either individually, or by category.
Version: 3.0
Author: Aaron Gloege
Contributor: Chris Olbekson
Author URI: http://www.aarongloege.com/
Contributor URI: http://c3mdigital.com
===============================================================================
Copyright 2009 Aaron Gloege ([email protected])
This program 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 2 of the License, or
(at your option) any later version.
This program 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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===============================================================================
*/
function cat_temp_menus() {
add_submenu_page('themes.php','Category Templates', 'Category Templates', 'activate_plugins', 'themes.php?page=cat_temp_options_page', 'cat_temp_options_page');
if (function_exists('add_meta_box')) {
add_meta_box('cat_template_select','Post Template','cat_temp_meta','post','side','low');
}
}
function cat_temp_meta() {
global $wpdb, $post_ID;
$templates = cat_temp_get_page_templates();
$current = cat_temp_post_template($post_ID);
$out = '<select name="cat_temp_template" style="width:100%">';
$out .= '<option value="none"';
if ($post_ID == 0 || !$current) $out .= ' selected="selected"';
$out .= '>No Template</option>';
$out .= '<option value="/single.php"';
if ($current == "/single.php") $out .= ' selected="selected"';
$out .= '>Default Template</option>';
foreach ($templates as $template => $file) {
$out .= '<option value="'.$file.'"';
if ($current == $file) $out .= ' selected="selected"';
$out .= '>'.$template.'</option>';
}
$out .= "</select>";
$out .= "<p>Choosing a template here will override any templates assigned to this post's categories.</p>";
echo $out;
}
function cat_temp_post_submit($post_ID) {
global $wpdb;
if ($_POST['cat_temp_template']) {
$templates = (get_option("cat_temp_post"));
if ($_POST['cat_temp_template'] != 'none') {
$templates[$post_ID] = $_POST['cat_temp_template'];
} else {
if ($templates[$post_ID]) {
unset($templates[$post_ID]);
}
}
update_option("cat_temp_post", ($templates));
}
}
function cat_temp_post_template($ID) {
$templates = (get_option("cat_temp_post"));
return $templates[$ID];
}
function cat_temp($template) {
global $wp_query;
$post_obj = $wp_query->get_queried_object();
if (is_single()) {
$data = cat_temp_get_data();
$categories = get_the_category($post_obj->ID);
} else if (is_category()) {
$data = cat_temp_get_data(true);
$categories[0] = &get_category($post_obj->cat_ID);
}
$temp_data;
// Get templates for post categories
foreach((array)$categories as $category) {
if ($data[$category->term_id]['template'] != '0') {
$temp_data[$data[$category->term_id]['template']] = $data[$category->term_id]['priority']+($category->term_id/80000);
}
}
// Get templates for parent categories
foreach((array)$data as $key => $cat) {
if ($cat['all'] == "all" && $cat['template'] != "0") {
$id = (is_single()) ? (int)$cat['id'] : $key;
$descendants = get_term_children($id, 'category');
if ($descendants && in_category($descendants)) {
$temp_data[$cat['template']] = $cat['priority']+($cat['id']/80000);
}
}
}
//print_r($temp_data);
// Sort templates by priotiry, and return the one with the highest priority
if (is_array($temp_data)) {
asort($temp_data);
$template = array_shift(array_keys($temp_data));
}
// If currrent post has a template, use that one instead
if (is_single()) {
$overRule = cat_temp_post_template($post_obj->ID);
if ($overRule) $template = $overRule;
}
// Return template path
if (!empty($template)) {
if (file_exists(TEMPLATEPATH.$template)) {
include(TEMPLATEPATH.$template);
exit;
}
}
}
function cat_temp_is_cat($cat, $_post = null) {
if (in_category($cat, $_post)) {
return true;
} else {
$descendants = get_term_children((int)$cat, 'category');
if ($descendants && in_category($descendants, $_post)) return true;
}
return false;
}
function cat_temp_get_page_templates($str = "Template Name") {
$themes = get_themes();
$theme = get_current_theme();
$templates = $themes[$theme]['Template Files'];
$page_templates = array();
if (is_array($templates)) {
foreach((array)$templates as $template) {
if (!file_exists($template)) $template = WP_CONTENT_DIR.$template;
$template_data = implode('', file($template));
$name = '';
if (preg_match('|'.$str.':(.*)$|mi', $template_data, $name))
$name = $name[1];
if (!empty($name)) {
$page_templates[trim($name)] = str_replace($themes[$theme]['Template Dir'], "", $template);// basename( $template );
}
}
}
return $page_templates;
}
function cat_temp_cats($item, $current, $archive=false) {
if ($archive) {
$templates = cat_temp_get_page_templates('Archive Template');
$default = '/archive.php';
} else {
$templates = cat_temp_get_page_templates();
$default = '/single.php';
}
$out = '<select title="Template" class="ct_template" name="data';
if ($archive) $out .= '[archive]';
$out .= '['.$item.'][template]">';
$out .= '<option value="0"';
if ($current == "0") $out .= ' selected="selected"';
$out .= '>No Template</option>';
$out .= '<option value="'.$default.'"';
if ($current == $default) $out .= ' selected="selected"';
$out .= '>Default Template</option>';
foreach ($templates as $template => $file) {
$out .= '<option value="'.$file.'"';
if ($current == $file) $out .= ' selected="selected"';
$out .= '>'.$template.'</option>';
}
$out .= "</select>";
return $out;
}
function cat_temp_categories($child = 0) {
$data = array(
"hide_empty" => false,
"child_of" => $child,
"pad_count" => false,
);
$categories = get_categories($data);
$list = array();
foreach ((array)$categories as $cat) {
if ($cat->parent == $child) {
$list[] = array(
"name" => $cat->name,
"id" => $cat->cat_ID,
"count" => cat_temp_getALLposts($cat->cat_ID),
"acount" => $cat->category_count,
"child" => cat_temp_categories($cat->cat_ID),
);
}
}
return $list;
}
function cat_temp_li_fun($data) {
$out = "<ul class=\"cat-temp\">";
foreach ((array)$data as $root) {
$out .= "<li>".$root['name']." (".$root['count'].")</li>";
if (count($root['child']) > 0) {
$out .= cat_temp_li_fun($root['child']);
}
//$out .= "</li>";
}
$out .= "</ul>";
return $out;
}
function cat_temp_priority($item, $current, $archive) {
$pri = array("Lowest","Low","Medium","High","Highest");
$out = '<select class="ct_priority" title="Template Priority" name="data';
if ($archive) $out .= '[archive]';
$out .= '['.$item.'][priority]">';
$t = 0;
for ($i = 10; $i >= 1; $i = $i-2) {
$out .= '<option value="'.$i.'"';
if (intval($current) == $i) $out .= ' selected="selected"';
$out .= '>'.$pri[$t].'</option>';
$t++;
}
$out .= "</select>";
return $out;
}
function cat_temp_getALLposts($ID) {
$td = array(
'numberposts' => -1,
'category' => $ID,
);
return count(get_posts($td));
}
function cat_temp_admin_head() {
echo '<link type="text/css" rel="stylesheet" media="all" href="'.plugins_url('category-templates/admin.css').'" />';
echo '<script type="text/javascript" src="'.plugins_url('category-templates/admin.js').'"></script>';
}
function cat_temp_get_data($archive=false, $id=false) {
$t = (!$archive) ? (get_option('cat_temp_data')) : (get_option('cat_arch_data'));
return (!$id) ? $t : $t[$id];
}
function cat_temp_update($data) {
$archive = $data['archive'];
unset($data['archive']);
update_option('cat_temp_data', ($data));
update_option('cat_arch_data', ($archive));
}
function cat_temp_delete() {
delete_option('cat_temp_data');
delete_option('cat_temp_post');
delete_option('cat_arch_data');
}
function cat_temp_sub_cats($id, $data, $archive=false) {
$out .= ' <input type="checkbox"';
if ($data == "all") $out .= ' checked="checked"';
$out .= ' name="data';
if ($archive) $out .= "[archive]";
$out .= '['.$id.'][all]" value="all" title="Apply to sub-categories" /> <small>Apply to sub-categories</small>';
return $out;
}
function cat_temp_templates($id, $archive) {
if ($archive) {
$title = "Archive";
$class = " class=\"noborder\"";
$class2 = " noborder";
} else {
$title = "Posts";
}
$data = cat_temp_get_data($archive, $id);
$out .= "<td class=\"r$class2\" >$title:</td>";
$out .= "<td $class><div>".cat_temp_cats($id, $data['template'], $archive);
$out .= cat_temp_sub_cats($id, $data['all'], $archive).'</div></td>';
$out .= "<td $class><div>".cat_temp_priority($id, $data['priority'], $archive)."</div></td>";
return $out;
}
function cat_temp_td_fun($data, $padding = 5) {
$out = "";
foreach ((array)$data as $root) {
$out .= '<tr>';
$out .= '<td class="c" rowspan="2">'.$root['id'];
$out .= '<input type="hidden" name="data['.$root['id'].'][id]" value="'.$root['id'].'" />';
$out .= '</td>';
$out .= '<td class="wide" style="padding-left:'.$padding.'px;" rowspan="2">'.$root['name'].'</td>';
$out .= cat_temp_templates($root['id'], true);
$out .= '<td rowspan="2">'.$root['acount'].' ('.$root['count'].')</td>';
$out .= "</tr><tr>";
$out .= cat_temp_templates($root['id'], false);
$out .= '</tr>';
if (count($root['child']) > 0) {
$out .= cat_temp_td_fun($root['child'], $padding+10);
}
}
return $out;
}
function cat_temp_options_page() {
$_GET['lang'] = 'all';
?>
<div class="wrap cat-template">
<div class="icon32" id="icon-themes"><br/>
</div>
<h2>Settings for Category Templates</h2>
<?php
if ($_POST['update_theme']) {
cat_temp_update($_POST['data']);
echo '<div id="message" class="updated"><p>Theme Settings Updated</p></div>';
}
?>
<p>Configure the options below to apply templates to your categories, and their posts.</p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];?>">
<?php
// New way of setting the fields, for WP 2.7 and newer
if(function_exists('settings_fields')){
settings_fields('cat-temp-options');
} else {
wp_nonce_field('update-options');
?>
<input type="hidden" name="action" value="update" />
<?php
}
?>
<table>
<tr>
<td style="vertical-align:top" width="70%"><table width="100%" class="widefat" id="cat_temps">
<thead>
<tr>
<th width="2%" class="c">ID</th>
<th><?php _e('Category');?></th>
<th colspan="2"><?php _e('Template');?></th>
<th width="4%"><?php _e('Priority');?></th>
<th><?php _e("Post's");?></th>
</tr>
</thead>
<tbody>
<?php echo cat_temp_td_fun(cat_temp_categories());?>
</tbody>
</table></td>
<td style="vertical-align:top"><table width="100%" class="widefat">
<thead>
<tr>
<th>Instructions</th>
</tr>
</thead>
<tbody>
<tr>
<td><p>To specify a template for a certain category, select the template from the appropriate drop down box. The templates listed are the standard WordPress page templates.</p>
<p>If not already done, you must add the following codes to the top of your template pages inside your <strong>current theme</strong> for a template to appear here.</p>
<p>Post Templates:<br />
<code><?php /* Template Name: Blog Template */ ?></code> </p>
<p>Archive Templates:<br />
<code><?php /* Archive Template: Blog Template */ ?></code></p>
<p>Checking the <strong>apply to sub-categories</strong> checkbox will apply the selected template to all posts within the category <em>and</em> sub-categories. Templates will only apply to sub-categories that have no templates assigned, or if its of higher priority.</p>
<p>Choosing a priority will tell the plugin which template to favor if more than one template is returned. If the priorities are the same, the category with the lowest ID will have its template applied.</p></td>
</tr>
</tbody>
</table></td>
</tr>
</table>
<p class="submit">
<input type="submit" name="update_theme" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
add_action('admin_head', 'cat_temp_admin_head');
add_action('admin_menu', 'cat_temp_menus');
add_action('save_post', 'cat_temp_post_submit');
add_filter('template_redirect', 'cat_temp');
?>