Issues (158)

code/data/ShareThisOptions.php (2 issues)

1
<?php
2
3
namespace SunnysideUp\ShareThis;
4
5
use SilverStripe\Core\Injector\Injectable;
6
use SilverStripe\Dev\Debug;
7
use SilverStripe\Control\Email\Email;
8
use SilverStripe\Control\Director;
9
use SilverStripe\Core\Config\Config;
10
use SunnysideUp\ShareThis\ShareThisSTE;
11
12
13
/**
14
 * @author nicolaas [at] sunnysideup.co.nz
15
 */
16
class ShareThisOptions
17
{
18
    use Injectable;
19
20
    /**
21
     * @var array
22
     */
23
    private static $page_specific_data;
24
25
    /**
26
     * @var general_data
27
     */
28
    private static $general_data;
29
30
    /**
31
     * @var share_all_data
32
     */
33
    private static $share_all_data;
34
35
    /**
36
     * @var non_encoded_page_url
37
     */
38
    private static $non_encoded_page_url;
39
40
    /**
41
     * @var encoded_page_url
42
     */
43
    private static $encoded_page_url;
44
45
    /**
46
     * @var encoded_page_title
47
     */
48
    private static $encoded_page_title;
49
50
    /**
51
     * @var encoded_page_title_space_encoded
52
     */
53
    private static $encoded_page_title_space_encoded;
54
55
    /**
56
     * @var encoded_description
57
     */
58
    private static $encoded_description;
59
60
    /**
61
     * @var icon
62
     */
63
    private static $icon;
64
65
    /**
66
     * Get's all options
67
     *
68
     * @return array
69
     */
70
    public static function get_all_options($title, $link, $description)
71
    {
72
        self::set_variables($title, $link, $description);
73
        self::$page_specific_data = [
74
            "email" => [
75
                "url" => "mailto:?".htmlentities("Subject=".self::$encoded_page_title."&Body=".self::$encoded_description."%0D%0A".self::$encoded_page_url),
76
                "faicon" => "fa-send",
77
                "title" => 'Email'
78
            ],
79
            "print" => [
80
                "url" => "#",
81
                "faicon" => "fa-print",
82
                "click" => "window.print(); return false;",
83
                "title" => "Print"
84
            ],
85
            "favourites" => [
86
                "url" => "#",
87
                "faicon" => "fa-bookmark",
88
                "click" => "sharethis.bookmark('".self::$encoded_page_url."', '".self::$encoded_page_title."'); return false;",
89
                "title" => "Add to favourites (Internet Explorer Only)"
90
            ],
91
            "delicious" => [
92
                "url" => "http://del.icio.us/post?".htmlentities("url=".self::$encoded_page_url."&title=".self::$encoded_page_title),
93
                "faicon" => "fa-delicious",
94
                "title" => "Delicious"
95
            ],
96
            "facebook" => [
97
                "url" => "http://www.facebook.com/share.php?".htmlentities("u=".self::$encoded_page_url."&title=".self::$encoded_page_title),
98
                "faicon" => "fa-facebook-square",
99
                "title" => "Facebook"
100
            ],
101
            "googleplus" => [
102
                "url" =>  "https://plus.google.com/share?url=".self::$encoded_page_url,
103
                "faicon" => "fa-google-plus",
104
                "title" => "Google Plus One"
105
            ],
106
            "linkedin" => [
107
                "url" =>  "http://www.linkedin.com/shareArticle?".htmlentities("mini=true&url=".self::$encoded_page_url."&title=".self::$encoded_page_title."&source=".Director::absoluteBaseURL()),
108
                "faicon" => "fa-linkedin-square",
109
                "title" => "LinkedIn"
110
            ],
111
            "pinterest" => [
112
                "url" => "http://pinterest.com/pin/create/bookmarklet/?".htmlentities("media=html&url=".self::$encoded_page_url."&is_video=false&description=".self::$encoded_page_title),
113
                "faicon" => "fa-pinterest",
114
                "title" => "Pinterest"
115
            ],
116
            "reddit" => [
117
                "url" => "http://reddit.com/submit?".htmlentities("url=".self::$encoded_page_url."&title=".self::$encoded_page_title),
118
                "faicon" => "fa-reddit",
119
                "title" => "Reddit"
120
            ],
121
            "twitter" => [
122
                "url" => "http://twitter.com/home?status=".htmlentities(urlencode("currently reading: ").self::$encoded_page_url),
123
                "faicon" => "fa-twitter-square",
124
                "title" => "Twitter"
125
            ],
126
            "tumblr" => [
127
                "url" => "http://www.tumblr.com/share/link?url=".htmlentities(self::$encoded_page_url."&name=".self::$encoded_page_title),
128
                "faicon" => "fa-tumblr-square",
129
                "title" => "Tumblr"
130
            ]
131
        ];
132
133
        return self::$page_specific_data;
134
    }
135
136
    /**
137
     * @param  string $title
138
     * @param  string $link
139
     * @param  string $description
140
     *
141
     * @return array
142
     */
143
    public static function get_page_specific_data($title, $link, $description = '')
144
    {
145
        $originalArray = self::$page_specific_data ? self::$page_specific_data : self::get_all_options($title, $link, $description);
146
        $finalArray = [];
0 ignored issues
show
The assignment to $finalArray is dead and can be removed.
Loading history...
147
        $inc = Config::inst()->get(ShareThisSTE::class, "included_icons");
148
        $exc = Config::inst()->get(ShareThisSTE::class, "excluded_icons");
149
150
        if (count($inc)) {
151
            $new_array_of_icons_to_include = [];
152
153
            foreach ($inc as $key => $value) {
154
                $new_array_of_icons_to_include[$value] = $value;
155
156
                if (! isset($originalArray[$value])) {
157
                    Debug::show("Error in ShareIcons::set_icons_to_include, $key does not exist in bookmark list");
158
                }
159
160
            }
161
162
            foreach ($originalArray as $key => $array) {
163
                if (! isset($new_array_of_icons_to_include[$key])) {
164
                    unset($originalArray[$key]);
165
                }
166
            }
167
        }
168
169
        //which ones do we exclude
170
        if (count($exc)) {
171
            foreach ($exc as $key) {
172
                if (! isset($originalArray[$key])) {
173
                    Debug::show("Error in ShareIcons::set_icons_to_exclude, $key does not exist in bookmark list");
174
                } else {
175
                    unset($originalArray[$key]);
176
                }
177
            }
178
        }
179
180
        if (! $link) {
181
            self::$page_specific_data = null;
182
        }
183
184
        return $originalArray;
185
    }
186
187
    /*
188
        summary: (required) utf-8 string, defaults to document.title
189
        content: (optional) utf-8 string, defaults to null
190
        updated: (optional) ISO 8601 date, defaults to document.lastModified
191
        published: (optional) ISO 8601 date, defaults to null
192
        author: currently not implemented
193
        category: currently not implemented
194
    */
195
    public static function get_share_all()
196
    {
197
        self::$share_all_data = '
198
            <script type="text/javascript" src="http://w.sharethis.com/button/sharethis.js#&amp;type=website"></script>
199
            <script type="text/javascript">
200
                SHARETHIS.addEntry(
201
                    {
202
                        title:"' . urldecode(self::$encoded_page_title) . '",
203
                        summary:"' . urldecode(self::$encoded_page_title) . '",
204
                        url:"' . urldecode(self::$encoded_page_url) . '",
205
                        icon:"' . urldecode(self::$icon) . '"
206
                    },
207
                    {button:true}
208
                );
209
            </script>
210
        ';
211
212
        return self::$share_all_data;
213
    }
214
215
    /**
216
     * Sets general data
217
     *
218
     * @return void
219
     */
220
    public static function set_general_data()
221
    {
222
        self::$general_data = null;
223
    }
224
225
    /**
226
     * Get's generic data
227
     */
228
    public static function get_general_data()
229
    {
230
        if (! self::$general_data) {
231
            $array = self::get_page_specific_data('', '', '');
232
            $newArray = [];
233
            if (count($array)) {
234
                foreach ($array as $key => $subArray) {
235
                    $newArray[$key] = $key;
236
                }
237
            }
238
            self::$general_data = $newArray;
239
        }
240
241
        return self::$general_data;
242
    }
243
244
    /**
245
     * Set's variables
246
     * @param string
247
     * @param string
248
     * @param string
249
     *
250
     * @return  void
251
     */
252
    private static function set_variables($title, $link, $description)
253
    {
254
        self::$icon = urlencode(Director::absoluteBaseURL() . 'favicon.ico');
255
        self::$non_encoded_page_url = Director::absoluteURL($link);
256
        self::$encoded_page_url = urlencode(self::$non_encoded_page_url);
257
        self::$encoded_page_title = urlencode($title);
258
        self::$encoded_page_title_space_encoded = str_replace('+', '%20', urlencode($title));
259
        if ($description) {
260
            self::$encoded_description = urlencode($description);
261
        } else {
262
            self::$encoded_description = self::$encoded_page_title;
263
        }
264
    }
265
266
    /**
267
     * @return string
268
     */
269
    private function facebookLike()
0 ignored issues
show
The method facebookLike() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
270
    {
271
        //see http://developers.facebook.com/docs/reference/plugins/like/
272
        return '<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=231498950207168&amp;xfbml=1"></script><fb:like href="www.test.com" send="false" width="450" show_faces="true" font="lucida grande"></fb:like>';
273
    }
274
}
275