Skip to content

Commit 30887eb

Browse files
committed
feature(renamer): rewrite composer.json author fields
Rebrand composer.json author metadata with user-supplied values. - Replace "Saltus" author name and "saltus.dev" homepage URLs - Suppress unlink warnings in delete_file helpers - Add Saltus contributor checkbox on the rebrand form When the contributor checkbox is checked, inject Saltus as a second author in the generated plugin.
1 parent e97c6d9 commit 30887eb

3 files changed

Lines changed: 63 additions & 22 deletions

File tree

src/Plugin/Admin/RenamerPage.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public function render(): void {
181181
<?php $this->render_text_field( 'plugin_uri', __( 'Plugin URI', 'framework-demo' ), $values['plugin_uri'], false, 'url' ); ?>
182182
<?php $this->render_text_field( 'version', __( 'Version', 'framework-demo' ), $values['version'], true ); ?>
183183
<?php $this->render_text_field( 'prefix', __( 'Code Prefix', 'framework-demo' ), $values['prefix'], true ); ?>
184+
<?php $this->render_checkbox_field( 'saltus_contributor', __( 'Credit Saltus as contributor', 'framework-demo' ), __( 'Include Saltus as a co-author in the generated plugin\'s composer.json', 'framework-demo' ) ); ?>
184185
</table>
185186

186187
<p class="submit">
@@ -329,6 +330,29 @@ class="regular-text"
329330
<?php
330331
}
331332

