Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. add_action('save_post','save_post_callback', 10, 3);
  2. function save_post_callback($post_id, $post, $update){
  3. if ( 'site' == $post->post_type ) {
  4. //Use this function to contact CW API
  5. /**
  6. *
  7. * @param string $method GET|POST|PUT|DELETE
  8. * @param string $url relative URL for the call
  9. * @param string $accessToken Access token generated using OAuth Call
  10. * @param type $post Optional post data for the call
  11. * @return object Output from CW API
  12. */
  13. function callCloudwaysAPI($method, $url, $accessToken, $post = [])
  14. {
  15. $baseURL = 'https://api.cloudways.com/api/v1';
  16. $ch = curl_init();
  17. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  18. curl_setopt($ch, CURLOPT_URL, $baseURL . $url);
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  20. //curl_setopt($ch, CURLOPT_HEADER, 1);
  21. //Set Authorization Header
  22. if ($accessToken) {
  23. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $accessToken]);
  24. }
  25. //Set Post Parameters
  26. $encoded = '';
  27. if (count($post)) {
  28. foreach ($post as $name => $value) {
  29. $encoded .= urlencode($name) . '=' . urlencode($value) . '&';
  30. }
  31. $encoded = substr($encoded, 0, strlen($encoded) - 1);
  32. curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
  33. curl_setopt($ch, CURLOPT_POST, 1);
  34. }
  35. $output = curl_exec($ch);
  36. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  37. if ($httpcode != '200') {
  38. die('An error occurred code: ' . $httpcode . ' output: ' . substr($output, 0, 10000));
  39. }
  40. curl_close($ch);
  41. return json_decode($output);
  42. }
  43. //Fetch Access Token
  44. $tokenResponse = callCloudwaysAPI('POST', '/oauth/access_token', null
  45. , [
  46. 'email' => 'test@domain.ocm'
  47. , 'api_key' => 'RANDOMKEY'
  48. ]);
  49. $accessToken = $tokenResponse->access_token;
  50. //Fetch Server List
  51. $serverList = callCloudwaysAPI('GET', '/server', $accessToken);
  52.  
  53. //object to json
  54. $json = json_encode($serverList);
  55.  
  56. //json to json array
  57. $decode = json_decode($json); // or json_decode($json, true); to set as array instead of object
  58.  
  59. // only use var_dump for debugging purposes var_dump($var);
  60. //print_r($json);
  61.  
  62. //https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php
  63.  
  64.  
  65. function getAppsLabel($decode)
  66. {
  67. $arr = array();
  68. for ($i = 0; $i < sizeof($decode->servers); $i++) {
  69. for ($j = 0; $j < sizeof($decode->servers[$i]->apps); $j++) {
  70. array_push($arr, $decode->servers[$i]->apps[$j]->label);
  71. }
  72. }
  73. return $arr;
  74. }
  75.  
  76. function getAppsServerId($decode)
  77. {
  78. $arr = array();
  79. for ($i = 0; $i < sizeof($decode->servers); $i++) {
  80. for ($j = 0; $j < sizeof($decode->servers[$i]->apps); $j++) {
  81. $arr[$decode->servers[$i]->apps[$j]->label] = $decode->servers[$i]->apps[$j]->server_id;
  82. }
  83. }
  84. return $arr;
  85. }
  86.  
  87. function getAppsId($decode)
  88. {
  89. $arr = array();
  90. for ($i = 0; $i < sizeof($decode->servers); $i++) {
  91. for ($j = 0; $j < sizeof($decode->servers[$i]->apps); $j++) {
  92. $arr[$decode->servers[$i]->apps[$j]->label] = $decode->servers[$i]->apps[$j]->id;
  93. }
  94. }
  95. return $arr;
  96. }
  97.  
  98. function getAppsDatabaseName($decode)
  99. {
  100. $arr = array();
  101. for ($i = 0; $i < sizeof($decode->servers); $i++) {
  102. for ($j = 0; $j < sizeof($decode->servers[$i]->apps); $j++) {
  103. $arr[$decode->servers[$i]->apps[$j]->label] = $decode->servers[$i]->apps[$j]->mysql_db_name;
  104. }
  105. }
  106. return $arr;
  107. }
  108.  
  109. function getAppsDatabaseUser($decode)
  110. {
  111. $arr = array();
  112. for ($i = 0; $i < sizeof($decode->servers); $i++) {
  113. for ($j = 0; $j < sizeof($decode->servers[$i]->apps); $j++) {
  114. $arr[$decode->servers[$i]->apps[$j]->label] = $decode->servers[$i]->apps[$j]->mysql_user;
  115. }
  116. }
  117. return $arr;
  118. }
  119.  
  120. function getAppsDatabasePassword($decode)
  121. {
  122. $arr = array();
  123. for ($i = 0; $i < sizeof($decode->servers); $i++) {
  124. for ($j = 0; $j < sizeof($decode->servers[$i]->apps); $j++) {
  125. $arr[$decode->servers[$i]->apps[$j]->label] = $decode->servers[$i]->apps[$j]->mysql_password;
  126. }
  127. }
  128. return $arr;
  129. }
  130.  
  131. $app_label = getAppsLabel($decode);
  132. $app_id = getAppsId($decode);
  133. $app_serverid = getAppsServerId($decode);
  134. $app_databasename = getAppsDatabaseName($decode);
  135. $app_databaseuser = getAppsDatabaseUser($decode);
  136. $app_databasepassword = getAppsDatabasePassword($decode);
  137.  
  138. $primary_domain = get_field( "primary_domain" );
  139. $primary_domain = str_replace(array('http://', 'https://', 'www.'), '', $primary_domain); // Remove http:// https:// www.
  140. $primary_domain = rtrim($primary_domain, '/'); // Remove trailing slash
  141. update_field('app_id', $app_id[$primary_domain]);
  142. update_field('server_id', $app_serverid[$primary_domain]);
  143. update_field('database_name', $app_databasename[$primary_domain]);
  144. update_field('database_username', $app_databaseuser[$primary_domain]);
  145. update_field('database_password', $app_databasepassword[$primary_domain]);
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement