-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.php
178 lines (161 loc) · 5.78 KB
/
logic.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
<?php
/**
* Format backslash to slash
* @param string $text text to be formatted
* @return string formatted text
*/
function format_slash($text) {
return str_replace( "\\", "/", $text);
}
/**
* Get formatted WP absolute path
* @return string WP absolute path (formatted)
*/
function get_absolute_path() {
return format_slash(rtrim(ABSPATH, "/"));
}
/**
* Get formatted relative directory of feature
* @param string $path path to file or folder
* @return string relative directory of feature (formatted)
*/
function feature_dir($path = "") {
return str_replace(get_absolute_path(), "", feature_absolute_dir($path));
}
/**
* Get formatted absolute directory of feature
* @param string $path path to file or folder
* @return string absolute directory of feature (formatted)
*/
function feature_absolute_dir($path = "") {
return format_slash(realpath(dirname(__FILE__) ."/". $path));
}
add_action('admin_menu', function () {
$pageTitle = __('Floating Button');
$menuTitle = $pageTitle;
$capability = 'edit_pages'; // permission just admin
$menuSlug = 'floating-button';
$icon = 'dashicons-buddicons-groups';
$position = 50; // https://developer.wordpress.org/reference/functions/add_menu_page/#default-bottom-of-menu-structure
$menu = add_menu_page($pageTitle, $menuTitle, $capability, $menuSlug, function () {
ob_start();
include "dashboard.php";
echo ob_get_clean();
}, $icon, $position);
// Enqueue script when in menu page
add_action('admin_print_scripts-' . $menu, 'media_include_js');
});
// allow editor save floating settings
add_filter(
hook_name: 'option_page_capability_floating_button_settings',
callback: fn($capability) => 'edit_pages'
);
// split css feature floating
add_action('wp_print_styles', function () {
wp_enqueue_style('floating-style', feature_dir('floating.css'), array(), filemtime(feature_absolute_dir('floating.css')));
wp_enqueue_script('floating-script', feature_dir('floating.js'), array('jquery'), filemtime(feature_absolute_dir('floating.js')), true);
});
// attach the settings to the admin_init hook
add_action('admin_init', function () {
// specify settings name to be saved to wp_options table
$optionGroup = 'floating_button_settings';
register_setting(
option_group: $optionGroup,
option_name: 'select-style'
);
register_setting(
option_group: $optionGroup,
option_name: 'floating_image_group'
);
register_setting(
option_group: $optionGroup,
option_name: 'floating_background_color_group'
);
// =====================================
for($i = 1; $i <= 5; $i++) {
register_setting(
option_group: $optionGroup,
option_name: 'floating_image_'.$i
);
register_setting(
option_group: $optionGroup,
option_name: 'floating_background_color_'.$i
);
register_setting(
option_group: $optionGroup,
option_name: 'floating_button_name_'.$i
);
register_setting(
option_group: $optionGroup,
option_name: 'floating_button_url_'.$i
);
}
});
add_shortcode('display_floating', function () {
$selectstyle = get_option('select-style');
if ($selectstyle == 'style-1') {
for ($i = 1; $i <= 5; $i++) {
$image = get_option('floating_image_'.$i);
$imageUrl = wp_get_attachment_image_url($image, 'original') ?: feature_dir('./img/wa-xl.svg');
$bgColor = get_option('floating_background_color_'.$i);
$url = get_option('floating_button_url_'.$i);
$name = get_option('floating_button_name_'.$i);
if (!empty($url) && !empty($name)) {
?>
<a href="<?= $url ?>" class="style-1 button_whatsapp_<?= $i ?>" target="_blank" style="background: <?= $bgColor ?> url('<?= $imageUrl ?>') center center no-repeat;">
<span data="<?= $image ?>">
<?= $name ?>
</span>
</a>
<?php
}
}
}
if ($selectstyle == 'style-2') {
$image = get_option('floating_image_group');
$imageUrl = wp_get_attachment_image_url($image, 'original');
$bgColor = get_option('floating_background_color_group');
?>
<a class="gate-style-2"><span>Call Us</span></a>
<style>
.gate-style-2 {
background: <?= $bgColor ?> url('<?= $imageUrl ?>') center center no-repeat;
}
.gate-style-2.x {
background-color: <?= $bgColor ?>;
}
</style>
<div class="wrap-style-2">
<?php
for ($i = 1; $i <= 5; $i++) {
$image = get_option('floating_image_'.$i);
$imageUrl = wp_get_attachment_image_url($image, 'original');
$bgColor = get_option('floating_background_color_'.$i);
$url = get_option('floating_button_url_'.$i);
$name = get_option('floating_button_name_'.$i);
if (!empty($url) && !empty($name)) {
?>
<a href="<?= $url ?>" class="style-2 button_whatsapp_<?= $i ?>" target="_blank" style="background: <?= $bgColor ?> url('<?= $imageUrl ?>') center center no-repeat;"><span>
<?= $name ?>
</span></a>
<?php
}
}
?>
</div>
<?php
}
});
function media_include_js()
{
// WordPress media uploader scripts
if (!did_action('wp_enqueue_media')) {
wp_enqueue_media();
}
// our custom JS
wp_enqueue_script(
'popup-media',
feature_dir('media.js'),
array('jquery')
);
}