333+
private function render_checkbox_field( string $id, string $label, string $description ): void {
334+
?>
335+
<tr>
336+
<th scope="row">
337+
<label for="<?php echo esc_attr( $id ); ?>"><?php echo esc_html( $label ); ?></label>
338+
</th>
339+
<td>
340+
<fieldset>
341+
<label for="<?php echo esc_attr( $id ); ?>">
342+
<input
343+
name="<?php echo esc_attr( $id ); ?>"
344+
id="<?php echo esc_attr( $id ); ?>"
345+
type="checkbox"
346+
value="1"
347+
>
348+
<?php echo esc_html( $description ); ?>
349+
</label>
350+
</fieldset>
351+
</td>
352+
</tr>
353+
<?php
354+
}
355+
332356
private function render_guide(): void {
333357
?>
334358
<aside class="framework-demo-renamer__guide">
@@ -405,7 +429,7 @@ private function redirect_with_error( string $message, array $form_data = array(
405429

406430
private function delete_file( string $path ): void {
407431
if ( is_file( $path ) ) {
408-
unlink( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions
432+
@unlink( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions,WordPress.PHP.NoSilencedErrors.Discouraged
409433
}
410434
}
411435

src/Plugin/Renamer/PluginIdentity.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@ class PluginIdentity {
1717
public string $plugin_uri;
1818
public string $version;
1919
public string $prefix;
20+
public bool $saltus_contributor = false;
2021

2122
/**
2223
* @param array<string,string> $data Renamer field values.
2324
*/
2425
public function __construct( array $data ) {
25-
$this->plugin_name = $data['plugin_name'];
26-
$this->plugin_slug = $data['plugin_slug'];
27-
$this->main_file = $data['main_file'];
28-
$this->namespace_segment = $data['namespace_segment'];
29-
$this->text_domain = $this->plugin_slug;
30-
$this->description = $data['description'];
31-
$this->author = $data['author'];
32-
$this->author_uri = $data['author_uri'];
33-
$this->plugin_uri = $data['plugin_uri'];
34-
$this->version = $data['version'];
35-
$this->prefix = $data['prefix'];
26+
$this->plugin_name = $data['plugin_name'];
27+
$this->plugin_slug = $data['plugin_slug'];
28+
$this->main_file = $data['main_file'];
29+
$this->namespace_segment = $data['namespace_segment'];
30+
$this->text_domain = $this->plugin_slug;
31+
$this->description = $data['description'];
32+
$this->author = $data['author'];
33+
$this->author_uri = $data['author_uri'];
34+
$this->plugin_uri = $data['plugin_uri'];
35+
$this->version = $data['version'];
36+
$this->prefix = $data['prefix'];
37+
$this->saltus_contributor = ! empty( $data['saltus_contributor'] );
3638
}
3739

3840
/**
@@ -64,8 +66,9 @@ public static function from_request( array $request ): self {
6466
$data[ $key ] = sanitize_text_field( $value );
6567
}
6668

67-
$data['author_uri'] = trim( $data['author_uri'] );
68-
$data['plugin_uri'] = trim( $data['plugin_uri'] );
69+
$data['saltus_contributor'] = ! empty( $request['saltus_contributor'] ) && (string) $request['saltus_contributor'] === '1';
70+
$data['author_uri'] = trim( $data['author_uri'] );
71+
$data['plugin_uri'] = trim( $data['plugin_uri'] );
6972

7073
$dangerous_tokens = array( '*/', '?>', '<?php', '<?=', '<?' );
7174
$fields = [ 'plugin_name', 'description', 'author', 'author_uri', 'plugin_uri' ];

src/Plugin/Renamer/PluginRenamer.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ private function add_files( \ZipArchive $zip, PluginIdentity $identity ): void {
119119
throw new \RuntimeException( 'Could not read ' . $relative_path ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
120120
}
121121
$contents = $this->rewrite_contents( $contents, $identity );
122-
$added = $zip->addFromString( $target_path, $contents );
122+
123+
if ( $identity->saltus_contributor && basename( $relative_path ) === 'composer.json' ) {
124+
$contents = $this->add_saltus_contributor( $contents );
125+
}
126+
127+
$added = $zip->addFromString( $target_path, $contents );
123128
} else {
124129
$added = $zip->addFile( $file->getPathname(), $target_path );
125130
}
@@ -164,12 +169,15 @@ private function is_text_file( string $relative_path ): bool {
164169

165170
private function rewrite_contents( string $contents, PluginIdentity $identity ): string {
166171
$replacements = array(
167-
self::ORIGINAL_NAMESPACE_SEGMENT => $identity->namespace_segment,
168-
self::ORIGINAL_PACKAGE => $this->package_name( $identity ),
169-
self::ORIGINAL_MAIN_FILE => $identity->main_file,
170-
self::ORIGINAL_PREFIX_UPPER => strtoupper( $identity->prefix ),
171-
self::ORIGINAL_PREFIX => $identity->prefix,
172-
self::ORIGINAL_SLUG => $identity->plugin_slug,
172+
self::ORIGINAL_NAMESPACE_SEGMENT => $identity->namespace_segment,
173+
self::ORIGINAL_PACKAGE => $this->package_name( $identity ),
174+
self::ORIGINAL_MAIN_FILE => $identity->main_file,
175+
self::ORIGINAL_PREFIX_UPPER => strtoupper( $identity->prefix ),
176+
self::ORIGINAL_PREFIX => $identity->prefix,
177+
self::ORIGINAL_SLUG => $identity->plugin_slug,
178+
'"name": "Saltus"' => '"name": "' . addcslashes( $identity->author, '"' ) . '"',
179+
'"homepage": "https://saltus.dev/"' => '"homepage": "' . esc_url_raw( $identity->author_uri ) . '"',
180+
'"homepage": "https://saltus.dev"' => '"homepage": "' . esc_url_raw( $identity->author_uri ) . '"',
173181
);
174182

175183
$contents = strtr( $contents, $replacements );
@@ -238,9 +246,15 @@ private function package_name( PluginIdentity $identity ): string {
238246
return $vendor . '/' . $identity->plugin_slug;
239247
}
240248

249+
private function add_saltus_contributor( string $contents ): string {
250+
$entry = ",\n\t\t{\n\t\t\t\"name\": \"Saltus\",\n\t\t\t\"email\": \"web@saltus.dev\",\n\t\t\t\"homepage\": \"https://saltus.dev\"\n\t\t}";
251+
252+
return str_replace( "\n\t]", $entry . "\n\t]", $contents );
253+
}
254+
241255
private function delete_file( string $path ): void {
242256
if ( is_file( $path ) ) {
243-
unlink( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions
257+
@unlink( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions,WordPress.PHP.NoSilencedErrors.Discouraged
244258
}
245259
}
246260
}

0 commit comments

Comments
 (0)