| 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 |
if (!apply_filters('logtivity_hide_from_menu', false)) { |
| 23 |
add_menu_page( |
| 24 |
($this->options->isWhiteLabelMode() ? 'Logs' : 'Logtivity'), |
| 25 |
($this->options->isWhiteLabelMode() ? 'Logs' : 'Logtivity'), |
| 26 |
'manage_options', |
| 27 |
($this->options->isWhiteLabelMode() ? 'logs' : 'logtivity'), |
| 28 |
[$this, 'showLogIndexPage'], |
| 29 |
'dashicons-chart-area', |
| 30 |
26 |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
if (!apply_filters('logtivity_hide_settings_page', false)) { |
| 35 |
add_submenu_page( |
| 36 |
($this->options->isWhiteLabelMode() ? 'logs' : 'logtivity'), |
| 37 |
'Logtivity Settings', |
| 38 |
'Settings', |
| 39 |
'manage_options', |
| 40 |
'logtivity'.'-settings', |
| 41 |
[$this, 'showLogtivitySettingsPage'] |
| 42 |
); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Show the admin log index |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function showLogIndexPage() |
| 52 |
{ |
| 53 |
if ( !current_user_can( 'manage_options' ) ) { |
| 54 |
wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); |
| 55 |
} |
| 56 |
|
| 57 |
$options = $this->options->getOptions(); |
| 58 |
|
| 59 |
echo logtivity_view('log-index', compact('options')); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Show the admin settings template |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public function showLogtivitySettingsPage() |
| 68 |
{ |
| 69 |
if ( !current_user_can( 'manage_options' ) ) { |
| 70 |
wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
$options = $this->options->getOptions(); |
| 74 |
|
| 75 |
echo logtivity_view('settings', compact('options')); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Update the settings |
| 80 |
* |
| 81 |
* @return WP_Redirect |
| 82 |
*/ |
| 83 |
public function update() |
| 84 |
{ |
| 85 |
if (!wp_verify_nonce( $_POST['logtivity_update_settings'], 'logtivity_update_settings' )) |
| 86 |
{ |
| 87 |
wp_safe_redirect( $this->settingsPageUrl() ); |
| 88 |
exit; |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$user = new Logtivity_Wp_User; |
| 93 |
|
| 94 |
if (!$user->hasRole('administrator')) { |
| 95 |
wp_safe_redirect( $this->settingsPageUrl() ); |
| 96 |
exit; |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
$this->options->update([ |
| 101 |
'logtivity_url_hash' => md5(home_url()), |
| 102 |
], |
| 103 |
false |
| 104 |
); |
| 105 |
|
| 106 |
delete_transient( 'dismissed-logtivity-site-url-has-changed-notice' ); |
| 107 |
|
| 108 |
$this->options->update(); |
| 109 |
|
| 110 |
wp_safe_redirect( $this->settingsPageUrl() ); |
| 111 |
exit; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get the url to the settings page |
| 116 |
* |
| 117 |
* @return string |
| 118 |
*/ |
| 119 |
public function settingsPageUrl() |
| 120 |
{ |
| 121 |
return admin_url('admin.php?page=logtivity-settings'); |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
$Logtivity_Admin = new Logtivity_Admin; |
| 127 |
|
| 128 |
|