-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
399 lines (346 loc) · 12.2 KB
/
functions.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
<?php
// This file is part of the Carrington Text Theme for WordPress
// http://carringtontheme.com
//
// Copyright (c) 2008-2010 Crowd Favorite, Ltd. All rights reserved.
// http://crowdfavorite.com
//
// Released under the GPL license
// http://www.opensource.org/licenses/gpl-license.php
//
// **********************************************************************
// 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.
// **********************************************************************
if (__FILE__ == $_SERVER['SCRIPT_FILENAME']) { die(); }
load_theme_textdomain('carrington-text');
define('CFCT_DEBUG', false);
define('CFCT_PATH', trailingslashit(TEMPLATEPATH));
include_once(CFCT_PATH.'functions/admin.php');
include_once(CFCT_PATH.'functions/sidebars.php');
include_once(CFCT_PATH.'carrington-core/carrington.php');
$cfct_options = array(
'cfct_ajax_load',
'cfct_lightbox',
'cfct_posts_per_archive_page',
'cfct_text_custom_colors',
'cfct_text_custom_header_image',
);
$cfct_color_options = array(
'cfct_header_background_color' => '51555c',
'cfct_header_text_color' => 'cecfd1',
'cfct_header_link_color' => 'ffffff',
'cfct_header_nav_background_color' => 'e9eaea',
'cfct_header_nav_link_color' => 'a00004',
'cfct_header_nav_text_color' => '51555c',
'cfct_page_title_color' => '51555c',
'cfct_page_subtitle_color' => '51555c',
'cfct_link_color' => 'a00004',
'cfct_footer_background_color' => '51555c',
'cfct_footer_text_color' => '999999',
'cfct_footer_link_color' => 'CECFD1',
);
foreach ($cfct_color_options as $k => $default) {
$cfct_options[] = $k;
}
function cfct_text_option_defaults($options) {
$options['cfct_ajax_load'] = 'yes';
$options['cfct_lightbox'] = 'yes';
$options['cfct_custom_colors'] = 'no';
$options['cfct_custom_header_image'] = 'no';
return $options;
}
add_filter('cfct_option_defaults', 'cfct_text_option_defaults');
function cfct_text_init() {
if (cfct_get_option('cfct_ajax_load') == 'yes') {
cfct_ajax_load();
}
if (cfct_get_option('cfct_lightbox') != 'no' && !is_admin()) {
wp_enqueue_script('cfct_thickbox', get_bloginfo('template_directory').'/carrington-core/lightbox/thickbox.js', array('jquery'), '1.0');
// in the future we'll use this, but for now we want 2.5 compatibility
// wp_enqueue_style('jquery-lightbox', get_bloginfo('template_directory').'/carrington-core/lightbox/css/lightbox.css');
}
}
add_action('init', 'cfct_text_init');
wp_enqueue_script('jquery');
wp_enqueue_script('carrington-text', get_bloginfo('template_directory').'/js/carrington-text.js', array('jquery'), '1.0');
// Filter comment reply link to work with namespaced comment-reply javascript.
add_filter('cancel_comment_reply_link', 'cfct_get_cancel_comment_reply_link', 10, 3);
function cfct_text_head() {
// see enqueued style in cfct_blog_init, we'll activate that in the future
if (cfct_get_option('cfct_lightbox') != 'no') {
echo '
<link rel="stylesheet" type="text/css" media="screen" href="'.get_bloginfo('template_directory').'/carrington-core/lightbox/css/thickbox.css" />
';
}
cfct_get_option('cfct_ajax_load') == 'no' ? $ajax_load = 'false' : $ajax_load = 'true';
echo '
<script type="text/javascript">
var CFCT_URL = "'.get_bloginfo('url').'";
var CFCT_AJAX_LOAD = '.$ajax_load.';
</script>
';
if (cfct_get_option('cfct_lightbox') != 'no') {
echo '
<script type="text/javascript">
tb_pathToImage = "' . get_bloginfo('template_directory') . '/carrington-core/lightbox/img/loadingAnimation.gif";
jQuery(function($) {
$("a.thickbox").each(function() {
var url = $(this).attr("rel");
var post_id = $(this).parents("div.post, div.page").attr("id");
$(this).attr("href", url).attr("rel", post_id);
});
});
</script>
';
}
// preview
if (isset($_GET['cfct_action']) && $_GET['cfct_action'] == 'custom_color_preview' && current_user_can('manage_options')) {
cfct_text_custom_colors('preview');
}
else if (cfct_get_option('cfct_text_custom_colors') == 'yes') {
cfct_text_custom_colors();
}
if (cfct_get_option('cfct_text_custom_header_image') == 'yes') {
$header_image = cfct_get_option('cfct_header_image');
if ($header_image != 0 && $img = wp_get_attachment_image_src($header_image, 'large')) {
?>
<style type="text/css">
#header .wrapper {
background-image: url(<?php echo $img[0]; ?>);
background-repeat: no-repeat;
height: <?php echo $img[2]; ?>px;
}
</style>
<?php
}
else {
?>
<style type="text/css">
#header .wrapper {
background-image: url();
}
</style>
<?php
}
}
}
if (!is_admin()) {
add_action('wp_head', 'cfct_text_head');
}
function cfct_text_custom_colors($type = 'option') {
$colors = cfct_get_custom_colors($type);
}
/**
* Start and end an output buffer at specific actions that you specify.
* Warning: this is a bit of a tricky thing to do, but it works in a pinch.
* Basically, don't use this unless you really have to.
*/
class CFCT_OB_On_Action {
protected $ob_started = false;
protected $hooks_attached = false;
protected $callback;
public function __construct($start_action, $end_action, $callback = null, $priority = 10) {
if ($callback === null) {
$callback = array($this, 'callback');
}
$this->start_action = $start_action;
$this->end_action = $end_action;
$this->callback = $callback;
$this->priority = $priority;
$this->attach_hooks();
}
/**
* If you specialize this class, you can customize this function to do something
* with the buffer. Otherwise, just pass your callback in using the $callback param in __construct().
*/
public function callback($buffer) {
// Do something...
}
/**
* Attach hooks for actions. Only runs once.
*/
protected function attach_hooks() {
if ($this->hooks_attached === true) {
return;
}
add_action($this->start_action, array($this, 'start'));
add_action($this->end_action, array($this, 'end'));
$this->hooks_attached = true;
}
protected function remove_hooks() {
remove_action($this->start_action, array($this, 'start'));
remove_action($this->end_action, array($this, 'end'));
}
public function start() {
ob_start($this->callback);
$this->ob_started = true;
}
public function end() {
/* Check if this function is running before $this->start() gets a chance to run.
This could happen if the end action passed runs before the start action. Under
no circumstances do you want that to happen. If it does, throw a traceable error
so we know what's going on. */
if ($this->ob_started !== true) {
throw new Exception("Uh-oh! Method end() ran before start(). This can happen if the end action you provided runs before the start action. Make sure you specify an end action that always runs after the start action.", 1);
}
ob_end_flush();
}
}
/**
* A collection of filters for comment_form().
* Usage: CFCT_Comment_Form::setup();
*/
class CFCT_Comment_Form {
public static $i18n = 'carrington-text';
protected static $instance;
protected static $hooks_attached = false;
/**
* Singleton factory method
* @return object instance of CFCT_Comment_Form
*/
public static function get_instance() {
if (!self::$instance) {
self::$instance = new CFCT_Comment_Form();
}
return self::$instance;
}
/**
* Convenient factory method that instantiates single instance and
* attaches hooks automatically.
* @return object instance of CFCT_Comment_Form
*/
public static function setup() {
$ins = self::get_instance();
$ins->attach_hooks();
return $ins;
}
/**
* Attach hooks to WordPress. Runs only once.
* @uses CFCT_OB_On_Action
*/
public function attach_hooks() {
if (self::$hooks_attached === true) {
return false;
}
add_action('comment_form_defaults', array($this, 'configure_args'));
add_filter('comment_id_fields', array($this, 'comment_id_fields'), 10, 3);
// Set up an output buffer so we can do string replacements on areas that aren't filterable.
new CFCT_OB_On_Action('comment_form_before', 'comment_form_after', array($this, 'ob_callback'));
self::$hooks_attached = true;
}
public function ob_callback($buffer) {
global $post;
$mod = 'p'.$post->ID;
$buffer = strtr($buffer, array(
'id="respond"' => 'id="respond-'.$mod.'"',
'id="commentform"' => 'class="comment-form"',
'id="reply-title"' => 'class="comment-form-title"',
'id="author"' => 'id="author-'.$mod.'"',
'for="author"' => 'for="author-'.$mod.'"',
'id="email"' => 'id="email-'.$mod.'"',
'for="email"' => 'for="email-'.$mod.'"',
'id="url"' => 'id="url-'.$mod.'"',
'for="url"' => 'for="url-'.$mod.'"',
'id="comment"' => 'id="comment-'.$mod.'"'
));
return $buffer;
}
public function comment_id_fields($result, $id, $replytoid) {
// Remove default WP comment ID fields
$result = '';
// Add trackback link, since this function renders right next to the submit button
$result .= ' <span class="comment-form-trackback">'.sprintf(__('or, reply to this post via <a rel="trackback" href="%s">trackback</a>.', self::$i18n), get_trackback_url()).'</span>';
// Add our customized comment ID fields
$result .= cfct_get_comment_id_fields($id, $replytoid);
return $result;
}
/**
* Attach to 'configure_args' hook
*/
public function configure_args($default_args) {
$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$author_help = ($req ? __('(required)', self::$i18n) : '');
$email_help = ($req ? __('(required, but never shared)', self::$i18n) : __('(never shared)', self::$i18n));
$fields = array(
'author' => self::to_input_block(__( 'Name', self::$i18n ), 'author', $commenter['comment_author'], $req, $author_help),
'email' => self::to_input_block(__( 'Email', self::$i18n ), 'email', $commenter['comment_author_email'], $req, $email_help),
'url' => self::to_input_block(__( 'Web', self::$i18n ), 'url', $commenter['comment_author_url'])
);
$textarea = self::to_tag('textarea', '', array(
'name' => 'comment',
'id' => 'comment',
'class' => 'comment',
'rows' => 6,
'cols' => 60,
'required' => 'required'
));
$comment_field = self::to_tag('p', $textarea, array('class' => 'comment-form-user-info tight'));
$html_tags = sprintf(__('You can use: %s', self::$i18n), esc_attr(allowed_tags()));
$args = array(
'fields' => $fields,
'comment_field' => $comment_field,
'label_submit' => __('Post Comment', self::$i18n),
'title_reply' => __('Leave a Reply', self::$i18n),
'title_reply_to' => __('Leave a Reply to %s', self::$i18n),
'cancel_reply_link' => __('Cancel reply', self::$i18n),
'comment_notes_after' => '<em class="some-html-is-ok"><abbr title="'.$html_tags.'">'.__('Some HTML is OK', 'carrington-text').'</abbr></em>',
'comment_notes_before' => ''
);
return array_merge($default_args, $args);
}
/**
* Helper: Turn an array or two into HTML attribute string
*/
public static function to_attr($arr1 = array(), $arr2 = array()) {
$attrs = array();
$arr = array_merge($arr1, $arr2);
foreach ($arr as $key => $value) {
if (function_exists('esc_attr')) {
$key = esc_attr($key);
$value = esc_attr($value);
}
$attrs[] = $key.'="'.$value.'"';
}
return implode(' ', $attrs);
}
/**
* Helper for creating HTML tag from strings and arrays of attributes.
*/
public static function to_tag($tag, $text = '', $attr1 = array(), $attr2 = array()) {
if (function_exists('esc_attr')) {
$tag = esc_attr($tag);
}
$attrs = self::to_attr($attr1, $attr2);
if ($text !== false) {
$tag = '<'.$tag.' '.$attrs.'>'.$text.'</'.$tag.'>';
}
// No text == self closing tag
else {
$tag = '<'.$tag.' '.$attrs.' />';
}
return $tag;
}
public static function to_input_block($label, $id, $value, $req = null, $help_text = '') {
$label = self::to_tag('label', $label, array('for' => $id));
$maybe_req = ($req ? array('required' => 'required') : array() );
$input_defaults = array(
'id' => $id,
'name' => $id,
'class' => 'type-text '.$id,
'value' => $value
);
$input = self::to_tag('input', false, $input_defaults, $maybe_req);
$help = '';
if ($help_text) {
$help = self::to_tag('small', $help_text, array('class' => 'help'));
}
return self::to_tag('p', $input . ' ' . $label . ' ' . $help, array(
'class' => 'comment-form-user-info tight'
));
}
}
CFCT_Comment_Form::setup();
?>