From 71aca51d2caa06a59e876e1268236a6cbb330f71 Mon Sep 17 00:00:00 2001 From: Kees Meijer Date: Wed, 31 Oct 2018 16:33:16 +0100 Subject: [PATCH 1/2] Add 'introduced', modified and deprecated version post meta to imported posts --- lib/class-importer.php | 80 ++++++++++++++++++++++++++++++++++++++++-- lib/template.php | 36 +++++++++++++++++++ 2 files changed, 114 insertions(+), 2 deletions(-) diff --git a/lib/class-importer.php b/lib/class-importer.php index b83e597..0506a0a 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..c429b9e 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. + * + * This filter allows you to do your own version validation by returning a boolean. + * + * @param mixed $check_version. Whether to check the version or not. Default null. + * @param string $version Version string. + */ + $check_version = apply_filters( 'wp_parser_maybe_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; +} From be898cb52fd1ae75391ab47381449ceb0e0b83b2 Mon Sep 17 00:00:00 2001 From: Kees Meijer Date: Thu, 1 Nov 2018 15:29:08 +0100 Subject: [PATCH 2/2] Hide the post meta in the wp-admin --- lib/class-importer.php | 16 ++++++++-------- lib/template.php | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/class-importer.php b/lib/class-importer.php index 0506a0a..99e1776 100644 --- a/lib/class-importer.php +++ b/lib/class-importer.php @@ -858,26 +858,26 @@ protected function _set_namespaces( $post_id, $namespaces ) { 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']; + $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'] ); + $anything_updated[] = update_post_meta( $post_id, '_wp-parser_introduced', $introduced_version['content'] ); } else { - delete_post_meta( $post_id, 'wp_parser_introduced' ); + 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'] ); + $anything_updated[] = update_post_meta( $post_id, '_wp-parser_deprecated', $deprecated_version['content'] ); } else { - delete_post_meta( $post_id, 'wp_parser_deprecated' ); + delete_post_meta( $post_id, '_wp-parser_deprecated' ); } - $old_meta = get_post_meta( $post_id, 'wp_parser_modified' ); + $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' ); + delete_post_meta( $post_id, '_wp-parser_modified' ); if ( ! empty( $since_versions ) ) { foreach ( $since_versions as $since ) { @@ -887,7 +887,7 @@ protected function _set_since_meta( $post_id, $since_versions, $deprecated_versi } $new_meta[] = $since['content']; - add_post_meta( $post_id, 'wp_parser_modified', $since['content'] ); + add_post_meta( $post_id, '_wp-parser_modified', $since['content'] ); } } diff --git a/lib/template.php b/lib/template.php index c429b9e..dc79916 100644 --- a/lib/template.php +++ b/lib/template.php @@ -348,12 +348,12 @@ function maybe_version( $version ) { /** * Whether a version should be checked or not. * - * This filter allows you to do your own version validation by returning a boolean. + * 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_maybe_version', null, $version ); + $check_version = apply_filters( 'wp_parser_check_version', null, $version ); if ( is_bool( $check_version ) ) { return $check_version; }