diff --git a/assets/blueprints/blueprint.json b/assets/blueprints/blueprint.json index bfa8530..6228e25 100644 --- a/assets/blueprints/blueprint.json +++ b/assets/blueprints/blueprint.json @@ -13,14 +13,11 @@ "plugin" ] }, - "siteOptions": { - "blogname": "WildApricot Press Demo Site" - }, "plugins": [ - "https://github-proxy.com/proxy/?repo=NewPath-Consulting/Wild-Apricot-Press&branch=1.1", + "https://github-proxy.com/proxy/?repo=NewPath-Consulting/Wild-Apricot-Press&branch=164-playgrounds-proxy", "error-log-viewer", "generateblocks", - "plugin-check" + "plugin-check" ], "features": { "networking": true @@ -28,28 +25,34 @@ "login": true, "landingPage": "/wp-admin/admin.php?page=wawp-wal-admin", "steps": [ + { + "step": "installTheme", + "themeData": { + "resource": "wordpress.org/themes", + "slug": "generatepress" + } + }, { "step": "defineWpConfigConsts", "consts": { "WP_DISABLE_FATAL_ERROR_HANDLER": true, "WP_DEBUG": true, "WP_DEBUG_LOG": true, - "WP_DEBUG_DISPLAY": false, + "WP_DEBUG_DISPLAY": true, "WAP_LICENSE_CHECK_DEV": true } }, + { + "step": "setSiteOptions", + "options": { + "blogname": "WildApricot Press Demo Site" + } + }, { "step": "writeFile", "path": "/wordpress/wp-content/mu-plugins/rewrite.php", "data": "set_permalink_structure('/%postname%/'); $wp_rewrite->flush_rules(); } );" }, - { - "step": "installTheme", - "themeData": { - "resource": "wordpress.org/themes", - "slug": "generatepress" - } - }, { "step": "writeFile", "path": "/wordpress/wp-content/mu-plugins/bgnightly-notice.php", diff --git a/src/class-addon.php b/src/class-addon.php index 4bd2cb2..98da547 100644 --- a/src/class-addon.php +++ b/src/class-addon.php @@ -23,10 +23,12 @@ class Addon { /** - * Base hook url. + * Base hook url for the proxy. * * @var string */ + public const PROXY_HOOK_URL = 'https://npc-proxy.newpathconsulting.com/check'; + public const HOOK_URL = 'https://newpathconsulting.com/check'; /** @@ -684,15 +686,12 @@ private static function post_request($license_key) { // check for dev flag, construct appropriate url - $url = self::HOOK_URL; - if (defined('WAP_LICENSE_CHECK_DEV') && WAP_LICENSE_CHECK_DEV) { - $url = $url . 'dev'; - } + $url = self::get_license_hook_url(); - // construct array of data to send $data = array('key' => $license_key, 'json' => 1); + $url = $url . '?' . http_build_query($data); + $args = array( - 'body' => $data, 'timeout' => '5', 'redirection' => '5', 'httpversion' => '1.0', @@ -702,7 +701,10 @@ private static function post_request($license_key) ); // make post request to hook and decode response data - $response = wp_remote_post($url, $args); + $response = wp_safe_remote_get($url, $args); + if ($response['response']['code'] != '200') { + throw API_Exception::api_connection_error('There was an error validating the license key.'); + } $response_data = $response['body']; return json_decode($response_data, true); @@ -729,7 +731,7 @@ public static function check_license_properties($response, $slug) } // license is valid if license hook doesn't return an error and dev flag is on, don't need to evaluate contents - if (defined('WAP_LICENSE_CHECK_DEV') && WAP_LICENSE_CHECK_DEV) { + if (is_dev()) { return true; } @@ -889,4 +891,23 @@ public static function license_wa_auth_changed_notice($slug, $is_licensing_page) } + private static function get_license_hook_url() + { + $url = ''; + if (is_playgrounds()) { + $url = self::PROXY_HOOK_URL; + } else { + $url = self::HOOK_URL; + } + + if (is_dev()) { + if (!is_playgrounds()) { + $url = $url . '-'; + } + $url = $url . 'dev'; + } + + return $url; + } + } // end of Addon class \ No newline at end of file diff --git a/src/class-wa-api.php b/src/class-wa-api.php index 687e720..eda08cd 100644 --- a/src/class-wa-api.php +++ b/src/class-wa-api.php @@ -15,11 +15,11 @@ class WA_API { // Constants - public const ADMIN_API_VERSION = 'v2.2'; - public const MEMBER_API_VERSION = 'v1'; - public const WAP_USER_AGENT = 'WildApricotPress/1.0.2'; - public const API_URL = 'https://api.wildapricot.org/'; - // const API_URL = 'https://google.com'; + public const WAP_USER_AGENT = 'WildApricotPress/1.1'; + public const AUTH_URL = 'https://oauth.wildapricot.org/auth/token'; + public const API_URL = 'https://api.wildapricot.org/v2.2'; + public const PUBLIC_API_URL = 'https://api.wildapricot.org/publicview/v1'; + public const PROXY_URL = 'https://npc-proxy.newpathconsulting.com'; // Class variables private $access_token; @@ -54,12 +54,15 @@ public function __construct($access_token, $wa_user_id) private static function response_to_data($response) { if (is_wp_error($response)) { + Log::wap_log_debug($response); throw new API_Exception(esc_html(API_Exception::api_connection_error())); } - // if user is unauthorized, return empty array + // if user is unauthorized, throw error if ($response['response']['code'] == '401') { - return array(); + throw new API_Exception(esc_html(API_Exception::api_connection_error())); + } elseif ($response['response']['code'] == '200') { + API_Exception::remove_error(); } // Get body of response @@ -212,8 +215,8 @@ public static function create_consistent_url($original_url) public function get_account_url_and_id() { $args = $this->request_data_args(); - $url = self::API_URL . self::ADMIN_API_VERSION . '/accounts/' . $this->wa_user_id; - $response_api = wp_remote_get($url, $args); + $url = self::get_api_url() . '/accounts/' . $this->wa_user_id; + $response_api = wp_safe_remote_get($url, $args); try { $details_response = self::response_to_data($response_api); @@ -246,10 +249,12 @@ public function retrieve_custom_fields() { // Make API request for custom fields $args = $this->request_data_args(); - $url = self::API_URL . self::ADMIN_API_VERSION . '/accounts/' . + $url = self::get_api_url() . '/accounts/' . $this->wa_user_id . '/contactfields?showSectionDividers=true'; - $response_api = wp_remote_get($url, $args); + $response_api = wp_safe_remote_get($url, $args); + Log::wap_log_debug($url); + Log::wap_log_debug($args); try { $custom_field_response = self::response_to_data($response_api); } catch (API_Exception $e) { @@ -463,7 +468,7 @@ public static function get_new_access_token($refresh_token) ), 'body' => 'grant_type=refresh_token&refresh_token=' . $refresh_token ); - $response = wp_remote_post('https://oauth.wildapricot.org/auth/token', $args); + $response = wp_remote_post(self::get_auth_url(), $args); try { $data = self::response_to_data($response); @@ -484,7 +489,7 @@ public function get_info_on_current_user() // Get details of current WA user with API request // Get user's contact ID $args = $this->request_data_args(); - $contact_info = wp_remote_get(self::API_URL . self::ADMIN_API_VERSION . '/accounts/' . $this->wa_user_id . '/contacts/me?getExtendedMembershipInfo=true', $args); + $contact_info = wp_safe_remote_get(self::get_api_url() . '/accounts/' . $this->wa_user_id . '/contacts/me?getExtendedMembershipInfo=true', $args); try { $contact_info = self::response_to_data($contact_info); @@ -498,9 +503,9 @@ public function get_info_on_current_user() $user_data_api = null; if (isset($is_administrator) && $is_administrator == '1') { // user is administrator $contact_id = $contact_info['Id']; - $user_data_api = wp_remote_get(self::API_URL . self::ADMIN_API_VERSION . '/accounts/' . $this->wa_user_id . '/contacts/' . $contact_id . '?getExtendedMembershipInfo=true', $args); + $user_data_api = wp_safe_remote_get(self::get_api_url() . '/accounts/' . $this->wa_user_id . '/contacts/' . $contact_id . '?getExtendedMembershipInfo=true', $args); } else { // not administrator - $user_data_api = wp_remote_get('https://api.wildapricot.org/publicview/' . self::MEMBER_API_VERSION . '/accounts/' . $this->wa_user_id . '/contacts/me?includeDetails=true', $args); + $user_data_api = wp_safe_remote_get(self::get_public_api_url() . '/accounts/' . $this->wa_user_id . '/contacts/me?includeDetails=true', $args); } // Extract body @@ -523,11 +528,11 @@ public function get_membership_levels($request_groups = false) { $args = $this->request_data_args(); // ABSTRACT VARIABLE IN URL - $url = self::API_URL . self::ADMIN_API_VERSION . '/accounts/' . $this->wa_user_id . '/membershiplevels'; + $url = self::get_api_url() . '/accounts/' . $this->wa_user_id . '/membershiplevels'; if ($request_groups) { - $url = self::API_URL . self::ADMIN_API_VERSION . '/accounts/' . $this->wa_user_id . '/membergroups'; + $url = self::get_api_url() . '/accounts/' . $this->wa_user_id . '/membergroups'; } - $membership_levels_response = wp_remote_get($url, $args); + $membership_levels_response = wp_safe_remote_get($url, $args); // Return membership levels try { @@ -570,7 +575,7 @@ public static function is_application_valid($entered_api_key) ), 'body' => 'grant_type=client_credentials&scope=auto&obtain_refresh_token=true' ); - $response = wp_remote_post('https://oauth.wildapricot.org/auth/token', $args); + $response = wp_remote_post(self::get_auth_url(), $args); $data = self::response_to_data($response); return $data; @@ -606,7 +611,7 @@ public static function login_email_password($valid_login) 'scope' => 'auto' ) ); - $response = wp_remote_post('https://oauth.wildapricot.org/auth/token', $args); + $response = wp_remote_post(self::get_auth_url(), $args); try { $data = self::response_to_data($response); @@ -629,7 +634,7 @@ public static function login_email_password($valid_login) */ public function retrieve_contacts_list($query, $block = false, $skip = 0, $top = 200) { - $base_url = self::API_URL . self::ADMIN_API_VERSION . '/accounts/' . + $base_url = self::get_api_url() . '/accounts/' . $this->wa_user_id . '/contacts?%24async=false&%24' . $query; // return single block @@ -679,11 +684,11 @@ public function get_contacts_count() return $count; } - $url = self::API_URL . self::ADMIN_API_VERSION . '/accounts/' . + $url = self::get_api_url() . '/accounts/' . $this->wa_user_id . '/contacts?%24async=false&%24count=true'; $args = $this->request_data_args(); - $response = wp_remote_get($url, $args); + $response = wp_safe_remote_get($url, $args); try { $data = self::response_to_data($response); @@ -697,6 +702,33 @@ public function get_contacts_count() return $count; } + private static function get_auth_url() + { + if (is_playgrounds()) { + return self::PROXY_URL . '/auth'; + } + + return self::AUTH_URL; + } + + private static function get_api_url() + { + if (is_playgrounds()) { + return self::PROXY_URL; + } + + return self::API_URL; + } + + private static function get_public_api_url() + { + if (is_playgrounds()) { + return self::PROXY_URL . '/publicview'; + } + + return self::PUBLIC_API_URL; + } + /** * Requests a single block of contacts from WildApricot. * @@ -718,7 +750,7 @@ private function request_contact_block($url, $skip, $top) $args = $this->request_data_args(); - $response = wp_remote_get($url, $args); + $response = wp_safe_remote_get($url, $args); try { $data = self::response_to_data($response); diff --git a/src/class-wa-integration.php b/src/class-wa-integration.php index 5fd22e6..5b16674 100644 --- a/src/class-wa-integration.php +++ b/src/class-wa-integration.php @@ -1250,6 +1250,8 @@ public function refresh_user_wa_info() $verified_data = WA_API::verify_valid_access_token(); $admin_access_token = $verified_data['access_token']; $admin_account_id = $verified_data['wa_account_id']; + Log::wap_log_debug($admin_access_token); + Log::wap_log_debug($admin_account_id); $wawp_api = new WA_API($admin_access_token, $admin_account_id); // Refresh custom fields first $wawp_api->retrieve_custom_fields(); diff --git a/src/helpers.php b/src/helpers.php index b785c65..a1c1768 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -345,4 +345,14 @@ function refresh_credentials() // if validate_license_key returns the stored license then it's still valid Addon::update_license_check_option(CORE_SLUG, Addon::LICENSE_STATUS_VALID); return $new_license; +} + +function is_dev() +{ + return defined('WAP_LICENSE_CHECK_DEV') && WAP_LICENSE_CHECK_DEV; +} + +function is_playgrounds() +{ + return defined('WP_PLAYGROUNDS_SUPPORT') && WP_PLAYGROUNDS_SUPPORT; } \ No newline at end of file