1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SEOmatic plugin for Craft CMS 3.x |
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) 2019 nystudio107 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace nystudio107\seomatic\seoelements; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\base\ElementInterface; |
16
|
|
|
use craft\base\Model; |
17
|
|
|
use craft\elements\db\ElementQueryInterface; |
18
|
|
|
use craft\events\DefineHtmlEvent; |
19
|
|
|
use craft\models\Site; |
20
|
|
|
use Exception; |
21
|
|
|
use nystudio107\seomatic\assetbundles\seomatic\SeomaticAsset; |
22
|
|
|
use nystudio107\seomatic\base\SeoElementInterface; |
23
|
|
|
use nystudio107\seomatic\helpers\ArrayHelper; |
24
|
|
|
use nystudio107\seomatic\helpers\Config as ConfigHelper; |
25
|
|
|
use nystudio107\seomatic\helpers\PluginTemplate; |
26
|
|
|
use nystudio107\seomatic\integrations\campaign\behaviors\CampaignBehavior; |
27
|
|
|
use nystudio107\seomatic\models\MetaBundle; |
28
|
|
|
use nystudio107\seomatic\Seomatic; |
29
|
|
|
use putyourlightson\campaign\Campaign; |
30
|
|
|
use putyourlightson\campaign\elements\CampaignElement; |
31
|
|
|
use putyourlightson\campaign\events\CampaignTypeEvent; |
32
|
|
|
use putyourlightson\campaign\models\CampaignTypeModel; |
33
|
|
|
use putyourlightson\campaign\services\CampaignTypesService; |
34
|
|
|
use yii\base\Event; |
35
|
|
|
use yii\base\InvalidConfigException; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @author nystudio107 |
39
|
|
|
* @package Seomatic |
40
|
|
|
* @since 3.2.0 |
41
|
|
|
*/ |
42
|
|
|
class SeoCampaign implements SeoElementInterface |
43
|
|
|
{ |
44
|
|
|
// Constants |
45
|
|
|
// ========================================================================= |
46
|
|
|
|
47
|
|
|
const META_BUNDLE_TYPE = 'campaign'; |
48
|
|
|
const ELEMENT_CLASSES = [ |
49
|
|
|
CampaignElement::class, |
50
|
|
|
]; |
51
|
|
|
const REQUIRED_PLUGIN_HANDLE = 'campaign'; |
52
|
|
|
const CONFIG_FILE_PATH = 'campaignmeta/Bundle'; |
53
|
|
|
|
54
|
|
|
// Public Static Methods |
55
|
|
|
// ========================================================================= |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return the sourceBundleType for that this SeoElement handles |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public static function getMetaBundleType(): string |
63
|
|
|
{ |
64
|
|
|
return self::META_BUNDLE_TYPE; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns an array of the element classes that are handled by this SeoElement |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public static function getElementClasses(): array |
73
|
|
|
{ |
74
|
|
|
return self::ELEMENT_CLASSES; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return the refHandle (e.g.: `entry` or `category`) for the SeoElement |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public static function getElementRefHandle(): string |
83
|
|
|
{ |
84
|
|
|
return CampaignElement::refHandle() ?? 'campaign'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return the handle to a required plugin for this SeoElement type |
89
|
|
|
* |
90
|
|
|
* @return null|string |
91
|
|
|
*/ |
92
|
|
|
public static function getRequiredPluginHandle() |
93
|
|
|
{ |
94
|
|
|
return self::REQUIRED_PLUGIN_HANDLE; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Install any event handlers for this SeoElement type |
99
|
|
|
*/ |
100
|
|
|
public static function installEventHandlers() |
101
|
|
|
{ |
102
|
|
|
$request = Craft::$app->getRequest(); |
103
|
|
|
|
104
|
|
|
// Install for all requests |
105
|
|
|
Event::on( |
106
|
|
|
CampaignTypesService::class, |
107
|
|
|
CampaignTypesService::EVENT_AFTER_SAVE_CAMPAIGN_TYPE, |
108
|
|
|
function(CampaignTypeEvent $event) { |
|
|
|
|
109
|
|
|
Craft::debug( |
110
|
|
|
'CampaignTypesService::EVENT_AFTER_SAVE_CAMPAIGN_TYPE', |
111
|
|
|
__METHOD__ |
112
|
|
|
); |
113
|
|
|
Seomatic::$plugin->metaBundles->resaveMetaBundles(self::META_BUNDLE_TYPE); |
114
|
|
|
} |
115
|
|
|
); |
116
|
|
|
Event::on( |
117
|
|
|
CampaignTypesService::class, |
118
|
|
|
CampaignTypesService::EVENT_AFTER_DELETE_CAMPAIGN_TYPE, |
119
|
|
|
function(CampaignTypeEvent $event) { |
|
|
|
|
120
|
|
|
Craft::debug( |
121
|
|
|
'CampaignTypesService::EVENT_AFTER_DELETE_CAMPAIGN_TYPE', |
122
|
|
|
__METHOD__ |
123
|
|
|
); |
124
|
|
|
Seomatic::$plugin->metaBundles->resaveMetaBundles(self::META_BUNDLE_TYPE); |
125
|
|
|
} |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
// Install for all non-console requests |
129
|
|
|
if (!$request->getIsConsoleRequest()) { |
130
|
|
|
// Handler: CampaignTypesService::EVENT_AFTER_SAVE_CAMPAIGN_TYPE |
131
|
|
|
Event::on( |
132
|
|
|
CampaignTypesService::class, |
133
|
|
|
CampaignTypesService::EVENT_AFTER_SAVE_CAMPAIGN_TYPE, |
134
|
|
|
function(CampaignTypeEvent $event) { |
135
|
|
|
Craft::debug( |
136
|
|
|
'CampaignTypesService::EVENT_AFTER_SAVE_CAMPAIGN_TYPE', |
137
|
|
|
__METHOD__ |
138
|
|
|
); |
139
|
|
|
if ($event->campaignType !== null && $event->campaignType->id !== null) { |
140
|
|
|
Seomatic::$plugin->metaBundles->invalidateMetaBundleById( |
141
|
|
|
SeoCampaign::getMetaBundleType(), |
142
|
|
|
$event->campaignType->id, |
143
|
|
|
$event->isNew |
144
|
|
|
); |
145
|
|
|
// Create the meta bundles for this campaign type if it's new |
146
|
|
|
if ($event->isNew) { |
147
|
|
|
SeoCampaign::createContentMetaBundle($event->campaignType); |
148
|
|
|
Seomatic::$plugin->sitemaps->submitSitemapIndex(); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
); |
153
|
|
|
// Handler: CampaignTypesService::EVENT_AFTER_DELETE_CAMPAIGN_TYPE |
154
|
|
|
Event::on( |
155
|
|
|
CampaignTypesService::class, |
156
|
|
|
CampaignTypesService::EVENT_AFTER_DELETE_CAMPAIGN_TYPE, |
157
|
|
|
function(CampaignTypeEvent $event) { |
158
|
|
|
Craft::debug( |
159
|
|
|
'CampaignTypesService::EVENT_AFTER_DELETE_CAMPAIGN_TYPE', |
160
|
|
|
__METHOD__ |
161
|
|
|
); |
162
|
|
|
if ($event->campaignType !== null && $event->campaignType->id !== null) { |
163
|
|
|
Seomatic::$plugin->metaBundles->invalidateMetaBundleById( |
164
|
|
|
SeoCampaign::getMetaBundleType(), |
165
|
|
|
$event->campaignType->id, |
166
|
|
|
false |
167
|
|
|
); |
168
|
|
|
// Delete the meta bundles for this campaign type |
169
|
|
|
Seomatic::$plugin->metaBundles->deleteMetaBundleBySourceId( |
170
|
|
|
SeoCampaign::getMetaBundleType(), |
171
|
|
|
$event->campaignType->id |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// Install only for non-console site requests |
179
|
|
|
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (Seomatic::$craft37) { |
183
|
|
|
// Handler: Entry::EVENT_DEFINE_SIDEBAR_HTML |
184
|
|
|
Event::on( |
185
|
|
|
CampaignElement::class, |
186
|
|
|
CampaignElement::EVENT_DEFINE_SIDEBAR_HTML, |
187
|
|
|
static function(DefineHtmlEvent $event) { |
188
|
|
|
Craft::debug( |
189
|
|
|
'CampaignElement::EVENT_DEFINE_SIDEBAR_HTML', |
190
|
|
|
__METHOD__ |
191
|
|
|
); |
192
|
|
|
$html = ''; |
193
|
|
|
Seomatic::$view->registerAssetBundle(SeomaticAsset::class); |
194
|
|
|
/** @var CampaignElement $campaign */ |
195
|
|
|
$campaign = $event->sender ?? null; |
196
|
|
|
if ($campaign !== null && $campaign->uri !== null) { |
197
|
|
|
Seomatic::$plugin->metaContainers->previewMetaContainers($campaign->uri, $campaign->siteId, true); |
198
|
|
|
// Render our preview sidebar template |
199
|
|
|
if (Seomatic::$settings->displayPreviewSidebar && Seomatic::$matchedElement) { |
200
|
|
|
$html .= PluginTemplate::renderPluginTemplate('_sidebars/campaign-preview.twig'); |
201
|
|
|
} |
202
|
|
|
// Render our analysis sidebar template |
203
|
|
|
// @TODO: This will be added an upcoming 'pro' edition |
204
|
|
|
// if (Seomatic::$settings->displayAnalysisSidebar && Seomatic::$matchedElement) { |
205
|
|
|
// $html .= PluginTemplate::renderPluginTemplate('_sidebars/campaign-analysis.twig'); |
206
|
|
|
// } |
207
|
|
|
} |
208
|
|
|
$event->html .= $html; |
209
|
|
|
} |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Return an ElementQuery for the sitemap elements for the given MetaBundle |
216
|
|
|
* |
217
|
|
|
* @param MetaBundle $metaBundle |
218
|
|
|
* |
219
|
|
|
* @return ElementQueryInterface |
220
|
|
|
*/ |
221
|
|
|
public static function sitemapElementsQuery(MetaBundle $metaBundle): ElementQueryInterface |
222
|
|
|
{ |
223
|
|
|
$query = CampaignElement::find() |
224
|
|
|
->campaignType($metaBundle->sourceHandle) |
225
|
|
|
->siteId($metaBundle->sourceSiteId) |
226
|
|
|
->limit($metaBundle->metaSitemapVars->sitemapLimit); |
227
|
|
|
|
228
|
|
|
return $query; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Return an ElementInterface for the sitemap alt element for the given MetaBundle |
233
|
|
|
* and Element ID |
234
|
|
|
* |
235
|
|
|
* @param MetaBundle $metaBundle |
236
|
|
|
* @param int $elementId |
237
|
|
|
* @param int $siteId |
238
|
|
|
* |
239
|
|
|
* @return null|ElementInterface |
240
|
|
|
*/ |
241
|
|
|
public static function sitemapAltElement( |
242
|
|
|
MetaBundle $metaBundle, |
243
|
|
|
int $elementId, |
244
|
|
|
int $siteId |
245
|
|
|
) { |
246
|
|
|
return CampaignElement::find() |
|
|
|
|
247
|
|
|
->campaignType($metaBundle->sourceHandle) |
248
|
|
|
->id($elementId) |
249
|
|
|
->siteId($siteId) |
250
|
|
|
->limit(1) |
251
|
|
|
->one(); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Return a preview URI for a given $sourceHandle and $siteId |
256
|
|
|
* This just returns the first element |
257
|
|
|
* |
258
|
|
|
* @param string $sourceHandle |
259
|
|
|
* @param int|null $siteId |
260
|
|
|
* @param int|string|null $typeId |
261
|
|
|
* |
262
|
|
|
* @return ?string |
263
|
|
|
*/ |
264
|
|
|
public static function previewUri(string $sourceHandle, $siteId, $typeId = null) |
265
|
|
|
{ |
266
|
|
|
$uri = null; |
267
|
|
|
$element = CampaignElement::find() |
268
|
|
|
->campaignType($sourceHandle) |
269
|
|
|
->siteId($siteId) |
270
|
|
|
->one(); |
271
|
|
|
if ($element) { |
272
|
|
|
$uri = $element->uri; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $uri; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Return an array of FieldLayouts from the $sourceHandle |
280
|
|
|
* |
281
|
|
|
* @param string $sourceHandle |
282
|
|
|
* @param int|string|null $typeId |
283
|
|
|
* |
284
|
|
|
* @return array |
285
|
|
|
*/ |
286
|
|
|
public static function fieldLayouts(string $sourceHandle, $typeId = null): array |
287
|
|
|
{ |
288
|
|
|
$layouts = []; |
289
|
|
|
try { |
290
|
|
|
$campaignType = Campaign::$plugin->campaignTypes->getCampaignTypeByHandle($sourceHandle); |
291
|
|
|
if ($campaignType) { |
292
|
|
|
$layouts[] = $campaignType->getFieldLayout(); |
293
|
|
|
} |
294
|
|
|
} catch (Exception $e) { |
|
|
|
|
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return $layouts; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Return the (entry) type menu as a $id => $name associative array |
302
|
|
|
* |
303
|
|
|
* @param string $sourceHandle |
304
|
|
|
* |
305
|
|
|
* @return array |
306
|
|
|
*/ |
307
|
|
|
public static function typeMenuFromHandle(string $sourceHandle): array |
308
|
|
|
{ |
309
|
|
|
return []; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Return the source model of the given $sourceId |
314
|
|
|
* |
315
|
|
|
* @param int $sourceId |
316
|
|
|
* |
317
|
|
|
* @return CampaignTypeModel|null |
318
|
|
|
*/ |
319
|
|
|
public static function sourceModelFromId(int $sourceId) |
320
|
|
|
{ |
321
|
|
|
// Attach a behavior to implement ::getSiteSettings() which the CampaignTypeModel lacks |
322
|
|
|
$sourceModel = Campaign::$plugin->campaignTypes->getCampaignTypeById($sourceId); |
323
|
|
|
if ($sourceModel) { |
324
|
|
|
$sourceModel->attachBehavior('SEOmaticCampaignBehavior', CampaignBehavior::class); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
return $sourceModel; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Return the source model of the given $sourceId |
332
|
|
|
* |
333
|
|
|
* @param string $sourceHandle |
334
|
|
|
* |
335
|
|
|
* @return CampaignTypeModel|null |
336
|
|
|
*/ |
337
|
|
|
public static function sourceModelFromHandle(string $sourceHandle) |
338
|
|
|
{ |
339
|
|
|
// Attach a behavior to implement ::getSiteSettings() which the CampaignTypeModel lacks |
340
|
|
|
$sourceModel = Campaign::$plugin->campaignTypes->getCampaignTypeByHandle($sourceHandle); |
341
|
|
|
if ($sourceModel) { |
342
|
|
|
$sourceModel->attachBehavior('SEOmaticCampaignBehavior', CampaignBehavior::class); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
return $sourceModel; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Return the most recently updated Element from a given source model |
350
|
|
|
* |
351
|
|
|
* @param Model $sourceModel |
352
|
|
|
* @param int $sourceSiteId |
353
|
|
|
* |
354
|
|
|
* @return null|ElementInterface |
355
|
|
|
*/ |
356
|
|
|
public static function mostRecentElement(Model $sourceModel, int $sourceSiteId) |
357
|
|
|
{ |
358
|
|
|
/** @var CampaignTypeModel $sourceModel */ |
359
|
|
|
return CampaignElement::find() |
|
|
|
|
360
|
|
|
->campaignType($sourceModel->handle) |
361
|
|
|
->siteId($sourceSiteId) |
362
|
|
|
->limit(1) |
363
|
|
|
->orderBy(['elements.dateUpdated' => SORT_DESC]) |
364
|
|
|
->one(); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Return the path to the config file directory |
369
|
|
|
* |
370
|
|
|
* @return string |
371
|
|
|
*/ |
372
|
|
|
public static function configFilePath(): string |
373
|
|
|
{ |
374
|
|
|
return self::CONFIG_FILE_PATH; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Return a meta bundle config array for the given $sourceModel |
379
|
|
|
* |
380
|
|
|
* @param Model $sourceModel |
381
|
|
|
* |
382
|
|
|
* @return array |
383
|
|
|
*/ |
384
|
|
|
public static function metaBundleConfig(Model $sourceModel): array |
385
|
|
|
{ |
386
|
|
|
/** @var CampaignTypeModel $sourceModel */ |
387
|
|
|
return ArrayHelper::merge( |
388
|
|
|
ConfigHelper::getConfigFromFile(self::configFilePath()), |
389
|
|
|
[ |
390
|
|
|
'sourceId' => $sourceModel->id, |
391
|
|
|
'sourceName' => (string)$sourceModel->name, |
392
|
|
|
'sourceHandle' => $sourceModel->handle, |
393
|
|
|
] |
394
|
|
|
); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Return the source id from the $element |
399
|
|
|
* |
400
|
|
|
* @param ElementInterface $element |
401
|
|
|
* |
402
|
|
|
* @return int|null |
403
|
|
|
*/ |
404
|
|
|
public static function sourceIdFromElement(ElementInterface $element) |
405
|
|
|
{ |
406
|
|
|
/** @var CampaignElement $element */ |
407
|
|
|
return $element->campaignTypeId; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Return the (entry) type id from the $element |
412
|
|
|
* |
413
|
|
|
* @param ElementInterface $element |
414
|
|
|
* |
415
|
|
|
* @return int|null |
416
|
|
|
*/ |
417
|
|
|
public static function typeIdFromElement(ElementInterface $element) |
418
|
|
|
{ |
419
|
|
|
return null; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Return the source handle from the $element |
424
|
|
|
* |
425
|
|
|
* @param ElementInterface $element |
426
|
|
|
* |
427
|
|
|
* @return string|null |
428
|
|
|
*/ |
429
|
|
|
public static function sourceHandleFromElement(ElementInterface $element) |
430
|
|
|
{ |
431
|
|
|
$sourceHandle = ''; |
|
|
|
|
432
|
|
|
/** @var CampaignElement $element */ |
433
|
|
|
try { |
434
|
|
|
$sourceHandle = $element->getCampaignType()->handle; |
435
|
|
|
} catch (InvalidConfigException $e) { |
|
|
|
|
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
return $sourceHandle; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Create a MetaBundle in the db for each site, from the passed in $sourceModel |
443
|
|
|
* |
444
|
|
|
* @param Model $sourceModel |
445
|
|
|
*/ |
446
|
|
|
public static function createContentMetaBundle(Model $sourceModel) |
447
|
|
|
{ |
448
|
|
|
/** @var CampaignTypeModel $sourceModel */ |
449
|
|
|
$sourceModel->attachBehavior('SEOmaticCampaignBehavior', CampaignBehavior::class); |
450
|
|
|
$sites = Craft::$app->getSites()->getAllSites(); |
451
|
|
|
/** @var Site $site */ |
452
|
|
|
foreach ($sites as $site) { |
453
|
|
|
$seoElement = self::class; |
454
|
|
|
Seomatic::$plugin->metaBundles->createMetaBundleFromSeoElement($seoElement, $sourceModel, $site->id, null, true); |
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Create all the MetaBundles in the db for this Seo Element |
460
|
|
|
*/ |
461
|
|
|
public static function createAllContentMetaBundles() |
462
|
|
|
{ |
463
|
|
|
// Get all of the campaign types with URLs |
464
|
|
|
$campaignTypes = Campaign::$plugin->campaignTypes->getAllCampaignTypes(); |
465
|
|
|
foreach ($campaignTypes as $campaignType) { |
466
|
|
|
self::createContentMetaBundle($campaignType); |
467
|
|
|
} |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.