Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('save_post','save_post_callback', 10, 3);
- function save_post_callback($post_id, $post, $update){
- if ( 'site' == $post->post_type ) {
- //Use this function to contact CW API
- /**
- *
- * @param string $method GET|POST|PUT|DELETE
- * @param string $url relative URL for the call
- * @param string $accessToken Access token generated using OAuth Call
- * @param type $post Optional post data for the call
- * @return object Output from CW API
- */
- function callCloudwaysAPI($method, $url, $accessToken, $post = [])
- {
- $baseURL = 'https://api.cloudways.com/api/v1';
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
- curl_setopt($ch, CURLOPT_URL, $baseURL . $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //curl_setopt($ch, CURLOPT_HEADER, 1);
- //Set Authorization Header
- if ($accessToken) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $accessToken]);
- }
- //Set Post Parameters
- $encoded = '';
- if (count($post)) {
- foreach ($post as $name => $value) {
- $encoded .= urlencode($name) . '=' . urlencode($value) . '&';
- }
- $encoded = substr($encoded, 0, strlen($encoded) - 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
- curl_setopt($ch, CURLOPT_POST, 1);
- }
- $output = curl_exec($ch);
- $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- if ($httpcode != '200') {
- die('An error occurred code: ' . $httpcode . ' output: ' . substr($output, 0, 10000));
- }
- curl_close($ch);
- return json_decode($output);
- }
- //Fetch Access Token
- $tokenResponse = callCloudwaysAPI('POST', '/oauth/access_token', null
- , [
- 'email' => 'test@domain.ocm'
- , 'api_key' => 'RANDOMKEY'
- ]);
- $accessToken = $tokenResponse->access_token;
- //Fetch Server List
- $serverList = callCloudwaysAPI('GET', '/server', $accessToken);
- //object to json
- $json = json_encode($serverList);
- //json to json array
- $decode = json_decode($json); // or json_decode($json, true); to set as array instead of object
- // only use var_dump for debugging purposes var_dump($var);
- //print_r($json);
- //https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php
- function getAppsLabel($decode)
- {
- $arr = array();
- for ($i = 0; $i < sizeof($decode->servers); $i++) {
- for ($j = 0; $j < sizeof($decode->servers[$i]->apps); $j++) {
- array_push($arr, $decode->servers[$i]->apps[$j]->label);
- }
- }
- return $arr;
- }
- function getAppsServerId($decode)
- {
- $arr = array();
- for ($i = 0; $i < sizeof($decode->servers); $i++) {
- for ($j = 0; $j < sizeof($decode->servers[$i]->apps); $j++) {
- $arr[$decode->servers[$i]->apps[$j]->label] = $decode->servers[$i]->apps[$j]->server_id;
- }
- }
- return $arr;
- }
- function getAppsId($decode)
- {
- $arr = array();
- for ($i = 0; $i < sizeof($decode->servers); $i++) {
- for ($j = 0; $j < sizeof($decode->servers[$i]->apps); $j++) {
- $arr[$decode->servers[$i]->apps[$j]->label] = $decode->servers[$i]->apps[$j]->id;
- }
- }
- return $arr;
- }
- function getAppsDatabaseName($decode)
- {
- $arr = array();
- for ($i = 0; $i < sizeof($decode->servers); $i++) {
- for ($j = 0; $j < sizeof($decode->servers[$i]->apps); $j++) {
- $arr[$decode->servers[$i]->apps[$j]->label] = $decode->servers[$i]->apps[$j]->mysql_db_name;
- }
- }
- return $arr;
- }
- function getAppsDatabaseUser($decode)
- {
- $arr = array();
- for ($i = 0; $i < sizeof($decode->servers); $i++) {
- for ($j = 0; $j < sizeof($decode->servers[$i]->apps); $j++) {
- $arr[$decode->servers[$i]->apps[$j]->label] = $decode->servers[$i]->apps[$j]->mysql_user;
- }
- }
- return $arr;
- }
- function getAppsDatabasePassword($decode)
- {
- $arr = array();
- for ($i = 0; $i < sizeof($decode->servers); $i++) {
- for ($j = 0; $j < sizeof($decode->servers[$i]->apps); $j++) {
- $arr[$decode->servers[$i]->apps[$j]->label] = $decode->servers[$i]->apps[$j]->mysql_password;
- }
- }
- return $arr;
- }
- $app_label = getAppsLabel($decode);
- $app_id = getAppsId($decode);
- $app_serverid = getAppsServerId($decode);
- $app_databasename = getAppsDatabaseName($decode);
- $app_databaseuser = getAppsDatabaseUser($decode);
- $app_databasepassword = getAppsDatabasePassword($decode);
- $primary_domain = get_field( "primary_domain" );
- $primary_domain = str_replace(array('http://', 'https://', 'www.'), '', $primary_domain); // Remove http:// https:// www.
- $primary_domain = rtrim($primary_domain, '/'); // Remove trailing slash
- update_field('app_id', $app_id[$primary_domain]);
- update_field('server_id', $app_serverid[$primary_domain]);
- update_field('database_name', $app_databasename[$primary_domain]);
- update_field('database_username', $app_databaseuser[$primary_domain]);
- update_field('database_password', $app_databasepassword[$primary_domain]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement