Completed
Push — master ( 7bfd2b...c80a15 )
by Andrew
01:40
created

getTitleFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
/**
3
 * Adds enhanced HTML SEO metadata.
4
 *
5
 * @package SEO
6
 * @subpackage Metadata
7
 * @author Andrew Gerber <[email protected]>
8
 * @version 1.0.0
9
 */
10
11
/**
12
 * Class SEO_Metadata_SiteConfig_DataExtension
13
 *
14
 * Adds additional statuses and defaults to control metadata output.
15
 */
16
class SEO_Metadata_SiteConfig_DataExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
19
    /* Attributes
20
    ------------------------------------------------------------------------------*/
21
22
    //// statuses
23
24
    /**
25
     * Character set status.
26
     *
27
     * Boolean value governing whether the character set is output.
28
     *
29
     * @var bool $CharsetStatus
30
     */
31
    protected static $CharsetStatus = false;
32
33
    /**
34
     * `rel="canonical"` status.
35
     *
36
     * Boolean value governing whether canonical links are output.
37
     *
38
     * @var bool $CanonicalStatus
39
     */
40
    protected static $CanonicalStatus = false;
41
42
    /**
43
     * Title status.
44
     *
45
     * Boolean value governing whether the page title should be output.
46
     *
47
     * @var bool $TitleStatus
48
     */
49
    protected static $TitleStatus = false;
50
51
    /**
52
     * Extra metadata status.
53
     *
54
     * Boolean value governing whether additional (arbitrary) metadata can be added to pages.
55
     *
56
     * @var bool $ExtraMetaStatus
57
     */
58
    protected static $ExtraMetaStatus = false;
59
60
    //// defaults
61
62
    /**
63
     * Character set.
64
     *
65
     * The character set to be used. Should always be `UTF-8` except for fringe configurations.
66
     *
67
     * @var string
68
     */
69
    protected static $Charset = 'UTF-8';
70
71
    /**
72
     * Default title separator.
73
     *
74
     * The default title (primary) separator.
75
     *
76
     * @var string
77
     */
78
    protected static $TitleSeparatorDefault = '|';
79
80
    /**
81
     * Default tagline separator.
82
     *
83
     * The default tagline (secondary) separator.
84
     *
85
     * @var string
86
     */
87
    protected static $TaglineSeparatorDefault = '-';
88
89
    /**
90
     * Title ordering options.
91
     *
92
     * @var array
93
     */
94
    protected static $TitleOrderOptions = array(
95
        'first' => 'Page Title | Website Name - Tagline',
96
        'last' => 'Website Name - Tagline | Page Title'
97
    );
98
99
100
    /* Status Methods
101
    ------------------------------------------------------------------------------*/
102
103
    /**
104
     * Character set enabled.
105
     *
106
     * Gets whether the character set should be output.
107
     *
108
     * @return bool
109
     */
110
    public function CharsetEnabled()
111
    {
112
        return ($this->owner->config()->CharsetStatus === true) ? true : self::$CharsetStatus;
113
    }
114
115
    /**
116
     * Canonical links enabled.
117
     *
118
     * Gets whether the canonical link should be output.
119
     *
120
     * @return bool
121
     */
122
    public function CanonicalEnabled()
123
    {
124
        return ($this->owner->config()->CanonicalStatus === true) ? true : self::$CanonicalStatus;
125
    }
126
127
    /**
128
     * Title enabled.
129
     *
130
     * Gets whether the title should be output.
131
     *
132
     * @return bool
133
     */
134
    public function TitleEnabled()
135
    {
136
        return ($this->owner->config()->TitleStatus === true) ? true : self::$TitleStatus;
137
    }
138
139
    /**
140
     * Extra metadata enabled.
141
     *
142
     * Gets whether additional (arbitrary) metadata should be output.
143
     *
144
     * @return bool
145
     */
146
    public function ExtraMetaEnabled()
147
    {
148
        return ($this->owner->config()->ExtraMetaStatus === true) ? true : self::$ExtraMetaStatus;
149
    }
150
151
152
    /* Config Methods
153
    ------------------------------------------------------------------------------*/
154
155
    /**
156
     * Character set.
157
     *
158
     * Gets the character set from configuration, or uses the class-defined default.
159
     *
160
     * @return string
161
     */
162
    public function Charset()
163
    {
164
        return ($this->owner->config()->Charset) ? $this->owner->config()->Charset : self::$Charset;
165
    }
166
167
168
    /* Overload Model
169
    ------------------------------------------------------------------------------*/
170
171
    /**
172
     * Database fields.
173
     *
174
     * An associative array of database fields: `name` => `type`.
175
     *
176
     * @var array $db
177
     */
178
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
179
        'TitleOrder' => 'Enum(array("first", "last"), "first")',
180
        'Title' => 'Text', // redundant, but included for backwards-compatibility
181
        'TitleSeparator' => 'Varchar(1)',
