Skip to content

Commit 7fa4542

Browse files
authored
feat: add site settings request (#5)
1 parent 4daaba0 commit 7fa4542

File tree

6 files changed

+143
-16
lines changed

6 files changed

+143
-16
lines changed

src/Facades/WordPress.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Storipress\WordPress\Requests\GeneralRequest;
1010
use Storipress\WordPress\Requests\Media;
1111
use Storipress\WordPress\Requests\Post;
12+
use Storipress\WordPress\Requests\Site;
1213
use Storipress\WordPress\Requests\Tag;
1314
use Storipress\WordPress\Requests\User;
1415

@@ -19,19 +20,20 @@
1920
* @method static Category category()
2021
* @method static Tag tag()
2122
* @method static Media media()
23+
* @method static Site site()
2224
* @method static \Storipress\WordPress\WordPress instance()
23-
* @method static \Storipress\WordPress\WordPress site()
24-
* @method static \Storipress\WordPress\WordPress setSite(string $site)
25-
* @method static \Storipress\WordPress\WordPress username()
25+
* @method static string url()
26+
* @method static \Storipress\WordPress\WordPress setUrl(string $url)
27+
* @method static string username()
2628
* @method static \Storipress\WordPress\WordPress setUsername(string $username)
27-
* @method static \Storipress\WordPress\WordPress password()
29+
* @method static string password()
2830
* @method static \Storipress\WordPress\WordPress setPassword(string $password)
29-
* @method static \Storipress\WordPress\WordPress userAgent()
31+
* @method static string|null userAgent()
3032
* @method static \Storipress\WordPress\WordPress withUserAgent(string $userAgent)
31-
* @method static \Storipress\WordPress\WordPress prefix()
33+
* @method static string prefix()
3234
* @method static \Storipress\WordPress\WordPress setPrefix(string $prefix)
3335
* @method static \Storipress\WordPress\WordPress prettyUrl()
34-
* @method static \Storipress\WordPress\WordPress isPrettyUrl()
36+
* @method static bool isPrettyUrl()
3537
*/
3638
class WordPress extends Facade
3739
{

src/Objects/Site.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Storipress\WordPress\Objects;
6+
7+
class Site extends WordPressObject
8+
{
9+
public string $title;
10+
11+
public string $description;
12+
13+
public string $url;
14+
15+
public string $email;
16+
17+
public string $timezone;
18+
19+
public string $date_format;
20+
21+
public string $time_format;
22+
23+
public int $start_of_week;
24+
25+
public string $language;
26+
27+
public bool $use_smilies;
28+
29+
public int $default_category;
30+
31+
public string $default_post_format;
32+
33+
public int $posts_per_page;
34+
35+
public string $show_on_front;
36+
37+
public int $page_on_front;
38+
39+
public int $page_for_posts;
40+
41+
public string $default_ping_status;
42+
43+
public string $default_comment_status;
44+
45+
public ?int $site_logo;
46+
47+
public int $site_icon;
48+
}

src/Requests/GeneralRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ public function getUrl(string $path, string $prefix, bool $pretty): string
6161
if ($pretty) {
6262
return sprintf(
6363
'%s/%s/%s',
64-
rtrim($this->app->site(), '/'),
64+
rtrim($this->app->url(), '/'),
6565
$prefix,
6666
ltrim($path, '/'),
6767
);
6868
}
6969

7070
return sprintf(
7171
'%s?rest_route=/%s',
72-
rtrim($this->app->site(), '/'),
72+
rtrim($this->app->url(), '/'),
7373
ltrim($path, '/'),
7474
);
7575
}

src/Requests/Request.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function getUrl(string $path, string $prefix, bool $pretty): string
105105
if ($pretty) {
106106
return sprintf(
107107
'%s/%s/wp/%s/%s',
108-
rtrim($this->app->site(), '/'),
108+
rtrim($this->app->url(), '/'),
109109
$prefix,
110110
self::VERSION,
111111
ltrim($path, '/'),
@@ -114,7 +114,7 @@ public function getUrl(string $path, string $prefix, bool $pretty): string
114114

115115
return sprintf(
116116
'%s?rest_route=/wp/%s/%s',
117-
rtrim($this->app->site(), '/'),
117+
rtrim($this->app->url(), '/'),
118118
self::VERSION,
119119
ltrim($path, '/'),
120120
);

src/Requests/Site.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Storipress\WordPress\Requests;
6+
7+
use Storipress\WordPress\Exceptions\WordPressException;
8+
use Storipress\WordPress\Objects\Site as SiteObject;
9+
10+
class Site extends Request
11+
{
12+
/**
13+
* https://developer.wordpress.org/rest-api/reference/settings/#retrieve-a-site-setting
14+
*
15+
*
16+
* @throws WordPressException
17+
*/
18+
public function retrieve(): SiteObject
19+
{
20+
$data = $this->request('get', '/settings');
21+
22+
if (is_array($data)) {
23+
throw $this->unexpectedValueException();
24+
}
25+
26+
return SiteObject::from($data);
27+
}
28+
29+
/**
30+
* https://developer.wordpress.org/rest-api/reference/settings/#update-a-site-setting
31+
*
32+
* @param array{
33+
* title?: string,
34+
* description?: string,
35+
* url?: string,
36+
* email?: string,
37+
* timezone?: string,
38+
* date_format?: string,
39+
* time_format?: string,
40+
* start_of_week?: int,
41+
* language?: string,
42+
* use_smilies?: bool,
43+
* default_category?: int,
44+
* default_post_format?: string,
45+
* posts_per_page?: int,
46+
* show_on_front?: string,
47+
* page_on_front?: int,
48+
* page_for_posts?: int,
49+
* default_ping_status?: string,
50+
* default_comment_status?: string,
51+
* site_logo?: int|null,
52+
* site_icon?: int,
53+
* } $arguments
54+
*
55+
* @throws WordPressException
56+
*/
57+
public function update(array $arguments): SiteObject
58+
{
59+
$data = $this->request('post', '/settings', $arguments);
60+
61+
if (is_array($data)) {
62+
throw $this->unexpectedValueException();
63+
}
64+
65+
return SiteObject::from($data);
66+
}
67+
}

src/WordPress.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Storipress\WordPress\Requests\GeneralRequest;
1010
use Storipress\WordPress\Requests\Media;
1111
use Storipress\WordPress\Requests\Post;
12+
use Storipress\WordPress\Requests\Site;
1213
use Storipress\WordPress\Requests\Tag;
1314
use Storipress\WordPress\Requests\User;
1415

@@ -26,7 +27,9 @@ class WordPress
2627

2728
protected readonly Media $media;
2829

29-
protected string $site;
30+
protected readonly Site $site;
31+
32+
protected string $url;
3033

3134
protected string $username;
3235

@@ -52,21 +55,23 @@ public function __construct(
5255
$this->tag = new Tag($this);
5356

5457
$this->media = new Media($this);
58+
59+
$this->site = new Site($this);
5560
}
5661

5762
public function instance(): static
5863
{
5964
return $this;
6065
}
6166

62-
public function site(): string
67+
public function url(): string
6368
{
64-
return $this->site;
69+
return $this->url;
6570
}
6671

67-
public function setSite(string $site): static
72+
public function setUrl(string $url): static
6873
{
69-
$this->site = $site;
74+
$this->url = $url;
7075

7176
return $this;
7277
}
@@ -160,4 +165,9 @@ public function media(): Media
160165
{
161166
return $this->media;
162167
}
168+
169+
public function site(): Site
170+
{
171+
return $this->site;
172+
}
163173
}

0 commit comments

Comments
 (0)