11<?php
2+
23/**
34 * @package Joomla.Plugin
45 * @subpackage System.Opengraph
2425
2526
2627
28+ // phpcs:disable PSR1.Files.SideEffects
2729\defined ('_JEXEC ' ) or die;
30+ // phpcs:enable PSR1.Files.SideEffects
31+
32+ /**
33+ * OpenGraph Metadata plugin.
34+ *
35+ * @since __DEPLOY_VERSION__
36+ */
37+
2838
2939final class Opengraph extends CMSPlugin implements SubscriberInterface
3040{
41+
42+ /**
43+ * The application object.
44+ *
45+ * @var CMSApplication
46+ */
3147 protected $ app ;
48+
49+ /**
50+ * Should the plugin autoload its language files.
51+ *
52+ * @var bool
53+ */
3254 protected $ autoloadLanguage = true ;
3355
56+ /**
57+ * Returns an array of events this subscriber will listen to.
58+ *
59+ * @return array
60+ *
61+ * @since __DEPLOY_VERSION__
62+ */
3463 public static function getSubscribedEvents (): array
3564 {
3665 return [
3766 'onBeforeCompileHead ' => 'onBeforeCompileHead ' ,
3867 'onContentPrepareForm ' => 'onContentPrepareForm ' ,
3968 ];
4069 }
41-
70+ /**
71+ * Handle the beforeCompileHead event.
72+ *
73+ * @param BeforeCompileHeadEvent $event
74+ *
75+ * @return void
76+ */
4277 public function onBeforeCompileHead (BeforeCompileHeadEvent $ event ): void
4378 {
4479 $ app = $ this ->getApplication ();
@@ -52,6 +87,9 @@ public function onBeforeCompileHead(BeforeCompileHeadEvent $event): void
5287 $ view = $ input ->get ('view ' , '' , 'cmd ' );
5388 $ id = $ input ->getInt ('id ' );
5489
90+
91+ // @todo: will be changed in future to support other components
92+
5593 if ($ option !== 'com_content ' || $ view !== 'article ' || !$ id ) {
5694 return ;
5795 }
@@ -76,6 +114,13 @@ public function onBeforeCompileHead(BeforeCompileHeadEvent $event): void
76114 $ this ->injectOpenGraphData ($ document , $ attribs );
77115 }
78116
117+ /**
118+ * Handle the onContentPrepareForm event.
119+ *
120+ * @param PrepareFormEvent $event
121+ *
122+ * @return void
123+ */
79124 public function onContentPrepareForm (PrepareFormEvent $ event ): void
80125 {
81126 $ app = $ this ->getApplication ();
@@ -95,6 +140,14 @@ public function onContentPrepareForm(PrepareFormEvent $event): void
95140 }
96141 }
97142
143+ /**
144+ * Inject the OpenGraph data into the document.
145+ *
146+ * @param Document $document
147+ * @param Registry $params
148+ *
149+ * @return void
150+ */
98151 private function injectOpenGraphData (Document $ document , Registry $ params ): void
99152 {
100153
@@ -116,17 +169,35 @@ private function injectOpenGraphData(Document $document, Registry $params): void
116169 $ params ->get ('og_image_alt ' ),
117170 Uri::base ()
118171 );
119-
120-
121172 }
122173
174+ /**
175+ * Set metadata tag in document.
176+ *
177+ * @param Document $document
178+ * @param string $name
179+ * @param string|null $value
180+ * @param string $attributeType
181+ *
182+ * @return void
183+ */
123184 private function setMetaData (Document $ document , string $ name , ?string $ value , string $ attributeType ): void
124185 {
125186 if (!empty ($ value )) {
126187 $ document ->setMetaData ($ name , $ value , $ attributeType );
127188 }
128189 }
129190
191+ /**
192+ * Set OpenGraph Image tag in document.
193+ *
194+ * @param Document $document
195+ * @param string|null $image
196+ * @param string|null $alt
197+ * @param string|null $baseUrl
198+ *
199+ * @return void
200+ */
130201 private function setOpenGraphImage (Document $ document , ?string $ image , ?string $ alt = '' , ?string $ baseUrl = '' ): void
131202 {
132203 if (empty ($ image )) {
@@ -140,6 +211,4 @@ private function setOpenGraphImage(Document $document, ?string $image, ?string $
140211 $ this ->setMetaData ($ document , 'twitter:image ' , $ url , 'name ' );
141212 $ this ->setMetaData ($ document , 'twitter:image:alt ' , $ alt , 'name ' );
142213 }
143-
144-
145- }
214+ }
0 commit comments