Issues (257)

src/models/MetaSiteVars.php (2 issues)

1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\models;
13
14
use Craft;
15
use craft\errors\SiteNotFoundException;
16
use craft\helpers\DateTimeHelper;
17
use craft\validators\ArrayValidator;
18
use craft\validators\DateTimeValidator;
19
use DateTime;
20
use nystudio107\seomatic\base\VarsModel;
21
use nystudio107\seomatic\helpers\Json as JsonHelper;
22
use function is_array;
23
use function is_string;
24
25
/**
26
 * Site variables object, containing values that determine side-wide settings
27
 *
28
 * @author    nystudio107
29
 * @package   Seomatic
30
 * @since     3.0.0
31
 */
32
class MetaSiteVars extends VarsModel
33
{
34
    // Constants
35
    // =========================================================================
36
37
    public const CONTAINER_TYPE = 'MetaSiteVarsContainer';
38
39
    // Public Properties
40
    // =========================================================================
41
    /**
42
     * @var string The name of the website
43
     */
44
    public $siteName = '';
45
    /**
46
     * @var string The alternate name of the website
47
     */
48
    public $siteAlternateName = '';
49
    /**
50
     * @var Entity|array|null
51
     */
52
    public $identity;
53
    /**
54
     * @var Entity|array|null
55
     */
56
    public $creator;
57
    /**
58
     * @var string The Twitter handle
59
     */
60
    public $twitterHandle = '';
61
    /**
62
     * @var string The Facebook profile ID
63
     */
64
    public $facebookProfileId = '';
65
    /**
66
     * @var string The Facebook app ID
67
     */
68
    public $facebookAppId = '';
69
    /**
70
     * @var string The Google Site Verification code
71
     */
72
    public $googleSiteVerification = '';
73
    /**
74
     * @var string The Bing Site Verification code
75
     */
76
    public $bingSiteVerification = '';
77
    /**
78
     * @var string The Pinterest Site Verification code
79
     */
80
    public $pinterestSiteVerification = '';
81
    /**
82
     * @var string The Facebook Site Verification code
83
     */
84
    public $facebookSiteVerification = '';
85
    /**
86
     * @var array Array of links for Same As... sites, indexed by the handle
87
     */
88
    public $sameAsLinks = [];
89
    /**
90
     * @var string Google Site Links search target
91
     */
92
    public $siteLinksSearchTarget = '';
93
    /**
94
     * @var string Google Site Links query input
95
     */
96
    public $siteLinksQueryInput = '';
97
    /**
98
     * @var string Default referrer tag setting
99
     */
100
    public $referrer = 'no-referrer-when-downgrade';
101
    /**
102
     * @var array Array of additional custom sitemap URLs
103
     */
104
    public $additionalSitemapUrls = [];
105
    /**
106
     * @var DateTime
107
     */
108
    public $additionalSitemapUrlsDateUpdated;
109
    /**
110
     * @var array Array of additional sitemaps
111
     */
112
    public $additionalSitemaps = [];
113
114
    /**
115
     * @param array $config
116
     *
117
     * @return null|MetaSiteVars
118
     */
119
    public static function create(array $config = [])
120
    {
121
        $model = new MetaSiteVars($config);
122
        $model->normalizeData();
123
124
        return $model;
125
    }
126
127
    // Public Methods
128
    // =========================================================================
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public function init(): void
134
    {
135
        parent::init();
136
137
        // Set some default values
138
        if (empty($this->siteName)) {
139
            try {
140
                $currentSite = Craft::$app->getSites()->getCurrentSite();
141
            } catch (SiteNotFoundException $e) {
142
                $currentSite = null;
143
            }
144
            if ($currentSite) {
145
                /** @var string|array $siteName */
146
                $siteName = Craft::t('site', $currentSite->getName());
147
                if (is_array($siteName)) {
0 ignored issues
show
The condition is_array($siteName) is always false.
Loading history...
148
                    $siteName = reset($siteName);
149
                }
150
            }
151
            $this->siteName = $siteName ?? Craft::$app->getSystemName();
152
        }
153
    }
154
155
    /**
156
     * @inheritdoc
157
     */
158
    public function rules(): array
159
    {
160
        return [
161
            [
162
                [
163
                    'siteName',
164
                    'siteAlternateName',
165
                    'twitterHandle',
166
                    'facebookProfileId',
167
                    'facebookAppId',
168
                    'googleSiteVerification',
169
                    'bingSiteVerification',
170
                    'pinterestSiteVerification',
171
                    'facebookSiteVerification',
172
                    'siteLinksSearchTarget',
173
                    'siteLinksQueryInput',
174
                    'referrer',
175
                ],
176
                'string',
177
            ],
178
            [
179
                [
180
                    'additionalSitemapUrlsDateUpdated',
181
                ],
182
                DateTimeValidator::class,
183
            ],
184
            [
185
                [
186
                    'sameAsLinks',
187
                    'additionalSitemapUrls',
188
                    'additionalSitemaps',
189
                ],
190
                ArrayValidator::class,
191
            ],
192
        ];
193
    }
194
195
    /**
196
     * @inheritdoc
197
     */
198
    public function datetimeAttributes(): array
199
    {
200
        $attributes = parent::datetimeAttributes();
0 ignored issues
show
Deprecated Code introduced by
The function craft\base\Model::datetimeAttributes() has been deprecated: in 4.0.0. Use [[\DateTime]] type declarations instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

200
        $attributes = /** @scrutinizer ignore-deprecated */ parent::datetimeAttributes();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
201
202
        if (property_exists($this, 'additionalSitemapUrlsDateUpdated')) {
203
            $attributes[] = 'additionalSitemapUrlsDateUpdated';
204
        }
205
206
        return $attributes;
207
    }
208
209
    /**
210
     * Normalizes model data
211
     */
212
    public function normalizeData()
213
    {
214
        // Decode any JSON data
215
        $properties = $this->getAttributes();
216
        foreach ($properties as $property => $value) {
217
            if (!empty($value) && is_string($value)) {
218
                $this->$property = JsonHelper::decodeIfJson($value);
219
            }
220
        }
221
        // Convert our date attributes in the additionalSitemaps array
222
        if (!empty($this->additionalSitemaps)) {
223
            foreach ($this->additionalSitemaps as $index => $additionalSitemap) {
224
                if (!empty($additionalSitemap['lastmod'])) {
225
                    $this->additionalSitemaps[$index]['lastmod']
226
                        = DateTimeHelper::toDateTime($additionalSitemap['lastmod']);
227
                }
228
            }
229
        }
230
        // Make sure these are strings
231
        if (!empty($this->facebookProfileId)) {
232
            $this->facebookProfileId = (string)$this->facebookProfileId;
233
        }
234
        if (!empty($this->facebookAppId)) {
235
            $this->facebookAppId = (string)$this->facebookAppId;
236
        }
237
        // Identity
238
        if (is_array($this->identity)) {
239
            $this->identity = new Entity($this->identity);
240
        }
241
        // Creator
242
        if (is_array($this->creator)) {
243
            $this->creator = new Entity($this->creator);
244
        }
245
    }
246
}
247