Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add introduced, modified and deprecated version post meta to imported posts #209

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 78 additions & 2 deletions lib/class-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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' ) ),
Expand Down Expand Up @@ -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 );
}
}
36 changes: 36 additions & 0 deletions lib/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}