diff --git a/lib/class-importer.php b/lib/class-importer.php index b83e597..99e1776 100644 --- a/lib/class-importer.php +++ b/lib/class-importer.php @@ -646,10 +646,10 @@ public function import_item( array $data, $parent_post_id = 0, $import_ignored = // Loop through all @since versions. foreach ( $since_versions as $since_version ) { - if ( ! empty( $since_version['content'] ) ) { + if ( ! empty( $since_version['content'] ) && maybe_version( $since_version['content'] ) ) { $since_term = $this->insert_term( $since_version['content'], $this->taxonomy_since_version ); - // Assign the tax item to the post + // Assign the tax item to the post. if ( ! is_wp_error( $since_term ) ) { $added_term_relationship = did_action( 'added_term_relationship' ); wp_set_object_terms( $post_id, (int) $since_term['term_id'], $this->taxonomy_since_version, true ); @@ -663,6 +663,28 @@ public function import_item( array $data, $parent_post_id = 0, $import_ignored = } } + $deprecated_version = wp_list_filter( $data['doc']['tags'], array( 'name' => 'deprecated' ) ); + $deprecated_version = array_shift( $deprecated_version ); + + $is_deprecated = isset( $deprecated_version['content'] ) && ! empty( $deprecated_version['content'] ); + if ( $is_deprecated && maybe_version( $deprecated_version['content'] ) ) { + $deprecated_term = $this->insert_term( $deprecated_version['content'], $this->taxonomy_since_version ); + + // Assign the deprecated tax item to the post. + if ( ! is_wp_error( $since_term ) ) { + $added_term_relationship = did_action( 'added_term_relationship' ); + wp_set_object_terms( $post_id, (int) $deprecated_term['term_id'], $this->taxonomy_since_version, true ); + if ( did_action( 'added_term_relationship' ) > $added_term_relationship ) { + $anything_updated[] = true; + } + } else { + $this->logger->warning( "\tCannot set deprecated @since term: " . $since_term->get_error_message() ); + } + } + + // Assign 'introduced', 'modified' and 'deprecated' meta to the post. + $anything_updated[] = $this->_set_since_meta( $post_id, $since_versions, $deprecated_version ); + $packages = array( 'main' => wp_list_filter( $data['doc']['tags'], array( 'name' => 'package' ) ), 'sub' => wp_list_filter( $data['doc']['tags'], array( 'name' => 'subpackage' ) ), @@ -822,4 +844,58 @@ protected function _set_namespaces( $post_id, $namespaces ) { } } } + + /** + * Set version meta. + * + * Imports introduced, modified, and deprecated post meta. + * + * @param int $post_id Post ID. + * @param array $since_versions Array with DocBlock data from versions. + * @param array $deprecated_version DocBlock deprecated version data. + * @return bool True if any meta whas updated + */ + protected function _set_since_meta( $post_id, $since_versions, $deprecated_version ) { + $anything_updated = array(); + $introduced_version = array_shift( $since_versions ); + + $introduced = isset( $introduced_version['content'] ) && $introduced_version['content']; + if ( $introduced && maybe_version( $introduced_version['content'] ) ) { + $anything_updated[] = update_post_meta( $post_id, '_wp-parser_introduced', $introduced_version['content'] ); + } else { + delete_post_meta( $post_id, '_wp-parser_introduced' ); + } + + $deprecated = isset( $deprecated_version['content'] ) && $deprecated_version['content']; + if ( $deprecated && maybe_version( $deprecated_version['content'] ) ) { + $anything_updated[] = update_post_meta( $post_id, '_wp-parser_deprecated', $deprecated_version['content'] ); + } else { + delete_post_meta( $post_id, '_wp-parser_deprecated' ); + } + + $old_meta = get_post_meta( $post_id, '_wp-parser_modified' ); + $new_meta = array(); + + // Delete modified meta and check if anything was updated after adding new meta. + delete_post_meta( $post_id, '_wp-parser_modified' ); + + if ( ! empty( $since_versions ) ) { + foreach ( $since_versions as $since ) { + $modified = $since['content'] && $since['content']; + if ( ! ( $modified && maybe_version( $since['content'] ) ) ) { + continue; + } + + $new_meta[] = $since['content']; + add_post_meta( $post_id, '_wp-parser_modified', $since['content'] ); + } + } + + $diff_meta = array_diff( $old_meta, $new_meta ); + $anything_updated[] = ! empty( $diff_meta ); + $anything_updated[] = empty( $old_meta ) && ! empty( $new_meta ); + $anything_updated = array_filter( $anything_updated ); + + return ! empty( $anything_updated ); + } } diff --git a/lib/template.php b/lib/template.php index e2a5c0b..dc79916 100644 --- a/lib/template.php +++ b/lib/template.php @@ -328,3 +328,39 @@ function get_source_link() { return $trac_url; } + +/** + * Check whether a string resembles a version. + * + * A version should start with a numeric value. + * The only non numeric versions that are allowed start with "mu" and "unknown". + * + * Note: the version validation is very minimal and doesn't check semantic versioning or + * any other type of version specifications. + * + * @param string $version The version to check. + * @return bool True if it resembles a version. + */ +function maybe_version( $version ) { + + $version = strtolower( trim( (string) $version ) ); + + /** + * Whether a version should be checked or not. + * + * Return boolean to validate the version yourself. + * + * @param mixed $check_version. Whether to check the version or not. Default null. + * @param string $version Version string. + */ + $check_version = apply_filters( 'wp_parser_check_version', null, $version ); + if ( is_bool( $check_version ) ) { + return $check_version; + } + + $mu = ( 0 === strpos( $version, 'mu' ) ); + $unknown = ( 0 === strpos( $version, 'unknown' ) ); + $numeric = preg_match( '/^\d/', $version ); + + return $mu || $unknown || $numeric; +}