Skip to content

Instantly share code, notes, and snippets.

@Gummibeer
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gummibeer/2a11df6022e5bbbf13df to your computer and use it in GitHub Desktop.
Save Gummibeer/2a11df6022e5bbbf13df to your computer and use it in GitHub Desktop.
narcis-radu/pico-share Issue #1 https://github.com/narcis-radu/pico-share/issues/1
<?php
/**
* Social plugin for Pico CMS
* Adds social media buttons to posts and pages
*
* @author Narcis Radu
* @link http://narcisradu.ro
* @license http://opensource.org/licenses/MIT
*
* @author Tom Witkowski
* @link https://github.com/Gummibeer
*/
class Pico_Share {
public $templates = array(
'twitter' => 'https://twitter.com/intent/tweet?text=__TITLE__&url=__URL__',
'facebook' => 'https://www.facebook.com/sharer/sharer.php?u=__URL__',
'google' => 'https://plus.google.com/share?url=__URL__',
'linkedin' => 'http://www.linkedin.com/shareArticle?mini=true&url=__URL__&title=__TITLE__&summary=__EXCERPT__&source=__URL__'
);
public function __construct() {
$this->config = array(
'services' => array(
'twitter' => true,
'facebook' => true,
'google' => true,
'linkedin' => true
),
'output' => 'list',
'container_class' => '', // container class
'listitem_class' => '', // listitem class
'class_prefix' => 'btn-', // link class
);
}
public function config_loaded(&$settings) {
if(isset($settings['social']['services'])) {
$this->config['services'] = $settings['social']['services'];
}
if(isset($settings['social']['output'])) {
$this->config['output'] = $settings['social']['output'];
}
if(isset($settings['social']['container_class'])) {
$this->config['container_class'] = $settings['social']['container_class'];
}
if(isset($settings['social']['listitem_class'])) {
$this->config['listitem_class'] = $settings['social']['listitem_class'];
}
if(isset($settings['social']['class_prefix'])) {
$this->config['class_prefix'] = $settings['social']['class_prefix'];
}
}
public function before_render(&$twig_vars, &$twig, &$template) {
$pageTitle = $twig_vars['current_page']['title'];
$pageURL = $twig_vars['current_page']['url'];
$pageExcerpt = $twig_vars['current_page']['excerpt'];
$activeServices = array();
foreach($this->config['services'] as $key => $value) {
if(is_bool($value) && $value) {
$activeServices[$key] = '<a class="'.$this->config['class_prefix'].$key.'" target="_blank" href="'.
preg_replace(array('/__TITLE__/', '/__URL__/', '/__EXCERPT__/'), array($pageTitle, $pageURL, $pageExcerpt), $this->templates[$key]).
'">'.$key.'</a>';
};
};
switch($this->config['output']) {
case 'link':
$twig_vars['social_share'] = '<div id="share" class="'.$this->config['container_class'].'">'.implode('', array_values($activeServices)).'</div>';
break;
default:
$twig_vars['social_share'] = '<ul id="share" class="'.$this->config['container_class'].'"><li class="'.$this->config['listitem_class'].'">'.implode('</li><li class="'.$this->config['listitem_class'].'">', array_values($activeServices)).'</li></ul>';
break;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment