PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 1.1.1
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v1.1.1
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.1.1, at src/blocks/social-sharing/index.php

229 lines 7.4 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{
164 color:' . $attributes['customfontColorOption'] .';
165 background-color:' . $attributes['custombackgroundColorOption'] . ';
166 }';
167 }
168 $block_content .=' .' . $blockuniqueclass . ' .blockspare-social-wrapper{
169 text-align:'.$attributes['sectionAlignment'].';
170 }';
171
172 $block_content .= '</style>';
173
174 return $block_content;
175 }
176
177 function blockspare_render_social_sharing_block_item($attribute_title, $attribute_url, $item_class, $icon_class, $icon_style, $is_amp_endpoint)
178 {
179
180
181 $share_url = '';
182
183 if (!$is_amp_endpoint) {
184 $share_url .= sprintf(
185 '<li>
186 <a
187 href="%1$s"
188 class="%4$s"
189 title="%2$s"
190 style="%3$s">
191 <i class="%5$s"></i> <span class="blockspare-social-text">%2$s</span>
192 </a>
193 </li>',
194 $attribute_url,
195 esc_html__($attribute_title),
196 esc_attr__($icon_style),
197 esc_attr__($item_class),
198 esc_attr__($icon_class)
199
200 );
201 } else {
202
203 $href_format = sprintf('href="javascript:void(0)" onClick="javascript:blockspareBlocksShare(\'%1$s\', \'%2$s\', \'600\', \'600\')"', esc_url($attribute_url), esc_html__($attribute_title));
204
205 if ($is_amp_endpoint) {
206 $href_format = sprintf('href="%1$s"', esc_url($attribute_url));
207 }
208 $share_url .= sprintf(
209 '<li>
210 <a
211 %1$s
212 class="%3$s"
213 title="%2$s"
214 >
215 <i class="%4$s"></i> <span class="blockspare-social-text">%2$s</span>
216 </a>
217 </li>',
218 $href_format,
219 esc_html__($attribute_title),
220 esc_attr__($item_class),
221 esc_attr__($icon_class)
222
223 );
224 }
225
226
227 return $share_url;
228 }
229