forked from jquery/jquery-wp-content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
executable file
·293 lines (256 loc) · 9.13 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
<?php
/**
* Functions and definitions
*
* For more information on hooks, actions, and filters, see https://codex.wordpress.org/Plugin_API.
*/
require_once 'functions.jquery.php';
/**
* Set the content width based on the theme's design and stylesheet.
*/
if ( !isset( $content_width ) ) {
$content_width = 584;
}
add_action('init', function () {
// Remove unused stuff from wp_head
add_action('wp_enqueue_scripts', function () {
// Disable default styles that we don't use
wp_dequeue_style('wp-block-library');
wp_dequeue_style('classic-theme-styles');
wp_dequeue_style('global-styles');
});
});
add_action( 'after_setup_theme', function () {
// This theme styles the visual editor with editor-style.css to match the theme style.
add_editor_style();
// Add default posts and comments RSS feed links to <head>.
add_theme_support( 'automatic-feed-links' );
// This theme uses wp_nav_menu() in one location.
register_nav_menu( 'primary', __( 'Primary Menu', 'twentyeleven' ) );
} );
/**
* Returns a "Continue Reading" link for excerpts
*/
function jq_continue_reading_link() {
return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'Continue reading <span class="meta-nav">→</span>', 'twentyeleven' ) . '</a>';
}
/**
* Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis
* and jq_continue_reading_link().
*
* To override this in a child theme, remove the filter and add your own
* function tied to the excerpt_more filter hook.
*/
add_filter( 'excerpt_more', function ( $more ) {
return ' …' . jq_continue_reading_link();
} );
/**
* Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
*/
add_filter( 'wp_page_menu_args', function ( $args ) {
$args['show_home'] = true;
return $args;
} );
/**
* Register our sidebars and widgetized areas. Also register the default Epherma widget.
*/
add_action( 'widgets_init', function () {
register_sidebar( array(
'name' => __( 'Main Sidebar', 'twentyeleven' ),
'id' => 'sidebar-1',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => "</aside>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Showcase Sidebar', 'twentyeleven' ),
'id' => 'sidebar-2',
'description' => __( 'The sidebar for the optional Showcase Template', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => "</aside>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Footer Area One', 'twentyeleven' ),
'id' => 'sidebar-3',
'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => "</aside>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Footer Area Two', 'twentyeleven' ),
'id' => 'sidebar-4',
'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => "</aside>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Footer Area Three', 'twentyeleven' ),
'id' => 'sidebar-5',
'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => "</aside>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
} );
/**
* Count the number of footer sidebars to enable dynamic classes for the footer
*/
function jq_footer_sidebar_class() {
$count = 0;
if ( is_active_sidebar( 'sidebar-3' ) )
$count++;
if ( is_active_sidebar( 'sidebar-4' ) )
$count++;
if ( is_active_sidebar( 'sidebar-5' ) )
$count++;
$class = '';
switch ( $count ) {
case '1':
$class = 'one';
break;
case '2':
$class = 'two';
break;
case '3':
$class = 'three';
break;
}
if ( $class )
echo 'class="' . $class . '"';
}
/**
* Template for comments and pingbacks.
*
* To override this walker in a child theme without modifying the comments template
* simply create your own jq_comment(), and that function will be used instead.
*
* Used as a callback by wp_list_comments() for displaying the comments.
*/
function jq_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case 'pingback' :
case 'trackback' :
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<p><span class="icon-link"></span> Pingback: <?php comment_author_link(); ?><?php edit_comment_link( 'Edit', ' • <span class="edit-link">', '</span>' ); ?></p>
<?php
break;
default :
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<article id="comment-<?php comment_ID(); ?>" class="comment">
<div class="comment-meta">
<div class="comment-author vcard">
<?php
$avatar_size = 68;
if ( '0' != $comment->comment_parent )
$avatar_size = 39;
echo get_avatar( $comment, $avatar_size );
echo sprintf( '%1s on %2s:',
sprintf( '<span class="fn">%s</span>', esc_html( get_comment_author() ) ),
sprintf( '<a href="%1s"><time pubdate datetime="%2s">%3s</time></a>',
esc_url( get_comment_link() ),
get_comment_time( 'c' ),
sprintf( '%1s at %2s', get_comment_date(), get_comment_time() )
)
);
edit_comment_link( __( 'Edit', 'twentyeleven' ), ' • <span class="edit-link">', '</span>' ); ?>
</div>
<?php if ( $comment->comment_approved == '0' ) : ?>
<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentyeleven' ); ?></em>
<br>
<?php endif; ?>
</div>
<div class="comment-content"><?php comment_text(); ?></div>
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply <span>↓</span>', 'twentyeleven' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div>
</article>
<?php
break;
endswitch;
}
/**
* Prints HTML with meta information for the current post-date/time and author.
* Create your own jq_posted_on to override in a child theme
*/
function jq_posted_on() {
printf( __( '<span class="sep">Posted on </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a><span class="by-author"> <span class="sep"> by </span> <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span>', 'twentyeleven' ),
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
sprintf( esc_attr__( 'View all posts by %s', 'twentyeleven' ), get_the_author() ),
esc_html( get_the_author() )
);
}
function jq_image_posted_on() {
printf( __( '<span class="sep">Published in </span><a href="%1$s" title="Return to %2$s" rel="gallery">%2$s</a>' ),
esc_url( get_permalink( wp_get_post_parent_id() ) ),
get_the_title( wp_get_post_parent_id() )
);
$metadata = wp_get_attachment_metadata();
printf( __( '<a href="%1$s" title="Link to full-size image">%2$s × %3$s</a>' ),
esc_url( wp_get_attachment_url() ),
$metadata['width'],
$metadata['height'],
);
}
/**
* Adds two classes to the array of body classes.
* The first is if the site has only had one author with published posts.
* The second is if a singular post being displayed
*/
add_filter( 'body_class', function ( $classes ) {
if ( ! is_multi_author() ) {
$classes[] = 'single-author';
}
if ( is_singular() && ! is_home() )
$classes[] = 'singular';
return $classes;
} );
/**
* Content Security Policy
*/
function jq_content_security_policy() {
$nonce = bin2hex( random_bytes( 8 ) );
$report_url = 'https://csp-report-api.openjs-foundation.workers.dev/';
$policy = array(
'default-src' => "'self'",
'script-src' => "'self' 'nonce-$nonce' code.jquery.com",
// The nonce is here so inline scripts can be used in the theme
'style-src' => "'self' 'nonce-$nonce' code.jquery.com",
// data: SVG images are used in typesense
// Allow gravatars in wordpress admins
'img-src' => "'self' data: secure.gravatar.com code.jquery.com",
'connect-src' => "'self' typesense.jquery.com",
// Allow data fonts for the wordpress admins
'font-src' => "'self' data:",
'object-src' => "'none'",
'frame-ancestors' => "'none'",
'base-uri' => "'self'",
'block-all-mixed-content' => '',
'report-to' => 'csp-endpoint',
// Add report-uri for Firefox, which
// does not yet support report-to
'report-uri' => $report_url,
);
$policy = apply_filters( 'jq_content_security_policy', $policy );
$policy_string = '';
foreach ( $policy as $key => $value ) {
$policy_string .= $key . ' ' . $value . '; ';
}
header( 'Reporting-Endpoints: csp-endpoint="' . $report_url . '"' );
header( 'Content-Security-Policy: ' . $policy_string );
}
add_action( 'send_headers', 'jq_content_security_policy' );