-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
589 lines (513 loc) · 21.5 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
<?php
/**
* Exits if accessed directly.
*/
if ( ! defined( 'ABSPATH' ) ) {
die( 'Cheatin’ uh?' );
}
define( 'THISTLE_MIN_WP_VERSION', '4.7' );
define( 'THISTLE_TEXT_DOMAIN', 'thistle' );
define( 'THISTLE_URI', get_parent_theme_file_uri() );
define( 'THISTLE_PATH', get_parent_theme_file_path() );
/**
* Thistle only works in WordPress X.Y.Z or later.
*/
if ( version_compare( get_bloginfo( 'version' ), THISTLE_MIN_WP_VERSION, '<=' ) ) {
require 'includes/back-compat.php';
}
if ( ! function_exists( 'thistle_disable_wporg_theme_update' ) ) {
/**
* Disables requests to wp.org repository to check if an update is available
* for a theme with "Thistle" as name.
*
* We need to do this because an outdated theme has the same name
* and its version number is greater than the Thistle one.
*
* @link https://wordpress.org/themes/search/thistle/
* @link https://wordpress.org/themes/thistle/
*
* @param array $r An array of HTTP request arguments.
* @param string $url The request URL.
* @return array
*/
function thistle_disable_wporg_theme_update( $r, $url ) {
// If it's not a theme update request, bail.
if ( strpos( $url, 'https://api.wordpress.org/themes/update-check/1.1/' ) !== 0 ) {
return $r;
}
// Decode the JSON response
$themes = json_decode( $r['body']['themes'] );
// Remove the active parent and child themes from the check
$parent = get_option( 'template' );
$child = get_option( 'stylesheet' );
unset( $themes->themes->$parent, $themes->themes->$child );
// Encode the updated JSON response
$r['body']['themes'] = json_encode( $themes );
return $r;
}
}
add_filter( 'http_request_args', 'thistle_disable_wporg_theme_update', 5, 2 );
if ( ! function_exists( 'thistle_active_theme' ) ) {
/**
* Sets up some default options and remove "Hello Dolly plugin.
*
* Note that this function is hooked into the `after_switch_theme` hook,
* which runs on the first WP load after a theme switch.
*
* @global WP_Rewrite $wp_rewrite
*/
function thistle_active_theme() {
global $wp_rewrite;
$options = array(
'timezone_string' => 'Europe/Paris',
'date_format' => 'j F Y',
'time_format' => 'G \h i \m\i\n',
'start_of_week' => 1,
'permalink_structure' => '/%category%/%postname%/',
'category_base' => 'categorie',
'tag_base' => 'tag',
'image_default_link_type' => 'none',
'users_can_register' => 0,
'admin_email' => '[email protected]',
'avatar_default' => 'blank',
'rss_use_excerpt' => 1
);
foreach ( $options as $option => $newvalue ) {
update_option( $option, $newvalue );
}
// Updates rules for permalink structure.
$wp_rewrite->flush_rules();
$hello = WP_PLUGIN_DIR . '/hello.php';
if ( file_exists( $hello ) ) {
@unlink( $hello );
}
}
}
add_action( 'after_switch_theme', 'thistle_active_theme' );
if ( ! function_exists( 'thistle_setup_theme' ) ) {
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the `after_setup_theme` hook,
* which runs before the init hook. The init hook is too late for some features,
* such as indicating support for post thumbnails.
*/
function thistle_setup_theme() {
/*
* Makes theme available for translation.
* Translations can be filed in the `/languages/` directory.
*/
load_theme_textdomain( THISTLE_TEXT_DOMAIN, THISTLE_PATH . '/languages' );
//
add_filter( 'xmlrpc_enabled', '__return_false' );
// Switches default core markup for gallery and caption to output valid HTML5.
add_theme_support( 'html5', array(
'gallery',
'caption'
) );
/*
* Enables support for Post Thumbnails on posts and pages.
* Post Thumbnail on pages will be used to construct Open Graph.
*/
add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );
/*
* Enables support for Excerpt on pages.
* Excerpt on pages will be used to construct Open Graph and
* `<meta name="description">`.
*/
add_post_type_support( 'page', 'excerpt' );
// Adds default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );
/*
* Lets WordPress manage the document title.
* By adding theme support, we declare that this theme does not
* use a hard-coded `<title>` tag in the document head, and expect
* WordPress to provide it for us.
*/
add_theme_support( 'title-tag' );
/*
* Adds a stylesheet to have the same styles between the site
* and the visual editor (TinyMCE) when writing.
*
* This file should contain at least: https://codex.wordpress.org/CSS
*/
$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
add_editor_style( array( THISTLE_CHILD_URI . '/assets/styles/editor-style' . $min . '.css' ) );
/*
* Enables support for some specific features of Thistle.
*
* - `thistle-social-meta-tags` ouputs Open Graph and Twitter Card
* for post, page, attachment and author.
* - `thistle-description-meta-tag` outputs description `<meta>` tag
* for the current page with the help of `post_excerpt` and a new field
* added into the general options panel and the customizer for home page.
*
* @see includes/share.php
* @see includes/seo.php
*/
add_theme_support( 'thistle-social-meta-tags' );
add_theme_support( 'thistle-description-meta-tag' );
}
}
add_action( 'after_setup_theme', 'thistle_setup_theme' );
if ( ! function_exists( 'thistle_remove_generator' ) ) {
/**
* Removes WordPress version from `<head>`, RSS.
*
* The WordPress version can be found into readme.html. To prevent someone
* from looking at this information, the access to this file should be denied
* with the help of an .htaccess file.
*/
function thistle_remove_generator() {
// <meta name="generator" />
remove_action( 'wp_head', 'wp_generator' );
// RSS: <generator>http://wordpress.org/?v=x.x.x</generator>
add_filter( 'the_generator', '__return_empty_string' );
}
}
add_action( 'init', 'thistle_remove_generator' );
if ( ! function_exists( 'thistle_javascript_detection' ) ) {
/**
* Handles JavaScript detection.
*
* Replaces the HTML class `no-js` by `js` on the <html> element when
* JavaScript is detected.
*/
function thistle_javascript_detection() {
echo '<script>(function(html){html.className=html.className.replace(/\bno-js\b/,"js")})(document.documentElement)</script>' . "\n";
}
}
add_action( 'wp_head', 'thistle_javascript_detection', 0 );
if ( ! function_exists( 'thistle_clean_head' ) ) {
/**
* Cleans `<head>` HTML element.
*/
function thistle_clean_head() {
/*
* Hides the shortlink for a post, page, attachment, or blog.
* Default shortlink support is limited to providing `?p=` style links for posts.
* It's possible to short-circuit this function via the `pre_get_shortlink` filter.
*/
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
/*
* Hides the link to the Windows Live Writer manifest file.
*
* @link http://msdn.microsoft.com/en-us/library/bb463265.aspx
*/
remove_action( 'wp_head', 'wlwmanifest_link' );
/*
* Hides the link to the Really Simple Discovery service endpoint.
*
* @link http://archipelago.phrasewise.com/rsd
*/
remove_action( 'wp_head', 'rsd_link' );
// Hides the REST API link tag into page header.
remove_action( 'wp_head', 'rest_output_link_wp_head' );
}
}
add_action( 'init', 'thistle_clean_head' );
if ( ! function_exists( 'thistle_clean_http_headers' ) ) {
/**
* Cleans HTTP headers.
*/
function thistle_clean_http_headers() {
// Removes the Link for the REST API.
remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
// Removes rel=shortlink Link.
remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
}
}
add_action( 'init', 'thistle_clean_http_headers' );
if ( ! function_exists( 'thistle_disable_rest_api' ) ) {
/**
* Disables all routes of the REST API for non logged users except
* the `/oembed/1.0/embed` one.
*/
function thistle_disable_rest_api( $response, $server, $request ) {
if ( $request->get_route() != '/oembed/1.0/embed' && ! is_user_logged_in() ) {
return new WP_Error( 'rest_no_route', __( 'No route was found matching the URL and request method' ), array( 'status' => 404 ) );
}
return $response;
}
}
add_filter( 'rest_pre_dispatch', 'thistle_disable_rest_api', 10, 3 );
if ( ! function_exists( 'thistle_disable_emoji' ) ) {
/**
* Disables the emoji's feature which is enabled by default since WordPress 4.2.
*/
function thistle_disable_emoji() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
add_filter( 'emoji_svg_url', '__return_empty_string' );
add_filter( 'tiny_mce_plugins', function ( $plugins ) { return array_diff( $plugins, array( 'wpemoji' ) ); } );
}
}
add_action( 'init', 'thistle_disable_emoji' );
if ( ! function_exists( 'thistle_disable_smilies' ) ) {
/**
* Disables the convertion of smiley code to the icon graphic file equivalent.
*
* @link https://codex.wordpress.org/Using_Smilies
*/
function thistle_disable_smilies() {
add_filter( 'pre_option_use_smilies', '__return_null' );
}
}
add_action( 'init', 'thistle_disable_smilies' );
if ( ! function_exists( 'thistle_remove_postcustom_support' ) ) {
/**
* Removes support for Custom Fields on posts and pages.
*
* IMHO, this WordPress feature is not a good idea because you can
* modify the key of the post meta by error and break the behaviour.
*/
function thistle_remove_postcustom_support() {
remove_post_type_support( 'post', 'custom-fields' );
remove_post_type_support( 'page', 'custom-fields' );
}
}
add_action( 'init', 'thistle_remove_postcustom_support' );
if ( ! function_exists( 'thistle_register_svgxuse' ) ) {
/**
* Registers script to use svgxuse.
*
* svgxuse is a simple polyfill that fetches external SVGs referenced
* in use elements when the browser itself fails to do so.
*
* @link https://github.com/Keyamoon/svgxuse
*/
function thistle_register_svgxuse() {
$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_register_script( 'thistle-svgxuse', THISTLE_CHILD_URI . '/assets/vendor/svgxuse/svgxuse' . $min . '.js', array(), null, true );
}
}
add_action( 'init', 'thistle_register_svgxuse' );
if ( ! function_exists( 'thistle_add_svgxuse_defer_attribute' ) ) {
/**
* Adds the `defer` attribute on the `<script>` HTML element for svgxuse.
*
* @param string $tag The `<script>` tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $src The script's source URL.
* @return
*/
function thistle_add_svgxuse_defer_attribute( $tag, $handle, $src ) {
if ( $handle == 'thistle-svgxuse' ) {
$tag = str_replace( ' src', ' defer src', $tag );
}
return $tag;
}
}
add_action( 'script_loader_tag', 'thistle_add_svgxuse_defer_attribute', 10, 3 );
/**
* Disables the option to publish on your blog using email because
* this functionality is deprecated and will be removed in an upcoming release.
*/
add_filter( 'enable_post_by_email_configuration', '__return_false' );
if ( ! function_exists( 'thistle_remove_wptexturize_filter' ) ) {
/**
* Short-circuits the wptexturize function if site language is French.
*
* The wptexturize behaviour doesn't care about French typography rules
* and it's a waste of time during the execution.
*
* If you love typography, you should favour a project like JolyTypo.
*/
function thistle_remove_wptexturize_filter() {
add_filter( 'run_wptexturize', '__return_false' );
}
}
add_action( 'init', 'thistle_remove_wptexturize_filter' );
if ( ! function_exists( 'thistle_sanitize_option' ) ) {
/**
* Sanitises various option values based on the nature of the option.
*
* This function is the sucession of `sanitize_option`. It will be used
* to check new option in `register_setting` and `$wp_customize->add_setting` methods.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $value The sanitized option value.
* @param string $option The option name.
* @param string $original_value The original value passed to the function.
* @return string Sanitized value.
*/
function thistle_sanitize_option( $value, $option, $original_value ) {
global $wpdb;
$error = '';
switch ( $option ) {
case 'thistle_blogdescription' :
$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
if ( is_wp_error( $value ) ) {
$error = $value->get_error_message();
} else {
$value = wp_kses_post( $value );
$value = esc_html( $value );
}
break;
}
if ( ! empty( $error ) ) {
$value = get_option( $option );
if ( function_exists( 'add_settings_error' ) ) {
add_settings_error( $option, 'invalid_' . $option, $error );
}
}
return $value;
}
}
if ( ! function_exists( 'thistle_enqueue_assets' ) ) {
/**
* Enqueues assets (if exist) automaticaly trying to follow
* (as far as possible) the template hierarchy behaviour
* and the WP naming convention.
*
* By default, Thistle enqueues one CSS file `style.css` and two
* JS files `script.js` and `svgxuse.js` except in 404 case.
* It will also enqueue needed assets when your content contains
* a gallery shortcode ;)
*
* To determine which assets file to enqueue, Thistle tries to load
* two different files on its own:
*
* 1. Named with the current post type when you are on:
* a single post type, a taxonomy archive or a post type archive.
* 2. Named matching the template file used.
*
* BTW, if the two files exist, Thistle loads all of them.
*
* By default, Thistle pushes all JS at the end of the document
* and sets media HTML attribut to `screen` for CSS.
*/
function thistle_enqueue_assets() {
global $template;
// Redefines defaults WP args for scripts and styles.
$style_atts = array( 'deps' => array(), 'ver' => null, 'media' => 'screen' );
$script_atts = array( 'deps' => array(), 'ver' => null, 'in_footer' => true );
$assets = array( 'styles' => array(), 'scripts' => array() );
$post_type = '';
$template = pathinfo( $template, PATHINFO_FILENAME );
$queried_object = get_queried_object();
$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
if ( ! is_404() ) {
// Registers defaults files.
$assets['styles']['style'] = array( 'src' => 'style.css' );
$assets['scripts']['svgxuse'] = array();
$assets['scripts']['script'] = array(
'src' => 'script.js',
'data' => array(
'THISTLE' => array(
'url' => THISTLE_CHILD_URI,
'ajax_url' => admin_url( 'admin-ajax.php' ),
'debug' => defined( 'WP_DEBUG' ) ? WP_DEBUG : false
)
)
);
// Registers assets for the gallery shortcode.
if ( (is_singular() && thistle_has_gallery_shortcode()) || apply_filters( 'thistle_get_gallery_template_part', false ) ) {
$assets['styles']['imagegallery'] = array( 'src' => 'imagegallery.css' );
$assets['scripts']['imagegallery'] = array();
}
// Tries to find the current post type.
if ( is_post_type_archive() ) {
$post_type = $queried_object->name;
} elseif ( is_category() || is_tag() || is_tax() ) {
$post_type = get_taxonomy( $queried_object->taxonomy )->object_type[0];
} elseif ( is_single() ) {
$post_type = $queried_object->post_type;
} elseif ( is_home() || is_archive() ) {
$post_type = 'post';
}
// If on a page related to a post type (archive, tax, single, date, etc)
if ( $post_type ) {
if ( file_exists( THISTLE_CHILD_PATH . '/assets/styles/' . $post_type . $min . '.css' ) ) {
$assets['styles'][ $post_type ] = array( 'src' => $post_type . '.css' );
}
if ( file_exists( THISTLE_CHILD_PATH . '/assets/scripts/' . $post_type . $min . '.js' ) ) {
$assets['scripts'][ $post_type ] = array( 'src' => $post_type . '.js' );
}
}
}
// Tries to register assets which have the same name as the matching template file.
if ( file_exists( THISTLE_CHILD_PATH . '/assets/styles/' . $template . $min . '.css' ) ) {
$assets['styles'][ $template ] = array( 'src' => $template . '.css' );
}
if ( file_exists( THISTLE_CHILD_PATH . '/assets/scripts/' . $template . $min . '.js' ) ) {
$assets['scripts'][ $template ] = array( 'src' => $template . '.js' );
}
if ( WP_DEBUG && file_exists( THISTLE_CHILD_PATH . '/assets/styles/debug.css' ) ) {
$assets['styles']['debug'] = array( 'src' => 'debug.css' );
}
/**
* Filters the array of enqueued styles and scripts before processing
* for output.
*
* @param array $assets The list of enqueued assets about to be processed.
* @param string $template The template used for the current content.
* @param string $post_type
*/
$assets = apply_filters( 'thistle_enqueue_assets', $assets, $template, $post_type );
$assets['styles'] = array_map( function( $s ) use( $style_atts ) { return wp_parse_args( $s, $style_atts ); }, $assets['styles'] );
$assets['scripts'] = array_map( function( $s ) use( $script_atts ) { return wp_parse_args( $s, $script_atts ); }, $assets['scripts'] );
foreach ( $assets['styles'] as $handle => $style ) {
$handle = $handle[0] == '!' ? $handle : 'thistle-' . $handle;
if ( isset( $style['src'] ) && mb_strpos( $style['src'], 'http' ) === false ) {
$style['src'] = THISTLE_CHILD_URI . '/assets/styles/' . $style['src'];
$style['src'] = str_replace( '.css', $min . '.css', $style['src'] );
}
wp_enqueue_style( $handle, $style['src'], $style['deps'], $style['ver'], $style['media'] );
}
foreach ( $assets['scripts'] as $handle => $script ) {
$handle = $handle[0] == '!' ? $handle : 'thistle-' . $handle;
if ( isset( $script['src'] ) && mb_strpos( $script['src'], 'http' ) === false ) {
$script['src'] = THISTLE_CHILD_URI . '/assets/scripts/' . $script['src'];
$script['src'] = str_replace( '.js', $min . '.js', $script['src'] );
}
if ( ! isset( $script['src'] ) ) {
wp_enqueue_script( $handle );
} else {
wp_enqueue_script( $handle, $script['src'], $script['deps'], $script['ver'], $script['in_footer'] );
}
if ( isset( $script['data'] ) ) {
foreach ( $script['data'] as $object_name => $data ) {
wp_localize_script( $handle, $object_name, $data );
}
}
}
}
}
add_action( 'wp_enqueue_scripts', 'thistle_enqueue_assets' );
if ( ! function_exists( 'thistle_change_dependencies_default_version' ) ) {
/**
* Transforms the default version of dependencies to hide the WP version in URLs.
*
* @param WP_Styles|WP_Scripts $dependencies WP_Dependencies instance, passed by reference.
*/
function thistle_change_dependencies_default_version( $dependencies ) {
if ( ! is_admin() ) {
$dependencies->default_version = md5( ABSPATH . '/wp-version/' . $dependencies->default_version );
}
}
}
add_action( 'wp_default_styles', 'thistle_change_dependencies_default_version', PHP_INT_MAX );
add_action( 'wp_default_scripts', 'thistle_change_dependencies_default_version', PHP_INT_MAX );
require_once THISTLE_PATH . '/includes/attachment.php';
require_once THISTLE_PATH . '/includes/author.php';
require_once THISTLE_PATH . '/includes/category.php';
require_once THISTLE_PATH . '/includes/editor.php';
require_once THISTLE_PATH . '/includes/embed.php';
require_once THISTLE_PATH . '/includes/excerpt.php';
require_once THISTLE_PATH . '/includes/gallery-shortcode.php';
require_once THISTLE_PATH . '/includes/search.php';
require_once THISTLE_PATH . '/includes/seo.php';
require_once THISTLE_PATH . '/includes/share.php';
require_once THISTLE_PATH . '/includes/thumbnail.php';
require_once THISTLE_PATH . '/includes/users.php';
require_once THISTLE_PATH . '/includes/admin.php';
require_once THISTLE_PATH . '/includes/post-type.php';
require_once THISTLE_PATH . '/includes/customize.php';
require_once THISTLE_PATH . '/includes/paged.php';