Merge branch 'dev' into 'dev'

Better Opengraph markup

See merge request hubzilla/core!1751
This commit is contained in:
Mario 2019-10-13 14:27:54 +02:00
commit e7e8a2ca5f
3 changed files with 86 additions and 18 deletions

View File

@ -9,6 +9,7 @@ use Zotlabs\Lib\PermissionDescription;
require_once('include/channel.php');
require_once('include/conversation.php');
require_once('include/acl_selectors.php');
require_once('include/opengraph.php');
class Articles extends Controller {
@ -192,7 +193,7 @@ class Articles extends Controller {
$parents_str = ids_to_querystr($r,'id');
$items = q("SELECT item.*, item.id AS item_id
$r = q("SELECT item.*, item.id AS item_id
FROM item
WHERE item.uid = %d $item_normal
AND item.parent IN ( %s )
@ -200,15 +201,18 @@ class Articles extends Controller {
intval(App::$profile['profile_uid']),
dbesc($parents_str)
);
if($items) {
xchan_query($items);
$items = fetch_post_tags($items, true);
if($r) {
xchan_query($r);
$items = fetch_post_tags($r, true);
$items = conv_sort($items,'updated');
}
else
$items = [];
}
// Add Opengraph markup
opengraph_add_meta(((! empty($items)) ? $r[0] : array()), App::$profile);
$mode = 'articles';
if(get_pconfig(local_channel(),'system','articles_list_mode') && (! $selected_card))

View File

@ -13,6 +13,7 @@ require_once('include/items.php');
require_once('include/security.php');
require_once('include/conversation.php');
require_once('include/acl_selectors.php');
require_once('include/opengraph.php');
/**
@ -111,17 +112,6 @@ class Channel extends Controller {
// we start loading content
profile_load($which,$profile);
App::$page['htmlhead'] .= '<meta property="og:title" content="' . htmlspecialchars($channel['channel_name']) . '">' . "\r\n";
App::$page['htmlhead'] .= '<meta property="og:image" content="' . $channel['xchan_photo_l'] . '">' . "\r\n";
if(App::$profile['about'] && perm_is_allowed($channel['channel_id'],get_observer_hash(),'view_profile')) {
App::$page['htmlhead'] .= '<meta property="og:description" content="' . htmlspecialchars(App::$profile['about']) . '">' . "\r\n";
}
else {
App::$page['htmlhead'] .= '<meta property="og:description" content="' . htmlspecialchars(sprintf( t('This is the home page of %s.'), $channel['channel_name'])) . '">' . "\r\n";
}
}
function get($update = 0, $load = false) {
@ -362,7 +352,7 @@ class Channel extends Controller {
$parents_str = ids_to_querystr($r,'item_id');
$items = q("SELECT item.*, item.id AS item_id
$r = q("SELECT item.*, item.id AS item_id
FROM item
WHERE item.uid = %d $item_normal
AND item.parent IN ( %s )
@ -371,8 +361,8 @@ class Channel extends Controller {
dbesc($parents_str)
);
xchan_query($items);
$items = fetch_post_tags($items, true);
xchan_query($r);
$items = fetch_post_tags($r, true);
$items = conv_sort($items,$ordering);
if($load && $mid && (! count($items))) {
@ -385,6 +375,9 @@ 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)

71
include/opengraph.php Normal file
View File

@ -0,0 +1,71 @@
<?php
/**
* @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
*
*/
function opengraph_add_meta($item, $profile) {
if(! empty($item)) {
if(! empty($item['title']))
$ogtitle = $item['title'];
// find first image if exist
if(preg_match("/\[[zi]mg(=[0-9]+x[0-9]+)?\]([^\[]+)/is", $item['body'], $matches))
$ogimage = $matches[2];
// 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 = rtrim(substr($ogdesc, 0, strrpos($ogdesc, " ")), "?.,:;!-") . "...";
$ogtype = "article";
}
$channel = channelx_by_n($profile['profile_uid']);
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'];
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="' . guess_image_type($ogimage) . '">' . "\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;
}