Skip to content

Commit

Permalink
Merge pull request #9 from silverstripe-superchargers/master
Browse files Browse the repository at this point in the history
upgrade/ss4: updated the share-this module to be fully compatible with SS4
  • Loading branch information
sunnysideup committed Jan 9, 2019
2 parents 7321843 + 0db6095 commit 5202d8f
Show file tree
Hide file tree
Showing 37 changed files with 1,179 additions and 479 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -1,5 +1,6 @@
.sass-cache
.DS_Store
.upgrade.yml
npm-debug.log
/.buildpath
/.project
Expand All @@ -9,3 +10,5 @@ admin/thirdparty/chosen/node_modules
node_modules/
coverage/
/**/*.js.map
/vendor
/resources
7 changes: 7 additions & 0 deletions .scrutinizer.yml
Expand Up @@ -65,5 +65,12 @@ checks:
assignment_of_null_return: true
argument_type_checks: true

build:
nodes:
analysis:
tests:
override:
- php-scrutinizer-run

filter:
paths: [code/*, tests/*]
32 changes: 0 additions & 32 deletions .upgrade.yml

This file was deleted.

4 changes: 2 additions & 2 deletions _config.php
@@ -1,11 +1,11 @@
<?php

namespace SunnySideUp\ShareThis;
namespace SunnysideUp\ShareThis;

/**
* developed by www.sunnysideup.co.nz
* author: Nicolaas - modules [at] sunnysideup.co.nz
*
**/

define('SS_SHARETHIS_DIR', 'sharethis');
define('SS_SHARETHIS_DIR', 'resources/vendor/sunnysideup/sharethis');
10 changes: 5 additions & 5 deletions _config/config.yml
Expand Up @@ -5,20 +5,20 @@ After: 'framework/*'
---
SilverStripe\SiteConfig\SiteConfig:
extensions:
- SunnySideUp\ShareThis\ShareThisSiteConfigDE
- SunnysideUp\ShareThis\ShareThisSiteConfigDE

SilverStripe\CMS\Model\SiteTree:
extensions:
- SunnySideUp\ShareThis\ShareThisSTE
- SunnySideUp\ShareThis\SocialNetworksSTE
- SunnysideUp\ShareThis\ShareThisSTE
- SunnysideUp\ShareThis\SocialNetworksSTE
---
Only:
classexists: DataObjectSorterDOD
---
SunnySideUp\ShareThis\ShareThisDataObject:
SunnysideUp\ShareThis\ShareThisDataObject:
extensions:
- DataObjectSorterDOD

SunnySideUp\ShareThis\SocialNetworkingLinksDataObject:
SunnysideUp\ShareThis\SocialNetworkingLinksDataObject:
extensions:
- DataObjectSorterDOD
2 changes: 1 addition & 1 deletion _config/routes.yml
Expand Up @@ -4,4 +4,4 @@ After: framework/routes#coreroutes
---
SilverStripe\Control\Director:
rules:
removefacebooklink//$Action/$ID: SunnySideUp\ShareThis\RemoveFacebookItemController
removefacebooklink//$Action/$ID: SunnysideUp\ShareThis\RemoveFacebookItemController
76 changes: 64 additions & 12 deletions code/api/MyTwitter.php
@@ -1,14 +1,14 @@
<?php

namespace SunnySideUp\ShareThis;
namespace SunnysideUp\ShareThis;

use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Control\Session;
use SilverStripe\Core\Config\Config;
use SunnySideUp\ShareThis\MyTwitter;
use SunnySideUp\ShareThis\MyTwitterData;
use SunnysideUp\ShareThis\MyTwitter;
use SunnysideUp\ShareThis\MyTwitterData;
use SilverStripe\Control\Director;
use SunnySideUp\ShareThis\TwitterOAuth;
use SunnysideUp\ShareThis\TwitterOAuth;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\View\ArrayData;
Expand All @@ -23,26 +23,53 @@ class MyTwitter
{
use Injectable;

/**
* @var boolean
*/
private static $debug = false;

private static $singletons = array();
/**
* @var array
*/
private static $singletons = [];

/**
* @var boolean
*/
private static $favourites_only = false;

/**
* @var boolean
*/
private static $non_replies_only = false;

/**
* @var string
*/
private static $twitter_consumer_key = "";

/**
* @var string
*/
private static $twitter_consumer_secret = "";

/**
* @var string
*/
private static $titter_oauth_token = "";

/**
* @var string
*/
private static $titter_oauth_token_secret = "";

private static $twitter_config = array(
/**
* @var array
*/
private static $twitter_config = [
'include_entities' => 'true',
'include_rts' => 'true'
);
];

/**
* returns a DataObjetSet of the last $count tweets.
Expand All @@ -57,17 +84,21 @@ public static function last_statuses($username, $count = 1, $useHourlyCache = tr
if (!$username) {
user_error("No username provided");
}

$sessionName = "MyTwitterFeeds$username".date("Ymdh");

if (Session::get($sessionName) && $useHourlyCache && !Config::inst()->get(MyTwitter::class, "debug")) {
//do nothing
} else {
if (empty(self::$singletons[$username])) {
self::$singletons[$username] = new MyTwitter($username, $count);
}

$dataObjectSet = self::$singletons[$username]->TwitterFeed($username, $count);

if ($dataObjectSet && $dataObjectSet->count()) {
foreach ($dataObjectSet as $tweet) {
if (!MyTwitterData::get()->filter(array("TwitterID" => $tweet->ID))->count()) {
if (!MyTwitterData::get()->filter(["TwitterID" => $tweet->ID])->count()) {
$myTwitterData = new MyTwitterData();
$myTwitterData->TwitterID = $tweet->ID;
$myTwitterData->Title = $tweet->Title;
Expand All @@ -76,10 +107,12 @@ public static function last_statuses($username, $count = 1, $useHourlyCache = tr
}
}
}

Session::set($sessionName, 1);
}

Config::inst()->update(MyTwitterData::class, "username", $username);
return MyTwitterData::get()->filter(array("Hide" => 0))->limit($count);
return MyTwitterData::get()->filter(["Hide" => 0])->limit($count);
}


Expand All @@ -95,61 +128,80 @@ public function TwitterFeed($username, $count = 5)
if (!$username) {
user_error("No username provided");
}

Config::inst()->update(MyTwitterData::class, "username", $username);

//check settings are available
$requiredSettings = array("twitter_consumer_key", "twitter_consumer_secret", "titter_oauth_token", "titter_oauth_token");
$requiredSettings = [
"twitter_consumer_key",
"twitter_consumer_secret",
"titter_oauth_token",
"titter_oauth_token"
];

foreach ($requiredSettings as $setting) {
if (!Config::inst()->get(MyTwitter::class, $setting)) {
user_error(" you must set MyTwitter::$setting", E_USER_NOTICE);
return null;
}
}

require_once(Director::baseFolder().'/'.SS_SHARETHIS_DIR.'/third_party/twitter_oauth/TwitterOAuthConsumer.php');

$connection = new TwitterOAuth(
Config::inst()->get(MyTwitter::class, "twitter_consumer_key"),
Config::inst()->get(MyTwitter::class, "twitter_consumer_secret"),
Config::inst()->get(MyTwitter::class, "titter_oauth_token"),
Config::inst()->get(MyTwitter::class, "titter_oauth_token_secret")
);

$config = Config::inst()->get(MyTwitter::class, "twitter_config");
$config['screen_name'] = $username;
$tweets = $connection->get('statuses/user_timeline', $config);
$tweetList = new ArrayList();

if (count($tweets) > 0 && !isset($tweets->error)) {
$i = 0;

foreach ($tweets as $tweet) {
if (Config::inst()->get(MyTwitter::class, "favourites_only") && $tweet->favorite_count == 0) {
break;
}

if (Config::inst()->get(MyTwitter::class, "non_replies_only") && $tweet->in_reply_to_status_id) {
break;
}

if (Config::inst()->get(MyTwitter::class, "debug")) {
print_r($tweet);
}

if (++$i > $count) {
break;
}

$date = new DBDatetime();
$date->setValue(strtotime($tweet->created_at));
$text = htmlentities($tweet->text, ENT_NOQUOTES, $encoding = "UTF-8", $doubleEncode = false);

if (!empty($tweet->entities) && !empty($tweet->entities->urls)) {
foreach ($tweet->entities->urls as $url) {
if (!empty($url->url) && !empty($url->display_url)) {
$text = str_replace($url->url, '<a href="'.$url->url.'" class="external">'.$url->display_url.'</a>', $text);
}
}
}

$tweetList->push(
new ArrayData(array(
new ArrayData([
'ID' => $tweet->id_str,
'Title' => $text,
'Date' => $date
))
])
);
}
}

return $tweetList;
}
}
16 changes: 11 additions & 5 deletions code/api/SilverstripeFacebookConnector.php
@@ -1,6 +1,6 @@
<?php

namespace SunnySideUp\ShareThis;
namespace SunnysideUp\ShareThis;

use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Config\Config;
Expand All @@ -20,25 +20,29 @@ class SilverstripeFacebookConnector

/**
* settings for connection
*
* @var array
*/
private static $connection_config = array();
private static $connection_config = [];

/**
* application ID - get from FB
*
* @var string
*/
private static $app_id = "";

/**
* application secret - get from FB
*
* @var string
*/
private static $app_secret = "";


/**
* debug
*
* @var boolean
*/
protected static $debug = false;
Expand All @@ -47,10 +51,11 @@ class SilverstripeFacebookConnector
* keep track of errors
* @var array
*/
protected static $error = array();
protected static $error = [];

/**
* set additional connection details - e.g. default_access_token
*
* @param array
*/
public static function set_connection_config($connectionConfig)
Expand All @@ -65,12 +70,12 @@ public static function set_connection_config($connectionConfig)
protected static function get_connection()
{
if (!self::$connection) {
self::$connection_config += array(
self::$connection_config += [
'app_id' => Config::inst()->get(SilverstripeFacebookConnector::class, "app_id"),
'app_secret' => Config::inst()->get(SilverstripeFacebookConnector::class, "app_secret"),
'default_graph_version' => 'v2.4',
//'default_access_token' => '{access-token}', // optional
);
];

self::$connection = new Facebook\Facebook(self::$connection_config);
}
Expand Down Expand Up @@ -119,6 +124,7 @@ public static function whoami()

/**
* returns an array of recent posts for a page
*
* @return array
*/
public static function get_feed($pageID)
Expand Down

0 comments on commit 5202d8f

Please sign in to comment.