Skip to content

Commit

Permalink
Squash merge try/release-page
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenDufresne committed Nov 18, 2024
1 parent 11b5eec commit 03f8c50
Show file tree
Hide file tree
Showing 94 changed files with 4,963 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ public function init() {

// Add duplicate search rule which will be hit before the following old-plugin tab rules
add_rewrite_rule( '^search/([^/]+)/?$', 'index.php?s=$matches[1]', 'top' );

// Add additional tags endpoint, to avoid being caught in old-plugins tab rules. See: https://meta.trac.wordpress.org/ticket/6819.
add_rewrite_rule( '^tags/([^/]+)/?$', 'index.php?plugin_tags=$matches[1]', 'top' );

Expand Down Expand Up @@ -1735,9 +1735,15 @@ public static function get_releases( $plugin ) {
$plugin = self::get_plugin_post( $plugin );
$releases = get_post_meta( $plugin->ID, 'releases', true );

// Meta doesn't exist yet? Lets fill it out.
// Data doesn't exist yet? Lets fill it out.
if ( false === $releases || ! is_array( $releases ) ) {
$releases = self::prefill_releses_meta( $plugin );
$releases = self::prefill_releases_meta( $plugin );

// FIXME: limit creation of data while we're testing. Remove this for production.
// For now we'll mirror the releases postmeta into the CPT. If/when we're confident the behaviour is identical, we can remove the postmeta part.
if ( in_array( 'wordpressdotorg', Tools::get_plugin_committers( $plugin->ID ) ) ) {
Plugin_Release::instance()->update_releases( $plugin, $releases );
}
}

/**
Expand All @@ -1756,15 +1762,12 @@ public static function get_releases( $plugin ) {
}

/**
* Prefill the releases meta for a plugin.
* Prefill the releases meta items for a plugin.
*
* @param \WP_Post $plugin Plugin post object.
* @return array
*/
public static function prefill_releses_meta( $plugin ) {
if ( ! $plugin->releases ) {
update_post_meta( $plugin->ID, 'releases', [] );
}
public static function prefill_releases_meta( $plugin ) {

$tags = get_post_meta( $plugin->ID, 'tags', true );
if ( $tags ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php
namespace WordPressdotorg\Plugin_Directory;

/**
* The Plugin Release class encapsulates the plugin release CPT and related code.
* Used for storing and interacting with plugin releases; ie versions of a plugin that are made available for download.
*
* @package WordPressdotorg\Plugin_Directory
*/
class Plugin_Release {
/**
* Fetch the instance of the Plugin_Release class.
*
* @static
*/
public static function instance() {
static $instance = null;

return ! is_null( $instance ) ? $instance : $instance = new Plugin_Release();
}

/**
* Plugin_Release constructor.
*
* @access private
*/
private function __construct() {
add_action( 'init', array( $this, 'init' ) );
}

/**
* Initialize the Plugin_Release class.
*/
public function init() {
register_post_type( 'plugin_release', array(
'labels' => array(
'name' => __( 'Releases', 'wporg-plugins' ),
'singular_name' => __( 'Release', 'wporg-plugins' ),
),
'public' => false,
'show_ui' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'show_in_rest' => true, // FIXME: maybe?
'supports' => array( 'title', 'editor' ), // TBD
'rewrite' => false,
'query_var' => false,
'hierarchical' => false, // Disappointingly, this doesn't help us make a Post -> Release hierarchy.
) );
}

// Starting point for an internal API, mostly copilot-generated.

/**
* Get all releases for a plugin.
*/
public function get_releases( $plugin ) {
$plugin_id = ( get_post( $plugin ) )->ID;

$releases = get_posts( array(
'post_type' => 'plugin_release',
'posts_per_page' => -1,
'post_parent' => $plugin_id,
'orderby' => 'date',
'order' => 'DESC',
) );

return $releases;
}

/**
* Add release info for a plugin.
*/
public function add_release( $plugin, $release ) {
$plugin_id = ( get_post( $plugin ) )->ID;

// Make sure we don't accidentally add junk from a sandbox while tinkering.
die( "Not yet ready for use" );

$release_date = date( 'Y-m-d H:i:s', strtotime( $tag['date'] ) );
$committer_user_id = get_user_by( 'login', $tag['author'] )->ID;
if ( ! $committer_user_id ) {
return new WP_Error( 'invalid_committer', 'Invalid committer' );
}

$release_id = wp_insert_post( array(
'post_type' => 'plugin_release',
'post_title' => $release['version'],
'post_parent' => $plugin_id,
'post_status' => 'publish',
'post_date' => $release_date, // And/or post_date_gmt?
// Mirrors the metadata.
'meta_input' => array(
'release_date' => $release['date'],
'release_tag' => $release['tag'],
'release_version' => $release['version'],
'release_committer' => $release['committer'],
'release_zips_built' => $release['zips_built'],
'release_confirmations_required' => $release['confirmations_required'],
),
// TODO: what else? Could store the changelog or other content at the point of release for comparison purposes.
) );

return $release_id;
}

/**
* Update existing release info.
*/
public function update_release( $release_id, $release ) {
// Make sure we don't accidentally add junk from a sandbox while tinkering.
die( "Not yet ready for use" );

$release_date = date( 'Y-m-d H:i:s', strtotime( $tag['date'] ) );
$committer_user_id = get_user_by( 'login', $tag['author'] )->ID;
if ( ! $committer_user_id ) {
return new WP_Error( 'invalid_committer', 'Invalid committer' );
}

$release_id = wp_update_post( array(
'ID' => $release_id,
'post_type' => 'plugin_release',
'post_title' => $release['version'],
'post_parent' => $plugin_id,
'post_status' => 'publish',
'post_date' => $release_date, // And/or post_date_gmt?
// Mirrors the metadata.
'meta_input' => array(
'release_date' => $release['date'],
'release_tag' => $release['tag'],
'release_version' => $release['version'],
'release_committer' => $release['committer'],
'release_zips_built' => $release['zips_built'],
'release_confirmations_required' => $release['confirmations_required'],
),
// TODO: what else? Could store the changelog or other content at the point of release for comparison purposes.
) );

return $release_id;
}

/**
* Update all release info for a plugin. This will insert or update each release, and remove any unknown releases.
*/
public function update_releases( $plugin, $releases ) {
$plugin_id = ( get_post( $plugin ) )->ID;

// Make sure we don't accidentally add junk from a sandbox while tinkering.
die( "Not yet ready for use" );

$changed = false;

// The current releases, if any, that need to be updated.
$current_releases = $this->get_releases( $plugin );
$current_versions = wp_list_pluck( $current_releases, 'post_title', 'ID' );

// Add or update each release.
foreach ( $releases as $release ) {
if ( ! isset( $current_versions[ $release['version'] ] ) ) {
$changed = $changed | (bool)$this->add_release( $plugin, $release );
} else {
$release_id = $current_versions[ $release['version'] ];
$changed = $changed | (bool)$this->update_release( $release_id, $release );
}
}

// Remove any releases that are no longer present.
foreach ( $current_releases as $release_id => $release ) {
if ( ! in_array( $release->post_title, wp_list_pluck( $releases, 'version' ) ) ) {
$changed = $changed | (bool)wp_delete_post( $release_id, true ); // Force delete.
}
}

return $changed;
}

/**
* Get a specific plugin release.
*/
public function get_release( $plugin, $version ) {
$plugin_id = ( get_post( $plugin ) )->ID;

$release = get_posts( array(
'post_type' => 'plugin_release',
'posts_per_page' => 1,
'post_parent' => $plugin_id,
'post_title' => $version,
) );

return $release ? $release[0] : null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ public static function get_plugin_section_titles() {
'developers' => _x( 'Contributors &amp; Developers', 'plugin tab title', 'wporg-plugins' ),
'other_notes' => _x( 'Other Notes', 'plugin tab title', 'wporg-plugins' ),
'blocks' => _x( 'Blocks', 'plugin tab title', 'wporg-plugins' ),
'releases' => _x( 'Releases', 'plugin tab title', 'wporg-plugins' )
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-server-side-render'), 'version' => '8627e1da9b24373a1f90');
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-server-side-render'), 'version' => '7f26875761f01d13f9dd');
Loading

0 comments on commit 03f8c50

Please sign in to comment.