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

timber / timber / 5690057835

pending completion
5690057835

push

github

nlemoine
Merge branch '2.x' of github.com:timber/timber into 2.x-refactor-file-models

# Conflicts:
#	src/Attachment.php
#	src/ExternalImage.php
#	src/FileSize.php
#	src/URLHelper.php

1134 of 1134 new or added lines in 55 files covered. (100.0%)

3923 of 4430 relevant lines covered (88.56%)

59.08 hits per line

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

59.74
/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 $site_name_or_id
192
     * @return integer with the ID of the new blog
193
     */
194
    protected static function switch_to_blog($site_name_or_id)
195
    {
196
        if ($site_name_or_id === null) {
×
197
            $site_name_or_id = \get_current_blog_id();
×
198
        }
199
        $info = \get_blog_details($site_name_or_id);
×
200
        \switch_to_blog($info->blog_id);
×
201
        return $info->blog_id;
×
202
    }
203

204
    /**
205
     * @internal
206
     * @param integer $site_id
207
     */
208
    protected function init_as_multisite($site_id)
209
    {
210
        $wp_site = \get_blog_details($site_id);
×
211
        $this->import($wp_site);
×
212
        $this->ID = $wp_site->blog_id;
×
213
        $this->id = $this->ID;
×
214
        // Site might be false, but $wp_object needs to be null if it can’t be set.
215
        $this->wp_object = $wp_site ?: null;
×
216
        $this->name = $this->blogname;
×
217
        $this->title = $this->blogname;
×
218
        $theme_slug = \get_blog_option($wp_site->blog_id, 'stylesheet');
×
219
        $this->theme = new Theme($theme_slug);
×
220
        $this->description = \get_blog_option($wp_site->blog_id, 'blogdescription');
×
221
        $this->admin_email = \get_blog_option($wp_site->blog_id, 'admin_email');
×
222
        $this->multisite = true;
×
223
    }
224

225
    /**
226
     * Executed for single-blog sites
227
     * @internal
228
     */
229
    protected function init_as_singlesite()
230
    {
231
        // No WP_Site object available in single site environments.
232
        $this->wp_object = null;
56✔
233

234
        $this->admin_email = \get_bloginfo('admin_email');
56✔
235
        $this->name = \get_bloginfo('name');
56✔
236
        $this->title = $this->name;
56✔
237
        $this->description = \get_bloginfo('description');
56✔
238
        $this->theme = new Theme();
56✔
239
        $this->multisite = false;
56✔
240
    }
241

242
    /**
243
     * Executed for all types of sites: both multisite and "regular"
244
     * @internal
245
     */
246
    protected function init()
247
    {
248
        $this->url = \home_url();
56✔
249
        $this->home_url = $this->url;
56✔
250
        $this->site_url = \site_url();
56✔
251
        $this->rdf = \get_bloginfo('rdf_url');
56✔
252
        $this->rss = \get_bloginfo('rss_url');
56✔
253
        $this->rss2 = \get_bloginfo('rss2_url');
56✔
254
        $this->atom = \get_bloginfo('atom_url');
56✔
255
        $this->language = \get_locale();
56✔
256
        $this->charset = \get_bloginfo('charset');
56✔
257
        $this->pingback = $this->pingback_url = \get_bloginfo('pingback_url');
56✔
258
    }
259

260
    /**
261
     * Returns the language attributes that you're looking for
262
     * @return string
263
     */
264
    public function language_attributes()
265
    {
266
        return \get_language_attributes();
1✔
267
    }
268

269
    /**
270
     * Get the value for a site option.
271
     *
272
     * @api
273
     * @example
274
     * ```twig
275
     * Published on: {{ post.date|date(site.date_format) }}
276
     * ```
277
     *
278
     * @param string $option The name of the option to get the value for.
279
     *
280
     * @return mixed The option value.
281
     */
282
    public function __get($option)
283
    {
284
        if (!isset($this->$option)) {
3✔
285
            if (\is_multisite()) {
3✔
286
                $this->$option = \get_blog_option($this->ID, $option);
×
287
            } else {
288
                $this->$option = \get_option($option);
3✔
289
            }
290
        }
291

292
        return $this->$option;
3✔
293
    }
