← All changes
|
json-endpoints/class.wpcom-json-api-update-site-logo-endpoint.php
+123
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,123 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | +/** | |
| 3 | + * Set site logo settings API. | |
| 4 | + * | |
| 5 | + * Endpoints: | |
| 6 | + * Set site logo settings: /sites/%s/logo | |
| 7 | + * Delete site logo settings: /sites/%s/logo/delete | |
| 8 | + */ | |
| 9 | + | |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | + exit( 0 ); | |
| 12 | +} | |
| 13 | + | |
| 14 | +new WPCOM_JSON_API_Update_Site_Logo_Endpoint( | |
| 15 | + array( | |
| 16 | + 'description' => 'Set site logo settings', | |
| 17 | + 'group' => '__do_not_document', | |
| 18 | + 'stat' => 'sites:1:logo', | |
| 19 | + 'method' => 'POST', | |
| 20 | + 'min_version' => '1.1', | |
| 21 | + 'path' => '/sites/%s/logo', | |
| 22 | + 'path_labels' => array( | |
| 23 | + '$site' => '(string) Site ID or domain.', | |
| 24 | + ), | |
| 25 | + 'request_format' => array( | |
| 26 | + 'id' => '(int) The ID of the logo post', | |
| 27 | + 'url' => '(string) The URL of the logo post (deprecated)', | |
| 28 | + ), | |
| 29 | + 'response_format' => array( | |
| 30 | + 'id' => '(int) The ID of the logo post', | |
| 31 | + 'url' => '(string) The URL of the logo post', | |
| 32 | + ), | |
| 33 | + 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/logo', | |
| 34 | + 'example_request_data' => array( | |
| 35 | + 'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ), | |
| 36 | + 'body' => array( | |
| 37 | + 'id' => 12345, | |
| 38 | + ), | |
| 39 | + ), | |
| 40 | + 'example_response' => ' | |
| 41 | + { | |
| 42 | + "id": 12345, | |
| 43 | + "url": "https:\/\/s.w.org\/about\/images\/logos\/codeispoetry-rgb.png" | |
| 44 | + }', | |
| 45 | + ) | |
| 46 | +); | |
| 47 | + | |
| 48 | +new WPCOM_JSON_API_Update_Site_Logo_Endpoint( | |
| 49 | + array( | |
| 50 | + 'description' => 'Delete site logo settings', | |
| 51 | + 'group' => '__do_not_document', | |
| 52 | + 'stat' => 'sites:1:logo:delete', | |
| 53 | + 'method' => 'POST', | |
| 54 | + 'min_version' => '1.1', | |
| 55 | + 'path' => '/sites/%s/logo/delete', | |
| 56 | + 'path_labels' => array( | |
| 57 | + '$site' => '(string) Site ID or domain.', | |
| 58 | + ), | |
| 59 | + 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/logo/delete', | |
| 60 | + 'example_request_data' => array( | |
| 61 | + 'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ), | |
| 62 | + ), | |
| 63 | + ) | |
| 64 | +); | |
| 65 | + | |
| 66 | +/** | |
| 67 | + * Set site logo settings API class. | |
| 68 | + * | |
| 69 | + * @phan-constructor-used-for-side-effects | |
| 70 | + */ | |
| 71 | +class WPCOM_JSON_API_Update_Site_Logo_Endpoint extends WPCOM_JSON_API_Endpoint { | |
| 72 | + /** | |
| 73 | + * Set site logo settings API callback. | |
| 74 | + * | |
| 75 | + * @param string $path API path. | |
| 76 | + * @param int $site_id Blog ID. | |
| 77 | + */ | |
| 78 | + public function callback( $path = '', $site_id = 0 ) { | |
| 79 | + // Switch to the given blog. | |
| 80 | + $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site_id ) ); | |
| 81 | + if ( is_wp_error( $blog_id ) ) { | |
| 82 | + return $blog_id; | |
| 83 | + } | |
| 84 | + | |
| 85 | + if ( ! current_user_can( 'edit_theme_options' ) ) { | |
| 86 | + return new WP_Error( 'unauthorized', 'User is not authorized to access logo settings', 403 ); | |
| 87 | + } | |
| 88 | + | |
| 89 | + if ( strpos( $path, '/delete' ) ) { | |
| 90 | + delete_option( 'site_logo' ); | |
| 91 | + return array(); | |
| 92 | + } | |
| 93 | + | |
| 94 | + $args = $this->input(); | |
| 95 | + $logo_settings = $this->get_current_settings(); | |
| 96 | + | |
| 97 | + if ( empty( $args ) || ! is_array( $args ) ) { | |
| 98 | + return $logo_settings; | |
| 99 | + } | |
| 100 | + | |
| 101 | + if ( isset( $args['id'] ) ) { | |
| 102 | + update_option( 'site_logo', (int) $args['id'] ); | |
| 103 | + } | |
| 104 | + | |
| 105 | + return $this->get_current_settings(); | |
| 106 | + } | |
| 107 | + | |
| 108 | + /** | |
| 109 | + * Get current logo settings. | |
| 110 | + */ | |
| 111 | + public function get_current_settings() { | |
| 112 | + $logo_id = get_option( 'site_logo' ); | |
| 113 | + | |
| 114 | + if ( ! $logo_id ) { | |
| 115 | + return array(); | |
| 116 | + } | |
| 117 | + | |
| 118 | + return array( | |
| 119 | + 'id' => $logo_id, | |
| 120 | + 'url' => wp_get_attachment_url( $logo_id ), | |
| 121 | + ); | |
| 122 | + } | |
| 123 | +} | |