chore: initial commit — Buddy v0.1.0 (Phase A complete)

Buddy is born. First commit of a new standalone WordPress plugin —
the spiritual successor to the tamagotchi that once lived inside
A-WP-Notes v1.1.5 (gracefully retired). Rebuilt from scratch with
all the v3-discipline lessons baked in from day one.

PHASE A — pet exists
- Dashboard widget at WP Admin → Dashboard showing SVG character +
  name + mood label + four stats bars.
- Dedicated admin page at WP Admin → Buddy → My Buddy (bigger view).
- About page with side-by-side intro + plain-prose cards (Logbook
  About-page pattern carried forward).
- Settings page with name-rename form + Updates panel.
- Per-user state in user_meta key buddy_state (each WP admin gets
  their own pet, no shared state).
- Inline SVG sprite renderer with three mood tones (happy/neutral/
  sad) and three sizes (sm/md/lg). CSS keyframe animations: bobbing
  + periodic blinking. Zero image files.
- Self-hosted update checker wired up from commit 1, ported from
  Logbook v3.3.5: /releases/latest with /tags?limit=1 fallback,
  12h success cache / 1h negative cache. UI on Settings page.
- dashicons-pets admin-menu icon — literal paw-print, brand match.

ARCHITECTURE LOCKED FROM COMMIT 1
- Single-word brand name "Buddy" — no WP prefix, no future rebrand.
- Public GPL v2+ Gitea repo (ranger/a-buddy).
- Constants prefix BUDDY_*, function prefix buddy_*, text domain
  buddy. Clean naming throughout — none of Logbook's wp-notes-*
  historical-artifact baggage.
- Single H1 per admin page, no nested toggle boxes, no duplicate
  sections — Tier-1 discipline carried forward from Logbook.
- All assets local (inline SVG, plain CSS), no third-party CDN,
  no Gravatar-style external pings.

NOT IN THIS RELEASE (planned)
- Phase B — Feed/Play/Clean/Sleep interactions + cooldown timers.
- Phase C — WP-cron decay + "Buddy is hungry" dismissible notices
  (port the persistent-dismissal pattern from Logbook).
- Phase D — Multiple species (dog, dragon, sprite), per-species
  personality phrases.
- Phase E — Site-health hook: pet stats react to wp_get_site_health()
  results. The killer feature.
