1<?php 2 3use ComboStrap\ExceptionNotFound; 4use ComboStrap\ExecutionContext; 5use ComboStrap\FileSystems; 6use ComboStrap\LdJson; 7use ComboStrap\LogUtility; 8use ComboStrap\MarkupPath; 9use ComboStrap\PluginUtility; 10 11 12require_once(__DIR__ . '/../ComboStrap/PluginUtility.php'); 13 14 15class action_plugin_combo_metagoogle extends DokuWiki_Action_Plugin 16{ 17 18 19 const CANONICAL = "google"; 20 const PUBLISHER = "publisher"; 21 22 function __construct() 23 { 24 // enable direct access to language strings 25 // ie $this->lang 26 $this->setupLocale(); 27 } 28 29 public function register(Doku_Event_Handler $controller) 30 { 31 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'metaGoogleProcessing', array()); 32 } 33 34 /** 35 * 36 * @param $event 37 */ 38 function metaGoogleProcessing($event) 39 { 40 41 42 try { 43 $templateForWebPage = ExecutionContext::getActualOrCreateFromEnv()->getExecutingPageTemplate(); 44 } catch (ExceptionNotFound $e) { 45 return; 46 } 47 48 if (!$templateForWebPage->isSocial()) { 49 return; 50 } 51 52 53 try { 54 $requestedPath = $templateForWebPage->getRequestedContextPath(); 55 } catch (ExceptionNotFound $e) { 56 LogUtility::internalError("Because the template is social, it should be at minima a path request"); 57 return; 58 } 59 60 61 $page = MarkupPath::createPageFromPathObject($requestedPath); 62 $ldJson = LdJson::createForPage($page) 63 ->getLdJsonMergedWithDefault(); 64 65 /** 66 * Publish 67 */ 68 if (!empty($ldJson)) { 69 $event->data["script"][] = array( 70 "type" => "application/ld+json", 71 "_data" => $ldJson, 72 ); 73 } 74 } 75 76 77} 78