├── readme.md
├── views
└── helpers
│ └── share_this.php
└── tests
└── cases
└── views
└── helpers
└── share_this.test.php
/readme.md:
--------------------------------------------------------------------------------
1 | ## CakeSocial CakePHP Plugin ##
2 |
3 | The CakeSocial plugin for CakePHP allows for simple integration of social media into your CakePHP application.
4 |
5 | ### Supported ###
6 |
7 | - [ShareThis](http://sharethis.com) for social link sharing and publicising. (Includes 44 social networks, including: Twitter, Facebook, MySpace, Slashdot, Reddit, and more)
8 |
9 | ### Planned ###
10 |
11 | - [AddThis](http://addthis.com)
12 |
13 | ## CakePHP Version ##
14 |
15 | This plugin is built for CakePHP 1.3 and above.
16 |
17 | Upgrade from CakePHP 1.2 to CakePHP 1.3 takes about 5 minutes, checkout the awesome [migration guide](http://book.cakephp.org/view/1561/Migrating-from-CakePHP-1-2-to-1-3).
18 |
19 | ## Installation ##
20 |
21 | ### Downloading an archive ###
22 |
23 | If you downloaded a tar.gz, tar.bz2 or zip file, extract the contents of the archive to your applications `/plugins` directory.
24 |
25 | The result will be a `cake_social` directory under your `plugins` directory.
26 |
27 | ### Using a git submodule ###
28 |
29 | Navigate to the root of your application, and add the CakeSocial plugin as a submodule with the following git command:
30 |
31 | git submodule add http://github.com/predominant/cake_social.git plugins/cake_social
32 | git submodule init
33 | git submodule update
34 |
35 | ## Usage ##
36 |
37 | The simplest method of usage is to include each helper you want to use on your AppController class.
38 |
39 | If you do not have one already, create the file `app_controller.php` in the root of your application, and add the following:
40 |
41 | array(
47 | 'publisher' => 'my-publisher-id',
48 | ),
49 | 'Session'
50 | );
51 | }
52 |
53 | Now you can just display the widget on any page with the following CakePHP view code:
54 |
55 | echo $this->ShareThis->display();
56 |
57 | Simple!
58 |
59 | Note: The ShareThis helper doesn't require the Session helper, its just included to show that other helpers go in there also, and to help copy+paste help vampires who after migrating to CakePHP 1.3, will get SessionHelper errors if they don't include it.
60 |
61 | ## Comments, Suggestions, and other... ##
62 |
63 | I'd be more than thrilled to hear anyones experiences with this CakeSocial plugin.
64 |
65 | If you have suggestions, enhancement requests, or bugs, please let me know.
66 |
67 | ## Contributors ##
68 |
69 | * Predominant (Graham Weldon) [Website](http://grahamweldon.com)
70 | * jose_zap (José Lorenzo Rodríguez)
71 | * real34 (Pierre Martin) [Website](http://www.pierre-martin.fr)
72 |
--------------------------------------------------------------------------------
/views/helpers/share_this.php:
--------------------------------------------------------------------------------
1 | true,
95 | 'publisher' => '',
96 | 'buttonJs' => 'http://w.sharethis.com/button/buttons.js',
97 | 'style' => '',
98 | 'embeds' => 'true'
99 | );
100 |
101 | /**
102 | * Constructor
103 | *
104 | * Takes options to configure instance variables like publisher
105 | *
106 | * ### Options
107 | *
108 | * - `publisher` (string) Publisher key from [ShareThis](http://sharethis.com)
109 | *
110 | * @param array $options
111 | */
112 | public function __construct($options = array()) {
113 | $this->_options = array_merge($this->_options, $options);
114 | }
115 |
116 | /**
117 | * Display the ShareThis widget
118 | *
119 | * @param array $types Social Media types (See ShareThisHelper::$_types)
120 | * @param array $options Display options (See ShareThisHelper::$_options)
121 | * @return string Html for the ShareThis widget
122 | */
123 | public function display($types = array(), $options = array()) {
124 | if (is_array($types)) {
125 | if (empty($types)) {
126 | $types = $this->_defaultTypes;
127 | }
128 | } else {
129 | $types = array();
130 | }
131 |
132 | $options = array_merge($this->_options, $options);
133 | $result = '';
134 | foreach ($types as $type) {
135 | $result .= $this->socialType($type);
136 | }
137 | if ($options['sharethis']) {
138 | $result .= $this->socialType('sharethis');
139 | }
140 | $this->_scripts($options);
141 | return $result;
142 | }
143 |
144 | /**
145 | * Generate a single social type span element
146 | *
147 | * @param string $type Social type key (See ShareThisHelper::$_types)
148 | * @return string Social Type HTML
149 | */
150 | public function socialType($type, $options = array()) {
151 | $options = array_merge($this->_options, $options);
152 | $attributes = array('class' => 'st_' . $type);
153 | if (in_array($options['style'], $this->_styles)) {
154 | $attributes['class'] .= '_' . $options['style'];
155 | }
156 | if (!empty($options['url'])) {
157 | $attributes['st_url'] = $this->url($options['url'], true);
158 | }
159 | if (!empty($options['title'])) {
160 | $attributes['st_title'] = $options['title'];
161 | }
162 | return $this->Html->tag('span', '', $attributes);
163 | }
164 |
165 | /**
166 | * Generate required Javascript
167 | *
168 | * @param array $options Options
169 | * @return void
170 | */
171 | public function _scripts($options = array()) {
172 | $options = array_merge($this->_options, $options);
173 | $this->Html->script($options['buttonJs'], array('inline' => false));
174 | $this->Html->ScriptBlock(sprintf(
175 | 'stLight.options({publisher:\'%s\', embeds:%s});', $options['publisher'], $options['embeds']
176 | ), array('inline' => false));
177 | }
178 | }
179 |
--------------------------------------------------------------------------------
/tests/cases/views/helpers/share_this.test.php:
--------------------------------------------------------------------------------
1 | __scripts;
53 | }
54 | }
55 |
56 | /**
57 | * ShareThis Test Case
58 | *
59 | * @package cake_social
60 | * @subpackage cake_social.tests.views.helpers
61 | * @author Graham Weldon (http://grahamweldon.com)
62 | */
63 | class ShareThisHelperTestCase extends CakeTestCase {
64 |
65 | /**
66 | * Start Test
67 | *
68 | * Setup class vars for testing
69 | *
70 | * @return void
71 | */
72 | public function startTest() {
73 | $this->ShareThis = new ShareThisHelper();
74 | $this->ShareThis->Html = new HtmlHelper();
75 | $this->View =& new TheView(new TheJsTestController());
76 | ClassRegistry::addObject('view', $this->View);
77 | }
78 |
79 | /**
80 | * End Test
81 | *
82 | * Clean up after each test is run
83 | *
84 | * @return void
85 | */
86 | public function endTest() {
87 | unset($this->ShareThis);
88 | ClassRegistry::removeObject('view');
89 | unset($this->View);
90 | }
91 |
92 | /**
93 | * testSocialType
94 | *
95 | * @return void
96 | */
97 | public function testSocialType() {
98 | $expected = '';
99 | $result = $this->ShareThis->socialType('test');
100 | $this->assertIdentical($expected, $result);
101 | }
102 |
103 | /**
104 | * testSocialTypeStyleLarge
105 | *
106 | * @return void
107 | */
108 | public function testSocialTypeStyleLarge() {
109 | $expected = '';
110 | $result = $this->ShareThis->socialType('test', array('style' => 'large'));
111 | $this->assertIdentical($expected, $result);
112 | }
113 |
114 | /**
115 | * testSocialTypeStyleButton
116 | *
117 | * @return void
118 | */
119 | public function testSocialTypeStyleButton() {
120 | $expected = '';
121 | $result = $this->ShareThis->socialType('test', array('style' => 'button'));
122 | $this->assertIdentical($expected, $result);
123 | }
124 |
125 | /**
126 | * testSocialType with a custom page
127 | *
128 | * @return void
129 | */
130 | public function testSocialTypeStyleCustomPage() {
131 | $expected = '';
132 | $result = $this->ShareThis->socialType(
133 | 'test',
134 | array('url' => 'http://example.com', 'title' => 42));
135 | $this->assertIdentical($expected, $result);
136 | }
137 |
138 | /**
139 | * testScripts
140 | *
141 | * @return void
142 | */
143 | public function testScripts() {
144 | $this->ShareThis->display();
145 | $result = $this->View->scripts();
146 | foreach ($result as &$script) {
147 | $script = str_replace("\n", '', $script);
148 | }
149 | $this->assertIdentical(
150 | $result,
151 | array(
152 | '',
153 | ''
154 | )
155 | );
156 | }
157 |
158 | /**
159 | * testPublisherScripts
160 | *
161 | * @return void
162 | */
163 | public function testPublisherScripts() {
164 | $this->ShareThis->display(array(), array('publisher' => 'Mr. Man'));
165 | $result = $this->View->scripts();
166 | foreach ($result as &$script) {
167 | $script = str_replace("\n", '', $script);
168 | }
169 | $this->assertIdentical(
170 | $result,
171 | array(
172 | '',
173 | ''
174 | )
175 | );
176 | }
177 |
178 | /**
179 | * testDefault
180 | *
181 | * @return void
182 | */
183 | public function testDefault() {
184 | $expected = '';
185 | $result = $this->ShareThis->display();
186 | $this->assertIdentical($expected, $result);
187 | }
188 |
189 | /**
190 | * testSingle
191 | *
192 | * @return void
193 | */
194 | public function testSingle() {
195 | $expected = '';
196 | $result = $this->ShareThis->display(array('twitter'));
197 | $this->assertIdentical($expected, $result);
198 | }
199 |
200 | /**
201 | * testNoShareThis
202 | *
203 | * @return void
204 | */
205 | public function testNoShareThis() {
206 | $expected = '';
207 | $result = $this->ShareThis->display(array(), array('sharethis' => false));
208 | $this->assertIdentical($expected, $result);
209 | }
210 |
211 | /**
212 | * testNonArrayTypes
213 | *
214 | * @return void
215 | */
216 | public function testNonArrayTypes() {
217 | $expected = '';
218 | $result = $this->ShareThis->display(null);
219 | $this->assertIdentical($expected, $result);
220 | }
221 | }
222 |
--------------------------------------------------------------------------------