Skip to content

Commit

Permalink
Merge pull request #4 from datashaman/master
Browse files Browse the repository at this point in the history
Neaten up release and add new services
  • Loading branch information
datashaman committed Mar 8, 2016
2 parents d279058 + be61c56 commit be44976
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ language: php
sudo: false

php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm
Expand Down
81 changes: 64 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ Share links with Laravel 5

This is a fork to John's share for Laravel 4.

## Services available

- Delicious : delicious
- Digg : digg
- Evernote : evernote
- Facebook : facebook
- Gmail : gmail
- Google Plus : gplus
- LinkedIn : linkedin
- Pinterest : pinterest
- Reddit : reddit
- Scoop.it : scoopit
- Tumblr : tumblr
- Twitter : twitter
- Viadeo : viadeo
- vk.com : vk


## Installation
Expand All @@ -19,7 +35,7 @@ Add *Chencha\Share\ShareServiceProvider* to providers array in *config/app.php*
Step 3 : Register Alias


Add *Chencha\Share\ShareFacade* to aliases array in *config/app.php*
Add *Share* => *Chencha\Share\ShareFacade* to aliases array in *config/app.php*


## Usage
Expand All @@ -45,21 +61,52 @@ Get many links

Returns an array :

{"facebook":"https:\/\/www.facebook.com\/sharer\/sharer.php?u=http%3A%2F%2Fwww.example.com&title=Link+description","gplus":"https:\/\/plus.google.com\/share?url=http%3A%2F%2Fwww.example.com","twitter":"https:\/\/twitter.com\/intent\/tweet?url=http%3A%2F%2Fwww.example.com&text=Link+description"}
{
"gplus" : "https://plus.google.com/share?url=http%3A%2F%2Fwww.example.com",
"twitter" : "https://twitter.com/intent/tweet?url=http%3A%2F%2Fwww.example.com&text=Link+description",
"facebook" : "https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fwww.example.com&title=Link+description"
}


## Services available
- Delicious : delicious
- Digg : digg
- Evernote : evernote
- Facebook : facebook
- Gmail : gmail
- Google Plus : gplus
- LinkedIn : linkedin
- Pinterest : pinterest
- Reddit : reddit
- Scoop.it : scoopit
- Tumblr : tumblr
- Twitter : twitter
- Viadeo : viadeo
- vk.com : vk
Get ALL the links

Route::get('/', function()
{
return Share::load('http://www.example.com', 'Link description')->services();
});

Returns an array of results for all defined services.

## Customization

Publish the package config:

php artisan vendor:publish --provider='Chencha\Share\ShareServiceProvider'

Add a new service in config/social-share.php:

'mynewservice' => [ 'view' => 'share.mynewservice' ]

Add Blade templating code in *share.mynewservice* view file to generate a URL for *mynewservice*. You have access to:

- service - the service definition (shown above).
- sep - separator used between parameters, defaults to '&'. Configurable as *social-share.separator*.
- url - the URL being shared.
- title - the title being shared.
- media - media link being shared.

Example:

https://mynewservice.example.com?url={{ urlencode($url) }}<?php echo $sep; ?>title={{ urlencode("Check this out! $title. See it here: $url") }}

Another example for the *email* service. Change the service config to be *[ 'view' => 'whatever' ]* and put this in the view file:

mailto?subject={{ urlencode("Wow, check this: $title") }}<?php echo $sep; ?>body={{ urlencode("Check this out! $title. See it here: $url") }}

Localizing? Easy, use Laravel's trans() call:

mailto:subject={{ urlencode(trans('share.email-subject', compact('url', 'title', 'media'))) }}<?php echo $sep ?>body={{ urlencode(trans('share.email-body', compact('url', 'title', 'media'))) }}

Create a file at resources/lang/en/share.php with your choice of subject and body. URLs arguably have a maximum length of 2000 characters.

