| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Admin |
| 4 |
{ |
| 5 |
protected $options; |
| 6 |
|
| 7 |
public function __construct() |
| 8 |
{ |
| 9 |
add_action( 'admin_menu', [$this, 'registerOptionsPage'] ); |
| 10 |
|
| 11 |
add_action( 'wp_ajax_logtivity_update_settings', [$this, 'update']); |
| 12 |
add_action( 'wp_ajax_nopriv_logtivity_update_settings', [$this, 'update']); |
| 13 |
|
| 14 |
$this->options = new Logtivity_Options; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Register the settings page |
| 19 |
*/ |
| 20 |
public function registerOptionsPage() |
| 21 |
{ |
| 22 |
add_management_page( 'Logtivity', 'Logtivity', 'manage_options', 'logtivity', [$this, 'content'] ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Show the admin settings template |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function content() |
| 31 |
{ |
| 32 |
if ( !current_user_can( 'manage_options' ) ) { |
| 33 |
wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); |
| 34 |
} |
| 35 |
|
| 36 |
$options = $this->options->getOptions(); |
| 37 |
|
| 38 |
echo logtivity_view('admin', compact('options')); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Update the settings |
| 43 |
* |
| 44 |
* @return WP_Redirect |
| 45 |
*/ |
| 46 |
public function update() |
| 47 |
{ |
| 48 |
if (!wp_verify_nonce( $_POST['logtivity_update_settings'], 'logtivity_update_settings' )) |
| 49 |
{ |
| 50 |
wp_safe_redirect( $this->settingsPageUrl() ); |
| 51 |
exit; |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$user = new Logtivity_Wp_User; |
| 56 |
|
| 57 |
if (!$user->hasRole('administrator')) { |
| 58 |
wp_safe_redirect( $this->settingsPageUrl() ); |
| 59 |
exit; |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$this->options->update(); |
| 64 |
|
| 65 |
wp_safe_redirect( $this->settingsPageUrl() ); |
| 66 |
exit; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get the url to the settings page |
| 71 |
* |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
public function settingsPageUrl() |
| 75 |
{ |
| 76 |
return admin_url('tools.php?page=logtivity'); |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
$Logtivity_Admin = new Logtivity_Admin; |
| 82 |
|
| 83 |
|