182
        'Tagline' => 'Text', // redundant, but included for backwards-compatibility
183
        'TaglineSeparator' => 'Varchar(1)',
184
    );
185
186
187
    /* Overload Methods
188
    ------------------------------------------------------------------------------*/
189
190
    // @todo @inheritdoc ?? or does it happen automagically as promised?
191
    public function updateCMSFields(FieldList $fields)
192
    {
193
        // Tab Set
194
        $fields->addFieldToTab('Root', new TabSet('Metadata'), 'Access');
195
196
        // Title
197
        if ($this->TitleEnabled()) {
198
            $fields->addFieldsToTab('Root.Metadata.Title', $this->owner->getTitleFields());
199
        }
200
    }
201
202
203
    /* Custom Methods
204
    ------------------------------------------------------------------------------*/
205
206
    /**
207
     * Gets the title fields.
208
     *
209
     * This approach for getting fields for updateCMSFields is to be duplicated through all other modules to reduce complexity.
210
     *
211
     * @TODO i18n implementation
212
     *
213
     * @return array
214
     */
215
    public function getTitleFields() {
216
        return array(
217
            // Information
218
            LabelField::create('FaviconDescription', 'A title tag is the main text that describes an online document. Title elements have long been considered one of the most important on-page SEO elements (the most important being overall content), and appear in three key places: browsers, search engine results pages, and external websites.<br />@ <a href="https://moz.com/learn/seo/title-tag" target="_blank">Title Tag - Learn SEO - Mozilla</a>')
219
                ->addExtraClass('information'),
220
            // Title Order
221
            DropdownField::create('TitleOrder', 'Page Title Order', self::$TitleOrderOptions),
222
            // Title Separator
223
            TextField::create('TitleSeparator', 'Page Title Separator')
224
                ->setAttribute('placeholder', self::$TitleSeparatorDefault)
225
                ->setAttribute('size', 1)
226
                ->setMaxLength(1)
227
                ->setDescription('max 1 character'),
228
            // Title
229
            TextField::create('Title', 'Website Name'),
230
            // Tagline Separator
231
            TextField::create('TaglineSeparator', 'Tagline Separator')
232
                ->setAttribute('placeholder', self::$TaglineSeparatorDefault)
233
                ->setAttribute('size', 1)
234
                ->setMaxLength(1)
235
                ->setDescription('max 1 character'),
236
            // Tagline
237
            TextField::create('Tagline', 'Tagline')
238
                ->setDescription('optional')
239
        );
240
    }
241
242
243
    /* Custom Methods
244
    ------------------------------------------------------------------------------*/
245
246
    /**
247
     * Fetch title separator.
248
     *
249
     * Fetches the title (primary) separator, falls back to default.
250
     *
251
     * @return string
252
     */
253
    public function FetchTitleSeparator()
254
    {
255
        return ($this->owner->TitleSeparator) ? $this->owner->TitleSeparator : self::$TitleSeparatorDefault;
256
    }
257
258
    /**
259
     * Fetch tagline separator.
260
     *
261
     * Fetches the tagline (secondary) separator, falls back to default.
262
     *
263
     * @return string
264
     */
265
    public function FetchTaglineSeparator()
266
    {
267
        return ($this->owner->TaglineSeparator) ? $this->owner->TaglineSeparator : self::$TaglineSeparatorDefault;
268
    }
269
270
    /**
271
     * Generates HTML title based on configuration settings.
272
     *
273
     * @dev Override this function for any custom title functionality.
274
     *
275
     * @param string $pageTitle
276
     *
277
     * @return string
278
     */
279
    public function GenerateTitle($pageTitle = 'Title Error')
280
    {
281
        // if there is a site name
282
        if ($this->owner->Title) {
283
284
            // title parts, begin with name/title
285
            $titles = array($this->owner->Title);
286
287
            // tagline
288
            if ($this->owner->Tagline) {
289
                array_push($titles, $this->owner->FetchTaglineSeparator());
290
                array_push($titles, $this->owner->Tagline);
291
            }
292
293
            // page title
294
            if ($this->owner->TitleOrder == 'first') {
295
                // add to the beginning
296
                array_unshift($titles, $this->owner->FetchTitleSeparator());
297
                array_unshift($titles, $pageTitle);
298
            } else {
299
                // add to the end
300
                array_push($titles, $this->owner->FetchTitleSeparator());
301
                array_push($titles, $pageTitle);
302
            }
303
304
            // implode to create title
305
            $title = implode(' ', $titles);
306
307
            // removes whitespace before punctuation marks: `,.;:!?`
308
            // @todo isn't this a little bit random ?
309
            $title = preg_replace('/\s*[,.;:!?]/', '', $title);
310
311
            // return
312
            return $title;
313
314
        } else {
315
            // just return the page title if there is no site name
316
            return $pageTitle;
317
        }
318
    }
319
320
}
321