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) 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 | public const META_BUNDLE_TYPE = 'campaign'; |
||
48 | public const ELEMENT_CLASSES = [ |
||
49 | CampaignElement::class, |
||
50 | ]; |
||
51 | public const REQUIRED_PLUGIN_HANDLE = 'campaign'; |
||
52 | public 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 | // Handler: Entry::EVENT_DEFINE_SIDEBAR_HTML |
||
183 | Event::on( |
||
184 | CampaignElement::class, |
||
185 | CampaignElement::EVENT_DEFINE_SIDEBAR_HTML, |
||
186 | static function(DefineHtmlEvent $event) { |
||
187 | Craft::debug( |
||
188 | 'CampaignElement::EVENT_DEFINE_SIDEBAR_HTML', |
||
189 | __METHOD__ |
||
190 | ); |
||
191 | $html = ''; |
||
192 | Seomatic::$view->registerAssetBundle(SeomaticAsset::class); |
||
193 | /** @var CampaignElement $campaign */ |
||
194 | $campaign = $event->sender ?? null; |
||
195 | if ($campaign !== null && $campaign->uri !== null) { |
||
196 | Seomatic::$plugin->metaContainers->previewMetaContainers($campaign->uri, $campaign->siteId, true); |
||
197 | // Render our preview sidebar template |
||
198 | if (Seomatic::$settings->displayPreviewSidebar && Seomatic::$matchedElement) { |
||
199 | $html .= PluginTemplate::renderPluginTemplate('_sidebars/campaign-preview.twig'); |
||
200 | } |
||
201 | // Render our analysis sidebar template |
||
202 | // @TODO: This will be added an upcoming 'pro' edition |
||
203 | // if (Seomatic::$settings->displayAnalysisSidebar && Seomatic::$matchedElement) { |
||
204 | // $html .= PluginTemplate::renderPluginTemplate('_sidebars/campaign-analysis.twig'); |
||
205 | // } |
||
206 | } |
||
207 | $event->html .= $html; |
||
208 | } |
||
209 | ); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Return an ElementQuery for the sitemap elements for the given MetaBundle |
||
214 | * |
||
215 | * @param MetaBundle $metaBundle |
||
216 | * |
||
217 | * @return ElementQueryInterface |
||
218 | */ |
||
219 | public static function sitemapElementsQuery(MetaBundle $metaBundle): ElementQueryInterface |
||
220 | { |
||
221 | $query = CampaignElement::find() |
||
222 | ->campaignType($metaBundle->sourceHandle) |
||
223 | ->siteId($metaBundle->sourceSiteId) |
||
224 | ->limit($metaBundle->metaSitemapVars->sitemapLimit); |
||
225 | |||
226 | return $query; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Return an ElementInterface for the sitemap alt element for the given MetaBundle |
||
231 | * and Element ID |
||
232 | * |
||
233 | * @param MetaBundle $metaBundle |
||
234 | * @param int $elementId |
||
235 | * @param int $siteId |
||
236 | * |
||
237 | * @return null|ElementInterface |
||
238 | */ |
||
239 | public static function sitemapAltElement( |
||
240 | MetaBundle $metaBundle, |
||
241 | int $elementId, |
||
242 | int $siteId, |
||
243 | ) |
||
244 | { |
||
245 | return CampaignElement::find() |
||
246 | ->campaignType($metaBundle->sourceHandle) |
||
247 | ->id($elementId) |
||
248 | ->siteId($siteId) |
||
249 | ->limit(1) |
||
250 | ->one(); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Return a preview URI for a given $sourceHandle and $siteId |
||
255 | * This just returns the first element |
||
256 | * |
||
257 | * @param string $sourceHandle |
||
258 | * @param int|null $siteId |
||
259 | * |
||
260 | * @return string|null |
||
261 | */ |
||
262 | public static function previewUri(string $sourceHandle, $siteId) |
||
263 | { |
||
264 | $uri = null; |
||
265 | $element = CampaignElement::find() |
||
266 | ->campaignType($sourceHandle) |
||
267 | ->siteId($siteId) |
||
268 | ->one(); |
||
269 | if ($element) { |
||
270 | $uri = $element->uri; |
||
271 | } |
||
272 | |||
273 | return $uri; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Return an array of FieldLayouts from the $sourceHandle |
||
278 | * |
||
279 | * @param string $sourceHandle |
||
280 | * |
||
281 | * @return array |
||
282 | */ |
||
283 | public static function fieldLayouts(string $sourceHandle): array |
||
284 | { |
||
285 | $layouts = []; |
||
286 | try { |
||
287 | $campaignType = Campaign::$plugin->campaignTypes->getCampaignTypeByHandle($sourceHandle); |
||
288 | if ($campaignType) { |
||
289 | $layouts[] = $campaignType->getFieldLayout(); |
||
290 | } |
||
291 | } catch (Exception $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
292 | } |
||
293 | |||
294 | return $layouts; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Return the (entry) type menu as a $id => $name associative array |
||
299 | * |
||
300 | * @param string $sourceHandle |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | public static function typeMenuFromHandle(string $sourceHandle): array |
||
305 | { |
||
306 | return []; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Return the source model of the given $sourceId |
||
311 | * |
||
312 | * @param int $sourceId |
||
313 | * |
||
314 | * @return CampaignTypeModel|null |
||
315 | */ |
||
316 | public static function sourceModelFromId(int $sourceId) |
||
317 | { |
||
318 | // Attach a behavior to implement ::getSiteSettings() which the CampaignTypeModel lacks |
||
319 | $sourceModel = Campaign::$plugin->campaignTypes->getCampaignTypeById($sourceId); |
||
320 | if ($sourceModel) { |
||
321 | $sourceModel->attachBehavior('SEOmaticCampaignBehavior', CampaignBehavior::class); |
||
322 | } |
||
323 | |||
324 | return $sourceModel; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Return the source model of the given $sourceId |
||
329 | * |
||
330 | * @param string $sourceHandle |
||
331 | * |
||
332 | * @return CampaignTypeModel|null |
||
333 | */ |
||
334 | public static function sourceModelFromHandle(string $sourceHandle) |
||
335 | { |
||
336 | // Attach a behavior to implement ::getSiteSettings() which the CampaignTypeModel lacks |
||
337 | $sourceModel = Campaign::$plugin->campaignTypes->getCampaignTypeByHandle($sourceHandle); |
||
338 | if ($sourceModel) { |
||
339 | $sourceModel->attachBehavior('SEOmaticCampaignBehavior', CampaignBehavior::class); |
||
340 | } |
||
341 | |||
342 | return $sourceModel; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Return the most recently updated Element from a given source model |
||
347 | * |
||
348 | * @param Model $sourceModel |
||
349 | * @param int $sourceSiteId |
||
350 | * |
||
351 | * @return null|ElementInterface |
||
352 | */ |
||
353 | public static function mostRecentElement(Model $sourceModel, int $sourceSiteId) |
||
354 | { |
||
355 | /** @var CampaignTypeModel $sourceModel */ |
||
356 | return CampaignElement::find() |
||
357 | ->campaignType($sourceModel->handle) |
||
358 | ->siteId($sourceSiteId) |
||
359 | ->limit(1) |
||
360 | ->orderBy(['elements.dateUpdated' => SORT_DESC]) |
||
361 | ->one(); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Return the path to the config file directory |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | public static function configFilePath(): string |
||
370 | { |
||
371 | return self::CONFIG_FILE_PATH; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Return a meta bundle config array for the given $sourceModel |
||
376 | * |
||
377 | * @param Model $sourceModel |
||
378 | * |
||
379 | * @return array |
||
380 | */ |
||
381 | public static function metaBundleConfig(Model $sourceModel): array |
||
382 | { |
||
383 | /** @var CampaignTypeModel $sourceModel */ |
||
384 | return ArrayHelper::merge( |
||
385 | ConfigHelper::getConfigFromFile(self::configFilePath()), |
||
386 | [ |
||
387 | 'sourceId' => $sourceModel->id, |
||
388 | 'sourceName' => (string)$sourceModel->name, |
||
389 | 'sourceHandle' => $sourceModel->handle, |
||
390 | ] |
||
391 | ); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Return the source id from the $element |
||
396 | * |
||
397 | * @param ElementInterface $element |
||
398 | * |
||
399 | * @return int|null |
||
400 | */ |
||
401 | public static function sourceIdFromElement(ElementInterface $element) |
||
402 | { |
||
403 | /** @var CampaignElement $element */ |
||
404 | return $element->campaignTypeId; |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * Return the (entry) type id from the $element |
||
409 | * |
||
410 | * @param ElementInterface $element |
||
411 | * |
||
412 | * @return int|null |
||
413 | */ |
||
414 | public static function typeIdFromElement(ElementInterface $element) |
||
415 | { |
||
416 | return null; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Return the source handle from the $element |
||
421 | * |
||
422 | * @param ElementInterface $element |
||
423 | * |
||
424 | * @return string|null |
||
425 | */ |
||
426 | public static function sourceHandleFromElement(ElementInterface $element) |
||
427 | { |
||
428 | $sourceHandle = ''; |
||
429 | /** @var CampaignElement $element */ |
||
430 | try { |
||
431 | $sourceHandle = $element->getCampaignType()->handle; |
||
432 | } catch (InvalidConfigException $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||
433 | } |
||
434 | |||
435 | return $sourceHandle; |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Create a MetaBundle in the db for each site, from the passed in $sourceModel |
||
440 | * |
||
441 | * @param Model $sourceModel |
||
442 | */ |
||
443 | public static function createContentMetaBundle(Model $sourceModel) |
||
444 | { |
||
445 | /** @var CampaignTypeModel $sourceModel */ |
||
446 | $sites = Craft::$app->getSites()->getAllSites(); |
||
447 | /** @var Site $site */ |
||
448 | foreach ($sites as $site) { |
||
449 | $seoElement = self::class; |
||
450 | /** @var SeoElementInterface $seoElement */ |
||
451 | Seomatic::$plugin->metaBundles->createMetaBundleFromSeoElement($seoElement, $sourceModel, $site->id); |
||
452 | } |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Create all the MetaBundles in the db for this Seo Element |
||
457 | */ |
||
458 | public static function createAllContentMetaBundles() |
||
459 | { |
||
460 | // Get all of the campaign types with URLs |
||
461 | $campaignTypes = Campaign::$plugin->campaignTypes->getAllCampaignTypes(); |
||
462 | foreach ($campaignTypes as $campaignType) { |
||
463 | self::createContentMetaBundle($campaignType); |
||
464 | } |
||
465 | } |
||
466 | } |
||
467 |