Skip to content

Commit

Permalink
Add support for OMP
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnyga committed Mar 29, 2020
1 parent 2e8e465 commit 15af139
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions OpenGraphPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function register($category, $path, $mainContextId = null) {
if ($this->getEnabled($mainContextId)) {
HookRegistry::register('ArticleHandler::view', array(&$this, 'submissionView'));
HookRegistry::register('PreprintHandler::view', array(&$this, 'submissionView'));
HookRegistry::register('CatalogBookHandler::book',array(&$this, 'submissionView'));
}
return true;
}
Expand All @@ -50,44 +51,66 @@ function submissionView($hookName, $args) {
$applicationName = $application->getName();
$request = $args[0];
$context = $request->getContext();
if ($applicationName == "ojs2"){
$issue = $args[1];
$submission = $args[2];
$submissionPath = 'article';
}
if ($applicationName == "ops"){
$submission = $args[1];
$submissionPath = 'preprint';
$submissionPath = array('preprint', 'view');
$objectType = "article";
}
elseif ($applicationName == "omp"){
$submission = $args[1];
$submissionPath = array('catalog', 'book');
$objectType = "book";
}
else {
$issue = $args[1];
$submission = $args[2];
$submissionPath = array('article', 'view');
$objectType = "article";
}

$templateMgr = TemplateManager::getManager($request);
$templateMgr->addHeader('openGraphSiteName', '<meta name="og:site_name" content="' . htmlspecialchars($context->getName($context->getPrimaryLocale())) . '"/>');
$templateMgr->addHeader('openGraphObjectType', '<meta name="og:type" content="article"/>');
$templateMgr->addHeader('openGraphObjectType', '<meta name="og:type" content="' . htmlspecialchars($objectType) . '"/>');
$templateMgr->addHeader('openGraphTitle', '<meta name="og:title" content="' . htmlspecialchars($submission->getFullTitle($submission->getLocale())) . '"/>');
if ($abstract = PKPString::html2text($submission->getAbstract($submission->getLocale()))) $templateMgr->addHeader('openGraphDescription', '<meta name="og:description" content="' . htmlspecialchars($abstract) . '"/>');
$templateMgr->addHeader('openGraphUrl', '<meta name="og:url" content="' . $request->url(null, $submissionPath, 'view', array($submission->getBestId())) . '"/>');
$templateMgr->addHeader('openGraphUrl', '<meta name="og:url" content="' . $request->url(null, $submissionPath[0], $submissionPath[1], array($submission->getBestId())) . '"/>');
if ($locale = $submission->getLocale()) $templateMgr->addHeader('openGraphLocale', '<meta name="og:locale" content="' . htmlspecialchars($locale) . '"/>');

$openGraphImage = "";
if ($contextPageHeaderLogo = $context->getLocalizedPageHeaderLogo()){
if ($contextPageHeaderLogo = $context->getLocalizedData('pageHeaderLogoImage')){
$openGraphImage = $templateMgr->getTemplateVars('publicFilesDir') . "/" . $contextPageHeaderLogo['uploadName'];
}
if ($issue && $issueCoverImage = $issue->getLocalizedCoverImageUrl()){
$openGraphImage = $issueCoverImage;
}
if ($submissionCoverImage = $submission->getLocalizedCoverImageUrl()){
if ($submissionCoverImage = $submission->getCurrentPublication()->getLocalizedCoverImageUrl($submission->getData('contextId'))){
$openGraphImage = $submissionCoverImage;
}
$templateMgr->addHeader('openGraphImage', '<meta name="og:image" content="' . htmlspecialchars($openGraphImage) . '"/>');

if ($datePublished = $submission->getDatePublished())$templateMgr->addHeader('openGraphDate', '<meta name="article:published_time" content="' . strftime('%Y-%m-%d', strtotime($datePublished)) . '"/>');
if ($datePublished = $submission->getDatePublished()) {
$openGraphDateName = $applicationName == "omp" ? "book:release_date" : "article:published_time";
$templateMgr->addHeader('openGraphDate', '<meta name="' . $openGraphDateName . '" content="' . strftime('%Y-%m-%d', strtotime($datePublished)) . '"/>');
}

if ($applicationName == "omp") {
$publicationFormats = $submission->getCurrentPublication()->getData('publicationFormats');
foreach ($publicationFormats as $publicationFormat) {
$identificationCodes = $publicationFormat->getIdentificationCodes();
while ($identificationCode = $identificationCodes->next()) {
if ($identificationCode->getCode() == "02" || $identificationCode->getCode() == "15") {
$templateMgr->addHeader('openGraphBookIsbn', '<meta name="book:isbn" content="' . htmlspecialchars($identificationCode->getValue()) . '"/>');
}
}
}
}

$i=0;
$dao = DAORegistry::getDAO('SubmissionKeywordDAO');
$keywords = $dao->getKeywords($submission->getCurrentPublication()->getId(), array(AppLocale::getLocale()));
foreach ($keywords as $locale => $localeKeywords) {
foreach ($localeKeywords as $keyword) {
$templateMgr->addHeader('openGraphArticleTag' . $i++, '<meta name="article:tag" content="' . htmlspecialchars($keyword) . '"/>');
$templateMgr->addHeader('openGraphArticleTag' . $i++, '<meta name="' . $objectType . ':tag" content="' . htmlspecialchars($keyword) . '"/>');
}
}

Expand Down

0 comments on commit 15af139

Please sign in to comment.