merge opengraph fixes from dev

This commit is contained in:
Mario 2019-11-25 08:41:34 +00:00
parent 8727f12b18
commit 004861fbb7
3 changed files with 66 additions and 60 deletions

View File

@ -211,7 +211,7 @@ class Articles extends Controller {
}
// Add Opengraph markup
opengraph_add_meta(((! empty($items)) ? $r[0] : array()), App::$profile);
opengraph_add_meta((! empty($items) ? $r[0] : []), $channel);
$mode = 'articles';

View File

@ -110,8 +110,20 @@ class Channel extends Controller {
// Run profile_load() here to make sure the theme is set before
// we start loading content
profile_load($which,$profile);
// Add Opengraph markup
$mid = ((x($_REQUEST,'mid')) ? $_REQUEST['mid'] : '');
if(strpos($mid,'b64.') === 0)
$mid = @base64url_decode(substr($mid,4));
if($mid)
$r = q("SELECT * FROM item WHERE mid = '%s' AND uid = %d AND item_private = 0 LIMIT 1",
dbesc($mid),
intval($channel['channel_id'])
);
opengraph_add_meta($r ? $r[0] : [], $channel);
}
function get($update = 0, $load = false) {
@ -375,9 +387,6 @@ class Channel extends Controller {
$items = array();
}
// Add Opengraph markup
opengraph_add_meta((isset($decoded) && (! empty($items)) ? $r[0] : array()), App::$profile);
if((! $update) && (! $load)) {
if($decoded)

View File

@ -3,73 +3,70 @@
* @file include/opengraph.php
* @brief Add Opengraph metadata and related functions.
*/
/**
* @brief Adds Opengraph meta tags into HTML head
*
* @param array $item
* @param array $profile
* @param array $channel
*
*/
function opengraph_add_meta($item, $profile) {
if(! empty($item)) {
function opengraph_add_meta($item, $channel) {
if(! empty($item['title']))
$ogtitle = $item['title'];
if(! empty($item)) {
// find first image if exist
if(preg_match("/\[[zi]mg(=[0-9]+x[0-9]+)?\]([^\[]+)/is", $item['body'], $matches)) {
$ogimage = $matches[2];
$ogimagetype = guess_image_type($ogimage);
}
if(! empty($item['title']))
$ogtitle = $item['title'];
// use summary as description if exist
$ogdesc = (empty($item['summary']) ? $item['body'] : $item['summary'] );
$ogdesc = str_replace("#^[", "[", $ogdesc);
$ogdesc = bbcode($ogdesc, [ 'tryoembed' => false ]);
$ogdesc = trim(html2plain($ogdesc, 0, true));
$ogdesc = html_entity_decode($ogdesc, ENT_QUOTES, 'UTF-8');
// remove all URLs
$ogdesc = preg_replace("/https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,\@]+/", "", $ogdesc);
// shorten description
$ogdesc = substr($ogdesc, 0, 300);
$ogdesc = str_replace("\n", " ", $ogdesc);
while (strpos($ogdesc, " ") !== false)
$ogdesc = str_replace(" ", " ", $ogdesc);
$ogdesc = (strlen($ogdesc) < 298 ? $ogdesc : rtrim(substr($ogdesc, 0, strrpos($ogdesc, " ")), "?.,:;!-") . "...");
$ogtype = "article";
}
// find first image if exist
if(preg_match("/\[[zi]mg(=[0-9]+x[0-9]+)?\]([^\[]+)/is", $item['body'], $matches)) {
$ogimage = $matches[2];
$ogimagetype = guess_image_type($ogimage);
}
$channel = channelx_by_n($profile['profile_uid']);
// use summary as description if exist
$ogdesc = (empty($item['summary']) ? $item['body'] : $item['summary'] );
if(! isset($ogdesc)) {
if($profile['about'] && perm_is_allowed($channel['channel_id'],get_observer_hash(),'view_profile')) {
$ogdesc = $profile['about'];
}
else {
$ogdesc = sprintf( t('This is the home page of %s.'), $channel['channel_name']);
}
}
if(! isset($ogimage)) {
$ogimage = $channel['xchan_photo_l'];
$ogimagetype = $channel['xchan_photo_mimetype'];
}
$ogdesc = str_replace("#^[", "[", $ogdesc);
App::$page['htmlhead'] .= '<meta property="og:title" content="' . htmlspecialchars((isset($ogtitle) ? $ogtitle : $channel['channel_name'])) . '">' . "\r\n";
App::$page['htmlhead'] .= '<meta property="og:image" content="' . $ogimage . '">' . "\r\n";
App::$page['htmlhead'] .= '<meta property="og:image:type" content="' . $ogimagetype . '">' . "\r\n";
App::$page['htmlhead'] .= '<meta property="og:description" content="' . htmlspecialchars($ogdesc) . '">' . "\r\n";
App::$page['htmlhead'] .= '<meta property="og:type" content="' . (isset($ogtype) ? $ogtype : "profile") . '">' . "\r\n";
$ogdesc = bbcode($ogdesc, [ 'tryoembed' => false ]);
$ogdesc = trim(html2plain($ogdesc, 0, true));
$ogdesc = html_entity_decode($ogdesc, ENT_QUOTES, 'UTF-8');
return true;
// remove all URLs
$ogdesc = preg_replace("/https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,\@]+/", "", $ogdesc);
// shorten description
$ogdesc = substr($ogdesc, 0, 300);
$ogdesc = str_replace("\n", " ", $ogdesc);
while (strpos($ogdesc, " ") !== false)
$ogdesc = str_replace(" ", " ", $ogdesc);
$ogdesc = (strlen($ogdesc) < 298 ? $ogdesc : rtrim(substr($ogdesc, 0, strrpos($ogdesc, " ")), "?.,:;!-") . "...");
$ogtype = "article";
}
if(! isset($ogdesc)) {
if(App::$profile['about'] && perm_is_allowed($channel['channel_id'],get_observer_hash(),'view_profile')) {
$ogdesc = App::$profile['about'];
}
else {
$ogdesc = sprintf( t('This is the home page of %s.'), $channel['channel_name']);
}
}
if(! isset($ogimage)) {
$ogimage = $channel['xchan_photo_l'];
$ogimagetype = $channel['xchan_photo_mimetype'];
}
App::$page['htmlhead'] .= '<meta property="og:title" content="' . htmlspecialchars((isset($ogtitle) ? $ogtitle : $channel['channel_name'])) . '">' . "\r\n";
App::$page['htmlhead'] .= '<meta property="og:image" content="' . $ogimage . '">' . "\r\n";
App::$page['htmlhead'] .= '<meta property="og:image:type" content="' . $ogimagetype . '">' . "\r\n";
App::$page['htmlhead'] .= '<meta property="og:description" content="' . htmlspecialchars($ogdesc) . '">' . "\r\n";
App::$page['htmlhead'] .= '<meta property="og:type" content="' . (isset($ogtype) ? $ogtype : "profile") . '">' . "\r\n";
return true;
}