| 1 |
<?php |
| 2 |
namespace ULTP\blocks; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class Post_Social_Share { |
| 7 |
public function __construct() { |
| 8 |
add_action( 'init', array( $this, 'register' ) ); |
| 9 |
} |
| 10 |
public function get_attributes() { |
| 11 |
return array( |
| 12 |
'blockId' => '', |
| 13 |
'repetableField' => array( |
| 14 |
array( |
| 15 |
'type' => 'facebook', |
| 16 |
'enableLabel' => true, |
| 17 |
'label' => 'Facebook', |
| 18 |
'iconColor' => '#fff', |
| 19 |
'iconColorHover' => '#d2d2d2', |
| 20 |
'shareBg' => '#4267B2', |
| 21 |
'bgHoverColor' => '#f5f5f5', |
| 22 |
), |
| 23 |
array( |
| 24 |
'type' => 'twitter', |
| 25 |
'enableLabel' => true, |
| 26 |
'label' => 'Twitter', |
| 27 |
'iconColor' => '#fff', |
| 28 |
'iconColorHover' => '#d2d2d2', |
| 29 |
'shareBg' => '#1DA1F2', |
| 30 |
'bgHoverColor' => '#f5f5f5', |
| 31 |
), |
| 32 |
array( |
| 33 |
'type' => 'pinterest', |
| 34 |
'enableLabel' => true, |
| 35 |
'label' => 'Pinterest', |
| 36 |
'iconColor' => '#fff', |
| 37 |
'iconColorHover' => '#d2d2d2', |
| 38 |
'shareBg' => '#E60023', |
| 39 |
'bgHoverColor' => '#f5f5f5', |
| 40 |
), |
| 41 |
array( |
| 42 |
'type' => 'linkedin', |
| 43 |
'enableLabel' => true, |
| 44 |
'label' => 'Linkedin', |
| 45 |
'iconColor' => '#fff', |
| 46 |
'iconColorHover' => '#d2d2d2', |
| 47 |
'shareBg' => '#0A66C2', |
| 48 |
'bgHoverColor' => '#f5f5f5', |
| 49 |
), |
| 50 |
array( |
| 51 |
'type' => 'mail', |
| 52 |
'enableLabel' => true, |
| 53 |
'label' => 'Mail', |
| 54 |
'iconColor' => '#fff', |
| 55 |
'iconColorHover' => '#d2d2d2', |
| 56 |
'shareBg' => '#EA4335', |
| 57 |
'bgHoverColor' => '#f5f5f5', |
| 58 |
), |
| 59 |
), |
| 60 |
/* |
| 61 |
============================ |
| 62 |
Social Share item style |
| 63 |
============================*/ |
| 64 |
'disInline' => true, |
| 65 |
|
| 66 |
/* |
| 67 |
============================ |
| 68 |
Post Social Share Label Style |
| 69 |
============================*/ |
| 70 |
'shareLabelShow' => true, |
| 71 |
'shareLabelStyle' => 'style1', |
| 72 |
'shareCountShow' => true, |
| 73 |
'shareCountLabel' => 'Shares', |
| 74 |
|
| 75 |
/* |
| 76 |
============================ |
| 77 |
Post Social Share sticky position Style |
| 78 |
============================*/ |
| 79 |
'enableSticky' => false, |
| 80 |
'stopSticky' => false, |
| 81 |
|
| 82 |
// -------------------------- |
| 83 |
// Advanced Settings |
| 84 |
// -------------------------- |
| 85 |
'advanceId' => '', |
| 86 |
'advanceZindex' => '', |
| 87 |
'hideExtraLarge' => false, |
| 88 |
'hideDesktop' => false, |
| 89 |
'hideTablet' => false, |
| 90 |
'hideMobile' => false, |
| 91 |
'advanceCss' => '', |
| 92 |
); |
| 93 |
} |
| 94 |
public function register() { |
| 95 |
register_block_type( |
| 96 |
'ultimate-post/post-social-share', |
| 97 |
array( |
| 98 |
'editor_script' => 'ultp-blocks-editor-script', |
| 99 |
'editor_style' => 'ultp-blocks-editor-css', |
| 100 |
'render_callback' => array( $this, 'content' ), |
| 101 |
) |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
public function share_link( $key = 'facebook', $post_link = '' ) { |
| 106 |
$shareLink = array( |
| 107 |
'facebook' => 'https://www.facebook.com/sharer.php?u=' . $post_link, |
| 108 |
'twitter' => 'https://twitter.com/intent/tweet?url=' . $post_link, |
| 109 |
'linkedin' => 'https://www.linkedin.com/sharing/share-offsite/?url=' . $post_link, |
| 110 |
'pinterest' => 'https://pinterest.com/pin/create/link?url=' . $post_link, |
| 111 |
'whatsapp' => 'https://api.whatsapp.com/send?text=' . $post_link, |
| 112 |
'messenger' => 'https://www.facebook.com/dialog/send?app_id=1904103319867886&link=' . $post_link . '&redirect_uri=' . $post_link, |
| 113 |
'mail' => 'mailto:?body=' . $post_link, |
| 114 |
'reddit' => 'https://www.reddit.com/submit?url=' . $post_link, |
| 115 |
'skype' => 'https://web.skype.com/share?url=' . $post_link, |
| 116 |
); |
| 117 |
return $shareLink[ $key ]; |
| 118 |
} |
| 119 |
|
| 120 |
public function content( $attr, $noAjax ) { |
| 121 |
$attr = wp_parse_args( $attr, $this->get_attributes() ); |
| 122 |
$block_name = 'post_share'; |
| 123 |
|
| 124 |
$wrapper_before = $wrapper_after = $wrapper_content = ''; |
| 125 |
|
| 126 |
$post_id = get_the_ID(); |
| 127 |
$post_link = home_url( esc_url_raw( $_SERVER['REQUEST_URI'] ) ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated |
| 128 |
$total_share = get_post_meta( $post_id, 'share_count', true ); |
| 129 |
$total_share = $total_share ? $total_share : 0; |
| 130 |
|
| 131 |
$attr['className'] = isset( $attr['className'] ) && $attr['className'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['className'] ) : ''; |
| 132 |
$attr['align'] = isset( $attr['align'] ) && $attr['align'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['align'] ) : ''; |
| 133 |
$attr['advanceId'] = isset( $attr['advanceId'] ) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 134 |
$attr['blockId'] = isset( $attr['blockId'] ) ? sanitize_html_class( $attr['blockId'] ) : ''; |
| 135 |
$attr['shareLabelStyle'] = isset( $attr['shareLabelStyle'] ) ? sanitize_html_class( $attr['shareLabelStyle'] ) : ''; |
| 136 |
$allowed_html_tags = ultimate_post()->ultp_allowed_html_tags(); |
| 137 |
$attr['shareCountLabel'] = wp_kses( $attr['shareCountLabel'], $allowed_html_tags ); |
| 138 |
|
| 139 |
$wrapper_before .= '<div ' . ( $attr['advanceId'] ? 'id="' . $attr['advanceId'] . '" ' : '' ) . ' class="wp-block-ultimate-post-' . $block_name . ' ultp-block-' . $attr['blockId'] . ( $attr['className'] ? ' ' . $attr['className'] : '' ) . '' . ( $attr['align'] ? ' align' . $attr['align'] : '' ) . '">'; |
| 140 |
$wrapper_before .= '<div class="ultp-block-wrapper">'; |
| 141 |
$wrapper_content .= '<div class="ultp-post-share">'; |
| 142 |
$wrapper_content .= '<div class="ultp-post-share-layout ultp-inline-' . ( $attr['disInline'] ? 'true' : 'false' ) . ' ' . ( $attr['stopSticky'] && $attr['enableSticky'] ? 'ultp-disable-sticky-footer' : '' ) . '">'; |
| 143 |
if ( $attr['shareLabelShow'] ) { |
| 144 |
$wrapper_content .= '<div class="ultp-post-share-count-section ultp-post-share-count-section-' . $attr['shareLabelStyle'] . '">'; |
| 145 |
if ( $attr['shareLabelStyle'] != 'style2' && $attr['shareCountShow'] ) { |
| 146 |
$wrapper_content .= '<span class="ultp-post-share-count">' . $total_share . '</span>'; |
| 147 |
} |
| 148 |
if ( $attr['shareLabelStyle'] == 'style2' ) { |
| 149 |
$wrapper_content .= '<span class="ultp-post-share-icon-section">' . ultimate_post()->get_svg_icon( 'share' ) . '</span>'; |
| 150 |
} |
| 151 |
if ( $attr['shareLabelStyle'] != 'style2' && $attr['shareCountLabel'] ) { |
| 152 |
$wrapper_content .= '<span class="ultp-post-share-label">' . $attr['shareCountLabel'] . '</span>'; |
| 153 |
} |
| 154 |
$wrapper_content .= '</div>'; |
| 155 |
} |
| 156 |
$wrapper_content .= '<div class="ultp-post-share-item-inner-block" postId="' . $post_id . '" count="' . $total_share . '">'; |
| 157 |
|
| 158 |
foreach ( $attr['repetableField'] as $key => $value ) { |
| 159 |
$value['type'] = sanitize_html_class( $value['type'] ); |
| 160 |
$value['label'] = wp_kses( $value['label'], $allowed_html_tags ); |
| 161 |
$wrapper_content .= '<div class="ultp-post-share-item ultp-repeat-' . sanitize_html_class( $key ) . ' ultp-social-' . $value['type'] . '">'; |
| 162 |
$wrapper_content .= '<a href="javascript:" class="ultp-post-share-item-' . $value['type'] . '" url="' . $this->share_link( $value['type'], $post_link ) . '">'; |
| 163 |
$wrapper_content .= '<span class="ultp-post-share-item-icon">' . ultimate_post()->get_svg_icon( $value['type'] ) . '</span>'; |
| 164 |
$wrapper_content .= '' . $value['enableLabel'] ? '<span class="ultp-post-share-item-label">' . $value['label'] . '</span>' : '' . ' '; |
| 165 |
$wrapper_content .= '</a>'; |
| 166 |
$wrapper_content .= '</div>'; |
| 167 |
} |
| 168 |
$wrapper_content .= '</div>'; |
| 169 |
$wrapper_content .= '</div>'; |
| 170 |
$wrapper_content .= '</div>'; |
| 171 |
$wrapper_after .= '</div>'; |
| 172 |
$wrapper_after .= '</div>'; |
| 173 |
|
| 174 |
return $wrapper_before . $wrapper_content . $wrapper_after; |
| 175 |
} |
| 176 |
} |
| 177 |
|