| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\QuickEdit\Controllers; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('No direct access.'); |
| 6 |
|
| 7 |
// Bypasses SaveController because title/tagline/logo live in wp_options, |
| 8 |
// not post_content. logo_id = 0 deletes the option (Customizer parity). |
| 9 |
class SiteIdentityController |
| 10 |
{ |
| 11 |
public static function init() |
| 12 |
{ |
| 13 |
add_action('rest_api_init', [self::class, 'registerRoutes']); |
| 14 |
} |
| 15 |
|
| 16 |
public static function registerRoutes() |
| 17 |
{ |
| 18 |
register_rest_route('extendify/v1', '/quick-edit/site-identity', [ |
| 19 |
[ |
| 20 |
'methods' => 'GET', |
| 21 |
'permission_callback' => [self::class, 'permissionCallback'], |
| 22 |
'callback' => [self::class, 'handle'], |
| 23 |
], |
| 24 |
[ |
| 25 |
'methods' => 'POST', |
| 26 |
'permission_callback' => [self::class, 'permissionCallback'], |
| 27 |
'callback' => [self::class, 'handle'], |
| 28 |
], |
| 29 |
]); |
| 30 |
} |
| 31 |
|
| 32 |
public static function permissionCallback(): bool |
| 33 |
{ |
| 34 |
// Site-wide settings — manage_options rather than edit_posts. |
| 35 |
return current_user_can('manage_options'); |
| 36 |
} |
| 37 |
|
| 38 |
public static function handle(\WP_REST_Request $req) |
| 39 |
{ |
| 40 |
if ($req->get_method() === 'GET') { |
| 41 |
$logoId = (int) get_option('site_logo'); |
| 42 |
$logoUrl = $logoId ? wp_get_attachment_image_url($logoId, 'medium') : ''; |
| 43 |
// WP's sanitize_option filter esc_html()s blogname/blogdescription |
| 44 |
// before storage, so the DB literally contains entities like |
| 45 |
// `'`. React sets `value` as a property (no markup parsing, |
| 46 |
// no entity decoding), so the input would show `'` instead of |
| 47 |
// `'`. update_option re-applies esc_html on save, so the round-trip |
| 48 |
// is consistent. |
| 49 |
return new \WP_REST_Response([ |
| 50 |
'title' => wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES), |
| 51 |
'tagline' => wp_specialchars_decode(get_bloginfo('description'), ENT_QUOTES), |
| 52 |
'logo_id' => $logoId, |
| 53 |
'logo_url' => $logoUrl ?: '', |
| 54 |
]); |
| 55 |
} |
| 56 |
|
| 57 |
$params = $req->get_json_params(); |
| 58 |
if (!is_array($params)) { |
| 59 |
$params = []; |
| 60 |
} |
| 61 |
if (isset($params['title'])) { |
| 62 |
update_option('blogname', sanitize_text_field($params['title'])); |
| 63 |
} |
| 64 |
if (isset($params['tagline'])) { |
| 65 |
update_option('blogdescription', sanitize_text_field($params['tagline'])); |
| 66 |
} |
| 67 |
if (array_key_exists('logo_id', $params)) { |
| 68 |
$lid = (int) $params['logo_id']; |
| 69 |
if ($lid) { |
| 70 |
update_option('site_logo', $lid); |
| 71 |
} else { |
| 72 |
delete_option('site_logo'); |
| 73 |
} |
| 74 |
} |
| 75 |
return new \WP_REST_Response(['ok' => true]); |
| 76 |
} |
| 77 |
} |
| 78 |
|