| 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 bubble_menu_lite; |
| 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 |
} |
| 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 |
echo "<span class='wow-help dashicons dashicons-editor-help' title='" . wp_kses_post( $tooltip ) . "'></span>"; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param array $arg parameters for creating field in the backend |
| 84 |
* |
| 85 |
* @return string field for displaying |
| 86 |
*/ |
| 87 |
static function option( $arg ) { |
| 88 |
$id = isset( $arg['id'] ) ? $arg['id'] : null; |
| 89 |
$name = isset( $arg['name'] ) ? $arg['name'] : ''; |
| 90 |
$type = isset( $arg['type'] ) ? $arg['type'] : ''; |
| 91 |
$func = ! empty( $arg['func'] ) ? ' onchange="' . $arg['func'] . '"' : ''; |
| 92 |
$options = isset( $arg['option'] ) ? $arg['option'] : ''; |
| 93 |
$val = $arg['val']; |
| 94 |
$separator = isset( $arg['sep'] ) ? $arg['sep'] : ''; |
| 95 |
$extra_class = ''; |
| 96 |
if ( $type == 'text' || $type == 'number' || $type == 'time' ) { |
| 97 |
$extra_class = 'input '; |
| 98 |
} |
| 99 |
$class = isset( $arg['class'] ) ? ' class="' . $extra_class . $arg['class'] . '"' : ' class="' . $extra_class . '"'; |
| 100 |
$field = ''; |
| 101 |
|
| 102 |
switch ( $type ) { |
| 103 |
case 'radio': |
| 104 |
include( 'fields/radio.php' ); |
| 105 |
break; |
| 106 |
case 'checkbox': |
| 107 |
include( 'fields/checkbox.php' ); |
| 108 |
break; |
| 109 |
case 'text': |
| 110 |
case 'number': |
| 111 |
case 'hidden': |
| 112 |
case 'time': |
| 113 |
include( 'fields/input.php' ); |
| 114 |
break; |
| 115 |
case 'select': |
| 116 |
include( 'fields/select.php' ); |
| 117 |
break; |
| 118 |
case 'color': |
| 119 |
include( 'fields/color.php' ); |
| 120 |
break; |
| 121 |
case 'editor': |
| 122 |
include( 'fields/editor.php' ); |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* @param string $tooltip tooltip for element |
| 129 |
* |
| 130 |
* @return string |
| 131 |
*/ |
| 132 |
public function pro( $tooltip = null ) { |
| 133 |
$link = admin_url() . 'admin.php?page=' . $this->plugin['slug'] . '&tab=extension'; |
| 134 |
$title = esc_attr__( 'More features in the PRO version', 'bubble-menu' ); |
| 135 |
$classes = 'wow-help dashicons dashicons-lock'; |
| 136 |
$tooltip = ! empty( $tooltip ) ? $title . '<br/>' . $tooltip : $title; |
| 137 |
echo '<a href="' . esc_url( $link ) . '" class="' . esc_attr( $classes ) . '" title="' . wp_kses_post( $tooltip ) . '"></a>'; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Add the plugin page in admin menu |
| 142 |
* |
| 143 |
* @since 1.0 |
| 144 |
*/ |
| 145 |
public function add_admin_page() { |
| 146 |
$parent = 'wow-company'; |
| 147 |
$title = $this->plugin['name'] . ' version ' . $this->plugin['version']; |
| 148 |
$menu_title = $this->plugin['menu']; |
| 149 |
$capability = 'manage_options'; |
| 150 |
$slug = $this->plugin['slug']; |
| 151 |
$function = array( $this, 'plugin_page' ); |
| 152 |
add_submenu_page( $parent, $title, $menu_title, $capability, $slug, $function ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Include main plugin page with Style and Script |
| 157 |
* |
| 158 |
* @since 1.0 |
| 159 |
*/ |
| 160 |
public function plugin_page() { |
| 161 |
global $wow_plugin_page; |
| 162 |
$wow_plugin_page = $this->plugin['slug']; |
| 163 |
require_once 'page-main.php'; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* Include Styles and Scripts on the plugin admin page |
| 169 |
* |
| 170 |
* @since 1.0 |
| 171 |
*/ |
| 172 |
public function admin_scripts( $hook ) { |
| 173 |
$page = 'wow-plugins_page_' . $this->plugin['slug']; |
| 174 |
|
| 175 |
if ( $page != $hook ) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
$slug = $this->plugin['slug']; |
| 179 |
$version = $this->plugin['version']; |
| 180 |
$url_assets = plugin_dir_url( __FILE__ ) . 'assets/'; |
| 181 |
|
| 182 |
$pre_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 183 |
|
| 184 |
// include the main style |
| 185 |
wp_enqueue_style( $slug . '-admin', $url_assets . 'css/style.css', false, $version ); |
| 186 |
|
| 187 |
// include fontAwesome icon |
| 188 |
$url_fontawesome = $this->plugin['url'] . 'vendors/fontawesome/css/fontawesome-all.min.css'; |
| 189 |
wp_enqueue_style( $slug . '-fontawesome', $url_fontawesome, null, '6.4.2' ); |
| 190 |
|
| 191 |
// include fonticonpicker styles & scripts |
| 192 |
$fonticonpicker_js = $url_assets . 'fonticonpicker/fonticonpicker.min.js'; |
| 193 |
wp_enqueue_script( $slug . '-fonticonpicker', $fonticonpicker_js, array( 'jquery' ) ); |
| 194 |
|
| 195 |
$fonticonpicker_css = $url_assets . 'fonticonpicker/css/fonticonpicker.min.css'; |
| 196 |
wp_enqueue_style( $slug . '-fonticonpicker', $fonticonpicker_css ); |
| 197 |
|
| 198 |
$fonticonpicker_dark_css = $url_assets . 'fonticonpicker/fonticonpicker.darkgrey.min.css'; |
| 199 |
wp_enqueue_style( $slug . '-fonticonpicker-darkgrey', $fonticonpicker_dark_css ); |
| 200 |
|
| 201 |
// include the color picker |
| 202 |
wp_enqueue_style( 'wp-color-picker' ); |
| 203 |
wp_enqueue_script( 'wp-color-picker' ); |
| 204 |
|
| 205 |
$url_alpha = $url_assets . 'js/wp-color-picker-alpha.min.js'; |
| 206 |
wp_enqueue_script( 'wp-color-picker-alpha', $url_alpha, array( 'wp-color-picker' ) ); |
| 207 |
|
| 208 |
// include tooltip script |
| 209 |
wp_enqueue_script( 'jquery-ui-tooltip' ); |
| 210 |
|
| 211 |
// include sortable |
| 212 |
wp_enqueue_script( 'jquery-ui-sortable' ); |
| 213 |
|
| 214 |
|
| 215 |
// include the plugin admin script |
| 216 |
$url_script = $url_assets . 'js/script' . $pre_suffix . '.js'; |
| 217 |
wp_enqueue_script( $slug . '-admin', $url_script, array( 'jquery' ), $version, true ); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Add the link to the plugin page on Plugins page |
| 222 |
* |
| 223 |
* @param $actions |
| 224 |
* @param $plugin_file - the plugin main file |
| 225 |
* |
| 226 |
* @return mixed |
| 227 |
*/ |
| 228 |
public function action_links( $actions, $plugin_file ) { |
| 229 |
if ( false === strpos( $plugin_file, plugin_basename( $this->plugin['file'] ) ) ) { |
| 230 |
return $actions; |
| 231 |
} |
| 232 |
$settings_link |
| 233 |
= '<a href="admin.php?page=' . esc_attr( $this->plugin['slug'] ) . '">' . esc_attr__( 'Settings', |
| 234 |
'bubble-menu' ) . '</a>'; |
| 235 |
array_unshift( $actions, $settings_link ); |
| 236 |
|
| 237 |
return $actions; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Add custom text in the footer on the wow plugin page |
| 242 |
* |
| 243 |
* @param $footer_text - text in the footer |
| 244 |
* |
| 245 |
* @return string - end text in the footer |
| 246 |
* @since 1.0 |
| 247 |
*/ |
| 248 |
public function rate_us( $footer_text ) { |
| 249 |
global $wow_plugin_page; |
| 250 |
if ( $wow_plugin_page == $this->plugin['slug'] ) { |
| 251 |
$rate_text = sprintf( __( 'Thank you for using <a href="%1$s" target="_blank">' . esc_attr( $this->plugin['name'] ) |
| 252 |
. '</a>! Please <a href="%2$s" target="_blank">rate us on ' |
| 253 |
. esc_attr( $this->rating['website'] ) . '</a>', 'bubble-menu' ), |
| 254 |
$this->url['home'], |
| 255 |
$this->rating['url'] ); |
| 256 |
|
| 257 |
return str_replace( '</span>', '', $footer_text ) . ' | ' . $rate_text . '</span>'; |
| 258 |
} else { |
| 259 |
return $footer_text; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
function item_save() { |
| 264 |
$response = 'No'; |
| 265 |
if ( isset( $_POST[ $this->plugin['slug'] . '_nonce' ] ) ) { |
| 266 |
if ( ! empty( $_POST ) |
| 267 |
&& wp_verify_nonce( $_POST[ $this->plugin['slug'] . '_nonce' ], $this->plugin['slug'] . '_action' ) |
| 268 |
&& current_user_can( 'manage_options' ) |
| 269 |
) { |
| 270 |
$response = self:: save_data(); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
wp_send_json( $response ); |
| 275 |
|
| 276 |
wp_die(); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Save and Update the Item into the plugin Database |
| 281 |
* |
| 282 |
* @return array response from DB |
| 283 |
* |
| 284 |
* @since 1.0 |
| 285 |
*/ |
| 286 |
public function save_data() { |
| 287 |
global $wpdb; |
| 288 |
$add = ( isset( $_REQUEST['add'] ) ) ? absint( $_REQUEST['add'] ) : ''; |
| 289 |
$table = $wpdb->prefix . 'wow_' . $this->plugin['prefix']; |
| 290 |
$id = absint( $_POST['tool_id'] ); |
| 291 |
$title = sanitize_text_field( $_POST['title'] ); |
| 292 |
$param = map_deep( $_POST['param'], array( $this, 'sanitize_param' ) ); |
| 293 |
$param['popupcontent'] = wp_kses_post( $_POST['param']['popupcontent'] ); |
| 294 |
$param['item_link'] = map_deep( $_POST['param']['item_link'], 'sanitize_url' ); |
| 295 |
$param = serialize( $param ); |
| 296 |
|
| 297 |
|
| 298 |
$response = array( |
| 299 |
'status' => 'NO', |
| 300 |
'message' => esc_attr__( 'Something went wrong. Contact the plugin developer', 'bubble-menu' ), |
| 301 |
); |
| 302 |
if ( 1 === $add ) { |
| 303 |
$insert = $wpdb->query( |
| 304 |
$wpdb->prepare( " INSERT INTO {$table} ( id, title, param ) VALUES ( %d, %s, %s )", |
| 305 |
$id, |
| 306 |
$title, |
| 307 |
$param |
| 308 |
) ); |
| 309 |
if ( $insert ) { |
| 310 |
$response = array( |
| 311 |
'status' => 'OK', |
| 312 |
'message' => esc_attr__( 'Item Added', 'bubble-menu' ), |
| 313 |
); |
| 314 |
} |
| 315 |
} elseif ( 2 === $add ) { |
| 316 |
$update = |
| 317 |
$wpdb->query( |
| 318 |
$wpdb->prepare( " UPDATE {$table} SET title = %s, param = %s WHERE id= %d;", |
| 319 |
$title, |
| 320 |
$param, |
| 321 |
$id |
| 322 |
) ); |
| 323 |
|
| 324 |
if ( ! empty( $update ) ) { |
| 325 |
$response = array( |
| 326 |
'status' => 'OK', |
| 327 |
'message' => esc_attr__( 'Item Updated', 'bubble-menu' ), |
| 328 |
); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
return $response; |
| 333 |
} |
| 334 |
|
| 335 |
public function sanitize_param( $value ) { |
| 336 |
return wp_unslash( sanitize_text_field( $value ) ); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
} |
| 341 |
|