1111namespace Joomla \Plugin \System \Opengraph \Extension ;
1212
1313use Joomla \CMS \Application \CMSApplication ;
14+ use Joomla \CMS \Component \ComponentHelper ;
15+ use Joomla \CMS \Document \Document ;
16+ use Joomla \CMS \Document \HtmlDocument ;
1417use Joomla \CMS \Event \Application \BeforeCompileHeadEvent ;
1518use Joomla \CMS \Event \Model \PrepareFormEvent ;
16- use Joomla \CMS \Plugin \CMSPlugin ;
17- use Joomla \Event \SubscriberInterface ;
18- use Joomla \CMS \Document \HtmlDocument ;
19- use Joomla \Registry \Registry ;
20- use Joomla \CMS \Document \Document ;
19+ use Joomla \CMS \Menu \MenuItem ;
20+ use Joomla \CMS \MVC \Factory \MVCFactoryInterface ;
2121use Joomla \CMS \Opengraph \OpengraphServiceInterface ;
22+ use Joomla \CMS \Plugin \CMSPlugin ;
2223use Joomla \CMS \Uri \Uri ;
23- use Exception ;
24- use Joomla \CMS \Component \ComponentHelper ;
25- use Joomla \CMS \Menu \MenuItem ;
26- use Joomla \Component \Fields \Administrator \Helper \FieldsHelper ;
2724use Joomla \Component \Content \Site \Model \ArticleModel ;
2825use Joomla \Component \Content \Site \Model \CategoryModel ;
29- use Joomla \CMS \ MVC \ Factory \ MVCFactoryInterface ;
30-
31-
26+ use Joomla \Component \ Fields \ Administrator \ Helper \ FieldsHelper ;
27+ use Joomla \ Event \ SubscriberInterface ;
28+ use Joomla \ Registry \ Registry ;
3229
3330// phpcs:disable PSR1.Files.SideEffects
3431\defined ('_JEXEC ' ) or die;
4340
4441final class Opengraph extends CMSPlugin implements SubscriberInterface
4542{
46-
4743 /**
4844 * The application object.
4945 *
@@ -69,7 +65,7 @@ final class Opengraph extends CMSPlugin implements SubscriberInterface
6965 public static function getSubscribedEvents (): array
7066 {
7167 return [
72- 'onBeforeCompileHead ' => 'onBeforeCompileHead ' ,
68+ 'onBeforeCompileHead ' => 'onBeforeCompileHead ' ,
7369 'onContentPrepareForm ' => 'onContentPrepareForm ' ,
7470 ];
7571 }
@@ -92,7 +88,7 @@ public function onContentPrepareForm(PrepareFormEvent $event): void
9288 }
9389
9490 $ isCategory = $ context === 'com_categories.categorycom_content ' ;
95- $ isMenu = $ context === 'com_menus.item ' ;
91+ $ isMenu = $ context === 'com_menus.item ' ;
9692
9793 $ groupName = $ isMenu ? 'params ' : 'attribs ' ;
9894
@@ -101,7 +97,7 @@ public function onContentPrepareForm(PrepareFormEvent $event): void
10197 try {
10298 $ form ::addFormPath (__DIR__ . '/../forms ' );
10399 $ form ->loadFile ('opengraphmappings ' , false );
104- } catch (Exception $ e ) {
100+ } catch (\ Exception $ e ) {
105101 error_log ('OpenGraph Plugin: Failed to load mappings form: ' . $ e ->getMessage ());
106102 }
107103
@@ -114,7 +110,7 @@ public function onContentPrepareForm(PrepareFormEvent $event): void
114110 try {
115111 $ modifiedXml = $ this ->adjustFieldsGroup ($ mainXml , $ groupName );
116112 $ form ->load ($ modifiedXml , false );
117- } catch (Exception $ e ) {
113+ } catch (\ Exception $ e ) {
118114 error_log ('OpenGraph Plugin: Failed to load main form: ' . $ e ->getMessage ());
119115 }
120116 }
@@ -148,7 +144,7 @@ public function onBeforeCompileHead(BeforeCompileHeadEvent $event): void
148144 $ input = $ app ->input ;
149145 if (
150146 $ input ->getCmd ('option ' ) !== 'com_content '
151- || $ input ->getCmd ('view ' ) !== 'article '
147+ || $ input ->getCmd ('view ' ) !== 'article '
152148 || ! $ id = $ input ->getInt ('id ' )
153149 ) {
154150 return ;
@@ -167,7 +163,7 @@ public function onBeforeCompileHead(BeforeCompileHeadEvent $event): void
167163 $ params = ComponentHelper::getParams ('com_content ' );
168164 // Fallback if for some reason it isn’t an object
169165 if (! $ params instanceof Registry) {
170- $ params = new Registry ;
166+ $ params = new Registry () ;
171167 }
172168
173169 /** @var ArticleModel $articleModel */
@@ -194,29 +190,29 @@ public function onBeforeCompileHead(BeforeCompileHeadEvent $event): void
194190
195191
196192 // Get menu parameters
197- $ menuParams = $ this ->getMenuParams ();
193+ $ menuParams = $ this ->getMenuParams ();
198194 $ articleAttribs = new Registry ($ article ->attribs ?? '{} ' );
199195 $ categoryParams = new Registry ($ category ->params ?? '{} ' );
200- $ articleImages = $ this ->getAllArticleImages (new Registry ($ article ->images ?? '{} ' ));
196+ $ articleImages = $ this ->getAllArticleImages (new Registry ($ article ->images ?? '{} ' ));
201197
202198
203199
204200 $ ogTags = [
205- 'og_title ' => '' ,
206- 'og_description ' => '' ,
207- 'og_image ' => '' ,
208- 'og_image_alt ' => '' ,
209- 'og_type ' => '' ,
210- 'og_url ' => '' ,
211- 'twitter_card ' => '' ,
212- 'twitter_title ' => '' ,
201+ 'og_title ' => '' ,
202+ 'og_description ' => '' ,
203+ 'og_image ' => '' ,
204+ 'og_image_alt ' => '' ,
205+ 'og_type ' => '' ,
206+ 'og_url ' => '' ,
207+ 'twitter_card ' => '' ,
208+ 'twitter_title ' => '' ,
213209 'twitter_description ' => '' ,
214- 'twitter_image ' => '' ,
215- 'twitter_image_alt ' => '' ,
216- 'fb_app_id ' => '' ,
217- 'site_name ' => '' ,
218- 'url ' => '' ,
219- 'base_url ' => ''
210+ 'twitter_image ' => '' ,
211+ 'twitter_image_alt ' => '' ,
212+ 'fb_app_id ' => '' ,
213+ 'site_name ' => '' ,
214+ 'url ' => '' ,
215+ 'base_url ' => '' ,
220216 ];
221217
222218 // Get Global settings
@@ -225,8 +221,8 @@ public function onBeforeCompileHead(BeforeCompileHeadEvent $event): void
225221
226222 $ ogTags ['fb_app_id ' ] = $ this ->params ->get ('fb_app_id ' );
227223 $ ogTags ['site_name ' ] = $ config ->get ('sitename ' );
228- $ ogTags ['base_url ' ] = Uri::base ();
229- $ ogTags ['url ' ] = Uri::getInstance ()->toString ();
224+ $ ogTags ['base_url ' ] = Uri::base ();
225+ $ ogTags ['url ' ] = Uri::getInstance ()->toString ();
230226
231227 // get OG tags from category mappings
232228 $ this ->getOgTagsFromCategoryMappings ($ categoryParams , $ article , $ articleImages , $ ogTags );
@@ -261,7 +257,7 @@ private function getOgTagsFromCategoryMappings(Registry $categoryParams, object
261257 if (strpos ($ key , 'og_ ' ) === 0 && str_ends_with ($ key , '_field ' )) {
262258 $ ogTagName = substr ($ key , 0 , -6 ); // Remove "_field" from the end
263259
264- $ value = $ this ->getFieldValue ($ article , $ fieldName , $ articleImages );
260+ $ value = $ this ->getFieldValue ($ article , $ fieldName , $ articleImages );
265261 $ ogTags [$ ogTagName ] = $ value ;
266262 }
267263 }
@@ -360,7 +356,7 @@ private function getFieldValue(object $article, string $fieldName, array $articl
360356 */
361357 private function getAllArticleImages (Registry $ articleImages ): array
362358 {
363- $ image_intro = $ image_intro_alt = '' ;
359+ $ image_intro = $ image_intro_alt = '' ;
364360 $ image_fulltext = $ image_fulltext_alt = '' ;
365361
366362 // Handle image_intro
@@ -383,9 +379,9 @@ private function getAllArticleImages(Registry $articleImages): array
383379 }
384380
385381 return [
386- 'image_intro ' => $ image_intro ,
387- 'image_intro_alt ' => $ image_intro_alt ,
388- 'image_fulltext ' => $ image_fulltext ,
382+ 'image_intro ' => $ image_intro ,
383+ 'image_intro_alt ' => $ image_intro_alt ,
384+ 'image_fulltext ' => $ image_fulltext ,
389385 'image_fulltext_alt ' => $ image_fulltext_alt ,
390386 ];
391387 }
@@ -427,10 +423,10 @@ private function getOgTagsFromParams(Registry $params, array &$ogTags): void
427423 private function getTwitterOgTags (array &$ ogTags ): void
428424 {
429425 $ twitterTags = [
430- 'twitter_title ' => $ ogTags ['twitter_title ' ],
426+ 'twitter_title ' => $ ogTags ['twitter_title ' ],
431427 'twitter_description ' => $ ogTags ['twitter_description ' ],
432- 'twitter_image ' => $ ogTags ['twitter_image ' ],
433- 'twitter_image_alt ' => $ ogTags ['twitter_image_alt ' ]
428+ 'twitter_image ' => $ ogTags ['twitter_image ' ],
429+ 'twitter_image_alt ' => $ ogTags ['twitter_image_alt ' ],
434430 ];
435431 foreach ($ twitterTags as $ key => $ value ) {
436432 // If the value is not set, use the OG value
@@ -497,10 +493,10 @@ private function setOpenGraphImage(
497493 Document $ document ,
498494 array $ ogTags
499495 ): void {
500- $ image = $ ogTags ['og_image ' ];
501- $ alt = $ ogTags ['og_image_alt ' ];
502- $ baseUrl = $ ogTags ['base_url ' ];
503- $ twitterImage = $ ogTags ['twitter_image ' ];
496+ $ image = $ ogTags ['og_image ' ];
497+ $ alt = $ ogTags ['og_image_alt ' ];
498+ $ baseUrl = $ ogTags ['base_url ' ];
499+ $ twitterImage = $ ogTags ['twitter_image ' ];
504500 $ twitterImageAlt = $ ogTags ['twitter_image_alt ' ];
505501
506502 if (empty ($ image ) || !empty ($ document ->getMetaData ('og:image ' ))) {
@@ -564,7 +560,7 @@ protected function isSupported($context): bool
564560
565561 try {
566562 $ component = $ this ->getApplication ()->bootComponent ($ componentName );
567- } catch (Exception $ e ) {
563+ } catch (\ Exception $ e ) {
568564 error_log ('OpenGraph Plugin: Failed to boot component: ' . $ e ->getMessage ());
569565 return false ;
570566 }
@@ -585,10 +581,10 @@ protected function isSupported($context): bool
585581 private function adjustFieldsGroup (string $ filePath , string $ newGroup ): string
586582 {
587583 $ xmlContent = file_get_contents ($ filePath );
588- $ xml = simplexml_load_string ($ xmlContent );
584+ $ xml = simplexml_load_string ($ xmlContent );
589585
590586 if ($ xml === false ) {
591- throw new Exception ("Could not load XML file: {$ filePath }" );
587+ throw new \ Exception ("Could not load XML file: {$ filePath }" );
592588 }
593589
594590 // Adjust all <fields> nodes to use the desired group
0 commit comments