• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

timber / timber / 5692353129

pending completion
5692353129

push

github

Levdbas
Make suggested typecast changes by @gchtr

3 of 3 new or added lines in 1 file covered. (100.0%)

3931 of 4438 relevant lines covered (88.58%)

58.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.1
/src/Site.php
1
<?php
2

3
namespace Timber;
4

5
use WP_Site;
6

7
/**
8
 * Class Site
9
 *
10
 * `Timber\Site` gives you access to information you need about your site. In Multisite setups, you
11
 * can get info on other sites in your network.
12
 *
13
 * @api
14
 * @example
15
 * ```php
16
 * $other_site_id = 2;
17
 *
18
 * $context = Timber::context( [
19
 *     'other_site' => new Timber\Site( $other_site_id ),
20
 * ] );
21
 *
22
 * Timber::render('index.twig', $context);
23
 * ```
24
 * ```twig
25
 * My site is called {{site.name}}, another site on my network is {{other_site.name}}
26
 * ```
27
 * ```html
28
 * My site is called Jared's blog, another site on my network is Upstatement.com
29
 * ```
30
 */
31
class Site extends Core implements CoreInterface
32
{
33
    /**
34
     * The underlying WordPress Core object.
35
     *
36
     * @since 2.0.0
37
     *
38
     * @var WP_Site|null Will only be filled in multisite environments. Otherwise `null`.
39
     */
40
    protected ?WP_Site $wp_object;
41

42
    /**
43
     * @api
44
     * @var string The admin email address set in the WP admin panel
45
     */
46
    public $admin_email;
47

48
    /**
49
     * @api
50
     * @var string
51
     */
52
    public $blogname;
53

54
    /**
55
     * @api
56
     * @var string
57
     */
58
    public $charset;
59

60
    /**
61
     * @api
62
     * @var string
63
     */
64
    public $description;
65

66
    /**
67
     * @api
68
     * @var int the ID of a site in multisite
69
     */
70
    public $id;
71

72
    /**
73
     * @api
74
     * @var string the language setting ex: en-US
75
     */
76
    public $language;
77

78
    /**
79
     * @api
80
     * @var bool true if multisite, false if plain ole' WordPress
81
     */
82
    public $multisite;
83

84
    /**
85
     * @api
86
     * @var string
87
     */
88
    public $name;
89

90
    /**
91
     * @deprecated 2.0.0, use $pingback_url
92
     * @var string for people who like trackback spam
93
     */
94
    public $pingback;
95

96
    /**
97
     * @api
98
     * @var string for people who like trackback spam
99
     */
100
    public $pingback_url;
101

102
    /**
103
     * @api
104
     * @var string
105
     */
106
    public $siteurl;
107

108
    /**
109
     * @api
110
     * @var \Timber\Theme
111
     */
112
    public $theme;
113

114
    /**
115
     * @api
116
     * @var string
117
     */
118
    public $title;
119

120
    /**
121
     * @api
122
     * @var string
123
     */
124
    public $url;
125

126
    /**
127
     * @api
128
     * @var string
129
     */
130
    public $home_url;
131

132
    /**
133
     * @api
134
     * @var string
135
     */
136
    public $site_url;
137

138
    /**
139
     * @api
140
     * @var string
141
     */
142
    public $rdf;
143

144
    public $rss;
145

146
    public $rss2;
147

148
    public $atom;
149

150
    /**
151
     * Constructs a Timber\Site object
152
     * @api
153
     * @example
154
     * ```php
155
     * //multisite setup
156
     * $site = new Timber\Site(1);
157
     * $site_two = new Timber\Site("My Cool Site");
158
     * //non-multisite
159
     * $site = new Timber\Site();
160
     * ```
161
     * @param string|int $site_name_or_id
162
     */
163
    public function __construct($site_name_or_id = null)
164
    {
165
        if (\is_multisite()) {
56✔
166
            $blog_id = self::switch_to_blog($site_name_or_id);
×
167
            $this->init();
×
168
            $this->init_as_multisite($blog_id);
×
169
            \restore_current_blog();
×
170
        } else {
171
            $this->init();
56✔
172
            $this->init_as_singlesite();
56✔
173
        }
174
    }
175

176
    /**
177
     * Gets the underlying WordPress Core object.
178
     *
179
     * @since 2.0.0
180
     *
181
     * @return WP_Site|null Will only return a `WP_Site` object in multisite environments. Otherwise `null`.
182
     */
183
    public function wp_object(): ?WP_Site
184
    {
185
        return $this->wp_object;
1✔
186
    }
187

188
    /**
189
     * Switches to the blog requested in the request
190
     *
191
     * @param string|integer|null $blog_identifier The name or ID of the blog to switch to. If `null`, the current blog.
192
     * @return integer with the ID of the new blog
193
     */
194
    protected static function switch_to_blog($blog_identifier = null): int
195
    {
196
        $current_id = \get_current_blog_id();
×
197

198
        if ($blog_identifier === null) {
×
199
            $blog_identifier = $current_id;
×
200
        }
201

202
        $info = \get_blog_details($blog_identifier);
×
203

204
        if (false === $info) {
×
205
            return $current_id;
×
206
        }
207

208
        (int) $blog_identifier = $info->blog_id;
×
209

210
        if ($current_id !== (int) $blog_identifier) {
×
211
            \switch_to_blog($blog_identifier);
×
212
        }
213

214
        return (int) $blog_identifier;
×
215
    }
216

217
    /**
218
     * @internal
219
     * @param integer $site_id
220
     */
221
    protected function init_as_multisite($site_id)
222
    {
223
        $wp_site = \get_blog_details($site_id);
×
224
        $this->import($wp_site);
×
225
        $this->ID = $wp_site->blog_id;
×
226
        $this->id = $this->ID;
×
227
        // Site might be false, but $wp_object needs to be null if it can’t be set.
228
        $this->wp_object = $wp_site ?: null;
×
229
        $this->name = $this->blogname;
×
230
        $this->title = $this->blogname;
×
231
        $theme_slug = \get_blog_option($wp_site->blog_id, 'stylesheet');
×
232
        $this->theme = new Theme($theme_slug);
×
233
        $this->description = \get_blog_option($wp_site->blog_id, 'blogdescription');
×
234
        $this->admin_email = \get_blog_option($wp_site->blog_id, 'admin_email');
×
235
        $this->multisite = true;
×
236
    }
237

238
    /**
239
     * Executed for single-blog sites
240
     * @internal
241
     */
242
    protected function init_as_singlesite()
243
    {
244
        // No WP_Site object available in single site environments.
245
        $this->wp_object = null;
56✔
246

247
        $this->admin_email = \get_bloginfo('admin_email');
56✔
248
        $this->name = \get_bloginfo('name');
56✔
249
        $this->title = $this->name;
56✔
250
        $this->description = \get_bloginfo('description');
56✔
251
        $this->theme = new Theme();
56✔
252
        $this->multisite = false;
56✔
253
    }
254

255
    /**
256
     * Executed for all types of sites: both multisite and "regular"
257
     * @internal
258
     */
259
    protected function init()
260
    {
261
        $this->url = \home_url();
56✔
262
        $this->home_url = $this->url;
56✔
263
        $this->site_url = \site_url();
56✔
264
        $this->rdf = \get_bloginfo('rdf_url');
56✔
265
        $this->rss = \get_bloginfo('rss_url');
56✔
266
        $this->rss2 = \get_bloginfo('rss2_url');
56✔
267
        $this->atom = \get_bloginfo('atom_url');
56✔
268
        $this->language = \get_locale();
56✔
269
        $this->charset = \get_bloginfo('charset');
56✔
270
        $this->pingback = $this->pingback_url = \get_bloginfo('pingback_url');
56✔
271
    }
272

273
    /**
274
     * Returns the language attributes that you're looking for
275
     * @return string
276
     */
277
    public function language_attributes()
278
    {
279
        return \get_language_attributes();
1✔
280
    }
281

282
    /**
283
     * Get the value for a site option.
284
     *
285
     * @api
286
     * @example
287
     * ```twig
288
     * Published on: {{ post.date|date(site.date_format) }}
289
     * ```
290
     *
291
     * @param string $option The name of the option to get the value for.
292
     *
293
     * @return mixed The option value.
294
     */
295
    public function __get($option)
296
    {
297
        if (!isset($this->$option)) {
3✔
298
            if (\is_multisite()) {
3✔
299
                $this->$option = \get_blog_option($this->ID, $option);
×
300
            } else {
301
                $this->$option = \get_option($option);
3✔
302
            }
303
        }
304

305
        return $this->$option;
3✔
306
    }
307

308
    /**
309
     * Get the value for a site option.
310
     *
311
     * @api
312
     * @example
313
     * ```twig
314
     * Published on: {{ post.date|date(site.option('date_format')) }}
315
     * ```
316
     *
317
     * @param string $option The name of the option to get the value for.
318
     *
319
     * @return mixed The option value.
320
     */
321
    public function option($option)
322
    {
323
        return $this->__get($option);
1✔
324
    }
325

326
    /**
327
     * Get the value for a site option.
328
     *
329
     * @api
330
     * @deprecated 2.0.0, use `{{ site.option }}` instead
331
     */
332
    public function meta($option)
333
    {
334
        Helper::deprecated('{{ site.meta() }}', '{{ site.option() }}', '2.0.0');
1✔
335

336
        return $this->__get($option);
1✔
337
    }
338

339
    /**
340
     * @api
341
     * @return null|\Timber\Image
342
     */
343
    public function icon()
344
    {
345
        if (\is_multisite()) {
2✔
346
            return $this->icon_multisite($this->ID);
×
347
        }
348
        $iid = \get_option('site_icon');
2✔
349
        if ($iid) {
2✔
350
            return Timber::get_post($iid);
1✔
351
        }
352

353
        return null;
1✔
354
    }
355

356
    protected function icon_multisite($site_id)
357
    {
358
        $image = null;
×
359
        $blog_id = self::switch_to_blog($site_id);
×
360
        $iid = \get_blog_option($blog_id, 'site_icon');
×
361
        if ($iid) {
×
362
            $image = Timber::get_post($iid);
×
363
        }
364
        \restore_current_blog();
×
365
        return $image;
×
366
    }
367

368
    /**
369
     * Returns the link to the site's home.
370
     *
371
     * @api
372
     * @example
373
     * ```twig
374
     * <a href="{{ site.link }}" title="Home">
375
     *       <img src="/wp-content/uploads/logo.png" alt="Logo for some stupid thing" />
376
     * </a>
377
     * ```
378
     * ```html
379
     * <a href="http://example.org" title="Home">
380
     *       <img src="/wp-content/uploads/logo.png" alt="Logo for some stupid thing" />
381
     * </a>
382
     * ```
383
     *
384
     * @return string
385
     */
386
    public function link()
387
    {
388
        return $this->url;
1✔
389
    }
390

391
    /**
392
     * Updates a site option.
393
     *
394
     * @deprecated 2.0.0 Use `update_option()` or `update_blog_option()` instead.
395
     *
396
     * @param string $key   The key of the site option to update.
397
     * @param mixed  $value The new value.
398
     */
399
    public function update($key, $value)
400
    {
401
        Helper::deprecated('Timber\Site::update()', 'update_option()', '2.0.0');
1✔
402

403
        /**
404
         * Filters a value before it is updated in the site options.
405
         *
406
         * @since 2.0.0
407
         *
408
         * @param mixed        $value   The new value.
409
         * @param string       $key     The option key.
410
         * @param int          $site_id The site ID.
411
         * @param \Timber\Site $site    The site object.
412
         */
413
        $value = \apply_filters('timber/site/update_option', $value, $key, $this->ID, $this);
1✔
414

415
        /**
416
         * Filters a value before it is updated in the site options.
417
         *
418
         * @deprecated 2.0.0, use `timber/site/update_option`
419
         * @since 0.20.0
420
         */
421
        $value = \apply_filters_deprecated(
1✔
422
            'timber_site_set_meta',
1✔
423
            [$value, $key, $this->ID, $this],
1✔
424
            '2.0.0',
1✔
425
            'timber/site/update_option'
1✔
426
        );
1✔
427

428
        if (\is_multisite()) {
1✔
429
            \update_blog_option($this->ID, $key, $value);
×
430
        } else {
431
            \update_option($key, $value);
1✔
432
        }
433
        $this->$key = $value;
1✔
434
    }
435
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc