forked from crowdfavorite/wp-favepersonal
-
Notifications
You must be signed in to change notification settings - Fork 12
/
functions.php
507 lines (440 loc) · 14.3 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?php
/**
* @package favepersonal
*
* This file is part of the Personal Theme for WordPress
* http://github.com/alexkingorg/wp-personal
* (Forked from http://crowdfavorite.com/favepersonal/)
*
* Copyright (c) 2008-2013 Crowd Favorite, Ltd. All rights reserved.
* http://crowdfavorite.com
*
* Copyright (c) 2015 Alex King.
*
* **********************************************************************
* 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('favepersonal');
define('CFCT_PATH', trailingslashit(TEMPLATEPATH));
/**
* Set this to "true" to turn on debugging mode.
* Debug helps with development by showing you the paths of the files loaded by Carrington.
*/
define('CFCT_DEBUG', false);
/**
* In production mode, or doing development?
* When true, assets/load.php will enqueue the built versions of the files
*/
define('CFCT_PRODUCTION', true);
/**
* Theme version.
*/
define('CFCT_THEME_VERSION', '2.0.2');
/**
* Theme URL version.
* Added to query var at the end of assets to force browser cache to reload after upgrade.
*/
if (!(defined('CFCT_URL_VERSION'))) {
define('CFCT_URL_VERSION', '7');
}
/**
* Define Header Text Color, even though it isn't used in the theme, need to define to prevent Notices
*/
if (!(defined('HEADER_TEXTCOLOR'))) {
define('HEADER_TEXTCOLOR', '');
}
/**
* CF Colors.
*/
define('CF_KULER_API_KEY', 'UNUSED');
/**
* Includes
*/
include_once(CFCT_PATH.'carrington-core/carrington.php');
include_once(CFCT_PATH.'functions/sidebars.php');
include_once(CFCT_PATH.'functions/gallery.php');
include_once(CFCT_PATH.'functions/about/about.php');
include_once(CFCT_PATH.'functions/header/header.php');
include_once(CFCT_PATH.'functions/patch-nav-menu.php');
include_once(CFCT_PATH.'functions/admin.php');
function cfcp_load_social() {
if (class_exists('Social')) {
// load filters for Social
add_filter('social_plugins_url', 'cfcp_social_plugins_url');
add_filter('social_plugins_path', 'cfcp_social_plugins_path');
add_filter('social_items_comment_avatar_size', 'cfcp_social_items_comment_avatar_size');
add_action('set_current_user', array('Social', 'social_loaded_by_theme'));
}
}
add_action('after_setup_theme', 'cfcp_load_social');
function cfcp_cfpf_base_url($url) {
return get_template_directory_uri().'/plugins/cf-post-formats/';
}
add_filter('cfpf_base_url', 'cfcp_cfpf_base_url');
/**
* Theme Setup
*/
if ( ! function_exists( 'carrington_personal_setup' ) ) {
function carrington_personal_setup() {
// Add default posts and comments RSS feed links to head
add_theme_support( 'automatic-feed-links' );
// This theme uses post thumbnails
add_theme_support( 'post-thumbnails' );
// New image sizes that are not overwrote in the admin
add_image_size('tiny-img', 80, 60, true); // thumbnails for full gallery
add_image_size('thumb-img', 160, 120, true); // gallery excerpt
add_image_size('small-img', 310, 180, true); // masthead featured img, bio box, image archive
add_image_size('medium-img', 510, 510, false); // excerpt image-format
add_image_size('large-img', 710, 700, false); // single view for image
add_image_size('gallery-large-img', 710, 474, false); // large size for gallery (~3:2 aspect ratio)
add_image_size('banner-img', 510, 180, true); // excerpt featured img
add_image_size('image-archive', 480, 280, true); // image archive
// set primary content width
global $content_width;
$content_width = '510';
// set default gallery dimensions
define('CFCT_GALLERY_HEIGHT', 474);
define('CFCT_GALLERY_WIDTH', 710);
// Add post formats
add_theme_support(
'post-formats',
array(
'status',
'link',
'image',
'gallery',
'video',
'quote',
)
);
add_theme_support(
'structured-post-formats',
array(
'status',
'link',
'image',
'gallery',
'video',
'quote',
)
);
register_nav_menu( 'main', __( 'Main Navigation', 'favepersonal'));
// No CSS, just IMG call. The %s is a placeholder for the theme template directory URI.
define( 'HEADER_IMAGE', '%s/functions/header/img/default.png' );
define( 'HEADER_IMAGE_WIDTH', apply_filters( 'cfcp_header_image_width', 990 ) );
define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'cfcp_header_image_height', 240 ) );
set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );
// Don't support text inside the header image.
define( 'NO_HEADER_TEXT', true );
add_theme_support( 'custom-header', array('admin-head-callback' => 'cfcp_admin_header_style'));
$patch_nav = new CF_Patch_Nav_Menu();
$patch_nav->attach_hooks();
/**
* Remove Carrington-added rev attribute -- now deprecated in HTML5
*/
remove_filter('comments_popup_link_attributes', 'cfct_ajax_comment_link');
}
}
add_action( 'after_setup_theme', 'carrington_personal_setup' );
function cfcp_image_size_input_fields_sizes($sizes) {
$sizes['medium-img'] = 'Content Width';
$sizes['banner-img'] = 'Content Width Short (Cropped)';
$sizes['large-img'] = 'Full Width';
return $sizes;
}
add_filter('image_size_names_choose', 'cfcp_image_size_input_fields_sizes');
function cfcp_admin_header_style() {} // empty.
/**
* Load assets at action 'wp', when conditionals like is_single are available.
*/
function cfct_load_assets() {
include_once(CFCT_PATH.'assets/load.php');
}
/**
* Load assets at action 'init' for admin, since wp fires inconsistently on admin side
*/
function cfct_load_admin_assets() {
include_once(CFCT_PATH.'assets/load-admin.php');
}
if (!is_admin()) {
add_action('wp', 'cfct_load_assets');
}
else {
add_action('init', 'cfct_load_admin_assets');
}
// Dequeue Social Plugin Stylesheet
function cfcp_social_dequeue_style() {
wp_dequeue_style('social_comments');
}
add_action('wp_enqueue_scripts', 'cfcp_social_dequeue_style', 999);
/**
* Misc other things we want added to the <head>
*/
function cfcp_head_extra() {
echo '<link rel="pingback" href="'.get_bloginfo('pingback_url').'" />';
}
add_action('wp_head', 'cfcp_head_extra');
// feed permalink for link posts
function cfcp_the_permalink_rss($url) {
global $post;
if (has_post_format('link', $post)) {
$link = get_post_meta($post->ID, '_format_link_url', true);
if (!empty($link)) {
$url = $link;
}
}
return $url;
}
add_filter('the_permalink_rss', 'cfcp_the_permalink_rss');
// Convert color to RGB so we can use background opacity
// source - http://css-tricks.com/snippets/php/convert-hex-to-rgb/
function hex2rgb($color) {
if ($color[0] == '#') {
$color = substr($color, 1);
}
if (strlen($color) == 6) {
list($r, $g, $b) = array($color[0].$color[1], $color[2].$color[3], $color[4].$color[5]);
}
else if (strlen($color) == 3) {
list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
}
else {
return false;
}
$r = hexdec($r);
$g = hexdec($g);
$b = hexdec($b);
return array($r, $g, $b);
}
function echo_hex($color2hex) {
$color_array = hex2rgb($color2hex);
for ($i = 0; $i < 3; $i++) {
if ($i == 2) {
echo $color_array[$i];
}
else {
echo $color_array[$i].',';
}
}
}
// Colors child theme export
function cfcp_child_theme_export() {
if (current_user_can('edit_theme_options') && isset($_GET['cf_action']) && $_GET['cf_action'] == 'cfcp_child_theme_export') {
$settings = cf_colors_get_settings();
if (!isset($settings['theme'])) {
wp_die('Sorry, an error occured.');
}
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename=style.css');
header('Content-Type: application/zip');
header('Content-type: text-css');
echo '/*
Theme Name: FavePersonal Child Theme ('.$settings['theme']['title'].')
Description: Child theme for FavePersonal (based on <a href="'.$settings['theme']['link'].'">'.$settings['theme']['title'].'</a> by '.$settings['theme']['author'].')
Author: '.get_bloginfo('name').'
Author URI: '.home_url().'
Template: favepersonal
Version: 1.0
*/
/* Add your custom CSS styles here */
/* Your selected colors are below */
';
echo cfcp_color_css()."\n\n";
die();
}
}
add_action('wp', 'cfcp_child_theme_export', 9999);
// Replaces "[...]" with something more pretty
function cfcp_excerpt_more($more) {
return '…';
}
add_filter( 'excerpt_more', 'cfcp_excerpt_more' );
// make status posts clickable if they include URLs
function cfcp_make_clickable($content) {
global $post;
if (has_post_format('status', $post)) {
$content = make_clickable($content);
}
return $content;
}
add_filter('the_content', 'cfcp_make_clickable');
// Common Relative date formatting. Uses plugins/cf-compat/
function cfcp_date() {
global $post;
$date_format = get_option('date_format');
// Preview?
if (isset($_GET['preview'])) {
return date($date_format, current_time('timestamp'));
}
else {
return cf_relative_time_ago($post->post_date_gmt, '', 'ago', '4', $date_format, '', true);
}
}
function cfcp_comment_date() {
global $comment;
$date_format = get_option('date_format');
return cf_relative_time_ago($comment->comment_date_gmt, '', 'ago', '4', $date_format, '', true);
}
// admin utility
function cfcp_get_popover_html($popover_id, $params = array()) {
$html = $class = '';
if (!empty($params['html'])) {
$html = $params['html'];
}
if (!empty($params['class'])) {
$class = esc_attr($params['class']);
}
$arrow_pos = 'center';
if (!empty($params['arrow_pos'])) {
$arrow_pos = esc_attr($params['arrow_pos']);
}
$display = 'none';
if (!empty($params['display'])) {
$display = esc_attr($params['display']);
}
return cfct_template_content(
'misc',
'admin-popover-template',
compact('popover_id', 'html', 'arrow_pos', 'class', 'display')
);
}
function cfcp_social_plugins_url($url) {
$url = trailingslashit(get_template_directory_uri());
return trailingslashit($url.'plugins/social');
}
function cfcp_social_plugins_path($path) {
$path = trailingslashit(get_template_directory());
return trailingslashit($path.'plugins/social');
}
function cfcp_social_items_comment_avatar_size($avatar_size) {
$avatar_size['height'] = $avatar_size['width'] = 24;
return $avatar_size;
}
/**
* Build simple relative dates
* Doesn't go too deep in to specificity as that is rarely needed
*
* @author http://snipplr.com/view/4912/relative-time/
* @param string $date - date to evaluate
* @param string $pre - default 'about' - what to put before the time output
* @param string $post - default 'ago' - what to put after the time output
* @param int $full_date_cutoff - default 4, how old a date should be until it gets formatted as a date string
* @param string $format - format for date output past 4 weeks
* @param string $pre_format - default '' - what to put before the date out past 4 weeks
* @return string
*/
if (!function_exists('cf_relative_time_ago')) {
function cf_relative_time_ago($date, $pre = 'about', $post = 'ago' , $full_date_cutoff = 4, $format='F j, Y', $pre_format, $gmt = false) {
$pre .= ' ';
$post = ' '.$post;
$pre_format = ' ';
if ($gmt) {
$now = gmmktime();
}
else {
$orig_tz = date_default_timezone_get();
date_default_timezone_set(get_option('timezone_string'));
$now = time();
}
if (!is_numeric($date)) {
$date = strtotime($date);
}
// seconds
$diff = $now - $date;
if ($diff < 60){
return sprintf('%1$s%2$s%3$s', $pre, sprintf(
_n('%d second', '%d seconds', $diff), $diff), $post);
}
// minutes
$diff = round($diff/60);
if ($diff < 60) {
return sprintf('%1$s%2$s%3$s', $pre, sprintf(
_n('%d minute', '%d minutes', $diff), $diff), $post);
}
// hours
$diff = round($diff/60);
if ($diff < 24) {
return sprintf('%1$s%2$s%3$s', $pre, sprintf(
_n('%d hour', '%d hours', $diff), $diff), $post);
}
// days
$diff = round($diff/24);
if ($diff < 7) {
return sprintf('%1$s%2$s%3$s', $pre, sprintf(
_n('%d day', '%d days', $diff), $diff), $post);
}
// weeks
$diff = round($diff/7);
if ($diff <= $full_date_cutoff) {
return sprintf('%1$s%2$s%3$s', $pre, sprintf(
_n('%d week', '%d weeks', $diff), $diff), $post);
}
// actual date string if farther than 4 weeks ago
$ago = $pre_format . mysql2date($format, date('Y-m-d H:i:s', $date));
if (!$gmt) {
date_default_timezone_set($orig_tz);
}
return $ago;
}
} // end exists check
/**
* Function for trimming text. This function takes text and length as an input, and returns
* the text truncated to the nearest word if the length of the text is longer than the length
*
* @param string $text - (required) Text to truncate
* @param string $length - Length of the truncated string to return
* @return $text - Truncated text being returned
*/
if (!function_exists('cf_trim_text')) {
function cf_trim_text($text, $length = 250, $before = '', $after = '') {
// If the text field is empty or is shorter than the $length, there is no need to make it smaller
/* Since servers must have MB module installed for mb_* functions, we're keeping the fallback to non-multibyte functions */
if (function_exists('mb_strlen')) { //
if (empty($text) || mb_strlen($text) <= $length) {
return $text;
}
if (mb_strlen($text) > $length) {
$text = mb_substr($text, 0, $length); // cut string to proper length
if (mb_strrpos($text, ' ')) { // if we have spaces in text, cut to the last word, not letter
$text = mb_substr($text, 0, mb_strrpos($text, ' '));
}
}
}
else {
if (empty($text) || strlen($text) <= $length) {
return $text;
}
if (strlen($text) > $length) {
$text = substr($text, 0, $length); // cut string to proper length
if (strrpos($text, ' ')) { // if we have spaces in text, cut to the last word, not letter
$text = substr($text, 0, strrpos($text, ' '));
}
}
}
// trim trailing dot if we are appending dots
if (substr($text, -1) == '.' && (substr($after, 0, 8) == '…' || substr($after, 0, 1) == '.')) {
$text = substr($text, 0, strlen($text) - 1);
}
return $before.$text.$after;
}
} // end exists check
function cfcp_add_taxonomy_tamplates ( $order ) {
$exec_order = array(
'author',
'role',
'category',
'taxonomy',
'tag',
'type',
'taxonomy',
'single',
'default'
);
return $order;
}
//add_filter('cfct_general_match_order', 'cfcp_add_taxonomy_tamplates');