1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
<?php /** * WPSEO plugin file. * * @package WPSEO\Frontend\Schema */
/** * Context variables for Schema generation. * * @property string $canonical The current page's canonical. * @property string $company_name Holds the company name, if the site represents a company. * @property int $company_logo_id Holds the company logo's ID, if the site represents a company. * @property int $id The post ID, if there is one. * @property string $site_name The site's name. * @property string $site_represents Whether this site represents a `company` or a `person`. * @property string $site_url The site's URL. * @property int $site_user_id The site's user ID if a site represents a `person`. * @property string $title Page title. * @property string $description Page description. * @property bool $breadcrumbs_enabled Whether or not this site has breadcrumbs enabled. * @property array $site_represents_reference A schema @id reference to the piece the site represents. * @property bool $has_image A boolean that determines whether the current URL has a primary image. * * @since 10.2 */ class WPSEO_Schema_Context {
/** * The current page's canonical. * * @var string */ public $canonical;
/** * Holds the company name, if the site represents a company. * * @var string */ public $company_name;
/** * Holds the company logo's ID, if the site represents a company. * * @var int */ public $company_logo_id;
/** * The queried object ID, if there is one. * * @var int */ public $id;
/** * Whether this site represents a `company` or a `person`. * * @var string */ public $site_represents;
/** * The site's Name. * * @var string */ public $site_name;
/** * The site's URL. * * @var string */ public $site_url;
/** * Page title. * * @var string */ public $title;
/** * User ID when the site represents a Person. * * @var int */ public $site_user_id;
/** * Page description. * * @var string */ public $description;
/** * Whether or not this site has breadcrumbs enabled. * * @var bool */ public $breadcrumbs_enabled;
/** * A schema @id reference to the piece the site represents. * * @var array */ public $site_represents_reference;
/** * A boolean that determines whether the current URL has a primary image. * * @var bool */ public $has_image = false;
/** * WPSEO_Schema_Context constructor. */ public function __construct() { $this->build_data(); }
/** * Builds all the required data for the context object. */ private function build_data() { // Page level variables. $front = WPSEO_Frontend::get_instance(); $this->canonical = $front->canonical( false, false, true ); $this->title = $front->title( '' ); $this->description = $front->metadesc( false ); $this->id = get_queried_object_id();
// Site level variables. $this->site_name = $this->set_site_name(); $this->site_url = trailingslashit( WPSEO_Utils::home_url() );
$this->set_breadcrumbs_variables(); $this->set_site_represents_variables(); $this->set_site_represents_reference(); }
/** * Retrieves the site's name from settings. * * @return string */ private function set_site_name() { if ( '' !== WPSEO_Options::get( 'website_name', '' ) ) { return WPSEO_Options::get( 'website_name' ); }
return get_bloginfo( 'name' ); }
/** * Sets our site represents reference for easy use. */ private function set_site_represents_reference() { $this->site_represents_reference = false;
if ( $this->site_represents === 'person' ) { $this->site_represents_reference = array( '@id' => WPSEO_Schema_Utils::get_user_schema_id( $this->site_user_id, $this ) ); }
if ( $this->site_represents === 'company' ) { $this->site_represents_reference = array( '@id' => $this->site_url . WPSEO_Schema_IDs::ORGANIZATION_HASH ); } }
/** * Determines what our site represents, and grabs their values. */ private function set_site_represents_variables() { $this->site_represents = WPSEO_Options::get( 'company_or_person', false );
switch ( $this->site_represents ) { case 'company': $this->company_name = WPSEO_Options::get( 'company_name' ); // Do not use a non-named company. if ( empty( $this->company_name ) ) { $this->site_represents = false; break; }
$this->company_logo_id = WPSEO_Image_Utils::get_attachment_id_from_settings( 'company_logo' );
/* * Do not use a company without a logo. * This is not a false check due to how `get_attachment_id_from_settings` works. */ if ( $this->company_logo_id < 1 ) { $this->site_represents = false; } break; case 'person': $this->site_user_id = WPSEO_Options::get( 'company_or_person_user_id', false ); // Do not use a non-existing user. if ( $this->site_user_id !== false && get_user_by( 'id', $this->site_user_id ) === false ) { $this->site_represents = false; } break; } }
/** * Determines whether the site uses Yoast SEO breadcrumbs. */ private function set_breadcrumbs_variables() { $this->breadcrumbs_enabled = current_theme_supports( 'yoast-seo-breadcrumbs' ); if ( ! $this->breadcrumbs_enabled ) { $this->breadcrumbs_enabled = WPSEO_Options::get( 'breadcrumbs-enable', false ); } } }
|