Skip to content

Commit 0d01829

Browse files
committed
Partial mirroring of releases into CPT; not yet tested
I've intentionally disabled the write code until I've been able to make sure it won't fill the prod tables with junk.
1 parent 2c18af2 commit 0d01829

File tree

2 files changed

+96
-10
lines changed

2 files changed

+96
-10
lines changed

wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ public function init() {
557557

558558
// Add duplicate search rule which will be hit before the following old-plugin tab rules
559559
add_rewrite_rule( '^search/([^/]+)/?$', 'index.php?s=$matches[1]', 'top' );
560-
560+
561561
// Add additional tags endpoint, to avoid being caught in old-plugins tab rules. See: https://meta.trac.wordpress.org/ticket/6819.
562562
add_rewrite_rule( '^tags/([^/]+)/?$', 'index.php?plugin_tags=$matches[1]', 'top' );
563563

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

1738-
// Meta doesn't exist yet? Lets fill it out.
1738+
// Data doesn't exist yet? Lets fill it out.
17391739
if ( false === $releases || ! is_array( $releases ) ) {
1740-
$releases = self::prefill_releses_meta( $plugin );
1740+
$releases = self::prefill_releases_meta( $plugin );
1741+
1742+
// FIXME: limit creation of data while we're testing. Remove this for production.
1743+
// 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.
1744+
if ( in_array( 'wordpressdotorg', Tools::get_plugin_committers( $plugin->ID ) ) ) {
1745+
Plugin_Release::instance()->update_releases( $plugin, $releases );
1746+
}
17411747
}
17421748

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

17581764
/**
1759-
* Prefill the releases meta for a plugin.
1765+
* Prefill the releases meta items for a plugin.
17601766
*
17611767
* @param \WP_Post $plugin Plugin post object.
17621768
* @return array
17631769
*/
1764-
public static function prefill_releses_meta( $plugin ) {
1765-
if ( ! $plugin->releases ) {
1766-
update_post_meta( $plugin->ID, 'releases', [] );
1767-
}
1770+
public static function prefill_releases_meta( $plugin ) {
17681771

17691772
$tags = get_post_meta( $plugin->ID, 'tags', true );
17701773
if ( $tags ) {

wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-release.php

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,103 @@ public function add_release( $plugin, $release ) {
7777
// Make sure we don't accidentally add junk from a sandbox while tinkering.
7878
die( "Not yet ready for use" );
7979

80+
$release_date = date( 'Y-m-d H:i:s', strtotime( $tag['date'] ) );
81+
$committer_user_id = get_user_by( 'login', $tag['author'] )->ID;
82+
if ( ! $committer_user_id ) {
83+
return new WP_Error( 'invalid_committer', 'Invalid committer' );
84+
}
85+
8086
$release_id = wp_insert_post( array(
8187
'post_type' => 'plugin_release',
8288
'post_title' => $release['version'],
8389
'post_parent' => $plugin_id,
8490
'post_status' => 'publish',
91+
'post_date' => $release_date, // And/or post_date_gmt?
92+
// Mirrors the metadata.
93+
'meta_input' => array(
94+
'release_date' => $release['date'],
95+
'release_tag' => $release['tag'],
96+
'release_version' => $release['version'],
97+
'release_committer' => $release['committer'],
98+
'release_zips_built' => $release['zips_built'],
99+
'release_confirmations_required' => $release['confirmations_required'],
100+
),
101+
// TODO: what else? Could store the changelog or other content at the point of release for comparison purposes.
85102
) );
86103

87-
if ( $release_id ) {
88-
update_post_meta( $release_id, 'release_svn_revision', $release['revision'] );
104+
return $release_id;
105+
}
106+
107+
/**
108+
* Update existing release info.
109+
*/
110+
public function update_release( $release_id, $release ) {
111+
// Make sure we don't accidentally add junk from a sandbox while tinkering.
112+
die( "Not yet ready for use" );
113+
114+
$release_date = date( 'Y-m-d H:i:s', strtotime( $tag['date'] ) );
115+
$committer_user_id = get_user_by( 'login', $tag['author'] )->ID;
116+
if ( ! $committer_user_id ) {
117+
return new WP_Error( 'invalid_committer', 'Invalid committer' );
89118
}
90119

120+
$release_id = wp_update_post( array(
121+
'ID' => $release_id,
122+
'post_type' => 'plugin_release',
123+
'post_title' => $release['version'],
124+
'post_parent' => $plugin_id,
125+
'post_status' => 'publish',
126+
'post_date' => $release_date, // And/or post_date_gmt?
127+
// Mirrors the metadata.
128+
'meta_input' => array(
129+
'release_date' => $release['date'],
130+
'release_tag' => $release['tag'],
131+
'release_version' => $release['version'],
132+
'release_committer' => $release['committer'],
133+
'release_zips_built' => $release['zips_built'],
134+
'release_confirmations_required' => $release['confirmations_required'],
135+
),
136+
// TODO: what else? Could store the changelog or other content at the point of release for comparison purposes.
137+
) );
138+
91139
return $release_id;
92140
}
93141

142+
/**
143+
* Update all release info for a plugin. This will insert or update each release, and remove any unknown releases.
144+
*/
145+
public function update_releases( $plugin, $releases ) {
146+
$plugin_id = ( get_post( $plugin ) )->ID;
147+
148+
// Make sure we don't accidentally add junk from a sandbox while tinkering.
149+
die( "Not yet ready for use" );
150+
151+
$changed = false;
152+
153+
// The current releases, if any, that need to be updated.
154+
$current_releases = $this->get_releases( $plugin );
155+
$current_versions = wp_list_pluck( $current_releases, 'post_title', 'ID' );
156+
157+
// Add or update each release.
158+
foreach ( $releases as $release ) {
159+
if ( ! isset( $current_versions[ $release['version'] ] ) ) {
160+
$changed = $changed | (bool)$this->add_release( $plugin, $release );
161+
} else {
162+
$release_id = $current_versions[ $release['version'] ];
163+
$changed = $changed | (bool)$this->update_release( $release_id, $release );
164+
}
165+
}
166+
167+
// Remove any releases that are no longer present.
168+
foreach ( $current_releases as $release_id => $release ) {
169+
if ( ! in_array( $release->post_title, wp_list_pluck( $releases, 'version' ) ) ) {
170+
$changed = $changed | (bool)wp_delete_post( $release_id, true ); // Force delete.
171+
}
172+
}
173+
174+
return $changed;
175+
}
176+
94177
/**
95178
* Get a specific plugin release.
96179
*/

0 commit comments

Comments
 (0)