PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 1.3.0
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v1.3.0
4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 2.6.1 All 38 releases
blockspare / src / blocks / social-sharing / index.php

index.php in BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites 1.3.0, at src/blocks/social-sharing/index.php

235 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Server-side rendering for the sharing block
4 *
5 * @since 1.1.2
6 * @package Blockspare
7 */
8
9 /**
10 * Register the block on the server
11 */
12 function blockspare_register_social_sharing_block()
13 {
14
15 if (!function_exists('register_block_type')) {
16 return;
17 }
18 ob_start();
19 include BLOCKSPARE_PLUGIN_DIR . '/src/blocks/social-sharing/block.json';
20
21 $metadata = json_decode(ob_get_clean(), true);
22
23 register_block_type(
24 'blockspare/blockspare-social-sharing',
25
26 array(
27 'style' => 'blockspare-style-css',
28 'attributes' => $metadata['attributes'],
29 'render_callback' => 'blockspare_render_social_sharing_block',
30 )
31 );
32 }
33
34 add_action('init', 'blockspare_register_social_sharing_block');
35
36
37 /**
38 * Add the pop-up share window to the footer
39 */
40 function blockspare_social_sharing_block_icon_footer_script()
41 {
42
43 if (function_exists('is_amp_endpoint') && is_amp_endpoint()) {
44 return;
45 }
46 ?>
47 <script type="text/javascript">
48 function blockspareBlocksShare(url, title, w, h) {
49 var left = (window.innerWidth / 2) - (w / 2);
50 var top = (window.innerHeight / 2) - (h / 2);
51 return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=600, height=600, top=' + top + ', left=' + left);
52 }
53 </script>
54 <?php
55 }
56
57 add_action('wp_footer', 'blockspare_social_sharing_block_icon_footer_script');
58
59 /**
60 * Render the sharing links
61 *
62 * @param array $attributes The block attributes.
63 *
64 * @return string The block HTML.
65 */
66 function blockspare_render_social_sharing_block($attributes)
67 {
68
69 global $post;
70
71 if (has_post_thumbnail()) {
72 $thumbnail_id = get_post_thumbnail_id($post->ID);
73 $thumbnail = $thumbnail_id ? current(wp_get_attachment_image_src($thumbnail_id, 'large', true)) : '';
74 } else {
75 $thumbnail = null;
76 }
77
78
79 $unq_class = mt_rand(100000,999999);
80 $blockuniqueclass = '';
81
82 if(!empty($attributes['uniqueClass'])){
83 $blockuniqueclass = $attributes['uniqueClass'];
84 }else{
85 $blockuniqueclass = 'blockspare-share-'.$unq_class;
86 }
87
88 $icon_style = '';
89
90 if ($attributes['iconColorOption'] != 'blockspare-default-official-color') {
91
92 $icon_style = "color:" . $attributes['customfontColorOption'] . ';';
93 $icon_style .= " background-color:" . $attributes['custombackgroundColorOption'] . ';';
94
95 }
96
97
98 $is_amp_endpoint = function_exists('is_amp_endpoint') && is_amp_endpoint();
99
100 $share_url = '';
101
102
103 if (isset($attributes['facebook']) && $attributes['facebook']) {
104 $facebook_url = 'https://www.facebook.com/sharer/sharer.php?u=' . get_the_permalink() . '&title=' . get_the_title() . '';
105 $share_url .= blockspare_render_social_sharing_block_item($attributes['facebookTitle'], $facebook_url, 'blockspare-share-facebook', 'fab fa-facebook-f', $icon_style, $is_amp_endpoint);
106 }
107
108 if (isset($attributes['twitter']) && $attributes['twitter']) {
109 $twitter_url = 'http://twitter.com/share?text=' . get_the_title() . '&url=' . get_the_permalink() . '';
110 $share_url .= blockspare_render_social_sharing_block_item($attributes['twitterTitle'], $twitter_url, 'blockspare-share-twitter', 'fab fa-twitter', $icon_style, $is_amp_endpoint);
111 }
112
113
114 if (isset($attributes['pinterest']) && $attributes['pinterest']) {
115
116 $pinterest_url = 'https://pinterest.com/pin/create/button/?&url=' . get_the_permalink() . '&description=' . get_the_title() . '&media=' . esc_url($thumbnail) . '';
117 $share_url .= blockspare_render_social_sharing_block_item($attributes['pinterestTitle'], $pinterest_url, 'blockspare-share-pinterest', 'fab fa-pinterest-p', $icon_style, $is_amp_endpoint);
118 }
119
120 if (isset($attributes['linkedin']) && $attributes['linkedin']) {
121
122 $linkedin_url = 'https://www.linkedin.com/shareArticle?mini=true&url=' . get_the_permalink() . '&title=' . get_the_title() . '';
123 $share_url .= blockspare_render_social_sharing_block_item($attributes['linkedinTitle'], $linkedin_url, 'blockspare-share-linkedin', 'fab fa-linkedin-in', $icon_style, $is_amp_endpoint);
124 }
125
126 if (isset($attributes['reddit']) && $attributes['reddit']) {
127
128
129 $reddit_url = 'https://www.reddit.com/submit?url=' . get_the_permalink() . '';
130 $share_url .= blockspare_render_social_sharing_block_item($attributes['redditTitle'], $reddit_url, 'blockspare-share-reddit', 'fab fa-reddit-alien', $icon_style, $is_amp_endpoint);
131 }
132
133 if (isset($attributes['email']) && $attributes['email']) {
134
135 $email_url = 'mailto:?subject=' . get_the_title() . '&body=' . get_the_title() . '&mdash;' . get_the_permalink() . '';
136 $share_url .= blockspare_render_social_sharing_block_item($attributes['emailTitle'], $email_url, 'blockspare-share-email', 'fas fa-envelope', $icon_style, false);
137 }
138
139 $block_content = sprintf(
140 '
141 <div class="%7$s">
142 <div class="blockspare-blocks blockspare-social-wrapper">
143 <ul class="blockspare-social-sharing %2$s %3$s %4$s %5$s %6$s">%1$s</ul>
144 </div>
145 </div>
146 ',
147 $share_url,
148 isset($attributes['iconColorOption']) ? $attributes['iconColorOption'] : null,
149 isset($attributes['buttonOptions']) ? $attributes['buttonOptions'] : null,
150 isset($attributes['buttonShapes']) ? $attributes['buttonShapes'] : null,
151 isset($attributes['buttonSizes']) ? $attributes['buttonSizes'] : null,
152 isset($attributes['buttonFills']) ? $attributes['buttonFills'] : null,
153 $blockuniqueclass
154
155
156
157 );
158
159 $block_content .= '<style type="text/css">';
160
161 if ($attributes['iconColorOption'] != 'blockspare-default-official-color') {
162
163 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-social-sharing a span{
164 color:' . $attributes['customfontColorOption'] .';
165 background-color:' . $attributes['custombackgroundColorOption'] . ';
166 }';
167 }
168 $block_content .=' .' . $blockuniqueclass . ' .blockspare-social-wrapper{
169 text-align:'.$attributes['sectionAlignment'].';
170 margin-top:'.$attributes['marginTop'].';
171 margin-bottom:'.$attributes['marginBottom'].';
172
173 }';
174
175 $block_content .= '</style>';
176
177 return $block_content;
178 }
179
180 function blockspare_render_social_sharing_block_item($attribute_title, $attribute_url, $item_class, $icon_class, $icon_style, $is_amp_endpoint)
181 {
182
183
184 $share_url = '';
185
186 if (!$is_amp_endpoint) {
187 $share_url .= sprintf(
188 '<li>
189 <a
190 href="%1$s"
191 class="%4$s"
192 title="%2$s"
193 style="%3$s">
194 <span class="blockspare-social-icons">
195 <i class="%5$s"></i> <span class="blockspare-social-text">%2$s</span>
196 </span>
197 </a>
198 </li>',
199 $attribute_url,
200 esc_html__($attribute_title),
201 esc_attr__($icon_style),
202 esc_attr__($item_class),
203 esc_attr__($icon_class)
204
205 );
206 } else {
207
208 $href_format = sprintf('href="javascript:void(0)" onClick="javascript:blockspareBlocksShare(\'%1$s\', \'%2$s\', \'600\', \'600\')"', esc_url($attribute_url), esc_html__($attribute_title));
209
210 if ($is_amp_endpoint) {
211 $href_format = sprintf('href="%1$s"', esc_url($attribute_url));
212 }
213 $share_url .= sprintf(
214 '<li>
215 <a
216 %1$s
217 class="%3$s"
218 title="%2$s"
219 ><span class="blockspare-social-icons">
220 <i class="%4$s"></i> <span class="blockspare-social-text">%2$s</span>
221 </span>
222 </a>
223 </li>',
224 $href_format,
225 esc_html__($attribute_title),
226 esc_attr__($item_class),
227 esc_attr__($icon_class)
228
229 );
230 }
231
232
233 return $share_url;
234 }
235