| 1 |
<?php |
| 2 |
/** |
| 3 |
* AdminActions class for Floating Button plugin. |
| 4 |
* |
| 5 |
* @package FloatingButton\Admin |
| 6 |
* |
| 7 |
* Methods: |
| 8 |
* - init() Initialize admin actions hook |
| 9 |
* - actions() Handle admin requests based on request name |
| 10 |
* - verify( $name ) Verify nonce and user capability |
| 11 |
* - check_name() Detect action name from $_REQUEST |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace FloatingButton\Admin; |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
use FloatingButton\Dashboard\DBManager; |
| 19 |
use FloatingButton\Dashboard\ImporterExporter; |
| 20 |
use FloatingButton\Dashboard\Settings; |
| 21 |
use FloatingButton\WOW_Plugin; |
| 22 |
|
| 23 |
class AdminActions { |
| 24 |
|
| 25 |
public function __construct() { |
| 26 |
add_action( 'admin_init', [ $this, 'actions' ] ); |
| 27 |
} |
| 28 |
|
| 29 |
public function actions(): void { |
| 30 |
$name = $this->check_name(); |
| 31 |
if ( ! $name || ! $this->verify( $name ) ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
$map = [ |
| 36 |
'_export_data' => [ ImporterExporter::class, 'export_data' ], |
| 37 |
'_export_item' => [ ImporterExporter::class, 'export_item' ], |
| 38 |
'_import_data' => [ ImporterExporter::class, 'import_data' ], |
| 39 |
'_remove_item' => [ DBManager::class, 'remove_item' ], |
| 40 |
'_settings' => [ Settings::class, 'save_item' ], |
| 41 |
'_activate_item' => [ Settings::class, 'activate_item' ], |
| 42 |
'_deactivate_item' => [ Settings::class, 'deactivate_item' ], |
| 43 |
'_activate_mode' => [ Settings::class, 'activate_mode' ], |
| 44 |
'_deactivate_mode' => [ Settings::class, 'deactivate_mode' ], |
| 45 |
]; |
| 46 |
|
| 47 |
foreach ( $map as $key => $callback ) { |
| 48 |
if ( is_callable( $callback ) && strpos( $name, $key ) !== false ) { |
| 49 |
$callback(); // call static method |
| 50 |
break; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
private function verify( string $name ): bool { |
| 56 |
$nonce_action = WOW_Plugin::PREFIX . '_nonce'; |
| 57 |
$nonce = sanitize_text_field( wp_unslash( $_REQUEST[ $name ] ?? '' ) ); |
| 58 |
|
| 59 |
return $nonce && wp_verify_nonce( $nonce, $nonce_action ) && current_user_can( 'manage_options' ); |
| 60 |
} |
| 61 |
|
| 62 |
private function check_name(): string { |
| 63 |
$actions = [ |
| 64 |
'_import_data', |
| 65 |
'_export_data', |
| 66 |
'_export_item', |
| 67 |
'_remove_item', |
| 68 |
'_settings', |
| 69 |
'_activate_item', |
| 70 |
'_deactivate_item', |
| 71 |
'_activate_mode', |
| 72 |
'_deactivate_mode', |
| 73 |
]; |
| 74 |
|
| 75 |
foreach ( $actions as $action ) { |
| 76 |
$name = WOW_Plugin::PREFIX . $action; |
| 77 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 78 |
if ( isset( $_REQUEST[ $name ] ) ) { |
| 79 |
return $name; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return ''; |
| 84 |
} |
| 85 |
} |