| @@ -1,468 +1,417 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Admin Class | |
| 5 | - * | |
| 6 | - * @package Wow_Plugin | |
| 7 | - * @subpackage Admin | |
| 8 | - * @author Wow-Company <helper@wow-company.com> | |
| 9 | - * @copyright 2019 Wow-Company | |
| 10 | - * @license GNU Public License | |
| 11 | - * @version 1.0 | |
| 12 | - */ | |
| 13 | - | |
| 14 | -namespace floatingbutton; | |
| 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', 'floating-button' ); | |
| 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' . $pre_suffix . '.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' . $pre_suffix . '.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 | - 'floating-button' ) . '</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>', 'floating-button' ), $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['popupcontent'] = wp_kses_post( $_POST['param']['popupcontent'] ); | |
| 299 | - $param = serialize( $param ); | |
| 300 | - | |
| 301 | - $response = array( | |
| 302 | - 'status' => 'NO', | |
| 303 | - 'message' => esc_attr__( 'Something went wrong. Contact the plugin developer', 'floating-button' ), | |
| 304 | - ); | |
| 305 | - if ( 1 === $add ) { | |
| 306 | - | |
| 307 | - $insert = $wpdb->query( | |
| 308 | - $wpdb->prepare( " INSERT INTO {$table} ( id, title, param ) VALUES ( %d, %s, %s )", | |
| 309 | - $id, | |
| 310 | - $title, | |
| 311 | - $param | |
| 312 | - ) ); | |
| 313 | - if ( $insert ) { | |
| 314 | - $response = array( | |
| 315 | - 'status' => 'OK', | |
| 316 | - 'message' => esc_attr__( 'Item Added', 'side-menu' ), | |
| 317 | - ); | |
| 318 | - } | |
| 319 | - | |
| 320 | - } elseif ( 2 === $add ) { | |
| 321 | - $update = | |
| 322 | - $wpdb->query( | |
| 323 | - $wpdb->prepare( " UPDATE {$table} SET title = %s, param = %s WHERE id= %d;", | |
| 324 | - $title, | |
| 325 | - $param, | |
| 326 | - $id | |
| 327 | - ) ); | |
| 328 | - | |
| 329 | - if ( ! empty( $update ) ) { | |
| 330 | - $response = array( | |
| 331 | - 'status' => 'OK', | |
| 332 | - 'message' => esc_attr__( 'Item Updated', 'side-menu' ), | |
| 333 | - ); | |
| 334 | - } | |
| 335 | - } | |
| 336 | - | |
| 337 | - return $response; | |
| 338 | - } | |
| 339 | - | |
| 340 | - public function sanitize_param( $value ) { | |
| 341 | - return wp_unslash( sanitize_text_field( $value ) ); | |
| 342 | - } | |
| 343 | - | |
| 344 | - public function import_data() { | |
| 345 | - | |
| 346 | - if ( empty( $_POST[ $this->plugin['slug'] . '_import_nonce' ] ) ) { | |
| 347 | - return; | |
| 348 | - } | |
| 349 | - | |
| 350 | - | |
| 351 | - if ( ! wp_verify_nonce( $_POST[ $this->plugin['slug'] . '_import_nonce' ], $this->plugin['slug'] . '_import_nonce' ) ) { | |
| 352 | - return; | |
| 353 | - } | |
| 354 | - | |
| 355 | - | |
| 356 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 357 | - return; | |
| 358 | - } | |
| 359 | - | |
| 360 | - if ( $this->get_file_extension( $_FILES['import_file']['name'] ) != 'json' ) { | |
| 361 | - wp_die( esc_attr__( 'Please upload a valid .json file', $this->plugin['text'] ), __( 'Error', $this->plugin['text'] ), array( 'response' => 400 ) ); | |
| 362 | - } | |
| 363 | - | |
| 364 | - $import_file = $_FILES['import_file']['tmp_name']; | |
| 365 | - | |
| 366 | - if ( empty( $import_file ) ) { | |
| 367 | - wp_die( esc_attr__( 'Please upload a file to import', $this->plugin['text'] ), __( 'Error', $this->plugin['text'] ), array( 'response' => 400 ) ); | |
| 368 | - } | |
| 369 | - | |
| 370 | - // Retrieve the settings from the file and convert the json object to an array | |
| 371 | - $settings = json_decode( file_get_contents( $import_file ) ); | |
| 372 | - | |
| 373 | - $update = ! empty( $_POST['wow_import_update'] ) ? '1' : ''; | |
| 374 | - | |
| 375 | - global $wpdb; | |
| 376 | - $table = $wpdb->prefix . "wow_" . $this->plugin['prefix']; | |
| 377 | - | |
| 378 | - foreach ( $settings as $key => $val ) { | |
| 379 | - $id = $val->id; | |
| 380 | - $title = $val->title; | |
| 381 | - $param = $val->param; | |
| 382 | - | |
| 383 | - $check_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ) ); | |
| 384 | - | |
| 385 | - if ( ! empty( $update ) && ! empty( $check_row ) ) { | |
| 386 | - $update = | |
| 387 | - $wpdb->query( | |
| 388 | - $wpdb->prepare( " UPDATE {$table} SET title = %s, param = %s WHERE id= %d;", | |
| 389 | - $title, | |
| 390 | - $param, | |
| 391 | - $id | |
| 392 | - ) ); | |
| 393 | - | |
| 394 | - } elseif ( ! empty( $check_row ) ) { | |
| 395 | - $insert = $wpdb->query( | |
| 396 | - $wpdb->prepare( " INSERT INTO {$table} ( title, param ) VALUES ( %s, %s )", | |
| 397 | - $title, | |
| 398 | - $param | |
| 399 | - ) ); | |
| 400 | - } else { | |
| 401 | - $insert = $wpdb->query( | |
| 402 | - $wpdb->prepare( " INSERT INTO {$table} ( id, title, param ) VALUES ( %d, %s, %s )", | |
| 403 | - $id, | |
| 404 | - $title, | |
| 405 | - $param | |
| 406 | - ) ); | |
| 407 | - } | |
| 408 | - | |
| 409 | - } | |
| 410 | - | |
| 411 | - wp_safe_redirect( admin_url( 'admin.php?page=' . esc_attr( $this->plugin['slug'] ) ) ); | |
| 412 | - exit; | |
| 413 | - | |
| 414 | - | |
| 415 | - } | |
| 416 | - | |
| 417 | - public function export_data() { | |
| 418 | - | |
| 419 | - if ( empty( $_POST[ $this->plugin['slug'] . '_export_nonce' ] ) ) { | |
| 420 | - return; | |
| 421 | - } | |
| 422 | - | |
| 423 | - | |
| 424 | - if ( ! wp_verify_nonce( $_POST[ $this->plugin['slug'] . '_export_nonce' ], $this->plugin['slug'] . '_export_nonce' ) ) { | |
| 425 | - return; | |
| 426 | - } | |
| 427 | - | |
| 428 | - | |
| 429 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 430 | - return; | |
| 431 | - } | |
| 432 | - | |
| 433 | - $file_name = $this->plugin['shortcode'] . '-database-' . date( 'm-d-Y' ) . '.json'; | |
| 434 | - | |
| 435 | - global $wpdb; | |
| 436 | - $table = $wpdb->prefix . "wow_" . $this->plugin['prefix']; | |
| 437 | - $result = $wpdb->get_results( "SELECT * FROM " . $table . " order by id asc" ); | |
| 438 | - | |
| 439 | - $settings = array(); | |
| 440 | - if ( count( $result ) > 0 ) { | |
| 441 | - foreach ( $result as $key => $val ) { | |
| 442 | - $settings[] = array( | |
| 443 | - 'id' => $val->id, | |
| 444 | - 'title' => $val->title, | |
| 445 | - 'param' => $val->param, | |
| 446 | - ); | |
| 447 | - } | |
| 448 | - } | |
| 449 | - ignore_user_abort( true ); | |
| 450 | - nocache_headers(); | |
| 451 | - header( 'Content-Type: application/json; charset=utf-8' ); | |
| 452 | - header( 'Content-Disposition: attachment; filename=' . $file_name ); | |
| 453 | - header( "Expires: 0" ); | |
| 454 | - | |
| 455 | - echo json_encode( $settings ); | |
| 456 | - exit; | |
| 457 | - | |
| 458 | - | |
| 459 | - } | |
| 460 | - | |
| 461 | - | |
| 462 | - function get_file_extension( $str ) { | |
| 463 | - $parts = explode( '.', $str ); | |
| 464 | - | |
| 465 | - return end( $parts ); | |
| 466 | - } | |
| 467 | - | |
| 468 | -} | |
| 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 floatingbutton; | |
| 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( 'plugins_loaded', array( $this, 'plugin_check' ) ); | |
| 49 | + | |
| 50 | + add_action( 'wp_ajax_'.$this->plugin['prefix'].'_item_save', array( $this, 'item_save' ) ); | |
| 51 | + | |
| 52 | + /*if ( get_option( 'wow_' . $this->plugin['prefix'] . '_notice_action' ) != 'read' ) { | |
| 53 | + add_action( 'admin_notices', array( $this, 'general_admin_notice' ) ); | |
| 54 | + add_action( 'admin_enqueue_scripts', array( $this, 'admin_notice_assets' ) ); | |
| 55 | + add_action( 'wp_ajax_bubble_menu_notice_action', array( $this, 'wow_notice_action_callback' ) ); | |
| 56 | + }*/ | |
| 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 | + $tooltip = ''; | |
| 67 | + foreach ( $arg as $key => $value ) { | |
| 68 | + if ( $key == 'title' ) { | |
| 69 | + $tooltip .= $value . '<p/>'; | |
| 70 | + } elseif ( $key == 'ul' ) { | |
| 71 | + $tooltip .= '<ul>'; | |
| 72 | + $arr = $value; | |
| 73 | + foreach ( $arr as $val ) { | |
| 74 | + $tooltip .= '<li>' . $val . '</li>'; | |
| 75 | + } | |
| 76 | + $tooltip .= '</ul>'; | |
| 77 | + } else { | |
| 78 | + $tooltip .= $value; | |
| 79 | + } | |
| 80 | + } | |
| 81 | + $tooltip = "<span class='wow-help dashicons dashicons-editor-help' title='" . $tooltip . "'></span>"; | |
| 82 | + | |
| 83 | + return $tooltip; | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * @param array $arg parameters for creating field in the backend | |
| 88 | + * | |
| 89 | + * @return string field for displaying | |
| 90 | + */ | |
| 91 | + static function option( $arg ) { | |
| 92 | + $id = isset( $arg['id'] ) ? $arg['id'] : null; | |
| 93 | + $name = isset( $arg['name'] ) ? $arg['name'] : ''; | |
| 94 | + $type = isset( $arg['type'] ) ? $arg['type'] : ''; | |
| 95 | + $func = ! empty( $arg['func'] ) ? ' onchange="' . $arg['func'] . '"' : ''; | |
| 96 | + $options = isset( $arg['option'] ) ? $arg['option'] : ''; | |
| 97 | + $val = $arg['val']; | |
| 98 | + $separator = isset( $arg['sep'] ) ? $arg['sep'] : ''; | |
| 99 | + $class = isset( $arg['class'] ) ? ' class="' . $arg['class'] . '"' : ''; | |
| 100 | + $field = ''; | |
| 101 | + | |
| 102 | + if ( $type == 'radio' ) { | |
| 103 | + // create radio fields | |
| 104 | + $option = ''; | |
| 105 | + foreach ( $options as $key => $value ) { | |
| 106 | + $select = ( $key == $val ) ? 'checked="checked"' : ''; | |
| 107 | + $option .= '<input name="' . $name . '" type="radio" value="' . $key . '" id="' . $id . '_' . $key | |
| 108 | + . '" ' . $select . $func . $class . '><label for="' . $id . '_' . $key . '"> ' . $value | |
| 109 | + . '</label>' . $separator; | |
| 110 | + } | |
| 111 | + $field = $option; | |
| 112 | + } elseif ( $type == 'checkbox' ) { | |
| 113 | + // create checkbox field | |
| 114 | + $select = ! empty( $val ) ? 'checked="checked"' : ''; | |
| 115 | + $field = '<input type="checkbox" ' . $select . $func . $class . ' id="' . $id . '" >' . $separator; | |
| 116 | + $field .= '<input type="hidden" name="' . $name . '" value="">'; | |
| 117 | + } elseif ( $type == 'text' || $type == 'number' || $type == 'hidden' ) { | |
| 118 | + // create text field | |
| 119 | + $option = ''; | |
| 120 | + if ( is_array( $options ) ) { | |
| 121 | + foreach ( $options as $key => $value ) { | |
| 122 | + $option .= ' ' . $key . '="' . $value . '"'; | |
| 123 | + } | |
| 124 | + } | |
| 125 | + $field | |
| 126 | + = '<input name="' . $name . '" type="' . $type . '" value="' . $val . '" id="' . $id . '"' . $func | |
| 127 | + . $option . $class . '>' . $separator; | |
| 128 | + } elseif ( $type == 'color' ) { | |
| 129 | + // create color field | |
| 130 | + $field = '<input name="' . $name . '" type="text" value="' . $val | |
| 131 | + . '" class="wp-color-picker-field" data-alpha="true" id="' . $id . '">' . $separator; | |
| 132 | + } // create select field | |
| 133 | + elseif ( $type == 'select' ) { | |
| 134 | + $disabled = isset( $arg['disabled'] ) ? ' disabled' : ''; | |
| 135 | + $readonly = isset( $arg['readonly'] ) ? ' readonly' : ''; | |
| 136 | + $option = ''; | |
| 137 | + foreach ( $options as $key => $value ) { | |
| 138 | + if ( strrpos( $key, '_start' ) != false ) { | |
| 139 | + $option .= '<optgroup label="' . $value . '">'; | |
| 140 | + } elseif ( strrpos( $key, '_end' ) != false ) { | |
| 141 | + $option .= '</optgroup>'; | |
| 142 | + } else { | |
| 143 | + $select = ( $key == $val ) ? 'selected="selected"' : ''; | |
| 144 | + $option .= '<option value="' . $key . '" ' . $select . '>' . $value . '</option>'; | |
| 145 | + } | |
| 146 | + } | |
| 147 | + $field = '<select name="' . $name . '"' . $func . $class . $disabled . $readonly . ' id="' . $id . '">'; | |
| 148 | + $field .= $option; | |
| 149 | + $field .= '</select>' . $separator; | |
| 150 | + } elseif ( $type == 'editor' ) { | |
| 151 | + // create editor field | |
| 152 | + $settings = array( | |
| 153 | + 'wpautop' => 0, | |
| 154 | + 'textarea_name' => '' . $name . '', | |
| 155 | + 'textarea_rows' => 15, | |
| 156 | + ); | |
| 157 | + $field = wp_editor( $val, $id, $settings ); | |
| 158 | + } elseif ( $type == 'textarea' ) { | |
| 159 | + // create textarea field | |
| 160 | + $field = '<textarea name="' . $name . '" id="' . $id . '">' . $val . '</textarea>' . $separator; | |
| 161 | + } | |
| 162 | + | |
| 163 | + return $field; | |
| 164 | + } | |
| 165 | + | |
| 166 | + /** | |
| 167 | + * @param string $tooltip tooltip for element | |
| 168 | + * | |
| 169 | + * @return string | |
| 170 | + */ | |
| 171 | + public function pro( $tooltip = null ) { | |
| 172 | + $link = admin_url() . 'admin.php?page=' . $this->plugin['slug'] . '&tab=extension'; | |
| 173 | + $title = esc_attr__( 'More features in the PRO version', $this->plugin['text'] ); | |
| 174 | + $classes = 'wow-help dashicons dashicons-lock'; | |
| 175 | + $tooltip = ! empty( $tooltip ) ? $title . '<br/>' . $tooltip : $title; | |
| 176 | + $pro = '<a href="' . $link . '" class="' . $classes . '" title="' . $tooltip . '"></a>'; | |
| 177 | + | |
| 178 | + return $pro; | |
| 179 | + } | |
| 180 | + | |
| 181 | + /** | |
| 182 | + * Add the plugin page in admin menu | |
| 183 | + * | |
| 184 | + * @since 1.0 | |
| 185 | + */ | |
| 186 | + public function add_admin_page() { | |
| 187 | + $parent = 'wow-company'; | |
| 188 | + $title = $this->plugin['name'] . ' version ' . $this->plugin['version']; | |
| 189 | + $menu_title = $this->plugin['menu']; | |
| 190 | + $capability = 'manage_options'; | |
| 191 | + $slug = $this->plugin['slug']; | |
| 192 | + $function = array( $this, 'plugin_page' ); | |
| 193 | + add_submenu_page( $parent, $title, $menu_title, $capability, $slug, $function ); | |
| 194 | + } | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * Include main plugin page with Style and Script | |
| 198 | + * | |
| 199 | + * @since 1.0 | |
| 200 | + */ | |
| 201 | + public function plugin_page() { | |
| 202 | + global $wow_plugin_page; | |
| 203 | + $wow_plugin_page = $this->plugin['slug']; | |
| 204 | + require_once 'partials/main.php'; | |
| 205 | + self:: admin_style_script(); | |
| 206 | + } | |
| 207 | + | |
| 208 | + /** | |
| 209 | + * Include Styles and Scripts on the plugin admin page | |
| 210 | + * | |
| 211 | + * @since 1.0 | |
| 212 | + */ | |
| 213 | + public function admin_style_script() { | |
| 214 | + $slug = $this->plugin['slug']; | |
| 215 | + $version = $this->plugin['version']; | |
| 216 | + $url_assets = $this->plugin['url'] . 'assets/'; | |
| 217 | + | |
| 218 | + // include the main style | |
| 219 | + wp_enqueue_style( $slug . '-admin', $url_assets . 'css/admin-style.min.css', false, $version ); | |
| 220 | + | |
| 221 | + // include fontAwesome icon | |
| 222 | + $url_fontawesome = $url_assets . 'vendors/fontawesome/css/fontawesome-all.min.css'; | |
| 223 | + wp_enqueue_style( $slug . '-fontawesome', $url_fontawesome, null, '5.6.3' ); | |
| 224 | + | |
| 225 | + // include fonticonpicker styles & scripts | |
| 226 | + $fonticonpicker_js = $url_assets . 'vendors/fonticonpicker/fonticonpicker.min.js'; | |
| 227 | + wp_enqueue_script( $slug . '-fonticonpicker', $fonticonpicker_js, array( 'jquery' ) ); | |
| 228 | + | |
| 229 | + $fonticonpicker_css = $url_assets . 'vendors/fonticonpicker/css/fonticonpicker.min.css'; | |
| 230 | + wp_enqueue_style( $slug . '-fonticonpicker', $fonticonpicker_css ); | |
| 231 | + | |
| 232 | + $fonticonpicker_dark_css = $url_assets . 'vendors/fonticonpicker/fonticonpicker.darkgrey.min.css'; | |
| 233 | + wp_enqueue_style( $slug . '-fonticonpicker-darkgrey', $fonticonpicker_dark_css ); | |
| 234 | + | |
| 235 | + // include the color picker | |
| 236 | + wp_enqueue_style( 'wp-color-picker' ); | |
| 237 | + wp_enqueue_script( 'wp-color-picker' ); | |
| 238 | + | |
| 239 | + // include tooltip script | |
| 240 | + wp_enqueue_script( 'jquery-ui-tooltip' ); | |
| 241 | + | |
| 242 | + // include sortable | |
| 243 | + //wp_enqueue_script( 'jquery-ui-sortable' ); | |
| 244 | + | |
| 245 | + // include an alpha rgba color picker | |
| 246 | + $url_alpha = $url_assets . 'js/wp-color-picker-alpha.min.js'; | |
| 247 | + // wp_enqueue_script( 'wp-color-picker-alpha', $url_alpha, array( 'wp-color-picker' ) ); | |
| 248 | + | |
| 249 | + // include the plugin admin script | |
| 250 | + $url_script = $url_assets . 'js/admin-script.min.js'; | |
| 251 | + wp_enqueue_script( $slug . '-admin', $url_script, array( 'jquery' ), $version ); | |
| 252 | +// wp_localize_script( $slug . '-admin', 'btg_count', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); | |
| 253 | + | |
| 254 | + // include the plugin preview script | |
| 255 | + $url_preview = $url_assets . 'js/preview.min.js'; | |
| 256 | +// wp_enqueue_script( $slug . '-preview', $url_preview, array( 'jquery' ), $version ); | |
| 257 | + | |
| 258 | + } | |
| 259 | + | |
| 260 | + /** | |
| 261 | + * Add the link to the plugin page on Plugins page | |
| 262 | + * | |
| 263 | + * @param $actions | |
| 264 | + * @param $plugin_file - the plugin main file | |
| 265 | + * | |
| 266 | + * @return mixed | |
| 267 | + */ | |
| 268 | + public function action_links( $actions, $plugin_file ) { | |
| 269 | + if ( false === strpos( $plugin_file, plugin_basename( $this->plugin['file'] ) ) ) { | |
| 270 | + return $actions; | |
| 271 | + } | |
| 272 | + $settings_link | |
| 273 | + = '<a href="admin.php?page=' . $this->plugin['slug'] . '">' . esc_attr__( 'Settings', | |
| 274 | + $this->plugin['text'] ) . '</a>'; | |
| 275 | + array_unshift( $actions, $settings_link ); | |
| 276 | + | |
| 277 | + return $actions; | |
| 278 | + } | |
| 279 | + | |
| 280 | + /** | |
| 281 | + * Add custom text in the footer on the wow plugin page | |
| 282 | + * | |
| 283 | + * @param $footer_text - text in the footer | |
| 284 | + * | |
| 285 | + * @return string - end text in the footer | |
| 286 | + * @since 1.0 | |
| 287 | + */ | |
| 288 | + public function rate_us( $footer_text ) { | |
| 289 | + global $wow_plugin_page; | |
| 290 | + if ( $wow_plugin_page == $this->plugin['slug'] ) { | |
| 291 | + $rate_text = sprintf( __( 'Thank you for using <a href="%1$s" target="_blank">' . $this->plugin['name'] | |
| 292 | + . '</a>! Please <a href="%2$s" target="_blank">rate us on ' | |
| 293 | + . $this->rating['website'] . '</a>', $this->plugin['text'] ), $this->url['home'], | |
| 294 | + $this->rating['url'] ); | |
| 295 | + | |
| 296 | + return str_replace( '</span>', '', $footer_text ) . ' | ' . $rate_text . '</span>'; | |
| 297 | + } else { | |
| 298 | + return $footer_text; | |
| 299 | + } | |
| 300 | + } | |
| 301 | + | |
| 302 | + /* | |
| 303 | + * Check the post parameter from wow plugin | |
| 304 | + * | |
| 305 | + * @since 1.0 | |
| 306 | + */ | |
| 307 | + public function plugin_check() { | |
| 308 | + if ( isset( $_POST[ $this->plugin['slug'] . '_nonce' ] ) ) { | |
| 309 | + if ( ! empty( $_POST ) | |
| 310 | + && wp_verify_nonce( $_POST[ $this->plugin['slug'] . '_nonce' ], $this->plugin['slug'] . '_action' ) | |
| 311 | + && current_user_can( 'manage_options' ) | |
| 312 | + ) { | |
| 313 | + self:: save_data(); | |
| 314 | + } | |
| 315 | + } | |
| 316 | + } | |
| 317 | + | |
| 318 | + /** | |
| 319 | + * Save and Update the Item into the plugin Database | |
| 320 | + * | |
| 321 | + * @return array response from DB | |
| 322 | + * | |
| 323 | + * @since 1.0 | |
| 324 | + */ | |
| 325 | + public function save_data() { | |
| 326 | + $save_class = __NAMESPACE__ . '\\Wow_DB_Update'; | |
| 327 | + $objItem = new $save_class( $this->plugin['dir'], $this->plugin['slug'] ); | |
| 328 | + $add = ( isset( $_REQUEST['add'] ) ) ? absint( $_REQUEST['add'] ) : ''; | |
| 329 | + $table = ( isset( $_REQUEST['data'] ) ) ? sanitize_text_field( $_REQUEST['data'] ) : ''; | |
| 330 | + $tool_id = absint( $_POST['tool_id'] ); | |
| 331 | + $info = array(); | |
| 332 | + $info['id'] = $tool_id; | |
| 333 | + $info['title'] = sanitize_text_field( $_POST['title'] ); | |
| 334 | + $info['param'] = $_POST['param']; | |
| 335 | + $response = ''; | |
| 336 | + if ( '1' == $add ) { | |
| 337 | + $objItem->create_item( $table, $info ); | |
| 338 | + $response = array( | |
| 339 | + 'status' => 'OK', | |
| 340 | + 'message' => esc_attr__( 'Item Added', $this->plugin['text'] ), | |
| 341 | + ); | |
| 342 | + } elseif ( '2' == $add ) { | |
| 343 | + | |
| 344 | + $objItem->update_item( $table, $info ); | |
| 345 | + $response = array( | |
| 346 | + 'status' => 'OK', | |
| 347 | + 'message' => esc_attr__( 'Item Updated', $this->plugin['text'] ), | |
| 348 | + ); | |
| 349 | + } | |
| 350 | + | |
| 351 | + return $response; | |
| 352 | + } | |
| 353 | + | |
| 354 | + function item_save() { | |
| 355 | + | |
| 356 | + $response = 'No'; | |
| 357 | + if ( isset( $_POST[ $this->plugin['slug'] . '_nonce' ] ) ) { | |
| 358 | + if ( ! empty( $_POST ) | |
| 359 | + && wp_verify_nonce( $_POST[ $this->plugin['slug'] . '_nonce' ], $this->plugin['slug'] . '_action' ) | |
| 360 | + && current_user_can( 'manage_options' ) | |
| 361 | + ) { | |
| 362 | + $response = self:: save_data(); | |
| 363 | + } | |
| 364 | + } | |
| 365 | + | |
| 366 | + wp_send_json( $response ); | |
| 367 | + | |
| 368 | + wp_die(); | |
| 369 | + | |
| 370 | + } | |
| 371 | + | |
| 372 | + /** | |
| 373 | + * Delete the files from plugin folder in 'uploads' | |
| 374 | + * | |
| 375 | + * @param integer $id id of the item | |
| 376 | + * | |
| 377 | + * @since 1.0 | |
| 378 | + */ | |
| 379 | + public function delete_file( $id = null ) { | |
| 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 | + echo '<div class="notice notice-warning wow-plugin-notice is-dismissible"> | |
| 400 | + <p><b>' . $this->plugin['name'] | |
| 401 | + . '</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="' | |
| 402 | + . admin_url() . 'admin.php?page=' . $this->plugin['slug'] . '">Go to the ' . $this->plugin['name'] . ' page</a></p> | |
| 403 | + </div>'; | |
| 404 | + | |
| 405 | + } | |
| 406 | + | |
| 407 | + function admin_notice_assets() { | |
| 408 | + wp_enqueue_script( 'wow-notice-update', $this->plugin['url'] . 'assets/js/notice-update.min.js', | |
| 409 | + array( 'jquery' ), '1.0', true ); | |
| 410 | + } | |
| 411 | + | |
| 412 | + function wow_notice_action_callback() { | |
| 413 | + update_option( 'wow_' . $this->plugin['prefix'] . '_notice_action', 'read' ); | |
| 414 | + wp_die(); | |
| 415 | + } | |
| 416 | + | |
| 417 | +} | |