Notice the use of *<?php echo $sep; ?>*. It's the only way to print out an unencoded ampersand (if configured that way).
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"laravel",
"laravel5"
],
"version": "5.1.0",
"version": "5.2.0",
"license": "MIT",
"authors": [
{
Expand All @@ -23,7 +23,7 @@
}
],
"require": {
"php": ">=5.6.0",
"php": ">=5.4.0",
"illuminate/support": "^5.0"
},
"autoload": {
Expand All @@ -38,6 +38,6 @@
"require-dev": {
"orchestra/testbench": "~3.0",
"mockery/mockery": "^0.9.4",
"guzzlehttp/guzzle": "^6.1"
"guzzlehttp/guzzle": ">=5.3"
}
}
4 changes: 4 additions & 0 deletions src/Chencha/Share/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function load($url, $title = '', $media = ''){
public function services(){
$services = func_get_args();

if (empty($services)) {
$services = array_keys($this->app->config->get('social-share.services'));
}

$object = false;
if (end($services) === true)
{
Expand Down
1 change: 1 addition & 0 deletions src/config/social-share.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'services' => [
'delicious' => [ 'uri' => 'https://delicious.com/post' ],
'digg' => [ 'uri' => 'http://www.digg.com/submit' ],
'email' => [ 'view' => 'social-share::email' ],
'evernote' => [ 'uri' => 'http://www.evernote.com/clip.action' ],
'facebook' => [ 'uri' => 'https://www.facebook.com/sharer/sharer.php', 'urlName' => 'u', ],
'gmail' => [ 'uri' => 'https://mail.google.com/mail/', 'urlName' => 'su', 'titleName' => 'body', 'extra' => [
Expand Down
1 change: 1 addition & 0 deletions src/views/email.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mailto:?subject={{ urlencode($title) }}&body={{ urlencode($url) }}
4 changes: 4 additions & 0 deletions src/views/test.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<ul>
@foreach(Share::services() as $service)
@endforeach
</ul>
72 changes: 52 additions & 20 deletions tests/ShareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@

class ShareTest extends TestCase
{
protected $expected = [
"delicious" => "https://delicious.com/post?url=http%3A%2F%2Fwww.example.com&title=Example",
"digg" => "http://www.digg.com/submit?url=http%3A%2F%2Fwww.example.com&title=Example",
"email" => "mailto:?subject=Example&body=http%3A%2F%2Fwww.example.com",
"evernote" => "http://www.evernote.com/clip.action?url=http%3A%2F%2Fwww.example.com&title=Example",
"facebook" => "https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fwww.example.com&title=Example",
"gmail" => "https://mail.google.com/mail/?su=http%3A%2F%2Fwww.example.com&body=Example&view=cm&fs=1&to=&ui=2&tf=1",
"gplus" => "https://plus.google.com/share?url=http%3A%2F%2Fwww.example.com",
"linkedin" => "http://www.linkedin.com/shareArticle?url=http%3A%2F%2Fwww.example.com&title=Example&mini=true",
"pinterest" => "http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.example.com&description=Example&media=Media",
"reddit" => "http://www.reddit.com/submit?url=http%3A%2F%2Fwww.example.com&title=Example",
"scoopit" => "http://www.scoop.it/oexchange/share?url=http%3A%2F%2Fwww.example.com&title=Example",
"tumblr" => "http://www.tumblr.com/share?u=http%3A%2F%2Fwww.example.com&t=Example&v=3",
"twitter" => "https://twitter.com/intent/tweet?url=http%3A%2F%2Fwww.example.com&text=Example",
"viadeo" => "http://www.viadeo.com/?url=http%3A%2F%2Fwww.example.com&title=Example",
"vk" => "http://vk.com/share.php?url=http%3A%2F%2Fwww.example.com&title=Example&image=Media&noparse=false",
"whatsapp" => "whatsapp://send?text=Example+http%3A%2F%2Fwww.example.com",
"whatsapp" => "whatsapp://send?text=Example+http%3A%2F%2Fwww.example.com",

"service" => "http://service.example.com?url=http%3A%2F%2Fwww.example.com&title=Example&media=Media",
"service2" => "http://service2.example.com?url=http%3A%2F%2Fwww.example.com&title=Example&extra1=Extra+1&extra2=Extra+2",
];

protected function getPackageProviders($app)
{
return [
Expand Down Expand Up @@ -93,29 +116,19 @@ public function testRenderExtra()
Share::load('http://www.example.com')->service2());
}

public function testServices()
public function testSeparator()
{
$expected = [
"delicious" => "https://delicious.com/post?url=http%3A%2F%2Fwww.example.com&title=Example",
"digg" => "http://www.digg.com/submit?url=http%3A%2F%2Fwww.example.com&title=Example",
"evernote" => "http://www.evernote.com/clip.action?url=http%3A%2F%2Fwww.example.com&title=Example",
"facebook" => "https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fwww.example.com&title=Example",
"gmail" => "https://mail.google.com/mail/?su=http%3A%2F%2Fwww.example.com&body=Example&view=cm&fs=1&to=&ui=2&tf=1",
"gplus" => "https://plus.google.com/share?url=http%3A%2F%2Fwww.example.com",
"linkedin" => "http://www.linkedin.com/shareArticle?url=http%3A%2F%2Fwww.example.com&title=Example&mini=true",
"pinterest" => "http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.example.com&description=Example&media=Media",
"reddit" => "http://www.reddit.com/submit?url=http%3A%2F%2Fwww.example.com&title=Example",
"scoopit" => "http://www.scoop.it/oexchange/share?url=http%3A%2F%2Fwww.example.com&title=Example",
"tumblr" => "http://www.tumblr.com/share?u=http%3A%2F%2Fwww.example.com&t=Example&v=3",
"twitter" => "https://twitter.com/intent/tweet?url=http%3A%2F%2Fwww.example.com&text=Example",
"viadeo" => "http://www.viadeo.com/?url=http%3A%2F%2Fwww.example.com&title=Example",
"vk" => "http://vk.com/share.php?url=http%3A%2F%2Fwww.example.com&title=Example&image=Media&noparse=false",
"whatsapp" => "whatsapp://send?text=Example+http%3A%2F%2Fwww.example.com",
];
$this->app->config->set('social-share.separator', '&amp;');
$this->assertEquals('http://service.example.com?url=http%3A%2F%2Fwww.example.com&amp;title=Example',
Share::load('http://www.example.com', 'Example')->service());
}

public function testServices()
{
$actual = Share::load('http://www.example.com', 'Example', 'Media')->services(
'delicious',
'digg',
'email',
'evernote',
'facebook',
'gmail',
Expand All @@ -128,10 +141,19 @@ public function testServices()
'twitter',
'viadeo',
'vk',
'whatsapp'
'whatsapp',

'service',
'service2'
);

$this->assertEquals($expected, $actual);
$this->assertEquals($this->expected, $actual);
}

public function testDefaultIsAll()
{
$actual = Share::load('http://www.example.com', 'Example', 'Media')->services();
$this->assertEquals($this->expected, $actual);
}

protected function assertPageFound($url)
Expand Down Expand Up @@ -164,6 +186,16 @@ public function testDigg()
// $this->assertPageFound($url);
}

/**
* @group live
*/
public function testEmail()
{
$url = 'mailto:?subject=Example&body=http%3A%2F%2Fwww.example.com';
$this->assertEquals($url, Share::load('http://www.example.com', 'Example')->email());
// $this->assertPageFound($url);
}

/**
* @group live
*/
Expand Down

0 comments on commit be44976

Please sign in to comment.