-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
141 lines (122 loc) · 4.66 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
<?php
/**
* Load, call, and hook everything needed by Wiki Modern.
*
* @package Wiki Modern Theme
*/
/**
* NOTE:
* Not included yet:
* > Prefix lock icon in post titles.
* > Add all AJAX form handlers. ???
* > Dynamically set the post per page based on the users cookie if present.
*/
require 'wm-autoload.php';
require 'include/wm-get-image-widths.php';
require 'include/wm-kint-fake.php';
// Check if dark mode is enabled for this user.
$dark_mode = false;
if ( isset( $_COOKIE['wm-dark-mode'] ) ) {
$dark_mode = true;
}
define( 'DARK_MODE', $dark_mode );
// Enqueue styles and scripts loaded with the built in wp_footer() function.
function wm_enqueue_assets() {
// TODO: Fix $ctime.
$ctime = random_bytes( 10 );
/**
* Theme's primary (default) style.css file. This file is not used by Wiki
* Modern but users may try to add styles to it.
*/
wp_enqueue_style( 'style-css', get_stylesheet_uri(), array(), $ctime );
// Wiki Modern's primary CSS files.
wp_enqueue_style( 'wm-normalize-css', get_template_directory_uri() . '/css/normalize.css', array(), $ctime );
if ( DARK_MODE ) {
wp_enqueue_style( 'wm-main-dark-css', get_template_directory_uri() . '/css/main-dark.css', array(), $ctime );
} else {
wp_enqueue_style( 'wm-main-css', get_template_directory_uri() . '/css/main.css', array(), $ctime );
}
// QR code generator JavaScript file.
// wp_enqueue_script( 'wm-qr-js', get_template_directory_uri() . '/js/QR.js', array(), $ctime, true );
// Wiki Modern's primary JavaScript files.
wp_enqueue_script( 'wm-polyfill-js', get_template_directory_uri() . '/js/polyfills.js', array(), $ctime, true );
wp_enqueue_script( 'wm-qr-code-js', get_template_directory_uri() . '/js/qr-code-styling.js', array(), $ctime, true );
wp_enqueue_script( 'wm-cookie-js', get_template_directory_uri() . '/js/cookie.js', array(), $ctime, true );
wp_enqueue_script( 'wm-wikimodern-js', get_template_directory_uri() . '/js/wiki-modern.js', array(), $ctime, true );
}
add_action( 'wp_enqueue_scripts', 'wm_enqueue_assets' );
// Wiki Modern uses the WordPress Customizer, load that now.
require 'customizer/wm-customizer.php';
/**
* Register the fact that we want to allow a custom logo.
* Handled by: customizer/include/logo.php
*/
add_theme_support( 'custom-logo' );
// Register the Primary Menu.
function wm_register_primary_menu() {
register_nav_menu( 'primary-menu', __( 'Primary Menu' ) );
}
add_action( 'init', 'wm_register_primary_menu' );
// Disable self pingbacks.
function wm_disable_self_ping( &$links ) {
foreach ( $links as $key => $link ) {
if ( 0 === strpos( $link, get_option( 'home' ) ) ) {
unset( $links[ $key ] );
}
}
}
add_action( 'pre_ping', 'wm_disable_self_ping' );
// Remove the read more link completely from the theme.
function wm_remove_read_more_link() {
return '';
}
add_filter( 'the_content_more_link', 'wm_remove_read_more_link' );
/**
* Add custom image sizes attribute to enhance responsive image functionality
* for content images
*
* @since Twenty Sixteen 1.0
*
* @param string $sizes A source size value for use in a 'sizes' attribute.
* @param array $size Image size. Accepts an array of width and height
* values in pixels (in that order).
* @return string A source size value for use in a content image 'sizes' attribute.
*/
function twentysixteen_content_image_sizes_attr( $sizes, $size ) {
$widths = wm_get_image_widths();
$sizes = '';
foreach ( $widths as $width ) {
$width = intval( $width );
$sizes .= '(max-width: ' . ( $width + 100 ) . 'px) ' . ( $width - 100 ) . 'px, ';
}
return $sizes . '100vw';
}
add_filter( 'wp_calculate_image_sizes', 'twentysixteen_content_image_sizes_attr', 10, 2 );
// Register WikiModern's Widget areas.
function wm_widget_areas() {
require 'include/wm-widgets.php';
}
add_action( 'widgets_init', 'wm_widget_areas' );
// Pretty print any site searches.
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_safe_redirect( home_url( '/search/' ) . rawurlencode( get_query_var( 's' ) ) );
exit;
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
// A simple cookie sanitization function to make WordPress happy.
function wm_sanitize_cookie( $data ) {
$cleaned = htmlspecialchars( $data, ENT_NOQUOTES );
if ( strlen( $cleaned ) !== strlen( $data ) ) {
return '';
}
return $data;
}
add_filter( 'sanitize_json_wm_cookie', 'wm_sanitize_cookie' );
add_action(
'after_setup_theme',
function () {
add_theme_support( 'admin-bar', array( 'callback' => '__return_false' ) );
}
);