Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use API proxy when Playgrounds option is on #177

Draft
wants to merge 29 commits into
base: 1.1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
705d697
turn off WP_DEBUG_DISPLAY
asirota Oct 16, 2024
41e72e6
Update FUNDING.yml
asirota Oct 20, 2024
0dd4b28
Update FUNDING.yml
asirota Oct 20, 2024
6ebb129
conditionally use WA api or proxy to make api reqs
nataliebrotherton Oct 30, 2024
5cf99ed
add playgrounds flag, point to this branch for testing
nataliebrotherton Oct 30, 2024
40888f1
add static keywords
nataliebrotherton Oct 30, 2024
e1548cb
update proxy url
nataliebrotherton Oct 30, 2024
d1140d8
update proxy url to include php file
nataliebrotherton Oct 31, 2024
8a1ffa6
Merge branch '1.1' of github.com:NewPath-Consulting/Wild-Apricot-Pres…
nataliebrotherton Oct 31, 2024
1be23bf
Merge branch '1.1' of github.com:NewPath-Consulting/Wild-Apricot-Pres…
nataliebrotherton Oct 31, 2024
8af2d14
Merge branch '1.1' of github.com:NewPath-Consulting/Wild-Apricot-Pres…
nataliebrotherton Nov 4, 2024
3a9da41
use proxy for license check, send data as query
nataliebrotherton Nov 21, 2024
fca2c56
update proxy url
nataliebrotherton Nov 21, 2024
5fe35e4
add data access plugin
nataliebrotherton Dec 4, 2024
f165c9c
add debug statement for api responses
nataliebrotherton Dec 4, 2024
3796bbc
debug for playgrounds
nataliebrotherton Dec 4, 2024
3d016ba
debug
nataliebrotherton Dec 4, 2024
0d4d58e
add query monitor, remove wp data access
nataliebrotherton Dec 4, 2024
94f86d0
debug
nataliebrotherton Dec 4, 2024
36964ce
update blueprint site option syntax
nataliebrotherton Dec 4, 2024
f640125
reorder site options
nataliebrotherton Dec 4, 2024
194a1b0
reorder steps in blueprint
nataliebrotherton Dec 4, 2024
284b57c
remove theme step
nataliebrotherton Dec 4, 2024
65bbf56
restore faulty steps in blueprint
nataliebrotherton Dec 4, 2024
5d42338
remove query-monitor
asirota Dec 5, 2024
4f37142
use wp_safe_remote_get
nataliebrotherton Dec 11, 2024
68e99fb
Merge branch '164-playgrounds-proxy' of github.com:NewPath-Consulting…
nataliebrotherton Dec 11, 2024
af22ec1
test playgrounds without proxy
nataliebrotherton Dec 20, 2024
2a732c8
clean up checking for dev/playgrounds
nataliebrotherton Dec 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions assets/blueprints/blueprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,46 @@
"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
},
"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": "<?php /* Use pretty permalinks */ add_action( 'after_setup_theme', function() { global $wp_rewrite; $wp_rewrite->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",
Expand Down
39 changes: 30 additions & 9 deletions src/class-addon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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',
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
80 changes: 56 additions & 24 deletions src/class-wa-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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

Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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.
*
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/class-wa-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}