294

295
    /**
296
     * Get the value for a site option.
297
     *
298
     * @api
299
     * @example
300
     * ```twig
301
     * Published on: {{ post.date|date(site.option('date_format')) }}
302
     * ```
303
     *
304
     * @param string $option The name of the option to get the value for.
305
     *
306
     * @return mixed The option value.
307
     */
308
    public function option($option)
309
    {
310
        return $this->__get($option);
1✔
311
    }
312

313
    /**
314
     * Get the value for a site option.
315
     *
316
     * @api
317
     * @deprecated 2.0.0, use `{{ site.option }}` instead
318
     */
319
    public function meta($option)
320
    {
321
        Helper::deprecated('{{ site.meta() }}', '{{ site.option() }}', '2.0.0');
1✔
322

323
        return $this->__get($option);
1✔
324
    }
325

326
    /**
327
     * @api
328
     * @return null|\Timber\Image
329
     */
330
    public function icon()
331
    {
332
        if (\is_multisite()) {
2✔
333
            return $this->icon_multisite($this->ID);
×
334
        }
335
        $iid = \get_option('site_icon');
2✔
336
        if ($iid) {
2✔
337
            return Timber::get_post($iid);
1✔
338
        }
339

340
        return null;
1✔
341
    }
342

343
    protected function icon_multisite($site_id)
344
    {
345
        $image = null;
×
346
        $blog_id = self::switch_to_blog($site_id);
×
347
        $iid = \get_blog_option($blog_id, 'site_icon');
×
348
        if ($iid) {
×
349
            $image = Timber::get_post($iid);
×
350
        }
351
        \restore_current_blog();
×
352
        return $image;
×
353
    }
354

355
    /**
356
     * Returns the link to the site's home.
357
     *
358
     * @api
359
     * @example
360
     * ```twig
361
     * <a href="{{ site.link }}" title="Home">
362
     *       <img src="/wp-content/uploads/logo.png" alt="Logo for some stupid thing" />
363
     * </a>
364
     * ```
365
     * ```html
366
     * <a href="http://example.org" title="Home">
367
     *       <img src="/wp-content/uploads/logo.png" alt="Logo for some stupid thing" />
368
     * </a>
369
     * ```
370
     *
371
     * @return string
372
     */
373
    public function link()
374
    {
375
        return $this->url;
1✔
376
    }
377

378
    /**
379
     * Updates a site option.
380
     *
381
     * @deprecated 2.0.0 Use `update_option()` or `update_blog_option()` instead.
382
     *
383
     * @param string $key   The key of the site option to update.
384
     * @param mixed  $value The new value.
385
     */
386
    public function update($key, $value)
387
    {
388
        Helper::deprecated('Timber\Site::update()', 'update_option()', '2.0.0');
1✔
389

390
        /**
391
         * Filters a value before it is updated in the site options.
392
         *
393
         * @since 2.0.0
394
         *
395
         * @param mixed        $value   The new value.
396
         * @param string       $key     The option key.
397
         * @param int          $site_id The site ID.
398
         * @param \Timber\Site $site    The site object.
399
         */
400
        $value = \apply_filters('timber/site/update_option', $value, $key, $this->ID, $this);
1✔
401

402
        /**
403
         * Filters a value before it is updated in the site options.
404
         *
405
         * @deprecated 2.0.0, use `timber/site/update_option`
406
         * @since 0.20.0
407
         */
408
        $value = \apply_filters_deprecated(
1✔
409
            'timber_site_set_meta',
1✔
410
            [$value, $key, $this->ID, $this],
1✔
411
            '2.0.0',
1✔
412
            'timber/site/update_option'
1✔
413
        );
1✔
414

415
        if (\is_multisite()) {
1✔
416
            \update_blog_option($this->ID, $key, $value);
×
417
        } else {
418
            \update_option($key, $value);
1✔
419
        }
420
        $this->$key = $value;
1✔
421
    }
422
}
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

© 2026 Coveralls, Inc