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