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