Skip to content

Commit

Permalink
📦 NEW: Site removal request
Browse files Browse the repository at this point in the history
  • Loading branch information
austinginder committed Nov 8, 2024
1 parent f823df2 commit 1cc5cd4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
52 changes: 52 additions & 0 deletions captaincore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br /><br />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.<br /><br />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'];
Expand Down Expand Up @@ -1550,6 +1595,13 @@ function captaincore_register_rest_endpoints() {
'show_in_index' => false
]
);
register_rest_route(
'captaincore/v1', '/sites/(?P<id>[\d]+)', [
'methods' => 'POST',
'callback' => 'captaincore_site_update_func',
'show_in_index' => false
]
);
register_rest_route(
'captaincore/v1', '/site/(?P<id>[\d]+)/(?P<environment>[a-zA-Z0-9-]+)/captures', [
'methods' => 'GET',
Expand Down
26 changes: 26 additions & 0 deletions templates/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1cc5cd4

Please sign in to comment.