@@ -70,6 +70,10 @@ class Importer {
70
70
*/
71
71
public $ errors = array ();
72
72
73
+ /**
74
+ * @var array Cached items of inserted terms
75
+ */
76
+ protected $ inserted_terms = array ();
73
77
74
78
/**
75
79
* Constructor. Sets up post type/taxonomy names.
@@ -96,6 +100,23 @@ public function __construct( array $args = array() ) {
96
100
}
97
101
}
98
102
103
+ protected function insert_term ( $ term , $ taxonomy , $ args = array () ) {
104
+ if ( isset ( $ this ->inserted_terms [ $ taxonomy ][ $ term ] ) ) {
105
+ return $ this ->inserted_terms [ $ taxonomy ][ $ term ];
106
+ }
107
+
108
+ $ parent = isset ( $ args ['parent ' ] ) ? $ args ['parent ' ] : 0 ;
109
+ if ( ! $ inserted_term = term_exists ( $ term , $ taxonomy , $ parent ) ) {
110
+ $ inserted_term = wp_insert_term ( $ term , $ taxonomy , $ args );
111
+ }
112
+
113
+ if ( ! is_wp_error ( $ inserted_term ) ) {
114
+ $ this ->inserted_terms [ $ taxonomy ][ $ term ] = $ inserted_term ;
115
+ }
116
+
117
+ return $ inserted_term ;
118
+ }
119
+
99
120
/**
100
121
* For a specific file, go through and import the file, functions, and classes.
101
122
*
@@ -107,26 +128,18 @@ public function import_file( array $file, $skip_sleep = false, $import_internal
107
128
108
129
// Maybe add this file to the file taxonomy
109
130
$ slug = sanitize_title ( str_replace ( '/ ' , '_ ' , $ file ['path ' ] ) );
110
- $ term = get_term_by ( 'slug ' , $ slug , $ this ->taxonomy_file , ARRAY_A );
111
-
112
- if ( ! $ term ) {
113
-
114
- $ term = wp_insert_term ( $ file ['path ' ], $ this ->taxonomy_file , array ( 'slug ' => $ slug ) );
115
131
116
- if ( is_wp_error ( $ term ) ) {
117
- $ this ->errors [] = sprintf ( 'Problem creating file tax item "%1$s" for %2$s: %3$s ' , $ slug , $ file ['path ' ], $ term ->get_error_message () );
118
-
119
- return ;
120
- }
132
+ $ term = $ this ->insert_term ( $ file ['path ' ], $ this ->taxonomy_file , array ( 'slug ' => $ slug ) );
121
133
122
- // Grab the full term object
123
- $ term = get_term_by ( 'slug ' , $ slug , $ this ->taxonomy_file , ARRAY_A );
134
+ if ( is_wp_error ( $ term ) ) {
135
+ $ this ->errors [] = sprintf ( 'Problem creating file tax item "%1$s" for %2$s: %3$s ' , $ slug , $ file ['path ' ], $ term ->get_error_message () );
136
+ return ;
124
137
}
125
138
126
139
// Store file meta for later use
127
140
$ this ->file_meta = array (
128
141
'docblock ' => $ file ['file ' ], // File docblock
129
- 'term_id ' => $ term [ ' name ' ], // File's term item in the file taxonomy
142
+ 'term_id ' => $ file [ ' path ' ], // Term name in the file taxonomy is the file name
130
143
);
131
144
132
145
// Functions
@@ -201,6 +214,14 @@ public function import_function( array $data, $parent_post_id = 0, $import_inter
201
214
* @return bool|int Post ID of this hook, false if any failure.
202
215
*/
203
216
public function import_hook ( array $ data , $ parent_post_id = 0 , $ import_internal = false ) {
217
+ if ( 0 === strpos ( $ data ['doc ' ]['description ' ], 'This action is documented in ' ) ) {
218
+ return false ;
219
+ } elseif ( 0 === strpos ( $ data ['doc ' ]['description ' ], 'This filter is documented in ' ) ) {
220
+ return false ;
221
+ } elseif ( '' === $ data ['doc ' ]['description ' ] && '' === $ data ['doc ' ]['long_description ' ] ) {
222
+ return false ;
223
+ }
224
+
204
225
$ hook_id = $ this ->import_item ( $ data , $ parent_post_id , $ import_internal , array ( 'post_type ' => $ this ->post_type_hook ) );
205
226
206
227
if ( ! $ hook_id ) {
@@ -230,16 +251,16 @@ protected function import_class( array $data, $import_internal = false ) {
230
251
}
231
252
232
253
// Set class-specific meta
233
- update_post_meta ( $ class_id , '_wp-parser_final ' , (bool ) $ data ['final ' ] );
234
- update_post_meta ( $ class_id , '_wp-parser_abstract ' , (bool ) $ data ['abstract ' ] );
254
+ update_post_meta ( $ class_id , '_wp-parser_final ' , (string ) $ data ['final ' ] );
255
+ update_post_meta ( $ class_id , '_wp-parser_abstract ' , (string ) $ data ['abstract ' ] );
235
256
update_post_meta ( $ class_id , '_wp-parser_extends ' , $ data ['extends ' ] );
236
257
update_post_meta ( $ class_id , '_wp-parser_implements ' , $ data ['implements ' ] );
237
258
update_post_meta ( $ class_id , '_wp-parser_properties ' , $ data ['properties ' ] );
238
259
239
260
// Now add the methods
240
261
foreach ( $ data ['methods ' ] as $ method ) {
241
262
// Namespace method names with the class name
242
- $ method ['name ' ] = $ data ['name ' ] . '- ' . $ method ['name ' ];
263
+ $ method ['name ' ] = $ data ['name ' ] . ':: ' . $ method ['name ' ];
243
264
$ this ->import_method ( $ method , $ class_id , $ import_internal );
244
265
}
245
266
@@ -266,9 +287,9 @@ protected function import_method( array $data, $parent_post_id = 0, $import_inte
266
287
}
267
288
268
289
// Set method-specific meta.
269
- update_post_meta ( $ method_id , '_wp-parser_final ' , (bool ) $ data ['final ' ] );
270
- update_post_meta ( $ method_id , '_wp-parser_abstract ' , (bool ) $ data ['abstract ' ] );
271
- update_post_meta ( $ method_id , '_wp-parser_static ' , (bool ) $ data ['static ' ] );
290
+ update_post_meta ( $ method_id , '_wp-parser_final ' , (string ) $ data ['final ' ] );
291
+ update_post_meta ( $ method_id , '_wp-parser_abstract ' , (string ) $ data ['abstract ' ] );
292
+ update_post_meta ( $ method_id , '_wp-parser_static ' , (string ) $ data ['static ' ] );
272
293
update_post_meta ( $ method_id , '_wp-parser_visibility ' , $ data ['visibility ' ] );
273
294
274
295
// Now add the hooks.
@@ -323,6 +344,10 @@ public function import_item( array $data, $parent_post_id = 0, $import_internal
323
344
return false ;
324
345
}
325
346
347
+ if ( wp_list_filter ( $ data ['doc ' ]['tags ' ], array ( 'name ' => 'ignore ' ) ) ) {
348
+ return false ;
349
+ }
350
+
326
351
$ is_new_post = true ;
327
352
$ slug = sanitize_title ( $ data ['name ' ] );
328
353
$ post_data = wp_parse_args (
@@ -340,9 +365,9 @@ public function import_item( array $data, $parent_post_id = 0, $import_internal
340
365
341
366
// Look for an existing post for this item
342
367
$ existing_post_id = $ wpdb ->get_var (
343
- $ wpdb ->prepare (
344
- "SELECT ID FROM $ wpdb ->posts WHERE post_title = %s AND post_type = %s AND post_parent = %d LIMIT 1 " ,
345
- $ data [ ' name ' ] ,
368
+ $ q = $ wpdb ->prepare (
369
+ "SELECT ID FROM $ wpdb ->posts WHERE post_name = %s AND post_type = %s AND post_parent = %d LIMIT 1 " ,
370
+ $ slug ,
346
371
$ post_data ['post_type ' ],
347
372
(int ) $ parent_post_id
348
373
)
@@ -351,12 +376,15 @@ public function import_item( array $data, $parent_post_id = 0, $import_internal
351
376
// Insert/update the item post
352
377
if ( ! empty ( $ existing_post_id ) ) {
353
378
$ is_new_post = false ;
354
- $ post_data ['ID ' ] = (int ) $ existing_post_id ;
355
- $ ID = wp_update_post ( $ post_data , true );
356
-
379
+ $ ID = $ post_data ['ID ' ] = (int ) $ existing_post_id ;
380
+ $ post_needed_update = array_diff_assoc ( sanitize_post ( $ post_data , 'db ' ), get_post ( $ existing_post_id , ARRAY_A , 'db ' ) );
381
+ if ( $ post_needed_update ) {
382
+ $ ID = wp_update_post ( wp_slash ( $ post_data ), true );
383
+ }
357
384
} else {
358
- $ ID = wp_insert_post ( $ post_data , true );
385
+ $ ID = wp_insert_post ( wp_slash ( $ post_data ) , true );
359
386
}
387
+ $ anything_updated = array ();
360
388
361
389
if ( ! $ ID || is_wp_error ( $ ID ) ) {
362
390
@@ -387,15 +415,16 @@ public function import_item( array $data, $parent_post_id = 0, $import_internal
387
415
388
416
$ since_version = array_shift ( $ since_version );
389
417
$ since_version = $ since_version ['content ' ];
390
- $ since_term = term_exists ( $ since_version , $ this ->taxonomy_since_version );
391
418
392
- if ( ! $ since_term ) {
393
- $ since_term = wp_insert_term ( $ since_version , $ this ->taxonomy_since_version );
394
- }
419
+ $ since_term = $ this ->insert_term ( $ since_version , $ this ->taxonomy_since_version );
395
420
396
421
// Assign the tax item to the post
397
422
if ( ! is_wp_error ( $ since_term ) ) {
423
+ $ added_term_relationship = did_action ( 'added_term_relationship ' );
398
424
wp_set_object_terms ( $ ID , (int ) $ since_term ['term_id ' ], $ this ->taxonomy_since_version );
425
+ if ( did_action ( 'added_term_relationship ' ) > $ added_term_relationship ) {
426
+ $ anything_updated [] = true ;
427
+ }
399
428
} else {
400
429
WP_CLI ::warning ( "\tCannot set @since term: " . $ since_term ->get_error_message () );
401
430
}
@@ -416,7 +445,7 @@ public function import_item( array $data, $parent_post_id = 0, $import_internal
416
445
}
417
446
418
447
$ main_package_id = false ;
419
- $ package_term_args = array ();
448
+ $ package_term_ids = array ();
420
449
421
450
// If the item has any @package/@subpackage markup (or has inherited it from file scope), assign the taxonomy.
422
451
foreach ( $ packages as $ pack_name => $ pack_value ) {
@@ -436,33 +465,45 @@ public function import_item( array $data, $parent_post_id = 0, $import_internal
436
465
}
437
466
438
467
// If the package doesn't already exist in the taxonomy, add it
439
- $ package_term = term_exists ( $ pack_value , $ this ->taxonomy_package , $ package_term_args ['parent ' ] );
440
- if ( ! $ package_term ) {
441
- $ package_term = wp_insert_term ( $ pack_value , $ this ->taxonomy_package , $ package_term_args );
442
- }
468
+ $ package_term = $ this ->insert_term ( $ pack_value , $ this ->taxonomy_package , $ package_term_args );
469
+ $ package_term_ids [] = (int ) $ package_term ['term_id ' ];
443
470
444
471
if ( $ pack_name === 'main ' && $ main_package_id === false && ! is_wp_error ( $ package_term ) ) {
445
472
$ main_package_id = (int ) $ package_term ['term_id ' ];
446
473
}
447
474
448
- // Assign the tax item to the post
449
- if ( ! is_wp_error ( $ package_term ) ) {
450
- wp_set_object_terms ( $ ID , (int ) $ package_term ['term_id ' ], $ this ->taxonomy_package );
451
- } elseif ( is_int ( $ main_package_id ) ) {
452
- WP_CLI ::warning ( "\tCannot set @subpackage term: " . $ package_term ->get_error_message () );
453
- } else {
454
- WP_CLI ::warning ( "\tCannot set @package term: " . $ package_term ->get_error_message () );
475
+ if ( is_wp_error ( $ package_term ) ) {
476
+ if ( is_int ( $ main_package_id ) ) {
477
+ WP_CLI ::warning ( "\tCannot create @subpackage term: " . $ package_term ->get_error_message () );
478
+ } else {
479
+ WP_CLI ::warning ( "\tCannot create @package term: " . $ package_term ->get_error_message () );
480
+ }
455
481
}
456
482
}
483
+ $ added_term_relationship = did_action ( 'added_term_relationship ' );
484
+ wp_set_object_terms ( $ ID , $ package_term_ids , $ this ->taxonomy_package );
485
+ if ( did_action ( 'added_term_relationship ' ) > $ added_term_relationship ) {
486
+ $ anything_updated [] = true ;
487
+ }
457
488
458
489
// Set other taxonomy and post meta to use in the theme templates
490
+ $ added_item = did_action ( 'added_term_relationship ' );
459
491
wp_set_object_terms ( $ ID , $ this ->file_meta ['term_id ' ], $ this ->taxonomy_file );
492
+ if ( did_action ( 'added_term_relationship ' ) > $ added_item ) {
493
+ $ anything_updated [] = true ;
494
+ }
495
+
460
496
if ( $ post_data ['post_type ' ] !== $ this ->post_type_class ) {
461
- update_post_meta ( $ ID , '_wp-parser_args ' , $ data ['arguments ' ] );
497
+ $ anything_updated [] = update_post_meta ( $ ID , '_wp-parser_args ' , $ data ['arguments ' ] );
498
+ }
499
+ $ anything_updated [] = update_post_meta ( $ ID , '_wp-parser_line_num ' , (string ) $ data ['line ' ] );
500
+ $ anything_updated [] = update_post_meta ( $ ID , '_wp-parser_end_line_num ' , (string ) $ data ['end_line ' ] );
501
+ $ anything_updated [] = update_post_meta ( $ ID , '_wp-parser_tags ' , $ data ['doc ' ]['tags ' ] );
502
+
503
+ // If the post didn't need to be updated, but meta or tax changed, update it to bump last modified.
504
+ if ( ! $ is_new_post && ! $ post_needed_update && array_filter ( $ anything_updated ) ) {
505
+ wp_update_post ( wp_slash ( $ post_data ), true );
462
506
}
463
- update_post_meta ( $ ID , '_wp-parser_line_num ' , $ data ['line ' ] );
464
- update_post_meta ( $ ID , '_wp-parser_end_line_num ' , $ data ['end_line ' ] );
465
- update_post_meta ( $ ID , '_wp-parser_tags ' , $ data ['doc ' ]['tags ' ] );
466
507
467
508
// Everything worked! Woo hoo!
468
509
if ( $ is_new_post ) {
0 commit comments