| 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 |
$icon_style = ''; |
| 79 |
|
| 80 |
if ($attributes['iconColorOption'] != 'blockspare-default-official-color') { |
| 81 |
|
| 82 |
$icon_style = "color:" . $attributes['customfontColorOption'] . ';'; |
| 83 |
$icon_style .= " background-color:" . $attributes['custombackgroundColorOption'] . ';'; |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
$is_amp_endpoint = function_exists('is_amp_endpoint') && is_amp_endpoint(); |
| 89 |
|
| 90 |
$share_url = ''; |
| 91 |
|
| 92 |
|
| 93 |
if (isset($attributes['facebook']) && $attributes['facebook']) { |
| 94 |
$facebook_url = 'https://www.facebook.com/sharer/sharer.php?u=' . get_the_permalink() . '&title=' . get_the_title() . ''; |
| 95 |
$share_url .= blockspare_render_social_sharing_block_item($attributes['facebookTitle'], $facebook_url, 'blockspare-share-facebook', 'fab fa-facebook-f', $icon_style, $is_amp_endpoint); |
| 96 |
} |
| 97 |
|
| 98 |
if (isset($attributes['twitter']) && $attributes['twitter']) { |
| 99 |
$twitter_url = 'http://twitter.com/share?text=' . get_the_title() . '&url=' . get_the_permalink() . ''; |
| 100 |
$share_url .= blockspare_render_social_sharing_block_item($attributes['twitterTitle'], $twitter_url, 'blockspare-share-twitter', 'fab fa-twitter', $icon_style, $is_amp_endpoint); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
if (isset($attributes['pinterest']) && $attributes['pinterest']) { |
| 105 |
|
| 106 |
$pinterest_url = 'https://pinterest.com/pin/create/button/?&url=' . get_the_permalink() . '&description=' . get_the_title() . '&media=' . esc_url($thumbnail) . ''; |
| 107 |
$share_url .= blockspare_render_social_sharing_block_item($attributes['pinterestTitle'], $pinterest_url, 'blockspare-share-pinterest', 'fab fa-pinterest-p', $icon_style, $is_amp_endpoint); |
| 108 |
} |
| 109 |
|
| 110 |
if (isset($attributes['linkedin']) && $attributes['linkedin']) { |
| 111 |
|
| 112 |
$linkedin_url = 'https://www.linkedin.com/shareArticle?mini=true&url=' . get_the_permalink() . '&title=' . get_the_title() . ''; |
| 113 |
$share_url .= blockspare_render_social_sharing_block_item($attributes['linkedinTitle'], $linkedin_url, 'blockspare-share-linkedin', 'fab fa-linkedin-in', $icon_style, $is_amp_endpoint); |
| 114 |
} |
| 115 |
|
| 116 |
if (isset($attributes['reddit']) && $attributes['reddit']) { |
| 117 |
|
| 118 |
|
| 119 |
$reddit_url = 'https://www.reddit.com/submit?url=' . get_the_permalink() . ''; |
| 120 |
$share_url .= blockspare_render_social_sharing_block_item($attributes['redditTitle'], $reddit_url, 'blockspare-share-reddit', 'fab fa-reddit-alien', $icon_style, $is_amp_endpoint); |
| 121 |
} |
| 122 |
|
| 123 |
if (isset($attributes['email']) && $attributes['email']) { |
| 124 |
|
| 125 |
$email_url = 'mailto:?subject=' . get_the_title() . '&body=' . get_the_title() . '—' . get_the_permalink() . ''; |
| 126 |
$share_url .= blockspare_render_social_sharing_block_item($attributes['emailTitle'], $email_url, 'blockspare-share-email', 'fas fa-envelope', $icon_style, false); |
| 127 |
} |
| 128 |
|
| 129 |
$block_content = sprintf( |
| 130 |
' |
| 131 |
<div style="text-align: %7$s" > |
| 132 |
<ul class="blockspare-social-sharing %2$s %3$s %4$s %5$s %6$s">%1$s</ul> |
| 133 |
</div> |
| 134 |
', |
| 135 |
$share_url, |
| 136 |
isset($attributes['iconColorOption']) ? $attributes['iconColorOption'] : null, |
| 137 |
isset($attributes['buttonOptions']) ? $attributes['buttonOptions'] : null, |
| 138 |
isset($attributes['buttonShapes']) ? $attributes['buttonShapes'] : null, |
| 139 |
isset($attributes['buttonSizes']) ? $attributes['buttonSizes'] : null, |
| 140 |
isset($attributes['buttonFills']) ? $attributes['buttonFills'] : null, |
| 141 |
isset($attributes['sectionAlignment']) ? $attributes['sectionAlignment'] : null |
| 142 |
|
| 143 |
|
| 144 |
); |
| 145 |
|
| 146 |
return $block_content; |
| 147 |
} |
| 148 |
|
| 149 |
function blockspare_render_social_sharing_block_item($attribute_title, $attribute_url, $item_class, $icon_class, $icon_style, $is_amp_endpoint) |
| 150 |
{ |
| 151 |
|
| 152 |
|
| 153 |
$share_url = ''; |
| 154 |
|
| 155 |
if (!$is_amp_endpoint) { |
| 156 |
$share_url .= sprintf( |
| 157 |
'<li> |
| 158 |
<a |
| 159 |
href="%1$s" |
| 160 |
class="%4$s" |
| 161 |
title="%2$s" |
| 162 |
style="%3$s"> |
| 163 |
<i class="%5$s"></i> <span class="blockspare-social-text">%2$s</span> |
| 164 |
</a> |
| 165 |
</li>', |
| 166 |
$attribute_url, |
| 167 |
esc_html__($attribute_title), |
| 168 |
esc_attr__($icon_style), |
| 169 |
esc_attr__($item_class), |
| 170 |
esc_attr__($icon_class) |
| 171 |
|
| 172 |
); |
| 173 |
} else { |
| 174 |
|
| 175 |
$href_format = sprintf('href="javascript:void(0)" onClick="javascript:blockspareBlocksShare(\'%1$s\', \'%2$s\', \'600\', \'600\')"', esc_url($attribute_url), esc_html__($attribute_title)); |
| 176 |
|
| 177 |
if ($is_amp_endpoint) { |
| 178 |
$href_format = sprintf('href="%1$s"', esc_url($attribute_url)); |
| 179 |
} |
| 180 |
$share_url .= sprintf( |
| 181 |
'<li> |
| 182 |
<a |
| 183 |
%1$s |
| 184 |
class="%4$s" |
| 185 |
title="%2$s" |
| 186 |
style="%3$s"> |
| 187 |
<i class="%5$s"></i> <span class="blockspare-social-text">%2$s</span> |
| 188 |
</a> |
| 189 |
</li>', |
| 190 |
$href_format, |
| 191 |
esc_html__($attribute_title), |
| 192 |
esc_attr__($icon_style), |
| 193 |
esc_attr__($item_class), |
| 194 |
esc_attr__($icon_class) |
| 195 |
|
| 196 |
); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
return $share_url; |
| 201 |
} |
| 202 |
|