| 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 modal_window; |
| 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, 'settings_link' ), 10, 2 ); |
| 46 |
add_filter( 'admin_footer_text', array( $this, 'footer_text' ) ); |
| 47 |
add_action( 'admin_menu', array( $this, 'add_admin_page' ) ); |
| 48 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) ); |
| 49 |
add_action( 'wp_ajax_' . $this->plugin['prefix'] . '_message', array( $this, 'ad_message_deactivate' ) ); |
| 50 |
add_action( 'wp_ajax_' . $this->plugin['prefix'] . '_item_save', array( $this, 'item_save' ) ); |
| 51 |
add_action( 'media_buttons', array( $this, 'shortcode_button' ) ); // add media button to Editor |
| 52 |
add_action( 'admin_init', [ $this, 'export_data' ] ); |
| 53 |
add_action( 'admin_init', [ $this, 'import_data' ] ); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Add the link to the plugin page on Plugins page |
| 59 |
* |
| 60 |
* @param $actions |
| 61 |
* @param $plugin_file - the plugin main file |
| 62 |
* |
| 63 |
* @return mixed |
| 64 |
*/ |
| 65 |
public function settings_link( $actions, $plugin_file ) { |
| 66 |
if ( false === strpos( $plugin_file, plugin_basename( $this->plugin['file'] ) ) ) { |
| 67 |
return $actions; |
| 68 |
} |
| 69 |
$link = admin_url( 'admin.php?page=' . $this->plugin['slug'] ); |
| 70 |
$text = esc_attr__( 'Settings', 'modal-window' ); |
| 71 |
$settings_link = '<a href="' . esc_url( $link ) . '">' . esc_attr( $text ) . '</a>'; |
| 72 |
array_unshift( $actions, $settings_link ); |
| 73 |
|
| 74 |
return $actions; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Add custom text in the footer on the wow plugin page |
| 79 |
* |
| 80 |
* @param $footer_text - text in the footer |
| 81 |
* |
| 82 |
* @return string - end text in the footer |
| 83 |
* @since 1.0 |
| 84 |
*/ |
| 85 |
public function footer_text( $footer_text ) { |
| 86 |
global $wow_plugin_page; |
| 87 |
if ( $wow_plugin_page == $this->plugin['slug'] ) { |
| 88 |
$text = sprintf( |
| 89 |
__( 'Thank you for using <a href="%1$s" target="_blank">%2$s</a>! Please <a href="%3$s" target="_blank">rate us on %4$s</a>', 'modal-window' ), |
| 90 |
esc_url( $this->url['home'] ), |
| 91 |
esc_attr( $this->plugin['name'] ), |
| 92 |
esc_url( $this->rating['url'] ), |
| 93 |
esc_attr( $this->rating['website'] ) |
| 94 |
); |
| 95 |
|
| 96 |
return str_replace( '</span>', '', $footer_text ) . ' | ' . $text . '</span>'; |
| 97 |
} else { |
| 98 |
return $footer_text; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Add the plugin page in admin menu |
| 104 |
* |
| 105 |
* @since 1.0 |
| 106 |
*/ |
| 107 |
public function add_admin_page() { |
| 108 |
$parent = 'wow-company'; |
| 109 |
$title = $this->plugin['name'] . ' version ' . $this->plugin['version']; |
| 110 |
$menu_title = $this->plugin['menu']; |
| 111 |
$capability = 'manage_options'; |
| 112 |
$slug = $this->plugin['slug']; |
| 113 |
add_submenu_page( $parent, $title, $menu_title, $capability, $slug, array( $this, 'plugin_page' ) ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Include main plugin page |
| 118 |
* |
| 119 |
* @since 1.0 |
| 120 |
*/ |
| 121 |
public function plugin_page() { |
| 122 |
global $wow_plugin_page; |
| 123 |
$wow_plugin_page = $this->plugin['slug']; |
| 124 |
require_once 'page-main.php'; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Include admin style and scripts on the plugin page |
| 129 |
* |
| 130 |
* @since 1.0 |
| 131 |
*/ |
| 132 |
public function admin_scripts( $hook ) { |
| 133 |
$page = 'wow-plugins_page_' . $this->plugin['slug']; |
| 134 |
|
| 135 |
if ( $page != $hook ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
$slug = $this->plugin['slug']; |
| 140 |
$version = $this->plugin['version']; |
| 141 |
$url_assets = plugin_dir_url( __FILE__ ) . 'assets/'; |
| 142 |
$pre_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 143 |
|
| 144 |
wp_enqueue_style( $slug . '-admin', $url_assets . 'css/main' . $pre_suffix . '.css', false, $version ); |
| 145 |
wp_enqueue_style( $slug . '-custom', $url_assets . 'css/custom' . $pre_suffix . '.css', false, $version ); |
| 146 |
wp_enqueue_style( $slug . '-preview', $url_assets . 'css/preview' . $pre_suffix . '.css', false, $version ); |
| 147 |
|
| 148 |
// include the color picker |
| 149 |
wp_enqueue_style( 'wp-color-picker' ); |
| 150 |
wp_enqueue_script( 'wp-color-picker' ); |
| 151 |
|
| 152 |
// include an alpha rgba color picker |
| 153 |
$url_alpha = $url_assets . 'js/wp-color-picker-alpha' . $pre_suffix . '.js'; |
| 154 |
wp_enqueue_script( 'wp-color-picker-alpha', $url_alpha, array( 'wp-color-picker' ) ); |
| 155 |
|
| 156 |
$url_vendors = $this->plugin['url'] . 'vendors/'; |
| 157 |
// include fontAwesome icon |
| 158 |
$url_fontawesome = $url_vendors . 'fontawesome/css/fontawesome-all' . $pre_suffix . '.css'; |
| 159 |
wp_enqueue_style( $slug . '-fontawesome', $url_fontawesome, null, '5.12' ); |
| 160 |
|
| 161 |
// include fonticonpicker styles & scripts |
| 162 |
$fonticonpicker_js = $url_assets . 'fonticonpicker/fonticonpicker.min.js'; |
| 163 |
wp_enqueue_script( $slug . '-fonticonpicker', $fonticonpicker_js, array( 'jquery' ) ); |
| 164 |
|
| 165 |
$fonticonpicker_css = $url_assets . 'fonticonpicker/css/fonticonpicker.min.css'; |
| 166 |
wp_enqueue_style( $slug . '-fonticonpicker', $fonticonpicker_css ); |
| 167 |
|
| 168 |
$fonticonpicker_dark_css = $url_assets . 'fonticonpicker/fonticonpicker.darkgrey.min.css'; |
| 169 |
wp_enqueue_style( $slug . '-fonticonpicker-darkgrey', $fonticonpicker_dark_css ); |
| 170 |
|
| 171 |
// include the plugin admin script |
| 172 |
$url_script = plugin_dir_url( __FILE__ ) . 'assets/js/admin-script' . $pre_suffix . '.js'; |
| 173 |
wp_enqueue_script( $slug . '-admin', $url_script, array( 'jquery' ), $version, true ); |
| 174 |
|
| 175 |
$url_shortcodes = plugin_dir_url( __FILE__ ) . 'assets/js/shortcodes.js'; |
| 176 |
wp_enqueue_script( $slug . '-shortcodes', $url_shortcodes, array( 'jquery' ), $version, true ); |
| 177 |
|
| 178 |
$url_preview = plugin_dir_url( __FILE__ ) . 'assets/js/preview.js'; |
| 179 |
wp_enqueue_script( $slug . '-preview', $url_preview, array( 'jquery' ), $version, true ); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
add_thickbox(); |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Deactivate the Ad message on plugin page |
| 189 |
* |
| 190 |
* @since 1.0 |
| 191 |
*/ |
| 192 |
public function ad_message_deactivate() { |
| 193 |
update_option( 'wow_' . $this->plugin['prefix'] . '_message', 'read' ); |
| 194 |
wp_die(); |
| 195 |
} |
| 196 |
|
| 197 |
static function input( $arg ) { |
| 198 |
include( 'fields/input.php' ); |
| 199 |
} |
| 200 |
|
| 201 |
static function number( $arg ) { |
| 202 |
include( 'fields/number.php' ); |
| 203 |
} |
| 204 |
|
| 205 |
static function select( $arg ) { |
| 206 |
include( 'fields/select.php' ); |
| 207 |
} |
| 208 |
|
| 209 |
static function editor( $arg ) { |
| 210 |
include( 'fields/editor.php' ); |
| 211 |
} |
| 212 |
|
| 213 |
static function textarea( $arg ) { |
| 214 |
include( 'fields/textarea.php' ); |
| 215 |
} |
| 216 |
|
| 217 |
static function color( $arg ) { |
| 218 |
include( 'fields/color.php' ); |
| 219 |
} |
| 220 |
|
| 221 |
static function checkbox( $arg ) { |
| 222 |
include( 'fields/checkbox.php' ); |
| 223 |
} |
| 224 |
|
| 225 |
static function time( $arg ) { |
| 226 |
include( 'fields/time.php' ); |
| 227 |
} |
| 228 |
|
| 229 |
static function date( $arg ) { |
| 230 |
include( 'fields/date.php' ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Save and Update the Item into the plugin Database |
| 235 |
* |
| 236 |
* @return array response from DB |
| 237 |
* |
| 238 |
* @since 1.0 |
| 239 |
*/ |
| 240 |
public function save_data() { |
| 241 |
global $wpdb; |
| 242 |
|
| 243 |
$add = ( isset( $_REQUEST['add'] ) ) ? absint( $_REQUEST['add'] ) : ''; |
| 244 |
$table = ( isset( $_REQUEST['data'] ) ) ? sanitize_text_field( $_REQUEST['data'] ) : ''; |
| 245 |
$param = map_deep( $_POST['param'], array( $this, 'sanitize_param' ) ); |
| 246 |
$param['content'] = wp_kses_post(wp_unslash( wp_encode_emoji( $_POST['param']['content'] ) )); |
| 247 |
$id = absint( $_POST['tool_id'] ); |
| 248 |
|
| 249 |
$data = array( |
| 250 |
'id' => $id, |
| 251 |
'title' => wp_unslash( sanitize_text_field( $_POST['title'] ) ), |
| 252 |
'param' => serialize( $param ), |
| 253 |
'script' => $this->get_script( $id, $param ), |
| 254 |
'style' => $this->get_style( $id, $param ), |
| 255 |
'status' => absint( $_POST['status'] ), |
| 256 |
); |
| 257 |
|
| 258 |
if ( $add === 1 ) { |
| 259 |
$wpdb->insert( $table, $data ); |
| 260 |
} elseif ( $add === 2 ) { |
| 261 |
$wpdb->update( $table, $data, array( 'id' => $id ), $format = null, $where_format = null ); |
| 262 |
} |
| 263 |
|
| 264 |
$response = array( |
| 265 |
'status' => 'OK', |
| 266 |
'message' => esc_attr__( 'Item Updated', 'modal-window' ), |
| 267 |
); |
| 268 |
|
| 269 |
return $response; |
| 270 |
} |
| 271 |
|
| 272 |
public function sanitize_param( $value ) { |
| 273 |
return wp_unslash( sanitize_text_field( $value ) ); |
| 274 |
} |
| 275 |
|
| 276 |
public function item_save() { |
| 277 |
$response = 'No'; |
| 278 |
if ( isset( $_POST[ $this->plugin['slug'] . '_nonce' ] ) ) { |
| 279 |
if ( ! empty( $_POST ) |
| 280 |
&& wp_verify_nonce( $_POST[ $this->plugin['slug'] . '_nonce' ], $this->plugin['slug'] . '_action' ) |
| 281 |
&& current_user_can( 'manage_options' ) |
| 282 |
) { |
| 283 |
$response = $this->save_data(); |
| 284 |
} |
| 285 |
} |
| 286 |
wp_send_json( $response ); |
| 287 |
wp_die(); |
| 288 |
} |
| 289 |
|
| 290 |
private function get_script( $id, $param ) { |
| 291 |
$script = array(); |
| 292 |
include( 'generate/script.php' ); |
| 293 |
|
| 294 |
return wp_json_encode( $script ); |
| 295 |
} |
| 296 |
|
| 297 |
private function get_style( $id, $param ) { |
| 298 |
$css = ''; |
| 299 |
include( 'generate/style.php' ); |
| 300 |
|
| 301 |
return $css; |
| 302 |
} |
| 303 |
|
| 304 |
public function shortcode_button() { |
| 305 |
|
| 306 |
global $current_screen; |
| 307 |
|
| 308 |
if ( $current_screen->base != 'wow-plugins_page_' . $this->plugin['slug'] ) { |
| 309 |
return false; |
| 310 |
} |
| 311 |
|
| 312 |
$title = 'Popup Shortcodes'; |
| 313 |
$context = '<a href="/?TB_inline&inlineId=popupShortcode&width=600&height=550" class="thickbox button popup-shortcode" title="' . $title . '" >Shortcodes</a>'; |
| 314 |
echo $context; |
| 315 |
} |
| 316 |
|
| 317 |
public function import_data() { |
| 318 |
|
| 319 |
if ( empty( $_POST[ $this->plugin['slug'] . '_import_nonce' ] ) ) { |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
if ( ! wp_verify_nonce( $_POST[ $this->plugin['slug'] . '_import_nonce' ], $this->plugin['slug'] . '_import_nonce' ) ) { |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 330 |
return; |
| 331 |
} |
| 332 |
|
| 333 |
if ( $this->get_file_extension( $_FILES['import_file']['name'] ) != 'json' ) { |
| 334 |
wp_die( esc_attr__( 'Please upload a valid .json file', 'modal-window' ), __( 'Error', 'modal-window' ), array( 'response' => 400 ) ); |
| 335 |
} |
| 336 |
|
| 337 |
$import_file = $_FILES['import_file']['tmp_name']; |
| 338 |
|
| 339 |
if ( empty( $import_file ) ) { |
| 340 |
wp_die( esc_attr__( 'Please upload a file to import', 'modal-window' ), __( 'Error', 'modal-window' ), array( 'response' => 400 ) ); |
| 341 |
} |
| 342 |
|
| 343 |
// Retrieve the settings from the file and convert the json object to an array |
| 344 |
$settings = json_decode( file_get_contents( $import_file ) ); |
| 345 |
|
| 346 |
$update = ! empty( $_POST['wow_import_update'] ) ? '1' : ''; |
| 347 |
|
| 348 |
global $wpdb; |
| 349 |
$table = $wpdb->prefix . "wow_" . $this->plugin['prefix']; |
| 350 |
|
| 351 |
foreach ( $settings as $key => $val ) { |
| 352 |
$id = $val->id; |
| 353 |
$title = $val->title; |
| 354 |
$param = $val->param; |
| 355 |
|
| 356 |
$check_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ) ); |
| 357 |
|
| 358 |
if ( ! empty( $update ) && ! empty( $check_row ) ) { |
| 359 |
$wpdb->query( |
| 360 |
$wpdb->prepare( " UPDATE {$table} SET title = %s, param = %s WHERE id= %d;", |
| 361 |
$title, |
| 362 |
$param, |
| 363 |
$id |
| 364 |
) ); |
| 365 |
|
| 366 |
} elseif ( ! empty( $check_row ) ) { |
| 367 |
$wpdb->query( |
| 368 |
$wpdb->prepare( " INSERT INTO {$table} ( title, param ) VALUES ( %s, %s )", |
| 369 |
$title, |
| 370 |
$param |
| 371 |
) ); |
| 372 |
} else { |
| 373 |
$wpdb->query( |
| 374 |
$wpdb->prepare( " INSERT INTO {$table} ( id, title, param ) VALUES ( %d, %s, %s )", |
| 375 |
$id, |
| 376 |
$title, |
| 377 |
$param |
| 378 |
) ); |
| 379 |
} |
| 380 |
|
| 381 |
} |
| 382 |
|
| 383 |
wp_safe_redirect( admin_url( 'admin.php?page=' . esc_attr( $this->plugin['slug'] ) ) ); |
| 384 |
exit; |
| 385 |
|
| 386 |
|
| 387 |
} |
| 388 |
|
| 389 |
public function export_data() { |
| 390 |
|
| 391 |
if ( empty( $_POST[ $this->plugin['slug'] . '_export_nonce' ] ) ) { |
| 392 |
return; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
if ( ! wp_verify_nonce( $_POST[ $this->plugin['slug'] . '_export_nonce' ], $this->plugin['slug'] . '_export_nonce' ) ) { |
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 402 |
return; |
| 403 |
} |
| 404 |
|
| 405 |
$file_name = $this->plugin['shortcode'] . '-database-' . date( 'm-d-Y' ) . '.json'; |
| 406 |
|
| 407 |
global $wpdb; |
| 408 |
$table = $wpdb->prefix . "wow_" . $this->plugin['prefix']; |
| 409 |
$result = $wpdb->get_results( "SELECT * FROM " . $table . " order by id asc" ); |
| 410 |
|
| 411 |
$settings = array(); |
| 412 |
if ( count( $result ) > 0 ) { |
| 413 |
foreach ( $result as $key => $val ) { |
| 414 |
$settings[] = array( |
| 415 |
'id' => $val->id, |
| 416 |
'title' => $val->title, |
| 417 |
'param' => $val->param, |
| 418 |
'script' => $val->script, |
| 419 |
'style' => $val->style, |
| 420 |
'status' => $val->status, |
| 421 |
); |
| 422 |
} |
| 423 |
} |
| 424 |
ignore_user_abort( true ); |
| 425 |
nocache_headers(); |
| 426 |
header( 'Content-Type: application/json; charset=utf-8' ); |
| 427 |
header( 'Content-Disposition: attachment; filename=' . $file_name ); |
| 428 |
header( "Expires: 0" ); |
| 429 |
|
| 430 |
echo json_encode( $settings ); |
| 431 |
exit; |
| 432 |
|
| 433 |
|
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
function get_file_extension( $str ) { |
| 438 |
$parts = explode( '.', $str ); |
| 439 |
|
| 440 |
return end( $parts ); |
| 441 |
} |
| 442 |
|
| 443 |
} |
| 444 |
|