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

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