-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwporg-docs.php
More file actions
503 lines (429 loc) · 15 KB
/
wporg-docs.php
File metadata and controls
503 lines (429 loc) · 15 KB
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
<?php
// From wporg-docs
namespace Devhub{
/**
* Added because the wporg-developer theme depends on a constant defined outside
* the theme in a plugin called plugins-directory. There's also a definition for
* this constant in other themes like wporg, but it is often wrapped in a
* condition to make sure it is not already defined like we are doing here.
*/
if ( ! defined( 'WPORGPATH' ) ) {
$path = array(
'',
'inc',
'wporg',
'',
);
// C:\MAMP\htdocs\inventorypresser.com\wp-content\themes\wporg-docs\inc\wporg\
define( 'WPORGPATH', __DIR__ . implode( DIRECTORY_SEPARATOR, $path ) );
}
/**
* Registrations (post type, taxonomies, etc).
*/
require __DIR__ . '/inc/registrations.php';
/**
* HTML head tags and customizations.
*/
require __DIR__ . '/inc/head.php';
/**
* Custom template tags for this theme.
*/
require __DIR__ . '/inc/template-tags.php';
/**
* Custom functions that act independently of the theme templates.
*/
require __DIR__ . '/inc/extras.php';
/**
* Class for editing parsed content on the Function, Class, Hook, and Method screens.
*/
require_once __DIR__ . '/inc/parsed-content.php';
if ( ! function_exists( 'loop_pagination' ) ) {
require __DIR__ . '/inc/loop-pagination.php';
}
if ( ! function_exists( 'breadcrumb_trail' ) ) {
require __DIR__ . '/inc/breadcrumb-trail.php';
}
/**
* User-submitted content (comments, notes, etc).
*/
require __DIR__ . '/inc/user-content.php';
/**
* Docs importer.
*/
if ( class_exists( '\\WordPressdotorg\\Markdown\\Importer' ) ) {
// Docs Importer base class.
require __DIR__ . '/inc/import-docs.php';
// Block Editor handbook.
require __DIR__ . '/inc/import-block-editor.php';
// Coding Standards handbook.
require __DIR__ . '/inc/import-coding-standards.php';
// REST API handbook.
require __DIR__ . '/inc/rest-api.php';
}
/**
* Explanations for functions. hooks, classes, and methods.
*/
require __DIR__ . '/inc/explanations.php';
/**
* Handbooks.
*/
require __DIR__ . '/inc/handbooks.php';
/**
* Redirects.
*/
require __DIR__ . '/inc/redirects.php';
/**
* Content formatting.
*/
require __DIR__ . '/inc/formatting.php';
/**
* Search query.
*/
require __DIR__ . '/inc/search.php';
/**
* Parser customizations.
*/
require __DIR__ . '/inc/parser.php';
/**
* Admin area customizations.
*/
if ( is_admin() ) {
require __DIR__ . '/inc/admin.php';
}
/**
* Set the content width based on the theme's design and stylesheet.
*/
if ( ! isset( $content_width ) ) {
$content_width = 640; /* pixels */
}
add_action( 'init', __NAMESPACE__ . '\\init' );
add_action( 'widgets_init', __NAMESPACE__ . '\\widgets_init' );
function init() {
register_menus();
add_action( 'pre_get_posts', __NAMESPACE__ . '\\pre_get_posts' );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\theme_scripts_styles' );
add_action( 'wp_head', __NAMESPACE__ . '\\header_js' );
add_action( 'add_meta_boxes', __NAMESPACE__ . '\\rename_comments_meta_box', 10, 2 );
add_filter( 'post_type_link', __NAMESPACE__ . '\\method_permalink', 11, 2 );
add_filter( 'term_link', __NAMESPACE__ . '\\taxonomy_permalink', 10, 3 );
add_theme_support( 'automatic-feed-links' );
add_theme_support( 'post-thumbnails' );
// Modify default breadcrumbs.
add_filter( 'breadcrumb_trail_items', __NAMESPACE__ . '\\breadcrumb_trail_items_for_hooks', 10, 2 );
add_filter( 'breadcrumb_trail_items', __NAMESPACE__ . '\\breadcrumb_trail_items_for_handbook_root', 10, 2 );
add_filter( 'syntaxhighlighter_htmlresult', __NAMESPACE__ . '\\syntaxhighlighter_htmlresult' );
/**
* Use the same single & archive block templates for all four wp-parser
* post types.
*/
add_filter( 'template_include', __NAMESPACE__ . '\\wporg_docs_provide_template' );
// Add a body tag class to simplify our CSS rules for four post types.
add_filter( 'body_class', __NAMESPACE__ . '\\wporg_docs_body_class' );
}
/**
* Adds the CSS class 'single-wp-parser' to singles in post types created by
* wp-parser.
*
* @param string[] $classes An array of body class names.
*/
function wporg_docs_body_class( $classes ) {
if ( is_singular( get_parsed_post_types() ) ) {
$classes[] = 'single-wp-parser';
}
return $classes;
}
function wporg_docs_provide_template( $template ) {
$post_types = get_parsed_post_types();
if ( is_singular( $post_types ) ) {
return locate_block_template( 'single-wp-parser-function', 'single-wp-parser-function', array( 'single-wp-parser-function' ) );
}
if ( is_post_type_archive( $post_types ) || is_tax( 'wp-parser-source-file' ) ) {
return locate_block_template( 'archive-wp-parser-function', 'archive-wp-parser-function', array( 'archive-wp-parser-function' ) );
}
return $template;
}
/**
* Fix breadcrumb for hooks.
*
* A hook has a parent (the function containing it), which causes the Breadcrumb
* Trail plugin to introduce trail items related to the parent that shouldn't
* be shown.
*
* @param array $items The breadcrumb trail items
* @param array $args Original args
* @return array
*/
function breadcrumb_trail_items_for_hooks( $items, $args ) {
$post_type = 'wp-parser-hook';
// Bail early when not the single archive for hook
if ( ! is_singular() || $post_type !== get_post_type() || ! isset( $items[4] ) ) {
return $items;
}
$post_type_object = get_post_type_object( $post_type );
// Replaces 'Functions' archive link with 'Hooks' archive link
$items[2] = '<a href="' . get_post_type_archive_link( $post_type ) . '">' . $post_type_object->labels->name . '</a>';
// Replace what the plugin thinks is the parent with the hook name
$items[3] = $items[4];
// Unset the last element since it shifted up in trail hierarchy
unset( $items[4] );
return $items;
}
/**
* Fix breadcrumb for handbook root pages.
*
* The handbook root/landing pages do not need a duplicated breadcrumb trail
* item that simply links to the currently loaded page. The trailing breadcrumb
* item is already the unlinked handbook name, which is sufficient.
*
* @param array $items The breadcrumb trail items
* @param array $args Original args
* @return array
*/
function breadcrumb_trail_items_for_handbook_root( $items, $args ) {
// Bail early if not a handbook landing page.
if ( ! function_exists( 'wporg_is_handbook_landing_page' ) || ! wporg_is_handbook_landing_page() ) {
return $items;
}
// Unset link to current handbook.
unset( $items[1] );
return $items;
}
/**
* widgets_init function.
*
* @access public
* @return void
*/
function widgets_init() {
register_sidebar(
array(
'name' => __( 'Sidebar', 'wporg' ),
'id' => 'sidebar-1',
'before_widget' => '<aside id="%1$s" class="box gray widget %2$s">',
'after_widget' => '</div></aside>',
'before_title' => '<h1 class="widget-title">',
'after_title' => '</h1><div class="widget-content">',
)
);
register_sidebar(
array(
'name' => __( 'Landing Page Footer - Left', 'wporg' ),
'id' => 'landing-footer-1',
'description' => __( 'Appears in footer of the primary landing page', 'wporg' ),
'before_widget' => '<div id="%1$s" class="widget box %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widget-title">',
'after_title' => '</h4>',
)
);
register_sidebar(
array(
'name' => __( 'Landing Page Footer - Center', 'wporg' ),
'id' => 'landing-footer-2',
'description' => __( 'Appears in footer of the primary landing page', 'wporg' ),
'before_widget' => '<div id="%1$s" class="widget box %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widget-title">',
'after_title' => '</h4>',
)
);
}
/**
* @param \WP_Query $query
*/
function pre_get_posts( $query ) {
if ( $query->is_main_query() && $query->is_post_type_archive() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
if ( $query->is_main_query() && $query->is_tax() && $query->get( 'wp-parser-source-file' ) ) {
$query->set( 'wp-parser-source-file', str_replace( array( '.php', '/' ), array( '-php', '_' ), $query->query['wp-parser-source-file'] ) );
}
// For search query modifications see DevHub_Search.
}
function register_menus() {
register_nav_menus(
array(
'devhub-menu' => __( 'Developer Resources Menu', 'wporg' ),
'devhub-cli-menu' => __( 'WP-CLI Commands Menu', 'wporg' ),
'reference-home-api' => __( 'Reference API Menu', 'wporg' ),
)
);
}
/**
* Filters the permalink for a wp-parser-method post.
*
* @param string $link The post's permalink.
* @param \WP_Post $post The post in question.
* @return string
*/
function method_permalink( $link, $post ) {
global $wp_rewrite;
if ( ! $wp_rewrite->using_permalinks() || ( 'wp-parser-method' !== $post->post_type ) ) {
return $link;
}
$parts = explode( '-', $post->post_name );
$method = array_pop( $parts );
$class = implode( '-', $parts );
return home_url( user_trailingslashit( "docs/reference/classes/$class/$method" ) );
}
function taxonomy_permalink( $link, $term, $taxonomy ) {
global $wp_rewrite;
if ( ! $wp_rewrite->using_permalinks() ) {
return $link;
}
if ( $taxonomy === 'wp-parser-source-file' ) {
$slug = $term->slug;
if ( substr( $slug, -4 ) === '-php' ) {
$slug = substr( $slug, 0, -4 ) . '.php';
$slug = str_replace( '_', '/', $slug );
}
$link = home_url( user_trailingslashit( "docs/reference/files/$slug" ) );
} elseif ( $taxonomy === 'wp-parser-since' ) {
$link = str_replace( $term->slug, str_replace( '-', '.', $term->slug ), $link );
}
return $link;
}
/**
* Outputs JavaScript intended to appear in the head of the page.
*/
function header_js() {
// Output CSS to hide markup with the class 'hide-if-js'. Ensures the markup is visible if JS is not present.
// Add class 'js' to the body element if JavaScript is enabled
echo "
<script type=\"text/javascript\">
jQuery( '<style>.hide-if-js { display: none; }</style>' ).appendTo( 'head' );
jQuery( function($) {
$( 'body' ).addClass('js');
} );
</script>\n";
}
function theme_scripts_styles() {
$min = defined( 'WP_DEBUG' ) && WP_DEBUG ? '' : '.min';
wp_enqueue_style(
'wp-dev-sass-compiled',
get_stylesheet_directory_uri() . "/wporg-docs{$min}.css",
array(),
wp_get_theme()->get( 'Version' )
);
wp_enqueue_script( 'wporg-developer-navigation', get_stylesheet_directory_uri() . '/js/navigation.js', array(), '20181209', true );
wp_enqueue_script( 'wporg-developer-skip-link-focus-fix', get_stylesheet_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );
wp_enqueue_script( 'wporg-developer-search', get_stylesheet_directory_uri() . '/js/search.js', array(), '20150430', true );
wp_enqueue_script( 'wporg-developer-chapters', get_stylesheet_directory_uri() . '/js/chapters.js', array( 'jquery' ), '20190603' );
wp_enqueue_script( 'wporg-developer-menu', get_stylesheet_directory_uri() . '/js/menu.js', array( 'jquery' ), '20180201', true );
}
/**
* Rename the 'Comments' meta box to 'User Contributed Notes' for reference-editing screens.
*
* @param string $post_type Post type.
* @param WP_Post $post WP_Post object for the current post.
*/
function rename_comments_meta_box( $post_type, $post ) {
if ( is_parsed_post_type( $post_type ) ) {
remove_meta_box( 'commentsdiv', $post_type, 'normal' );
add_meta_box( 'commentsdiv', __( 'User Contributed Notes', 'wporg' ), 'post_comment_meta_box', $post_type, 'normal', 'high' );
}
}
/**
* If a syntax highlighted code block exceeds a given number of lines, wrap the
* markup with other markup to trigger the code expansion/collapse JS handling
* already implemented for the code reference.
*
* @param string $text The pending result of the syntax highlighting.
* @return string
*/
function syntaxhighlighter_htmlresult( $text ) {
// is_admin() is true in front end AJAX requests.
if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
return $text;
}
$new_text = '';
// Collapse is handled for >10 lines. But just go ahead and show the full
// code if that is just barely being exceeded (no one wants to expand to
// see one or two more lines).
$lines_to_show = 12;
$do_collapse = ( substr_count( $text, "\n" ) - 1 ) > $lines_to_show;
if ( $do_collapse ) {
$new_text .= '<section class="source-content">';
$new_text .= '<div class="source-code-container">';
}
$new_text .= $text;
if ( $do_collapse ) {
$new_text .= '</div>';
$new_text .= '<p class="source-code-links"><span>';
$new_text .= '<a href="#source" class="show-complete-source">' . __( 'Expand full source code', 'wporg' ) . '</a>';
$new_text .= '<a href="#source" class="less-complete-source">' . __( 'Collapse full source code', 'wporg' ) . '</a>';
$new_text .= '</span></p>';
$new_text .= '</section>';
}
return $new_text;
}
/**
* Get an array of all parsed post types.
*
* @param string $labels If set to 'labels' post types with their labels are returned.
* @return array
*/
function get_parsed_post_types( $labels = '' ) {
$post_types = array(
'wp-parser-class' => __( 'Classes', 'wporg' ),
'wp-parser-function' => __( 'Functions', 'wporg' ),
'wp-parser-hook' => __( 'Hooks', 'wporg' ),
'wp-parser-method' => __( 'Methods', 'wporg' ),
);
if ( 'labels' !== $labels ) {
return array_keys( $post_types );
}
return $post_types;
}
/**
* Checks if given post type is one of the parsed post types.
*
* @param null|string Optional. The post type. Default null.
* @return bool True if post has a parsed post type
*/
function is_parsed_post_type( $post_type = null ) {
$post_type = $post_type ? $post_type : get_post_type();
return in_array( $post_type, get_parsed_post_types() );
}
/**
* Indicates if the current user can post a user contibuted note.
*
* This only affects parsed post types as they are the only things
* that can have user contributed notes.
*
* A custom check can be performed by hooking the filter
* 'wporg_devhub-can_user_post_note' and returning a
* value other than null.
*
* By default, the ability to post notes is restricted to members of the
* blog.
*
* @param bool $open If the user can post comments in general.
* @param WP_Post $post Post ID or post object.
*
* @return bool True if the user can post a note.
*/
function can_user_post_note( $open, $post ) {
// Only proceed if for a parsed post type.
if ( ! is_parsed_post_type( get_post_type( $post ) ) ) {
return $open;
}
// Permit default logic to be overridden via filter that returns value other than null.
if ( null !== ( $can = apply_filters( 'wporg_devhub-can_user_post_note', null, $post ) ) ) {
return $can;
}
return $open;
}
/**
* Compare two objects by name for sorting.
*
* @param WP_Post $a Post A
* @param WP_Post $b Post B
*
* @return int
*/
function compare_objects_by_name( $a, $b ) {
return strcmp( $a->post_name, $b->post_name );
}
}