| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Admin Class |
| 5 |
* |
| 6 |
* @package Wow_Plugin |
| 7 |
* @subpackage Admin |
| 8 |
* @author Dmytro Lobov <d@dayes.dev> |
| 9 |
* @copyright 2020 Dayes |
| 10 |
* @license GNU Public License |
| 11 |
* @version 1.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace counter_box; |
| 15 |
|
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Class WP_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 WP_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 ); // add settings link |
| 46 |
add_filter( 'admin_footer_text', array( $this, 'footer_text' ) ); // add footer information |
| 47 |
add_action( 'admin_menu', array( $this, 'add_admin_page' ) ); // add admin page |
| 48 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) ); // add admin script |
| 49 |
add_action( 'wp_ajax_wow_plugin_message', array( $this, 'ad_message_deactivate' ) ); // deactivate Ad message |
| 50 |
add_action( 'wp_ajax_' . $this->plugin['prefix'] . '_item_save', array( $this, 'item_save' ) ); // save element |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Add the link to the plugin page on Plugins page |
| 57 |
* |
| 58 |
* @param $actions |
| 59 |
* @param $plugin_file - the plugin main file |
| 60 |
* |
| 61 |
* @return mixed |
| 62 |
*/ |
| 63 |
public function settings_link( $actions, $plugin_file ) { |
| 64 |
if ( false === strpos( $plugin_file, plugin_basename( $this->plugin['file'] ) ) ) { |
| 65 |
return $actions; |
| 66 |
} |
| 67 |
$link = admin_url( 'admin.php?page=' . $this->plugin['slug'] ); |
| 68 |
$settings_link = '<a href="' . esc_url( $link ) . '">' . esc_attr__( 'Settings', 'counter_box' ) . '</a>'; |
| 69 |
array_unshift( $actions, $settings_link ); |
| 70 |
|
| 71 |
return $actions; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Add custom text in the footer on the wow plugin page |
| 76 |
* |
| 77 |
* @param $footer_text - text in the footer |
| 78 |
* |
| 79 |
* @return string - end text in the footer |
| 80 |
* @since 1.0 |
| 81 |
*/ |
| 82 |
public function footer_text( $footer_text ) { |
| 83 |
global $wow_plugin_page; |
| 84 |
if ( $wow_plugin_page == $this->plugin['slug'] ) { |
| 85 |
$text = sprintf( |
| 86 |
__( '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>', 'counter_box' ), |
| 87 |
esc_url( $this->url['home'] ), |
| 88 |
esc_attr( $this->plugin['name'] ), |
| 89 |
esc_url( $this->rating['url'] ), |
| 90 |
esc_attr( $this->rating['website'] ) |
| 91 |
); |
| 92 |
|
| 93 |
return str_replace( '</span>', '', $footer_text ) . ' | ' . $text . '</span>'; |
| 94 |
} else { |
| 95 |
return $footer_text; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Add the plugin page in admin menu |
| 101 |
* |
| 102 |
* @since 1.0 |
| 103 |
*/ |
| 104 |
public function add_admin_page() { |
| 105 |
$title = $this->plugin['name'] . ' version ' . $this->plugin['version']; |
| 106 |
$menu_title = $this->plugin['menu']; |
| 107 |
$capability = 'manage_options'; |
| 108 |
$slug = $this->plugin['slug']; |
| 109 |
$icon = 'dashicons-clock'; |
| 110 |
add_menu_page( $title, $menu_title, $capability, $slug, array( $this, 'plugin_page' ), $icon, 90 ); |
| 111 |
add_submenu_page( $slug, 'All Counters', 'All Counters', $capability, $slug ); |
| 112 |
|
| 113 |
$subpages_arr = array( |
| 114 |
'settings' => array( 'Add New Counter', 'Add New' ), |
| 115 |
'support' => array( 'Counter Box Support', 'Support' ), |
| 116 |
'extension' => array( 'Counter Box Extension', 'Extension' ), |
| 117 |
); |
| 118 |
|
| 119 |
$subpages = apply_filters( $this->plugin['slug'] . '_sub_menu', $subpages_arr ); |
| 120 |
|
| 121 |
foreach ( $subpages as $key => $val ) { |
| 122 |
$sub_slug = $slug . '&tab=' . $key; |
| 123 |
$url = admin_url( 'admin.php?page=' . $sub_slug ); |
| 124 |
add_submenu_page( $slug, $val[0], $val[1], $capability, $url ); |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Include main plugin page |
| 131 |
* |
| 132 |
* @since 1.0 |
| 133 |
*/ |
| 134 |
public function plugin_page() { |
| 135 |
global $wow_plugin_page; |
| 136 |
$wow_plugin_page = $this->plugin['slug']; |
| 137 |
require_once 'page-main.php'; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/** |
| 142 |
* Include admin style and scripts on the plugin page |
| 143 |
* |
| 144 |
* @since 1.0 |
| 145 |
*/ |
| 146 |
public function admin_scripts( $hook ) { |
| 147 |
$page = 'toplevel_page_' . $this->plugin['slug']; |
| 148 |
|
| 149 |
if ( $page != $hook ) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
$slug = $this->plugin['slug']; |
| 154 |
$version = $this->plugin['version']; |
| 155 |
$url_assets = plugin_dir_url( __FILE__ ) . 'assets/'; |
| 156 |
$pre_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 157 |
|
| 158 |
wp_enqueue_style( $slug . '-admin', $url_assets . 'css/main' . $pre_suffix . '.css', false, $version ); |
| 159 |
wp_enqueue_style( $slug . '-custom', $url_assets . 'css/custom' . $pre_suffix . '.css', false, $version ); |
| 160 |
wp_enqueue_style( $slug . '-preview', $url_assets . 'css/preview' . $pre_suffix . '.css', false, $version ); |
| 161 |
|
| 162 |
$url_navigation = $url_assets . 'js/navigation' . $pre_suffix . '.js'; |
| 163 |
wp_enqueue_script( $slug . '-navigation', $url_navigation, array( 'jquery' ), $version, true ); |
| 164 |
|
| 165 |
if ( isset( $_GET['tab'] ) && $_GET['tab'] !== 'settings' ) { |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
// include the plugin admin script |
| 170 |
$url_script = $url_assets . 'js/script' . $pre_suffix . '.js'; |
| 171 |
wp_enqueue_script( $slug . '-admin', $url_script, array( 'jquery' ), $version, true ); |
| 172 |
|
| 173 |
$url_preview = plugin_dir_url( __FILE__ ) . 'assets/js/preview' . $pre_suffix . '.js'; |
| 174 |
wp_enqueue_script( $slug . '-preview', $url_preview, array( 'jquery' ), $version, true ); |
| 175 |
|
| 176 |
// include the color picker |
| 177 |
wp_enqueue_style( 'wp-color-picker' ); |
| 178 |
wp_enqueue_script( 'wp-color-picker' ); |
| 179 |
|
| 180 |
// include an alpha rgba color picker |
| 181 |
$url_alpha = $url_assets . 'js/wp-color-picker-alpha.min.js'; |
| 182 |
wp_enqueue_script( 'wp-color-picker-alpha', $url_alpha, array( 'wp-color-picker' ) ); |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Deactivate the Ad message on plugin page |
| 188 |
* |
| 189 |
* @since 1.0 |
| 190 |
*/ |
| 191 |
public function ad_message_deactivate() { |
| 192 |
update_option( 'wow_' . $this->plugin['prefix'] . '_message', 'read' ); |
| 193 |
wp_die(); |
| 194 |
} |
| 195 |
|
| 196 |
static function input( $arg ) { |
| 197 |
include( 'fields/input.php' ); |
| 198 |
} |
| 199 |
|
| 200 |
static function number( $arg ) { |
| 201 |
include( 'fields/number.php' ); |
| 202 |
} |
| 203 |
|
| 204 |
static function select( $arg ) { |
| 205 |
include( 'fields/select.php' ); |
| 206 |
} |
| 207 |
|
| 208 |
static function editor( $arg ) { |
| 209 |
include( 'fields/editor.php' ); |
| 210 |
} |
| 211 |
|
| 212 |
static function textarea( $arg ) { |
| 213 |
include( 'fields/textarea.php' ); |
| 214 |
} |
| 215 |
|
| 216 |
static function color( $arg ) { |
| 217 |
include( 'fields/color.php' ); |
| 218 |
} |
| 219 |
|
| 220 |
static function checkbox( $arg ) { |
| 221 |
include( 'fields/checkbox.php' ); |
| 222 |
} |
| 223 |
|
| 224 |
static function time( $arg ) { |
| 225 |
include( 'fields/time.php' ); |
| 226 |
} |
| 227 |
|
| 228 |
static function date( $arg ) { |
| 229 |
include( 'fields/date.php' ); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Save and Update the Item into the plugin Database |
| 234 |
* |
| 235 |
* @return array response from DB |
| 236 |
* |
| 237 |
* @since 1.0 |
| 238 |
*/ |
| 239 |
public function save_data() { |
| 240 |
global $wpdb; |
| 241 |
|
| 242 |
$add = ( isset( $_REQUEST['add'] ) ) ? absint( $_REQUEST['add'] ) : ''; |
| 243 |
$table = ( isset( $_REQUEST['data'] ) ) ? sanitize_text_field( $_REQUEST['data'] ) : ''; |
| 244 |
$param = array_map( 'wp_kses_post', wp_unslash( $_POST['param'] ) ); |
| 245 |
$id = absint( $_POST['tool_id'] ); |
| 246 |
|
| 247 |
$data = array( |
| 248 |
'id' => $id, |
| 249 |
'title' => wp_unslash( sanitize_text_field( $_POST['title'] ) ), |
| 250 |
'param' => serialize( $param ), |
| 251 |
'status' => absint($_POST['status']), |
| 252 |
); |
| 253 |
|
| 254 |
if ( $add === 1 ) { |
| 255 |
$wpdb->insert( $table, $data ); |
| 256 |
} elseif ( $add === 2 ) { |
| 257 |
$wpdb->update( $table, $data, array( 'id' => $id ), $format = null, $where_format = null ); |
| 258 |
} |
| 259 |
|
| 260 |
$response = array( |
| 261 |
'status' => 'OK', |
| 262 |
'message' => esc_attr__( 'Item Updated', 'counter_box' ), |
| 263 |
); |
| 264 |
|
| 265 |
return $response; |
| 266 |
} |
| 267 |
|
| 268 |
public function item_save() { |
| 269 |
$response = 'No'; |
| 270 |
if ( isset( $_POST[ $this->plugin['slug'] . '_nonce' ] ) ) { |
| 271 |
if ( ! empty( $_POST ) |
| 272 |
&& wp_verify_nonce( $_POST[ $this->plugin['slug'] . '_nonce' ], $this->plugin['slug'] . '_action' ) |
| 273 |
&& current_user_can( 'manage_options' ) |
| 274 |
) { |
| 275 |
$response = self:: save_data(); |
| 276 |
} |
| 277 |
} |
| 278 |
wp_send_json( $response ); |
| 279 |
wp_die(); |
| 280 |
} |
| 281 |
|
| 282 |
} |
| 283 |
|