Total Complexity | 136 |
Total Lines | 1368 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 3 | Features | 0 |
Complex classes like SettingsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SettingsController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class SettingsController extends Controller |
||
50 | { |
||
51 | // Constants |
||
52 | // ========================================================================= |
||
53 | |||
54 | public const DOCUMENTATION_URL = 'https://github.com/nystudio107/craft-seomatic'; |
||
55 | |||
56 | public const SETUP_GRADES = [ |
||
57 | ['id' => 'data1', 'name' => 'A', 'color' => '#008002'], |
||
58 | ['id' => 'data2', 'name' => 'B', 'color' => '#9ACD31'], |
||
59 | ['id' => 'data4', 'name' => 'C', 'color' => '#FFA500'], |
||
60 | ['id' => 'data5', 'name' => 'D', 'color' => '#8B0100'], |
||
61 | ]; |
||
62 | |||
63 | public const SEO_SETUP_FIELDS = [ |
||
64 | 'mainEntityOfPage' => 'Main Entity of Page', |
||
65 | 'seoTitle' => 'SEO Title', |
||
66 | 'seoDescription' => 'SEO Description', |
||
67 | 'seoKeywords' => 'SEO Keywords', |
||
68 | 'seoImage' => 'SEO Image', |
||
69 | 'seoImageDescription' => 'SEO Image Description', |
||
70 | ]; |
||
71 | |||
72 | public const SITE_SETUP_FIELDS = [ |
||
73 | 'siteName' => 'Site Name', |
||
74 | 'twitterHandle' => 'Twitter Handle', |
||
75 | 'facebookProfileId' => 'Facebook Profile ID', |
||
76 | ]; |
||
77 | |||
78 | public const IDENTITY_SETUP_FIELDS = [ |
||
79 | 'computedType' => 'Identity Entity Type', |
||
80 | 'genericName' => 'Identity Entity Name', |
||
81 | 'genericDescription' => 'Identity Entity Description', |
||
82 | 'genericUrl' => 'Identity Entity URL', |
||
83 | 'genericImage' => 'Identity Entity Brand', |
||
84 | ]; |
||
85 | |||
86 | // Protected Properties |
||
87 | // ========================================================================= |
||
88 | |||
89 | /** |
||
90 | * @inheritdoc |
||
91 | */ |
||
92 | protected array|bool|int $allowAnonymous = [ |
||
93 | ]; |
||
94 | |||
95 | // Public Methods |
||
96 | // ========================================================================= |
||
97 | |||
98 | /** |
||
99 | * Dashboard display |
||
100 | * |
||
101 | * @param string|null $siteHandle |
||
102 | * @param bool $showWelcome |
||
103 | * |
||
104 | * @return Response The rendered result |
||
105 | * @throws NotFoundHttpException |
||
106 | * @throws ForbiddenHttpException |
||
107 | */ |
||
108 | public function actionDashboard(string $siteHandle = null, bool $showWelcome = false): Response |
||
109 | { |
||
110 | $variables = []; |
||
111 | // Get the site to edit |
||
112 | $siteId = $this->getSiteIdFromHandle($siteHandle); |
||
113 | $pluginName = Seomatic::$settings->pluginName; |
||
114 | $templateTitle = Craft::t('seomatic', 'Dashboard'); |
||
115 | // Asset bundle |
||
116 | try { |
||
117 | Seomatic::$view->registerAssetBundle(SeomaticAsset::class); |
||
|
|||
118 | } catch (InvalidConfigException $e) { |
||
119 | Craft::error($e->getMessage(), __METHOD__); |
||
120 | } |
||
121 | $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
||
122 | '@nystudio107/seomatic/web/assets/dist', |
||
123 | true |
||
124 | ); |
||
125 | // Enabled sites |
||
126 | $this->setMultiSiteVariables($siteHandle, $siteId, $variables); |
||
127 | $variables['controllerHandle'] = 'dashboard'; |
||
128 | |||
129 | // Basic variables |
||
130 | $variables['fullPageForm'] = false; |
||
131 | $variables['docsUrl'] = self::DOCUMENTATION_URL; |
||
132 | $variables['pluginName'] = Seomatic::$settings->pluginName; |
||
133 | $variables['title'] = $templateTitle; |
||
134 | $variables['docTitle'] = "{$pluginName} - {$templateTitle}"; |
||
135 | $siteHandleUri = Craft::$app->isMultiSite ? '/' . $siteHandle : ''; |
||
136 | $variables['crumbs'] = [ |
||
137 | [ |
||
138 | 'label' => $pluginName, |
||
139 | 'url' => UrlHelper::cpUrl('seomatic'), |
||
140 | ], |
||
141 | [ |
||
142 | 'label' => $templateTitle, |
||
143 | 'url' => UrlHelper::cpUrl('seomatic/dashboard' . $siteHandleUri), |
||
144 | ], |
||
145 | ]; |
||
146 | $variables['selectedSubnavItem'] = 'dashboard'; |
||
147 | $variables['showWelcome'] = $showWelcome; |
||
148 | // Calulate the setup grades |
||
149 | $variables['contentSetupStats'] = []; |
||
150 | $variables['setupGrades'] = self::SETUP_GRADES; |
||
151 | $numFields = count(self::SEO_SETUP_FIELDS); |
||
152 | $numGrades = count(self::SETUP_GRADES); |
||
153 | while ($numGrades--) { |
||
154 | $variables['contentSetupStats'][] = 0; |
||
155 | } |
||
156 | $numGrades = count(self::SETUP_GRADES); |
||
157 | // Content SEO grades |
||
158 | $variables['metaBundles'] = Seomatic::$plugin->metaBundles->getContentMetaBundlesForSiteId($siteId); |
||
159 | $variables['contentSetupChecklistCutoff'] = floor(count($variables['metaBundles']) / 2); |
||
160 | $variables['contentSetupChecklist'] = []; |
||
161 | Seomatic::$plugin->metaBundles->pruneVestigialMetaBundles($variables['metaBundles']); |
||
162 | /** @var MetaBundle $metaBundle */ |
||
163 | foreach ($variables['metaBundles'] as $metaBundle) { |
||
164 | $stat = 0; |
||
165 | foreach (self::SEO_SETUP_FIELDS as $setupField => $setupLabel) { |
||
166 | $stat += (int)!empty($metaBundle->metaGlobalVars[$setupField]); |
||
167 | $value = $variables['contentSetupChecklist'][$setupField]['value'] ?? 0; |
||
168 | $variables['contentSetupChecklist'][$setupField] = [ |
||
169 | 'label' => $setupLabel, |
||
170 | 'value' => $value + (int)!empty($metaBundle->metaGlobalVars[$setupField]), |
||
171 | ]; |
||
172 | } |
||
173 | $stat = round($numGrades - (($stat * $numGrades) / $numFields)); |
||
174 | if ($stat >= $numGrades) { |
||
175 | $stat = $numGrades - 1; |
||
176 | } |
||
177 | $variables['contentSetupStats'][$stat]++; |
||
178 | } |
||
179 | // Global SEO grades |
||
180 | Seomatic::$previewingMetaContainers = true; |
||
181 | $metaBundle = Seomatic::$plugin->metaBundles->getGlobalMetaBundle((int)$siteId); |
||
182 | Seomatic::$previewingMetaContainers = false; |
||
183 | if ($metaBundle !== null) { |
||
184 | $stat = 0; |
||
185 | $variables['globalSetupChecklist'] = []; |
||
186 | foreach (self::SEO_SETUP_FIELDS as $setupField => $setupLabel) { |
||
187 | $stat += (int)!empty($metaBundle->metaGlobalVars[$setupField]); |
||
188 | $variables['globalSetupChecklist'][$setupField] = [ |
||
189 | 'label' => $setupLabel, |
||
190 | 'value' => (int)!empty($metaBundle->metaGlobalVars[$setupField]), |
||
191 | ]; |
||
192 | } |
||
193 | $stat = round(($stat / $numFields) * 100); |
||
194 | $variables['globalSetupStat'] = $stat; |
||
195 | // Site Settings grades |
||
196 | $numFields = count(self::SITE_SETUP_FIELDS) + count(self::IDENTITY_SETUP_FIELDS); |
||
197 | $stat = 0; |
||
198 | $variables['siteSetupChecklist'] = []; |
||
199 | foreach (self::SITE_SETUP_FIELDS as $setupField => $setupLabel) { |
||
200 | $stat += (int)!empty($metaBundle->metaSiteVars[$setupField]); |
||
201 | $variables['siteSetupChecklist'][$setupField] = [ |
||
202 | 'label' => $setupLabel, |
||
203 | 'value' => (int)!empty($metaBundle->metaSiteVars[$setupField]), |
||
204 | ]; |
||
205 | } |
||
206 | foreach (self::IDENTITY_SETUP_FIELDS as $setupField => $setupLabel) { |
||
207 | $stat += (int)!empty($metaBundle->metaSiteVars->identity[$setupField]); |
||
208 | $variables['siteSetupChecklist'][$setupField] = [ |
||
209 | 'label' => $setupLabel, |
||
210 | 'value' => (int)!empty($metaBundle->metaSiteVars->identity[$setupField]), |
||
211 | ]; |
||
212 | } |
||
213 | $stat = round(($stat / $numFields) * 100); |
||
214 | $variables['siteSetupStat'] = $stat; |
||
215 | } |
||
216 | $this->setCrumbVariables($variables); |
||
217 | |||
218 | // Render the template |
||
219 | return $this->renderTemplate('seomatic/dashboard/index', $variables); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Global settings |
||
224 | * |
||
225 | * @param string $subSection |
||
226 | * @param string|null $siteHandle |
||
227 | * @param string|null $loadFromSiteHandle |
||
228 | * |
||
229 | * @return Response The rendered result |
||
230 | * @throws NotFoundHttpException |
||
231 | * @throws ForbiddenHttpException |
||
232 | */ |
||
233 | public function actionGlobal(string $subSection = 'general', string $siteHandle = null, $loadFromSiteHandle = null, $editedMetaBundle = null): Response |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * @return Response|null |
||
350 | * @throws BadRequestHttpException |
||
351 | * @throws MissingComponentException |
||
352 | */ |
||
353 | public function actionSaveGlobal() |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Content settings |
||
445 | * |
||
446 | * @param string|null $siteHandle |
||
447 | * |
||
448 | * @return Response The rendered result |
||
449 | * @throws NotFoundHttpException |
||
450 | * @throws ForbiddenHttpException |
||
451 | */ |
||
452 | public function actionContent(string $siteHandle = null): Response |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * Global settings |
||
500 | * |
||
501 | * @param string $subSection |
||
502 | * @param string $sourceBundleType |
||
503 | * @param string $sourceHandle |
||
504 | * @param string|null $siteHandle |
||
505 | * @param string|int|null $typeId |
||
506 | * @param string|null $loadFromSiteHandle |
||
507 | * |
||
508 | * @return Response The rendered result |
||
509 | * @throws NotFoundHttpException |
||
510 | * @throws ForbiddenHttpException |
||
511 | */ |
||
512 | public function actionEditContent( |
||
513 | string $subSection, |
||
514 | string $sourceBundleType, |
||
515 | string $sourceHandle, |
||
516 | string $siteHandle = null, |
||
517 | $typeId = null, |
||
518 | $loadFromSiteHandle = null, |
||
519 | ): Response { |
||
520 | $variables = []; |
||
521 | // @TODO: Let people choose an entry/categorygroup/product as the preview |
||
522 | // Get the site to edit |
||
523 | $siteId = $this->getSiteIdFromHandle($siteHandle); |
||
524 | if (is_string($typeId)) { |
||
525 | $typeId = (int)$typeId; |
||
526 | } |
||
527 | // Get the (entry) type menu |
||
528 | $typeMenu = []; |
||
529 | $seoElement = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType($sourceBundleType); |
||
530 | if ($seoElement !== null) { |
||
531 | $typeMenu = $seoElement::typeMenuFromHandle($sourceHandle); |
||
532 | } |
||
533 | $variables['typeMenu'] = $typeMenu; |
||
534 | $variables['currentTypeId'] = null; |
||
535 | if (!empty($typeMenu)) { |
||
536 | $currentType = reset($typeMenu); |
||
537 | $variables['currentType'] = $typeMenu[$typeId] ?? $currentType; |
||
538 | $variables['currentTypeId'] = $typeId ?? key($typeMenu); |
||
539 | $typeId = (int)$variables['currentTypeId']; |
||
540 | } |
||
541 | $pluginName = Seomatic::$settings->pluginName; |
||
542 | // Asset bundle |
||
543 | try { |
||
544 | Seomatic::$view->registerAssetBundle(SeomaticAsset::class); |
||
545 | } catch (InvalidConfigException $e) { |
||
546 | Craft::error($e->getMessage(), __METHOD__); |
||
547 | } |
||
548 | $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
||
549 | '@nystudio107/seomatic/web/assets/dist', |
||
550 | true |
||
551 | ); |
||
552 | // Enabled sites |
||
553 | $this->setMultiSiteVariables($siteHandle, $siteId, $variables); |
||
554 | $this->cullDisabledSites($sourceBundleType, $sourceHandle, $variables); |
||
555 | // Meta Bundle settings |
||
556 | Seomatic::$previewingMetaContainers = true; |
||
557 | // Get the site to copy the settings from, if any |
||
558 | $variables['loadFromSiteHandle'] = $loadFromSiteHandle; |
||
559 | $loadFromSiteId = $this->getSiteIdFromHandle($loadFromSiteHandle); |
||
560 | $siteIdToLoad = $loadFromSiteHandle === null ? (int)$variables['currentSiteId'] : $loadFromSiteId; |
||
561 | // Load the metabundle |
||
562 | $metaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceHandle( |
||
563 | $sourceBundleType, |
||
564 | $sourceHandle, |
||
565 | $siteIdToLoad, |
||
566 | $typeId |
||
567 | ); |
||
568 | Seomatic::$previewingMetaContainers = false; |
||
569 | $templateTitle = ''; |
||
570 | if ($metaBundle !== null) { |
||
571 | $variables['metaGlobalVars'] = clone $metaBundle->metaGlobalVars; |
||
572 | $variables['metaSitemapVars'] = $metaBundle->metaSitemapVars; |
||
573 | $variables['metaBundleSettings'] = $metaBundle->metaBundleSettings; |
||
574 | $variables['currentSourceHandle'] = $metaBundle->sourceHandle; |
||
575 | $variables['currentSourceBundleType'] = $metaBundle->sourceBundleType; |
||
576 | $templateTitle = $metaBundle->sourceName; |
||
577 | } |
||
578 | // Basic variables |
||
579 | $subSectionTitle = Craft::t('seomatic', ucfirst($subSection)); |
||
580 | $variables['fullPageForm'] = true; |
||
581 | $variables['docsUrl'] = self::DOCUMENTATION_URL; |
||
582 | $variables['pluginName'] = Seomatic::$settings->pluginName; |
||
583 | $variables['title'] = $templateTitle; |
||
584 | $variables['subSectionTitle'] = $subSectionTitle; |
||
585 | $variables['docTitle'] = "{$pluginName} - Content SEO - {$templateTitle} - {$subSectionTitle}"; |
||
586 | $siteHandleUri = Craft::$app->isMultiSite ? '/' . $siteHandle : ''; |
||
587 | $variables['siteHandleUri'] = $siteHandleUri; |
||
588 | $variables['crumbs'] = [ |
||
589 | [ |
||
590 | 'label' => $pluginName, |
||
591 | 'url' => UrlHelper::cpUrl('seomatic'), |
||
592 | ], |
||
593 | [ |
||
594 | 'label' => 'Content SEO', |
||
595 | 'url' => UrlHelper::cpUrl('seomatic/content' . $siteHandleUri), |
||
596 | ], |
||
597 | [ |
||
598 | 'label' => $metaBundle->sourceName . ' · ' . $subSectionTitle, |
||
599 | 'url' => UrlHelper::cpUrl("seomatic/edit-content/{$subSection}/{$sourceBundleType}/{$sourceHandle}"), |
||
600 | ], |
||
601 | ]; |
||
602 | $variables['selectedSubnavItem'] = 'content'; |
||
603 | $variables['controllerHandle'] = "edit-content/{$subSection}/{$sourceBundleType}/{$sourceHandle}"; |
||
604 | $this->setCrumbVariables($variables); |
||
605 | // Image selectors |
||
606 | $variables['currentSubSection'] = $subSection; |
||
607 | $bundleSettings = $metaBundle->metaBundleSettings; |
||
608 | $variables['elementType'] = Asset::class; |
||
609 | $variables['seoImageElements'] = ImageTransformHelper::assetElementsFromIds( |
||
610 | $bundleSettings->seoImageIds, |
||
611 | $siteId |
||
612 | ); |
||
613 | $variables['twitterImageElements'] = ImageTransformHelper::assetElementsFromIds( |
||
614 | $bundleSettings->twitterImageIds, |
||
615 | $siteId |
||
616 | ); |
||
617 | $variables['ogImageElements'] = ImageTransformHelper::assetElementsFromIds( |
||
618 | $bundleSettings->ogImageIds, |
||
619 | $siteId |
||
620 | ); |
||
621 | $variables['sourceType'] = $metaBundle->sourceType; |
||
622 | // Pass in the pull fields |
||
623 | $groupName = ucfirst($metaBundle->sourceType); |
||
624 | $this->setContentFieldSourceVariables($sourceBundleType, $sourceHandle, $groupName, $variables, $typeId); |
||
625 | $uri = $this->uriFromSourceBundle($sourceBundleType, $sourceHandle, $siteId, $typeId); |
||
626 | // Preview the meta containers |
||
627 | Seomatic::$plugin->metaContainers->previewMetaContainers( |
||
628 | $uri, |
||
629 | (int)$variables['currentSiteId'], |
||
630 | false, |
||
631 | false |
||
632 | ); |
||
633 | |||
634 | // Render the template |
||
635 | return $this->renderTemplate('seomatic/settings/content/' . $subSection, $variables); |
||
636 | } |
||
637 | |||
638 | /** |
||
639 | * @return Response |
||
640 | * @throws BadRequestHttpException |
||
641 | * @throws MissingComponentException |
||
642 | */ |
||
643 | public function actionSaveContent(): Response |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * Site settings |
||
699 | * |
||
700 | * @param string $subSection |
||
701 | * @param string|null $siteHandle |
||
702 | * @param string|null $loadFromSiteHandle |
||
703 | * |
||
704 | * @return Response The rendered result |
||
705 | * @throws NotFoundHttpException |
||
706 | * @throws ForbiddenHttpException |
||
707 | */ |
||
708 | public function actionSite(string $subSection = 'identity', string $siteHandle = null, $loadFromSiteHandle = null): Response |
||
709 | { |
||
710 | $variables = []; |
||
711 | // Get the site to edit |
||
712 | $siteId = $this->getSiteIdFromHandle($siteHandle); |
||
713 | |||
714 | $pluginName = Seomatic::$settings->pluginName; |
||
715 | $templateTitle = Craft::t('seomatic', 'Site Settings'); |
||
716 | $subSectionSuffix = ''; |
||
717 | if ($subSection === 'social') { |
||
718 | $subSectionSuffix = ' Media'; |
||
719 | } |
||
720 | $subSectionTitle = Craft::t('seomatic', ucfirst($subSection) . $subSectionSuffix); |
||
721 | // Asset bundle |
||
722 | try { |
||
723 | Seomatic::$view->registerAssetBundle(SeomaticAsset::class); |
||
724 | } catch (InvalidConfigException $e) { |
||
725 | Craft::error($e->getMessage(), __METHOD__); |
||
726 | } |
||
727 | $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
||
728 | '@nystudio107/seomatic/web/assets/dist', |
||
729 | true |
||
730 | ); |
||
731 | // Basic variables |
||
732 | $variables['fullPageForm'] = true; |
||
733 | $variables['docsUrl'] = self::DOCUMENTATION_URL; |
||
734 | $variables['pluginName'] = Seomatic::$settings->pluginName; |
||
735 | $variables['title'] = $templateTitle; |
||
736 | $variables['subSectionTitle'] = $subSectionTitle; |
||
737 | $variables['docTitle'] = "{$pluginName} - {$templateTitle} - {$subSectionTitle}"; |
||
738 | $siteHandleUri = Craft::$app->isMultiSite ? '/' . $siteHandle : ''; |
||
739 | $variables['crumbs'] = [ |
||
740 | [ |
||
741 | 'label' => $pluginName, |
||
742 | 'url' => UrlHelper::cpUrl('seomatic'), |
||
743 | ], |
||
744 | [ |
||
745 | 'label' => $templateTitle, |
||
746 | 'url' => UrlHelper::cpUrl('seomatic/site/identity' . $siteHandleUri), |
||
747 | ], |
||
748 | [ |
||
749 | 'label' => $subSectionTitle, |
||
750 | 'url' => UrlHelper::cpUrl('seomatic/site/' . $subSection . $siteHandleUri), |
||
751 | ], |
||
752 | ]; |
||
753 | $variables['selectedSubnavItem'] = 'site'; |
||
754 | $variables['currentSubSection'] = $subSection; |
||
755 | |||
756 | // Enabled sites |
||
757 | $this->setMultiSiteVariables($siteHandle, $siteId, $variables); |
||
758 | $variables['controllerHandle'] = 'site' . '/' . $subSection; |
||
759 | |||
760 | // The site settings for the appropriate meta bundle |
||
761 | Seomatic::$previewingMetaContainers = true; |
||
762 | // Get the site to copy the settings from, if any |
||
763 | $variables['loadFromSiteHandle'] = $loadFromSiteHandle; |
||
764 | $loadFromSiteId = $this->getSiteIdFromHandle($loadFromSiteHandle); |
||
765 | $siteIdToLoad = $loadFromSiteHandle === null ? (int)$variables['currentSiteId'] : $loadFromSiteId; |
||
766 | // Load the metabundle |
||
767 | $metaBundle = Seomatic::$plugin->metaBundles->getGlobalMetaBundle($siteIdToLoad); |
||
768 | Seomatic::$previewingMetaContainers = false; |
||
769 | if ($metaBundle !== null) { |
||
770 | $variables['site'] = $metaBundle->metaSiteVars; |
||
771 | $variables['identityImageElements'] = ImageTransformHelper::assetElementsFromIds( |
||
772 | $variables['site']->identity->genericImageIds, |
||
773 | $siteId |
||
774 | ); |
||
775 | $variables['creatorImageElements'] = ImageTransformHelper::assetElementsFromIds( |
||
776 | $variables['site']->creator->genericImageIds, |
||
777 | $siteId |
||
778 | ); |
||
779 | } |
||
780 | $variables['elementType'] = Asset::class; |
||
781 | $this->setCrumbVariables($variables); |
||
782 | |||
783 | // Render the template |
||
784 | return $this->renderTemplate('seomatic/settings/site/' . $subSection, $variables); |
||
785 | } |
||
786 | |||
787 | /** |
||
788 | * @return Response |
||
789 | * @throws BadRequestHttpException |
||
790 | * @throws MissingComponentException |
||
791 | */ |
||
792 | public function actionSaveSite(): Response |
||
793 | { |
||
794 | $this->requirePostRequest(); |
||
795 | $request = Craft::$app->getRequest(); |
||
796 | $siteId = $request->getParam('siteId'); |
||
797 | $siteSettings = $request->getParam('seomaticSite'); |
||
798 | |||
799 | // Make sure the twitter handle isn't prefixed with an @ |
||
800 | if (!empty($siteSettings['twitterHandle'])) { |
||
801 | $siteSettings['twitterHandle'] = ltrim($siteSettings['twitterHandle'], '@'); |
||
802 | } |
||
803 | // Make sure the sameAsLinks are indexed by the handle |
||
804 | if (!empty($siteSettings['sameAsLinks'])) { |
||
805 | $siteSettings['sameAsLinks'] = ArrayHelper::index($siteSettings['sameAsLinks'], 'handle'); |
||
806 | } |
||
807 | // The site settings for the appropriate meta bundle |
||
808 | Seomatic::$previewingMetaContainers = true; |
||
809 | $metaBundle = Seomatic::$plugin->metaBundles->getGlobalMetaBundle($siteId); |
||
810 | Seomatic::$previewingMetaContainers = false; |
||
811 | if ($metaBundle) { |
||
812 | if (is_array($siteSettings)) { |
||
813 | if (!empty($siteSettings['identity'])) { |
||
814 | $settings = $siteSettings['identity']; |
||
815 | $this->prepEntitySettings($settings); |
||
816 | $metaBundle->metaSiteVars->identity->setAttributes($settings); |
||
817 | $siteSettings['identity'] = $metaBundle->metaSiteVars->identity; |
||
818 | } |
||
819 | if (!empty($siteSettings['creator'])) { |
||
820 | $settings = $siteSettings['creator']; |
||
821 | $this->prepEntitySettings($settings); |
||
822 | $metaBundle->metaSiteVars->creator->setAttributes($settings); |
||
823 | $siteSettings['creator'] = $metaBundle->metaSiteVars->creator; |
||
824 | } |
||
825 | if (!empty($siteSettings['additionalSitemapUrls'])) { |
||
826 | $siteSettings['additionalSitemapUrlsDateUpdated'] = new DateTime(); |
||
827 | Seomatic::$plugin->sitemaps->submitCustomSitemap($siteId); |
||
828 | } |
||
829 | $metaBundle->metaSiteVars->setAttributes($siteSettings); |
||
830 | } |
||
831 | Seomatic::$plugin->metaBundles->syncBundleWithConfig($metaBundle, true); |
||
832 | Seomatic::$plugin->metaBundles->updateMetaBundle($metaBundle, $siteId); |
||
833 | |||
834 | Seomatic::$plugin->clearAllCaches(); |
||
835 | Craft::$app->getSession()->setNotice(Craft::t('seomatic', 'SEOmatic site settings saved.')); |
||
836 | } |
||
837 | |||
838 | return $this->redirectToPostedUrl(); |
||
839 | } |
||
840 | |||
841 | /** |
||
842 | * Plugin settings |
||
843 | * |
||
844 | * @return Response The rendered result |
||
845 | * @throws ForbiddenHttpException |
||
846 | */ |
||
847 | public function actionPlugin(): Response |
||
848 | { |
||
849 | // Ensure they have permission to edit the plugin settings |
||
850 | $currentUser = Craft::$app->getUser()->getIdentity(); |
||
851 | if (!$currentUser->can('seomatic:plugin-settings')) { |
||
852 | throw new ForbiddenHttpException('You do not have permission to edit SEOmatic plugin settings.'); |
||
853 | } |
||
854 | $general = Craft::$app->getConfig()->getGeneral(); |
||
855 | if (!$general->allowAdminChanges) { |
||
856 | throw new ForbiddenHttpException('Unable to edit SEOmatic plugin settings because admin changes are disabled in this environment.'); |
||
857 | } |
||
858 | // Edit the plugin settings |
||
859 | $variables = []; |
||
860 | $pluginName = Seomatic::$settings->pluginName; |
||
861 | $templateTitle = Craft::t('seomatic', 'Plugin Settings'); |
||
862 | // Asset bundle |
||
863 | try { |
||
864 | Seomatic::$view->registerAssetBundle(SeomaticAsset::class); |
||
865 | } catch (InvalidConfigException $e) { |
||
866 | Craft::error($e->getMessage(), __METHOD__); |
||
867 | } |
||
868 | $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
||
869 | '@nystudio107/seomatic/web/assets/dist', |
||
870 | true |
||
871 | ); |
||
872 | // Basic variables |
||
873 | $variables['fullPageForm'] = true; |
||
874 | $variables['docsUrl'] = self::DOCUMENTATION_URL; |
||
875 | $variables['pluginName'] = Seomatic::$settings->pluginName; |
||
876 | $variables['title'] = $templateTitle; |
||
877 | $variables['docTitle'] = "{$pluginName} - {$templateTitle}"; |
||
878 | $variables['crumbs'] = [ |
||
879 | [ |
||
880 | 'label' => $pluginName, |
||
881 | 'url' => UrlHelper::cpUrl('seomatic'), |
||
882 | ], |
||
883 | [ |
||
884 | 'label' => $templateTitle, |
||
885 | 'url' => UrlHelper::cpUrl('seomatic/plugin'), |
||
886 | ], |
||
887 | ]; |
||
888 | $variables['selectedSubnavItem'] = 'plugin'; |
||
889 | $variables['settings'] = Seomatic::$settings; |
||
890 | $sites = ArrayHelper::map(Craft::$app->getSites()->getAllSites(), 'id', 'name'); |
||
891 | $variables['sites'] = $sites; |
||
892 | |||
893 | // Render the template |
||
894 | return $this->renderTemplate('seomatic/settings/plugin/_edit', $variables); |
||
895 | } |
||
896 | |||
897 | /** |
||
898 | * Tracking settings |
||
899 | * |
||
900 | * @param string $subSection |
||
901 | * @param string|null $siteHandle |
||
902 | * @param string|null $loadFromSiteHandle |
||
903 | * |
||
904 | * @return Response The rendered result |
||
905 | * @throws NotFoundHttpException |
||
906 | * @throws ForbiddenHttpException |
||
907 | */ |
||
908 | public function actionTracking(string $subSection = 'gtag', string $siteHandle = null, $loadFromSiteHandle = null, $editedMetaBundle = null): Response |
||
909 | { |
||
910 | $variables = []; |
||
911 | // Get the site to edit |
||
912 | $siteId = $this->getSiteIdFromHandle($siteHandle); |
||
913 | // Enabled sites |
||
914 | $this->setMultiSiteVariables($siteHandle, $siteId, $variables); |
||
915 | $variables['controllerHandle'] = 'tracking' . '/' . $subSection; |
||
916 | $variables['currentSubSection'] = $subSection; |
||
917 | |||
918 | // The script meta containers for the global meta bundle |
||
919 | Seomatic::$previewingMetaContainers = true; |
||
920 | // Get the site to copy the settings from, if any |
||
921 | $variables['loadFromSiteHandle'] = $loadFromSiteHandle; |
||
922 | $loadFromSiteId = $this->getSiteIdFromHandle($loadFromSiteHandle); |
||
923 | $siteIdToLoad = $loadFromSiteHandle === null ? (int)$variables['currentSiteId'] : $loadFromSiteId; |
||
924 | // Load the metabundle |
||
925 | $metaBundle = Seomatic::$plugin->metaBundles->getGlobalMetaBundle($siteIdToLoad); |
||
926 | if ($editedMetaBundle) { |
||
927 | $metaBundle = $editedMetaBundle; |
||
928 | } |
||
929 | Seomatic::$previewingMetaContainers = false; |
||
930 | if ($metaBundle !== null) { |
||
931 | $variables['scripts'] = Seomatic::$plugin->metaBundles->getContainerDataFromBundle( |
||
932 | $metaBundle, |
||
933 | MetaScriptContainer::CONTAINER_TYPE |
||
934 | ); |
||
935 | } |
||
936 | // Add in the variables to the autocomplete cache so they can be accessed across requests |
||
937 | $subSectionSettings = $variables['scripts'][$subSection]; |
||
938 | $variables['codeEditorOptions'] = [ |
||
939 | TrackingVarsAutocomplete::OPTIONS_DATA_KEY => $subSectionSettings->vars, |
||
940 | ]; |
||
941 | // Plugin and section settings |
||
942 | $pluginName = Seomatic::$settings->pluginName; |
||
943 | $templateTitle = Craft::t('seomatic', 'Tracking Scripts'); |
||
944 | $subSectionTitle = $variables['scripts'][$subSection]->name; |
||
945 | $subSectionTitle = Craft::t('seomatic', $subSectionTitle); |
||
946 | // Asset bundle |
||
947 | try { |
||
948 | Seomatic::$view->registerAssetBundle(SeomaticAsset::class); |
||
949 | } catch (InvalidConfigException $e) { |
||
950 | Craft::error($e->getMessage(), __METHOD__); |
||
951 | } |
||
952 | $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
||
953 | '@nystudio107/seomatic/web/assets/dist', |
||
954 | true |
||
955 | ); |
||
956 | // Basic variables |
||
957 | $variables['fullPageForm'] = true; |
||
958 | $variables['docsUrl'] = self::DOCUMENTATION_URL; |
||
959 | $variables['pluginName'] = Seomatic::$settings->pluginName; |
||
960 | $variables['title'] = $templateTitle; |
||
961 | $variables['subSectionTitle'] = $subSectionTitle; |
||
962 | $variables['docTitle'] = "{$pluginName} - {$templateTitle} - {$subSectionTitle}"; |
||
963 | $siteHandleUri = Craft::$app->isMultiSite ? '/' . $siteHandle : ''; |
||
964 | $variables['crumbs'] = [ |
||
965 | [ |
||
966 | 'label' => $pluginName, |
||
967 | 'url' => UrlHelper::cpUrl('seomatic'), |
||
968 | ], |
||
969 | [ |
||
970 | 'label' => $templateTitle, |
||
971 | 'url' => UrlHelper::cpUrl('seomatic/tracking'), |
||
972 | ], |
||
973 | [ |
||
974 | 'label' => $subSectionTitle, |
||
975 | 'url' => UrlHelper::cpUrl('seomatic/tracking/' . $subSection . $siteHandleUri), |
||
976 | ], |
||
977 | ]; |
||
978 | $variables['selectedSubnavItem'] = 'tracking'; |
||
979 | $this->setCrumbVariables($variables); |
||
980 | |||
981 | // Render the template |
||
982 | return $this->renderTemplate('seomatic/settings/tracking/_edit', $variables); |
||
983 | } |
||
984 | |||
985 | /** |
||
986 | * @return Response|null |
||
987 | * @throws BadRequestHttpException |
||
988 | * @throws MissingComponentException |
||
989 | */ |
||
990 | public function actionSaveTracking() |
||
1046 | } |
||
1047 | |||
1048 | /** |
||
1049 | * Saves a plugin’s settings. |
||
1050 | * |
||
1051 | * @return Response|null |
||
1052 | * @throws NotFoundHttpException if the requested plugin cannot be found |
||
1053 | * @throws BadRequestHttpException |
||
1054 | * @throws MissingComponentException |
||
1055 | */ |
||
1056 | public function actionSavePluginSettings() |
||
1057 | { |
||
1058 | // Ensure they have permission to edit the plugin settings |
||
1059 | $currentUser = Craft::$app->getUser()->getIdentity(); |
||
1060 | if (!$currentUser->can('seomatic:plugin-settings')) { |
||
1061 | throw new ForbiddenHttpException('You do not have permission to edit SEOmatic plugin settings.'); |
||
1062 | } |
||
1063 | $general = Craft::$app->getConfig()->getGeneral(); |
||
1064 | if (!$general->allowAdminChanges) { |
||
1065 | throw new ForbiddenHttpException('Unable to edit SEOmatic plugin settings because admin changes are disabled in this environment.'); |
||
1066 | } |
||
1067 | // Save the plugin settings |
||
1068 | $this->requirePostRequest(); |
||
1069 | $pluginHandle = Craft::$app->getRequest()->getRequiredBodyParam('pluginHandle'); |
||
1070 | $settings = Craft::$app->getRequest()->getBodyParam('settings', []); |
||
1071 | $plugin = Craft::$app->getPlugins()->getPlugin($pluginHandle); |
||
1072 | |||
1073 | if ($plugin === null) { |
||
1074 | throw new NotFoundHttpException('Plugin not found'); |
||
1075 | } |
||
1076 | |||
1077 | if (!Craft::$app->getPlugins()->savePluginSettings($plugin, $settings)) { |
||
1078 | Craft::$app->getSession()->setError(Craft::t('app', "Couldn't save plugin settings.")); |
||
1079 | |||
1080 | // Send the redirect back to the template |
||
1081 | /** @var UrlManager $urlManager */ |
||
1082 | $urlManager = Craft::$app->getUrlManager(); |
||
1083 | $urlManager->setRouteParams([ |
||
1084 | 'plugin' => $plugin, |
||
1085 | ]); |
||
1086 | |||
1087 | return null; |
||
1088 | } |
||
1089 | |||
1090 | Seomatic::$plugin->clearAllCaches(); |
||
1091 | Craft::$app->getSession()->setNotice(Craft::t('app', 'Plugin settings saved.')); |
||
1092 | |||
1093 | return $this->redirectToPostedUrl(); |
||
1094 | } |
||
1095 | |||
1096 | // Protected Methods |
||
1097 | // ========================================================================= |
||
1098 | |||
1099 | /** |
||
1100 | * Return a siteId from a siteHandle |
||
1101 | * |
||
1102 | * @param string|null $siteHandle |
||
1103 | * |
||
1104 | * @return int|null |
||
1105 | * @throws NotFoundHttpException |
||
1106 | */ |
||
1107 | protected function getSiteIdFromHandle($siteHandle) |
||
1108 | { |
||
1109 | // Get the site to edit |
||
1110 | if ($siteHandle !== null) { |
||
1111 | $site = Craft::$app->getSites()->getSiteByHandle($siteHandle); |
||
1112 | if (!$site) { |
||
1113 | throw new NotFoundHttpException('Invalid site handle: ' . $siteHandle); |
||
1114 | } |
||
1115 | $siteId = $site->id; |
||
1116 | } else { |
||
1117 | $siteId = Craft::$app->getSites()->currentSite->id; |
||
1118 | } |
||
1119 | |||
1120 | return $siteId; |
||
1121 | } |
||
1122 | |||
1123 | /** |
||
1124 | * @param $siteHandle |
||
1125 | * @param $siteId |
||
1126 | * @param array $variables |
||
1127 | * @param $element |
||
1128 | * @return void |
||
1129 | * @throws ForbiddenHttpException |
||
1130 | */ |
||
1131 | protected function setMultiSiteVariables($siteHandle, &$siteId, array &$variables, $element = null) |
||
1165 | ); |
||
1166 | } |
||
1167 | |||
1168 | /** |
||
1169 | * @param array $variables |
||
1170 | * @return void |
||
1171 | */ |
||
1172 | protected function setCrumbVariables(array &$variables) |
||
1173 | { |
||
1174 | $sites = Craft::$app->getSites(); |
||
1175 | if ($variables['showSites']) { |
||
1176 | $siteCrumbItems = []; |
||
1177 | $siteGroups = Craft::$app->getSites()->getAllGroups(); |
||
1178 | $crumbSites = Collection::make($sites->getAllSites()) |
||
1179 | ->map(fn(Site $site) => ['site' => $site]) |
||
1180 | ->keyBy(fn(array $site) => $site['site']->id) |
||
1181 | ->filter(fn(array $site) => in_array($site['site']->id, $variables['enabledSiteIds'])) |
||
1182 | ->all(); |
||
1183 | |||
1184 | foreach ($siteGroups as $siteGroup) { |
||
1185 | $groupSites = $siteGroup->getSites(); |
||
1186 | |||
1187 | if (empty($groupSites)) { |
||
1188 | continue; |
||
1189 | } |
||
1190 | |||
1191 | $groupSiteItems = array_map(fn(Site $site) => [ |
||
1192 | 'status' => $crumbSites[$site->id]['site']->status ?? null, |
||
1193 | 'label' => Craft::t('site', $site->name), |
||
1194 | 'url' => UrlHelper::cpUrl("seomatic/{$variables['controllerHandle']}/$site->handle"), |
||
1195 | 'hidden' => !isset($crumbSites[$site->id]), |
||
1196 | 'selected' => $site->id === $variables['currentSiteId'], |
||
1197 | 'attributes' => [ |
||
1198 | 'data' => [ |
||
1199 | 'site-id' => $site->id, |
||
1200 | ], |
||
1201 | ], |
||
1202 | ], $groupSites); |
||
1203 | |||
1204 | if (count($siteGroups) > 1) { |
||
1205 | $siteCrumbItems[] = [ |
||
1206 | 'heading' => Craft::t('site', $siteGroup->name), |
||
1207 | 'items' => $groupSiteItems, |
||
1208 | 'hidden' => !ArrayHelper::contains($groupSiteItems, fn(array $item) => !$item['hidden']), |
||
1209 | ]; |
||
1210 | } else { |
||
1211 | array_push($siteCrumbItems, ...$groupSiteItems); |
||
1212 | } |
||
1213 | } |
||
1214 | |||
1215 | if (array_key_exists('crumbs', $variables)) { |
||
1216 | $variables['crumbs'] = [ |
||
1217 | [ |
||
1218 | 'id' => 'language-menu', |
||
1219 | 'icon' => 'world', |
||
1220 | 'label' => Craft::t( |
||
1221 | 'site', |
||
1222 | $sites->getSiteById((int)$variables['currentSiteId'])->name |
||
1223 | ), |
||
1224 | 'menu' => [ |
||
1225 | 'items' => $siteCrumbItems, |
||
1226 | 'label' => Craft::t('site', 'Select site'), |
||
1227 | ], |
||
1228 | ], |
||
1229 | ...$variables['crumbs'], |
||
1230 | ]; |
||
1231 | } else { |
||
1232 | $variables['crumbs'] = [ |
||
1233 | [ |
||
1234 | 'id' => 'language-menu', |
||
1235 | 'icon' => 'world', |
||
1236 | 'label' => Craft::t( |
||
1237 | 'site', |
||
1238 | $sites->getSiteById((int)$variables['currentSiteId'])->name |
||
1239 | ), |
||
1240 | 'menu' => [ |
||
1241 | 'items' => $siteCrumbItems, |
||
1242 | 'label' => Craft::t('site', 'Select site'), |
||
1243 | ], |
||
1244 | ], |
||
1245 | ]; |
||
1246 | } |
||
1247 | $variables['sitesMenuLabel'] = Craft::t( |
||
1248 | 'site', |
||
1249 | $sites->getSiteById((int)$variables['currentSiteId'])->name |
||
1250 | ); |
||
1251 | } else { |
||
1252 | $variables['sitesMenuLabel'] = ''; |
||
1253 | } |
||
1254 | } |
||
1255 | |||
1256 | /** |
||
1257 | * @param array $variables |
||
1258 | */ |
||
1259 | protected function setGlobalFieldSourceVariables(array &$variables) |
||
1260 | { |
||
1261 | $variables['textFieldSources'] = array_merge( |
||
1262 | ['globalsGroup' => ['optgroup' => 'Globals Fields']], |
||
1263 | FieldHelper::fieldsOfTypeFromGlobals( |
||
1264 | FieldHelper::TEXT_FIELD_CLASS_KEY, |
||
1265 | false |
||
1266 | ) |
||
1267 | ); |
||
1268 | $variables['assetFieldSources'] = array_merge( |
||
1269 | ['globalsGroup' => ['optgroup' => 'Globals Fields']], |
||
1270 | FieldHelper::fieldsOfTypeFromGlobals( |
||
1271 | FieldHelper::ASSET_FIELD_CLASS_KEY, |
||
1272 | false |
||
1273 | ) |
||
1274 | ); |
||
1275 | } |
||
1276 | |||
1277 | /** |
||
1278 | * Remove any sites for which meta bundles do not exist (they may be |
||
1279 | * disabled for this section) |
||
1280 | * |
||
1281 | * @param string $sourceBundleType |
||
1282 | * @param string $sourceHandle |
||
1283 | * @param array $variables |
||
1284 | */ |
||
1285 | protected function cullDisabledSites(string $sourceBundleType, string $sourceHandle, array &$variables) |
||
1286 | { |
||
1287 | $entries = Craft::$app->getEntries(); |
||
1288 | $section = $entries->getSectionByHandle($sourceHandle); |
||
1289 | $sectionSiteIds = []; |
||
1290 | if ($section) { |
||
1291 | $sectionSettings = $entries->getSectionSiteSettings($section->id); |
||
1292 | foreach ($sectionSettings as $sectionSetting) { |
||
1293 | $sectionSiteIds[] = $sectionSetting->siteId; |
||
1294 | } |
||
1295 | } |
||
1296 | if (isset($variables['enabledSiteIds'])) { |
||
1297 | foreach ($variables['enabledSiteIds'] as $key => $value) { |
||
1298 | $metaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceHandle( |
||
1299 | $sourceBundleType, |
||
1300 | $sourceHandle, |
||
1301 | $value |
||
1302 | ); |
||
1303 | // Make sure the site exists for this Section |
||
1304 | if (!in_array($value, $sectionSiteIds, true)) { |
||
1305 | unset($variables['enabledSiteIds'][$key]); |
||
1306 | } |
||
1307 | if ($metaBundle === null) { |
||
1308 | unset($variables['enabledSiteIds'][$key]); |
||
1309 | } |
||
1310 | } |
||
1311 | } |
||
1312 | } |
||
1313 | |||
1314 | /** |
||
1315 | * @param string $sourceBundleType |
||
1316 | * @param string $sourceHandle |
||
1317 | * @param string $groupName |
||
1318 | * @param array $variables |
||
1319 | * @param int|string|null $typeId |
||
1320 | */ |
||
1321 | protected function setContentFieldSourceVariables( |
||
1322 | string $sourceBundleType, |
||
1323 | string $sourceHandle, |
||
1324 | string $groupName, |
||
1325 | array &$variables, |
||
1326 | $typeId = null, |
||
1327 | ) { |
||
1328 | $variables['textFieldSources'] = array_merge( |
||
1329 | ['entryGroup' => ['optgroup' => $groupName . ' Fields'], 'title' => 'Title'], |
||
1330 | FieldHelper::fieldsOfTypeFromSource( |
||
1331 | $sourceBundleType, |
||
1332 | $sourceHandle, |
||
1333 | FieldHelper::TEXT_FIELD_CLASS_KEY, |
||
1334 | false, |
||
1335 | $typeId |
||
1336 | ) |
||
1337 | ); |
||
1338 | $variables['assetFieldSources'] = array_merge( |
||
1339 | ['entryGroup' => ['optgroup' => $groupName . ' Fields']], |
||
1340 | FieldHelper::fieldsOfTypeFromSource( |
||
1341 | $sourceBundleType, |
||
1342 | $sourceHandle, |
||
1343 | FieldHelper::ASSET_FIELD_CLASS_KEY, |
||
1344 | false, |
||
1345 | $typeId |
||
1346 | ) |
||
1347 | ); |
||
1348 | $variables['assetVolumeTextFieldSources'] = array_merge( |
||
1349 | ['entryGroup' => ['optgroup' => 'Asset Volume Fields'], '' => '--', 'title' => 'Title'], |
||
1350 | array_merge( |
||
1351 | FieldHelper::fieldsOfTypeFromAssetVolumes( |
||
1352 | FieldHelper::TEXT_FIELD_CLASS_KEY, |
||
1353 | false |
||
1354 | ) |
||
1355 | ) |
||
1356 | ); |
||
1357 | $variables['userFieldSources'] = array_merge( |
||
1358 | ['entryGroup' => ['optgroup' => 'User Fields']], |
||
1359 | FieldHelper::fieldsOfTypeFromUsers( |
||
1360 | FieldHelper::TEXT_FIELD_CLASS_KEY, |
||
1361 | false |
||
1362 | ) |
||
1363 | ); |
||
1364 | } |
||
1365 | |||
1366 | /** |
||
1367 | * @param string $sourceBundleType |
||
1368 | * @param string $sourceHandle |
||
1369 | * @param null|int $siteId |
||
1370 | * @param int|string|null $typeId |
||
1371 | * |
||
1372 | * @return string |
||
1373 | */ |
||
1374 | protected function uriFromSourceBundle(string $sourceBundleType, string $sourceHandle, $siteId, $typeId): string |
||
1392 | } |
||
1393 | |||
1394 | /** |
||
1395 | * Prep the entity settings for saving to the db |
||
1396 | * |
||
1397 | * @param array &$settings |
||
1398 | */ |
||
1399 | protected function prepEntitySettings(&$settings) |
||
1417 | } |
||
1418 | } |
||
1419 | } |
||
1421 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.