forked from WordPress/phpdoc-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
267 lines (228 loc) · 6.66 KB
/
plugin.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
<?php
/**
* Plugin Name: WP Parser
* Description: Create a function reference site powered by WordPress
* Author: Ryan McCue and Paul Gibbs
* Plugin URI: https://github.com/rmccue/WP-Parser
* Version:
*/
namespace WP_Parser;
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require __DIR__ . '/vendor/autoload.php';
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
\WP_CLI::add_command( 'funcref', __NAMESPACE__ . '\\Command' );
}
add_action( 'init', __NAMESPACE__ . '\\register_post_types' );
add_action( 'init', __NAMESPACE__ . '\\register_taxonomies' );
add_filter( 'wp_parser_get_arguments', __NAMESPACE__ . '\\make_args_safe' );
add_filter( 'wp_parser_return_type', __NAMESPACE__ . '\\humanize_separator' );
add_filter( 'the_content', __NAMESPACE__ . '\\expand_content' );
add_filter( 'the_content', __NAMESPACE__ . '\\autop_for_non_funcref' );
remove_filter( 'the_content', 'wpautop' );
/**
* Register the function and class post types
*/
function register_post_types() {
$supports = array(
'comments',
'custom-fields',
'editor',
'excerpt',
'revisions',
'title',
);
// Functions
register_post_type(
'wpapi-function',
array(
'has_archive' => 'functions',
'label' => __( 'Functions', 'wp-parser' ),
'public' => true,
'rewrite' => array(
'feeds' => false,
'slug' => 'function',
'with_front' => false,
),
'supports' => $supports,
)
);
// Methods
add_rewrite_rule( 'method/([^/]+)/([^/]+)/?$', 'index.php?post_type=wpapi-function&name=$matches[1]-$matches[2]', 'top' );
// Classes
register_post_type(
'wpapi-class',
array(
'has_archive' => 'classes',
'label' => __( 'Classes', 'wp-parser' ),
'public' => true,
'rewrite' => array(
'feeds' => false,
'slug' => 'class',
'with_front' => false,
),
'supports' => $supports,
)
);
// Hooks
register_post_type(
'wpapi-hook',
array(
'has_archive' => 'hooks',
'label' => __( 'Hooks', 'wp-parser' ),
'public' => true,
'rewrite' => array(
'feeds' => false,
'slug' => 'hook',
'with_front' => false,
),
'supports' => $supports,
)
);
}
/**
* Register the file and @since taxonomies
*/
function register_taxonomies() {
// Files
register_taxonomy(
'wpapi-source-file',
array( 'wpapi-class', 'wpapi-function', 'wpapi-hook' ),
array(
'label' => __( 'Files', 'wp-parser' ),
'public' => true,
'rewrite' => array( 'slug' => 'files' ),
'sort' => false,
'update_count_callback' => '_update_post_term_count',
)
);
// Package
register_taxonomy(
'wpapi-package',
array( 'wpapi-class', 'wpapi-function', 'wpapi-hook' ),
array(
'hierarchical' => true,
'label' => '@package',
'public' => true,
'rewrite' => array( 'slug' => 'package' ),
'sort' => false,
'update_count_callback' => '_update_post_term_count',
)
);
// @since
register_taxonomy(
'wpapi-since',
array( 'wpapi-class', 'wpapi-function', 'wpapi-hook' ),
array(
'hierarchical' => true,
'label' => __( '@since', 'wp-parser' ),
'public' => true,
'rewrite' => array( 'slug' => 'since' ),
'sort' => false,
'update_count_callback' => '_update_post_term_count',
)
);
}
function method_permalink( $link, $post ) {
if ( $post->post_type !== 'wpapi-function' || $post->post_parent == 0 ) {
return $link;
}
list( $class, $method ) = explode( '-', $post->post_name );
$link = home_url( user_trailingslashit( "method/$class/$method" ) );
return $link;
}
add_filter( 'post_type_link', __NAMESPACE__ . '\\method_permalink', 10, 2 );
/**
* Raw phpDoc could potentially introduce unsafe markup into the HTML, so we sanitise it here.
*
* @param array $args Parameter arguments to make safe
*
* @return array
*/
function make_args_safe( $args ) {
$filters = array(
'wp_filter_kses',
'make_clickable',
'force_balance_tags',
'wptexturize',
'convert_smilies',
'convert_chars',
'stripslashes_deep',
);
foreach ( $args as &$arg ) {
foreach ( $arg as &$value ) {
foreach ( $filters as $filter_function ) {
if ( is_array( $value ) ) {
foreach ( $value as &$v ) {
$v = call_user_func( $filter_function, $v );
}
} else {
$value = call_user_func( $filter_function, $value );
}
}
}
}
return apply_filters( 'wp_parser_make_args_safe', $args );
}
/**
* Replace separators with a more readable version
*
* @param string $type Variable type
*
* @return string
*/
function humanize_separator( $type ) {
return str_replace( '|', '<span class="wpapi-item-type-or">' . _x( ' or ', 'separator', 'wp-parser' ) . '</span>', $type );
}
/**
* Extend the post's content with function reference pieces
*
* @param string $content Unfiltered content
*
* @return string Content with Function reference pieces added
*/
function expand_content( $content ) {
$post = get_post();
if ( $post->post_type !== 'wpapi-class' && $post->post_type !== 'wpapi-function' && $post->post_type !== 'wpapi-hook' ) {
return $content;
}
$before_content = get_prototype();
$before_content .= '<p class="wpfuncref-description">' . get_the_excerpt() . '</p>';
$before_content .= '<div class="wpfuncref-longdesc">';
$after_content = '</div>';
$after_content .= '<div class="wpfuncref-arguments"><h3>Arguments</h3>';
$args = get_arguments();
foreach ( $args as $arg ) {
$after_content .= '<div class="wpfuncref-arg">';
$after_content .= '<h4><code><span class="type">' . implode( '|', $arg['types'] ) . '</span> <span class="variable">' . $arg['name'] . '</span></code></h4>';
if ( ! empty( $arg['desc'] ) ) {
$after_content .= wpautop( $arg['desc'], false );
}
$after_content .= '</div>';
}
$after_content .= '</div>';
$source = get_source_link();
if ( $source ) {
$after_content .= '<a href="' . $source . '">Source</a>';
}
$before_content = apply_filters( 'wp_parser_before_content', $before_content );
$after_content = apply_filters( 'wp_parser_after_content', $after_content );
return $before_content . $content . $after_content;
}
/**
* Re-enable autopee for the non-funcref posts
*
* We can't selectively filter the_content for wpautop, so we remove it and
* readd this to check instead.
*
* @param string $content Unfiltered content
*
* @return string Autopeed content
*/
function autop_for_non_funcref( $content ) {
$post = get_post();
if ( $post->post_type !== 'wpapi-class' && $post->post_type !== 'wpapi-function' && $post->post_type !== 'wpapi-hook' ) {
$content = wpautop( $content );
}
return $content;
}