Source of file ShareThisSimpleProvider.php
Size: 17,065 Bytes - Last Modified: 2021-12-23T10:46:29+00:00
/var/www/docs.ssmods.com/process/src/src/Api/ShareThisSimpleProvider.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 | <?php namespace Sunnysideup\ShareThisSimple\Api; use SilverStripe\Core\Config\Config; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\View\ArrayData; use SilverStripe\View\ViewableData; class ShareThisSimpleProvider extends ViewableData { /** * @var null|DataObject */ protected $object; protected $linkMethod = 'AbsoluteLink'; protected $titleMethod = 'Title'; protected $imageMethods = []; protected $descriptionMethod = 'SocialMediaDescription'; //change to 'SocialMediaDescription' protected $hashTagArray = []; protected $mentionsArray = []; protected $viasArray = []; /** * @var string */ protected $pageURL = ''; /** * @var string */ protected $title = ''; /** * @var string */ protected $titleFull = ''; /** * @var string */ protected $media = ''; /** * @var string */ protected $description = ''; /** * @var string */ protected $descriptionFull = ''; /** * @var string */ protected $hashTags = ''; /** * @var string */ protected $mentions = ''; /** * @var string */ protected $vias = ''; protected static $pop_up_window_height = 320; protected static $pop_up_window_width = 200; protected static $cacheGetShareThisArray = []; private static $description_method = ''; private static $default_mentions = []; private static $default_vias = []; private static $default_hash_tags = []; private static $image_methods = []; private static $casting = [ 'FacebookShareLink' => 'Varchar', 'TwitterShareLink' => 'Varchar', 'TumblrShareLink' => 'Varchar', 'PinterestShareLink' => 'Varchar', 'EmailShareLink' => 'Varchar', 'RedditShareLink' => 'Varchar', 'PinterestLinkForSpecificImage' => 'Varchar', ]; /** * @param DataObject $object */ public function __construct($object) { parent::__construct(); $this->object = $object; } public function setLinkMethod(string $s): self { $this->linkMethod = $s; return $this; } public function setTitleMethod(string $s): self { $this->titleMethod = $s; return $this; } public function setImageMethods(string $a): self { $this->imageMethods = $a; return $this; } public function setDescriptionMethod(string $s): self { $this->descriptionMethod = $s; return $this; } public function setHashTags(array $a): self { $this->hashTagsArray = $a; return $this; } public function setMentions(array $a): self { $this->mentionsArray = $a; return $this; } public function setVias(array $a): self { $this->viasArray = $a; return $this; } public function getWindowPopupHtml(): string { $width = $this->Config()->get('pop_up_window_width'); $height = $this->Config()->get('pop_up_window_height'); $html = <<<html onclick="window.open(this.href,'Share','width={$width},height={$height},toolbar=no,menubar=no,location=no,status=no,scrollbars=no,resizable=yes'); return '';" html; $html = preg_replace('#\s+#', ' ', $html); return DBField::create_field('HTMLText', $html); } /** * return of ShareThisLinks. * * @param string $customDescription e.g. foo bar cool stuff */ public function ShareThisLinks(?string $customDescription = ''): ArrayList { $arrayList = ArrayList::create(); $options = array_keys(Config::inst()->get(ShareThisSimpleProvider::class, 'casting', Config::UNINHERITED)); foreach ($options as $option) { $className = str_replace('ShareLink', '', $option); $className = strtolower($className); $method = 'get' . $option; $arrayList->push( ArrayData::create( [ 'Class' => $className, 'Link' => $this->{$method}($customDescription), ] ) ); } return $arrayList; } /** * ALIAS * Generate a URL to share this content on Facebook. * * @param string $customDescription e.g. foo bar cool stuff */ public function FacebookShareLink(?string $customDescription = ''): string { return $this->getFacebookShareLink($customDescription); } /** * Generate a URL to share this content on Facebook. * * @param string $customDescription e.g. foo bar cool stuff * https://www.facebook.com/dialog/feed? * &link=URL_HERE * &picture=IMAGE_LINK_HERE * &name=TITLE_HERE * &caption=%20 * &description=DESCRIPTION_HERE * &redirect_uri=http%3A%2F%2Fwww.facebook.com%2F */ public function getFacebookShareLink(?string $customDescription = ''): string { $this->getShareThisArray($customDescription); return '' !== $this->pageURL ? 'https://www.facebook.com/sharer/sharer.php?u=' . $this->pageURL . '&t=' . $this->title . '' : ''; } /** * ALIAS * Generate a URL to share this content on Twitter * Specs: https://dev.twitter.com/web/tweet-button/web-intent. * * @param string $customDescription e.g. foo bar cool stuff */ public function TwitterShareLink(?string $customDescription = ''): string { return $this->getTwitterShareLink($customDescription); } /** * Generate a URL to share this content on Twitter * Specs: https://dev.twitter.com/web/tweet-button/web-intent. * example: https://twitter.com/intent/tweet? * &source=http%3A%2F%2Fsunnysideup.co.nz * &text=test:%20http%3A%2F%2Fsunnysideup.co.nz * &via=foobar. * * @param string $customDescription e.g. foo bar cool stuff */ public function getTwitterShareLink(?string $customDescription = ''): string { $this->getShareThisArray($customDescription); return '' !== $this->pageURL ? 'https://twitter.com/intent/tweet?source=' . $this->pageURL . '&text=' . $this->titleFull . '' . urlencode(': ') . $this->pageURL : ''; } /** * ALIAS * Generate a URL to share this content on Twitter * Specs: https://dev.twitter.com/web/tweet-button/web-intent. * * @param string $customDescription e.g. foo bar cool stuff */ public function LinkedInShareLink(?string $customDescription = ''): string { return $this->getLinkedInShareLink($customDescription); } /** * Generate a URL to share this content on Twitter * Specs: ??? * example: https://www.linkedin.com/shareArticle? * mini=true&url=http://www.cnn.com&title=&summary=chek this out&source=. * * @param string $customDescription e.g. foo bar cool stuff */ public function getLinkedInShareLink(?string $customDescription = ''): string { $this->getShareThisArray($customDescription); return '' !== $this->pageURL ? 'https://www.linkedin.com/shareArticle?mini=true&url=' . $this->pageURL . '&summary=' . $this->titleFull . '' : ''; } /** * ALIAS * Generate a URL to share this content on Twitter * Specs: https://dev.twitter.com/web/tweet-button/web-intent. * * @param string $customDescription e.g. foo bar cool stuff */ public function TumblrShareLink(?string $customDescription = ''): string { return $this->getTumblrShareLink($customDescription); } /** * Generate a URL to share this content on Twitter * Specs: https://dev.twitter.com/web/tweet-button/web-intent. * * @param string $customDescription e.g. foo bar cool stuff * * @return string */ public function getTumblrShareLink(?string $customDescription = '') { $this->getShareThisArray($customDescription); return '' !== $this->pageURL ? 'http://www.tumblr.com/share/link?url=' . $this->pageURL . '&name=' . $this->title . '&description=' . $this->description . '' : ''; } /** * ALIAS * Generate a URL to share this content on Twitter * Specs: https://dev.twitter.com/web/tweet-button/web-intent. * * @param string $customDescription e.g. foo bar cool stuff */ public function PinterestShareLink(?string $customDescription = ''): string { return $this->getPinterestShareLink($customDescription); } /** * Generate a URL to share this content on Twitter * Specs: https://dev.twitter.com/web/tweet-button/web-intent. * * @param string $customDescription e.g. foo bar cool stuff */ public function getPinterestShareLink(?string $customDescription = ''): string { $this->getShareThisArray($customDescription); return '' !== $this->pageURL ? 'http://pinterest.com/pin/create/button/?url=' . $this->pageURL . '&description=' . $this->description . '&media=' . $this->media . '' : ''; } /** * ALIAS * Generate a 'mailto' URL to share this content via Email. * * @param string $customDescription e.g. foo bar cool stuff */ public function EmailShareLink(?string $customDescription = ''): string { return $this->getEmailShareLink($customDescription); } /** * Generate a 'mailto' URL to share this content via Email. * * @param string $customDescription e.g. foo bar cool stuff */ public function getEmailShareLink(?string $customDescription = ''): string { $this->getShareThisArray($customDescription); return '' !== $this->pageURL ? 'mailto:?subject=' . $this->title . '&body=' . $this->pageURL . '' : ''; } /** * ALIAS * Generate a URL to share this content on Twitter * Specs: https://dev.twitter.com/web/tweet-button/web-intent. * * @param string $customDescription e.g. foo bar cool stuff */ public function RedditShareLink(?string $customDescription = ''): string { return $this->getRedditShareLink($customDescription); } /** * Generate a URL to share this content on Twitter * Specs: https://dev.twitter.com/web/tweet-button/web-intent. * * @param string $customDescription e.g. foo bar cool stuff */ public function getRedditShareLink(?string $customDescription = ''): string { $this->getShareThisArray($customDescription); return '' !== $this->pageURL ? 'http://reddit.com/submit?url=' . $this->pageURL . '&title=' . $this->title . '' : ''; } /** * @param string $customDescription e.g. foo bar cool stuff */ public function getShareThisArray(?string $customDescription = ''): array { $cacheKey = $this->object->ID . '_' . preg_replace('#[^A-Za-z0-9]#', '_', $customDescription); if (! isset(self::$cacheGetShareThisArray[$cacheKey])) { //1. link $this->link = $this->shareThisLinkField(); $this->title = $this->shareThisTitleField(); $this->media = $this->shareThisMediaField(); $this->description = $this->shareThisDescriptionField($customDescription); $this->hashTags = $this->getValuesFromArrayToString('hashTagsArray', 'hash_tags', '#'); $this->mentions = $this->getValuesFromArrayToString('mentionsArray', 'mentions'); $this->vias = $this->getValuesFromArrayToString('viasArray', 'vias'); $this->titleFull = trim($this->mentions . ' ' . $this->title . ' ' . $this->hashTags . ' ' . $this->vias); $this->descriptionFull = trim($this->mentions . ' ' . $this->description . ' ' . $this->hashTags . ' ' . $this->vias); //return ... self::$cacheGetShareThisArray[$cacheKey] = [ 'pageURL' => rawurlencode($this->link), 'title' => rawurlencode($this->title), 'titleFull' => rawurlencode($this->titleFull), 'media' => rawurlencode($this->media), 'description' => rawurlencode($this->description), 'descriptionFull' => rawurlencode($this->descriptionFull), 'hashTags' => rawurlencode($this->hashTags), 'mentions' => rawurlencode($this->mentions), 'vias' => rawurlencode($this->vias), ]; } foreach (self::$cacheGetShareThisArray[$cacheKey] as $field => $value) { $this->{$field} = $value; } return self::$cacheGetShareThisArray[$cacheKey]; } /** * @param string $imageMethod e.g. MyImage * @param bool $useImageTitle if set to false, it will use the page title as the image title */ public function PinterestLinkForSpecificImage(string $imageMethod, ?bool $useImageTitle = false): string { return $this->getPinterestLinkForSpecificImage( $imageMethod, $useImageTitle ); } public function getPinterestLinkForSpecificImage(string $imageMethod, ?bool $useImageTitle = false): string { if ($this->object && $this->object->exists() && $this->object->hasMethod($imageMethod)) { $image = $this->object->{$imageMethod}(); if ($image && $image->exists()) { $imageTitle = $useImageTitle ? $image->Title : $this->object->Title; return 'http://pinterest.com/pin/create/button/' . '?url=' . urlencode($this->object->AbsoluteLink()) . '&' . 'description=' . urlencode($imageTitle) . '&' . 'media=' . urlencode($image->AbsoluteLink()); } } return ''; } protected function getValuesFromArrayToString(string $variable, string $staticVariable, ?string $prepender = '@') { $a = empty($this->{$variable}) ? $this->Config()->get($staticVariable) : $this->{$variable}; $str = ''; if (is_array($a) && count($a)) { $str = $prepender . implode(' ' . $prepender, $a); } return trim($str); } private function shareThisLinkField(): string { return $this->shareThisFieldAsString($this->linkMethod); } private function shareThisTitleField(): string { return $this->shareThisFieldAsString($this->titleMethod); } private function shareThisFieldAsString(string $field): string { $value = ''; if ($this->object->hasMethod($field)) { $value = $this->object->{$field}(); } elseif (isset($this->object->{$field})) { $value = $this->object->{$field}; } return (string) $value; } private function shareThisMediaField(): string { $media = ''; $imageMethods = $this->imageMethods; if (is_array($imageMethods) && count($imageMethods)) { //do nothing } else { $imageMethods = Config::inst()->get( 'ShareThisSimpleProvider', 'image_methods' ); } if (is_array($imageMethods) && count($imageMethods)) { foreach ($imageMethods as $imageMethod) { if ($this->object->hasMethod($imageMethod)) { $imageField = $imageMethod . 'ID'; if ($this->{$imageField}) { $image = $this->object->{$imageMethod}(); if ($image && $image->exists()) { $media = $image->AbsoluteLink(); break; } } } } } return $media; } private function shareThisDescriptionField(?string $customDescription = ''): string { if ($customDescription) { $description = $customDescription; } else { $description = ''; $descriptionMethod = $this->descriptionMethod; if (! $descriptionMethod) { $descriptionMethod = Config::inst()->get( 'ShareThisSimpleProvider', 'description_method' ); } if ($descriptionMethod) { $description = $this->shareThisFieldAsString($descriptionMethod); } } return $description; } } |