-
Notifications
You must be signed in to change notification settings - Fork 53
/
functions.php
566 lines (490 loc) · 16.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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<?php
/**
* Functions
*
* @package Genesis CRM
* @author Bill Erickson <[email protected]>
* @copyright Copyright (c) 2011, Bill Erickson
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*
*/
/**
* Theme Setup
*
* This setup function attaches all of the site-wide functions
* to the correct hooks and filters. All the functions themselves
* are defined below this setup function.
*
*/
add_action('genesis_setup','child_theme_setup', 15);
function child_theme_setup() {
// ** Backend **
// Remove Unused Menu Items
add_action('admin_menu', 'be_remove_menus');
// Customize Menu Order
add_filter( 'custom_menu_order', 'be_custom_menu_order' );
add_filter( 'menu_order', 'be_custom_menu_order' );
// Set up Taxonomies
add_action( 'init', 'be_create_my_taxonomies' );
// Set up Taxonomy Default Terms
add_action( 'save_post', 'mfields_set_default_object_terms', 100, 2 );
// Set up Meta Boxes
add_action( 'init' , 'be_create_metaboxes' );
// Setup Sidebars
genesis_register_sidebar(array('name' => 'Home Column 1', 'id' => 'home-column-1'));
genesis_register_sidebar(array('name' => 'Home Column 2', 'id' => 'home-column-2'));
genesis_register_sidebar(array('name' => 'Home Column 3', 'id' => 'home-column-3'));
// Setup Default Layout
genesis_set_default_layout( 'full-width-content' );
// Setup Widgets
include_once( 'lib/widgets/widget-old-prospects.php' );
include_once( 'lib/widgets/widget-new-prospects.php' );
include_once( 'lib/widgets/widget-active-projects.php' );
include_once( 'lib/widgets/widget-other-stats.php' );
include_once( 'lib/widgets/widget-poc.php' );
include_once( 'lib/widgets/widget-inquiry.php' );
include_once( 'lib/widgets/widget-inquiry-result.php' );
include_once( 'lib/widgets/widget-forwarded.php' );
include_once( 'lib/widgets/widget-project-sources.php' );
include_once( 'lib/widgets/widget-referral.php' );
include_once( 'lib/widgets/widget-activity-graph.php' );
include_once( 'lib/widgets/widget-quotes.php' );
// Move Post Editor to Metabox
add_action( 'admin_enqueue_scripts', 'crm_move_posteditor', 10, 1 );
// Don't update theme
add_filter( 'http_request_args', 'be_dont_update_theme', 5, 2 );
// Change the labeling for the "Posts" menu to "Contacts"
add_action( 'init', 'crm_change_post_object_label' );
add_action( 'admin_menu', 'crm_change_post_menu_label' );
// Change post title text
add_action( 'gettext', 'crm_change_title_text' );
// Modify post column layout
add_filter( 'manage_posts_columns', 'crm_add_new_columns' );
// Add taxonomies to post column
add_action( 'manage_posts_custom_column', 'crm_manage_columns', 10, 2 );
// Remove post meta fields
add_action( 'admin_menu' , 'crm_remove_page_fields' );
// Remove Genesis SEO Metabox
remove_post_type_support( 'post', 'genesis-seo' );
// Remove Genesis Layout Options
remove_post_type_support( 'post', 'genesis-layouts' );
// ** Frontend **
// Exclude Form from login
add_filter('registered-users-only_exclusions', 'crm_form_exclusion');
// Remove Footer
remove_action('genesis_footer', 'genesis_do_footer');
}
// ** Backend Functions ** //
/**
* Remove Menu Items
*
* Remove unused menu items by adding them to the array.
* See the commented list of menu items for reference.
*
* @author Bill Erickson
* @link https://github.com/billerickson/BE-Genesis-Child
*/
function be_remove_menus () {
global $menu;
$restricted = array(__('Links'));
// Example:
//$restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}
}
/**
* Customize Menu Order
*
* @param $menu_ord. Current order.
* @return $menu_ord. New order.
*
* @author Bill Erickson
* @link https://github.com/billerickson/BE-Genesis-Child
*/
function be_custom_menu_order( $menu_ord ) {
if ( !$menu_ord ) return true;
return array(
'index.php', // this represents the dashboard link
'edit.php?post_type=page', //the page tab
'edit.php', //the posts tab
'edit-comments.php', // the comments tab
'upload.php', // the media manager
);
}
/**
* Create Taxonomies
*
* @author Bill Erickson
* @link http://codex.wordpress.org/Function_Reference/register_taxonomy
*
*/
function be_create_my_taxonomies() {
register_taxonomy(
'poc',
'post',
array(
'hierarchical' => true,
'labels' => array(
'name' => 'Points of Contact',
'singlular_name' => 'Point of Contact'
),
'query_var' => true,
'rewrite' => true
)
);
register_taxonomy(
'sources',
'post',
array(
'hierarchical' => true,
'labels' => array(
'name' => 'Source',
'singlular_name' => 'Source'
),
'query_var' => true,
'rewrite' => true
)
);
}
/**
* Define default terms for custom taxonomies in WordPress 3.0.1
*
* @author Michael Fields http://wordpress.mfields.org/
* @link http://wordpress.mfields.org/2010/set-default-terms-for-your-custom-taxonomies-in-wordpress-3-0/
*/
function mfields_set_default_object_terms( $post_id, $post ) {
if ( 'publish' === $post->post_status ) {
$defaults = array(
'poc' => array( 'email-form' ),
);
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
}
}
}
}
/**
* Create Metaboxes
*
* @author Andrew Norcross, Jared Atchison, Bill Erickson
* @link http://www.billerickson.net/wordpress-metaboxes/
*/
include_once( 'lib/functions/create-metaboxes.php');
/**
* Move Post Editor
*
* @author Jared Atchison
* @link http://www.billerickson.net/twentyten-crm/
*/
function crm_move_posteditor( $hook ) {
if ( $hook == 'post.php' OR $hook == 'post-new.php' ) {
wp_enqueue_script( 'jquery' );
add_action('admin_print_footer_scripts','crm_move_posteditor_scripts');
}
}
function crm_move_posteditor_scripts() {
?>
<script type="text/javascript">
jQuery('#postdiv, #postdivrich').prependTo('#crm_notes .inside');
</script>
<style type="text/css">
#normal-sortables {margin-top: 20px;}
#titlediv { margin-bottom: 0px; }
#postdiv.postarea, #postdivrich.postarea { margin:0; }
#post-status-info { line-height:1.4em; font-size:13px; }
#custom_editor .inside { margin:2px 6px 6px 6px; }
#ed_toolbar { display:none; }
#postdiv #ed_toolbar, #postdivrich #ed_toolbar { display:block; }
</style>
<?php
}
/**
* Don't Update Theme
* If there is a theme in the repo with the same name,
* this prevents WP from prompting an update.
*
* @author Mark Jaquith
* @link http://markjaquith.wordpress.com/2009/12/14/excluding-your-plugin-or-theme-from-update-checks/
*
*/
function be_dont_update_theme( $r, $url ) {
if ( 0 !== strpos( $url, 'http://api.wordpress.org/themes/update-check' ) )
return $r; // Not a theme update request. Bail immediately.
$themes = unserialize( $r['body']['themes'] );
unset( $themes[ get_option( 'template' ) ] );
unset( $themes[ get_option( 'stylesheet' ) ] );
$r['body']['themes'] = serialize( $themes );
return $r;
}
/**
* Change Posts to Contacts
*
* @author Andrew Norcross
* @link http://www.billerickson.net/twentyten-crm/
*/
function crm_change_post_menu_label() {
global $menu;
global $submenu;
$menu[5][0] = 'Contacts';
$menu[10][0] = 'Files';
$submenu['edit.php'][5][0] = 'Contacts';
$submenu['edit.php'][10][0] = 'Add Contacts';
$submenu['edit.php'][15][0] = 'Status';
echo '';
}
function crm_change_post_object_label() {
global $wp_post_types;
$labels = &$wp_post_types['post']->labels;
$labels->name = 'Contacts';
$labels->singular_name = 'Contact';
$labels->add_new = 'Add Contact';
$labels->add_new_item = 'Add Contact';
$labels->edit_item = 'Edit Contacts';
$labels->new_item = 'Contact';
$labels->view_item = 'View Contact';
$labels->search_items = 'Search Contacts';
$labels->not_found = 'No Contacts found';
$labels->not_found_in_trash = 'No Contacts found in Trash';
}
/**
* Change post title text
*
* @author Andrew Norcross
* @link http://www.billerickson.net/twentyten-crm/
*/
function crm_change_title_text( $translation ) {
global $post;
if( isset( $post ) ) {
switch( $post->post_type ){
case 'post' :
if( $translation == 'Enter title here' ) return 'Enter Contact Name Here';
break;
}
}
return $translation;
}
/**
* Modify post column layout
*
* @author Andrew Norcross
* @link http://www.billerickson.net/twentyten-crm/
*/
function crm_add_new_columns( $crm_columns ) {
$new_columns['cb'] = '<input type="checkbox" />';
$new_columns['title'] = _x('Contact Name', 'column name');
$new_columns['status'] = __('Status');
$new_columns['poc'] = __('Point Of Contact');
$new_columns['source'] = __('Source');
$new_columns['date'] = _x('Date', 'column name');
return $new_columns;
}
/**
* Add taxonomies to post column
*
* @author Andrew Norcross
* @link http://www.billerickson.net/twentyten-crm/
*/
function crm_manage_columns ($column_name, $id ) {
global $post;
switch ($column_name) {
case 'status':
$category = get_the_category();
echo $category[0]->cat_name;
break;
case 'poc':
echo get_the_term_list( $post->ID, 'poc', '', ', ', '');
break;
case 'source':
echo get_the_term_list( $post->ID, 'sources', '', ', ', '');
break;
default:
break;
} // end switch
}
/**
* Remove post meta fields
*
* @author Andrew Norcross
* @link http://www.billerickson.net/twentyten-crm/
*/
function crm_remove_page_fields() {
remove_meta_box( 'commentstatusdiv' , 'post' , 'normal' ); //removes comments status
remove_meta_box( 'commentsdiv' , 'post' , 'normal' ); //removes comments
remove_meta_box( 'postexcerpt' , 'post' , 'normal' );
remove_meta_box( 'trackbacksdiv' , 'post' , 'normal' );
remove_meta_box( 'authordiv' , 'post' , 'normal' );
remove_meta_box( 'postcustom' , 'post' , 'normal' );
remove_meta_box( 'revisionsdiv' , 'post' , 'normal' );
remove_meta_box( 'tagsdiv-post_tag', 'post', 'normal' );
}
/**
* Use Genesis Layout Metabox only on pages
* Submitted patch to Genesis (#190) so this can be replaced with
* remove_post_type_support('post', 'genesis-layouts');
*
* @author Bill Erickson
* @props Thomas Griffin
*/
function be_genesis_add_inpost_layout_box() {
if ( !current_theme_supports('genesis-inpost-layouts') )
return;
foreach ( (array)get_post_types( array( 'public' => true ) ) as $type ) {
if ( post_type_supports( $type, 'genesis-layouts' ) || $type = 'page' ) {
add_meta_box('genesis_inpost_layout_box', __('Genesis Layout Settings', 'genesis'), 'genesis_inpost_layout_box', $type, 'normal', 'high');
}
}
}
// ** Frontend Functions ** //
/**
* Exclude Form from login
*
* @author Bill Erickson
* @link http://www.billerickson.net/contact-form-to-wordpress-crm/
*/
function crm_form_exclusion($exclusion) {
$exclusion[] = 'form';
$exclusion[] = 'contact-form';
return $exclusion;
}
/**
* Disable RSS Feed
*
* @author Frank Bultge
* @link http://wpengineer.com/287/disable-wordpress-feed/
*/
// ** Helper Functions ** //
/**
* Percent Function
*
* @author Bill Erickson
* @link http://www.billerickson.net/twentyten-crm/
*/
function crm_percent($num_amount, $num_total) {
$count1 = $num_amount / $num_total;
$count2 = $count1 * 100;
$count = number_format($count2, 0);
return $count.'%';
}
/**
* Get Post Meta Shorthand
* Carryover from TwentyTen CRM. You could also use genesis_get_custom_field().
*
* @author Bill Erickson
* @link http://www.billerickson.net/twentyten-crm/
*/
function get_custom_field($field) {
global $post;
$value = get_post_meta($post->ID, $field, true);
if ($value) return esc_attr( $value );
else return false;
}
/**
* Project Name
*
* @author Bill Erickson
* @link http://www.billerickson.net/twentyten-crm/
*/
function be_get_project_name() {
global $prefix;
$project_name = get_custom_field($prefix.'project_name');
if (!empty($project_name)) $name = $project_name .' ('.get_the_title().')';
else $name = get_the_title();
return $name;
}
if( !function_exists( 'money_format' ) ) {
/*
That it is an implementation of the function money_format for the
platforms that do not it bear.
The function accepts to same string of format accepts for the
original function of the PHP.
(Sorry. my writing in English is very bad)
The function is tested using PHP 5.1.4 in Windows XP
and Apache WebServer.
*/
function money_format($format, $number)
{
$regex = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?'.
'(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/';
if (setlocale(LC_MONETARY, 0) == 'C') {
setlocale(LC_MONETARY, '');
}
$locale = localeconv();
preg_match_all($regex, $format, $matches, PREG_SET_ORDER);
foreach ($matches as $fmatch) {
$value = floatval($number);
$flags = array(
'fillchar' => preg_match('/\=(.)/', $fmatch[1], $match) ?
$match[1] : ' ',
'nogroup' => preg_match('/\^/', $fmatch[1]) > 0,
'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ?
$match[0] : '+',
'nosimbol' => preg_match('/\!/', $fmatch[1]) > 0,
'isleft' => preg_match('/\-/', $fmatch[1]) > 0
);
$width = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
$left = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
$right = trim($fmatch[4]) ? (int)$fmatch[4] : $locale['int_frac_digits'];
$conversion = $fmatch[5];
$positive = true;
if ($value < 0) {
$positive = false;
$value *= -1;
}
$letter = $positive ? 'p' : 'n';
$prefix = $suffix = $cprefix = $csuffix = $signal = '';
$signal = $positive ? $locale['positive_sign'] : $locale['negative_sign'];
switch (true) {
case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+':
$prefix = $signal;
break;
case $locale["{$letter}_sign_posn"] == 2 && $flags['usesignal'] == '+':
$suffix = $signal;
break;
case $locale["{$letter}_sign_posn"] == 3 && $flags['usesignal'] == '+':
$cprefix = $signal;
break;
case $locale["{$letter}_sign_posn"] == 4 && $flags['usesignal'] == '+':
$csuffix = $signal;
break;
case $flags['usesignal'] == '(':
case $locale["{$letter}_sign_posn"] == 0:
$prefix = '(';
$suffix = ')';
break;
}
if (!$flags['nosimbol']) {
$currency = $cprefix .
($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol']) .
$csuffix;
} else {
$currency = '';
}
$space = $locale["{$letter}_sep_by_space"] ? ' ' : '';
$value = number_format($value, $right, $locale['mon_decimal_point'],
$flags['nogroup'] ? '' : $locale['mon_thousands_sep']);
$value = @explode($locale['mon_decimal_point'], $value);
$n = strlen($prefix) + strlen($currency) + strlen($value[0]);
if ($left > 0 && $left > $n) {
$value[0] = str_repeat($flags['fillchar'], $left - $n) . $value[0];
}
$value = implode($locale['mon_decimal_point'], $value);
if ($locale["{$letter}_cs_precedes"]) {
$value = $prefix . $currency . $space . $value . $suffix;
} else {
$value = $prefix . $value . $space . $currency . $suffix;
}
if ($width > 0) {
$value = str_pad($value, $width, $flags['fillchar'], $flags['isleft'] ?
STR_PAD_RIGHT : STR_PAD_LEFT);
}
$format = str_replace($fmatch[0], $value, $format);
}
return $format;
}
}