diff --git a/captaincore.php b/captaincore.php index c7273dc..3ba23fa 100755 --- a/captaincore.php +++ b/captaincore.php @@ -645,6 +645,51 @@ function captaincore_sites_func( $request ) { return ( new CaptainCore\Sites )->list(); } +function captaincore_site_update_func( $request ) { + $site_id = $request['id']; + $updated_details = $request['details']; + + if ( ! is_numeric ( $site_id ) ) { + $site = ( new CaptainCore\Sites )->where( [ "site" => $site_id ] ); + if ( count( $site ) == 1 ) { + $site_id = $site[0]->site_id; + } + } + + if ( ! captaincore_verify_permissions( $site_id ) ) { + return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] ); + } + + $site = CaptainCore\Sites::get( $site_id ); + $details = empty( $site->details ) ? (object) [] : json_decode( $site->details ); + foreach ( $updated_details as $field => $value ) { + $details->$field = $value; + if ( $field == "removed" ) { + $title = ( new CaptainCore\Configurations )->get()->name; + $user = (object) ( new CaptainCore\User )->fetch(); + if ( $value == true ) { + $subject = "$title - Site Removal Request"; + $message = "Site {$site->name} #{$site_id} has been requested to be removed.

Requested By: {$user->name} #{$user->user_id}"; + } + if ( $value != true ) { + $subject = "$title - Cancel Site Removal Request"; + $message = "Site {$site->name} #{$site_id} has been requested to keep. Disregard previous removal request.

Requested By: {$user->name} #{$user->user_id}"; + } + wp_mail( + get_option( "admin_email" ), + $subject, + $message + ); + } + } + $query = CaptainCore\Sites::update([ + "details" => json_encode( $details ) + ], [ + "site_id" => $site_id + ]); + return; +} + function captaincore_site_func( $request ) { $site_id = $request['id']; @@ -1550,6 +1595,13 @@ function captaincore_register_rest_endpoints() { 'show_in_index' => false ] ); + register_rest_route( + 'captaincore/v1', '/sites/(?P[\d]+)', [ + 'methods' => 'POST', + 'callback' => 'captaincore_site_update_func', + 'show_in_index' => false + ] + ); register_rest_route( 'captaincore/v1', '/site/(?P[\d]+)/(?P[a-zA-Z0-9-]+)/captures', [ 'methods' => 'GET', diff --git a/templates/core.php b/templates/core.php index 908b2ee..16fb5b0 100644 --- a/templates/core.php +++ b/templates/core.php @@ -9854,6 +9854,32 @@ class="code" this.dialog_edit_site.site = JSON.parse ( JSON.stringify ( this.dialog_site.site ) ) this.dialog_site.step = 4 }, + cancelSiteRemoved() { + site_id = this.dialog_site.site.site_id + this.dialog_site.site.removed = false + this.snackbar.message = `Cancelling removal request for ${this.dialog_site.site.name}.` + this.snackbar.show = true + axios.post( `/wp-json/captaincore/v1/sites/${site_id}`, { + details: { + removed: false + } + }, { + headers: { 'X-WP-Nonce':this.wp_nonce } + }) + }, + markSiteRemoved() { + site_id = this.dialog_site.site.site_id + this.dialog_site.site.removed = true + this.snackbar.message = `Marking ${this.dialog_site.site.name} for removal.` + this.snackbar.show = true + axios.post( `/wp-json/captaincore/v1/sites/${site_id}`, { + details: { + removed: true + } + }, { + headers: { 'X-WP-Nonce':this.wp_nonce } + }) + }, deleteSite( site_id ) { site = this.dialog_site.site site_name = site.name;