\d+)', 'GET', 'finding' ), array( '/findings/(?P\d+)/action', 'POST', 'finding_action' ), array( '/findings/bulk', 'POST', 'findings_bulk' ), array( '/ai/analyze', 'POST', 'ai_analyze' ), array( '/ai/test', 'POST', 'ai_test' ), array( '/ai/models', 'GET', 'ai_models' ), array( '/knowledge', 'POST', 'knowledge_add' ), array( '/knowledge/(?P\d+)', 'GET', 'knowledge_get' ), array( '/knowledge/(?P\d+)', 'POST', 'knowledge_update' ), array( '/knowledge/(?P\d+)', 'DELETE', 'knowledge_delete' ), array( '/firewall/lockouts', 'GET', 'lockouts' ), array( '/firewall/unblock', 'POST', 'unblock' ), array( '/firewall/block', 'POST', 'block' ), array( '/tools/admin-email', 'POST', 'tool_admin_email' ), array( '/tools/username', 'POST', 'tool_username' ), array( '/tools/salts', 'POST', 'tool_salts' ), array( '/hardening/toggle', 'POST', 'hardening_toggle' ), array( '/hardening/apply', 'POST', 'hardening_apply' ), array( '/alerts/test', 'POST', 'alerts_test' ), array( '/log/clear', 'POST', 'log_clear' ), array( '/fix/plan', 'GET', 'fix_plan' ), array( '/modules', 'GET', 'modules_get' ), array( '/modules', 'POST', 'modules_save' ), array( '/redirects', 'GET', 'redirects_get' ), array( '/redirects', 'POST', 'redirects_save' ), array( '/redirects/delete', 'POST', 'redirects_delete' ), array( '/redirects/toggle', 'POST', 'redirects_toggle' ), array( '/redirects/test', 'POST', 'redirects_test' ), array( '/redirects/404', 'POST', 'redirects_clear_404' ), array( '/links', 'GET', 'links_get' ), array( '/links/scan', 'POST', 'links_scan' ), array( '/links/step', 'POST', 'links_step' ), array( '/links/action', 'POST', 'links_action' ), array( '/dbopt', 'GET', 'dbopt_overview' ), array( '/dbopt/run', 'POST', 'dbopt_run' ), array( '/dbopt/tables', 'GET', 'dbopt_tables' ), array( '/dbopt/table', 'POST', 'dbopt_table' ), array( '/dbopt/optimize', 'POST', 'dbopt_optimize' ), array( '/dbopt/settings', 'POST', 'dbopt_settings' ), array( '/dbopt/backups', 'GET', 'dbopt_backups' ), array( '/dbopt/backups/delete', 'POST', 'dbopt_backup_delete' ), // 1.4: Security Wall screens and security tools. array( '/settings/import', 'POST', 'settings_import' ), array( '/settings/reset', 'POST', 'settings_reset' ), array( '/feed/update', 'POST', 'feed_update' ), array( '/traffic', 'GET', 'traffic_rows' ), array( '/traffic/clear', 'POST', 'traffic_clear' ), array( '/sessions', 'GET', 'sessions' ), array( '/sessions/logout', 'POST', 'sessions_logout' ), array( '/users/fix-name', 'POST', 'users_fix_name' ), array( '/users/approve', 'POST', 'users_approve' ), array( '/users/reject', 'POST', 'users_reject' ), array( '/monitor', 'GET', 'monitor_stats' ), array( '/monitor/check', 'POST', 'monitor_check' ), array( '/tools/emergency', 'POST', 'tool_emergency' ), array( '/tools/permissions', 'GET', 'tool_permissions' ), array( '/tools/permissions', 'POST', 'tool_permissions_fix' ), array( '/tools/prefix', 'POST', 'tool_prefix' ), array( '/tools/whois', 'GET', 'tool_whois' ), array( '/tools/connectivity', 'GET', 'tool_connectivity' ), array( '/tools/test-email', 'POST', 'tool_test_email' ), array( '/tools/logs', 'GET', 'tool_logs' ), array( '/tools/db-backup', 'POST', 'tool_db_backup' ), // 1.5: more site features on the Tools page. array( '/comments-off', 'GET', 'comments_off_counts' ), array( '/comments-off/delete', 'POST', 'comments_off_delete' ), array( '/maintenance', 'GET', 'maintenance_get' ), array( '/maintenance', 'POST', 'maintenance_save' ), array( '/recaptcha/test', 'POST', 'recaptcha_test' ), array( '/posttypes', 'GET', 'posttypes_get' ), array( '/posttypes/save', 'POST', 'posttypes_save' ), array( '/posttypes/delete', 'POST', 'posttypes_delete' ), array( '/posttypes/code', 'GET', 'posttypes_code' ), array( '/posttypes/export', 'GET', 'posttypes_export' ), array( '/posttypes/import', 'POST', 'posttypes_import' ), array( '/roles', 'GET', 'roles_get' ), array( '/roles/save', 'POST', 'roles_save' ), array( '/roles/add', 'POST', 'roles_add' ), array( '/roles/rename', 'POST', 'roles_rename' ), array( '/roles/delete', 'POST', 'roles_delete' ), array( '/roles/cap-add', 'POST', 'roles_cap_add' ), array( '/roles/cap-delete', 'POST', 'roles_cap_delete' ), array( '/roles/restore', 'POST', 'roles_restore' ), array( '/roles/users', 'GET', 'roles_users' ), array( '/roles/user', 'POST', 'roles_user' ), // Tools › Admin tools. array( '/switch/users', 'GET', 'switch_users' ), array( '/cron', 'GET', 'cron_list' ), array( '/cron/run', 'POST', 'cron_run' ), array( '/cron/delete', 'POST', 'cron_delete' ), array( '/mail', 'GET', 'mail_get' ), array( '/mail', 'POST', 'mail_save' ), array( '/mail/test', 'POST', 'mail_test' ), array( '/mail/log', 'GET', 'mail_log' ), array( '/mail/log/clear', 'POST', 'mail_log_clear' ), array( '/admin-area', 'GET', 'admin_area_get' ), array( '/admin-area', 'POST', 'admin_area_save' ), ); // Ordering is for editors too; the capability of the post type is checked inside. register_rest_route( self::NS, '/ordering', array( 'methods' => 'POST', 'callback' => array( $this, 'ordering_save' ), 'permission_callback' => function () { return current_user_can( 'edit_pages' ) || current_user_can( 'edit_others_posts' ); }, ) ); foreach ( $routes as $route ) { register_rest_route( self::NS, $route[0], array( 'methods' => $route[1], 'callback' => array( $this, $route[2] ), 'permission_callback' => $admin, ) ); } } public function can_manage() { return current_user_can( Softvolt_Ai_Bodyguard::cap() ); } private function ok( $data, $status = 200 ) { $response = new WP_REST_Response( $data, $status ); $response->header( 'Cache-Control', 'no-store' ); return $response; } private function fail( $error, $status = 400 ) { if ( is_wp_error( $error ) ) { return $this->ok( array( 'ok' => false, 'error' => $error->get_error_message(), 'code' => $error->get_error_code() ), $status ); } return $this->ok( array( 'ok' => false, 'error' => (string) $error ), $status ); } private function param( $request, $key, $default = '' ) { $value = $request->get_param( $key ); return null === $value ? $default : $value; } /* ------------------------------- scan ------------------------------- */ public function scan_start( $request ) { $id = Softvolt_Ai_Bodyguard_Scanner::start( 'manual' ); if ( is_wp_error( $id ) ) { return $this->fail( $id ); } return $this->ok( array( 'ok' => true, 'scan_id' => $id, 'progress' => Softvolt_Ai_Bodyguard_Scanner::progress() ) ); } public function scan_step( $request ) { $budget = max( 5, min( 40, (int) $this->param( $request, 'budget', Softvolt_Ai_Bodyguard_Settings::get( 'scan_time_budget', 12 ) ) ) ); $progress = Softvolt_Ai_Bodyguard_Scanner::step( $budget ); return $this->ok( array( 'ok' => true, 'progress' => $progress ) ); } public function scan_progress( $request ) { return $this->ok( array( 'ok' => true, 'progress' => Softvolt_Ai_Bodyguard_Scanner::progress() ) ); } public function scan_cancel( $request ) { Softvolt_Ai_Bodyguard_Scanner::cancel(); return $this->ok( array( 'ok' => true, 'progress' => Softvolt_Ai_Bodyguard_Scanner::progress() ) ); } /* ----------------------------- findings ----------------------------- */ public function findings( $request ) { $args = array( 'scan_id' => (int) $this->param( $request, 'scan_id', 0 ), 'status' => sanitize_key( $this->param( $request, 'status', 'open' ) ), 'severity' => sanitize_key( $this->param( $request, 'severity', '' ) ), 'category' => sanitize_key( $this->param( $request, 'category', '' ) ), 'verdict' => sanitize_key( $this->param( $request, 'verdict', '' ) ), 'search' => sanitize_text_field( $this->param( $request, 'search', '' ) ), 'per_page' => max( 1, min( 200, (int) $this->param( $request, 'per_page', 50 ) ) ), 'page' => max( 1, (int) $this->param( $request, 'page', 1 ) ), 'orderby' => sanitize_key( $this->param( $request, 'orderby', 'severity' ) ), ); $plan = sanitize_key( $this->param( $request, 'plan', '' ) ); if ( '' !== $plan ) { $ids = Softvolt_Ai_Bodyguard_Fixes::phase_ids( $plan, $args['scan_id'] ); if ( ! $ids ) { return $this->ok( array( 'ok' => true, 'total' => 0, 'rows' => array(), 'counts' => Softvolt_Ai_Bodyguard_Findings::counts( $args['scan_id'] ), 'page' => 1, 'pages' => 0 ) ); } $args['ids'] = $ids; $args['status'] = 'all'; } $result = Softvolt_Ai_Bodyguard_Findings::query( $args ); $rows = array(); foreach ( $result['rows'] as $f ) { $rows[] = $this->format_finding( $f, false ); } return $this->ok( array( 'ok' => true, 'total' => $result['total'], 'rows' => $rows, 'counts' => Softvolt_Ai_Bodyguard_Findings::counts( $args['scan_id'] ), 'page' => $args['page'], 'pages' => (int) ceil( $result['total'] / $args['per_page'] ), ) ); } public function finding( $request ) { $f = Softvolt_Ai_Bodyguard_Findings::get( (int) $request['id'] ); if ( ! $f ) { return $this->fail( __( 'Not found.', 'ai-guardwall' ), 404 ); } return $this->ok( array( 'ok' => true, 'finding' => $this->format_finding( $f, true ) ) ); } private function format_finding( $f, $full ) { $meta = $f['meta_arr']; $out = array( 'id' => (int) $f['id'], 'scan_id' => (int) $f['scan_id'], 'category' => $f['category'], 'category_label' => isset( Softvolt_Ai_Bodyguard_Findings::categories()[ $f['category'] ] ) ? Softvolt_Ai_Bodyguard_Findings::categories()[ $f['category'] ] : $f['category'], 'location' => $f['location'], 'line' => (int) $f['line'], 'severity' => $f['severity'], 'confidence' => (int) $f['confidence'], 'signature' => $f['signature'], 'title' => $f['title'], 'description' => $f['description'], 'status' => $f['status'], 'status_label' => isset( Softvolt_Ai_Bodyguard_Findings::statuses()[ $f['status'] ] ) ? Softvolt_Ai_Bodyguard_Findings::statuses()[ $f['status'] ] : $f['status'], 'verdict' => $f['verdict'], 'remediation' => $f['remediation'], 'remediation_label' => isset( Softvolt_Ai_Bodyguard_Findings::remediations()[ $f['remediation'] ] ) ? Softvolt_Ai_Bodyguard_Findings::remediations()[ $f['remediation'] ] : $f['remediation'], 'ai_verdict' => $f['ai_verdict'], 'ai_confidence' => (int) $f['ai_confidence'], 'updated_at' => $f['updated_at'], 'actions' => Softvolt_Ai_Bodyguard_Remediation::actions_for( $f ), 'official' => ! empty( $meta['official_url'] ), 'hits' => isset( $meta['hits'] ) ? array_slice( (array) $meta['hits'], 0, 12 ) : array(), 'edit_link' => isset( $meta['edit_link'] ) ? $meta['edit_link'] : '', 'plugin_slug' => isset( $meta['plugin_slug'] ) ? $meta['plugin_slug'] : '', 'integrity' => isset( $meta['integrity'] ) ? $meta['integrity'] : '', 'details' => isset( $meta['details'] ) ? $meta['details'] : '', 'size' => isset( $meta['size'] ) ? Softvolt_Ai_Bodyguard_Helpers::human_size( $meta['size'] ) : '', 'mtime' => isset( $meta['mtime_human'] ) ? $meta['mtime_human'] : '', ); $guide = Softvolt_Ai_Bodyguard_Fixes::guide( $f ); $out['fix'] = $guide['title']; $out['fix_primary'] = $guide['primary']; if ( $full ) { $out['snippet'] = (string) $f['snippet']; $out['ai'] = $f['ai_arr']; $out['notes'] = isset( $meta['notes'] ) ? $meta['notes'] : array(); $out['guide'] = $guide; } return $out; } public function finding_action( $request ) { $action = sanitize_key( $this->param( $request, 'action', '' ) ); $result = Softvolt_Ai_Bodyguard_Remediation::run( (int) $request['id'], $action ); if ( is_wp_error( $result ) ) { return $this->fail( $result ); } $f = Softvolt_Ai_Bodyguard_Findings::get( (int) $request['id'] ); return $this->ok( array( 'ok' => true, 'message' => $result['message'], 'finding' => $f ? $this->format_finding( $f, true ) : null ) ); } public function findings_bulk( $request ) { $action = sanitize_key( $this->param( $request, 'action', '' ) ); $ids = array_map( 'intval', (array) $this->param( $request, 'ids', array() ) ); $done = 0; $errors = array(); foreach ( array_slice( $ids, 0, 200 ) as $id ) { $result = Softvolt_Ai_Bodyguard_Remediation::run( $id, $action ); if ( is_wp_error( $result ) ) { $errors[] = '#' . $id . ': ' . $result->get_error_message(); } else { $done++; } } return $this->ok( array( 'ok' => true, 'done' => $done, 'errors' => array_slice( $errors, 0, 10 ) ) ); } /* -------------------------------- AI -------------------------------- */ public function ai_analyze( $request ) { $ids = array_map( 'intval', (array) $this->param( $request, 'ids', array() ) ); $budget = max( 10, min( 60, (int) $this->param( $request, 'budget', 25 ) ) ); Softvolt_Ai_Bodyguard_Helpers::raise_limits( 300 ); $result = Softvolt_Ai_Bodyguard_Analyst::analyze_many( $ids, $budget ); if ( is_wp_error( $result ) ) { return $result; // Waiting for Claude Desktop: the screen follows the task and asks again. } $rows = array(); foreach ( $result['done'] as $id ) { $f = Softvolt_Ai_Bodyguard_Findings::get( $id ); if ( $f ) { $rows[] = $this->format_finding( $f, true ); } } return $this->ok( array( 'ok' => true, 'done' => $rows, 'failed' => $result['failed'], 'remaining' => $result['remaining'] ) ); } public function ai_test( $request ) { $provider = sanitize_key( $this->param( $request, 'provider', Softvolt_Ai_Bodyguard_Settings::get( 'provider' ) ) ); $model = Softvolt_Ai_Bodyguard_Settings::model_id( $this->param( $request, 'model', '' ) ); $key = (string) $this->param( $request, 'key', '' ); $key = '' !== $key ? preg_replace( '/[^\x21-\x7E]/', '', $key ) : null; if ( 'custom' === $provider ) { $base = esc_url_raw( (string) $this->param( $request, 'base_url', '' ) ); if ( '' !== $base ) { add_filter( 'svabg_settings', function ( $s ) use ( $base, $model ) { $s['custom_base_url'] = untrailingslashit( $base ); if ( '' !== $model ) { $s['custom_model'] = $model; } return $s; } ); Softvolt_Ai_Bodyguard_Settings::flush(); } } if ( '' === $model ) { $model = null; } $client = Softvolt_Ai_Bodyguard_Providers::client( $provider, $model, $key ); if ( is_wp_error( $client ) ) { return $this->fail( $client ); } $result = $client->test(); if ( Softvolt_Ai_Bodyguard_Claude::is_waiting( $result ) ) { return $result; // Waiting for Claude Desktop: the screen follows the task and asks again. } if ( is_wp_error( $result ) ) { Softvolt_Ai_Bodyguard_Providers::remember_error( $result->get_error_message() ); return $this->fail( $result ); } $text = isset( $result['text'] ) ? trim( $result['text'] ) : ''; return $this->ok( array( 'ok' => true, 'message' => '' !== $text ? sprintf( /* translators: %s: text */ __( 'Connected. The model answered: "%s"', 'ai-guardwall' ), Softvolt_Ai_Bodyguard_Helpers::trim_text( $text, 60 ) ) : __( 'Connected.', 'ai-guardwall' ) ) ); } public function ai_models( $request ) { $models = Softvolt_Ai_Bodyguard_Catalog::openrouter_free_models(); if ( is_wp_error( $models ) ) { return $this->fail( $models ); } return $this->ok( array( 'ok' => true, 'models' => $models ) ); } /* ----------------------------- knowledge ---------------------------- */ public function knowledge_add( $request ) { $purpose = sanitize_key( $this->param( $request, 'purpose', 'malware' ) ); $files = $request->get_file_params(); if ( ! empty( $files['file'] ) ) { $id = Softvolt_Ai_Bodyguard_Knowledge::import_upload( $files['file'], $purpose ); } else { $id = Softvolt_Ai_Bodyguard_Knowledge::add( (string) $this->param( $request, 'name', '' ), (string) $this->param( $request, 'content', '' ), $purpose ); } if ( is_wp_error( $id ) ) { return $this->fail( $id ); } return $this->ok( array( 'ok' => true, 'id' => $id, 'files' => Softvolt_Ai_Bodyguard_Knowledge::all() ) ); } public function knowledge_get( $request ) { $row = Softvolt_Ai_Bodyguard_Knowledge::get( (int) $request['id'] ); if ( ! $row ) { return $this->fail( __( 'Not found.', 'ai-guardwall' ), 404 ); } return $this->ok( array( 'ok' => true, 'file' => $row ) ); } public function knowledge_update( $request ) { $id = (int) $request['id']; if ( null !== $request->get_param( 'active' ) ) { Softvolt_Ai_Bodyguard_Knowledge::set_active( $id, Softvolt_Ai_Bodyguard_Helpers::bool( $request->get_param( 'active' ) ) ); } if ( null !== $request->get_param( 'purpose' ) ) { Softvolt_Ai_Bodyguard_Knowledge::set_purpose( $id, sanitize_key( $request->get_param( 'purpose' ) ) ); } return $this->ok( array( 'ok' => true, 'files' => Softvolt_Ai_Bodyguard_Knowledge::all() ) ); } public function knowledge_delete( $request ) { $result = Softvolt_Ai_Bodyguard_Knowledge::delete( (int) $request['id'] ); if ( is_wp_error( $result ) ) { return $this->fail( $result ); } return $this->ok( array( 'ok' => true, 'files' => Softvolt_Ai_Bodyguard_Knowledge::all() ) ); } /* ------------------------------ firewall ---------------------------- */ public function lockouts( $request ) { return $this->ok( array( 'ok' => true, 'lockouts' => Softvolt_Ai_Bodyguard_Lockouts::active( 200 ), 'count' => Softvolt_Ai_Bodyguard_Lockouts::count_active() ) ); } public function unblock( $request ) { $ip = sanitize_text_field( $this->param( $request, 'ip', '' ) ); if ( ! Softvolt_Ai_Bodyguard_Helpers::is_valid_ip( $ip ) ) { return $this->fail( __( 'That is not a valid IP address.', 'ai-guardwall' ) ); } Softvolt_Ai_Bodyguard_Lockouts::unblock( $ip ); return $this->ok( array( 'ok' => true, 'message' => sprintf( /* translators: %s: IP address */ __( '%s was unblocked.', 'ai-guardwall' ), $ip ), 'lockouts' => Softvolt_Ai_Bodyguard_Lockouts::active( 200 ) ) ); } public function block( $request ) { $ip = sanitize_text_field( $this->param( $request, 'ip', '' ) ); $minutes = max( 0, (int) $this->param( $request, 'minutes', 0 ) ); if ( ! Softvolt_Ai_Bodyguard_Helpers::is_valid_ip( $ip ) ) { return $this->fail( __( 'That is not a valid IP address.', 'ai-guardwall' ) ); } if ( $ip === Softvolt_Ai_Bodyguard_Helpers::client_ip() ) { return $this->fail( __( 'That is your own address.', 'ai-guardwall' ) ); } Softvolt_Ai_Bodyguard_Lockouts::block( $ip, 'manual', sanitize_text_field( $this->param( $request, 'reason', __( 'Blocked by the administrator', 'ai-guardwall' ) ) ), $minutes ); return $this->ok( array( 'ok' => true, 'message' => sprintf( /* translators: %s: IP address */ __( '%s was blocked.', 'ai-guardwall' ), $ip ), 'lockouts' => Softvolt_Ai_Bodyguard_Lockouts::active( 200 ) ) ); } /* ------------------------------- tools ------------------------------ */ public function tool_admin_email( $request ) { $result = Softvolt_Ai_Bodyguard_Tools::change_admin_email( (string) $this->param( $request, 'email', '' ), Softvolt_Ai_Bodyguard_Helpers::bool( $this->param( $request, 'also_user', false ) ) ); return is_wp_error( $result ) ? $this->fail( $result ) : $this->ok( array( 'ok' => true, 'message' => $result['message'] ) ); } public function tool_username( $request ) { $result = Softvolt_Ai_Bodyguard_Tools::change_username( (int) $this->param( $request, 'user_id', 0 ), (string) $this->param( $request, 'login', '' ) ); return is_wp_error( $result ) ? $this->fail( $result ) : $this->ok( array( 'ok' => true, 'message' => $result['message'], 'users' => Softvolt_Ai_Bodyguard_Tools::users_for_rename() ) ); } public function tool_salts( $request ) { $result = Softvolt_Ai_Bodyguard_Tools::rotate_salts(); return is_wp_error( $result ) ? $this->fail( $result ) : $this->ok( array( 'ok' => true, 'message' => $result['message'], 'relogin' => true ) ); } /* ----------------------------- hardening ---------------------------- */ public function hardening_toggle( $request ) { $rule = sanitize_key( $this->param( $request, 'rule', '' ) ); $on = Softvolt_Ai_Bodyguard_Helpers::bool( $this->param( $request, 'on', false ) ); $rules = Softvolt_Ai_Bodyguard_Hardening::rules(); if ( ! isset( $rules[ $rule ] ) || 'setting' === $rules[ $rule ]['type'] ) { return $this->fail( __( 'Unknown rule.', 'ai-guardwall' ) ); } if ( $on && ! empty( $rules[ $rule ]['pro'] ) && ! Softvolt_Ai_Bodyguard_Pro::is_active() ) { return $this->fail( __( 'This rule needs an active Pro license.', 'ai-guardwall' ) ); } if ( 'admin_ip_allowlist' === $rule && $on && '' === trim( (string) Softvolt_Ai_Bodyguard_Settings::get( 'admin_ip_allowlist', '' ) ) ) { return $this->fail( __( 'Add your IP addresses to the wp-admin allowlist first (Login security tab).', 'ai-guardwall' ) ); } Softvolt_Ai_Bodyguard_Settings::set_hardening( $rule, $on ); $applied = Softvolt_Ai_Bodyguard_Hardening::apply_all_enabled(); Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'hardening', sprintf( /* translators: 1: title, 2: value */ __( 'Hardening rule "%1$s" switched %2$s.', 'ai-guardwall' ), $rules[ $rule ]['title'], $on ? 'on' : 'off' ) ); return $this->ok( array( 'ok' => true, 'on' => Softvolt_Ai_Bodyguard_Hardening::is_on( $rule ), 'score' => Softvolt_Ai_Bodyguard_Hardening::score(), 'warning' => is_wp_error( $applied ) ? implode( ' ', $applied->get_error_messages() ) : '' ) ); } public function hardening_apply( $request ) { $applied = Softvolt_Ai_Bodyguard_Hardening::apply_all_enabled(); return $this->ok( array( 'ok' => ! is_wp_error( $applied ), 'message' => is_wp_error( $applied ) ? implode( ' ', $applied->get_error_messages() ) : __( 'Server rules are in place.', 'ai-guardwall' ), 'status' => Softvolt_Ai_Bodyguard_Htaccess::status() ) ); } /* ------------------------------- misc ------------------------------- */ public function alerts_test( $request ) { $sent = Softvolt_Ai_Bodyguard_Alerts::send( __( 'Test alert', 'ai-guardwall' ), array( __( 'This is a test alert from AI GuardWall. If you can read this, alerts work.', 'ai-guardwall' ) ) ); return $sent ? $this->ok( array( 'ok' => true, 'message' => sprintf( /* translators: %s: email address */ __( 'Test email sent to %s.', 'ai-guardwall' ), Softvolt_Ai_Bodyguard_Settings::alert_email() ) ) ) : $this->fail( __( 'WordPress could not send the email. An SMTP plugin usually fixes that.', 'ai-guardwall' ) ); } public function log_clear( $request ) { Softvolt_Ai_Bodyguard_Logger::clear(); return $this->ok( array( 'ok' => true ) ); } /* ----------------------------- cleanup plan ----------------------------- */ public function fix_plan( $request ) { return $this->ok( array( 'ok' => true, 'plan' => Softvolt_Ai_Bodyguard_Fixes::plan( (int) $this->param( $request, 'scan_id', 0 ) ) ) ); } private function result( $result, $extra = array() ) { if ( is_wp_error( $result ) ) { return $this->fail( $result ); } return $this->ok( array_merge( array( 'ok' => true ), is_array( $result ) ? $result : array(), $extra ) ); } /* ----------------------------- site features ----------------------------- */ private function modules_payload() { $registry = Softvolt_Ai_Bodyguard_Modules::registry(); $out = array(); foreach ( $registry as $id => $m ) { unset( $m['class'] ); $m['on'] = Softvolt_Ai_Bodyguard_Modules::enabled( $id ); $m['tabUrl'] = '' !== $m['tab'] ? Softvolt_Ai_Bodyguard_Admin::url( 'tools', $m['tab'] ) : ''; $out[ $id ] = $m; } return array( 'ok' => true, 'modules' => $out, 'state' => Softvolt_Ai_Bodyguard_Modules::get(), 'types' => Softvolt_Ai_Bodyguard_Modules::post_types(), ); } public function modules_get( $request ) { return $this->ok( $this->modules_payload() ); } public function modules_save( $request ) { $id = sanitize_key( (string) $this->param( $request, 'module', '' ) ); if ( '' !== $id ) { $on = (bool) $this->param( $request, 'on', 0 ) && 'false' !== $this->param( $request, 'on', 0 ); $result = Softvolt_Ai_Bodyguard_Modules::toggle( $id, $on ); if ( is_wp_error( $result ) ) { return $this->fail( $result ); } $payload = $this->modules_payload(); $payload['message'] = $on ? __( 'Switched on.', 'ai-guardwall' ) : __( 'Switched off.', 'ai-guardwall' ); $registry = Softvolt_Ai_Bodyguard_Modules::registry(); $payload['reload'] = '' !== $registry[ $id ]['tab']; return $this->ok( $payload ); } $options = $request->get_param( 'options' ); if ( is_string( $options ) ) { $options = json_decode( wp_unslash( $options ), true ); } Softvolt_Ai_Bodyguard_Modules::update( is_array( $options ) ? wp_unslash( $options ) : array() ); if ( is_array( $options ) && array_key_exists( 'permalink_types', $options ) ) { Softvolt_Ai_Bodyguard_Permalinks::rebuild_map(); } $payload = $this->modules_payload(); $payload['message'] = __( 'Saved.', 'ai-guardwall' ); return $this->ok( $payload ); } public function ordering_save( $request ) { $ids = array_map( 'intval', (array) $this->param( $request, 'ids', array() ) ); return $this->result( Softvolt_Ai_Bodyguard_Ordering::save( sanitize_key( (string) $this->param( $request, 'type', '' ) ), $ids, (int) $this->param( $request, 'offset', 0 ) ) ); } public function redirects_get( $request ) { return $this->ok( array_merge( array( 'ok' => true ), Softvolt_Ai_Bodyguard_Redirects::overview() ) ); } public function redirects_save( $request ) { $rule = Softvolt_Ai_Bodyguard_Redirects::save( array( 'id' => (string) $this->param( $request, 'id', '' ), 'source' => wp_unslash( (string) $this->param( $request, 'source', '' ) ), 'target' => wp_unslash( (string) $this->param( $request, 'target', '' ) ), 'code' => (int) $this->param( $request, 'code', 301 ), 'regex' => $this->param( $request, 'regex', 0 ), 'note' => wp_unslash( (string) $this->param( $request, 'note', '' ) ), 'enabled' => $this->param( $request, 'enabled', 1 ), ) ); if ( is_wp_error( $rule ) ) { return $this->fail( $rule ); } return $this->ok( array_merge( array( 'ok' => true, 'message' => __( 'Redirect saved.', 'ai-guardwall' ), 'rule' => $rule ), Softvolt_Ai_Bodyguard_Redirects::overview() ) ); } public function redirects_delete( $request ) { $r = Softvolt_Ai_Bodyguard_Redirects::delete( (string) $this->param( $request, 'id', '' ) ); return is_wp_error( $r ) ? $this->fail( $r ) : $this->ok( array_merge( array( 'ok' => true ), $r, Softvolt_Ai_Bodyguard_Redirects::overview() ) ); } public function redirects_toggle( $request ) { $r = Softvolt_Ai_Bodyguard_Redirects::toggle( (string) $this->param( $request, 'id', '' ), (bool) $this->param( $request, 'on', 0 ) && 'false' !== $this->param( $request, 'on', 0 ) ); return is_wp_error( $r ) ? $this->fail( $r ) : $this->ok( array_merge( array( 'ok' => true ), Softvolt_Ai_Bodyguard_Redirects::overview() ) ); } public function redirects_test( $request ) { return $this->ok( array_merge( array( 'ok' => true ), Softvolt_Ai_Bodyguard_Redirects::test( wp_unslash( (string) $this->param( $request, 'url', '' ) ) ) ) ); } public function redirects_clear_404( $request ) { $r = Softvolt_Ai_Bodyguard_Redirects::clear_404( (string) $this->param( $request, 'key', '' ) ); return $this->ok( array_merge( array( 'ok' => true ), $r, Softvolt_Ai_Bodyguard_Redirects::overview() ) ); } /* --------------------------- database optimizer --------------------------- */ public function dbopt_overview( $request ) { return $this->ok( array_merge( array( 'ok' => true ), Softvolt_Ai_Bodyguard_Db_Optimizer::overview() ) ); } public function dbopt_run( $request ) { $items = $this->param( $request, 'items', array() ); $items = is_array( $items ) ? array_map( 'sanitize_key', $items ) : array_filter( array_map( 'sanitize_key', explode( ',', (string) $items ) ) ); if ( ! $items ) { return $this->fail( __( 'Tick at least one optimization.', 'ai-guardwall' ) ); } $r = Softvolt_Ai_Bodyguard_Db_Optimizer::run( $items, (bool) $this->param( $request, 'backup', 1 ) ); return empty( $r['ok'] ) ? $this->fail( $r['error'] ) : $this->ok( $r ); } public function dbopt_tables( $request ) { return $this->ok( array_merge( array( 'ok' => true ), Softvolt_Ai_Bodyguard_Db_Optimizer::tables( (bool) $this->param( $request, 'refresh', 0 ) ) ) ); } public function dbopt_table( $request ) { $r = Softvolt_Ai_Bodyguard_Db_Optimizer::table_action( (string) $this->param( $request, 'table', '' ), sanitize_key( (string) $this->param( $request, 'op', '' ) ), (bool) $this->param( $request, 'backup', 1 ) ); if ( is_wp_error( $r ) ) { return $this->fail( $r ); } return $this->ok( array_merge( array( 'ok' => true ), $r, Softvolt_Ai_Bodyguard_Db_Optimizer::tables() ) ); } public function dbopt_optimize( $request ) { $only = $this->param( $request, 'tables', array() ); $r = Softvolt_Ai_Bodyguard_Db_Optimizer::optimize_tables( is_array( $only ) ? array_map( 'strval', $only ) : array() ); return $this->ok( array_merge( array( 'ok' => true ), $r, Softvolt_Ai_Bodyguard_Db_Optimizer::tables() ) ); } public function dbopt_settings( $request ) { $s = Softvolt_Ai_Bodyguard_Db_Optimizer::save_settings( array( 'schedule' => sanitize_key( (string) $this->param( $request, 'schedule', 'off' ) ), 'items' => array_map( 'sanitize_key', (array) $this->param( $request, 'items', array() ) ), 'keep_revisions' => (int) $this->param( $request, 'keep_revisions', 0 ), 'backup' => $this->param( $request, 'backup', 1 ), ) ); $next = wp_next_scheduled( Softvolt_Ai_Bodyguard_Db_Optimizer::HOOK ); return $this->ok( array( 'ok' => true, 'settings' => $s, 'next' => $next ? sprintf( /* translators: %s: date */ __( 'Next automatic run: %s', 'ai-guardwall' ), wp_date( 'Y-m-d H:i', $next ) ) : '', 'message' => __( 'Saved.', 'ai-guardwall' ) ) ); } public function dbopt_backups( $request ) { return $this->ok( array( 'ok' => true, 'backups' => Softvolt_Ai_Bodyguard_Db_Optimizer::backups() ) ); } public function dbopt_backup_delete( $request ) { $ok = Softvolt_Ai_Bodyguard_Db_Optimizer::delete_backup( (string) $this->param( $request, 'file', '' ) ); return $ok ? $this->ok( array( 'ok' => true, 'backups' => Softvolt_Ai_Bodyguard_Db_Optimizer::backups() ) ) : $this->fail( __( 'That backup does not exist.', 'ai-guardwall' ) ); } public function links_get( $request ) { return $this->ok( array_merge( array( 'ok' => true ), Softvolt_Ai_Bodyguard_Link_Checker::rows( array( 'state' => (string) $this->param( $request, 'state', 'broken' ), 'search' => wp_unslash( (string) $this->param( $request, 'search', '' ) ), 'page' => (int) $this->param( $request, 'page', 1 ), ) ) ) ); } public function links_scan( $request ) { return $this->ok( array( 'ok' => true, 'progress' => Softvolt_Ai_Bodyguard_Link_Checker::start( true ) ) ); } public function links_step( $request ) { return $this->ok( array( 'ok' => true, 'progress' => Softvolt_Ai_Bodyguard_Link_Checker::step( max( 5, min( 25, (int) $this->param( $request, 'budget', 12 ) ) ) ) ) ); } public function links_action( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Link_Checker::action( (int) $this->param( $request, 'id', 0 ), sanitize_key( (string) $this->param( $request, 'action', '' ) ), wp_unslash( (string) $this->param( $request, 'url', '' ) ) ) ); } /* ------------------------------- 1.4 screens ------------------------------- */ public function settings_import( $request ) { $data = $request->get_json_params(); if ( isset( $data['file'] ) && is_string( $data['file'] ) ) { $data = json_decode( $data['file'], true ); } if ( ! is_array( $data ) ) { return $this->fail( __( 'The file is not valid JSON.', 'ai-guardwall' ) ); } return $this->result( Softvolt_Ai_Bodyguard_Site_Tools::import( $data ) ); } public function settings_reset( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Site_Tools::reset() ); } public function feed_update( $request ) { Softvolt_Ai_Bodyguard_Helpers::raise_limits( 120 ); $result = Softvolt_Ai_Bodyguard_Threat_Feed::update(); if ( is_wp_error( $result ) ) { return $this->fail( $result ); } return $this->ok( array( 'ok' => true, 'status' => Softvolt_Ai_Bodyguard_Threat_Feed::status() ) ); } public function traffic_rows( $request ) { $ip = sanitize_text_field( (string) $this->param( $request, 'ip', '' ) ); $args = array( 'filter' => sanitize_key( (string) $this->param( $request, 'filter', 'all' ) ), 'ip' => Softvolt_Ai_Bodyguard_Helpers::is_valid_ip( $ip ) ? $ip : '', 'page' => max( 1, (int) $this->param( $request, 'page', 1 ) ), 'since_id' => max( 0, (int) $this->param( $request, 'since_id', 0 ) ), ); $out = Softvolt_Ai_Bodyguard_Traffic::rows( $args ); if ( empty( $args['since_id'] ) && 1 === $args['page'] ) { $out['summary'] = Softvolt_Ai_Bodyguard_Traffic::summary(); } $out['me'] = Softvolt_Ai_Bodyguard_Helpers::client_ip(); return $this->ok( array_merge( array( 'ok' => true ), $out ) ); } public function traffic_clear( $request ) { Softvolt_Ai_Bodyguard_Traffic::clear(); Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'settings', __( 'The live traffic log was cleared.', 'ai-guardwall' ) ); return $this->ok( array( 'ok' => true ) ); } public function sessions( $request ) { return $this->ok( array( 'ok' => true, 'sessions' => Softvolt_Ai_Bodyguard_Access::sessions() ) ); } public function sessions_logout( $request ) { $user = (int) $this->param( $request, 'user', 0 ); $verifier = preg_replace( '/[^a-f0-9]/', '', (string) $this->param( $request, 'verifier', '' ) ); if ( ! $user && ! $this->param( $request, 'all', false ) ) { return $this->fail( __( 'Choose a session.', 'ai-guardwall' ) ); } $n = Softvolt_Ai_Bodyguard_Access::logout( $user, $verifier ); return $this->ok( array( 'ok' => true, 'ended' => $n, 'message' => sprintf( /* translators: %d: number */ _n( '%d session ended.', '%d sessions ended.', $n, 'ai-guardwall' ), $n ), 'sessions' => Softvolt_Ai_Bodyguard_Access::sessions() ) ); } public function users_fix_name( $request ) { $ids = $this->param( $request, 'all', false ) ? wp_list_pluck( Softvolt_Ai_Bodyguard_Access::exposed_logins(), 'id' ) : array( (int) $this->param( $request, 'user', 0 ) ); $out = array(); foreach ( array_filter( array_map( 'intval', $ids ) ) as $id ) { if ( ! current_user_can( 'edit_user', $id ) ) { continue; } $name = Softvolt_Ai_Bodyguard_Access::fix_display_name( $id ); if ( ! is_wp_error( $name ) ) { $out[ $id ] = $name; } } return $this->ok( array( 'ok' => (bool) $out, 'names' => $out, 'error' => $out ? '' : __( 'Nothing was changed.', 'ai-guardwall' ) ) ); } public function users_approve( $request ) { $id = (int) $this->param( $request, 'user', 0 ); if ( ! current_user_can( 'edit_user', $id ) || ! Softvolt_Ai_Bodyguard_Access::approve( $id ) ) { return $this->fail( __( 'This account is not waiting for approval.', 'ai-guardwall' ) ); } return $this->ok( array( 'ok' => true ) ); } public function users_reject( $request ) { $id = (int) $this->param( $request, 'user', 0 ); if ( ! $id || ! current_user_can( 'delete_user', $id ) || ! get_user_meta( $id, Softvolt_Ai_Bodyguard_Access::META_PENDING, true ) ) { return $this->fail( __( 'Only accounts waiting for approval can be deleted here.', 'ai-guardwall' ) ); } $user = get_userdata( $id ); require_once ABSPATH . 'wp-admin/includes/user.php'; wp_delete_user( $id ); Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'registration', sprintf( /* translators: %s: ID */ __( 'Pending account "%s" deleted.', 'ai-guardwall' ), $user ? $user->user_login : '#' . $id ) ); return $this->ok( array( 'ok' => true ) ); } public function monitor_stats( $request ) { return $this->ok( array( 'ok' => true, 'stats' => Softvolt_Ai_Bodyguard_Monitor::stats() ) ); } public function monitor_check( $request ) { Softvolt_Ai_Bodyguard_Monitor::check(); return $this->ok( array( 'ok' => true, 'stats' => Softvolt_Ai_Bodyguard_Monitor::stats() ) ); } public function tool_emergency( $request ) { return $this->result( $this->param( $request, 'on', false ) ? Softvolt_Ai_Bodyguard_Site_Tools::emergency_restore() : Softvolt_Ai_Bodyguard_Site_Tools::emergency_off() ); } public function tool_permissions( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Site_Tools::permissions() ); } public function tool_permissions_fix( $request ) { $paths = array_map( 'sanitize_text_field', (array) $this->param( $request, 'paths', array() ) ); $res = Softvolt_Ai_Bodyguard_Site_Tools::fix_permissions( $paths ); return $this->result( $res, is_wp_error( $res ) ? array() : Softvolt_Ai_Bodyguard_Site_Tools::permissions() ); } public function tool_prefix( $request ) { Softvolt_Ai_Bodyguard_Helpers::raise_limits( 300 ); return $this->result( Softvolt_Ai_Bodyguard_Site_Tools::change_prefix( (string) $this->param( $request, 'prefix', '' ), (bool) $this->param( $request, 'dry_run', false ) ) ); } public function tool_whois( $request ) { $res = Softvolt_Ai_Bodyguard_Site_Tools::whois( sanitize_text_field( (string) $this->param( $request, 'q', '' ) ) ); return $this->result( $res, is_wp_error( $res ) ? array() : array( 'blocked' => Softvolt_Ai_Bodyguard_Helpers::is_valid_ip( $res['query'] ) && (bool) Softvolt_Ai_Bodyguard_Lockouts::is_blocked( $res['query'] ) ) ); } public function tool_connectivity( $request ) { Softvolt_Ai_Bodyguard_Helpers::raise_limits( 120 ); return $this->ok( array( 'ok' => true, 'checks' => Softvolt_Ai_Bodyguard_Site_Tools::connectivity() ) ); } public function tool_test_email( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Site_Tools::test_email( (string) $this->param( $request, 'to', '' ) ) ); } public function tool_db_backup( $request ) { global $wpdb; Softvolt_Ai_Bodyguard_Helpers::raise_limits( 300 ); $tables = array_values( array_filter( (array) $wpdb->get_col( 'SHOW TABLES' ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery function ( $t ) use ( $wpdb ) { return 0 === strpos( $t, $wpdb->prefix ); } ) ); $saved = Softvolt_Ai_Bodyguard_Db_Optimizer::backup( $tables, 'manual' ); if ( is_wp_error( $saved ) ) { return $this->fail( $saved ); } return $this->ok( array( 'ok' => true, 'message' => sprintf( /* translators: 1: number of tables, 2: size */ __( 'Backup saved: %1$d tables, %2$s.', 'ai-guardwall' ), count( $saved['tables'] ), Softvolt_Ai_Bodyguard_Helpers::human_size( $saved['size'] ) ), 'backups' => array_slice( Softvolt_Ai_Bodyguard_Db_Optimizer::backups(), 0, 10 ) ) ); } public function tool_logs( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Site_Tools::log_tail( sanitize_key( (string) $this->param( $request, 'id', '' ) ), (int) $this->param( $request, 'lines', 200 ), sanitize_text_field( (string) $this->param( $request, 'filter', '' ) ) ) ); } /* ------------------------------ 1.5 site features ------------------------------ */ /** * A feature's screen works only while the feature is on. * * @param string $id Module. * @return WP_REST_Response|null The refusal, or null when it is on. */ private function module_off( $id ) { if ( Softvolt_Ai_Bodyguard_Modules::enabled( $id ) ) { return null; } return $this->fail( __( 'Switch this feature on first (Tools › Site features).', 'ai-guardwall' ), 409 ); } private function flag( $request, $key, $default = false ) { $value = $this->param( $request, $key, null ); if ( null === $value ) { return $default; } return ! empty( $value ) && 'false' !== $value && '0' !== $value; } public function comments_off_counts( $request ) { $off = $this->module_off( 'comments' ); return $off ? $off : $this->result( array( 'counts' => Softvolt_Ai_Bodyguard_Comments_Off::counts() ) ); } public function comments_off_delete( $request ) { $off = $this->module_off( 'comments' ); if ( $off ) { return $off; } Softvolt_Ai_Bodyguard_Helpers::raise_limits( 120 ); $result = Softvolt_Ai_Bodyguard_Comments_Off::delete( sanitize_key( (string) $this->param( $request, 'scope', '' ) ), sanitize_key( (string) $this->param( $request, 'type', '' ) ), $this->flag( $request, 'first' ) ); return $this->result( $result, is_wp_error( $result ) ? array() : array( 'counts' => Softvolt_Ai_Bodyguard_Comments_Off::counts() ) ); } private function maintenance_payload() { $s = Softvolt_Ai_Bodyguard_Maintenance::get(); return array( 'settings' => $s, 'end_local' => Softvolt_Ai_Bodyguard_Maintenance::local_time( (int) $s['end'] ), 'bypass_url' => Softvolt_Ai_Bodyguard_Maintenance::bypass_url(), 'preview' => add_query_arg( 'svabg_maintenance_preview', '1', home_url( '/' ) ), ); } public function maintenance_get( $request ) { $off = $this->module_off( 'maintenance' ); return $off ? $off : $this->result( $this->maintenance_payload() ); } public function maintenance_save( $request ) { $off = $this->module_off( 'maintenance' ); if ( $off ) { return $off; } $in = $request->get_param( 'settings' ); if ( is_string( $in ) ) { $in = json_decode( $in, true ); } Softvolt_Ai_Bodyguard_Maintenance::save( is_array( $in ) ? $in : array() ); return $this->result( array_merge( $this->maintenance_payload(), array( 'message' => __( 'Saved.', 'ai-guardwall' ) ) ) ); } public function recaptcha_test( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Captcha::test_token( (string) $this->param( $request, 'token', '' ) ) ); } public function posttypes_get( $request ) { $off = $this->module_off( 'posttypes' ); return $off ? $off : $this->result( Softvolt_Ai_Bodyguard_Post_Types::overview() ); } public function posttypes_save( $request ) { $off = $this->module_off( 'posttypes' ); if ( $off ) { return $off; } $def = $request->get_param( 'def' ); if ( is_string( $def ) ) { $def = json_decode( $def, true ); } $result = Softvolt_Ai_Bodyguard_Post_Types::save( 'tax' === $this->param( $request, 'kind', 'type' ) ? 'tax' : 'type', is_array( $def ) ? $def : array(), (string) $this->param( $request, 'original', '' ), $this->flag( $request, 'move', true ) ); return $this->result( $result, is_wp_error( $result ) ? array() : Softvolt_Ai_Bodyguard_Post_Types::overview() ); } public function posttypes_delete( $request ) { $off = $this->module_off( 'posttypes' ); if ( $off ) { return $off; } $result = Softvolt_Ai_Bodyguard_Post_Types::delete( 'tax' === $this->param( $request, 'kind', 'type' ) ? 'tax' : 'type', (string) $this->param( $request, 'name', '' ) ); return $this->result( $result, is_wp_error( $result ) ? array() : Softvolt_Ai_Bodyguard_Post_Types::overview() ); } public function posttypes_code( $request ) { $off = $this->module_off( 'posttypes' ); return $off ? $off : $this->result( array( 'code' => Softvolt_Ai_Bodyguard_Post_Types::php_code() ) ); } public function posttypes_export( $request ) { $off = $this->module_off( 'posttypes' ); return $off ? $off : $this->result( array( 'export' => Softvolt_Ai_Bodyguard_Post_Types::export_json() ) ); } public function posttypes_import( $request ) { $off = $this->module_off( 'posttypes' ); if ( $off ) { return $off; } $result = Softvolt_Ai_Bodyguard_Post_Types::import_json( (string) $this->param( $request, 'json', '' ) ); return $this->result( $result, is_wp_error( $result ) ? array() : Softvolt_Ai_Bodyguard_Post_Types::overview() ); } private function roles_result( $result ) { return $this->result( $result, is_wp_error( $result ) ? array() : Softvolt_Ai_Bodyguard_Role_Editor::overview() ); } public function roles_get( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Role_Editor::overview() ); } public function roles_save( $request ) { return $this->roles_result( Softvolt_Ai_Bodyguard_Role_Editor::save_role( sanitize_key( (string) $this->param( $request, 'role', '' ) ), (array) $this->param( $request, 'caps', array() ) ) ); } public function roles_add( $request ) { return $this->roles_result( Softvolt_Ai_Bodyguard_Role_Editor::add( (string) $this->param( $request, 'slug', '' ), (string) $this->param( $request, 'name', '' ), sanitize_key( (string) $this->param( $request, 'copy', '' ) ) ) ); } public function roles_rename( $request ) { return $this->roles_result( Softvolt_Ai_Bodyguard_Role_Editor::rename( sanitize_key( (string) $this->param( $request, 'role', '' ) ), (string) $this->param( $request, 'name', '' ) ) ); } public function roles_delete( $request ) { return $this->roles_result( Softvolt_Ai_Bodyguard_Role_Editor::delete( sanitize_key( (string) $this->param( $request, 'role', '' ) ), sanitize_key( (string) $this->param( $request, 'move_to', '' ) ) ) ); } public function roles_cap_add( $request ) { return $this->roles_result( Softvolt_Ai_Bodyguard_Role_Editor::add_cap( (string) $this->param( $request, 'cap', '' ) ) ); } public function roles_cap_delete( $request ) { return $this->roles_result( Softvolt_Ai_Bodyguard_Role_Editor::delete_cap( (string) $this->param( $request, 'cap', '' ) ) ); } public function roles_restore( $request ) { return $this->roles_result( Softvolt_Ai_Bodyguard_Role_Editor::restore() ); } public function roles_users( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Role_Editor::find_users( sanitize_text_field( (string) $this->param( $request, 'search', '' ) ) ) ); } public function roles_user( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Role_Editor::set_user_roles( (int) $this->param( $request, 'user', 0 ), (array) $this->param( $request, 'roles', array() ) ) ); } /* ------------------------------ Tools › Admin tools ------------------------------ */ /** A JSON object parameter, kept exactly as sent (passwords may contain backslashes). */ private function json_arg( $request, $key ) { $value = $request->get_param( $key ); if ( is_string( $value ) ) { $value = json_decode( $value, true ); } return is_array( $value ) ? $value : array(); } public function switch_users( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Admin_Tools::switch_candidates( sanitize_text_field( (string) $this->param( $request, 'search', '' ) ) ) ); } public function cron_list( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Admin_Tools::cron_events() ); } public function cron_run( $request ) { $result = Softvolt_Ai_Bodyguard_Admin_Tools::cron_run( (string) $this->param( $request, 'hook', '' ), (string) $this->param( $request, 'sig', '' ), (int) $this->param( $request, 'time', 0 ) ); return $this->result( $result, is_wp_error( $result ) ? array() : Softvolt_Ai_Bodyguard_Admin_Tools::cron_events() ); } public function cron_delete( $request ) { $result = Softvolt_Ai_Bodyguard_Admin_Tools::cron_delete( (string) $this->param( $request, 'hook', '' ), (string) $this->param( $request, 'sig', '' ), (int) $this->param( $request, 'time', 0 ) ); return $this->result( $result, is_wp_error( $result ) ? array() : Softvolt_Ai_Bodyguard_Admin_Tools::cron_events() ); } public function mail_get( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Mailer::overview() ); } public function mail_save( $request ) { Softvolt_Ai_Bodyguard_Mailer::save( $this->json_arg( $request, 'settings' ) ); return $this->result( array_merge( Softvolt_Ai_Bodyguard_Mailer::overview(), array( 'message' => __( 'Saved.', 'ai-guardwall' ) ) ) ); } public function mail_test( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Mailer::test( (string) $this->param( $request, 'to', '' ) ) ); } public function mail_log( $request ) { return $this->result( Softvolt_Ai_Bodyguard_Mailer::log_rows() ); } public function mail_log_clear( $request ) { return $this->result( array_merge( Softvolt_Ai_Bodyguard_Mailer::clear_log(), Softvolt_Ai_Bodyguard_Mailer::log_rows() ) ); } public function admin_area_get( $request ) { return $this->result( array( 'settings' => Softvolt_Ai_Bodyguard_Admin_Tools::get() ) ); } public function admin_area_save( $request ) { $saved = Softvolt_Ai_Bodyguard_Admin_Tools::save( $this->json_arg( $request, 'settings' ) ); return $this->result( array( 'settings' => $saved, 'message' => __( 'Saved.', 'ai-guardwall' ), ) ); } }