| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class AdminActions |
| 5 |
* |
| 6 |
* This class handles administrative actions in the plugin. |
| 7 |
* |
| 8 |
* @package WowPlugin |
| 9 |
* @subpackage Admin |
| 10 |
* @author Dmytro Lobov <dev@wow-company.com>, Wow-Company |
| 11 |
* @copyright 2024 Dmytro Lobov |
| 12 |
* @license GPL-2.0+ |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
namespace FloatingButton\Admin; |
| 17 |
|
| 18 |
use FloatingButton\WOWP_Plugin; |
| 19 |
|
| 20 |
defined( 'ABSPATH' ) || exit; |
| 21 |
|
| 22 |
class AdminActions { |
| 23 |
|
| 24 |
public static function init(): void { |
| 25 |
add_action( 'admin_init', [ __CLASS__, 'actions' ] ); |
| 26 |
} |
| 27 |
|
| 28 |
public static function actions(): bool { |
| 29 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 30 |
$name = self::check_name( $_REQUEST ); |
| 31 |
if ( ! $name ) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
$verify = self::verify( $name ); |
| 35 |
|
| 36 |
if ( ! $verify ) { |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
if ( strpos( $name, '_export_data' ) !== false ) { |
| 41 |
ImporterExporter::export_data(); |
| 42 |
} elseif ( strpos( $name, '_export_item' ) !== false ) { |
| 43 |
ImporterExporter::export_item(); |
| 44 |
} elseif ( strpos( $name, '_import_data' ) !== false ) { |
| 45 |
ImporterExporter::import_data(); |
| 46 |
} elseif ( strpos( $name, '_remove_item' ) !== false ) { |
| 47 |
DBManager::remove_item(); |
| 48 |
} |
| 49 |
elseif ( strpos( $name, '_activate_item' ) !== false ) { |
| 50 |
Settings::activate_item(); |
| 51 |
} elseif ( strpos( $name, '_deactivate_item' ) !== false ) { |
| 52 |
Settings::deactivate_item(); |
| 53 |
} elseif ( strpos( $name, '_activate_mode' ) !== false ) { |
| 54 |
Settings::activate_mode(); |
| 55 |
} elseif ( strpos( $name, '_deactivate_mode' ) !== false ) { |
| 56 |
Settings::deactivate_mode(); |
| 57 |
} |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
public static function verify( $name ): bool { |
| 63 |
$nonce_action = WOWP_Plugin::PREFIX . '_nonce'; |
| 64 |
$nonce = isset( $_REQUEST[ $name ] ) ? sanitize_text_field( wp_unslash( $_REQUEST[ $name ] ) ) : ''; |
| 65 |
|
| 66 |
return ( ! empty( $nonce ) && wp_verify_nonce( $nonce, $nonce_action ) && current_user_can( 'manage_options' ) ); |
| 67 |
} |
| 68 |
|
| 69 |
private static function check_name( $request ) { |
| 70 |
$names = [ |
| 71 |
WOWP_Plugin::PREFIX . '_import_data', |
| 72 |
WOWP_Plugin::PREFIX . '_export_data', |
| 73 |
WOWP_Plugin::PREFIX . '_export_item', |
| 74 |
WOWP_Plugin::PREFIX . '_remove_item', |
| 75 |
WOWP_Plugin::PREFIX . '_settings', |
| 76 |
WOWP_Plugin::PREFIX . '_activate_item', |
| 77 |
WOWP_Plugin::PREFIX . '_deactivate_item', |
| 78 |
WOWP_Plugin::PREFIX . '_activate_mode', |
| 79 |
WOWP_Plugin::PREFIX . '_deactivate_mode', |
| 80 |
]; |
| 81 |
|
| 82 |
foreach ( $request as $key => $value ) { |
| 83 |
|
| 84 |
if ( in_array( $key, $names, true ) ) { |
| 85 |
return $key; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return false; |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
} |