This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
helpers.php
675 lines (509 loc) · 14.5 KB
/
helpers.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
<?php
/**
* Custom helper functions for this theme.
*
* @package Functions
* @category Core
* @author GoDaddy
* @since 1.0.0
*/
/**
* Return a page title based on the current page.
*
* @since 1.0.0
*
* @return string Returns the current page title.
*/
function primer_get_the_page_title() {
$title = '';
$post = get_queried_object();
switch ( true ) {
case is_front_page() :
$title = ( 'posts' === get_option( 'show_on_front' ) ) ? get_theme_mod( 'front_page_title', '' ) : get_the_title( get_option( 'page_on_front' ) );
break;
case is_home() :
$title = get_the_title( get_option( 'page_for_posts' ) );
break;
case is_archive() :
$title = get_the_archive_title();
break;
case is_search() :
$title = sprintf(
/* translators: search term */
esc_html__( 'Search Results for: %s', 'primer' ),
sprintf(
'<span>%s</span>',
get_search_query()
)
);
break;
case is_404() :
$title = esc_html__( '404 Page Not Found', 'primer' );
break;
case is_page() :
$title = get_the_title();
break;
case ( ! is_post_type_hierarchical( get_post_type( $post ) ) ) :
$show_on_front = get_option( 'show_on_front' );
$page_for_posts = get_option( 'page_for_posts' );
if ( 'post' === $post->post_type && 'posts' !== $show_on_front && ! empty( $page_for_posts ) ) {
$title = get_the_title( $page_for_posts );
break;
}
$labels = get_post_type_labels( get_post_type_object( $post->post_type ) );
$title = isset( $labels->name ) ? $labels->name : false;
break;
} // End switch().
/**
* Filter the page title.
*
* @since 1.0.0
*
* @var string
*/
return (string) apply_filters( 'primer_the_page_title', $title );
}
/**
* Return the current page width setting.
*
* @since 1.0.0
*
* @return string Returns the current `page_width` theme mod.
*/
function primer_get_page_width() {
return get_theme_mod( 'page_width', 'fixed' );
}
/**
* Check if pages are being displayed with fluid width.
*
* @since 1.0.0
*
* @return bool Returns true when `primer_get_page_width()` is set to fluid, otherwise returns false.
*/
function primer_is_fluid_width() {
return ( 'fluid' === primer_get_page_width() );
}
/**
* Check if pages are being displayed with fixed width.
*
* @since 1.0.0
*
* @return bool Returns true when `primer_get_page_width()` is set to 'fixed', otherwise returns false.
*/
function primer_is_fixed_width() {
return ( 'fixed' === primer_get_page_width() );
}
/**
* Return the current layout.
*
* @global $primer_customizer_layouts
* @since 1.0.0
*
* @uses get_current_layout
*
* @param int $post_id (optional) Post ID. Defaults to the current queried object.
*
* @return string Returns the current Primer theme layout.
*/
function primer_get_layout( $post_id = null ) {
global $primer_customizer_layouts;
$post_id = ( $post_id ) ? $post_id : get_queried_object_id();
return $primer_customizer_layouts->get_current_layout( $post_id );
}
/**
* Return the global layout.
*
* @global Primer_Customizer_Layouts $primer_customizer_layouts
* @since 1.0.0
*
* @uses get_global_layout
*
* @return string Returns the global layout.
*/
function primer_get_global_layout() {
global $primer_customizer_layouts;
return $primer_customizer_layouts->get_global_layout();
}
/**
* Check if the current layout has a sidebar.
*
* @since 1.0.0
*
* @param string $layout (optional) Layout slug name.
*
* @return bool Returns true when a sidebar is set on the given layout, otherwise returns false.
*/
function primer_layout_has_sidebar( $layout = null ) {
$layout = ( $layout ) ? $layout : primer_get_layout();
$has_sidebar = ! in_array( $layout, array( 'one-column-wide', 'one-column-narrow' ), true );
/**
* Filter if the current layout has a sidebar.
*
* @since 1.0.0
*
* @param string $layout
*
* @var bool
*/
return (bool) apply_filters( 'primer_layout_has_sidebar', $has_sidebar, $layout );
}
/**
* Check if the site has a custom logo.
*
* @since 1.0.0
*
* @uses has_custom_logo
*
* @return bool Returns true when a Primer logo is set.
*/
function primer_has_custom_logo() {
/**
* For backwards compatibility prior to WordPress 4.5.
*
* @link https://developer.wordpress.org/reference/functions/has_custom_logo/
* @since 1.0.0
*/
$enabled = function_exists( 'has_custom_logo' ) ? has_custom_logo() : (bool) get_theme_mod( 'custom_logo' );
/**
* Filter if the site has a custom logo.
*
* @since 1.0.0
*
* @var bool
*/
return (bool) apply_filters( 'primer_has_custom_logo', $enabled );
}
/**
* Return the hero image element selector.
*
* @since 1.0.0
*
* @return string Returns the hero image selector.
*/
function primer_get_hero_image_selector() {
/**
* Filter the hero image element selector.
*
* @since 1.0.0
*
* @var string
*/
$selector = (string) apply_filters( 'primer_hero_image_selector', '.site-header' );
return wp_strip_all_tags( $selector );
}
/**
* Check if a post's featured image should be the header image.
*
* @since 1.0.0
*
* @return bool Returns true when the `use_featured_hero_image` theme mod is set, otherwise returns false.
*/
function primer_use_featured_hero_image() {
$enabled = (bool) get_theme_mod( 'use_featured_hero_image' );
/**
* Filter if a post's featured image should be the header image.
*
* @since 1.0.0
*
* @var bool
*/
return (bool) apply_filters( 'primer_use_featured_hero_image', $enabled );
}
/**
* Check if there is a header image to display.
*
* @since 1.0.0
*
* @uses has_header_image
* @uses primer_use_featured_hero_image
* @uses [has_post_thumbnail](https://developer.wordpress.org/reference/functions/has_post_thumbnail/)
* @uses [get_queried_object](https://codex.wordpress.org/Function_Reference/get_queried_object)
*
* @return bool Returns true when a header image is set, or when the featured hero image setting is true and a featured image is set, otherwise return false.
*/
function primer_has_hero_image() {
return ( has_header_image() || ( primer_use_featured_hero_image() && has_post_thumbnail( get_queried_object() ) ) );
}
/**
* Return the hero image URL.
*
* @since 1.0.0
*
* @uses primer_use_featured_hero_image
* @uses [get_queried_object](https://codex.wordpress.org/Function_Reference/get_queried_object)
* @uses [has_post_thumbnail](https://developer.wordpress.org/reference/functions/has_post_thumbnail/)
* @uses [wp_get_attachment_image_src](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/)
* @uses [get_post_thumbnail_id](https://developer.wordpress.org/reference/functions/get_post_thumbnail_id/)
*
* @return string|null Returns the featured image if one is set, otherwise null.
*/
function primer_get_hero_image() {
/**
* Filter the hero image size.
*
* @since 1.0.0
*
* @var string
*/
$size = (string) apply_filters( 'primer_hero_image_size', 'primer-hero' );
$post = get_queried_object();
/**
* Featured Image (if enabled)
*/
if ( primer_use_featured_hero_image() && has_post_thumbnail( $post ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post ), $size );
if ( isset( $image[0] ) ) {
return $image[0];
}
}
/**
* Header Image
*/
if ( has_header_image() ) {
$header = get_custom_header();
if ( ! empty( $header->attachment_id ) ) {
$image = wp_get_attachment_image_src( $header->attachment_id, $size );
if ( isset( $image[0] ) ) {
return $image[0];
}
}
return get_header_image();
}
}
/**
* Return the size to use for featured images.
*
* @since 1.0.0
*
* @return string Returns the feature image size.
*/
function primer_get_featured_image_size() {
/**
* Filter the size to use for featured images.
*
* @var string
*/
return (string) apply_filters( 'primer_featured_image_size', 'primer-featured' );
}
/**
* Return the posts page URL.
*
* In the event a custom homepage exists, we need
* to find the posts page and return its URL.
*
* @uses [get_option](https://developer.wordpress.org/reference/functions/get_option/)
* @uses [get_permalink](https://developer.wordpress.org/reference/functions/get_permalink/)
*
* @since 1.0.0
*
* @return string Return the post page URL.
*/
function primer_get_posts_url() {
$url = ( 'page' === get_option( 'show_on_front' ) ) ? get_permalink( (int) get_option( 'page_for_posts' ) ) : null;
/**
* Filter the posts page URL.
*
* @since 1.0.0
*
* @var string
*/
return (string) apply_filters( 'primer_posts_url', $url );
}
/**
* Return an array of footer widget areas.
*
* @global array $wp_registered_sidebars
* @since 1.0.0
*
* @return array Returns the Primer theme sidebars array.
*/
function primer_get_footer_sidebars() {
global $wp_registered_sidebars;
$sidebars = preg_grep( '/^footer-(.*)/', array_keys( $wp_registered_sidebars ) );
/**
* Filter the array of footer widget areas.
*
* @since 1.0.0
*
* @var array
*/
return (array) apply_filters( 'primer_footer_sidebars', $sidebars );
}
/**
* Return an array of active footer widget areas.
*
* @since 1.0.0
*
* @return array Returns an array of active footer widget areas.
*/
function primer_get_active_footer_sidebars() {
return array_filter( primer_get_footer_sidebars(), 'is_active_sidebar' );
}
/**
* Check if there are active footer widget areas.
*
* @since 1.0.0
*
* @uses primer_get_active_footer_sidebars
*
* @return bool Returns true when footer sidebars are set, otherwise returns false.
*/
function primer_has_active_footer_sidebars() {
return (bool) primer_get_active_footer_sidebars();
}
/**
* Check if a footer or social menu is assigned.
*
* @since 1.0.0
*
* @uses [has_nav_menu](https://developer.wordpress.org/reference/functions/has_nav_menu/)
*
* @return bool Returns true when the 'footer' or 'social' navigation menu is set.
*/
function primer_has_footer_menu() {
return ( has_nav_menu( 'footer' ) || has_nav_menu( 'social' ) );
}
/**
* Check if the site has active categories.
*
* We will store the result in a transient so this function
* can be called frequently without any performance concern.
*
* @see primer_has_active_categories_reset()
*
* @since 1.0.0
*
* @uses [get_transient](https://developer.wordpress.org/reference/functions/get_transient/)
* @uses [get_categories](https://developer.wordpress.org/reference/functions/get_categories/)
* @uses [set_transient](https://developer.wordpress.org/reference/functions/set_transient/)
*
* @return bool Returns true when categories are found, otherwise returns false.
*/
function primer_has_active_categories() {
$has_active_categories = get_transient( 'primer_has_active_categories' );
if ( WP_DEBUG || false === $has_active_categories ) {
$categories = get_categories(
array(
'fields' => 'ids',
'hide_empty' => 1,
'number' => 2, // We only care if more than one exists.
)
);
$has_active_categories = ( count( $categories ) > 1 );
set_transient( 'primer_has_active_categories', $has_active_categories );
}
/**
* Filter if the site has active categories.
*
* @since 1.0.0
*
* @var bool
*/
return (bool) apply_filters( 'primer_has_active_categories', ! empty( $has_active_categories ) );
}
/**
* Convert a 3- or 6-digit hexadecimal color to an associative RGB array.
*
* @since 1.0.0
*
* @param string $color Hex color, with or without a leading `#`.
*
* @return array Returns an array of Red, Green and Blue values to use in CSS styles.
*/
function primer_hex2rgb( $color ) {
$color = trim( $color, '#' );
switch ( strlen( $color ) ) {
case 3 :
$r = hexdec( substr( $color, 0, 1 ) . substr( $color, 0, 1 ) );
$g = hexdec( substr( $color, 1, 1 ) . substr( $color, 1, 1 ) );
$b = hexdec( substr( $color, 2, 1 ) . substr( $color, 2, 1 ) );
break;
case 6 :
$r = hexdec( substr( $color, 0, 2 ) );
$g = hexdec( substr( $color, 2, 2 ) );
$b = hexdec( substr( $color, 4, 2 ) );
break;
default :
return array();
}
return array( 'red' => $r, 'green' => $g, 'blue' => $b );
}
/**
* Recursively replace elements from passed arrays into the first array (safe for PHP 5.2).
*
* @author Frankie Jarrett <fjarrett@gmail.com>
* @link https://secure.php.net/manual/en/function.array-replace-recursive.php
*
* @since 1.0.0
*
* @param array $array1 The array in which elements are replaced.
* @param array $array2 The array from which elements will be extracted.
* @param array $array,... (optional) More arrays from which elements will be extracted.
*
* @return array Returns an array with recursively replaced elements.
*/
function primer_array_replace_recursive( array $array1, array $array2 ) {
if ( function_exists( 'array_replace_recursive' ) ) {
$args = func_get_args();
return call_user_func_array( 'array_replace_recursive', $args );
}
$total = func_num_args();
$result = array();
for ( $i = 0; $i < $total; $i++ ) {
$_array = func_get_arg( $i );
foreach ( $_array as $key => &$value ) {
if ( is_array( $value ) && isset( $result[ $key ] ) && is_array( $result[ $key ] ) ) {
$result[ $key ] = call_user_func( __FUNCTION__, $result[ $key ], $value );
continue;
}
$is_assoc = ( array_keys( $_array ) !== range( 0, count( $_array ) - 1 ) );
if ( ! $is_assoc && ! in_array( $value, $merged, true ) ) {
$result[] = $value;
continue;
}
$result[ $key ] = $value;
}
}
return (array) $result;
}
/**
* Render a widget in the output buffer and return the markup.
*
* @since 1.5.0
*
* @uses [the_widget](https://developer.wordpress.org/reference/functions/the_widget/) To render the widget.
*
* @param string $widget The widget's PHP class name.
* @param array $instance (optional) The widget's instance settings.
* @param array $args (optional) Array of arguments to configure the display of the widget.
*
* @return string
*/
function primer_get_the_widget( $widget, $instance = array(), $args = array() ) {
ob_start();
the_widget( $widget, $instance, $args );
return ob_get_clean();
}
/**
* Check if the current theme is Primer or direct Primer child theme
*
* @since 1.7.0
*
* @uses [wp_get_theme](https://developer.wordpress.org/reference/functions/wp_get_theme/) To retreive the current theme data.
*
* @return boolean True if Primer or custom Primer child theme, otherwise false
*/
function is_custom_primer_child() {
$theme = wp_get_theme();
return ( ! is_child_theme() || ! in_array( $theme->get( 'Name' ), array(
'Activation',
'Ascension',
'Escapade',
'Mins',
'Scribbles',
'Stout',
'Lyrical',
'Uptown Style',
'Velux',
), true ) );
}