Last active
February 7, 2016 14:18
-
-
Save Vyygir/fe2ae0f2b25434b6c70f to your computer and use it in GitHub Desktop.
Post Sharing for Programmers original class before plugin creation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A class to enable easier generation of sharing links per service, rather than | |
* having to hard-code them in templates | |
**/ | |
global $sharing_links; | |
class Sharing_Links { | |
private $title; | |
private $link; | |
private $services = array(); | |
public function __construct() { | |
$this->addService('email', 'mailto:?subject={title}&body={permalink}'); | |
$this->addService('facebook', 'https://www.facebook.com/sharer/sharer.php?u={permalink}'); | |
$this->addService('twitter', 'https://twitter.com/home?status={title} {permalink}'); | |
$this->addService('plus', 'https://plus.google.com/share?url={permalink}'); | |
$this->addService('linkedin', 'https://www.linkedin.com/shareArticle?mini=true&url={permalink}&title={title}'); | |
} | |
public function setTitle($title) { | |
$this->title = $title; | |
return $this; | |
} | |
public function setLink($link) { | |
$this->link = $link; | |
return $this; | |
} | |
public function addService($name, $url) { | |
$this->services[$name] = $url; | |
} | |
public function getService($name) { | |
return isset($this->services[$name]) ? $this->applyReplacement($this->services[$name]) : null; | |
} | |
public function getNonService($url, $name = '', $add = false) { | |
if (true === $add && $name != '') { | |
$this->addService($name, $url); | |
$service = $this->applyReplacement($this->services[$name]); | |
} else { | |
$service = $this->applyReplacement($url); | |
} | |
return $service; | |
} | |
public function getServices() { | |
$services = $this->services; | |
foreach ($services as &$service) { | |
$service = $this->applyReplacement($service); | |
} | |
return (object) $services; | |
} | |
private function applyReplacement($url) { | |
return str_replace(array('{title}', '{permalink}'), array($this->title, $this->link), $url); | |
} | |
} | |
$sharing_links = new Sharing_Links(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment