PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 All 155 releases
← All changes | modules/api-backups/classes/class-api-backups-3rd-party.php +168 -28 6.1.3 → 6.2.2 View file →
@@ -30,8 +30,10 @@
30 30 class Api_Backups_3rd_Party { //phpcs:ignore -- NOSONAR - multi methods.
31 31
32 32 // phpcs:disable WordPress.DB.RestrictedFunctions, Generic.Metrics.CyclomaticComplexity, WordPress.WP.AlternativeFunctions, WordPress.PHP.NoSilencedErrors -- Using cURL functions.
33 33
34 + const CLOUDWAYS_API_URL = 'https://api.cloudways.com/api/v2';
35 +
34 36 /**
35 37 * Public static variable to hold the single instance of the class.
36 38 *
37 39 * @var mixed Default null
@@ -39,8 +41,15 @@
39 41 protected static $instance = null;
40 42
41 43
42 44 /**
45 + * Public static variable to hold the saved access token value.
46 + *
47 + * @var mixed Default null
48 + */
49 + protected static $access_token = null;
50 +
51 + /**
43 52 * Public static variable.
44 53 */
45 54 const OPERATION = '/operation/'; // to clean code.
46 55
@@ -1651,8 +1660,32 @@
1651 1660 <?php
1652 1661 } // END if.
1653 1662 }
1654 1663
1664 + /**
1665 + * Fetch Cloudways oAuth Access Token.
1666 + *
1667 + * Fetch the Cloudways oAuth access token from the given API credentials.
1668 + *
1669 + * @return mixed Return Cloudways Access Token.
1670 + */
1671 + public static function get_saved_v2_access_token() {
1672 + if ( null === static::$access_token ) {
1673 + static::$access_token = self::get_cloudways_access_token();
1674 + }
1675 + return static::$access_token;
1676 + }
1677 + /**
1678 + * Fetch Cloudways oAuth Access Token.
1679 + *
1680 + * Fetch the Cloudways oAuth access token from the given API credentials.
1681 + *
1682 + * @return mixed Return Cloudways Access Token.
1683 + */
1684 + public static function is_access_token_v2() {
1685 + return ! empty( self::get_saved_v2_access_token() );
1686 + }
1687 +
1655 1688 /*********************************************************************************
1656 1689 * Cloudways API Methods.
1657 1690 **********************************************************************************/
1658 1691
@@ -1665,11 +1698,68 @@
1665 1698 * 2. Enter in your account email address and API Key below.
1666 1699 *
1667 1700 * @param string $method GET|POST|PUT|DELETE.
1668 1701 * @param string $url relative URL for the call.
1702 + * @param array $post Optional post data for the call.
1703 + * @return object|false Output from CW API.
1704 + */
1705 + public static function call_cloudways_api_v2( $method, $url, $post = array() ) {
1706 +
1707 + $access_token = self::get_saved_v2_access_token();
1708 +
1709 + $ch = curl_init();
1710 +
1711 + curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $method );
1712 + curl_setopt( $ch, CURLOPT_URL, self::CLOUDWAYS_API_URL . $url );
1713 + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
1714 +
1715 + curl_setopt(
1716 + $ch,
1717 + CURLOPT_HTTPHEADER,
1718 + array(
1719 + 'Authorization: Bearer ' . $access_token,
1720 + 'Accept: application/json',
1721 + )
1722 + );
1723 +
1724 + // Set Post Parameters.
1725 + $encoded = '';
1726 + if ( count( $post ) ) {
1727 + foreach ( $post as $name => $value ) {
1728 + $encoded .= rawurlencode( $name ) . '=' . rawurlencode( $value ) . '&';
1729 + }
1730 +
1731 + $encoded = substr( $encoded, 0, strlen( $encoded ) - 1 );
1732 +
1733 + curl_setopt( $ch, CURLOPT_POSTFIELDS, $encoded );
1734 + curl_setopt( $ch, CURLOPT_POST, 1 );
1735 + }
1736 +
1737 + $output = curl_exec( $ch );
1738 + $httpcode = (int) curl_getinfo( $ch, CURLINFO_HTTP_CODE );
1739 +
1740 + curl_close( $ch );
1741 +
1742 + if ( 200 !== $httpcode ) {
1743 + return false;
1744 + }
1745 +
1746 + return json_decode( $output );
1747 + }
1748 +
1749 + /**
1750 + * Call Cloudways API, Authenticate & perform given method.
1751 + *
1752 + * Use this function to contact CW API
1753 + * We use OAuth, an open standard for authorization. Here are the steps involved:
1754 + * 1. Get your API Key from here: https://platform.cloudways.com/api
1755 + * 2. Enter in your account email address and API Key below.
1756 + *
1757 + * @param string $method GET|POST|PUT|DELETE.
1758 + * @param string $url relative URL for the call.
1669 1759 * @param string $accessToken Access token generated using OAuth Call.
1670 1760 * @param array $post Optional post data for the call.
1671 - * @return object Output from CW API.
1761 + * @return object|false Output from CW API.
1672 1762 */
1673 1763 public static function call_cloudways_api( $method, $url, $accessToken, $post = array() ) {
1674 1764 $baseURL = 'https://api.cloudways.com/api/v1';
1675 1765
@@ -1698,12 +1788,14 @@
1698 1788 $output = curl_exec( $ch );
1699 1789
1700 1790 $httpcode = (string) curl_getinfo( $ch, CURLINFO_HTTP_CODE );
1701 1791
1792 + curl_close( $ch );
1793 +
1702 1794 if ( '200' !== $httpcode ) {
1703 1795 return false;
1704 1796 }
1705 - curl_close( $ch );
1797 +
1706 1798 return json_decode( $output );
1707 1799 }
1708 1800
1709 1801 /**
@@ -1738,13 +1830,19 @@
1738 1830 *
1739 1831 * @return mixed Returns available list of servers.
1740 1832 */
1741 1833 public static function fetch_cloudways_server_list() { //phpcs:ignore -- NOSONAR - complex.
1742 - // Grab access token.
1743 - $accessToken = static::fetch_cloudways_access_token();
1744 1834
1745 - // Fetch Server List.
1746 - $serverList = (array) static::call_cloudways_api( 'GET', '/server', $accessToken );
1835 + if ( static::is_access_token_v2() ) {
1836 + $serverList = (array) static::call_cloudways_api_v2( 'GET', '/server' );
1837 + } else {
1838 + // Grab access token.
1839 + $accessToken = static::fetch_cloudways_access_token();
1840 +
1841 + // Fetch Server List.
1842 + $serverList = (array) static::call_cloudways_api( 'GET', '/server', $accessToken );
1843 + }
1844 +
1747 1845 if ( ! array_key_exists( 'servers', $serverList ) ) {
1748 1846 return;
1749 1847 }
1750 1848 // Return the server list.
@@ -1860,21 +1958,34 @@
1860 1958 'server_id' => $server_id,
1861 1959 'app_id' => $app_id,
1862 1960 );
1863 1961
1864 - // Grab Cloudways access token.
1865 - $accessToken = static::fetch_cloudways_access_token();
1962 + $accessToken = '';
1866 1963
1867 - // Send Payload & create backup.
1868 - $api_response = (array) static::call_cloudways_api( 'POST', '/app/manage/takeBackup', $accessToken, $data );
1964 + if ( static::is_access_token_v2() ) {
1965 + $api_response = (array) static::call_cloudways_api_v2( 'POST', '/app/manage/takeBackup', $data );
1966 + } else {
1869 1967
1968 + // Grab Cloudways access token.
1969 + $accessToken = static::fetch_cloudways_access_token();
1970 +
1971 + // Send Payload & create backup.
1972 + $api_response = (array) static::call_cloudways_api( 'POST', '/app/manage/takeBackup', $accessToken, $data );
1973 + }
1974 +
1870 1975 $success = true;
1871 1976
1872 1977 if ( true !== $api_response['status'] ) {
1873 1978 $success = false;
1874 1979 } else {
1875 - // Save current backup operation.
1876 - $backup_operation = (array) static::call_cloudways_api( 'GET', static::OPERATION . $api_response['operation_id'], $accessToken );
1980 + if ( static::is_access_token_v2() ) {
1981 + // Save current backup operation.
1982 + $backup_operation = (array) static::call_cloudways_api_v2( 'GET', static::OPERATION . $api_response['operation_id'] );
1983 + } else {
1984 + // Save current backup operation.
1985 + $backup_operation = (array) static::call_cloudways_api( 'GET', static::OPERATION . $api_response['operation_id'], $accessToken );
1986 + }
1987 +
1877 1988 $backup_operation = wp_json_encode( $backup_operation['operation'] );
1878 1989 Api_Backups_Helper::update_website_option( $website_id, 'mainwp_3rd_party_cloudways_backup_operation', $backup_operation );
1879 1990
1880 1991 // Save Timestamp.
@@ -1923,13 +2034,19 @@
1923 2034 // Current backup operation.
1924 2035 $backup_operation = isset( $site_options['mainwp_3rd_party_cloudways_backup_operation'] ) ? $site_options['mainwp_3rd_party_cloudways_backup_operation'] : null;
1925 2036 $operation_id = json_decode( $backup_operation, true )['id'];
1926 2037
1927 - // Grab Cloudways access token.
1928 - $accessToken = static::fetch_cloudways_access_token();
2038 + if ( static::is_access_token_v2() ) {
2039 + // Send Payload & list backups. https://api.cloudways.com/api/v1/operation/{id}.
2040 + $operation_polling = static::call_cloudways_api_v2( 'GET', static::OPERATION . $operation_id );
2041 + } else {
1929 2042
1930 - // Send Payload & list backups. https://api.cloudways.com/api/v1/operation/{id}.
1931 - $operation_polling = static::call_cloudways_api( 'GET', static::OPERATION . $operation_id, $accessToken );
2043 + // Grab Cloudways access token.
2044 + $accessToken = static::fetch_cloudways_access_token();
2045 +
2046 + // Send Payload & list backups. https://api.cloudways.com/api/v1/operation/{id}.
2047 + $operation_polling = static::call_cloudways_api( 'GET', static::OPERATION . $operation_id, $accessToken );
2048 + }
1932 2049 $backup_polling_response = $operation_polling->operation;
1933 2050
1934 2051 // If backup has not been completed yet, sleep for 30 seconds and check again.
1935 2052 // If backup has been completed, save the backup operation results.
@@ -1965,10 +2082,8 @@
1965 2082 // Grab $_GET['data'] from url if not Ajax call.
1966 2083 $website_id = isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0; //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended
1967 2084 }
1968 2085
1969 - // Grab Cloudways access token.
1970 - $accessToken = static::fetch_cloudways_access_token();
1971 2086
1972 2087 // Grab needed site options from WP_MAINWP_WP_OPTIONS table.
1973 2088 $site_options = Api_Backups_Helper::get_website_options(
1974 2089 $website_id,
@@ -1979,18 +2094,31 @@
1979 2094 );
1980 2095 $server_id = isset( $site_options['mainwp_3rd_party_instance_id'] ) ? $site_options['mainwp_3rd_party_instance_id'] : null;
1981 2096 $app_id = isset( $site_options['mainwp_3rd_party_app_id'] ) ? $site_options['mainwp_3rd_party_app_id'] : null;
1982 2097
1983 - // Grab Backup operation from API.
1984 - $backup_operation = static::call_cloudways_api( 'GET', '/app/manage/backup?server_id=' . $server_id . '&app_id=' . $app_id, $accessToken );
1985 - $operation_id = $backup_operation->operation_id;
2098 + if ( static::is_access_token_v2() ) {
2099 + // Grab Backup operation from API.
2100 + $backup_operation = static::call_cloudways_api_v2( 'GET', '/app/manage/backup?server_id=' . $server_id . '&app_id=' . $app_id );
2101 + } else {
2102 + // Grab Cloudways access token.
2103 + $accessToken = static::fetch_cloudways_access_token();
2104 + // Grab Backup operation from API.
2105 + $backup_operation = static::call_cloudways_api( 'GET', '/app/manage/backup?server_id=' . $server_id . '&app_id=' . $app_id, $accessToken );
2106 + }
1986 2107
2108 + $operation_id = $backup_operation->operation_id;
2109 +
1987 2110 sleep( 3 ); // REQUIRED! Wait 3 seconds for cURL connection to be closed before opening a new one.
1988 2111
1989 - // Send Payload & list backups. https://api.cloudways.com/api/v1/operation/{id}.
1990 - $operation_polling = static::call_cloudways_api( 'GET', static::OPERATION . $operation_id, $accessToken );
1991 - $operation = $operation_polling->operation;
2112 + if ( static::is_access_token_v2() ) {
2113 + $operation_polling = static::call_cloudways_api_v2( 'GET', static::OPERATION . $operation_id );
2114 + } else {
2115 + // Send Payload & list backups. https://api.cloudways.com/api/v1/operation/{id}.
2116 + $operation_polling = static::call_cloudways_api( 'GET', static::OPERATION . $operation_id, $accessToken );
2117 + }
1992 2118
2119 + $operation = $operation_polling->operation;
2120 +
1993 2121 // Save backup operation.
1994 2122 Api_Backups_Helper::update_website_option( $website_id, 'mainwp_3rd_party_cloudways_available_backups', $operation->parameters );
1995 2123
1996 2124 Api_Backups_Utility::save_lasttime_backup( $website_id, $operation->parameters, 'cloudways' );
@@ -2038,13 +2166,17 @@
2038 2166 'time' => $backup_date,
2039 2167 'type' => 'complete',
2040 2168 );
2041 2169
2042 - // Grab Cloudways access token.
2043 - $accessToken = static::fetch_cloudways_access_token();
2170 + if ( static::is_access_token_v2() ) {
2171 + $api_response = static::call_cloudways_api_v2( 'POST', '/app/manage/restore', $data );
2172 + } else {
2173 + // Grab Cloudways access token.
2174 + $accessToken = static::fetch_cloudways_access_token();
2044 2175
2045 - // Send Payload & create backup.
2046 - $api_response = static::call_cloudways_api( 'POST', '/app/manage/restore', $accessToken, $data );
2176 + // Send Payload & create backup.
2177 + $api_response = static::call_cloudways_api( 'POST', '/app/manage/restore', $accessToken, $data );
2178 + }
2047 2179
2048 2180 // Handle API response.
2049 2181 if ( 1 !== (int) $api_response->status ) {
2050 2182 wp_die( 'false' );
@@ -2233,8 +2365,16 @@
2233 2365 */
2234 2366 public static function get_cloudways_api_key() {
2235 2367 return Api_Backups_Utility::get_instance()->get_api_key( 'cloudways' );
2236 2368 }
2369 +
2370 + /**
2371 + * Method get_cloudways_access_token().
2372 + */
2373 + public static function get_cloudways_access_token() {
2374 + return Api_Backups_Utility::get_instance()->get_api_key( 'cloudways', '', 'access_token' );
2375 + }
2376 +
2237 2377
2238 2378 /**
2239 2379 * Method get_cpanel_account_password().
2240 2380 */