-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
624 lines (520 loc) · 18.9 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
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
<?php
// =============================================================================
// CONSTANTS
// =============================================================================
define( 'CHILD_VERSION', '1.0.6' );
define( 'CHILD_NAME', 'Fictioneer Minimalist' );
define( 'CHILD_RELEASE_TAG', 'v1.0.6' );
// Boolean: Base theme switch in site settings
if ( ! defined( 'FICTIONEER_THEME_SWITCH' ) ) {
define( 'FICTIONEER_THEME_SWITCH', false );
}
// Integer: Default site width
if ( ! defined( 'FICTIONEER_DEFAULT_SITE_WIDTH' ) ) {
define( 'FICTIONEER_DEFAULT_SITE_WIDTH', 896 );
}
// Integer: Update check timeout
if ( ! defined( 'FCNMM_UPDATE_CHECK_TIMEOUT' ) ) {
define( 'FCNMM_UPDATE_CHECK_TIMEOUT', 43200 ); // 12 hours
}
// =============================================================================
// CHILD THEME SETUP
// =============================================================================
function fcnmm_theme_setup() {
// Load translations (if any)
load_child_theme_textdomain( 'fcnmm', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'fcnmm_theme_setup' );
/**
* Enqueue child theme styles and scripts
*/
function fcnmm_enqueue_styles_and_scripts() {
// Setup
$cache_bust = get_option( 'fictioneer_cache_bust' ) ?: CHILD_VERSION;
$parenthandle = 'fictioneer-application';
// Add child theme styles
wp_enqueue_style(
'fcnmm-style',
get_stylesheet_directory_uri() . '/css/fcnmm-style.css',
[ $parenthandle ],
$cache_bust
);
}
add_action( 'wp_enqueue_scripts', 'fcnmm_enqueue_styles_and_scripts', 99 );
/**
* Add or remove parent filters and actions on the frontend
*/
function fcnmm_customize_parent() {
// Remove unused parent partials
remove_action( 'fictioneer_site', 'fictioneer_post_content_header', 20 );
remove_action( 'fictioneer_site', 'fictioneer_text_center_header', 20 );
remove_action( 'fictioneer_site', 'fictioneer_inner_header', 20 );
remove_action( 'fictioneer_site', 'fictioneer_top_header', 9 );
remove_action( 'fictioneer_site', 'fictioneer_inner_header', 20 );
remove_action( 'fictioneer_site', 'fictioneer_navigation_bar' );
remove_action( 'fictioneer_main', 'fictioneer_page_background' );
}
add_action( 'init', 'fcnmm_customize_parent' );
// =============================================================================
// CHECK FOR UPDATES
// =============================================================================
/**
* Get basic child theme info
*
* @since 1.0.0
*
* @return array Associative array with theme info.
*/
function fcnmm_get_theme_info() {
// Setup
$info = get_option( 'fcnmm_theme_info' ) ?: [];
// Set up if missing
if ( ! $info || ! is_array( $info ) ) {
$info = array(
'last_update_check' => current_time( 'mysql', 1 ),
'last_update_version' => '',
'last_update_nag' => current_time( 'mysql', 1 ),
'last_update_notes' => '',
'last_version_download_url' => '',
'setup' => 0,
'version' => CHILD_VERSION
);
update_option( 'fcnmm_theme_info', $info, 'yes' );
}
// Merge with defaults (in case of incomplete data)
$info = array_merge(
array(
'last_update_check' => current_time( 'mysql', 1 ),
'last_update_version' => '',
'last_update_nag' => '',
'last_update_notes' => '',
'last_version_download_url' => '',
'setup' => 0,
'version' => CHILD_VERSION
),
$info
);
// Return info
return $info;
}
/**
* Check Github repository for a new release of the child theme
*
* 1.0.0
*
* @return boolean True if there is a newer version, false if not.
*/
function fcnmm_check_for_updates() {
global $pagenow;
// Setup
$theme_info = fcnmm_get_theme_info();
$last_check_timestamp = strtotime( $theme_info['last_update_check'] ?? 0 );
$remote_version = $theme_info['last_update_version'];
$is_updates_page = $pagenow === 'update-core.php';
// Only call API every n seconds, otherwise check database
if (
( ! $is_updates_page && current_time( 'timestamp', true ) < $last_check_timestamp + FCNMM_UPDATE_CHECK_TIMEOUT ) ||
( $_GET['action'] ?? 0 ) === 'do-plugin-upgrade'
) {
if ( ! $remote_version ) {
return false;
}
return version_compare( $remote_version, CHILD_RELEASE_TAG, '>' );
}
// Remember this check
$theme_info['last_update_check'] = current_time( 'mysql', 1 );
// Request to repository
$response = wp_remote_get(
'https://api.github.com/repos/Tetrakern/fictioneer-minimalist/releases/latest',
array(
'headers' => array(
'User-Agent' => 'FICTIONEER-MINIMALIST',
'Accept' => 'application/vnd.github+json',
'X-GitHub-Api-Version' => '2022-11-28'
)
)
);
// Abort if request failed or is not a 2xx success status code
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) >= 300 ) {
// Remember check to avoid continuous tries on each page load
update_option( 'fcnmm_theme_info', $theme_info );
return false;
}
// Decode JSON to array
$release = json_decode( wp_remote_retrieve_body( $response ), true );
$release_tag = sanitize_text_field( $release['tag_name'] ?? '' );
// Abort if request did not return expected data
if ( ! $release_tag ) {
return false;
}
// Add to theme info
$theme_info['last_update_version'] = $release_tag;
$theme_info['last_update_notes'] = sanitize_textarea_field( $release['body'] ?? '' );
$theme_info['last_update_nag'] = ''; // Reset
if ( ( $release['assets'] ?? 0 ) && function_exists( 'fictioneer_sanitize_url' ) ) {
$theme_info['last_version_download_url'] = fictioneer_sanitize_url( $release['assets'][0]['browser_download_url'] ?? '' );
} else {
$theme_info['last_version_download_url'] = '';
}
// Update info in database
update_option( 'fcnmm_theme_info', $theme_info );
// Compare with currently installed version
return version_compare( $release_tag, CHILD_RELEASE_TAG, '>' );
}
/**
* Show notice when a newer version of the child theme is available
*
* @since 1.0.0
*/
function fcnmm_admin_update_notice() {
// Guard
if (
! current_user_can( 'install_themes' ) ||
( $_GET['action'] ?? 0 ) === 'upload-theme' ||
! fcnmm_check_for_updates()
) {
return;
}
global $pagenow;
// Setup
$theme_info = fcnmm_get_theme_info();
$last_update_nag = strtotime( $theme_info['last_update_nag'] ?? 0 );
$is_updates_page = $pagenow == 'update-core.php';
// Show only once every n seconds
if ( ! $is_updates_page && current_time( 'timestamp', true ) < $last_update_nag + 60 ) {
return;
}
// Render notice
if ( function_exists( 'fictioneer_prepare_release_notes' ) ) {
$notes = fictioneer_prepare_release_notes( $theme_info['last_update_notes'] ?? '' );
} else {
$notes = sanitize_textarea_field( $theme_info['last_update_notes'] ?? '' );
}
wp_admin_notice(
sprintf(
__( '<strong>Fictioneer Minimalist %1$s</strong> is available. Please <a href="%2$s" target="_blank">download</a> and install the latest version at your next convenience.%3$s', 'fcnmm' ),
$theme_info['last_update_version'],
'https://github.com/Tetrakern/fictioneer-minimalist/releases',
$notes ? '<br><details><summary>' . __( 'Release Notes', 'fcnmm' ) . '</summary>' . $notes . '</details>' : ''
),
array(
'type' => 'warning',
'dismissible' => true,
'additional_classes' => ['fictioneer-update-notice']
)
);
// Remember notice
$theme_info['last_update_nag'] = current_time( 'mysql', 1 );
// Update info in database
update_option( 'fcnmm_theme_info', $theme_info );
}
add_action( 'admin_notices', 'fcnmm_admin_update_notice' );
// =============================================================================
// CUSTOMIZER
// =============================================================================
/**
* Modify the theme customizer
*
* @since 1.0.0
*
* @param WP_Customize_Manager $manager The customizer instance.
*/
function fcnmm_modify_customizers( $manager ) {
// Site width
$manager->add_setting(
'site_width',
array(
'capability' => 'edit_theme_options',
'sanitize_callback' => 'absint',
'default' => 896
)
);
$manager->add_control(
'site_width',
array(
'type' => 'number',
'priority' => 2,
'section' => 'layout',
'label' => __( 'Site Width', 'fictioneer' ),
'description' => __( 'Maximum site width in pixels, should not be less than 832. Default 896.', 'fcnmm' ),
'input_attrs' => array(
'placeholder' => '896',
'min' => 832,
'style' => 'width: 80px'
)
)
);
// Header tagline color
fictioneer_add_color_theme_option(
$manager,
array(
'section' => 'dark_mode_colors',
'setting' => 'dark_header_tagline_color',
'label' => __( 'Dark Header Tagline', 'fictioneer' ),
'priority' => 7,
'default' => '#9097a7'
)
);
// Remove controls
$manager->remove_control( 'inset_header_image' );
$manager->remove_control( 'header_style' );
$manager->remove_control( 'title_text_shadow' );
$manager->remove_control( 'header_post_content_id' );
$manager->remove_control( 'header_height_min' );
$manager->remove_control( 'header_height_max' );
$manager->remove_control( 'page_style' );
$manager->remove_control( 'page_shadow' );
$manager->remove_control( 'horizontal_spacing_min' );
$manager->remove_control( 'horizontal_spacing_max' );
$manager->remove_control( 'horizontal_spacing_small_min' );
$manager->remove_control( 'horizontal_spacing_small_max' );
}
add_action( 'customize_register', 'fcnmm_modify_customizers', 99 );
/**
* Always sets inset_header_image to true
*
* @since 1.0.0
*
* @param string $mod Value of the theme mod.
*
* @return string Updated value of the theme mod.
*/
function fcnmm_modify_inset_header_image( $mod ) {
return true;
}
add_filter( 'theme_mod_inset_header_image', 'fcnmm_modify_inset_header_image' );
/**
* Overrides default cover position on story pages
*
* @since 1.0.0
*
* @param string $mod Value of the theme mod.
*
* @return string Updated value of the theme mod.
*/
function fcnmm_modify_story_cover_position_fix_default( $mod ) {
if ( ! $mod || $mod === 'top-left-overflow' ) {
return 'float-top-left';
}
return $mod;
}
add_filter( 'theme_mod_story_cover_position', 'fcnmm_modify_story_cover_position_fix_default' );
/**
* Changes default cover position in the Customizer
*
* @since 1.0.0
*
* @param string $default Default value
*
* @return string Changed default value.
*/
function fcnmm_change_story_cover_position_default( $default ) {
return 'float-top-left';
}
add_filter( 'fictioneer_filter_customizer_story_cover_position_default', 'fcnmm_change_story_cover_position_default' );
/**
* Removes top-left-overflow story cover position option
*
* @since 1.0.0
*
* @param array $options Customizer options for the story cover position.
*
* @return array Updated options.
*/
function fcnmm_modify_story_cover_position_options( $options ) {
unset( $options['top-left-overflow'] );
$options['float-top-left'] = _x( 'Floating Top-Left (Default)', 'Customizer story cover position option.', 'fcnmm' );
return $options;
}
add_filter( 'fictioneer_filter_customizer_story_cover_position', 'fcnmm_modify_story_cover_position_options' );
// =============================================================================
// SITE HEADER & NAVIGATION
// =============================================================================
/**
* Outputs the HTML for the child theme header
*
* @since 1.0.0
*
* @param int|null $args['post_id'] Optional. Current post ID.
* @param int|null $args['story_id'] Optional. Current story ID (if chapter).
* @param string|boolean $args['header_image_url'] URL of the filtered header image or false.
* @param array $args['header_args'] Arguments passed to the header.php partial.
*/
function fcnmm_header( $args ) {
// Return early if...
if ( $args['header_args']['blank'] ?? 0 ) {
return;
}
// Setup
$home_url = home_url();
$title = get_bloginfo( 'name' );
$tagline = get_bloginfo( 'description' );
$title_tag = is_front_page() ? 'h1' : 'h3';
$sub_tag = is_front_page() ? 'h2' : 'h4';
$classes = [];
// CSS classes
if ( ! display_header_text() || empty( $title ) ) {
$classes[] = '_no-title';
}
if ( empty( $tagline ) ) {
$classes[] = '_no-tagline';
}
if ( ! has_custom_logo() ) {
$classes[] = '_no-logo';
}
// Render Elementor or theme header
if ( ! function_exists( 'elementor_theme_do_location' ) || ! elementor_theme_do_location( 'header' ) ) {
// Start HTML ---> ?>
<header class="fcnmm-header <?php echo implode( ' ', $classes ); ?>">
<div class="fcnmm-header__content">
<div class="fcnmm-header__identity"><?php
if ( has_custom_logo() ) {
echo get_custom_logo();
}
if ( display_header_text() ) {
if ( $title ) {
echo "<{$title_tag} class='fcnmm-header__title'><a href='{$home_url}'>{$title}</a></{$title_tag}>";
}
if ( $tagline ) {
echo "<{$sub_tag} class='fcnmm-header__tagline'>{$tagline}</{$sub_tag}>";
}
}
?></div>
<div class="fcnmm-header__spacer"></div>
<div class="fcnmm-header__icon-menu"><?php
fictioneer_render_icon_menu( array( 'location' => 'in-navigation' ) );
?></div>
</div>
</header>
<?php // <--- End HTML
}
}
add_action( 'fictioneer_site', 'fcnmm_header', 20 );
/**
* Outputs the HTML for the child theme navigation bar
*
* @since 1.0.0
*
* @param int|null $args['post_id'] Optional. Current post ID.
* @param int|null $args['story_id'] Optional. Current story ID (if chapter).
* @param string|boolean $args['header_image_url'] URL of the filtered header image or false.
* @param array $args['header_args'] Arguments passed to the header.php partial.
*/
function fcnmm_navigation_bar( $args ) {
// Return early if...
if ( $args['header_args']['blank'] ?? 0 ) {
return;
}
// Setup
$mobile_nav_style = get_theme_mod( 'mobile_nav_style', 'overflow' );
$classes = [];
if ( $mobile_nav_style === 'collapse' ) {
$classes[] = '_collapse-on-mobile';
}
// Start HTML ---> ?>
<div id="full-navigation" class="main-navigation <?php echo implode( ' ', $classes ); ?>">
<div id="nav-observer-sticky" class="observer nav-observer"></div>
<div class="main-navigation__background"></div>
<?php do_action( 'fictioneer_navigation_top', $args ); ?>
<div class="main-navigation__wrapper">
<?php if ( ! function_exists( 'elementor_theme_do_location' ) || ! elementor_theme_do_location( 'nav_bar' ) ) : ?>
<?php do_action( 'fictioneer_navigation_wrapper_start', $args ); ?>
<nav class="main-navigation__left" aria-label="<?php echo esc_attr__( 'Main Navigation', 'fictioneer' ); ?>">
<button class="mobile-menu-button follows-alert-number" data-fictioneer-follows-target="newDisplay" data-action="click->fictioneer#toggleMobileMenu">
<?php
fictioneer_icon( 'fa-bars', 'off' );
fictioneer_icon( 'fa-xmark', 'on' );
?>
<span class="mobile-menu-button__label"><?php _ex( 'Menu' , 'Mobile menu label', 'fictioneer' ); ?></span>
</button>
<?php
if ( ! function_exists( 'elementor_theme_do_location' ) || ! elementor_theme_do_location( 'nav_menu' ) ) {
if ( has_nav_menu( 'nav_menu' ) ) {
$menu = null;
if ( fictioneer_enable_menu_transients( 'nav_menu' ) ) {
$menu = get_transient( 'fictioneer_fcnmm_nav_menu_html' );
}
if ( empty( $menu ) ) {
$menu = wp_nav_menu(
array(
'theme_location' => 'nav_menu',
'menu_class' => 'main-navigation__list',
'container' => '',
'menu_id' => 'menu-navigation',
'items_wrap' => '<ul id="%1$s" data-menu-id="main" class="%2$s">%3$s</ul>',
'echo' => false
)
);
if ( $menu !== false ) {
$menu = str_replace( ['current_page_item', 'current-menu-item', 'aria-current="page"'], '', $menu );
}
if ( fictioneer_enable_menu_transients( 'nav_menu' ) ) {
set_transient( 'fictioneer_fcnmm_nav_menu_html', $menu );
}
}
fictioneer_add_taxonomy_submenus( $menu );
echo $menu;
}
}
?>
</nav>
<?php do_action( 'fictioneer_navigation_wrapper_end', $args ); ?>
<?php endif; ?>
</div>
<?php do_action( 'fictioneer_navigation_bottom', $args ); ?>
</div>
<?php // <--- End HTML
}
add_action( 'fictioneer_site', 'fcnmm_navigation_bar', 21 );
/**
* Purges nav menu Transients on menu updates
*
* @since 1.0.0
*/
function fcnmm_purge_nav_menu_transients() {
delete_transient( 'fictioneer_fcnmm_nav_menu_html' );
}
add_action( 'wp_update_nav_menu', 'fcnmm_purge_nav_menu_transients' );
/**
* Outputs the HTML for the header image (if any)
*
* @since 1.0.0
* @since 1.0.4 - Considered additional arguments.
*
* @param int|null $args['post_id'] Optional. Current post ID.
* @param string|null $args['post_type'] Optional. Current post type.
* @param int|null $args['story_id'] Optional. Current story ID (if chapter).
* @param string|boolean $args['header_image_url'] URL of the filtered header image or false.
* @param string $args['header_image_source'] Either default, post, or story.
* @param array $args['header_args'] Arguments passed to the header.php partial.
*/
function fcnmm_header_image( $args ) {
// Abort if...
if ( ! ( $args['header_image_url'] ?? 0 ) || ( $args['header_args']['no_header'] ?? 0 ) ) {
return;
}
// Setup
$header_image_style = get_theme_mod( 'header_image_style', 'default' );
$extra_classes = array(
"_style-{$header_image_style}",
"_image-source-{$args['header_image_source']}",
"_post-{$args['post_id']}"
);
if ( absint( get_theme_mod( 'header_image_fading_start', 0 ) ) > 0 ) {
$extra_classes[] = '_fading-bottom';
}
if ( get_theme_mod( 'header_image_shadow', true ) ) {
$extra_classes[] = '_shadow';
}
if ( $args['post_type'] ) {
$extra_classes[] = '_' . $args['post_type'];
}
// Start HTML ---> ?>
<div class="header-background _fcnmm hide-on-fullscreen polygon <?php echo implode( ' ', $extra_classes ); ?>">
<div class="header-background__wrapper _fcnmm polygon">
<img src="<?php echo $args['header_image_url']; ?>" alt="Header Image" class="header-background__image _fcnmm">
</div>
</div>
<?php // <--- End HTML
}
add_action( 'fictioneer_site', 'fcnmm_header_image', 25 );