📦 NEW: Global site defaults

This commit is contained in:
Austin Ginder 2020-03-25 20:41:39 -04:00
parent 2cf16ed89e
commit 7fd990c353
5 changed files with 206 additions and 24 deletions

73
app/Defaults.php Normal file
View file

@ -0,0 +1,73 @@
<?php

namespace CaptainCore;

class Defaults {

protected $defaults = [];

public function __construct( $domain_id = "" ) {
$this->domain_id = $domain_id;
}

public function get() {
$defaults = json_decode( get_site_option( 'captaincore_defaults' ) );
if ( empty( $defaults ) ) {
$defaults = (object) [];
}
if ( ! isset( $defaults->users ) ) {
$defaults->users = [];
}
if ( ! isset( $defaults->recipes ) ) {
$defaults->recipes = [];
}
if ( ! isset( $defaults->email ) ) {
$defaults->email = "";
}
if ( ! isset( $defaults->timezone ) ) {
$defaults->timezone = "";
}
return $defaults;
}

public function update( $field, $value ) {
$defaults = json_decode( get_site_option( 'captaincore_defaults' ) );
if ( empty( $defaults ) ) {
$defaults = (object) [];
}
$defaults->{$field} = $value;
update_site_option( 'captaincore_defaults', json_encode( $defaults ) );
}

public function sync() {

$command = "default-sync";
// Disable https when debug enabled
if ( defined( 'CAPTAINCORE_DEBUG' ) ) {
add_filter( 'https_ssl_verify', '__return_false' );
}

$data = [
'timeout' => 45,
'headers' => [
'Content-Type' => 'application/json; charset=utf-8',
'token' => CAPTAINCORE_CLI_TOKEN
],
'body' => json_encode( [ "command" => $command ]),
'method' => 'POST',
'data_format' => 'body'
];

// Add command to dispatch server
$response = wp_remote_post( CAPTAINCORE_CLI_ADDRESS . "/run/background", $data );
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
return "Something went wrong: $error_message";
}
return $response["body"];
}

}

View file

