PluginProbe
Button Generator – Easily Create Custom Buttons with Icons and Analytics / 2.3.3
Button Generator – Easily Create Custom Buttons with Icons and Analytics v2.3.3
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.3, at admin/class-admin.php

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