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