-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathclass-plugin.php
297 lines (252 loc) · 7.34 KB
/
class-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
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
<?php
namespace WP_Parser;
/**
* Main plugin's class. Registers things and adds WP CLI command.
*/
class Plugin {
/**
* @var \WP_Parser\Relationships
*/
public $relationships;
public function on_load() {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
\WP_CLI::add_command( 'parser', __NAMESPACE__ . '\\Command' );
}
$this->relationships = new Relationships;
add_action( 'init', array( $this, 'register_post_types' ), 11 );
add_action( 'init', array( $this, 'register_taxonomies' ), 11 );
add_action( 'wp', array( $this, 'replace_autop' ), 11 );
add_filter( 'wp_parser_get_arguments', array( $this, 'make_args_safe' ) );
add_filter( 'wp_parser_return_type', array( $this, 'humanize_separator' ) );
add_filter( 'post_type_link', array( $this, 'method_permalink' ), 10, 2 );
}
/**
* Register the function and class post types
*/
public function register_post_types() {
$supports = array(
'comments',
'custom-fields',
'excerpt',
'revisions',
'title',
);
if ( ! post_type_exists( 'wp-parser-function' ) ) {
register_post_type(
'wp-parser-function',
array(
'has_archive' => 'functions',
'label' => __( 'Functions', 'wp-parser' ),
'public' => true,
'rewrite' => array(
'feeds' => false,
'slug' => 'function',
'with_front' => false,
),
'supports' => $supports,
)
);
}
if ( ! post_type_exists( 'wp-parser-method' ) ) {
add_rewrite_rule( 'method/([^/]+)/([^/]+)/?$', 'index.php?post_type=wp-parser-method&name=$matches[1]-$matches[2]', 'top' );
register_post_type(
'wp-parser-method',
array(
'has_archive' => 'methods',
'label' => __( 'Methods', 'wp-parser' ),
'public' => true,
'rewrite' => array(
'feeds' => false,
'slug' => 'method',
'with_front' => false,
),
'supports' => $supports,
)
);
}
if ( ! post_type_exists( 'wp-parser-class' ) ) {
register_post_type(
'wp-parser-class',
array(
'has_archive' => 'classes',
'label' => __( 'Classes', 'wp-parser' ),
'public' => true,
'rewrite' => array(
'feeds' => false,
'slug' => 'class',
'with_front' => false,
),
'supports' => $supports,
)
);
}
if ( ! post_type_exists( 'wp-parser-hook' ) ) {
register_post_type(
'wp-parser-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
*/
public function register_taxonomies() {
$object_types = array( 'wp-parser-class', 'wp-parser-method', 'wp-parser-function', 'wp-parser-hook' );
if ( ! taxonomy_exists( 'wp-parser-source-file' ) ) {
register_taxonomy(
'wp-parser-source-file',
$object_types,
array(
'label' => __( 'Files', 'wp-parser' ),
'public' => true,
'rewrite' => array( 'slug' => 'files' ),
'sort' => false,
'update_count_callback' => '_update_post_term_count',
)
);
}
if ( ! taxonomy_exists( 'wp-parser-package' ) ) {
register_taxonomy(
'wp-parser-package',
$object_types,
array(
'hierarchical' => true,
'label' => '@package',
'public' => true,
'rewrite' => array( 'slug' => 'package' ),
'sort' => false,
'update_count_callback' => '_update_post_term_count',
)
);
}
if ( ! taxonomy_exists( 'wp-parser-since' ) ) {
register_taxonomy(
'wp-parser-since',
$object_types,
array(
'hierarchical' => true,
'label' => __( '@since', 'wp-parser' ),
'public' => true,
'rewrite' => array( 'slug' => 'since' ),
'sort' => false,
'update_count_callback' => '_update_post_term_count',
)
);
}
if ( ! taxonomy_exists( 'wp-parser-namespace' ) ) {
register_taxonomy(
'wp-parser-namespace',
$object_types,
array(
'hierarchical' => true,
'label' => __( 'Namespaces', 'wp-parser' ),
'public' => true,
'rewrite' => array( 'slug' => 'namespace' ),
'sort' => false,
'update_count_callback' => '_update_post_term_count',
)
);
}
}
/**
* @param string $link
* @param \WP_Post $post
*
* @return string|void
*/
public function method_permalink( $link, $post ) {
if ( 'wp-parser-method' !== $post->post_type || 0 == $post->post_parent ) {
return $link;
}
list( $class, $method ) = explode( '-', $post->post_name );
$link = home_url( user_trailingslashit( "method/$class/$method" ) );
return $link;
}
/**
* 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
*/
public function make_args_safe( $args ) {
array_walk_recursive( $args, array( $this, 'sanitize_argument' ) );
return apply_filters( 'wp_parser_make_args_safe', $args );
}
/**
* @param mixed $value
*
* @return mixed
*/
public function sanitize_argument( &$value ) {
static $filters = array(
'wp_filter_kses',
'make_clickable',
'force_balance_tags',
'wptexturize',
'convert_smilies',
'convert_chars',
'stripslashes_deep',
);
foreach ( $filters as $filter ) {
$value = call_user_func( $filter, $value );
}
return $value;
}
/**
* Replace separators with a more readable version
*
* @param string $type Variable type
*
* @return string
*/
public function humanize_separator( $type ) {
return str_replace( '|', '<span class="wp-parser-item-type-or">' . _x( ' or ', 'separator', 'wp-parser' ) . '</span>', $type );
}
/**
* Replaces the normal content autop with a custom version to skip parser types.
*
* By running this filter replacement late on the wp hook, plugins are given ample
* time to remove autop themselves. If they have removed autop, then the maybe_autop
* filter is not added. There are potentially conflicting edge cases, but this
* should catch at least some of them.
*
* Other plugins can also remove this functionality fairly easily by removing the wp
* hook and stopping this process if needed.
*/
public function replace_autop() {
if ( has_filter( 'the_content', 'wpautop' ) ) {
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', array( $this, 'maybe_autop' ) );
}
}
/**
* Autop's all post content except for wp-parser types.
*
* @param string $content The content to filter.
* @return string The filtered content, conditionally with autop run.
*/
public function maybe_autop( $content ) {
// Get and cache the blacklist
static $blacklist;
if ( is_null( $blacklist ) ) {
$blacklist = apply_filters( 'wp_parser_autop_blacklist', array(
'wp-parser-function' => true,
'wp-parser-hook' => true,
'wp-parser-class' => true,
'wp-parser-method' => true,
) );
}
return ( isset( $blacklist[ get_post_type() ] ) ) ? $content : wpautop( $content );
}
}