PluginProbe
Bubble Menu – Floating Button Menu with Sticky Navigation / 2.2
Bubble Menu – Floating Button Menu with Sticky Navigation v2.2
trunk 1.3 2.0 2.2 2.2.1 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 4.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.1 4.1.1
bubble-menu / admin / class-admin.php

class-admin.php in Bubble Menu – Floating Button Menu with Sticky Navigation 2.2, at admin/class-admin.php

424 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Admin Class
5 *
6 * @package Wow_Plugin
7 * @subpackage Admin
8 * @author Dmytro Lobov <i@wpbiker.com>
9 * @copyright 2019 Wow-Company
10 * @license GNU Public License
11 * @version 1.0
12 */
13
14 namespace bubble_menu_lite;
15
16 if (!defined("ABSPATH")) {
17 exit();
18 }
19
20 /**
21 * Class Wow_Plugin_Admin
22 *
23 * @package wow_plugin
24 *
25 * @property array plugin - base information about the plugin
26 * @property array url - home, pro and other URL for plugin
27 * @property array rating - website and link for rating
28 *
29 */
30 class Wow_Plugin_Admin
31 {
32 /**
33 * Setup to admin panel of the plugin
34 *
35 * @param array $info general information about the plugin
36 *
37 * @since 1.0
38 */
39 public function __construct($info)
40 {
41 $this->plugin = $info["plugin"];
42 $this->url = $info["url"];
43 $this->rating = $info["rating"];
44
45 add_filter("plugin_action_links", [$this, "action_links"], 10, 2);
46 add_action("admin_menu", [$this, "add_admin_page"]);
47 add_filter("admin_footer_text", [$this, "rate_us"]);
48 // add_action( 'plugins_loaded', array( $this, 'plugin_check' ) );
49
50 add_action("admin_enqueue_scripts", [$this, "admin_style_script"]);
51 add_action("wp_ajax_wow_item_save", [$this, "item_save"]);
52
53 if (get_option("wow_" . $this->plugin["prefix"] . "_notice_action") != "read") {
54 add_action("admin_notices", [$this, "general_admin_notice"]);
55 add_action("admin_enqueue_scripts", [$this, "admin_notice_assets"]);
56 add_action("wp_ajax_bubble_menu_notice_action", [$this, "wow_notice_action_callback"]);
57 }
58 }
59
60 /**
61 * @param string|array $arg text which need show in the tooltip
62 *
63 * @return string tooltip for the element
64 */
65 static function tooltip($arg)
66 {
67 $tooltip = "";
68 foreach ($arg as $key => $value) {
69 if ($key == "title") {
70 $tooltip .= $value . "<p/>";
71 } elseif ($key == "ul") {
72 $tooltip .= "<ul>";
73 $arr = $value;
74 foreach ($arr as $val) {
75 $tooltip .= "<li>" . $val . "</li>";
76 }
77 $tooltip .= "</ul>";
78 } else {
79 $tooltip .= $value;
80 }
81 }
82 $tooltip = "<span class='wow-help dashicons dashicons-editor-help' title='" . $tooltip . "'></span>";
83
84 return $tooltip;
85 }
86
87 /**
88 * @param array $arg parameters for creating field in the backend
89 *
90 * @return string field for displaying
91 */
92 static function option($arg)
93 {
94 $id = isset($arg["id"]) ? $arg["id"] : null;
95 $name = isset($arg["name"]) ? $arg["name"] : "";
96 $type = isset($arg["type"]) ? $arg["type"] : "";
97 $func = !empty($arg["func"]) ? ' onchange="' . $arg["func"] . '"' : "";
98 $options = isset($arg["option"]) ? $arg["option"] : "";
99 $val = $arg["val"];
100 $separator = isset($arg["sep"]) ? "<" . esc_attr($arg["sep"]) . "/>" : "";
101 $class = isset($arg["class"]) ? ' class="' . $arg["class"] . '"' : "";
102 $field = "";
103
104 if ($type == "radio") {
105 // create radio fields
106 $option = "";
107 foreach ($options as $key => $value) {
108 $select = $key == $val ? ' checked="checked"' : "";
109 $option .= '<input name="' . esc_attr($name) . '" type="radio" value="' . esc_attr($key) . '" id="' . esc_attr($id) . "_" . esc_attr($key) . '" ' . $select . $func . $class . '><label for="' . esc_attr($id) . "_" . esc_attr($key) . '"> ' . esc_attr($value) . "</label>" . $separator;
110 }
111 $field = $option;
112 } elseif ($type == "checkbox") {
113 // create checkbox field
114 $select = !empty($val) ? ' checked="checked"' : "";
115 $id = $id === null ? "" : ' id="' . esc_attr($id) . '"';
116 $field = '<input type="checkbox" ' . $id . $select . $func . $class . ">" . $separator;
117 $field .= '<input type="hidden" name="' . esc_attr($name) . '" value="">';
118 } elseif ($type == "text" || $type == "number" || $type == "hidden") {
119 // create text field
120 $option = "";
121 if (is_array($options)) {
122 foreach ($options as $key => $value) {
123 $option .= " " . $key . '="' . $value . '"';
124 }
125 }
126 $id = $id === null ? "" : ' id="' . $id . '"';
127 $field = '<input name="' . esc_attr($name) . '" type="' . esc_attr($type) . '" value="' . esc_attr($val) . '"' . $id . $func . $option . $class . ">" . $separator;
128 } elseif ($type == "color") {
129 // create color field
130 $id = $id === null ? "" : ' id="' . $id . '"';
131 $field = '<input name="' . esc_attr($name) . '" type="text" value="' . esc_attr($val) . '" class="wp-color-picker-field" data-alpha="true"' . $id . ">" . $separator;
132 }
133 // create select field
134 elseif ($type == "select") {
135 $disabled = isset($arg["disabled"]) ? " disabled" : "";
136 $readonly = isset($arg["readonly"]) ? " readonly" : "";
137 $option = "";
138 foreach ($options as $key => $value) {
139 if (strrpos($key, "_start") != false) {
140 $option .= '<optgroup label="' . esc_attr($value) . '">';
141 } elseif (strrpos($key, "_end") != false) {
142 $option .= "</optgroup>";
143 } else {
144 $select = $key == $val ? 'selected="selected"' : "";
145 $option .= '<option value="' . esc_attr($key) . '" ' . $select . ">" . esc_attr($value) . "</option>";
146 }
147 }
148 $id = $id === null ? "" : ' id="' . $id . '"';
149 $field = '<select name="' . esc_attr($name) . '"' . $func . $class . $disabled . $readonly . $id . ">";
150 $field .= $option;
151 $field .= "</select>" . $separator;
152 } elseif ($type == "editor") {
153 // create editor field
154 $settings = [
155 "wpautop" => 0,
156 "textarea_name" => "" . $name . "",
157 "textarea_rows" => 15,
158 ];
159 $field = wp_editor($val, $id, $settings);
160 } elseif ($type == "textarea") {
161 // create textarea field
162 $id = $id === null ? "" : ' id="' . $id . '"';
163 $field = '<textarea name="' . esc_attr($name) . '"' . $id . ">" . $val . "</textarea>" . $separator;
164 }
165
166 return $field;
167 }
168
169 /**
170 * @param string $tooltip tooltip for element
171 *
172 * @return string
173 */
174 public function pro($tooltip = null)
175 {
176 $link = admin_url() . "admin.php?page=" . $this->plugin["slug"] . "&tab=extension";
177 $title = esc_attr__("More features in the PRO version", $this->plugin["text"]);
178 $classes = "wow-help dashicons dashicons-lock";
179 $tooltip = !empty($tooltip) ? $title . "<br/>" . $tooltip : $title;
180 $pro = '<a href="' . $link . '" class="' . $classes . '" title="' . $tooltip . '"></a>';
181
182 return $pro;
183 }
184
185 /**
186 * Add the plugin page in admin menu
187 *
188 * @since 1.0
189 */
190 public function add_admin_page()
191 {
192 $parent = "wow-company";
193 $title = $this->plugin["name"] . " version " . $this->plugin["version"];
194 $menu_title = $this->plugin["menu"];
195 $capability = "manage_options";
196 $slug = $this->plugin["slug"];
197 $function = [$this, "plugin_page"];
198 add_submenu_page($parent, $title, $menu_title, $capability, $slug, $function);
199 }
200
201 /**
202 * Include main plugin page with Style and Script
203 *
204 * @since 1.0
205 */
206 public function plugin_page()
207 {
208 global $wow_plugin_page;
209 $wow_plugin_page = $this->plugin["slug"];
210 require_once "partials/main.php";
211 }
212
213 /**
214 * Include Styles and Scripts on the plugin admin page
215 *
216 * @since 1.0
217 */
218 public function admin_style_script($hook)
219 {
220 $page = "wow-plugins_page_" . $this->plugin["slug"];
221
222 if ($page != $hook) {
223 return false;
224 }
225
226 $slug = $this->plugin["slug"];
227 $version = $this->plugin["version"];
228 $url_assets = $this->plugin["url"] . "assets/";
229
230 $pre_suffix = defined("SCRIPT_DEBUG") && SCRIPT_DEBUG ? "" : ".min";
231
232 // include the main style
233 wp_enqueue_style($slug . "-admin", $url_assets . "css/admin-style" . $pre_suffix . ".css", false, $version);
234
235 // include fontAwesome icon
236 $url_fontawesome = $url_assets . "vendors/fontawesome/css/fontawesome-all" . $pre_suffix . ".css";
237 wp_enqueue_style($slug . "-fontawesome", $url_fontawesome, null, "5.14");
238
239 // include fonticonpicker styles & scripts
240 $fonticonpicker_js = $url_assets . "vendors/fonticonpicker/fonticonpicker.min.js";
241 wp_enqueue_script($slug . "-fonticonpicker", $fonticonpicker_js, ["jquery"]);
242
243 $fonticonpicker_css = $url_assets . "vendors/fonticonpicker/css/fonticonpicker.min.css";
244 wp_enqueue_style($slug . "-fonticonpicker", $fonticonpicker_css);
245
246 $fonticonpicker_dark_css = $url_assets . "vendors/fonticonpicker/fonticonpicker.darkgrey.min.css";
247 wp_enqueue_style($slug . "-fonticonpicker-darkgrey", $fonticonpicker_dark_css);
248
249 // include the color picker
250 wp_enqueue_style("wp-color-picker");
251 wp_enqueue_script("wp-color-picker");
252
253 // include tooltip script
254 wp_enqueue_script("jquery-ui-tooltip");
255
256 // include sortable
257 wp_enqueue_script("jquery-ui-sortable");
258
259 // include an alpha rgba color picker
260 $url_alpha = $url_assets . "js/wp-color-picker-alpha" . $pre_suffix . ".js";
261 wp_enqueue_script("wp-color-picker-alpha", $url_alpha, ["wp-color-picker"]);
262
263 // include the plugin admin script
264 $url_script = $url_assets . "js/admin-script.js";
265 wp_enqueue_script($slug . "-admin", $url_script, ["jquery"], $version, true);
266 }
267
268 /**
269 * Add the link to the plugin page on Plugins page
270 *
271 * @param $actions
272 * @param $plugin_file - the plugin main file
273 *
274 * @return mixed
275 */
276 public function action_links($actions, $plugin_file)
277 {
278 if (false === strpos($plugin_file, plugin_basename($this->plugin["file"]))) {
279 return $actions;
280 }
281 $settings_link = '<a href="admin.php?page=' . $this->plugin["slug"] . '">' . esc_attr__("Settings", $this->plugin["text"]) . "</a>";
282 array_unshift($actions, $settings_link);
283
284 return $actions;
285 }
286
287 /**
288 * Add custom text in the footer on the wow plugin page
289 *
290 * @param $footer_text - text in the footer
291 *
292 * @return string - end text in the footer
293 * @since 1.0
294 */
295 public function rate_us($footer_text)
296 {
297 global $wow_plugin_page;
298 if ($wow_plugin_page == $this->plugin["slug"]) {
299 $rate_text = sprintf(__('Thank you for using <a href="%1$s" target="_blank">' . $this->plugin["name"] . '</a>! Please <a href="%2$s" target="_blank">rate us on ' . $this->rating["website"] . "</a>", $this->plugin["text"]), $this->url["home"], $this->rating["url"]);
300
301 return str_replace("</span>", "", $footer_text) . " | " . $rate_text . "</span>";
302 } else {
303 return $footer_text;
304 }
305 }
306
307 /*
308 * Check the post parameter from wow plugin
309 *
310 * @since 1.0
311 */
312 public function plugin_check()
313 {
314 if (isset($_POST[$this->plugin["slug"] . "_nonce"])) {
315 if (!empty($_POST) && wp_verify_nonce($_POST[$this->plugin["slug"] . "_nonce"], $this->plugin["slug"] . "_action") && current_user_can("manage_options")) {
316 self::save_data();
317 }
318 }
319 }
320
321 /**
322 * Save and Update the Item into the plugin Database
323 *
324 * @return array response from DB
325 *
326 * @since 1.0
327 */
328 public function save_data()
329 {
330 $save_class = __NAMESPACE__ . "\\Wow_DB_Update";
331 $objItem = new $save_class($this->plugin["dir"], $this->plugin["slug"]);
332 $add = isset($_REQUEST["add"]) ? absint($_REQUEST["add"]) : "";
333 $table = isset($_REQUEST["data"]) ? sanitize_text_field($_REQUEST["data"]) : "";
334 $tool_id = absint($_POST["tool_id"]);
335 $info = [];
336 $info["id"] = $tool_id;
337 $info["title"] = sanitize_text_field($_POST["title"]);
338 $info["param"] = $_POST["param"];
339 $response = "";
340 if ("1" == $add) {
341 $objItem->create_item($table, $info);
342 $response = [
343 "status" => "OK",
344 "message" => esc_attr__("Item Added", $this->plugin["text"]),
345 ];
346 } elseif ("2" == $add) {
347 $objItem->update_item($table, $info);
348 $response = [
349 "status" => "OK",
350 "message" => esc_attr__("Item Updated", $this->plugin["text"]),
351 ];
352 }
353
354 return $response;
355 }
356
357 function item_save()
358 {
359 $response = "No";
360 if (isset($_POST[$this->plugin["slug"] . "_nonce"])) {
361 if (!empty($_POST) && wp_verify_nonce($_POST[$this->plugin["slug"] . "_nonce"], $this->plugin["slug"] . "_action") && current_user_can("manage_options")) {
362 $response = self::save_data();
363 }
364 }
365
366 wp_send_json($response);
367
368 wp_die();
369 }
370
371 /**
372 * Delete the files from plugin folder in 'uploads'
373 *
374 * @param integer $id id of the item
375 *
376 * @since 1.0
377 */
378 public function delete_file($id = null)
379 {
380 $upload = wp_upload_dir();
381 $basedir = $upload["basedir"] . "/" . $this->plugin["slug"] . "/";
382 $file_style = $basedir . "style-" . $id . ".css";
383 $file_script = $basedir . "script-" . $id . ".js";
384 if (file_exists($file_style)) {
385 wp_delete_file($file_style);
386 }
387 if (file_exists($file_script)) {
388 wp_delete_file($file_script);
389 }
390
391 return;
392 }
393
394 /*
395 * Notice for updating the plugin
396 */
397
398 function general_admin_notice()
399 {
400 echo '<div class="notice notice-warning wow-plugin-notice is-dismissible">
401 <p><b>' .
402 $this->plugin["name"] .
403 '</b>! In connection with the upgrade of Font Awesome to version 5, we ask you to edit and update all previously created menus. <a href="' .
404 admin_url() .
405 "admin.php?page=" .
406 $this->plugin["slug"] .
407 '">Go to the ' .
408 $this->plugin["name"] .
409 ' page</a></p>
410 </div>';
411 }
412
413 function admin_notice_assets()
414 {
415 wp_enqueue_script("wow-notice-update", $this->plugin["url"] . "assets/js/notice-update.min.js", ["jquery"], "1.0", true);
416 }
417
418 function wow_notice_action_callback()
419 {
420 update_option("wow_" . $this->plugin["prefix"] . "_notice_action", "read");
421 wp_die();
422 }
423 }
424