Add provider dropdown and per-project Gitea base URL to metabox
This commit is contained in:
+78
-15
@@ -6,15 +6,15 @@ if ( ! defined( 'WPINC' ) ) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add GitHub Repository URL meta box.
|
||||
* Add Repository meta box (provider-aware).
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @return void
|
||||
*/
|
||||
function projects_portfolio_add_meta_boxes() {
|
||||
add_meta_box(
|
||||
'projects_portfolio_github_url',
|
||||
esc_html__( 'GitHub URL', 'projects-wp' ),
|
||||
'projects_portfolio_repo',
|
||||
esc_html__( 'Repository', 'projects-wp' ),
|
||||
'projects_portfolio_render_meta_box',
|
||||
'projects',
|
||||
'side'
|
||||
@@ -23,27 +23,64 @@ function projects_portfolio_add_meta_boxes() {
|
||||
add_action( 'add_meta_boxes', 'projects_portfolio_add_meta_boxes' );
|
||||
|
||||
/**
|
||||
* Renders the GitHub Repository URL meta box for the Projects WP plugin.
|
||||
* Render the Repository meta box.
|
||||
*
|
||||
* Outputs a label and input field for setting the GitHub repository URL,
|
||||
* as well as a nonce for security.
|
||||
*
|
||||
* @param WP_Post $post The post object currently being edited.
|
||||
* @param WP_Post $post
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @return void
|
||||
*/
|
||||
function projects_portfolio_render_meta_box( $post ) {
|
||||
wp_nonce_field( 'projects_portfolio_save_meta_box', 'projects_portfolio_meta_box_nonce' );
|
||||
$github_url = get_post_meta( $post->ID, '_projects_portfolio_github_url', true );
|
||||
echo '<label for="projects_portfolio_github_url">' . __( 'GitHub Repository URL:', 'projects-wp' ) . '</label>';
|
||||
echo '<input type="url" id="projects_portfolio_github_url" name="projects_portfolio_github_url" value="' . esc_attr( $github_url ) . '" style="width: 100%;" />';
|
||||
|
||||
$provider = get_post_meta( $post->ID, '_projects_portfolio_provider', true );
|
||||
if ( '' === $provider ) {
|
||||
$provider = 'github';
|
||||
}
|
||||
$repo_url = get_post_meta( $post->ID, '_projects_portfolio_repo_url', true );
|
||||
if ( '' === $repo_url ) {
|
||||
$repo_url = get_post_meta( $post->ID, '_projects_portfolio_github_url', true );
|
||||
}
|
||||
$gitea_base_url = get_post_meta( $post->ID, '_projects_portfolio_gitea_base_url', true );
|
||||
?>
|
||||
<p>
|
||||
<label for="projects_portfolio_provider"><?php esc_html_e( 'Provider', 'projects-wp' ); ?></label>
|
||||
<select id="projects_portfolio_provider" name="projects_portfolio_provider" style="width: 100%;">
|
||||
<option value="github" <?php selected( $provider, 'github' ); ?>><?php esc_html_e( 'GitHub', 'projects-wp' ); ?></option>
|
||||
<option value="gitea" <?php selected( $provider, 'gitea' ); ?>><?php esc_html_e( 'Gitea', 'projects-wp' ); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="projects_portfolio_repo_url"><?php esc_html_e( 'Repository URL', 'projects-wp' ); ?></label>
|
||||
<input type="url" id="projects_portfolio_repo_url" name="projects_portfolio_repo_url" value="<?php echo esc_attr( $repo_url ); ?>" style="width: 100%;" placeholder="https://github.com/owner/repo" />
|
||||
</p>
|
||||
<p class="projects-portfolio-gitea-only" <?php echo 'gitea' !== $provider ? 'style="display:none;"' : ''; ?>>
|
||||
<label for="projects_portfolio_gitea_base_url"><?php esc_html_e( 'Gitea Base URL (optional override)', 'projects-wp' ); ?></label>
|
||||
<input type="url" id="projects_portfolio_gitea_base_url" name="projects_portfolio_gitea_base_url" value="<?php echo esc_attr( $gitea_base_url ); ?>" style="width: 100%;" placeholder="https://codeberg.org" />
|
||||
<span class="description"><?php esc_html_e( 'Leave blank to use the default from Settings.', 'projects-wp' ); ?></span>
|
||||
</p>
|
||||
<script>
|
||||
(function(){
|
||||
var sel = document.getElementById('projects_portfolio_provider');
|
||||
if (!sel) return;
|
||||
var gitea = document.querySelector('.projects-portfolio-gitea-only');
|
||||
function toggle(){
|
||||
if (!gitea) return;
|
||||
gitea.style.display = sel.value === 'gitea' ? '' : 'none';
|
||||
}
|
||||
sel.addEventListener('change', toggle);
|
||||
toggle();
|
||||
})();
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the GitHub repository URL meta box data for the Projects WP plugin.
|
||||
* Save the Repository meta box.
|
||||
*
|
||||
* @param int $post_id The ID of the post currently being saved.
|
||||
* Performs a lazy migration from legacy `_projects_portfolio_github_url`.
|
||||
*
|
||||
* @param int $post_id
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @return void
|
||||
@@ -55,8 +92,34 @@ function projects_portfolio_save_meta_box( $post_id ) {
|
||||
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
|
||||
return;
|
||||
}
|
||||
if ( isset( $_POST['projects_portfolio_github_url'] ) ) {
|
||||
update_post_meta( $post_id, '_projects_portfolio_github_url', esc_url_raw( $_POST['projects_portfolio_github_url'] ) );
|
||||
|
||||
$existing_provider = get_post_meta( $post_id, '_projects_portfolio_provider', true );
|
||||
$existing_repo_url = get_post_meta( $post_id, '_projects_portfolio_repo_url', true );
|
||||
$legacy_github = get_post_meta( $post_id, '_projects_portfolio_github_url', true );
|
||||
|
||||
// Lazy migration: legacy URL present, no explicit choice.
|
||||
if ( '' === $existing_provider && '' === $existing_repo_url && '' !== $legacy_github ) {
|
||||
update_post_meta( $post_id, '_projects_portfolio_provider', 'github' );
|
||||
update_post_meta( $post_id, '_projects_portfolio_repo_url', $legacy_github );
|
||||
}
|
||||
|
||||
if ( isset( $_POST['projects_portfolio_provider'] ) ) {
|
||||
$provider = ( 'gitea' === $_POST['projects_portfolio_provider'] ) ? 'gitea' : 'github';
|
||||
update_post_meta( $post_id, '_projects_portfolio_provider', $provider );
|
||||
}
|
||||
|
||||
if ( isset( $_POST['projects_portfolio_repo_url'] ) ) {
|
||||
$repo_url = esc_url_raw( wp_unslash( $_POST['projects_portfolio_repo_url'] ) );
|
||||
update_post_meta( $post_id, '_projects_portfolio_repo_url', $repo_url );
|
||||
// Maintain the legacy key for backward-compat reads (only when this is a GitHub project).
|
||||
$provider_now = get_post_meta( $post_id, '_projects_portfolio_provider', true );
|
||||
if ( 'github' === $provider_now ) {
|
||||
update_post_meta( $post_id, '_projects_portfolio_github_url', $repo_url );
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $_POST['projects_portfolio_gitea_base_url'] ) ) {
|
||||
update_post_meta( $post_id, '_projects_portfolio_gitea_base_url', esc_url_raw( wp_unslash( $_POST['projects_portfolio_gitea_base_url'] ) ) );
|
||||
}
|
||||
}
|
||||
add_action( 'save_post', 'projects_portfolio_save_meta_box' );
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../admin/metabox.php';
|
||||
|
||||
/**
|
||||
* @covers \projects_portfolio_save_meta_box
|
||||
*/
|
||||
class Metabox_Test extends \PHPUnit\Framework\TestCase {
|
||||
|
||||
protected function setUp(): void {
|
||||
\Brain\Monkey\setUp();
|
||||
}
|
||||
|
||||
protected function tearDown(): void {
|
||||
$_POST = [];
|
||||
\Brain\Monkey\tearDown();
|
||||
}
|
||||
|
||||
/** @var array<string,string> */
|
||||
private array $saved = [];
|
||||
|
||||
protected function setUpSaveStubs(): void {
|
||||
$this->saved = [];
|
||||
\Brain\Monkey\Functions\stubs( [
|
||||
'update_post_meta' => function ( $post_id, $key, $value ) {
|
||||
$this->saved[ $key ] = $value;
|
||||
return true;
|
||||
},
|
||||
'wp_verify_nonce' => function () { return true; },
|
||||
'wp_nonce_field' => function () { /* noop */ },
|
||||
] );
|
||||
}
|
||||
|
||||
public function test_save_persists_provider_and_repo_url(): void {
|
||||
$this->setUpSaveStubs();
|
||||
$_POST['projects_portfolio_meta_box_nonce'] = 'nonce';
|
||||
$_POST['projects_portfolio_provider'] = 'gitea';
|
||||
$_POST['projects_portfolio_repo_url'] = 'https://codeberg.org/owner/repo';
|
||||
$_POST['projects_portfolio_gitea_base_url'] = 'https://codeberg.org';
|
||||
|
||||
projects_portfolio_save_meta_box( 42 );
|
||||
|
||||
$this->assertSame( 'gitea', $this->saved['_projects_portfolio_provider'] );
|
||||
$this->assertSame( 'https://codeberg.org/owner/repo', $this->saved['_projects_portfolio_repo_url'] );
|
||||
$this->assertSame( 'https://codeberg.org', $this->saved['_projects_portfolio_gitea_base_url'] );
|
||||
}
|
||||
|
||||
public function test_lazy_migration_from_legacy_url(): void {
|
||||
$existing = [
|
||||
'_projects_portfolio_provider' => '',
|
||||
'_projects_portfolio_repo_url' => '',
|
||||
'_projects_portfolio_github_url' => 'https://github.com/owner/repo',
|
||||
];
|
||||
\Brain\Monkey\Functions\stubs( [
|
||||
'get_post_meta' => function ( $post_id, $key, $single = false ) use ( &$existing ) {
|
||||
return $existing[ $key ] ?? '';
|
||||
},
|
||||
'update_post_meta' => function ( $post_id, $key, $value ) use ( &$existing ) {
|
||||
$existing[ $key ] = $value;
|
||||
return true;
|
||||
},
|
||||
'wp_verify_nonce' => function () { return true; },
|
||||
'wp_nonce_field' => function () { /* noop */ },
|
||||
] );
|
||||
|
||||
// No $_POST provider/repo_url. Lazy migration should fire.
|
||||
$_POST['projects_portfolio_meta_box_nonce'] = 'nonce';
|
||||
|
||||
projects_portfolio_save_meta_box( 1 );
|
||||
|
||||
$this->assertSame( 'github', $existing['_projects_portfolio_provider'] );
|
||||
$this->assertSame( 'https://github.com/owner/repo', $existing['_projects_portfolio_repo_url'] );
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,18 @@ if ( ! function_exists( 'wp_remote_retrieve_header' ) ) {
|
||||
if ( ! function_exists( 'error_log' ) ) {
|
||||
function error_log( $message ) { /* swallow during tests */ }
|
||||
}
|
||||
if ( ! function_exists( 'get_post_meta' ) ) {
|
||||
function get_post_meta( $post_id, $key = '', $single = false ) { return ''; }
|
||||
}
|
||||
if ( ! function_exists( 'update_post_meta' ) ) {
|
||||
function update_post_meta( $post_id, $key, $value ) { return true; }
|
||||
}
|
||||
if ( ! function_exists( 'wp_unslash' ) ) {
|
||||
function wp_unslash( $value ) { return $value; }
|
||||
}
|
||||
if ( ! function_exists( 'esc_url_raw' ) ) {
|
||||
function esc_url_raw( $url ) { return $url; }
|
||||
}
|
||||
|
||||
// Minimal WP_Error stub if Brain\Monkey doesn't supply one.
|
||||
if ( ! class_exists( 'WP_Error' ) ) {
|
||||
|
||||
Reference in New Issue
Block a user