PluginProbe
Float menu – awesome floating side menu / 5.0.2
Float menu – awesome floating side menu v5.0.2
7.2.5 trunk 2.1 2.2 3.0.1 3.1 3.2.2 3.3.1 3.5 3.5.1 3.5.2 3.5.3. 3.5.4 4.0 4.1 4.1.1 4.2 4.3 4.3.1 4.3.2 5.0 5.0.1 5.0.2 5.0.3 5.1 All 57 releases
float-menu / admin / class-admin.php

class-admin.php in Float menu – awesome floating side menu 5.0.2, at admin/class-admin.php

476 lines 12.9 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 Wow-Company <yoda@wow-company.com>
9 * @copyright 2019 Wow-Company
10 * @license GNU Public License
11 * @version 1.0
12 */
13
14 namespace float_menu_free;
15
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit;
19 }
20
21 /**
22 * Class Wow_Plugin_Admin
23 *
24 * @package wow_plugin
25 *
26 * @property array plugin - base information about the plugin
27 * @property array url - home, pro and other URL for plugin
28 * @property array rating - website and link for rating
29 *
30 */
31 class Wow_Plugin_Admin {
32
33 /**
34 * Setup to admin panel of the plugin
35 *
36 * @param array $info general information about the plugin
37 *
38 * @since 1.0
39 */
40 public function __construct( $info ) {
41 $this->plugin = $info['plugin'];
42 $this->url = $info['url'];
43 $this->rating = $info['rating'];
44
45 add_filter( 'plugin_action_links', array( $this, 'action_links' ), 10, 2 );
46 add_action( 'admin_menu', array( $this, 'add_admin_page' ) );
47 add_filter( 'admin_footer_text', array( $this, 'rate_us' ) );
48 add_action( 'wp_ajax_' . $this->plugin['prefix'] . '_item_save', array( $this, 'item_save' ) );
49 add_action( 'wp_ajax_float_menu_message', array( $this, 'wow_message_callback' ) );
50 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
51 add_action( 'admin_init', [ $this, 'export_data' ] );
52 add_action( 'admin_init', [ $this, 'import_data' ] );
53
54 }
55
56 public function wow_message_callback() {
57 update_option( 'wow_' . $this->plugin['prefix'] . '_message', 'read' );
58 wp_die();
59 }
60
61 /**
62 * @param string|array $arg text which need show in the tooltip
63 *
64 * @return string tooltip for the element
65 */
66 static function tooltip( $arg ) {
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 echo "<span class='wow-help dashicons dashicons-editor-help' title='" . wp_kses_post( $tooltip ) . "'></span>";
83 }
84
85 /**
86 * @param array $arg parameters for creating field in the backend
87 *
88 * @return string field for displaying
89 */
90 static function option( $arg ) {
91 $id = isset( $arg['id'] ) ? $arg['id'] : null;
92 $name = isset( $arg['name'] ) ? $arg['name'] : '';
93 $type = isset( $arg['type'] ) ? $arg['type'] : '';
94 $func = ! empty( $arg['func'] ) ? ' onchange="' . $arg['func'] . '"' : '';
95 $options = isset( $arg['option'] ) ? $arg['option'] : '';
96 $val = $arg['val'];
97 $separator = isset( $arg['sep'] ) ? $arg['sep'] : '';
98 $extra_class = '';
99 if ( $type == 'text' || $type == 'number' || $type == 'time' ) {
100 $extra_class = 'input ';
101 }
102 $class = isset( $arg['class'] ) ? ' class="' . $extra_class . $arg['class'] . '"' : ' class="' . $extra_class . '"';
103 $field = '';
104
105 switch ( $type ) {
106 case 'radio':
107 include( 'fields/radio.php' );
108 break;
109 case 'checkbox':
110 include( 'fields/checkbox.php' );
111 break;
112 case 'text':
113 case 'number':
114 case 'hidden':
115 case 'time':
116 include( 'fields/input.php' );
117 break;
118 case 'select':
119 include( 'fields/select.php' );
120 break;
121 case 'color':
122 include( 'fields/color.php' );
123 break;
124 case 'editor':
125 include( 'fields/editor.php' );
126 break;
127 }
128 }
129
130 /**
131 * @param string $tooltip tooltip for element
132 *
133 * @return string
134 */
135 public function pro( $tooltip = null ) {
136 $link = admin_url() . 'admin.php?page=' . $this->plugin['slug'] . '&tab=extension';
137 $title = esc_attr__( 'More features in the PRO version', $this->plugin['text'] );
138 $classes = 'wow-help dashicons dashicons-lock';
139 $tooltip = ! empty( $tooltip ) ? $title . '<br/>' . $tooltip : $title;
140 echo '<a href="' . esc_url( $link ) . '" class="' . esc_attr( $classes ) . '" title="' . wp_kses_post( $tooltip ) . '"></a>';
141 }
142
143 /**
144 * Add the plugin page in admin menu
145 *
146 * @since 1.0
147 */
148 public function add_admin_page() {
149 $parent = 'wow-company';
150 $title = $this->plugin['name'] . ' version ' . $this->plugin['version'];
151 $menu_title = $this->plugin['menu'];
152 $capability = 'manage_options';
153 $slug = $this->plugin['slug'];
154 $function = array( $this, 'plugin_page' );
155 add_submenu_page( $parent, $title, $menu_title, $capability, $slug, $function );
156 }
157
158 /**
159 * Include main plugin page with Style and Script
160 *
161 * @since 1.0
162 */
163 public function plugin_page() {
164 global $wow_plugin_page;
165 $wow_plugin_page = $this->plugin['slug'];
166 require_once 'page-main.php';
167 }
168
169
170 /**
171 * Include Styles and Scripts on the plugin admin page
172 *
173 * @since 1.0
174 */
175 public function admin_scripts( $hook ) {
176 $page = 'wow-plugins_page_' . $this->plugin['slug'];
177
178 if ( $page != $hook ) {
179 return false;
180 }
181 $slug = $this->plugin['slug'];
182 $version = $this->plugin['version'];
183 $url_assets = plugin_dir_url( __FILE__ ) . 'assets/';
184
185 $pre_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
186
187 // include the main style
188 wp_enqueue_style( $slug . '-admin', $url_assets . 'css/style.css', false, $version );
189
190 // include fontAwesome icon
191 $url_fontawesome = $this->plugin['url'] . 'vendors/fontawesome/css/fontawesome-all.min.css';
192 wp_enqueue_style( $slug . '-fontawesome', $url_fontawesome, null, '5.15.3' );
193
194 // include fonticonpicker styles & scripts
195 $fonticonpicker_js = $url_assets . 'fonticonpicker/fonticonpicker.min.js';
196 wp_enqueue_script( $slug . '-fonticonpicker', $fonticonpicker_js, array( 'jquery' ) );
197
198 $fonticonpicker_css = $url_assets . 'fonticonpicker/css/fonticonpicker.min.css';
199 wp_enqueue_style( $slug . '-fonticonpicker', $fonticonpicker_css );
200
201 $fonticonpicker_dark_css = $url_assets . 'fonticonpicker/fonticonpicker.darkgrey.min.css';
202 wp_enqueue_style( $slug . '-fonticonpicker-darkgrey', $fonticonpicker_dark_css );
203
204 // include the color picker
205 wp_enqueue_style( 'wp-color-picker' );
206 wp_enqueue_script( 'wp-color-picker' );
207
208 $url_alpha = $url_assets . 'js/wp-color-picker-alpha.min.js';
209 wp_enqueue_script( 'wp-color-picker-alpha', $url_alpha, array( 'wp-color-picker' ) );
210
211 // include tooltip script
212 wp_enqueue_script( 'jquery-ui-tooltip' );
213
214 // include sortable
215 wp_enqueue_script( 'jquery-ui-sortable' );
216
217
218 // include the plugin admin script
219 $url_script = $url_assets . 'js/script.js';
220 wp_enqueue_script( $slug . '-admin', $url_script, array( 'jquery' ), $version, true );
221
222 }
223
224 /**
225 * Add the link to the plugin page on Plugins page
226 *
227 * @param $actions
228 * @param $plugin_file - the plugin main file
229 *
230 * @return mixed
231 */
232 public function action_links( $actions, $plugin_file ) {
233 if ( false === strpos( $plugin_file, plugin_basename( $this->plugin['file'] ) ) ) {
234 return $actions;
235 }
236 $settings_link
237 = '<a href="admin.php?page=' . esc_attr( $this->plugin['slug'] ) . '">' . esc_attr__( 'Settings',
238 $this->plugin['text'] ) . '</a>';
239 array_unshift( $actions, $settings_link );
240
241 return $actions;
242 }
243
244 /**
245 * Add custom text in the footer on the wow plugin page
246 *
247 * @param $footer_text - text in the footer
248 *
249 * @return string - end text in the footer
250 * @since 1.0
251 */
252 public function rate_us( $footer_text ) {
253 global $wow_plugin_page;
254 if ( $wow_plugin_page == $this->plugin['slug'] ) {
255 $rate_text = sprintf( __( 'Thank you for using <a href="%1$s" target="_blank">' . esc_attr( $this->plugin['name'] )
256 . '</a>! Please <a href="%2$s" target="_blank">rate us on '
257 . esc_attr( $this->rating['website'] ) . '</a>', $this->plugin['text'] ), $this->url['home'],
258 $this->rating['url'] );
259
260 return str_replace( '</span>', '', $footer_text ) . ' | ' . $rate_text . '</span>';
261 } else {
262 return $footer_text;
263 }
264 }
265
266 function item_save() {
267
268 $response = 'No';
269 if ( isset( $_POST[ $this->plugin['slug'] . '_nonce' ] ) ) {
270 if ( ! empty( $_POST )
271 && wp_verify_nonce( $_POST[ $this->plugin['slug'] . '_nonce' ], $this->plugin['slug'] . '_action' )
272 && current_user_can( 'manage_options' )
273 ) {
274 $response = self:: save_data();
275 }
276 }
277
278 wp_send_json( $response );
279
280 wp_die();
281
282 }
283
284 /**
285 * Save and Update the Item into the plugin Database
286 *
287 * @return array response from DB
288 *
289 * @since 1.0
290 */
291 public function save_data() {
292 global $wpdb;
293 $add = ( isset( $_REQUEST['add'] ) ) ? absint( $_REQUEST['add'] ) : '';
294 $table = $wpdb->prefix . 'wow_' . $this->plugin['prefix'];
295 $id = absint( $_POST['tool_id'] );
296 $title = sanitize_text_field( $_POST['title'] );
297 $param = map_deep( $_POST['param'], array( $this, 'sanitize_param' ) );
298 $param['menu_1']['item_tooltip'] = map_deep( $_POST['param']['menu_1']['item_tooltip'], array(
299 $this,
300 'sanitize_tooltip'
301 ) );
302 $param = serialize( $param );
303
304 $response = array(
305 'status' => 'NO',
306 'message' => esc_attr__( 'Something went wrong. Contact the plugin developer', $this->plugin['text'] ),
307 );
308 if ( 1 === $add ) {
309
310 $insert = $wpdb->query(
311 $wpdb->prepare( " INSERT INTO {$table} ( id, title, param ) VALUES ( %d, %s, %s )",
312 $id,
313 $title,
314 $param
315 ) );
316 if ( $insert ) {
317 $response = array(
318 'status' => 'OK',
319 'message' => esc_attr__( 'Item Added', 'side-menu' ),
320 );
321 }
322
323 } elseif ( 2 === $add ) {
324 $update =
325 $wpdb->query(
326 $wpdb->prepare( " UPDATE {$table} SET title = %s, param = %s WHERE id= %d;",
327 $title,
328 $param,
329 $id
330 ) );
331
332 if ( ! empty( $update ) ) {
333 $response = array(
334 'status' => 'OK',
335 'message' => esc_attr__( 'Item Updated', 'side-menu' ),
336 );
337 }
338 }
339
340 return $response;
341 }
342
343 public function sanitize_param( $value ) {
344 return wp_unslash( sanitize_text_field( $value ) );
345 }
346
347 public function sanitize_tooltip( $value ) {
348 return wp_unslash( wp_kses_post( wp_encode_emoji( $value ) ) );
349 }
350
351 public function import_data() {
352
353 if ( empty( $_POST[ $this->plugin['slug'] . '_import_nonce' ] ) ) {
354 return;
355 }
356
357
358 if ( ! wp_verify_nonce( $_POST[ $this->plugin['slug'] . '_import_nonce' ], $this->plugin['slug'] . '_import_nonce' ) ) {
359 return;
360 }
361
362
363 if ( ! current_user_can( 'manage_options' ) ) {
364 return;
365 }
366
367 if ( $this->get_file_extension( $_FILES['import_file']['name'] ) != 'json' ) {
368 wp_die( esc_attr__( 'Please upload a valid .json file', $this->plugin['text'] ), __( 'Error', $this->plugin['text'] ), array( 'response' => 400 ) );
369 }
370
371 $import_file = $_FILES['import_file']['tmp_name'];
372
373 if ( empty( $import_file ) ) {
374 wp_die( esc_attr__( 'Please upload a file to import', $this->plugin['text'] ), __( 'Error', $this->plugin['text'] ), array( 'response' => 400 ) );
375 }
376
377 // Retrieve the settings from the file and convert the json object to an array
378 $settings = json_decode( file_get_contents( $import_file ) );
379
380 $update = ! empty( $_POST['wow_import_update'] ) ? '1' : '';
381
382 global $wpdb;
383 $table = $wpdb->prefix . "wow_" . $this->plugin['prefix'];
384
385 foreach ( $settings as $key => $val ) {
386 $id = $val->id;
387 $title = $val->title;
388 $param = $val->param;
389
390 $check_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ) );
391
392 if ( ! empty( $update ) && ! empty( $check_row ) ) {
393 $wpdb->query(
394 $wpdb->prepare( " UPDATE {$table} SET title = %s, param = %s WHERE id= %d;",
395 $title,
396 $param,
397 $id
398 ) );
399
400 } elseif ( ! empty( $check_row ) ) {
401 $wpdb->query(
402 $wpdb->prepare( " INSERT INTO {$table} ( title, param ) VALUES ( %s, %s )",
403 $title,
404 $param
405 ) );
406 } else {
407 $wpdb->query(
408 $wpdb->prepare( " INSERT INTO {$table} ( id, title, param ) VALUES ( %d, %s, %s )",
409 $id,
410 $title,
411 $param
412 ) );
413 }
414
415 }
416
417 wp_safe_redirect( admin_url( 'admin.php?page=' . esc_attr( $this->plugin['slug'] ) ) );
418 exit;
419
420
421 }
422
423 public function export_data() {
424
425 if ( empty( $_POST[ $this->plugin['slug'] . '_export_nonce' ] ) ) {
426 return;
427 }
428
429
430 if ( ! wp_verify_nonce( $_POST[ $this->plugin['slug'] . '_export_nonce' ], $this->plugin['slug'] . '_export_nonce' ) ) {
431 return;
432 }
433
434
435 if ( ! current_user_can( 'manage_options' ) ) {
436 return;
437 }
438
439 $file_name = $this->plugin['shortcode'] . '-database-' . date( 'm-d-Y' ) . '.json';
440
441 global $wpdb;
442 $table = $wpdb->prefix . "wow_" . $this->plugin['prefix'];
443 $result = $wpdb->get_results( "SELECT * FROM " . $table . " order by id asc" );
444
445 $settings = array();
446 if ( count( $result ) > 0 ) {
447 foreach ( $result as $key => $val ) {
448 $settings[] = array(
449 'id' => $val->id,
450 'title' => $val->title,
451 'param' => $val->param,
452 );
453 }
454 }
455 ignore_user_abort( true );
456 nocache_headers();
457 header( 'Content-Type: application/json; charset=utf-8' );
458 header( 'Content-Disposition: attachment; filename=' . $file_name );
459 header( "Expires: 0" );
460
461 echo json_encode( $settings );
462 exit;
463
464
465 }
466
467
468 function get_file_extension( $str ) {
469 $parts = explode( '.', $str );
470
471 return end( $parts );
472 }
473
474
475 }
476