PluginProbe
Button Generator – Easily Create Custom Buttons with Icons and Analytics / 2.3.9
Button Generator – Easily Create Custom Buttons with Icons and Analytics v2.3.9
trunk 1.1.1 2.0 2.1 2.2 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.2 3.1.3 3.2 3.2.1 3.2.2 3.2.3 All 28 releases
button-generation / admin / class-admin.php

class-admin.php in Button Generator – Easily Create Custom Buttons with Icons and Analytics 2.3.9, at admin/class-admin.php

385 lines 12.3 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 button_generator;
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 private $plugin;
34 private $url;
35 private $rating;
36
37 /**
38 * Setup to admin panel of the plugin
39 *
40 * @param array $info general information about the plugin
41 *
42 * @since 1.0
43 */
44 public function __construct( $info ) {
45 $this->plugin = $info['plugin'];
46 $this->url = $info['url'];
47 $this->rating = $info['rating'];
48
49 add_filter( 'plugin_action_links', array( $this, 'action_links' ), 10, 2 );
50 add_action( 'admin_menu', array( $this, 'add_admin_page' ) );
51 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
52 add_filter( 'admin_footer_text', array( $this, 'rate_us' ) );
53 add_action( 'wp_ajax_button_generation_message', array( $this, 'wow_message_callback' ) );
54 add_action( 'wp_ajax_wow_item_save', array( $this, 'item_save' ) );
55 }
56
57 public function wow_message_callback() {
58 update_option( 'wow_' . $this->plugin['prefix'] . '_message', 'read' );
59 wp_die();
60 }
61
62 /**
63 * @param string|array $arg text which need show in the tooltip
64 *
65 * @return string tooltip for the element
66 */
67 static function tooltip( $arg ) {
68 $tooltip = '';
69 foreach ( $arg as $key => $value ) {
70 if ( $key == 'title' ) {
71 $tooltip .= $value . '<p/>';
72 } elseif ( $key == 'ul' ) {
73 $tooltip .= '<ul>';
74 $arr = $value;
75 foreach ( $arr as $val ) {
76 $tooltip .= '<li>' . $val . '</li>';
77 }
78 $tooltip .= '</ul>';
79 } else {
80 $tooltip .= $value;
81 }
82 }
83 $tooltip = "<span class='wow-help dashicons dashicons-editor-help' title='" . $tooltip . "'></span>";
84
85 return $tooltip;
86 }
87
88 /**
89 * @param array $arg parameters for creating field in the backend
90 *
91 * @return string field for displaying
92 */
93 static function option( $arg ) {
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'] ) ? $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) . '" ' .
110 $select . $func . $class . '><label for="' . esc_attr($id) . '_' . esc_attr($key) . '"> ' . esc_attr($value) . '</label>' .
111 $separator;
112 }
113 $field = $option;
114 } elseif ( $type == 'checkbox' ) {
115 // create checkbox field
116 $select = ! empty( $val ) ? 'checked="checked"' : '';
117 $field = '<input type="checkbox" ' . $select . $func . $class . ' id="' . esc_attr($id) . '" >' . $separator;
118 $field .= '<input type="hidden" name="' . esc_attr($name) . '" value="">';
119 } elseif ( $type == 'text' || $type == 'number' || $type == 'hidden' ) {
120 // create text field
121 $option = '';
122 if ( is_array( $options ) ) {
123 foreach ( $options as $key => $value ) {
124 $option .= ' ' . $key . '="' . $value . '"';
125 }
126 }
127 $field =
128 '<input name="' . esc_attr($name) . '" type="' . esc_attr($type) . '" value="' . esc_attr($val) . '" id="' . esc_attr($id) . '"' . $func . $option .
129 $class . '>' . $separator;
130 } elseif ( $type == 'color' ) {
131 // create color field
132 $field = '<input name="' . esc_attr($name) . '" type="text" value="' . esc_attr($val) .
133 '" class="wp-color-picker-field" data-alpha="true" id="' . esc_attr($id) . '">' . $separator;
134 } // create select field
135 elseif ( $type == 'select' ) {
136 $disabled = isset( $arg['disabled'] ) ? ' disabled' : '';
137 $readonly = isset( $arg['readonly'] ) ? ' readonly' : '';
138 $option = '';
139 foreach ( $options as $key => $value ) {
140 if ( strrpos( $key, '_start' ) != false ) {
141 $option .= '<optgroup label="' . $value . '">';
142 } elseif ( strrpos( $key, '_end' ) != false ) {
143 $option .= '</optgroup>';
144 } else {
145 $select = ( $key == $val ) ? 'selected="selected"' : '';
146 $option .= '<option value="' . esc_attr($key) . '" ' . $select . '>' . esc_attr($value) . '</option>';
147 }
148 }
149 $field = '<select name="' .esc_attr( $name ) . '"' . $func . $class . $disabled . $readonly . ' id="' . esc_attr($id) . '">';
150 $field .= $option;
151 $field .= '</select>' . $separator;
152 } elseif ( $type == 'editor' ) {
153 // create editor field
154 $settings = array(
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 $field = '<textarea name="' . esc_attr($name) . '" id="' . esc_attr($id) . '">' . esc_attr($val) . '</textarea>' . $separator;
163 }
164
165 return $field;
166 }
167
168 /**
169 * @param string $tooltip tooltip for element
170 *
171 * @return string
172 */
173 public function pro( $tooltip = null ) {
174 $link = admin_url() . 'admin.php?page=' . $this->plugin['slug'] . '&tab=extension';
175 $title = esc_attr__( 'More features in the PRO version', $this->plugin['text'] );
176 $classes = 'wow-help dashicons dashicons-lock';
177 $tooltip = ! empty( $tooltip ) ? $title . '<br/>' . $tooltip : $title;
178 $pro = '<a href="' . $link . '" class="' . $classes . '" title="' . $tooltip . '"></a>';
179
180 return $pro;
181 }
182
183 /**
184 * Add the plugin page in admin menu
185 *
186 * @since 1.0
187 */
188 public function add_admin_page() {
189 $parent = 'wow-company';
190 $title = $this->plugin['name'] . ' version ' . $this->plugin['version'];
191 $menu_title = $this->plugin['menu'];
192 $capability = 'manage_options';
193 $slug = $this->plugin['slug'];
194 $function = array( $this, 'plugin_page' );
195 add_submenu_page( $parent, $title, $menu_title, $capability, $slug, $function );
196 }
197
198 /**
199 * Include main plugin page with Style and Script
200 *
201 * @since 1.0
202 */
203 public function plugin_page() {
204 global $wow_plugin_page;
205 $wow_plugin_page = $this->plugin['slug'];
206 require_once 'partials/main.php';
207 // self:: admin_style_script();
208 }
209
210 /**
211 * Include Styles and Scripts on the plugin admin page
212 *
213 * @since 1.0
214 */
215 public function admin_scripts( $hook ) {
216
217 $page = 'wow-plugins_page_' . $this->plugin['slug'];
218
219 if ( $page != $hook ) {
220 return;
221 }
222
223 $slug = $this->plugin['slug'];
224 $version = $this->plugin['version'];
225 $url_assets = $this->plugin['url'] . 'assets/';
226
227
228 // include the main style
229 wp_enqueue_style( $slug . '-admin', $url_assets . 'css/admin-style.css', false, $version );
230
231 // include fontAwesome icon
232 $url_fontawesome = $url_assets . 'vendors/fontawesome/css/fontawesome-all.min.css';
233 wp_enqueue_style( $slug . '-fontawesome', $url_fontawesome, null, '5.6.3' );
234
235 // include fonticonpicker styles & scripts
236 $fonticonpicker_js = $url_assets . 'vendors/fonticonpicker/fonticonpicker.min.js';
237 wp_enqueue_script( $slug . '-fonticonpicker', $fonticonpicker_js, array( 'jquery' ) );
238
239 $fonticonpicker_css = $url_assets . 'vendors/fonticonpicker/css/fonticonpicker.min.css';
240 wp_enqueue_style( $slug . '-fonticonpicker', $fonticonpicker_css );
241
242 $fonticonpicker_dark_css = $url_assets . 'vendors/fonticonpicker/fonticonpicker.darkgrey.min.css';
243 wp_enqueue_style( $slug . '-fonticonpicker-darkgrey', $fonticonpicker_dark_css );
244
245 // include the color picker
246 wp_enqueue_style( 'wp-color-picker' );
247 wp_enqueue_script( 'wp-color-picker' );
248
249 // include tooltip script
250 wp_enqueue_script( 'jquery-ui-tooltip' );
251
252 // include an alpha rgba color picker
253 $url_alpha = $url_assets . 'js/wp-color-picker-alpha.min.js';
254 wp_enqueue_script( 'wp-color-picker-alpha', $url_alpha, array( 'wp-color-picker' ) );
255
256 // include the plugin admin script
257 $url_script = $url_assets . 'js/admin-script.js';
258 wp_enqueue_script( $slug . '-admin', $url_script, array( 'jquery', 'jquery-ui-tooltip', 'wp-color-picker' ), $version, true );
259 wp_localize_script( $slug . '-admin', 'btg_count', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'btn_security' ), ) );
260
261 // include the plugin preview script
262 $url_preview = $url_assets . 'js/preview.min.js';
263 wp_enqueue_script( $slug . '-preview', $url_preview, array( 'jquery' ), $version, true );
264
265 }
266
267 /**
268 * Add the link to the plugin page on Plugins page
269 *
270 * @param $actions
271 * @param $plugin_file - the plugin main file
272 *
273 * @return mixed
274 */
275 public function action_links( $actions, $plugin_file ) {
276 if ( false === strpos( $plugin_file, plugin_basename( $this->plugin['file'] ) ) ) {
277 return $actions;
278 }
279 $settings_link =
280 '<a href="admin.php?page=' . $this->plugin['slug'] . '">' . esc_attr__( 'Settings', $this->plugin['text'] ) . '</a>';
281 array_unshift( $actions, $settings_link );
282
283 return $actions;
284 }
285
286 /**
287 * Add custom text in the footer on the wow plugin page
288 *
289 * @param $footer_text - text in the footer
290 *
291 * @return string - end text in the footer
292 * @since 1.0
293 */
294 public function rate_us( $footer_text ) {
295 global $wow_plugin_page;
296 if ( $wow_plugin_page == $this->plugin['slug'] ) {
297 $rate_text = sprintf( __( 'Thank you for using <a href="%1$s" target="_blank">' . $this->plugin['name'] .
298 '</a>! Please <a href="%2$s" target="_blank">rate us on ' . $this->rating['website'] .
299 '</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 /**
309 * Save and Update the Item into the plugin Database
310 *
311 * @return array response from DB
312 *
313 * @since 1.0
314 */
315 public function save_data() {
316 global $wpdb;
317 $add = ( isset( $_REQUEST['add'] ) ) ? absint( $_REQUEST['add'] ) : '';
318 $table = $wpdb->prefix . 'wow_button_generator';
319 $id = absint( $_POST['tool_id'] );
320 $title = sanitize_text_field( $_POST['title'] );
321 $param = map_deep( $_POST['param'], array( $this, 'sanitize_param' ) );
322 $param = serialize( $param );
323
324 $response = array(
325 'status' => 'NO',
326 'message' => esc_attr__( 'Something went wrong. Contact the plugin developer', $this->plugin['text'] ),
327 );
328 if ( 1 === $add ) {
329
330 $insert = $wpdb->query(
331 $wpdb->prepare( " INSERT INTO {$table} ( id, title, param ) VALUES ( %d, %s, %s )",
332 $id,
333 $title,
334 $param
335 ) );
336 if ( $insert ) {
337 $response = array(
338 'status' => 'OK',
339 'message' => esc_attr__( 'Item Added', 'button_generator' ),
340 );
341 }
342
343 } elseif ( 2 === $add ) {
344 $update =
345 $wpdb->query(
346 $wpdb->prepare( " UPDATE {$table} SET title = %s, param = %s WHERE id= %d;",
347 $title,
348 $param,
349 $id
350 ) );
351
352 if ( ! empty( $update ) ) {
353 $response = array(
354 'status' => 'OK',
355 'message' => esc_attr__( 'Item Updated', 'button_generator' ),
356 );
357 }
358 }
359
360 return $response;
361 }
362
363 public function sanitize_param( $value ) {
364 return wp_unslash( sanitize_text_field( $value ) );
365 }
366
367 function item_save() {
368
369 $response = 'No';
370 if ( isset( $_POST[ $this->plugin['slug'] . '_nonce' ] ) ) {
371 if ( ! empty( $_POST ) &&
372 wp_verify_nonce( $_POST[ $this->plugin['slug'] . '_nonce' ], $this->plugin['slug'] . '_action' ) &&
373 current_user_can( 'manage_options' ) ) {
374 $response = self:: save_data();
375 }
376 }
377
378 wp_send_json( $response );
379
380 wp_die();
381
382 }
383
384 }
385