@ -1321,7 +1321,7 @@ function captaincore_api_func( WP_REST_Request $request ) {

// Error if site not valid
$current_site = ( new CaptainCore\Sites )->get( $site_id );
if ( $current_site == "" && $site_id != "" ) {
if ( $current_site == "" && $site_id != "" && $command != "default-get" ) {
return new WP_Error( 'command_invalid', 'Invalid Command', [ 'status' => 404 ] );
}

@ -1545,6 +1545,13 @@ function captaincore_api_func( WP_REST_Request $request ) {
];
}

if ( $command == 'default-get' ) {
$defaults = ( new CaptainCore\Defaults )->get();
$response = [
"response" => "Fetching global defaults",
"defaults" => $defaults,
];
}

if ( $command == 'quicksave-add' ) {
@ -1666,17 +1673,29 @@ function captaincore_users_func( $request ) {
return ( new CaptainCore\Users() )->list();
}

function captaincore_keys_func( $request ) {
function captaincore_keys_func( $request ) {

$current_user = wp_get_current_user();
$role_check = in_array( 'administrator', $current_user->roles );

// Checks for a current user. If admin found pass
if ( $current_user && $role_check ) {
return (new CaptainCore\Keys())->all( "title", "ASC" );
} else {
return [];
return ( new CaptainCore\Keys )->all( "title", "ASC" );
}
return [];

}

function captaincore_defaults_func( $request ) {

$current_user = wp_get_current_user();
$role_check = in_array( 'administrator', $current_user->roles );

// Checks for a current user. If admin found pass
if ( $current_user && $role_check ) {
return ( new CaptainCore\Defaults )->get();
}
return [];

}

@ -1867,7 +1886,7 @@ function captaincore_register_rest_endpoints() {
]
);

// Custom endpoint for domains
// Custom endpoint for keys
register_rest_route(
'captaincore/v1', '/keys/', [
'methods' => 'GET',
@ -1876,6 +1895,15 @@ function captaincore_register_rest_endpoints() {
]
);

// Custom endpoint for defaults
register_rest_route(
'captaincore/v1', '/defaults/', [
'methods' => 'GET',
'callback' => 'captaincore_defaults_func',
'show_in_index' => false
]
);

// Add meta fields to API
register_rest_field(
'captcore_website', 'launch_date',
@ -3364,6 +3392,17 @@ function captaincore_local_action_callback() {
echo json_encode( "Record updated." );
}

if ( $cmd == 'saveGlobalDefaults' ) {
$user = new CaptainCore\User;
if ( ! $user->is_admin() ) {
echo json_encode( "Permission denied" );
wp_die();
}
update_site_option( 'captaincore_defaults', json_encode( $value ) );
( new CaptainCore\Defaults )->sync();
echo json_encode( "Global defaults updated." );
}

wp_die();

}

View file

@ -3625,23 +3625,51 @@ if ( $role_check ) {
<v-toolbar-title>Site Defaults</v-toolbar-title>
<v-spacer></v-spacer>
</v-toolbar>
<v-card-text style="max-height: 100%;">
<v-container fluid grid-list-lg>
<v-layout row wrap>
<v-flex xs12 v-for="key in keys">
<v-card :hover="true" @click="viewKey( key.key_id )">
<v-card-title primary-title class="pt-2">
<div>
<span class="title">{{ key.title }}</a></span>
</div>
</v-card-title>
<v-card-text>
<v-chip color="primary" text-color="white" text>{{ key.fingerprint }}</v-chip>
</v-card-text>
</v-card>
</v-flex>
<v-card-text>
<v-alert :value="true" type="info" class="mb-4 mt-4">
When new sites are added then the following default settings will be applied.
</v-alert>
<v-layout wrap>
<v-flex xs6 pr-2><v-text-field :value="defaults.email" @change.native="defaults.email = $event.target.value" label="Default Email" required></v-text-field></v-flex>
<v-flex xs6 pl-2><v-autocomplete :items="timezones" label="Default Timezone" v-model="defaults.timezone"></v-autocomplete></v-flex>
</v-layout>
</v-container>
<v-layout wrap>
<v-flex><v-autocomplete label="Default Recipes" v-model="defaults.recipes" ref="default_recipes" :items="recipes" item-text="title" item-value="recipe_id" multiple chips deletable-chips></v-autocomplete></v-flex>
</v-layout>

<span class="body-2">Default Users</span>
<v-data-table
:items="defaults.users"
hide-default-header
hide-default-footer
v-if="typeof defaults.users == 'object'"
>
<template v-slot:body="{ items }">
<tbody>
<tr v-for="(item, index) in items" style="border-bottom: 0px;">
<td class="pa-1"><v-text-field :value="item.username" @change.native="item.username = $event.target.value" label="Username"></v-text-field></td>
<td class="pa-1"><v-text-field :value="item.email" @change.native="item.email = $event.target.value" label="Email"></v-text-field></td>
<td class="pa-1"><v-text-field :value="item.first_name" @change.native="item.first_name = $event.target.value" label="First Name"></v-text-field></td>
<td class="pa-1"><v-text-field :value="item.last_name" @change.native="item.last_name = $event.target.value" label="Last Name"></v-text-field></td>
<td class="pa-1" style="width:145px;"><v-select :value="item.role" v-model="item.role" :items="roles" label="Role" item-text="name"></v-select></td>
<td class="pa-1"><v-btn text small icon color="primary" @click="deleteGlobalUserValue( index )"><v-icon small>mdi-delete</v-icon></v-btn></td>
</tr>
</tbody>
</template>
<template v-slot:footer>
<tr style="border-top: 0px;">
<td colspan="5" style="padding:0px;">
<v-btn depressed small class="ma-0 mb-3" @click="addGlobalDefaultsUser()">Add Additional User</v-btn>
</td>
</tr>
</template>
</v-data-table>

<v-flex xs12 text-right>
<v-btn color="primary" dark @click="saveGlobalDefaults()">
Save Changes
</v-btn>
</v-flex>
</v-card-text>
</v-card>
<v-card tile v-show="route == 'keys'" v-if="role == 'administrator'" flat>
@ -4160,6 +4188,7 @@ new Vue({
timezones: <?php echo json_encode( timezone_identifiers_list() ); ?>,
jobs: [],
keys: [],
defaults: [],
custom_script: "",
recipes: [],
processes: [],
@ -4549,6 +4578,7 @@ new Vue({
if ( this.route == "defaults" ) {
this.selected_nav = ""
this.loading_page = false;
this.fetchDefaults()
}
if ( this.route == "profile" ) {
this.selected_nav = ""
@ -4704,6 +4734,24 @@ new Vue({
);
};
},
saveGlobalDefaults() {
this.dialog_configure_defaults.loading = true;
// Prep AJAX request
var data = {
'action': 'captaincore_local',
'command': "saveGlobalDefaults",
'value': this.defaults
};
axios.post( ajaxurl, Qs.stringify( data ) )
.then( response => {
this.snackbar.message = response.data
this.snackbar.show = true
})
.catch(error => {
this.snackbar.message = error.response
this.snackbar.show = true
});
},
saveDefaults() {
this.dialog_configure_defaults.loading = true;
// Prep AJAX request
@ -5267,6 +5315,20 @@ new Vue({
setTimeout(this.fetchMissing, 4000)
});
},
fetchDefaults() {
if ( this.role != 'administrator' ) {
return
}
axios.get(
'/wp-json/captaincore/v1/defaults', {
headers: {'X-WP-Nonce':this.wp_nonce}
})
.then(response => {
this.defaults = response.data;
this.loading_page = false;
setTimeout(this.fetchMissing, 4000)
});
},
fetchAccounts() {
axios.get(
'/wp-json/captaincore/v1/accounts', {
@ -6807,6 +6869,9 @@ new Vue({
addDefaultsUser() {
this.dialog_account.records.account.defaults.users.push({ email: "", first_name: "", last_name: "", role: "administrator", username: "" })
},
addGlobalDefaultsUser() {
this.defaults.users.push({ email: "", first_name: "", last_name: "", role: "administrator", username: "" })
},
addDomain() {
this.dialog_new_domain.loading = true;
this.dialog_new_domain.errors = [];
@ -6880,10 +6945,13 @@ new Vue({
}
},
deleteUserValue( delete_index ) {
this.dialog_account.records.account.defaults.users = this.dialog_account.records.account.defaults.users.filter( (u, index) => index != delete_index );
this.dialog_account.records.account.defaults.users = this.dialog_account.records.account.defaults.users.filter( (u, index) => index != delete_index )
},
deleteGlobalUserValue( delete_index ) {
this.defaults.users = this.defaults.users.filter( (u, index) => index != delete_index )
},
deleteRecordValue( index, value_index ) {
this.dialog_domain.records[index].update.record_value.splice( value_index, 1 );
this.dialog_domain.records[index].update.record_value.splice( value_index, 1 )
},
deleteCurrentRecord( record_id ){
record = this.dialog_domain.records.filter( r => r.id == record_id )[0];

View file

@ -49,6 +49,7 @@ return array(
'CaptainCore\\AccountsCPT' => $baseDir . '/app/AccountsCPT.php',
'CaptainCore\\Captures' => $baseDir . '/app/Captures.php',
'CaptainCore\\DB' => $baseDir . '/app/DB.php',
'CaptainCore\\Defaults' => $baseDir . '/app/Defaults.php',
'CaptainCore\\Domain' => $baseDir . '/app/Domain.php',
'CaptainCore\\Domains' => $baseDir . '/app/Domains.php',
'CaptainCore\\Environments' => $baseDir . '/app/Environments.php',

View file

@ -168,6 +168,7 @@ class ComposerStaticInit9f7509cc1c55bc410ccf6f05510f2050
'CaptainCore\\AccountsCPT' => __DIR__ . '/../..' . '/app/AccountsCPT.php',
'CaptainCore\\Captures' => __DIR__ . '/../..' . '/app/Captures.php',
'CaptainCore\\DB' => __DIR__ . '/../..' . '/app/DB.php',
'CaptainCore\\Defaults' => __DIR__ . '/../..' . '/app/Defaults.php',
'CaptainCore\\Domain' => __DIR__ . '/../..' . '/app/Domain.php',
'CaptainCore\\Domains' => __DIR__ . '/../..' . '/app/Domains.php',
'CaptainCore\\Environments' => __DIR__ . '/../..' . '/app/Environments.php',