| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License |
| 6 |
as published by the Free Software Foundation; either version 2 |
| 7 |
of the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 |
|
| 18 |
Copyright 2017 Buttonizer |
| 19 |
*/ |
| 20 |
namespace Buttonizer; |
| 21 |
|
| 22 |
# No script kiddies |
| 23 |
defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); |
| 24 |
class Button |
| 25 |
{ |
| 26 |
// Saved settings |
| 27 |
private $aButtons ; |
| 28 |
private $aPageSettings ; |
| 29 |
private $aSettings ; |
| 30 |
private $aOpeningData ; |
| 31 |
// Default settings |
| 32 |
private $bIsMobile = false ; |
| 33 |
private $bIsOpened = true ; |
| 34 |
// Current page |
| 35 |
private $sCurrentPageId = 0 ; |
| 36 |
private $sCurrentPageUrl = '' ; |
| 37 |
private $sCurrentPageTitle = '' ; |
| 38 |
private $sCurrentPageDescription = '' ; |
| 39 |
// Output string |
| 40 |
private $iAmountOfButtons = 0 ; |
| 41 |
private $iAmountOfShareButtons = 0 ; |
| 42 |
private $sOutput = '' ; |
| 43 |
public function __construct( $bIsMobile ) |
| 44 |
{ |
| 45 |
// Set some data |
| 46 |
$this->bIsMobile = (bool) $bIsMobile; |
| 47 |
add_action( 'wp_footer', function () { |
| 48 |
// Setup |
| 49 |
$this->setup(); |
| 50 |
// Generate |
| 51 |
$this->generate(); |
| 52 |
// Share buttons |
| 53 |
$this->share_btns(); |
| 54 |
// Output |
| 55 |
$this->output(); |
| 56 |
} ); |
| 57 |
add_action( 'wp_print_styles', array( &$this, 'siteLoadStyles' ) ); |
| 58 |
add_action( 'wp_print_scripts', array( &$this, 'siteLoadScripts' ) ); |
| 59 |
} |
| 60 |
|
| 61 |
private function setup() |
| 62 |
{ |
| 63 |
// Timezone |
| 64 |
date_default_timezone_set( ( get_option( 'timezone_string' ) != '' ? get_option( 'timezone_string' ) : 'Europe/Amsterdam' ) ); |
| 65 |
// Get options |
| 66 |
$this->aButtons = (array) get_option( 'buttonizer_buttons' ); |
| 67 |
$this->aPageSettings = (array) get_option( 'buttonizer_page_categories' ); |
| 68 |
$this->aOpeningData = (array) get_option( 'buttonizer_opening_settings' ); |
| 69 |
$this->aSettings = (array) get_option( 'buttonizer_general_settings' ); |
| 70 |
// Current page data |
| 71 |
$this->sCurrentPageId = get_the_ID(); |
| 72 |
$this->sCurrentPageUrl = urlencode( get_permalink() ); |
| 73 |
$this->sCurrentPageTitle = get_the_title(); |
| 74 |
$this->sCurrentPageDescription = str_replace( ' ', '+', get_the_content( 'Read more' ) ); |
| 75 |
// Is store opened |
| 76 |
$this->bIsOpened = $this->isOpened(); |
| 77 |
} |
| 78 |
|
| 79 |
public function siteLoadStyles() |
| 80 |
{ |
| 81 |
wp_enqueue_style( 'buttonizer', plugins_url( '/css/buttonizer.css?v=' . md5( BUTTONIZER_VERSION ), BUTTONIZER_PLUGIN_DIR ) ); |
| 82 |
wp_enqueue_style( 'font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css' ); |
| 83 |
} |
| 84 |
|
| 85 |
public function siteLoadScripts() |
| 86 |
{ |
| 87 |
wp_enqueue_script( 'buttonizer-js', plugins_url( '/js/buttonizer.js?v=' . md5( BUTTONIZER_VERSION ), BUTTONIZER_PLUGIN_DIR ) ); |
| 88 |
} |
| 89 |
|
| 90 |
// Generate |
| 91 |
private function generate() |
| 92 |
{ |
| 93 |
$aButtons = ( isset( $this->aButtons['buttonorder'] ) && is_array( $this->aButtons['buttonorder'] ) ? $this->aButtons['buttonorder'] : array() ); |
| 94 |
foreach ( $aButtons as $sKey => $iId ) { |
| 95 |
if ( '-1' == $iId ) { |
| 96 |
continue; |
| 97 |
} |
| 98 |
$sButton = $this->generateB( $iId ); |
| 99 |
|
| 100 |
if ( 'continue' == $sButton ) { |
| 101 |
continue; |
| 102 |
} else { |
| 103 |
$this->sOutput .= $sButton; |
| 104 |
} |
| 105 |
|
| 106 |
$this->iAmountOfButtons++; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
private function generateB( $bNmbr ) |
| 111 |
{ |
| 112 |
// Check if this one must be showed when the company is open: |
| 113 |
if ( isset( $this->aButtons['button_' . $bNmbr . '_show_when_opened'] ) && !$this->bIsOpened ) { |
| 114 |
return 'continue'; |
| 115 |
} |
| 116 |
// Check if this one must be showed when the company is open: |
| 117 |
if ( isset( $this->aButtons['button_' . $bNmbr . '_show_not_when_opened'] ) && $this->bIsOpened ) { |
| 118 |
return 'continue'; |
| 119 |
} |
| 120 |
// Do we need to show the button? |
| 121 |
|
| 122 |
if ( !isset( $this->aButtons['button_' . $bNmbr . '_show_on_phone'] ) && !isset( $this->aButtons['button_' . $bNmbr . '_show_on_desktop'] ) ) { |
| 123 |
// Skipping because not showing on desktop AND phone |
| 124 |
return 'continue'; |
| 125 |
} else { |
| 126 |
// Is the button visble on mobile phones?: |
| 127 |
if ( $this->bIsMobile && !isset( $this->aButtons['button_' . $bNmbr . '_show_on_phone'] ) ) { |
| 128 |
// Skipping because this is a mobile |
| 129 |
return 'continue'; |
| 130 |
} |
| 131 |
// Is the button visible on desktop?: |
| 132 |
if ( !$this->bIsMobile && !isset( $this->aButtons['button_' . $bNmbr . '_show_on_desktop'] ) ) { |
| 133 |
// Skipping because this is desktop and it musn't get shown here |
| 134 |
return 'continue'; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/* |
| 139 |
* Page categories selected or not |
| 140 |
* Check on what pages the button must be shown |
| 141 |
*/ |
| 142 |
$buttonShowOnPages = ( isset( $this->aButtons['button_' . $bNmbr . '_show_on_pages'] ) ? $this->aButtons['button_' . $bNmbr . '_show_on_pages'] : 'all' ); |
| 143 |
$buttonPagesData = ( isset( $this->aPageSettings['category_' . $buttonShowOnPages] ) ? $this->aPageSettings['category_' . $buttonShowOnPages] : 'all' ); |
| 144 |
$sCategoryType = ( isset( $this->aPageSettings['category_' . $buttonShowOnPages . '_type'] ) ? $this->aPageSettings['category_' . $buttonShowOnPages . '_type'] : 'include' ); |
| 145 |
if ( $buttonShowOnPages != 'all' && $buttonShowOnPages != -5 && $buttonPagesData != 'all' && ($sCategoryType == 'include' && !in_array( $this->sCurrentPageId, $buttonPagesData ) || $sCategoryType == 'exclude' && in_array( $this->sCurrentPageId, $buttonPagesData )) ) { |
| 146 |
return 'continue'; |
| 147 |
} |
| 148 |
/* |
| 149 |
* All button info |
| 150 |
*/ |
| 151 |
$buttonText = ( isset( $this->aButtons['button_' . $bNmbr . '_text'] ) ? $this->aButtons['button_' . $bNmbr . '_text'] : 'Button ' . $bNmbr ); |
| 152 |
$buttonTitle = ( isset( $this->aButtons['button_' . $bNmbr . '_title'] ) ? $this->aButtons['button_' . $bNmbr . '_title'] : 'Button ' . $bNmbr ); |
| 153 |
$buttonIcon = ( isset( $this->aButtons['button_' . $bNmbr . '_icon'] ) ? $this->aButtons['button_' . $bNmbr . '_icon'] : 'plus' ); |
| 154 |
$buttonIsPhone = ( isset( $this->aButtons['button_' . $bNmbr . '_is_phonenumber'] ) ? $this->aButtons['button_' . $bNmbr . '_is_phonenumber'] : '' ); |
| 155 |
$link = ( isset( $this->aButtons['button_' . $bNmbr . '_url'] ) ? $this->aButtons['button_' . $bNmbr . '_url'] : '' ); |
| 156 |
$linkNewTab = ( isset( $this->aButtons['button_' . $bNmbr . '_url_newtab'] ) ? 'target="_blank"' : '' ); |
| 157 |
$hideLabel = ( isset( $this->aButtons['button_' . $bNmbr . '_hide_label'] ) ? $this->aButtons['button_' . $bNmbr . '_hide_label'] : '' ); |
| 158 |
|
| 159 |
if ( $link != '#' && $link != '' && strpos( 'javascript:', $link ) !== false ) { |
| 160 |
$linkInfo = parse_url( $link ); |
| 161 |
if ( !$buttonIsPhone && empty($linkInfo['scheme']) && substr( $link, 0, 1 ) != '/' ) { |
| 162 |
$link = 'http://' . $link; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
$sButtonClasses = 'is_extra bt_' . $this->iAmountOfButtons; |
| 167 |
return '<a href="' . (( $buttonIsPhone ? 'tel:' . $link : $link )) . '" class="' . $sButtonClasses . '" ' . $linkNewTab . ' onclick="onButtonizerClickEvent(\'' . $buttonText . '\')">' . (( $buttonText != '' ? '<div class="text"' . (( $hideLabel == '1' ? 'style="display: none;"' : '' )) . '><div>' . $buttonText . '</div></div>' : '' )) . '<i class="fa ' . $buttonIcon . '"></i></a>'; |
| 168 |
} |
| 169 |
|
| 170 |
// Show on timeout |
| 171 |
private function showOnTimeout() |
| 172 |
{ |
| 173 |
return '0'; |
| 174 |
} |
| 175 |
|
| 176 |
// Show on scroll |
| 177 |
private function showOnScroll() |
| 178 |
{ |
| 179 |
return '0'; |
| 180 |
} |
| 181 |
|
| 182 |
// Exit intent |
| 183 |
private function enableExitIntent() |
| 184 |
{ |
| 185 |
return '0'; |
| 186 |
} |
| 187 |
|
| 188 |
// Exit intent |
| 189 |
private function enableExitIntentText() |
| 190 |
{ |
| 191 |
return ''; |
| 192 |
} |
| 193 |
|
| 194 |
// Is the store opened right now? |
| 195 |
private function isOpened() |
| 196 |
{ |
| 197 |
$sDayOfWeek = $this->getDay( date( 'N' ) ); |
| 198 |
$bIsOpened = true; |
| 199 |
// Result |
| 200 |
$bTodayOpened = ( isset( $this->aOpeningData['buttonizer_' . $sDayOfWeek . '_opened'] ) ? $this->aOpeningData['buttonizer_' . $sDayOfWeek . '_opened'] : '' ); |
| 201 |
// If result == 1 => Opened |
| 202 |
|
| 203 |
if ( $bTodayOpened != '1' ) { |
| 204 |
$bIsOpened = false; |
| 205 |
} else { |
| 206 |
// Get the time right now |
| 207 |
$sCurentHour = date( 'G' ); |
| 208 |
$sCurrentMinute = date( 'i' ); |
| 209 |
// Get the opening and closing time from today. |
| 210 |
$hourOpening = explode( ':', ( isset( $this->aOpeningData['buttonizer_' . $sDayOfWeek . '_opened_from'] ) ? $this->aOpeningData['buttonizer_' . $sDayOfWeek . '_opened_from'] : '10:00' ) ); |
| 211 |
$hourClosing = explode( ':', ( isset( $this->aOpeningData['buttonizer_' . $sDayOfWeek . '_closing_on'] ) ? $this->aOpeningData['buttonizer_' . $sDayOfWeek . '_closing_on'] : '17:00' ) ); |
| 212 |
// Check if the company is opened/closed |
| 213 |
|
| 214 |
if ( $sCurentHour < $hourOpening[0] || $sCurentHour == $hourOpening[0] && $sCurrentMinute < $hourOpening[1] || $sCurentHour > $hourClosing[0] || $sCurentHour == $hourClosing[0] && $sCurrentMinute > $hourClosing[1] - 1 ) { |
| 215 |
$bIsOpened = false; |
| 216 |
} else { |
| 217 |
$bIsOpened = true; |
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
return $bIsOpened; |
| 223 |
} |
| 224 |
|
| 225 |
// Day-number to text |
| 226 |
private function getDay( $sDay ) |
| 227 |
{ |
| 228 |
switch ( $sDay ) { |
| 229 |
case '1': |
| 230 |
return 'monday'; |
| 231 |
break; |
| 232 |
case '2': |
| 233 |
return 'tuesday'; |
| 234 |
break; |
| 235 |
case '3': |
| 236 |
return 'wednesday'; |
| 237 |
break; |
| 238 |
case '4': |
| 239 |
return 'thursday'; |
| 240 |
break; |
| 241 |
case '5': |
| 242 |
return 'friday'; |
| 243 |
break; |
| 244 |
case '6': |
| 245 |
return 'saturday'; |
| 246 |
break; |
| 247 |
case '7': |
| 248 |
return 'monday'; |
| 249 |
break; |
| 250 |
default: |
| 251 |
return 'sunday'; |
| 252 |
break; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
// Share buttons |
| 257 |
private function share_btns() |
| 258 |
{ |
| 259 |
|
| 260 |
if ( isset( $this->aSettings['share_facebook'] ) && '1' == $this->aSettings['share_facebook'] ) { |
| 261 |
$buttonText = ( isset( $this->aSettings['share_facebook_text'] ) && $this->aSettings['share_facebook_text'] != '' ? $this->aSettings['share_facebook_text'] : 'Share this on Facebok' ); |
| 262 |
$this->sOutput .= '<a href="javascript:void(0)" onclick="onButtonizerClickEvent(\'Facebook share click\'); onButtonizerButtonFacebook();" class="is_extra share share_' . ($this->iAmountOfShareButtons + 1) . '">' . (( $buttonText != '' ? '<div class="text"><div>' . $buttonText . '</div></div>' : '' )) . '<i class="fa fa-facebook"></i></a>'; |
| 263 |
$this->iAmountOfShareButtons++; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
if ( isset( $this->aSettings['share_twitter'] ) && '1' == $this->aSettings['share_twitter'] ) { |
| 268 |
$buttonText = ( isset( $this->aSettings['share_twitter_text'] ) && $this->aSettings['share_twitter_text'] != '' ? $this->aSettings['share_twitter_text'] : 'Share this on Twitter' ); |
| 269 |
$this->sOutput .= '<a href="javascript:void(0)" onclick="onButtonizerClickEvent(\'Twitter share click\'); onButtonizerButtonTwitter();" class="is_extra share share_' . ($this->iAmountOfShareButtons + 1) . '">' . (( $buttonText != '' ? '<div class="text"><div>' . $buttonText . '</div></div>' : '' )) . '<i class="fa fa-twitter"></i></a>'; |
| 270 |
$this->iAmountOfShareButtons++; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
if ( isset( $this->aSettings['share_linkedin'] ) && '1' == $this->aSettings['share_linkedin'] ) { |
| 275 |
$buttonText = ( isset( $this->aSettings['share_linkedin_text'] ) && $this->aSettings['share_linkedin_text'] != '' ? $this->aSettings['share_linkedin_text'] : 'Share this on LinkedIn' ); |
| 276 |
$this->sOutput .= '<a href="javascript:void(0)" onclick="onButtonizerClickEvent(\'Linkedin share click\'); onButtonizerButtonLinkedin();" class="is_extra share share_' . ($this->iAmountOfShareButtons + 1) . '">' . (( $buttonText != '' ? '<div class="text"><div>' . $buttonText . '</div></div>' : '' )) . '<i class="fa fa-linkedin"></i></a>'; |
| 277 |
$this->iAmountOfShareButtons++; |
| 278 |
} |
| 279 |
|
| 280 |
} |
| 281 |
|
| 282 |
// Output |
| 283 |
public function output() |
| 284 |
{ |
| 285 |
$bShowOnScroll = ( isset( $this->aSettings['show_on_scroll'] ) ? true : false ); |
| 286 |
$bShowOnProcent = ( isset( $this->aSettings['procent_to_scroll'] ) ? $this->aSettings['procent_to_scroll'] : '0' ); |
| 287 |
$sGoogleAnalyticsCode = ( isset( $this->aSettings['google_analytics'] ) ? $this->aSettings['google_analytics'] : false ); |
| 288 |
$showAfterTimeout = $this->showOnTimeout(); |
| 289 |
$showOnScroll = $this->showOnScroll(); |
| 290 |
// Main button image or icon |
| 291 |
$sImage = ( isset( $this->aSettings['custom_icon'] ) ? $this->aSettings['custom_icon'] : '' ); |
| 292 |
|
| 293 |
if ( $sImage != '' ) { |
| 294 |
$sIcon = '<img src="' . $sImage . '" style="width: 32px; vertical-align: middle;" />'; |
| 295 |
} else { |
| 296 |
$sIcon = ( !empty($this->aSettings['icon_icon']) && $this->aSettings['icon_icon'] != '+' ? '<i class="isicon-fa fa ' . $this->aSettings['icon_icon'] . '"></i>' : '<i>+</i>' ); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
if ( $this->iAmountOfButtons == 1 && $this->iAmountOfShareButtons == 0 ) { |
| 301 |
$output = str_replace( 'is_extra bt_0', 'buttonizer_head onlyone', $this->sOutput ); |
| 302 |
} else { |
| 303 |
|
| 304 |
if ( $this->iAmountOfButtons > 1 || $this->iAmountOfShareButtons > 0 ) { |
| 305 |
$output = '<a href="javascript:void(0)" class="buttonizer_head" onclick="onButtonizerClickEvent(\'Open/Close Buttonizer button\')">' . $sIcon . '</a>' . $this->sOutput; |
| 306 |
} else { |
| 307 |
$output = ''; |
| 308 |
} |
| 309 |
|
| 310 |
} |
| 311 |
|
| 312 |
// Google analytics toevoegen |
| 313 |
if ( isset( $this->aSettings['google_analytics'] ) && $this->aSettings['google_analytics'] != '' ) { |
| 314 |
$output .= '<script>(function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,\'script\',\'https://www.google-analytics.com/analytics.js\',\'ga\');ga(\'create\', \'' . $this->aSettings['google_analytics'] . '\', \'auto\');</script>'; |
| 315 |
} |
| 316 |
list($sShadowColorRed, $sShadowColorGreen, $sShadowColorBlue) = sscanf( $this->aSettings['button_unpushed'], '#%02x%02x%02x' ); |
| 317 |
echo '<style>.buttonizer-button a:hover, .buttonizer-button a:focus{ background:' . $this->aSettings['button_pushed'] . '; } .buttonizer-button a { background:' . $this->aSettings['button_unpushed'] . '; } .buttonizer-button a i { color: ' . $this->aSettings['icon_color'] . '; }</style> |
| 318 |
<div class="buttonizer-button ' . (( $showOnScroll > 0 || $showAfterTimeout > 0 ? 'hide' : '' )) . '" style=" right: ' . $this->aSettings['position_right'] . '%; bottom: ' . $this->aSettings['position_bottom'] . '%;" id="buttonizer-button"><div class="buttonizer_inner" id="buttonizer-sys">' . $output . '</div></div> |
| 319 |
<script type="text/javascript"> |
| 320 |
buttonizer.init({ |
| 321 |
scrollBarTop: ' . $showOnScroll . ', |
| 322 |
showAfter: ' . $showAfterTimeout . ', |
| 323 |
' . (( ButtonizerLicense()->is__premium_only() ? 'exitIntent: ' . $this->enableExitIntent() . ',' : '' )) . ' |
| 324 |
' . (( ButtonizerLicense()->is__premium_only() ? 'exitIntentText: "' . $this->enableExitIntentText() . '",' : '' )) . ' |
| 325 |
}); |
| 326 |
</script>' ; |
| 327 |
} |
| 328 |
|
| 329 |
} |