- Phase F — Pro tier (€2.99 lifetime) with custom skins + multi-pet.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-25 10:23:57 +01:00
commit 48e97862a6
12 changed files with 1135 additions and 0 deletions
+239
View File
@@ -0,0 +1,239 @@
<?php
/**
* Buddy — self-hosted update checker against the Gitea repo.
*
* Direct port of the Logbook v3.3.5 updater (proven in production).
* Polls Gitea's /releases/latest, falls back to /tags?limit=1 when no
* formal Release object exists, compares against BUDDY_VERSION,
* renders an Updates panel on the Settings page. Cached 12h on
* success, 1h on negative responses.
*
* Repo coordinates are constants you can override via define() in
* wp-config.php if the repo ever moves.
*/
if ( ! defined( 'ABSPATH' ) ) { exit; }
if ( ! defined( 'BUDDY_GITEA_HOST' ) ) { define( 'BUDDY_GITEA_HOST', 'https://git.davidtkeane.com' ); }
if ( ! defined( 'BUDDY_GITEA_OWNER' ) ) { define( 'BUDDY_GITEA_OWNER', 'ranger' ); }
if ( ! defined( 'BUDDY_GITEA_REPO' ) ) { define( 'BUDDY_GITEA_REPO', 'a-buddy' ); }
function buddy_gitea_repo_url() {
return BUDDY_GITEA_HOST . '/' . BUDDY_GITEA_OWNER . '/' . BUDDY_GITEA_REPO;
}
function buddy_gitea_releases_url() {
return buddy_gitea_repo_url() . '/releases';
}
/**
* Fetch the latest release/tag, normalised. Returns null on hard
* error, or an array including `version`. See Logbook updater for the
* full shape.
*/
function buddy_fetch_latest_release( $force_refresh = false ) {
$cache_key = 'buddy_gitea_latest';
if ( ! $force_refresh ) {
$cached = get_site_transient( $cache_key );
if ( is_array( $cached ) ) { return $cached; }
}
$base_api = BUDDY_GITEA_HOST . '/api/v1/repos/' . BUDDY_GITEA_OWNER . '/' . BUDDY_GITEA_REPO;
// Try formal Release first.
$response = wp_remote_get( $base_api . '/releases/latest', array( 'timeout' => 8 ) );
if ( is_wp_error( $response ) ) { return null; }
$code = (int) wp_remote_retrieve_response_code( $response );
$body = ( $code === 200 ) ? json_decode( wp_remote_retrieve_body( $response ), true ) : null;
// Fallback to /tags if no Release object exists yet.
if ( $code !== 200 || ! is_array( $body ) || empty( $body['tag_name'] ) ) {
$tags_response = wp_remote_get( $base_api . '/tags?limit=1', array( 'timeout' => 8 ) );
if ( ! is_wp_error( $tags_response )
&& (int) wp_remote_retrieve_response_code( $tags_response ) === 200 ) {
$tags = json_decode( wp_remote_retrieve_body( $tags_response ), true );
if ( is_array( $tags ) && ! empty( $tags[0]['name'] ) ) {
$body = array(
'tag_name' => $tags[0]['name'],
'html_url' => buddy_gitea_repo_url() . '/src/tag/' . rawurlencode( $tags[0]['name'] ),
'body' => isset( $tags[0]['message'] ) ? $tags[0]['message'] : '',
'published_at' => isset( $tags[0]['commit']['created'] ) ? $tags[0]['commit']['created'] : null,
'assets' => array(),
);
$code = 200;
}
}
}
if ( $code !== 200 || ! is_array( $body ) || empty( $body['tag_name'] ) ) {
$info = array(
'version' => null,
'html_url' => buddy_gitea_releases_url(),
'download_url' => null,
'body' => '',
'published_at' => null,
'error_code' => $code,
);
set_site_transient( $cache_key, $info, HOUR_IN_SECONDS );
return $info;
}
$version = ltrim( (string) $body['tag_name'], 'vV' );
// Prefer a .zip asset; fall back to Gitea source-archive URL.
$download_url = null;
if ( ! empty( $body['assets'] ) && is_array( $body['assets'] ) ) {
foreach ( $body['assets'] as $asset ) {
if ( isset( $asset['name'], $asset['browser_download_url'] )
&& substr( strtolower( $asset['name'] ), -4 ) === '.zip' ) {
$download_url = $asset['browser_download_url'];
break;
}
}
}
if ( ! $download_url ) {
$download_url = buddy_gitea_repo_url() . '/archive/' . rawurlencode( $body['tag_name'] ) . '.zip';
}
$info = array(
'version' => $version,
'html_url' => isset( $body['html_url'] ) ? esc_url_raw( $body['html_url'] ) : '',
'download_url' => esc_url_raw( $download_url ),
'body' => isset( $body['body'] ) ? wp_strip_all_tags( $body['body'] ) : '',
'published_at' => isset( $body['published_at'] ) ? $body['published_at'] : null,
);
set_site_transient( $cache_key, $info, 12 * HOUR_IN_SECONDS );
return $info;
}
function buddy_update_status( $force_refresh = false ) {
$current = defined( 'BUDDY_VERSION' ) ? BUDDY_VERSION : '0.0.0';
$latest = buddy_fetch_latest_release( $force_refresh );
if ( ! $latest || empty( $latest['version'] ) ) {
$msg = __( 'No releases tagged on the Gitea repo yet.', 'buddy' );
if ( $latest && ! empty( $latest['error_code'] ) && (int) $latest['error_code'] !== 404 ) {
$msg = sprintf( __( 'Could not reach Gitea (HTTP %d). Try again in a few minutes.', 'buddy' ), (int) $latest['error_code'] );
}
return array(
'status' => 'unknown',
'current' => $current,
'message' => $msg,
'repo_url' => buddy_gitea_repo_url(),
);
}
if ( version_compare( $latest['version'], $current, '>' ) ) {
return array(
'status' => 'available',
'current' => $current,
'latest' => $latest['version'],
'html_url' => $latest['html_url'],
'download_url' => $latest['download_url'],
'published_at' => $latest['published_at'],
'body' => $latest['body'],
'message' => sprintf( __( 'A new version (v%1$s) is available — you are on v%2$s.', 'buddy' ), $latest['version'], $current ),
);
}
return array(
'status' => 'up-to-date',
'current' => $current,
'latest' => $latest['version'],
'message' => sprintf( __( 'You are up to date (v%s).', 'buddy' ), $current ),
'repo_url' => buddy_gitea_repo_url(),
);
}
add_action( 'wp_ajax_buddy_check_updates', 'buddy_ajax_check_updates' );
function buddy_ajax_check_updates() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( 'Insufficient permissions.', 403 );
}
check_ajax_referer( 'buddy_check_updates', 'nonce' );
delete_site_transient( 'buddy_gitea_latest' );
wp_send_json_success( buddy_update_status( true ) );
}
function buddy_render_updates_panel() {
$status = buddy_update_status( false );
$nonce = wp_create_nonce( 'buddy_check_updates' );
$repo_url = buddy_gitea_repo_url();
$rel_url = buddy_gitea_releases_url();
?>
<div class="buddy-updates" style="max-width:720px; margin-top:24px; padding:18px 20px; background:#fff; border:1px solid #ccd0d4; border-radius:4px;">
<h2 style="margin-top:0;"><?php esc_html_e( 'Updates', 'buddy' ); ?></h2>
<p style="margin:0 0 12px;">
<?php esc_html_e( 'Buddy is self-hosted on Gitea. Click Check now to ask the repo whether there is a newer release than the one you are running.', 'buddy' ); ?>
</p>
<p id="buddy-update-status" style="margin:0 0 12px;">
<strong><?php esc_html_e( 'Status:', 'buddy' ); ?></strong>
<span id="buddy-update-status-text"><?php echo esc_html( $status['message'] ); ?></span>
<?php if ( $status['status'] === 'available' && ! empty( $status['download_url'] ) ) : ?>
<br>
<a href="<?php echo esc_url( $status['download_url'] ); ?>" class="button button-primary" style="margin-top:8px;">
<?php
/* translators: %s is the latest version number, e.g. "0.2.0" */
echo esc_html( sprintf( __( 'Download v%s (.zip)', 'buddy' ), $status['latest'] ) );
?>
</a>
<?php if ( ! empty( $status['html_url'] ) ) : ?>
<a href="<?php echo esc_url( $status['html_url'] ); ?>" target="_blank" rel="noopener" style="margin-left:8px;"><?php esc_html_e( 'View release notes →', 'buddy' ); ?></a>
<?php endif; ?>
<?php endif; ?>
</p>
<p style="margin:0 0 4px;">
<button type="button" id="buddy-check-updates-btn" class="button" data-nonce="<?php echo esc_attr( $nonce ); ?>">
↻ <?php esc_html_e( 'Check now', 'buddy' ); ?>
</button>
<a href="<?php echo esc_url( $repo_url ); ?>" target="_blank" rel="noopener" class="button" style="margin-left:6px;"><?php esc_html_e( 'View on Gitea', 'buddy' ); ?></a>
<a href="<?php echo esc_url( $rel_url ); ?>" target="_blank" rel="noopener" class="button" style="margin-left:6px;"><?php esc_html_e( 'View all releases', 'buddy' ); ?></a>
</p>
<p style="margin:10px 0 0; color:#646970; font-size:12px;">
<?php esc_html_e( 'Manual update path: download the .zip, deactivate the plugin in WordPress, upload via Plugins → Add New → Upload, reactivate. Your Buddy survives the upgrade (state is stored in user_meta).', 'buddy' ); ?>
</p>
</div>
<script>
(function () {
var btn = document.getElementById('buddy-check-updates-btn');
var statusText = document.getElementById('buddy-update-status-text');
if (!btn || !statusText) { return; }
btn.addEventListener('click', function () {
var nonce = btn.getAttribute('data-nonce');
btn.disabled = true;
var orig = btn.textContent;
btn.textContent = '↻ Checking…';
statusText.textContent = 'Asking Gitea…';
var fd = new FormData();
fd.append('action', 'buddy_check_updates');
fd.append('nonce', nonce);
fetch(ajaxurl, { method: 'POST', credentials: 'same-origin', body: fd })
.then(function (r) { return r.json(); })
.then(function (res) {
if (!res || !res.success) {
statusText.textContent = (res && res.data) ? String(res.data) : 'Check failed.';
} else {
statusText.textContent = res.data.message || 'Check complete.';
if (res.data.status === 'available' && res.data.download_url) {
setTimeout(function () { window.location.reload(); }, 400);
}
}
})
.catch(function () { statusText.textContent = 'Network error — try again in a moment.'; })
.finally(function () {
btn.disabled = false;
btn.textContent = orig;
});
});
})();
</script>
<?php
}