Completed
Pull Request — master (#7)
by
unknown
13:44
created

ShareThisOptions::set_general_data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
 */
17
class ShareThisOptions
18
{
19
    use Injectable;
20
21
    private static $page_specific_data;
22
23
    private static $general_data;
24
25
    private static $share_all_data;
26
27
    private static $non_encoded_page_url;
28
    private static $encoded_page_url;
29
    private static $encoded_page_title;
30
    private static $encoded_page_title_space_encoded;
31
    private static $encoded_description;
32
    private static $icon;
33
34
    public static function get_all_options($title, $link, $description)
35
    {
36
        self::set_variables($title, $link, $description);
37
        self::$page_specific_data = array(
38
"email" => array(
39
    "url" => "mailto:?".htmlentities("Subject=".self::$encoded_page_title."&Body=".self::$encoded_description."%0D%0A".self::$encoded_page_url),
40
    "faicon" => "fa-send",
41
    "title" => Email::class),
42
"print" => array(
43
    "url" => "#",
44
    "faicon" => "fa-print",
45
    "click" => "window.print(); return false;",
46
    "title" => "Print"),
47
"favourites" => array(
48
    "url" => "#",
49
    "faicon" => "fa-bookmark",
50
    "click" => "sharethis.bookmark('".self::$encoded_page_url."', '".self::$encoded_page_title."'); return false;",
51
    "title" => "Add to favourites (Internet Explorer Only)"),
52
//"foursquare" => array(
53
//	"url" => "http://foursquare.com/home?status=".htmlentities(urlencode("currently reading: ").self::$encoded_page_url),
54
//	"faicon" => "fa-foursquare-square",
55
//	"title" => "FourSquareIt"),
56
"delicious" => array(
57
    "url" => "http://del.icio.us/post?".htmlentities("url=".self::$encoded_page_url."&title=".self::$encoded_page_title),
58
    "faicon" => "fa-delicious",
59
    "title" => "Add to Delicious"),
60
"facebook" => array(
61
    "url" => "http://www.facebook.com/share.php?".htmlentities("u=".self::$encoded_page_url."&title=".self::$encoded_page_title),
62
    "faicon" => "fa-facebook-square",
63
    "title" => "Share on Facebook"),
64
"googleplus" => array(
65
    "url" =>  "https://plus.google.com/share?url=".self::$encoded_page_url,
66
    "faicon" => "fa-google-plus",
67
    "title" => "Google Plus One"),
68
"linkedin" => array(
69
    "url" =>  "http://www.linkedin.com/shareArticle?".htmlentities("mini=true&url=".self::$encoded_page_url."&title=".self::$encoded_page_title."&source=".Director::absoluteBaseURL()),
70
    "faicon" => "fa-linkedin-square",
71
    "title" => "Share on LinkedIn"),
72
"pinterest" => array(
73
    "url" => "http://pinterest.com/pin/create/bookmarklet/?".htmlentities("media=html&url=".self::$encoded_page_url."&is_video=false&description=".self::$encoded_page_title),
74
    "faicon" => "fa-pinterest",
75
    "title" => "Pinterest it"),
76
"reddit" => array(
77
    "url" => "http://reddit.com/submit?".htmlentities("url=".self::$encoded_page_url."&title=".self::$encoded_page_title),
78
    "faicon" => "fa-reddit",
79
    "title" => "Reddit"),
80
"stumbleupon" => array(
81
    "url" => "http://www.stumbleupon.com/submit?".htmlentities("url=".self::$non_encoded_page_url."&title=".self::$encoded_page_title),
82
    "faicon" => "fa-stumbleupon",
83
    "title" => "Stumble It"),
84
"twitter" => array(
85
    "url" => "http://twitter.com/home?status=".htmlentities(urlencode("currently reading: ").self::$encoded_page_url),
86
    "faicon" => "fa-twitter-square",
87
    "title" => "Tweet It"),
88
"thumblr" => array(
89
    "url" => "http://www.tumblr.com/share/link?url=".htmlentities(self::$encoded_page_url."&name=".self::$encoded_page_title),
90
    "faicon" => "fa-tumblr-square",
91
    "title" => "Thumblr")
92
);
93
        return self::$page_specific_data;
94
    }
95
96
    public static function get_page_specific_data($title, $link, $description = '')
97
    {
98
        $originalArray = self::$page_specific_data ? self::$page_specific_data : self::get_all_options($title, $link, $description);
99
        $finalArray = array();
0 ignored issues
show
Unused Code introduced by
$finalArray is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
100
        $inc = Config::inst()->get(ShareThisSTE::class, "included_icons");
101
        $exc = Config::inst()->get(ShareThisSTE::class, "excluded_icons");
102
        if (count($inc)) {
103
            $new_array_of_icons_to_include = array();
104
            foreach ($inc as $key => $value) {
105
                $new_array_of_icons_to_include[$value] = $value;
106
                if (! isset($originalArray[$value])) {
107
                    Debug::show("Error in ShareIcons::set_icons_to_include, $key does not exist in bookmark list");
108
                }
109
            }
110
            foreach ($originalArray as $key => $array) {
111
                if (! isset($new_array_of_icons_to_include[$key])) {
112
                    unset($originalArray[$key]);
113
                }
114
            }
115
        }
116
        //which ones do we exclude
117
        if (count($exc)) {
118
            foreach ($exc as $key) {
119
                if (! isset($originalArray[$key])) {
120
                    Debug::show("Error in ShareIcons::set_icons_to_exclude, $key does not exist in bookmark list");
121
                } else {
122
                    unset($originalArray[$key]);
123
                }
124
            }
125
        }
126
        if (! $link) {
127
            self::$page_specific_data = null;
128
        }
129
        return $originalArray;
130
    }
131
132
    /*
133
        summary: (required) utf-8 string, defaults to document.title
134
        content: (optional) utf-8 string, defaults to null
135
        updated: (optional) ISO 8601 date, defaults to document.lastModified
136
        published: (optional) ISO 8601 date, defaults to null
137
        author: currently not implemented
138
        category: currently not implemented
139
    */
140
    public static function get_share_all()
141
    {
142
        //self::set_variables($title, $link, $description);
143
        self::$share_all_data = '
144
<script type="text/javascript" src="http://w.sharethis.com/button/sharethis.js#&amp;type=website"></script>
145
<script type="text/javascript">
146
SHARETHIS.addEntry(
147
	{
148
		title:"' . urldecode(self::$encoded_page_title) . '",
149
		summary:"' . urldecode(self::$encoded_page_title) . '",
150
		url:"' . urldecode(self::$encoded_page_url) . '",
151
		icon:"' . urldecode(self::$icon) . '"
152
	},
153
	{button:true}
154
);
155
</script>';
156
        return self::$share_all_data;
157
    }
158
159
    public static function set_general_data()
160
    {
161
        self::$general_data = null;
162
    }
163
    public static function get_general_data()
164
    {
165
        if (! self::$general_data) {
166
            $array = self::get_page_specific_data('', '', '');
167
            $newArray = array();
168
            if (count($array)) {
169
                foreach ($array as $key => $subArray) {
170
                    $newArray[$key] = $key;
171
                }
172
            }
173
            self::$general_data = $newArray;
174
        }
175
        return self::$general_data;
176
    }
177
178
    private static function set_variables($title, $link, $description)
179
    {
180
        self::$icon = urlencode(Director::absoluteBaseURL() . 'favicon.ico');
181
        self::$non_encoded_page_url = Director::absoluteURL($link);
182
        self::$encoded_page_url = urlencode(self::$non_encoded_page_url);
183
        self::$encoded_page_title = urlencode($title);
184
        self::$encoded_page_title_space_encoded = str_replace('+', '%20', urlencode($title));
185
        if ($description) {
186
            self::$encoded_description = urlencode($description);
187
        } else {
188
            self::$encoded_description = self::$encoded_page_title;
189
        }
190
    }
191
192
    private function facebookLike()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
193
    {
194
        //see http://developers.facebook.com/docs/reference/plugins/like/
195
        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>';
196
    }
197
}
198