├── LICENSE.md
├── README.md
├── composer.json
├── config
└── share-buttons.php
├── resources
├── css
│ └── share-buttons.css
└── js
│ ├── share-buttons.jquery.js
│ └── share-buttons.js
└── src
├── Exceptions
├── InvalidOptionValue.php
├── InvalidProcessedCallArgument.php
└── InvalidTemplaterFactoryArgument.php
├── Facades
└── ShareButtonsFacade.php
├── Presenters
├── Formatters
│ ├── AttributesFormatter.php
│ └── DefaultAttributesFormatter.php
├── ShareButtonsPresenter.php
├── TemplateBasedBlockPresenter.php
├── TemplateBasedElementPresenter.php
├── TemplateBasedPresenterMediator.php
└── TemplateBasedUrlPresenter.php
├── Providers
└── ShareButtonsServiceProvider.php
├── ShareButtons.php
├── Templaters
├── LaravelTemplater.php
├── SimpleColonTemplater.php
└── Templater.php
└── ValueObjects
└── ProcessedCall.php
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Sergey Kudashev
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Laravel Share Buttons
2 |
3 | [](https://packagist.org/packages/kudashevs/laravel-share-buttons)
4 | [](https://github.com/kudashevs/laravel-share-buttons/actions/workflows/run-tests.yml)
5 | [](LICENSE.md)
6 |
7 | This Laravel package provides the possibility to generate share links (social media share buttons) for your site in
8 | a flexible and convenient way within seconds. The package was originated from the Laravel Share.
9 |
10 | [//]: # (@todo don't forget to update these services)
11 | ### Available services
12 |
13 | * Reddit
14 | * LinkedIn
15 | * Facebook
16 | * X (formerly Twitter)
17 | * Bluesky
18 | * Mastodon
19 | * Hacker News
20 | * Tumblr
21 | * Telegram
22 | * WhatsApp
23 | * VKontakte
24 | * Pinterest
25 | * Pocket
26 | * Evernote
27 | * Skype
28 | * Xing
29 | * Copy the link
30 | * Mail the link
31 |
32 |
33 | ## Installation
34 |
35 | You can install the package via composer:
36 | ```bash
37 | composer require kudashevs/laravel-share-buttons
38 | ```
39 |
40 | If you don't use auto-discovery, just add a ShareButtonsServiceProvider to the `config/app.php`
41 | ```php
42 | 'providers' => [
43 | Kudashevs\ShareButtons\Providers\ShareButtonsServiceProvider::class,
44 | ],
45 | ```
46 | By default, the `ShareButtons` class instance is bound to the `sharebuttons` alias.
47 |
48 | **Note**: Don't forget to publish the configuration file (required) and assets.
49 | ```bash
50 | php artisan vendor:publish --provider="Kudashevs\ShareButtons\Providers\ShareButtonsServiceProvider"
51 | ```
52 | > In case of a major change, it is recommended to back up your config file and republish a new one from scratch.
53 |
54 | If you want to publish certain assets only, use the `--tag` option with one of the following assets tags: `config`, `css`,
55 | `js` (includes all possible js files), `vanilla`, `jquery`.
56 |
57 |
58 | ## Assets
59 |
60 | By default, this package relies on the `Font Awesome` icons. The buttons' interactivity is implemented in two
61 | different ways (via `Vanilla JS` and `jQuery`). However, you can use any custom fonts, icons, or JavaScript.
62 |
63 | ### Font Awesome and default styles
64 |
65 | To enable Font Awesome icons, you can load it from a CDN. For more information on how to use Font Awesome, please read the [introduction](https://fontawesome.com/docs/web/setup/get-started).
66 | ```html
67 |
68 | ```
69 |
70 | To enable the default styles, [publish](#publish) the `css` asset (the command will create a `resources/css/share-buttons.css` file).
71 | After publishing, you can copy the file to the `public/css` folder and use it directly. Or you can integrate the css file into
72 | your assets building process.
73 | ```html
74 |
75 | ```
76 |
77 | ### JavaScript
78 |
79 | To enable interaction on social media buttons with JavaScript, [publish](#publish) the `vanilla` asset (the command will create a `resources/js/share-buttons.js` file).
80 | After publishing, you can copy the file to the `public/js` folder and use it directly. Or you can integrate this file into
81 | your assets building process.
82 | ```html
83 |
84 | ```
85 |
86 | ### jQuery
87 |
88 | To enable interaction on social media buttons with jQuery, [publish](#publish) the `jquery` asset (the command will create a `resources/js/share-buttons.jquery.js` file).
89 | After publishing, you can copy the file to the `public/js` folder and use it directly. Or you can integrate this file into
90 | your assets building process.
91 | ```html
92 |
93 |
94 | ```
95 |
96 |
97 | ## Usage
98 |
99 | Let's take a look at a short usage example (you can find a detailed usage example in the [corresponding section](#a-detailed-usage-example)).
100 | ```php
101 | ShareButtons::page('https://site.com', 'Page title', [
102 | 'title' => 'Page title',
103 | 'rel' => 'nofollow noopener noreferrer',
104 | ])
105 | ->facebook()
106 | ->linkedin(['rel' => 'follow'])
107 | ->render();
108 | ```
109 |
110 | The code above will result into the following HTML code:
111 | ```html
112 |
113 |
114 |
115 |
116 | ```
117 |
118 | ### Fluent interface
119 |
120 | The `ShareButtons` instance provides a fluent interface. The fluent interface is a pattern based on method chaining.
121 | To start a method chaining you just need to use one of the methods listed below (the starting point).
122 | ```
123 | page($url, $title, $options) # Creates a chaining with a given URL and a given page title
124 | createForPage($url, $title, $options) # Does the same (an alias of the page() method)
125 | currentPage($title, $options) # Creates a chaining with the current page URL and a given page title
126 | createForCurrentPage($title, $options) # Does the same (an alias of the currentPage() method)
127 | ```
128 |
129 | ### Add buttons
130 |
131 | To generate a single social media button, you just need to add one of the following methods to the [method chaining](#fluent-interface).
132 | Each method accepts an array of options (more information about these options in the [local options](#local-options) section).
133 |
134 | [//]: # (@todo don't forget to update these methods)
135 | ```
136 | reddit($options) # Generates a Reddit share button
137 | linkedin($options) # Generates a LinkedIn share button
138 | facebook($options) # Generates a Facebook share button
139 | twitter($options) # Generates an X (former Twitter) share button
140 | bluesky($options) # Generates a Bluesky share button
141 | mastodon($options) # Generates a Mastodon share button
142 | hackernews($options) # Generates a Hacker News share button
143 | tumblr($options) # Generates a Tumblr share button
144 | telegram($options) # Generates a Telegram share button
145 | whatsapp($options) # Generates a WhatsApp share button
146 | vkontakte($options) # Generates a VKontakte share button
147 | pinterest($options) # Generates a Pinterest share button
148 | pocket($options) # Generates a Pocket share button
149 | evernote($options) # Generates an Evernote share button
150 | skype($options) # Generates a Skype share button
151 | xing($options) # Generates a Xing share button
152 | copylink($options) # Generates a copy to the clipboard share button
153 | mailto($options) # Generates a send by mail share button
154 | ```
155 |
156 | These methods are a part of the fluent interface. Therefore, to create multiple social media share buttons you just need to chain them.
157 |
158 | ### Getting share buttons
159 |
160 | You can use a ShareButtons instance as a string or cast it to a string to get ready-to-use HTML code. However, this is not the best way.
161 | If you want to be clear in your intentions, use one of the methods that return generated HTML code. These methods are:
162 | ```php
163 | render() # Returns a generated share buttons HTML code
164 | getShareButtons() # Does the same (an alias of the render() method)
165 | ```
166 |
167 | ### Getting raw links
168 |
169 | Sometimes, you may only want the raw links without any HTML. In such a case, just use the `getRawLinks` method.
170 | ```php
171 | getRawLinks() # Returns an array of generated links
172 | ```
173 |
174 | ## Parameters
175 |
176 | There is the possibility of providing different options to style and decorate the resulting HTML code at different levels.
177 |
178 | ### Global options
179 |
180 | Every time a chaining method is called, it accepts several arguments, including a page URL (depending on the method), a page title,
181 | and an array of options. These options are global because they change the representation of all share buttons. These options are:
182 | ```
183 | 'block_prefix' => 'tag' # Sets a share buttons block prefix (default is
)
184 | 'block_suffix' => 'tag' # Sets a share buttons block suffix (default is
)
185 | 'element_prefix' => 'tag' # Sets an element prefix (default is empty)
186 | 'element_suffix' => 'tag' # Sets an element suffix (default is empty)
187 | 'id' => 'value' # Adds an HTML id attribute to the output links
188 | 'class' => 'value' # Adds an HTML class attribute to the output links
189 | 'title' => 'value' # Adds an HTML title attribute to the output links
190 | 'rel' => 'value' # Adds an HTML rel attribute to the output links
191 | ```
192 |
193 | ### Local options
194 |
195 | Any of the [share button methods](#add-buttons), that generates a button, accepts several arguments. These options are local
196 | because they will be applied to a specific element only. The local options have a **higher priority**. Therefore, they
197 | will overwrite the global options if there is any overlap. At the moment, the package supports the following local options:
198 | ```
199 | 'text' => 'value' # Adds a link text to a generated URL (overrides global page title)
200 | 'id' => 'value' # Adds an HTML id attribute to the button link
201 | 'class' => 'value' # Adds an HTML class attribute to the button link
202 | 'title' => 'value' # Adds an HTML title attribute to the button link
203 | 'rel' => 'value' # Adds an HTML rel attribute to the button link
204 | 'summary' => 'value' # Adds a summary text to the URL (linkedin button only)
205 | ```
206 |
207 | ## Configuration
208 |
209 | The configuration settings are located in the `config/share-buttons.php` file.
210 |
211 | ### Representation section
212 |
213 | This section contains settings related to the "container" in which the social media buttons will be displayed.
214 | ```
215 | 'block_prefix' => 'tag' # Sets a block prefix (default is
)
216 | 'block_suffix' => 'tag' # Sets a block suffix (default is
)
217 | 'element_prefix' => 'tag' # Sets an element prefix (default is empty)
218 | 'element_suffix' => 'tag' # Sets an element suffix (default is empty)
219 | ```
220 |
221 | ### Share buttons section
222 |
223 | Each social media share button has its own individual configuration settings.
224 | ```
225 | 'url' => 'value' # A share button URL template (is used to generate a button's URL)
226 | 'text' => 'value' # A default text to be added to the url (is used when the page title is empty)
227 | 'extra' => [ # Extra options that are required by some specific buttons
228 | 'summary' => 'value' # A default summary to be added to the url (linkedin only)
229 | 'raw' => 'value' # A boolean defines whether to skip the URL-encoding of the url
230 | 'hash' => 'value' # A boolean defines whether to use a hash instead of the url
231 | ]
232 | ```
233 | **Note**: a text value might contain a `url` element, which will be replaced by the page url while processing.
234 |
235 | ### Templates section
236 |
237 | Each share button has a corresponding link template. A template contains several elements that will be substituted with
238 | data from different arguments and options. The format of these elements depends on the `templater` setting. By default,
239 | these elements are:
240 | ```
241 | :url # Will be replaced with a prepared URL
242 | :id # Will be replaced with an id attribute
243 | :class # Will be replaced with a class attribute
244 | :title # Will be replaced with a title attribute
245 | :rel # Will be replaced with a rel attribute
246 | ```
247 |
248 | ### Templaters section
249 |
250 | For processing different templates and substitute elements in them, the package uses templaters (template engines).
251 | By default, these options are optional (if no value provided, the default templater will be used).
252 | ```
253 | 'templater' # A template engine for processing link templates
254 | 'url_templater' # A template engine for processing share buttons URLs
255 | ```
256 |
257 |
258 | ## A detailed usage example
259 |
260 | To summarize all of the information from above, let's take a look at a real-life example. We begin with one of the methods
261 | that start the fluent interface, and we provide some global options. Then, we add some specific methods that generate social
262 | media share buttons. At this step, we can provide any local options, as it is done in the `linkedin()` method. Finally,
263 | we finish the fluent interface chain with one of the methods that return the resulting HTML code.
264 | ```php
265 | ShareButtons::page('https://site.com', 'Page title', [
266 | 'block_prefix' => '
',
267 | 'block_suffix' => '
',
268 | 'element_prefix' => '
',
269 | 'element_suffix' => '
',
270 | 'class' => 'my-class',
271 | 'id' => 'my-id',
272 | 'title' => 'my-title',
273 | 'rel' => 'nofollow noopener noreferrer',
274 | ])
275 | ->facebook()
276 | ->linkedin(['id' => 'linked', 'class' => 'hover', 'rel' => 'follow', 'summary' => 'cool summary'])
277 | ->render();
278 | ```
279 |
280 | The code above will result into the following HTML code:
281 | ```html
282 |