| 1 |
<?php |
| 2 |
/* |
| 3 |
This program is free software; you can redistribute it and/or |
| 4 |
modify it under the terms of the GNU General Public License |
| 5 |
as published by the Free Software Foundation; either version 2 |
| 6 |
of the License, or (at your option) any later version. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, |
| 9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License |
| 14 |
along with this program; if not, write to the Free Software |
| 15 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 |
|
| 17 |
Copyright 2017 Buttonizer |
| 18 |
*/ |
| 19 |
|
| 20 |
namespace Buttonizer\Admin; |
| 21 |
|
| 22 |
# No script kiddies |
| 23 |
defined( 'ABSPATH' ) or die('No script kiddies please!'); |
| 24 |
|
| 25 |
class Buttons { |
| 26 |
private $aSavedData = []; |
| 27 |
private $aPageCategories = []; |
| 28 |
private $aFontAwesome = []; |
| 29 |
|
| 30 |
public function __construct($aFontAwesome) { |
| 31 |
$this->aFontAwesome = $aFontAwesome; |
| 32 |
$this->loadData(); |
| 33 |
$this->loadButtons(); |
| 34 |
} |
| 35 |
|
| 36 |
private function loadData() { |
| 37 |
$this->aSavedData = (array) get_option('buttonizer_buttons'); |
| 38 |
$this->aPageCategories = (array) get_option('buttonizer_page_categories'); |
| 39 |
} |
| 40 |
|
| 41 |
private function loadButtons() { |
| 42 |
// Get buttons |
| 43 |
$aButtons = (isset($this->aSavedData['buttonorder']) && is_array($this->aSavedData['buttonorder']) ? $this->aSavedData['buttonorder'] : [0 => -1]); |
| 44 |
|
| 45 |
if($aButtons[0] != '-1') { |
| 46 |
$aButtons[-1] = '-1'; |
| 47 |
} |
| 48 |
|
| 49 |
echo '<div id="button-rows">'; |
| 50 |
$iButtonCount = 0; |
| 51 |
foreach($aButtons as $sButtonKey => $iButtonId) { |
| 52 |
$this->loadIndividialButtonRow($sButtonKey, $iButtonId, (isset($bDisabled) ? true : false)); |
| 53 |
$iButtonCount++; |
| 54 |
} |
| 55 |
|
| 56 |
echo '<img src="'. plugins_url('/assets/no-buttons.jpg', BUTTONIZER_PLUGIN_DIR) .'" style="'. (0 == ($iButtonCount - 1) ? 'display: block;' : 'display: none;') .'" class="buttonizer-no-buttons" />'; |
| 57 |
|
| 58 |
echo '</div>'; |
| 59 |
} |
| 60 |
|
| 61 |
private function loadIndividialButtonRow($sButtonKey, $iButtonId, $bDisabled) { |
| 62 |
$sButtonTitle = (isset($this->aSavedData['button_' . $iButtonId . '_title']) ? ( $this->aSavedData['button_' . $iButtonId . '_title'] ):'Button ' . ($iButtonId + 1)); |
| 63 |
|
| 64 |
$onMobile = (isset($this->aSavedData['button_' . $iButtonId . '_show_on_phone'])?( $this->aSavedData['button_' . $iButtonId . '_show_on_phone'] ):''); |
| 65 |
$onDesktop = (isset($this->aSavedData['button_' . $iButtonId . '_show_on_desktop'])?( $this->aSavedData['button_' . $iButtonId . '_show_on_desktop'] ):''); |
| 66 |
|
| 67 |
?> |
| 68 |
<div class="button-row" id="btn_row_<?=$iButtonId?>" button-id="<?=$iButtonId?>"<?php if($iButtonId == -1) { echo ' style="display: none"'; } ?>> |
| 69 |
<input type="checkbox" name="buttonizer_buttons[buttonorder][]" value="<?=$iButtonId?>" checked="checked" style="display: none;" /> |
| 70 |
|
| 71 |
<div class="row-info"> |
| 72 |
<span><i class="fa <?=$this->buttonOutputIcon($iButtonId);?> button-icon"></i> <span class="row-title"><?=($iButtonId == -1 ? 'New button' : $sButtonTitle)?></span> <i class="fa fa-pencil pencil-edit"></i></span> |
| 73 |
|
| 74 |
<a href="javascript:buttonizer.removeRow(<?=$iButtonId?>)" class="delete-button">Delete <i class="fa fa-trash-o"></i></a> |
| 75 |
|
| 76 |
<a href="javascript:buttonizer.toggleMobile(<?=$iButtonId?>)" class="mobiledesktop mobile-button <?=($onMobile == "" ? 'selected' : '');?>"><i class="fa fa-mobile"></i></a> |
| 77 |
<a href="javascript:buttonizer.toggleDesktop(<?=$iButtonId?>)" class="mobiledesktop desktop-button <?=($onDesktop == "" ? 'selected' : '');?>"><i class="fa fa-desktop"></i></a> |
| 78 |
</div> |
| 79 |
|
| 80 |
<div class="button-data"> |
| 81 |
<table> |
| 82 |
<tr style="display: none;"> |
| 83 |
<td width="20%"></td> |
| 84 |
<td width="20%"></td> |
| 85 |
<td></td> |
| 86 |
</tr> |
| 87 |
<tr> |
| 88 |
<td> |
| 89 |
<label for="button_text" class="label-top"> |
| 90 |
<span class="info-class"><i class="fa fa-info-circle"></i> <span>This is for internal use only.<br /><br />When using Google Analytics, this will be the title of the event when clicking this button.</span></span> |
| 91 |
Button name: |
| 92 |
</label> |
| 93 |
<input type="text" name="buttonizer_buttons[button_<?=$iButtonId?>_title]" value="<?=$sButtonTitle?>" class="button_title" /> |
| 94 |
</td> |
| 95 |
<td class="button-pdr" colspan="2"> |
| 96 |
<label for="button_icon" class="label-top"> |
| 97 |
<span class="info-class"><i class="fa fa-info-circle"></i> <span>This will be the icon of the button. You can choose from the build-in Font Awesome icons.<br /><br />When you want to use your own icon or image, you can click the 'or upload an image' link to use your own icon.</span></span> |
| 98 |
Button icon: |
| 99 |
</label> |
| 100 |
<?=$this->buttonGetIcon($iButtonId); ?> |
| 101 |
</td> |
| 102 |
</tr> |
| 103 |
|
| 104 |
<tr> |
| 105 |
<td> </td> |
| 106 |
<td></td> |
| 107 |
<td></td> |
| 108 |
</tr> |
| 109 |
|
| 110 |
<tr> |
| 111 |
<td> |
| 112 |
<label for="button_text" class="label-top"> |
| 113 |
<span class="info-class"><i class="fa fa-info-circle"></i> <span>This is the label what the guest on your website will see. Something like 'Contact me!', or 'Like us on Facebook'</span></span> |
| 114 |
Button label: |
| 115 |
</label> |
| 116 |
<?=$this->buttonGetText($iButtonId); ?> |
| 117 |
</td> |
| 118 |
<td colspan="2"> |
| 119 |
<label for="button_category" class="label-top"> |
| 120 |
<span class="info-class"><i class="fa fa-info-circle"></i> <span>Select a page category. You can add more page rules at the 'Page categories' tab.</span></span> |
| 121 |
Button page category: |
| 122 |
</label> |
| 123 |
<?=$this->buttonGetShowOnPages($iButtonId); ?> |
| 124 |
</td> |
| 125 |
</tr> |
| 126 |
|
| 127 |
<tr> |
| 128 |
<td> </td> |
| 129 |
<td></td> |
| 130 |
<td></td> |
| 131 |
</tr> |
| 132 |
|
| 133 |
<tr class="settings-row"> |
| 134 |
<td> |
| 135 |
<i class="fa fa-globe setting-icon"></i> URL or phone-number |
| 136 |
<span class="info-class"><i class="fa fa-info-circle"></i> <span>This is the click-action of your button. You can input a full url (http://myawesomewebsite.com), a URI (/contact), an anchor-link (#anchor) or a phone number. Note that if you input a phone number that you select the option ‘Is phone number’. This will be a direct click-to-call action. Please don’t add a full url without the http:// (like: www.myawesomewebsite.com). </span></span> |
| 137 |
</td> |
| 138 |
<td colspan="2"><?=$this->buttonGetUrl($iButtonId)?></td> |
| 139 |
</tr> |
| 140 |
|
| 141 |
<tr class="settings-row"> |
| 142 |
<td> |
| 143 |
<i class="fa fa-phone setting-icon"></i> Is phone number |
| 144 |
</td> |
| 145 |
<td><?=$this->buttonGetIsPhoneNumber($iButtonId)?><label for="button_<?=$iButtonId?>_is_phonenumber"> Yes</label></td> |
| 146 |
<td></td> |
| 147 |
</tr> |
| 148 |
|
| 149 |
<tr class="settings-row setting-new-tab"> |
| 150 |
<td> |
| 151 |
<i class="fa fa-external-link setting-icon"></i> Open in new tab |
| 152 |
</td> |
| 153 |
<td><?=$this->buttonGetNewTab($iButtonId)?><label for="button_<?=$iButtonId?>_url_newtab"> Yes</label></td> |
| 154 |
<td></td> |
| 155 |
</tr> |
| 156 |
|
| 157 |
<tr> |
| 158 |
<td> </td> |
| 159 |
<td></td> |
| 160 |
<td></td> |
| 161 |
</tr> |
| 162 |
|
| 163 |
<tr class="settings-row"> |
| 164 |
<td> |
| 165 |
<i class="fa fa-mobile setting-icon"></i> Show on mobile |
| 166 |
</td> |
| 167 |
<td><?=$this->buttonGetShowPhone($iButtonId)?><label for="button_<?=$iButtonId?>_show_on_phone"> Yes</label></td> |
| 168 |
<td></td> |
| 169 |
</tr> |
| 170 |
|
| 171 |
<tr class="settings-row"> |
| 172 |
<td> |
| 173 |
<i class="fa fa-desktop setting-icon"></i> Show on desktop |
| 174 |
</td> |
| 175 |
<td><?=$this->buttonGetShowDesktop($iButtonId)?><label for="button_<?=$iButtonId?>_show_on_desktop"> Yes</label></td> |
| 176 |
<td></td> |
| 177 |
</tr> |
| 178 |
|
| 179 |
<tr class="settings-row"> |
| 180 |
<td> |
| 181 |
<i class="fa fa-tag setting-icon"></i> Hide label |
| 182 |
|
| 183 |
<span class="info-class"> |
| 184 |
<i class="fa fa-info-circle"></i> |
| 185 |
<span> |
| 186 |
This is the label what the guest on your website will see. Something like 'Contact me!', or 'Like us on Facebook' |
| 187 |
</span> |
| 188 |
</span> |
| 189 |
</td> |
| 190 |
<td><?=$this->buttonGetHideLabel($iButtonId)?><label for="button_<?=$iButtonId?>_hide_label"> Yes</label></td> |
| 191 |
<td></td> |
| 192 |
</tr> |
| 193 |
|
| 194 |
<tr> |
| 195 |
<td> </td> |
| 196 |
<td></td> |
| 197 |
<td></td> |
| 198 |
</tr> |
| 199 |
|
| 200 |
<tr class="settings-row"> |
| 201 |
<td> |
| 202 |
<i class="fa fa-clock-o setting-icon"></i> Show <b>only</b> on opening hours |
| 203 |
</td> |
| 204 |
<td><?=$this->buttonGetShowWenOpen($iButtonId)?><label for="button_<?=$iButtonId?>_show_when_opened"> Yes</label></td> |
| 205 |
<td></td> |
| 206 |
</tr> |
| 207 |
|
| 208 |
<tr class="settings-row"> |
| 209 |
<td> |
| 210 |
<i class="fa fa-times-circle-o setting-icon"></i> Show <b>only outside</b> opening hours |
| 211 |
</td> |
| 212 |
<td><?=$this->buttonGetShowNotWenOpen($iButtonId)?><label for="button_<?=$iButtonId?>_show_not_when_opened"> Yes</label></td> |
| 213 |
<td></td> |
| 214 |
</tr> |
| 215 |
</table> |
| 216 |
</div> |
| 217 |
</div> |
| 218 |
<?php |
| 219 |
} |
| 220 |
|
| 221 |
/* |
| 222 |
* Form functions |
| 223 |
*/ |
| 224 |
// Get the text of the button |
| 225 |
function buttonGetText($iButtonId = 0) { |
| 226 |
$fieldName = 'button_' . $iButtonId . '_text'; |
| 227 |
|
| 228 |
$fieldValue = (isset($this->aSavedData[$fieldName]) ? ($this->aSavedData[$fieldName]) : 'New button'); |
| 229 |
|
| 230 |
return '<input type="text" name="buttonizer_buttons['. $fieldName .']" id="'. $fieldName .'" value="'. $fieldValue .'" class="button_textfield" placeholder="Button text" />'; |
| 231 |
} |
| 232 |
|
| 233 |
// Get button icon |
| 234 |
function buttonGetIcon($iButtonId = 0) { |
| 235 |
$fieldName = 'button_' . $iButtonId . '_icon'; |
| 236 |
|
| 237 |
$fab_icon_image = (isset($this->aSavedData[$fieldName])?( $this->aSavedData[$fieldName] ):''); |
| 238 |
|
| 239 |
$p = '<select name="buttonizer_buttons['. $fieldName .']" id="'. $fieldName .'" style="font-family: \'FontAwesome\', \'Helvetica\';" class="button_iconfield fa-chooser">'; |
| 240 |
|
| 241 |
foreach($this->aFontAwesome as $key=>$row) { |
| 242 |
$p .= '<option value="'. $key . '" '. ($fab_icon_image == $key ? 'selected="selected"' : '') . '>'. $row ." ".$key .'</option>'; |
| 243 |
} |
| 244 |
|
| 245 |
return $p; |
| 246 |
} |
| 247 |
|
| 248 |
// Get button icon |
| 249 |
function buttonOutputIcon($iButtonId = 0) { |
| 250 |
$fieldName = 'button_' . $iButtonId . '_icon'; |
| 251 |
|
| 252 |
return (isset($this->aSavedData[$fieldName])?( $this->aSavedData[$fieldName] ):'fa-info'); |
| 253 |
} |
| 254 |
|
| 255 |
// Only show this button on phone? |
| 256 |
function buttonGetShowPhone($iButtonId = 0) { |
| 257 |
$fieldName = 'button_' . $iButtonId . '_show_on_phone'; |
| 258 |
|
| 259 |
$is_selected = (isset($this->aSavedData[$fieldName])?( $this->aSavedData[$fieldName] ):''); |
| 260 |
|
| 261 |
return '<input type="checkbox" name="buttonizer_buttons['. $fieldName .']" id="'. $fieldName .'" value="1" class="button_showonphone" '. ($is_selected == '1' ? 'checked="checked"' : '') .' />'; |
| 262 |
} |
| 263 |
|
| 264 |
// buttonGetShowOnPages |
| 265 |
function buttonGetShowOnPages($iButtonId = 0) { |
| 266 |
$fieldName = 'button_' . $iButtonId . '_show_on_pages'; |
| 267 |
|
| 268 |
$is_selected = (isset($this->aSavedData[$fieldName]) ? ($this->aSavedData[$fieldName]) : ''); |
| 269 |
|
| 270 |
$pageOrders = (isset($this->aPageCategories['categorieOrder']) ? $this->aPageCategories['categorieOrder'] : []); |
| 271 |
|
| 272 |
/* |
| 273 |
* Generate <select> |
| 274 |
*/ |
| 275 |
$html = '<select name="buttonizer_buttons['. $fieldName .']" id="button_category"><option value="-5">Show on every page</option><option value="-4">Hide everywhere</option>'; |
| 276 |
foreach($pageOrders as $arrayKey => $pageKey) { |
| 277 |
if($pageKey == -1) { continue; } |
| 278 |
|
| 279 |
$html .= '<option value="'. $pageKey .'"'. ($pageKey == $is_selected ? 'selected="selected"' : '') .'>'. $this->aPageCategories['category_' . $pageKey . '_title'] .'</option>'; |
| 280 |
} |
| 281 |
return $html . '</select>'; |
| 282 |
} |
| 283 |
|
| 284 |
// Only show this button on desktop? |
| 285 |
function buttonGetShowDesktop($iButtonId = 0) { |
| 286 |
$fieldName = 'button_' . $iButtonId . '_show_on_desktop'; |
| 287 |
|
| 288 |
$is_selected = (isset($this->aSavedData[$fieldName])?( $this->aSavedData[$fieldName] ):''); |
| 289 |
|
| 290 |
return '<input type="checkbox" name="buttonizer_buttons['. $fieldName .']" id="'. $fieldName .'" value="1" class="button_showondesktop" '. ($is_selected == '1' ? 'checked="checked"' : '') .' />'; |
| 291 |
} |
| 292 |
|
| 293 |
// Only show when company is open? |
| 294 |
function buttonGetShowWenOpen($iButtonId = 0) { |
| 295 |
$fieldName = 'button_' . $iButtonId . '_show_when_opened'; |
| 296 |
|
| 297 |
$is_selected = (isset($this->aSavedData[$fieldName])?( $this->aSavedData[$fieldName] ):''); |
| 298 |
|
| 299 |
return '<input type="checkbox" name="buttonizer_buttons['. $fieldName .']" id="'. $fieldName .'" value="1" class="button_showwhenopened" '. ($is_selected == '1' ? 'checked="checked"' : '') .' />'; |
| 300 |
} |
| 301 |
|
| 302 |
// Only show when company is open? |
| 303 |
function buttonGetShowNotWenOpen($iButtonId = 0) { |
| 304 |
$fieldName = 'button_' . $iButtonId . '_show_not_when_opened'; |
| 305 |
|
| 306 |
$is_selected = (isset($this->aSavedData[$fieldName])?( $this->aSavedData[$fieldName] ):''); |
| 307 |
|
| 308 |
return '<input type="checkbox" name="buttonizer_buttons['. $fieldName .']" id="'. $fieldName .'" value="1" class="button_showwhenopened" '. ($is_selected == '1' ? 'checked="checked"' : '') .' />'; |
| 309 |
} |
| 310 |
|
| 311 |
function buttonGetHideLabel($iButtonId = 0) { |
| 312 |
$fieldName = 'button_' . $iButtonId . '_hide_label'; |
| 313 |
|
| 314 |
$is_selected = (isset($this->aSavedData[$fieldName])?( $this->aSavedData[$fieldName] ):''); |
| 315 |
|
| 316 |
return '<input type="checkbox" name="buttonizer_buttons['. $fieldName .']" id="'. $fieldName .'" value="1" class="button_hidelabel" '. ($is_selected == '1' ? 'checked="checked"' : '') .' />'; |
| 317 |
} |
| 318 |
|
| 319 |
// Is this a phone number? |
| 320 |
function buttonGetIsPhoneNumber($iButtonId = 0) { |
| 321 |
$fieldName = 'button_' . $iButtonId . '_is_phonenumber'; |
| 322 |
|
| 323 |
$is_selected = (isset($this->aSavedData[$fieldName])?( $this->aSavedData[$fieldName] ):''); |
| 324 |
|
| 325 |
return '<input type="checkbox" name="buttonizer_buttons['. $fieldName .']" id="'. $fieldName .'" value="1" class="button_isphonenumber" '. ($is_selected == '1' ? 'checked="checked"' : '') .' />'; |
| 326 |
} |
| 327 |
|
| 328 |
function buttonGetUrl($iButtonId = 0) { |
| 329 |
$fieldName = 'button_' . $iButtonId . '_url'; |
| 330 |
|
| 331 |
$fieldValue = (isset($this->aSavedData[$fieldName])?( $this->aSavedData[$fieldName] ):''); |
| 332 |
|
| 333 |
return '<input type="text" name="buttonizer_buttons['. $fieldName .']" id="'. $fieldName .'" value="'. $fieldValue .'" class="button_isurl" placeholder="URL/Phone number" />'; |
| 334 |
} |
| 335 |
|
| 336 |
// Only show this button on phone? |
| 337 |
function buttonGetNewTab($iButtonId = 0) { |
| 338 |
$fieldName = 'button_' . $iButtonId . '_url_newtab'; |
| 339 |
|
| 340 |
$is_selected = (isset($this->aSavedData[$fieldName])?( $this->aSavedData[$fieldName] ):''); |
| 341 |
|
| 342 |
return '<input type="checkbox" name="buttonizer_buttons['. $fieldName .']" id="'. $fieldName .'" value="1" class="button_isnewtab" '. ($is_selected == '1' ? 'checked="checked"' : '') .' />'; |
| 343 |
} |
| 344 |
} |
| 345 |
|