2016-06-21 14:08:20 +02:00
< ? php
if ( IN_serendipity !== true ) {
die ( " Don't hack! " );
}
@ serendipity_plugin_api :: load_language ( dirname ( __FILE__ ));
class serendipity_event_social extends serendipity_event {
var $title = PLUGIN_EVENT_SOCIAL_NAME ;
function introspect ( & $propbag ) {
global $serendipity ;
$propbag -> add ( 'name' , PLUGIN_EVENT_SOCIAL_NAME );
$propbag -> add ( 'description' , PLUGIN_EVENT_SOCIAL_DESC );
$propbag -> add ( 'stackable' , false );
2016-09-04 16:12:18 +02:00
$propbag -> add ( 'author' , 'onli, Matthias Mees, Thomas Hochstein' );
2020-04-18 19:09:48 +02:00
$propbag -> add ( 'version' , '0.14.1' );
2016-06-21 14:08:20 +02:00
$propbag -> add ( 'requirements' , array (
'serendipity' => '2.0'
));
$propbag -> add ( 'event_hooks' , array ( 'frontend_display:html:per_entry' => true ,
'css' => true ,
2016-06-22 21:29:36 +02:00
'frontend_footer' => true ,
2020-03-27 22:18:39 +01:00
'frontend_header' => true ,
'backend_display' => true ,
'backend_publish' => true ,
'backend_save' => true ));
2016-06-21 14:08:20 +02:00
$propbag -> add ( 'groups' , array ( 'FRONTEND_EXTERNAL_SERVICES' ));
2016-08-12 15:53:45 +02:00
$propbag -> add ( 'configuration' , array ( 'services' , 'theme' , 'overview' , 'twitter_via' , 'social_image' , 'lang' , 'backend' ));
2018-04-05 14:17:19 +02:00
$propbag -> add ( 'legal' , array (
'services' => array (
'Multiple' => array (
'url' => 'https://github.com/heiseonline/shariff' ,
'desc' => 'All supported social platforms can receive user data and metadata (IP, cookies)'
),
's9y Shariff' => array (
'url' => 'https://onli.columba.uberspace.de/s9y_shariff/' ,
'desc' => 'When enabled, this shariff backend will receive metadata of URL requests'
)
),
'frontend' => array (
'When sharing functions of the plugin are used by the visitor, those selected sharing services will receive the URL and the metadata of the visitor (IP, User Agent, Referrer, etc.).' ,
),
'backend' => array (
),
'cookies' => array (
),
'stores_user_input' => false ,
'stores_ip' => false ,
'uses_ip' => false ,
'transmits_user_input' => true
));
2016-06-21 14:08:20 +02:00
}
function generate_content ( & $title ) {
$title = $this -> title ;
}
function introspect_config_item ( $name , & $propbag ) {
global $serendipity ;
switch ( $name ) {
case 'services' :
$propbag -> add ( 'type' , 'multiselect' );
$propbag -> add ( 'name' , PLUGIN_EVENT_SOCIAL_SERVICES );
$propbag -> add ( 'description' , PLUGIN_EVENT_SOCIAL_SERVICES_DESC );
2019-04-05 08:29:07 +02:00
$propbag -> add ( 'default' , 'twitter^facebook' );
$propbag -> add ( 'select_values' , array ( 'twitter' => 'twitter' , 'facebook' => 'facebook' , 'linkedin' => 'linkedin' , 'pinterest' => 'pinterest' , 'xing' => 'xing' , 'whatsapp' => 'whatsapp' , 'mail' => 'mail' , 'info' => 'info' , 'addthis' => 'addthis' , 'tumblr' => 'tumblr' , 'flattr' => 'flattr' , 'diaspora' => 'diaspora' , 'reddit' => 'reddit' , 'stumbleupon' => 'stumbleupon' , 'threema' => 'threema' , 'weibo' => 'weibo' , 'tencent-weibo' => 'tencent-weibo' , 'qzone' => 'qzone' , 'print' => 'print' , 'telegram' => 'telegram' , 'vk' => 'vk' , 'flipboard' => 'flipboard' ));
2016-06-21 14:08:20 +02:00
break ;
2016-06-21 17:27:05 +02:00
case 'theme' :
$propbag -> add ( 'type' , 'select' );
$propbag -> add ( 'name' , PLUGIN_EVENT_SOCIAL_THEME );
$propbag -> add ( 'description' , PLUGIN_EVENT_SOCIAL_THEME_DESC );
$propbag -> add ( 'select_values' , array ( 'standard' => PLUGIN_EVENT_SOCIAL_THEME_STD , 'white' => PLUGIN_EVENT_SOCIAL_THEME_WHITE , 'grey' => PLUGIN_EVENT_SOCIAL_THEME_GREY ));
$propbag -> add ( 'default' , 'standard' );
break ;
2016-06-21 14:08:20 +02:00
case 'overview' :
$propbag -> add ( 'type' , 'boolean' );
$propbag -> add ( 'name' , PLUGIN_EVENT_SOCIAL_OVERVIEW );
$propbag -> add ( 'description' , PLUGIN_EVENT_SOCIAL_OVERVIEW_DESC );
$propbag -> add ( 'default' , true );
2016-06-22 21:29:36 +02:00
break ;
case 'twitter_via' :
$propbag -> add ( 'type' , 'string' );
$propbag -> add ( 'name' , PLUGIN_EVENT_SOCIAL_TWITTERVIA );
$propbag -> add ( 'description' , PLUGIN_EVENT_SOCIAL_TWITTERVIA_DESC );
$propbag -> add ( 'default' , 'none' );
break ;
case 'lang' :
$propbag -> add ( 'type' , 'select' );
$propbag -> add ( 'name' , INSTALL_LANG );
$propbag -> add ( 'description' , PLUGIN_EVENT_SOCIAL_LANG_DESC );
$propbag -> add ( 'default' , 'en' );
$propbag -> add ( 'select_values' , array ( 'bg' => 'bg' , 'de' => 'de' , 'en' => 'en' , 'es' => 'es' , 'fi' => 'fi' , 'hr' => 'hr' , 'hu' => 'hu' , 'ja' => 'ja' , 'ko' => 'ko' , 'no' => 'no' , 'pl' => 'pl' , 'pt' => 'pt' , 'ro' => 'ro' , 'ru' => 'ru' , 'sk' => 'sk' , 'sl' => 'sl' , 'sr' => 'sr' , 'sv' => 'sv' , 'tr' => 'tr' , 'zh' => 'zh' ));
break ;
2016-06-25 03:44:07 +02:00
case 'backend' :
$propbag -> add ( 'type' , 'string' );
$propbag -> add ( 'name' , PLUGIN_EVENT_SOCIAL_BACKEND );
$propbag -> add ( 'description' , PLUGIN_EVENT_SOCIAL_BACKEND_DESC );
$propbag -> add ( 'default' , 'https://onli.columba.uberspace.de/s9y_shariff/' );
break ;
2016-08-12 15:53:45 +02:00
case 'social_image' :
$propbag -> add ( 'type' , 'media' );
$propbag -> add ( 'name' , PLUGIN_EVENT_SOCIAL_IMAGE );
$propbag -> add ( 'description' , PLUGIN_EVENT_SOCIAL_IMAGE_DESC );
$propbag -> add ( 'default' , '' );
break ;
2016-06-21 14:08:20 +02:00
}
return true ;
}
2019-04-05 08:29:07 +02:00
function performConfig ( & $bag ) {
// remove googleplus from config
$services = $this -> get_config ( 'services' );
if ( strpos ( $services , 'googleplus' ) !== false ) {
$services = preg_replace ( '/\^?googleplus/' , '' , $services );
$this -> set_config ( 'services' , $services );
}
}
2016-06-21 14:08:20 +02:00
function event_hook ( $event , & $bag , & $eventData , $addData = null ) {
global $serendipity ;
$hooks = & $bag -> get ( 'event_hooks' );
if ( isset ( $hooks [ $event ])) {
switch ( $event ) {
case 'frontend_display:html:per_entry' :
2016-06-22 21:29:36 +02:00
if ( $serendipity [ 'view' ] != 'entry' ) {
if ( ! serendipity_db_bool ( $this -> get_config ( 'overview' , true ))) {
// We are in overview mode and the user opted to not show the button
return true ;
}
// when sharing on the frontpage, at least the twitter button is using the page title instead of the entry title, so we set that manually
$hardcoded_title = ' data-title="' . $eventData [ 'title' ] . '"' ;
}
$twitter_via = $this -> get_config ( 'twitter_via' , 'none' );
if ( $twitter_via != 'none' ) {
2016-08-16 20:28:38 +02:00
$twitter_via_tag = ' data-twitter-via="' . str_replace ( '@' , '' , $twitter_via ) . '"' ;
2016-06-21 14:08:20 +02:00
}
2016-06-25 03:44:07 +02:00
$backend = $this -> get_config ( 'backend' , 'https://onli.columba.uberspace.de/s9y_shariff/' );
if ( $backend != 'none' ) {
$backend_tag = ' data-backend-url="' . $backend . '"' ;
}
2016-06-22 21:29:36 +02:00
$theme = $this -> get_config ( 'theme' );
$lang = $this -> get_config ( 'lang' , 'en' );
2016-06-21 14:08:20 +02:00
$services = $this -> get_config ( 'services' );
2019-04-05 08:29:07 +02:00
# remove googleplus from services
if ( strpos ( $services , 'googleplus' ) !== false ) {
$services = preg_replace ( '/\^?googleplus/' , '' , $services );
}
2016-06-21 17:57:24 +02:00
$services = '"' . str_replace ( '^' , '","' , $services ) . '"' ;
if ( strpos ( $services , 'info' ) !== false ) {
// the info button looks strange if not at the end, hardcode that position
$services = str_replace ( ',"info"' , '' , $services ) . ',"info"' ;
}
2018-04-22 23:18:49 +02:00
2016-06-25 03:44:07 +02:00
$eventData [ 'display_dat' ] = '<div class="shariff" data-url="' . $eventData [ 'rdf_ident' ] . '" data-services="[' . $services . ']" data-lang="' . $lang . '" data-theme="' . $theme . '" data-mail-url="mailto:foo@example.org"' . $hardcoded_title . $twitter_via_tag . $backend_tag . '></div>' ;
2016-06-21 14:08:20 +02:00
break ;
2018-04-22 23:18:49 +02:00
2016-06-21 14:08:20 +02:00
case 'css' :
$eventData .= file_get_contents ( dirname ( __FILE__ ) . '/shariff.complete.css' );
break ;
2018-04-22 23:18:49 +02:00
2016-06-21 14:08:20 +02:00
case 'frontend_footer' :
// this script should go into the JS hook, but it has to be at the bottom to work, and the js hook places it at the top
2016-06-21 17:36:56 +02:00
echo '<script src="' . $serendipity [ 'serendipityHTTPPath' ] . 'plugins/serendipity_event_social/shariff.min.js' . '"></script>' ;
2016-06-21 14:08:20 +02:00
break ;
2018-04-22 23:18:49 +02:00
2016-06-22 21:29:36 +02:00
case 'frontend_header' :
if ( $serendipity [ 'view' ] != 'entry' ) {
return true ;
}
2016-08-08 23:54:30 +02:00
// Facebook & Twitter can profit from having the og-properties set
if ( strpos ( $this -> get_config ( 'services' ), 'facebook' ) !== false || strpos ( $this -> get_config ( 'services' ), 'twitter' ) !== false ) {
2018-04-22 23:18:49 +02:00
2016-06-22 21:29:36 +02:00
// we iterate over the internal smarty object to see which entry we are printing. This is hacky and should be improved
$entry = ( current ( $eventData [ 'smarty' ] -> tpl_vars [ 'entries' ] -> value )[ 'entries' ][ 0 ]);
2016-08-12 15:53:45 +02:00
$blogURL = 'http' . ( $_SERVER [ 'HTTPS' ] ? 's' : '' ) . '://' . $_SERVER [ 'HTTP_HOST' ];
2018-04-22 23:18:49 +02:00
2016-06-22 21:29:36 +02:00
echo '<!--serendipity_event_shariff-->' . " \n " ;
2016-08-08 23:54:30 +02:00
echo '<meta name="twitter:card" content="summary" />' . " \n " ;
2016-06-22 21:29:36 +02:00
echo '<meta property="og:title" content="' . serendipity_specialchars ( $entry [ 'title' ]) . '" />' . " \n " ;
2016-08-31 19:55:02 +02:00
# get desciption from serendipity_event_metadesc, if set; take first 200 chars from body otherwise
2017-05-16 22:55:46 +02:00
$meta_description = strip_tags ( $entry [ 'properties' ][ 'meta_description' ]);
2016-08-31 19:55:02 +02:00
if ( empty ( $meta_description )) {
# /\s+/: multiple newline and whitespaces
$meta_description = trim ( preg_replace ( '/\s+/' , ' ' , substr ( strip_tags ( $entry [ 'body' ]), 0 , 200 ))) . '...' ;
}
2016-09-07 23:20:37 +02:00
echo '<meta property="og:description" content="' . serendipity_specialchars ( $meta_description ) . '" />' . " \n " ;
2016-06-22 21:29:36 +02:00
echo '<meta property="og:type" content="article" />' . " \n " ;
echo '<meta property="og:site_name" content="' . $serendipity [ 'blogTitle' ] . '" />' . " \n " ;
2016-08-12 15:53:45 +02:00
echo '<meta property="og:url" content="' . $blogURL . serendipity_specialchars ( $_SERVER [ 'REQUEST_URI' ]) . '" />' . " \n " ;
2016-06-22 21:29:36 +02:00
2020-03-27 22:18:39 +01:00
// set default image from plugin configuration
2016-09-07 23:20:37 +02:00
$social_image = $this -> get_config ( 'social_image' , '' );
2020-03-27 22:18:39 +01:00
if ( isset ( $entry [ 'properties' ]) && isset ( $entry [ 'properties' ][ 'entry_image' ])) {
// if entry_image is set, use this image instead (first priority)
$social_image = $entry [ 'properties' ][ 'entry_image' ];
} else if ( isset ( $entry [ 'properties' ]) && isset ( $entry [ 'properties' ][ 'timeline_image' ])) {
// if timeline_image from timeline theme is set, use this image (second priority)
2016-09-07 23:20:37 +02:00
$social_image = $entry [ 'properties' ][ 'timeline_image' ];
2016-09-08 00:05:42 +02:00
} else if ( isset ( $entry [ 'properties' ]) && isset ( $entry [ 'properties' ][ 'ep_featuredImage' ])) {
2020-03-27 22:18:39 +01:00
// if ep_featuredImage from photo theme is set, use this image (third priority)
2016-09-08 00:05:42 +02:00
$social_image = $entry [ 'properties' ][ 'ep_featuredImage' ];
2016-09-07 23:20:37 +02:00
} else {
2020-03-27 22:18:39 +01:00
// Fourth priority:
2016-09-07 23:20:37 +02:00
// This is searching for the first image in an entry to use as facebook article image.
// A better approach would be to register in the entry editor when an image was added
if ( preg_match_all ( '@<img.*src=["\'](.+)["\']@imsU' , $entry [ 'body' ] . $entry [ 'extended' ], $images )) {
2016-09-07 23:24:55 +02:00
foreach ( $images [ 1 ] as $im ) {
2016-09-07 23:20:37 +02:00
if ( strpos ( $im , '/emoticons/' ) === false ) {
$social_image = $im ;
2016-09-07 23:24:55 +02:00
break ;
2016-09-07 23:20:37 +02:00
}
}
}
}
if ( ! preg_match ( '/^http/i' , $social_image )) {
$social_image = $blogURL . $social_image ;
2016-06-22 21:29:36 +02:00
}
2018-04-22 23:18:49 +02:00
2016-08-12 15:53:45 +02:00
if ( $social_image != $blogURL && $social_image != $blogURL . 'none' ) {
echo '<meta property="og:image" content="' . $social_image . '" />' . " \n " ;
}
2016-06-22 21:29:36 +02:00
}
2016-08-16 20:28:38 +02:00
break ;
2020-03-27 22:18:39 +01:00
case 'backend_display' :
if ( isset ( $serendipity [ 'POST' ][ 'properties' ][ 'entry_image' ])) {
$entry_image = $serendipity [ 'POST' ][ 'properties' ][ 'entry_image' ];
} elseif ( ! empty ( $eventData [ 'properties' ][ 'entry_image' ])) {
$entry_image = $eventData [ 'properties' ][ 'entry_image' ];
} else {
$entry_image = '' ;
}
?>
< div class = " social_entry_image adv_opts_box form_field " >
< div class = " clearfix form_area media_choose " id = " ep_column_entry_image " >
< label for = " properties_entry_image " >< ? php echo PLUGIN_EVENT_SOCIAL_ENTRY_IMAGE ; ?> :</label>
< textarea data - configitem = " properties_entry_image " name = " serendipity[properties][entry_image] " class = " change_preview " id = " properties_entry_image " style = " width: 100% " >< ? php echo serendipity_specialchars ( $entry_image ); ?> </textarea>
< button class = " customfieldMedia " type = " button " name = " insImage " title = " <?php echo MEDIA ; ?> " >< span class = " icon-picture " aria - hidden = " true " ></ span >< span class = " visuallyhidden " >< ? php echo MEDIA ; ?> </span></button>
< figure id = " properties_entry_image_preview " >
< figcaption >< ? php echo PREVIEW ; ?> </figcaption>
< img src = " <?php echo $entry_image ; ?> " alt = " " />
</ figure >
</ div >
</ div >
< ? php
return true ;
break ;
case 'backend_publish' :
case 'backend_save' :
if ( ! isset ( $serendipity [ 'POST' ][ 'properties' ]) || ! is_array ( $serendipity [ 'POST' ][ 'properties' ]) || ! isset ( $eventData [ 'id' ])) {
return true ;
}
$entry_image = $serendipity [ 'POST' ][ 'properties' ][ 'entry_image' ];
2020-04-18 19:09:48 +02:00
// don't change anything if entry_image is not set
if ( ! isset ( $entry_image )) {
return true ;
}
2020-03-27 22:18:39 +01:00
// delete old entry, if any
$q = " DELETE FROM { $serendipity [ 'dbPrefix' ] } entryproperties WHERE entryid = " . ( int ) $eventData [ 'id' ] . " AND property = 'entry_image' " ;
serendipity_db_query ( $q );
// save new entry
if ( ! empty ( $entry_image )) {
$q = " INSERT INTO { $serendipity [ 'dbPrefix' ] } entryproperties (entryid, property, value) VALUES ( " . ( int ) $eventData [ 'id' ] . " , 'entry_image', ' " . serendipity_db_escape_string ( $entry_image ) . " ') " ;
serendipity_db_query ( $q );
}
2016-06-21 14:08:20 +02:00
default :
return false ;
}
} else {
return false ;
}
}
}
/* vim: set sts=4 ts=4 expandtab : */
2016-06-21 16:58:42 +02:00
?>