-
Notifications
You must be signed in to change notification settings - Fork 1
/
friendlycase.php
143 lines (142 loc) · 7.35 KB
/
friendlycase.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
<?php
/*
Plugin Name: FriendlyCase
Plugin URI: http://badcat.com/friendlycase/
Description: Enable Friendly, User-Definable Word Capitalization and Sentence Casing in Post and Page Titles.
Author: FriendlyCase Team
Author URI: http://badcat.com/friendlycase/
Version: 1.0.7
*/
/*
/--------------------------------------------------------------------\
| |
| License: GPL |
| |
| Copyright (C) 2011 - 2012, Joseph Kelter, Saul Rosenbaum, |
| Sharon Rooney Ross |
| http://badcat.com , http://visualchutzpah.com, |
| http://moonlightcoaching.com/ |
| All rights reserved. |
| |
| 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 Street, Fifth Floor |
| Boston, MA 02110-1301, USA |
| |
\--------------------------------------------------------------------/
*/
add_filter('the_title', 'friendlycase');
function friendlycase($title){
$ourData = get_option('fc_options');
$lowerCaseWords = array_map('trim', explode(',', $ourData['fc_text_lc']));
$upperCaseWords = array_map('trim', explode(',', $ourData['fc_text_uc']));
$ignoredWords = array_map('trim', explode(',', $ourData['fc_text_ig']));
$firstonly = $ourData['fc_firstonly'];
$wrapinspans = $ourData['fc_wrappit'];
$words = explode(' ', $title);
$countCycles = 0;
foreach ($words as $k => $word) {
$countCycles = $countCycles + 1;
if(in_arrayi($word, $lowerCaseWords)){
$words[$k] = ($wrapinspans == 1)? "<span class='fc_lc'>".mb_strtolower($word)."</span>":mb_strtolower($word);
}elseif(in_arrayi($word, $upperCaseWords)){
$words[$k] = ($wrapinspans == 1)? "<span class='fc_uc'>".strtoupper($word)."</span>":strtoupper($word);
}elseif(in_arrayi($word, $ignoredWords)){
$n = in_arrayi($word, $ignoredWords);
$words[$k] = ($wrapinspans == 1)? "<span class='fc_ig'>".$ignoredWords[$n]."</span>":$ignoredWords[$n];
}elseif($firstonly == 1){
if($countCycles == 1){
$words[$k] = ucwords(mb_strtolower($word));
}else{
$words[$k] = mb_strtolower($word);
}
}else{
$words[$k] = ucwords(mb_strtolower($word));
}
}
$newtitle = implode(' ', $words);
return ucfirst($newtitle);
}
function in_arrayi($element,$array){
return array_search(mb_strtolower($element),array_map('mb_strtolower',$array));
}
add_action('admin_init', 'friendlycaseoptions_init' );
add_action('admin_menu', 'friendlycaseoptions_add_page');
function friendlycaseoptions_init(){
register_setting( 'friendlycase_options', 'fc_options', 'friendlycaseoptions_validate' );
}
function friendlycaseoptions_add_page() {
add_options_page('FriendlyCase Settings Page', 'FriendlyCase Settings', 'manage_options', 'friendlycase_options', 'friendlycaseoptions_do_page');
}
function friendlycaseoptions_do_page() {
?>
<div id="fc-wrap">
<h2>FriendlyCase Settings</h2>
<h3>Which words in the titles should we change and how?</h3>
<p>Use a comma separated list in the sections below. <em><strong>Note:</strong> A word may be used in only one section at a time.</em></p>
<p>Checking the "Wrap FriendlyCased words in CSS Spans" option will wrap your words in FriendlyCase CSS Class spans.<br />
You can then add these classes to your theme stylesheet.</p>
<p>Checking the "Make only first word Uppercase" will make the Titles become "Sentence cased".</p>
<div id="fc-table">
<form method="post" action="options.php">
<?php settings_fields('friendlycase_options'); ?>
<?php
$ourDefaultData[fc_text_lc] = "a, am, an, and, at, for, of, in, is, on, or, the, to, with";
$ourDefaultData[fc_text_uc] = "CIA, FBI, NSA";
$ourDefaultData[fc_text_ig] = "FAQs, FriendlyCase, WordPress";
$ourDefaultData[fc_wrappit] = "1";
$ourDefaultData[fc_firstonly] = "0";
?>
<?php $options = get_option('fc_options', $ourDefaultData); ?>
<table class="form-table">
<tr valign="top"><th scope="row" class="fc-row">Words to always <strong>make lowercase</strong>:<br />
<span class="description">CSS Class name: <code>fc_lc</code></span></th>
<td><textarea name="fc_options[fc_text_lc]" class="fc-textarea" rows="4" wrap='on'><?php echo $options['fc_text_lc']; ?></textarea></td>
</tr>
<tr valign="top" ><th scope="row" class="fc-row">Words to always <strong>make UPPERCASE</strong>:<br />
<span class="description">CSS Class name: <code>fc_uc</code></span></th>
<td><textarea name="fc_options[fc_text_uc]" class="fc-textarea" rows="4" wrap='on'><?php echo $options['fc_text_uc']; ?></textarea></td>
</tr>
<tr valign="top"><th scope="row" class="fc-row">Words to always <strong>leave as typed</strong>:<br />
<span class="description">CSS Class name: <code>fc_ig</code></span></th>
<td><textarea name="fc_options[fc_text_ig]" class="fc-textarea" rows="4" wrap='on'><?php echo $options['fc_text_ig']; ?></textarea></td>
</tr>
<tr valign="top"><th scope="row" class="fc-row">Wrap FriendlyCased words in CSS spans:</th>
<td><input type="checkbox" name="fc_options[fc_wrappit]" value="1" <?php checked('1', $options['fc_wrappit']); ?> /></td>
</tr>
<tr valign="top"><th scope="row" class="fc-row">Make only first word Uppercase<br />(Also known as Sentence case):</th>
<td><input type="checkbox" name="fc_options[fc_firstonly]" value="1" <?php checked('1', $options['fc_firstonly']); ?> /></td>
</tr>
</table>
</div>
<p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /></p>
</form>
</div>
<style type="text/css">
#fc-table {border: 1px solid #DFDFDF; border-radius: 3px; width: 90%; padding: 20px;}
#fc-wrap p { padding-top:5px;}
th.fc-row {width: 250px;}
.fc-textarea {width: 350px;}
</style>
<?php
}
function friendlycaseoptions_validate($input) {
$input['fc_text_lc'] = wp_filter_nohtml_kses($input['fc_text_lc']);
$input['fc_text_uc'] = wp_filter_nohtml_kses($input['fc_text_uc']);
$input['fc_text_ig'] = wp_filter_nohtml_kses($input['fc_text_ig']);
$input['fc_wrappit'] = wp_filter_nohtml_kses($input['fc_wrappit']);
$input['fc_firstonly'] = wp_filter_nohtml_kses($input['fc_firstonly']);
return $input;
}
?>