-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsticky-phone-button.php
480 lines (443 loc) · 18.3 KB
/
sticky-phone-button.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
/*
Plugin Name: Sticky Phone Button
Description: Wyświetla przyklejoną ikonę telefonu po bokach ekranu na urządzeniach mobilnych, umożliwiającą natychmiastowe połączenie. Dostosowanie miejsca, numeru telefonu, dni i godzin wyświetlania oraz kolorów.
Version: 1.3
Author: Wirtualny Handlowiec
Author URI: http://wirtualnyhandlowiec.pl/
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* Enqueue style and script for frontend
*/
function sticky_phone_button_enqueue_scripts()
{
/**
* Load CSS file with styles for the sticky phone button
*/
wp_enqueue_style(
'sticky-phone-button-style',
plugin_dir_url(__FILE__) . 'style.css'
);
/**
* Load JavaScript file with the script for the sticky phone button
*/
wp_enqueue_script(
'sticky-phone-button-script',
plugin_dir_url(__FILE__) . 'script.js',
array(),
false,
true
);
}
add_action('wp_enqueue_scripts', 'sticky_phone_button_enqueue_scripts');
/**
* Add a page to the WordPress admin menu for the sticky phone button settings
*/
function sticky_phone_button_add_admin_menu()
{
/**
* Add an options page to the WordPress admin menu
* @param string $page_title The title of the page
* @param string $menu_title The title of the menu item
* @param string $capability The capability required to access the page
* @param string $menu_slug The slug of the menu item
* @param callable $function The function to call when the page is accessed
*/
add_options_page(
__('Sticky Phone Button Settings', 'sticky-phone-button'),
__('Sticky Phone Button', 'sticky-phone-button'),
'manage_options',
'sticky-phone-button',
'sticky_phone_button_options_page'
);
}
add_action('admin_menu', 'sticky_phone_button_add_admin_menu');
/**
* Register the settings for the sticky phone button plugin.
*/
function sticky_phone_button_settings_init()
{
// Register the settings for the plugin
register_setting('stickyPhoneButtonSettings', 'sticky_phone_button_settings');
// Add a section for the phone button settings
add_settings_section(
'sticky_phone_button_settings_section',
__('Ustawienia przycisku telefonu', 'sticky-phone-button'),
/**
* Callback function for displaying a description of the settings section.
* This function is optional and can be used to provide additional information
* to the user about this section.
*/
function () {
// Do nothing
},
'stickyPhoneButtonSettings'
);
// Add a field for selecting the display device
add_settings_field(
'sticky_phone_button_display_device',
__('Wyświetlaj na', 'sticky-phone-button'),
/**
* Callback function to render the select field for the device type.
* This field allows users to choose on which devices the button will be displayed.
* @return void
*/
'sticky_phone_button_display_device_render',
'stickyPhoneButtonSettings',
'sticky_phone_button_settings_section'
);
// Add a field for entering the phone number
add_settings_field(
'sticky_phone_button_phone_number',
__('Numer telefonu', 'sticky-phone-button'),
/**
* Callback function to render the text field for the phone number.
* This field allows users to specify the phone number that will be called.
* @return void
*/
'sticky_phone_button_phone_number_render',
'stickyPhoneButtonSettings',
'sticky_phone_button_settings_section'
);
// Add a field for selecting the button position
add_settings_field(
'sticky_phone_button_position',
__('Pozycja przycisku (left/right)', 'sticky-phone-button'),
/**
* Callback function to render the select field for the button position.
* This field allows users to specify where the button will appear on the screen.
* @return void
*/
'sticky_phone_button_position_render',
'stickyPhoneButtonSettings',
'sticky_phone_button_settings_section'
);
// Add fields for selecting display days and hours
add_settings_field(
'sticky_phone_button_display_days',
__('Dni wyświetlania i godziny', 'sticky-phone-button'),
/**
* Callback function to render the checkboxes and text fields for display days and hours.
* This field allows users to specify on which days and during which hours the button will be displayed.
* @return void
*/
'sticky_phone_button_display_days_render',
'stickyPhoneButtonSettings',
'sticky_phone_button_settings_section'
);
// Add a field for selecting the icon color
add_settings_field(
'sticky_phone_button_icon_color',
__('Kolor ikony słuchawki', 'sticky-phone-button'),
/**
* Callback function to render the color picker for the icon color.
* This field allows users to choose the color of the phone icon.
* @return void
*/
'sticky_phone_button_icon_color_render',
'stickyPhoneButtonSettings',
'sticky_phone_button_settings_section'
);
// Add a field for selecting the background color
add_settings_field(
'sticky_phone_button_background_color',
__('Kolor tła koła', 'sticky-phone-button'),
/**
* Callback function to render the color picker for the background color.
* This field allows users to choose the background color of the button.
* @return void
*/
'sticky_phone_button_background_color_render',
'stickyPhoneButtonSettings',
'sticky_phone_button_settings_section'
);
// Add a field for setting the blink time
add_settings_field(
'sticky_phone_button_blink_time',
__('Czas między mrugnięciami', 'sticky-phone-button'),
/**
* Callback function to render the text field for the blink time.
* This field allows users to specify the time interval between blinks of the button.
* @return void
*/
'sticky_phone_button_blink_time_render',
'stickyPhoneButtonSettings',
'sticky_phone_button_settings_section'
);
}
add_action('admin_init', 'sticky_phone_button_settings_init');
/**
* Render a select field for the device type.
* This field allows users to choose on which devices the button will be displayed.
* @return void
*/
function sticky_phone_button_display_device_render()
{
$options = get_option('sticky_phone_button_settings');
// Get the current value of the display device setting
$display_device = isset($options['sticky_phone_button_display_device']) ? $options['sticky_phone_button_display_device'] : 'both';
// Render the select field with options for phones, desktops and both
?>
<select name='sticky_phone_button_settings[sticky_phone_button_display_device]'>
<option value='phones' <?php selected($display_device, 'phones'); ?>>Tylko na telefonach</option>
<option value='desktops' <?php selected($display_device, 'desktops'); ?>>Tylko na komputerach</option>
<option value='both' <?php selected($display_device, 'both'); ?>>Na telefonach i komputerach</option>
</select>
<?php
}
/**
* Render a text field for the phone number.
* This field allows users to enter a phone number to be displayed on the button.
* @return void
*/
function sticky_phone_button_phone_number_render()
{
$options = get_option('sticky_phone_button_settings');
?>
<input type='text' name='sticky_phone_button_settings[sticky_phone_button_phone_number]'
value='<?php echo esc_attr($options['sticky_phone_button_phone_number']); ?>'>
<p class="description">Numer telefonu, np. 501 501 501</p>
<?php
}
/**
* Render a text field for the blink time.
* This field allows users to enter the time (in seconds) of the blink animation for the button.
* @return void
*/
function sticky_phone_button_blink_time_render()
{
$options = get_option('sticky_phone_button_settings');
// Default blink time is 4 seconds
$blink_time = isset($options['sticky_phone_button_blink_time']) ? $options['sticky_phone_button_blink_time'] : '4';
?>
<input type="number" step="0.1" name="sticky_phone_button_settings[sticky_phone_button_blink_time]" value="<?php echo esc_attr($blink_time); ?>" min="0.5" max="10">
<p class="description">
Czas (w sekundach) co ile ma mrugać przycisk.
<br>
Wartość musi być z zakresu 0,5-10 sekund.
</p>
<?php
}
/**
* Render the dropdown field for selecting the button position.
* This field allows users to choose where the sticky phone button will be displayed on the screen.
*
* @return void
*/
function sticky_phone_button_position_render()
{
// Retrieve the plugin options from the database
$options = get_option('sticky_phone_button_settings');
// Define the available positions for the sticky phone button
$positions = [
'bottom-right' => 'Prawy dolny róg',
'bottom-left' => 'Lewy dolny róg',
'bottom-center' => 'Dół środek',
'top-right' => 'Góra prawy róg',
'top-left' => 'Góra lewy róg',
'top-center' => 'Góra środek',
'middle-right' => 'Prawy środek',
'middle-left' => 'Lewy środek'
];
?>
<select name='sticky_phone_button_settings[sticky_phone_button_position]'>
<?php
// Iterate over each position and render it as an option in the dropdown
foreach ($positions as $value => $label) : ?>
<option value='<?php echo esc_attr($value); ?>' <?php selected($options['sticky_phone_button_position'], $value); ?>>
<?php echo esc_html($label); ?>
</option>
<?php endforeach; ?>
</select>
<?php
}
// Renderowanie checkboxów
$days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
/**
* Renderowanie checkboxów dla dni i godzin
*
* This function renders checkboxes for each day of the week and a text field for specifying the hours of the day.
* The function also retrieves the plugin options from the database and checks if the day is selected.
* If the day is selected, the checkbox is checked and the text field is populated with the hours.
* If the day is not selected, the checkbox is unchecked and the text field is empty.
*
* @return void
*/
function sticky_phone_button_display_days_render()
{
global $days_of_week;
$options = get_option('sticky_phone_button_settings'); // Pobierz opcje
// Dla każdego dnia wyświetl checkbox i pole godziny
foreach ($days_of_week as $day) {
// Sprawdzamy, czy dzień jest wybrany
$checked = isset($options['display_days'][$day]['enabled']) ? 'checked' : '';
// Sprawdzamy, czy godziny dla danego dnia są ustawione, jeśli nie, ustawiamy pustą wartość
$hours = isset($options['display_days'][$day]['hours']) ? $options['display_days'][$day]['hours'] : '';
?>
<label>
<input type='checkbox' name='sticky_phone_button_settings[display_days][<?php echo $day; ?>][enabled]' <?php echo $checked; ?>>
<?php echo $day; ?>
</label>
<input type='text' name='sticky_phone_button_settings[display_days][<?php echo $day; ?>][hours]' placeholder='09:00-17:00' value='<?php echo esc_attr($hours); ?>'>
<br>
<?php
}
}
/**
* Renderowanie pola wyboru koloru ikony słuchawki.
*
* This function renders a color picker field for the icon color.
* The field is populated with the current value from the database.
* If no value is set, the field is set to white (#FFF).
*
* @return void
*/
function sticky_phone_button_icon_color_render()
{
$options = get_option('sticky_phone_button_settings');
?>
<input type='color' name='sticky_phone_button_settings[sticky_phone_button_icon_color]' value='<?php echo esc_attr(isset($options['sticky_phone_button_icon_color']) ? $options['sticky_phone_button_icon_color'] : '#FFF'); ?>'>
<?php
}
/**
* Renderowanie pola wyboru koloru tła koła.
*
* This function renders a color picker field for the background color of the circle.
* The field is populated with the current value from the database.
* If no value is set, the field is set to a default green color (#10941f).
*
* @return void
*/
function sticky_phone_button_background_color_render()
{
$options = get_option('sticky_phone_button_settings');
?>
<input type='color' name='sticky_phone_button_settings[sticky_phone_button_background_color]' value='<?php echo esc_attr(isset($options['sticky_phone_button_background_color']) ? $options['sticky_phone_button_background_color'] : '#10941f'); ?>'>
<?php
}
/**
* Displays the options page for the sticky phone button plugin in the WordPress admin area.
* This function generates the HTML form with the plugin settings.
*
* @return void
*/
function sticky_phone_button_options_page()
{
// Form with the plugin settings
?>
<form action='options.php' method='post'>
<h2>Sticky Phone Button</h2>
<?php
// Display the settings sections
settings_fields('stickyPhoneButtonSettings');
do_settings_sections('stickyPhoneButtonSettings');
// Display the submit button
submit_button();
?>
</form>
<?php
}
// Dodawanie dynamicznego stylu CSS na podstawie ustawień czasu mrugania
/**
* Generuje dynamiczny styl CSS, aby ustawić animację mrugania dla przycisku telefonu.
* Animacja jest ustawiona na mruganie co X sekund, w zależności od wartości w ustawieniach.
* Animacja jest nieograniczona i będzie mrugać przyciskiem w i out of view.
*
* @return void
*/
function sticky_phone_button_dynamic_css()
{
$options = get_option('sticky_phone_button_settings');
$blink_time = isset($options['sticky_phone_button_blink_time']) ? $options['sticky_phone_button_blink_time'] : '4'; // domyślna wartość 4s
// Wygeneruj kod CSS z ustawionym czasem mrugania
?>
<style type="text/css">
/**
* Ustawia animację mrugania dla przycisku telefonu.
* Animacja jest ustawiona na mruganie co X sekund, w zależności od wartości w ustawieniach.
* Animacja jest nieograniczona i będzie mrugać przyciskiem w i out of view.
*/
.sticky-phone-button {
animation: blink <?php echo esc_attr($blink_time); ?>s infinite;
}
/**
* Animacja mrugania przycisku telefonu.
* Animacja jest ustawiona na mruganie co X sekund, w zależności od wartości w ustawieniach.
* Animacja jest nieograniczona i będzie mrugać przyciskiem w i out of view.
*
* 0% - 97%: The button is visible
* 98% - 99%: The button is hidden (quick blink)
* 100%: The button is visible again
*/
@keyframes blink {
/**
* 0% - 97%: The button is visible
*/
0%,
97%,
100% {
opacity: 1;
/* Przycisk widoczny */
}
/**
* 98% - 99%: The button is hidden (quick blink)
*/
98%,
99% {
opacity: 0;
/* Krótkie mrugnięcie */
}
}
</style>
<?php
}
add_action('wp_head', 'sticky_phone_button_dynamic_css');
/**
* Display the sticky phone button on the frontend with the options.
* This function renders the button with the specified settings and ensures it only displays
* during the configured days and hours. The button is styled and positioned based on user preferences.
*
* @return void
*/
function sticky_phone_button_html()
{
// Retrieve the plugin options from the database
$options = get_option('sticky_phone_button_settings');
// Retrieve the phone number, position, icon color, background color, and display device settings
$phone_number = isset($options['sticky_phone_button_phone_number']) ? $options['sticky_phone_button_phone_number'] : '';
$position = isset($options['sticky_phone_button_position']) ? $options['sticky_phone_button_position'] : 'bottom-right';
$icon_color = isset($options['sticky_phone_button_icon_color']) ? $options['sticky_phone_button_icon_color'] : '#ffffff';
$background_color = isset($options['sticky_phone_button_background_color']) ? $options['sticky_phone_button_background_color'] : '#000000';
$display_device = isset($options['sticky_phone_button_display_device']) ? $options['sticky_phone_button_display_device'] : 'both';
// Get the current day and time
$current_day = date('l');
$current_time = date('H:i');
// Check if the current day is selected and if the current time is within the specified range
if (isset($options['display_days'][$current_day]['enabled'])) {
$hours_range = explode('-', $options['display_days'][$current_day]['hours']);
if (!empty($hours_range) && count($hours_range) == 2) {
if ($current_time < $hours_range[0] || $current_time > $hours_range[1]) {
return; // Do not display the button if the current time is not within the range
}
}
} else {
return; // Do not display the button if the current day is not selected
}
// Determine the CSS class based on the selected display device
$device_class = '';
if ($display_device == 'phones') {
$device_class = 'sticky-phone-only'; // Class for phone-only display
} elseif ($display_device == 'desktops') {
$device_class = 'sticky-desktop-only'; // Class for desktop-only display
}
// Render the phone button with the specified options and styles
echo '<a href="tel:' . esc_attr($phone_number) . '" id="sticky-phone-button" class="sticky-phone-button sticky-' . esc_attr($position) . ' ' . esc_attr($device_class) . '" style="background-color: ' . esc_attr($background_color) . ';">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="' . esc_attr($icon_color) . '">
<path d="M6.62 10.79a15.091 15.091 0 006.59 6.59l2.2-2.2a1 1 0 011.11-.27c1.21.49 2.54.75 3.91.75a1 1 0 011 1v3.5a1 1 0 01-1 1A17.918 17.918 0 013 3a1 1 0 011-1h3.5a1 1 0 011 1c0 1.37.26 2.7.75 3.91a1 1 0 01-.27 1.11l-2.2 2.2z"/>
</svg>
</a>';
}
add_action('wp_footer', 'sticky_phone_button_html');