2323use Joomla \CMS \Opengraph \OpengraphServiceInterface ;
2424use Joomla \CMS \Uri \Uri ;
2525use Exception ;
26+ use Joomla \CMS \Component \ComponentHelper ;
2627use Joomla \CMS \Table \Content ;
2728use Joomla \Database \DatabaseInterface ;
2829use Joomla \CMS \Menu \MenuItem ;
2930use Joomla \Component \Fields \Administrator \Helper \FieldsHelper ;
31+ use Joomla \Component \Content \Site \Model \ArticleModel ;
32+ use Joomla \Component \Content \Site \Model \CategoryModel ;
33+ use Joomla \CMS \MVC \Factory \MVCFactoryInterface ;
34+
35+
3036
3137// phpcs:disable PSR1.Files.SideEffects
3238\defined ('_JEXEC ' ) or die;
@@ -56,16 +62,6 @@ final class Opengraph extends CMSPlugin implements SubscriberInterface
5662 */
5763 protected $ autoloadLanguage = true ;
5864
59- /**
60- * Database object
61- *
62- * The database is injected by parent constructor
63- *
64- * @var DatabaseInterface
65- * @since __DEPLOY_VERSION__
66- */
67- protected $ db ;
68-
6965
7066 /**
7167 * Returns an array of events this subscriber will listen to.
@@ -140,46 +136,64 @@ public function onBeforeCompileHead(BeforeCompileHeadEvent $event): void
140136 {
141137 $ app = $ this ->app ;
142138
139+
143140 if (!$ app ->isClient ('site ' )) {
144141 return ;
145142 }
146143
144+ /** @var HtmlDocument $document */
147145 $ document = $ app ->getDocument ();
148146
149147 // Only process HTML documents
150148 if (!($ document instanceof HtmlDocument)) {
151149 return ;
152150 }
153151
154- $ input = $ app ->input ;
155- $ option = $ input ->get ('option ' , '' , 'cmd ' );
156- $ view = $ input ->get ('view ' , '' , 'cmd ' );
157- $ id = $ input ->getInt ('id ' );
158-
159- // Skip indexer and feed formats
160- if (($ option . '. ' . $ view ) === 'com_finder.indexer ' ) {
161- return ;
162- }
163-
164- if ($ input ->get ('format ' , '' , 'cmd ' ) === 'feed ' ) {
152+ $ input = $ app ->input ;
153+ if (
154+ $ input ->getCmd ('option ' ) !== 'com_content '
155+ || $ input ->getCmd ('view ' ) !== 'article '
156+ || ! $ id = $ input ->getInt ('id ' )
157+ ) {
165158 return ;
166159 }
167- // Check if plugin is enabled globally
160+ // Plugin globally disabled?
168161 if (!$ this ->params ->get ('enable_og_generation ' , 1 )) {
169162 return ;
170163 }
171- $ uniqueArticle = true ;
172- if (is_array ($ input ->get ('id ' , 0 , 'int ' ))) {
173- $ uniqueArticle = false ;
164+
165+ /** @var MVCComponent $component */
166+ $ component = $ app ->bootComponent ('com_content ' );
167+
168+ /** @var MVCFactoryInterface $mvcFactory */
169+ $ mvcFactory = $ component ->getMVCFactory ();
170+
171+ $ params = ComponentHelper::getParams ('com_content ' );
172+ // Fallback if for some reason it isn’t an object
173+ if (! $ params instanceof Registry) {
174+ $ params = new Registry ;
174175 }
175176
176- if ($ uniqueArticle ) {
177- $ article = new Content ($ this ->db );
178- $ article ->load ($ id );
179- $ category = Table::getInstance ('Category ' );
180- $ category ->load ($ article ->catid );
177+ /** @var ArticleModel $articleModel */
178+ $ articleModel = $ mvcFactory ->createModel ('Article ' , 'Site ' , ['ignore_request ' => true ]);
179+
180+ $ articleModel ->setState ('params ' , clone $ params );
181+ $ articleModel ->setState ('article.id ' , $ id );
182+
183+ $ article = $ articleModel ->getItem ($ id );
184+ if (! $ article ) {
185+ return ;
181186 }
182187
188+ /** @var CategoryModel $categoryModel */
189+ $ categoryModel = $ mvcFactory ->createModel (
190+ 'Category ' ,
191+ 'Site ' ,
192+ ['ignore_request ' => true ]
193+ );
194+ $ categoryModel ->setState ('category.id ' , $ article ->catid );
195+ $ category = $ categoryModel ->getCategory ();
196+
183197
184198
185199
@@ -243,15 +257,16 @@ public function onBeforeCompileHead(BeforeCompileHeadEvent $event): void
243257 * @param array $articleImages The array of article images.
244258 * @param array $ogTags The array of OG tags.
245259 */
246- private function getOgTagsFromCategoryMappings (Registry $ categoryParams , Content $ article , array $ articleImages , array &$ ogTags ): void
260+ private function getOgTagsFromCategoryMappings (Registry $ categoryParams , object $ article , array $ articleImages , array &$ ogTags ): void
247261 {
248262
249263 foreach ($ categoryParams as $ key => $ fieldName ) {
250264 // Only process keys that start with 'og_'
251265 if (strpos ($ key , 'og_ ' ) === 0 && str_ends_with ($ key , '_field ' )) {
252266 $ ogTagName = substr ($ key , 0 , -6 ); // Remove "_field" from the end
253267
254- $ ogTags [$ ogTagName ] = $ this ->getFieldValue ($ article , $ fieldName , $ articleImages );
268+ $ value = $ this ->getFieldValue ($ article , $ fieldName , $ articleImages );
269+ $ ogTags [$ ogTagName ] = $ value ;
255270 }
256271 }
257272 }
@@ -267,7 +282,7 @@ private function getOgTagsFromCategoryMappings(Registry $categoryParams, Content
267282 *
268283 * @return string
269284 */
270- private function getFieldValue (Content $ article , string $ fieldName , array $ articleImages ): string
285+ private function getFieldValue (object $ article , string $ fieldName , array $ articleImages ): string
271286 {
272287 // Check if it's a custom field
273288 if (strpos ($ fieldName , 'field. ' ) === 0 ) {
0 commit comments