-
Notifications
You must be signed in to change notification settings - Fork 9
/
functions.php
240 lines (195 loc) · 6.43 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
<?php
/**
* hm-handbook functions and definitions.
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
* @package hm-handbook
*/
namespace HM_Handbook;
require_once( __DIR__ . '/inc/primary-nav.php' );
require_once( __DIR__ . '/inc/page-history.php' );
require_once( __DIR__ . '/inc/private-links.php' );
require_once( __DIR__ . '/inc/search.php' );
require_once( __DIR__ . '/inc/editor-mods.php' );
require_once( __DIR__ . '/inc/updates.php' );
add_action( 'after_setup_theme', __NAMESPACE__ . '\\setup' );
add_action( 'after_setup_theme', __NAMESPACE__ . '\\content_width', 0 );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\enqueue_scripts' );
add_action( 'admin_init', __NAMESPACE__ . '\\setup_admin' );
/**
* Set up the theme.
*/
function setup() {
// Let WordPress manage the document title.
add_theme_support( 'title-tag' );
// Disable support for Post Thumbnails on posts and pages.
remove_theme_support( 'post-thumbnails' );
// Enable custom site logo support
add_theme_support( 'custom-logo' );
// Switch default core markup for search form, comment form, and comments to output valid HTML5.
add_theme_support( 'html5', [ 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption' ] );
// Show page history in the theme
add_theme_support( 'hm-page-history' );
// Register navigation menus.
register_nav_menu( 'nav-primary', 'Main navigation' );
// Register Sidebars.
register_sidebar( [
'name' => __( 'Content Siedebar (Logged Out)', 'hm-handbook' ),
'id' => 'site-content-logged-out',
'description' => __( 'Shown only to logged out visitors.', 'hm-handbook' ),
'class' => '',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
] );
register_sidebar( [
'name' => __( 'Footer Content', 'hm-handbook' ),
'id' => 'footer-content',
'description' => __( 'Show in the site footer.', 'hm-handbook' ),
'class' => '',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
] );
// Filter next/prev post classes.
add_filter( 'next_posts_link_attributes', __NAMESPACE__ . '\\posts_link_attributes_next' );
add_filter( 'previous_posts_link_attributes', __NAMESPACE__ . '\\posts_link_attributes_prev' );
add_filter( 'wp_link_pages_link', __NAMESPACE__ . '\\multi_page_links_markup', 10, 2 );
}
/**
* Set up the admin.
*/
function setup_admin() {
add_editor_style( 'assets/dist/styles/editor.css' );
add_filter( 'mce_external_plugins', function( $plugin_array ) {
$plugin_array['typekit'] = get_template_directory_uri() . '/inc/tinyMCE/tinyMCE-typekit.js';
return $plugin_array;
} );
}
function get_asset_version() {
if ( wp_get_environment_type() === 'local' ) {
return filemtime( __DIR__ . '/assets/dist/styles/theme.css' );
}
return wp_get_theme()->Version;
}
/**
* Add login Styling
*/
add_action( 'login_enqueue_scripts', function() {
wp_enqueue_style( 'hm-login', get_theme_file_uri( 'assets/dist/styles/login.css' ), [], get_asset_version() );
} );
/**
* Enqueue all theme scripts.
*/
function enqueue_scripts() {
$version = get_asset_version();
wp_enqueue_script( 'hm-handbook', get_theme_file_uri( 'assets/dist/scripts/theme.js' ), [], $version, true );
wp_enqueue_style( 'hm-handbook', get_theme_file_uri( 'assets/dist/styles/theme.css' ), [], $version );
add_action( 'wp_head', function() {
echo '<script src="https://use.typekit.net/qly7hgx.js"></script>';
echo '<script>try{Typekit.load({ async: true });}catch(e){}</script>';
} );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
/**
* Set the content width in pixels, based on the theme's design and stylesheet.
*
* Priority 0 to make it available to lower priority callbacks.
*
* @global int $content_width
*/
function content_width() {
$GLOBALS['content_width'] = 640;
}
/**
* Retrieve the URL of a file in the theme.
*
* Searches in the stylesheet directory before the template directory so themes
* which inherit from a parent theme can just override one file.
*
* @param string $file File to search for in the stylesheet directory.
* @return string The URL of the file.
*/
function get_theme_file_uri( $file = '' ) {
$file = ltrim( $file, '/' );
if ( empty( $file ) ) {
$url = get_stylesheet_directory_uri();
} elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
$url = get_stylesheet_directory_uri() . '/' . $file;
} else {
$url = get_template_directory_uri() . '/' . $file;
}
return $url;
}
/**
* Retrieve the URL of a file in the parent theme.
*
* @param string $file File to return the URL for in the template directory.
* @return string The URL of the file.
*/
function get_parent_theme_file_uri( $file = '' ) {
$file = ltrim( $file, '/' );
if ( empty( $file ) ) {
$url = get_template_directory_uri();
} else {
$url = get_template_directory_uri() . '/' . $file;
}
return $url;
}
/**
* Add Class to pagination next links
*
* @return string Attributes.
*/
function posts_link_attributes_next() {
return 'class="btn btn--secondary btn--small Pagination-Next"';
}
/**
* Add Class to pagination previous links
*
* @return string Attributes.
*/
function posts_link_attributes_prev() {
return 'class="btn btn--secondary btn--small Pagination-Prev"';
}
/**
* Add indicative icon to private page titles
*
* @return string Title format.
*/
add_filter( 'private_title_format', function( $format ) {
return '🔒 %s';
} );
/**
* Handle the markup for multi-page posts.
*/
function multi_page_links_markup( $link, $i ) {
global $page;
if ( $i === $page ) {
$link = '<span class="Pagination-Current">' . $link . '</span>';
}
return $link;
}
/**
* Redirect private pages to a hiring page if a user is logged out.
*/
function redirect_private_pages_to_join_page() {
// Error page - that non logged in users get when accessing private content.
if ( is_404() ) {
$queried_object = get_queried_object();
if (
isset( $queried_object->post_status ) &&
'private' === $queried_object->post_status &&
! is_user_logged_in()
) {
wp_safe_redirect( home_url( '/join-human-made/' ) );
exit();
}
}
}
add_action( 'template_redirect', __NAMESPACE__ . '\\redirect_private_pages_to_join_page' );