Completed
Push — master ( 32ede4...dc669f )
by Andrew
01:57
created

code/SEO_Metadata_SiteConfig_DataExtension.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace {
4
    
5
    /**
6
     * Adds enhanced HTML SEO metadata.
7
     *
8
     * @package SEO
9
     * @subpackage Metadata
10
     * @author Andrew Gerber <[email protected]>
11
     * @version 1.0.0
12
     *
13
     */
14
    class SEO_Metadata_SiteConfig_DataExtension extends DataExtension
15
    {
16
17
18
        /* Static Variables
19
        ------------------------------------------------------------------------------*/
20
21
        // status variables
22
        private static $CharsetStatus = false;
23
        private static $CanonicalStatus = false;
24
        private static $TitleStatus = false;
25
        private static $ExtraMetaStatus = false;
26
27
        // defaults
28
        private static $Charset = 'UTF-8';
29
        private static $TitleSeparatorDefault = '|';
30
        private static $TaglineSeparatorDefault = '-';
31
32
33
        /* Status Methods
34
        ------------------------------------------------------------------------------*/
35
36
        /**
37
         * @return bool
38
         */
39
        public function CharsetEnabled()
40
        {
41
            return ($this->owner->config()->CharsetStatus === true) ? true : self::$CharsetStatus;
42
        }
43
44
        /**
45
         * @return bool
46
         */
47
        public function CanonicalEnabled()
48
        {
49
            return ($this->owner->config()->CanonicalStatus === true) ? true : self::$CanonicalStatus;
50
        }
51
52
        /**
53
         * @return bool
54
         */
55
        public function TitleEnabled()
56
        {
57
            return ($this->owner->config()->TitleStatus === true) ? true : self::$TitleStatus;
58
        }
59
60
        /**
61
         * @return bool
62
         */
63
        public function ExtraMetaEnabled()
64
        {
65
            return ($this->owner->config()->ExtraMetaStatus === true) ? true : self::$ExtraMetaStatus;
66
        }
67
68
69
        /* Config Methods
70
        ------------------------------------------------------------------------------*/
71
72
        /**
73
         * @return string
74
         */
75
        public function Charset()
76
        {
77
            return ($this->owner->config()->Charset) ? $this->owner->config()->Charset : self::$Charset;
78
        }
79
80
81
        /* Overload Model
82
        ------------------------------------------------------------------------------*/
83
84
        private static $db = array(
0 ignored issues
show
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...
85
            'TitleOrder' => 'Enum(array("first", "last"), "first")',
86
            'Title' => 'Text', // redundant, but included for backwards-compatibility
87
            'TitleSeparator' => 'Varchar(1)',
88
            'Tagline' => 'Text', // redundant, but included for backwards-compatibility
89
            'TaglineSeparator' => 'Varchar(1)',
90
        );
91
92
93
        /* Overload Methods
94
        ------------------------------------------------------------------------------*/
95
96
        // CMS Fields
97
        public function updateCMSFields(FieldList $fields)
98
        {
99
100
            // Tab Set
101
            $fields->addFieldToTab('Root', new TabSet('Metadata'), 'Access');
102
103
            //// Title
104
105
            if ($this->TitleEnabled()) {
106
107
                // Tab
108
                $tab = 'Root.Metadata.Title';
109
110
                // Title Order Options
111
                $titleOrderOptions = array(
112
                    'first' => 'Page Title | Website Name - Tagline',
113
                    'last' => 'Website Name - Tagline | Page Title'
114
                );
115
116
                // Fields
117
                $fields->addFieldsToTab($tab, array(
118
                    // Information
119
                    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>')
120
                        ->addExtraClass('information'),
121
                    // Title Order
122
                    DropdownField::create('TitleOrder', 'Page Title Order', $titleOrderOptions),
123
                    // Title Separator
124
                    TextField::create('TitleSeparator', 'Page Title Separator')
125
                        ->setAttribute('placeholder', self::$TitleSeparatorDefault)
126
                        ->setAttribute('size', 1)
127
                        ->setMaxLength(1)
128
                        ->setDescription('max 1 character'),
129
                    // Title
130
                    TextField::create('Title', 'Website Name'),
131
                    // Tagline Separator
132
                    TextField::create('TaglineSeparator', 'Tagline Separator')
133
                        ->setAttribute('placeholder', self::$TaglineSeparatorDefault)
134
                        ->setAttribute('size', 1)
135
                        ->setMaxLength(1)
136
                        ->setDescription('max 1 character'),
137
                    // Tagline
138
                    TextField::create('Tagline', 'Tagline')
139
                        ->setDescription('optional')
140
                ));
141
            }
142
143
        }
144
145
146
        /* Custom Methods
147
        ------------------------------------------------------------------------------*/
148
149
        /**
150
         * Fetches the title separator, falls back to default
151
         *
152
         * @return string
153
         */
154
        public function FetchTitleSeparator()
155
        {
156
157
            return ($this->owner->TitleSeparator) ? $this->owner->TitleSeparator : self::$TitleSeparatorDefault;
158
159
        }
160
161
        /**
162
         * Fetches the tagline separator, falls back to default
163
         *
164
         * @return string
165
         */
166
        public function FetchTaglineSeparator()
167
        {
168
169
            return ($this->owner->TaglineSeparator) ? $this->owner->TaglineSeparator : self::$TaglineSeparatorDefault;
170
171
        }
172
173
        /**
174
         * Generates HTML title based on configuration settings.
175
         *
176
         * @param string $pageTitle
177
         *
178
         * @return string
179
         */
180
        public function GenerateTitle($pageTitle = 'Title Error')
181
        {
182
183
            // variables
184
            $owner = $this->owner;
185
186
            if ($owner->Title) {
187
188
                // title parts, begin with name/title
189
                $titles = array($owner->Title);
190
191
                // tagline
192
                if ($owner->Tagline) {
193
                    array_push($titles, $owner->FetchTaglineSeparator());
194
                    array_push($titles, $owner->Tagline);
195
                }
196
197
                // page title
198
                if ($owner->TitleOrder == 'first') {
199
                    // add to the beginning
200
                    array_unshift($titles, $owner->FetchTitleSeparator());
201
                    array_unshift($titles, $pageTitle);
202
                } else {
203
                    // add to the end
204
                    array_push($titles, $owner->FetchTitleSeparator());
205
                    array_push($titles, $pageTitle);
206
207
                }
208
209
                // implode to create title
210
                $title = implode(' ', $titles);
211
212
                // @todo remove whitespace before certain characters e.g. `,` `.` `;` `:`
213
                //
214
                $title = preg_replace('/\s*[,.:]/', '', $title);
215
216
                // return
217
                return $title;
218
219
            } else {
220
221
                // just return the page title if there is no name
222
                return $pageTitle;
223
224
            }
225
226
        }
227
    }
228
}
229