Source of file TwitterShortcode.php
Size: 6,198 Bytes - Last Modified: 2021-12-24T06:48:08+00:00
/var/www/docs.ssmods.com/process/src/src/Shortcodes/TwitterShortcode.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 | <?php namespace Fractas\ShortcodableCodes; use RestfulService; use SilverStripe\View\ArrayData; use SilverStripe\Forms\TextField; use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\FieldList; use SilverStripe\View\ViewableData; class TwitterShortcode extends ViewableData { private static $singular_name = 'Twitter Shortcode'; private static $plural_name = 'Twitter Shortcodes'; /** * The API base URL. */ const API_URL = 'https://publish.twitter.com/oembed'; const API_VIDEO_URL = 'https://api.twitter.com/1.1/statuses/oembed.json'; /** * @return mixed */ public function singular_name() { return $this->stat('singular_name'); } /** * Parse the shortcode and render as a string, probably with a template. * * @param array $attributes the list of attributes of the shortcode * @param string $content the shortcode content * @param ShortcodeParser $parser the ShortcodeParser instance * @param string $shortcode the raw shortcode being parsed * * @return string */ public static function parse_shortcode($arguments, $content, $parser, $shortcode) { if (!array_key_exists('link', $arguments) || !$arguments['link']) { return 'Twitter Shortcode: no link'; } $arguments['link'] = str_replace("'", '', $arguments['link']); if (strpos($arguments['link'], ',')) { return 'Twitter Shortcode: only one link supported at the moment'; } $apiUrl = self::API_URL; $query = array('url' => Convert::raw2xml($arguments['link'])); if (array_key_exists('isvideo', $arguments)) { $query['widget_type'] = 'video'; $apiUrl = self::API_VIDEO_URL; } $fetch = new RestfulService(self::API_URL, 1800); // 0 = no caching service / default cache is set to 1 hour (3600) $fetch->setQueryString($query); if (!$fetch) { return false; } try { $conn = $fetch->request('', 'GET', null, null, array(CURLOPT_SSL_VERIFYPEER => false)); // check response status code if (200 == $conn->getStatusCode()) { // read body from response $body = $conn->getBody(); } else { // response status is not OK (send email) $body = false; $loginErr = false; if (404 == $conn->getStatusCode()) { SS_Log::log(new Exception('(404) API is not available'), SS_Log::ERR); } elseif (503 == $conn->getStatusCode()) { SS_Log::log(new Exception('(503) API is not available'), SS_Log::ERR); } elseif (500 == $conn->getStatusCode()) { SS_Log::log(new Exception('(500) API is not available'), SS_Log::ERR); } elseif (403 == $conn->getStatusCode()) { SS_Log::log(new Exception('(403) API is not available'), SS_Log::ERR); } elseif (401 == $conn->getStatusCode()) { SS_Log::log(new Exception('(401) API is not available'), SS_Log::ERR); } else { SS_Log::log(new Exception(print_r($conn, true)), SS_Log::ERR); } return false; } } catch (Exception $e) { SS_Log::log(new Exception(print_r($e, true)), SS_Log::ERR); return false; } $urlDecode = json_decode(($body), 1); $data = new ArrayData(array( 'Title' => (array_key_exists('title', $arguments)) ? $arguments['title'] : false, 'Data' => ($urlDecode) ? $urlDecode['html'] : false, 'Video' => (array_key_exists('isvideo', $arguments)) ? $arguments['isvideo'] : false, )); // render with template return $data->renderWith('TwitterShortcode'); } /** * Returns a list of fields for editing the shortcode's attributes * in the insert shortcode popup window. * * @return Fieldlist **/ public function getShortcodeFields() { $fields = FieldList::create( TextField::create('title', _t('ShortcodeExt.TITLE', 'Title')) ->setMaxLength(80) ->setDescription('<span class="optional-shortcode-label">OPTIONAL:</span> add a title that shows above this Twitter post. <br>Max. <strong>80 characters</strong>'), TextField::create('link', _t('ShortcodeExt.LINK', 'Link')) ->setDescription('<span class="required-shortcode-label">REQUIRED:</span> enter full Twitter post link.<br>Example post: https://twitter.com/NatGeo/status/914340365834113025<br> Example video: https://twitter.com/NatGeo/status/914143809100931072'), CheckboxField::create('isvideo', _t('ShortcodeExt.ISVIDEO', 'Is Video')) ->setDescription('Choose this if you wanna embed Twitter video.') ); return $fields; } /** * Returns a link to an image to be displayed as a placeholder in the editor * In this example we make easy work of this task by using the placehold.it service * But you could also return a link to an image in the filesystem - perharps the first * image in this TwitterShortcode a placeholder. * * @param array $attributes the list of attributes of the shortcode * * @return string **/ public function getShortcodePlaceHolder($attributes) { $title = (array_key_exists('title', $attributes)) ? $attributes['title'] : false; $link = (array_key_exists('link', $attributes)) ? $attributes['link'] : false; $text = 'Tweet'; $text .= "\r\n"; $text .= $title; $text .= "\r\n"; $text .= '('.$link.')'; $params = array( 'txt' => $text, 'w' => 300, 'h' => 200, 'txtsize' => '20', 'bg' => '55acee', 'txtclr' => 'FFFFFF', ); return 'https://placeholdit.imgix.net/~text?'.http_build_query($params); } } |