From 891d013415aff36e5ac48544b48c6e2246dd38b0 Mon Sep 17 00:00:00 2001 From: Natalie Brotherton Date: Tue, 5 Nov 2024 17:34:54 -0500 Subject: [PATCH] Issues 145, 152, 163 - add WA login settings (#173) --- css/wawp-styles-admin.css | 12 +- src/admin-settings.php | 372 ++++++++++++++++++++++++----------- src/class-wa-api.php | 27 ++- src/class-wa-integration.php | 53 ++++- 4 files changed, 335 insertions(+), 129 deletions(-) diff --git a/css/wawp-styles-admin.css b/css/wawp-styles-admin.css index 50be6cc..0a3fcf3 100644 --- a/css/wawp-styles-admin.css +++ b/css/wawp-styles-admin.css @@ -59,8 +59,8 @@ /* Styling for CSS textarea */ .wawp_user_style_input { font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; - width: 50vw; - height: 60vh; + width: 40vw; + height: 40vh; white-space: nowrap; overflow-y: scroll; padding: 15px; @@ -75,3 +75,11 @@ ul.wawp_list { .form-table th { width: 100px; } + +input.wap-login-settings#login-title { + width: 20vw; +} + +#reset { + margin-left: 5px; +} diff --git a/src/admin-settings.php b/src/admin-settings.php index 7903d56..bb83eb8 100644 --- a/src/admin-settings.php +++ b/src/admin-settings.php @@ -25,7 +25,7 @@ class Settings private $admin_settings; private $wa_auth_settings; private $license_settings; - private $style_settings; + private $support_page; /** * Adds actions and includes files @@ -38,7 +38,7 @@ public function __construct() $this->admin_settings = new Admin_Settings(); $this->wa_auth_settings = new WA_Auth_Settings(); $this->license_settings = new License_Settings(); - $this->style_settings = new Style_Settings(); + $this->support_page = new Support_Page(); // Activate option in table if it does not exist yet // Currently, there is a WordPress bug that calls the 'sanitize' function twice if the option is not already in the database @@ -237,7 +237,7 @@ public function add_settings_page() $this->wa_auth_settings->add_submenu_page(); $this->license_settings->add_submenu_page(); - $this->style_settings->add_submenu_page(); + $this->support_page->add_submenu_page(); } @@ -251,7 +251,6 @@ public function page_init() $this->wa_auth_settings->register_setting_add_fields(); $this->license_settings->register_setting_add_fields(); $this->admin_settings->register_setting_add_fields(); - $this->style_settings->register_setting_add_fields(); } } @@ -271,6 +270,12 @@ class Admin_Settings public const DELETE_DB_DATA = 'delete_db_data'; public const DELETE_USER_DATA = 'delete_user_data'; + public const STYLE_OPTION_GROUP = 'wap_styles_group'; + public const STYLE_SUBMENU_PAGE = 'wap-styles-submenu'; + public const STYLE_SECTION = 'wap_styles_section'; + public const STYLE_OPTION_NAME = 'wawp_user_style'; + public const CSS_FILE_PATH = 'css/wawp-styles-user.css'; + public function __construct() { } @@ -321,6 +326,10 @@ public function register_setting_add_fields() $this->register_deletion_option(); $this->register_logfile_option(); + // style options + $this->register_user_style(); + $this->register_login_page_settings(); + } /** @@ -343,7 +352,8 @@ public function create_admin_page() ?>
nav-tab-active">Plug break; case 'plugin': $this->create_plugin_options_tab(); break; + case 'style': $this->create_login_settings_tab(); + break; default: $this->create_content_restriction_options_tab(); endswitch; ?> @@ -527,7 +542,8 @@ public function restriction_status_input_box() } ?> /> + value="" + />
- />

Your WildApricot site does not have any contact fields! Please ensure that you have correctly entered your - WildApricot site's credentials under WildApricot + WildApricot site's credentials under WildApricot Press -> Authorization

- />

-> +> +
+ + /> + $editor_name, 'tinymce' => true); + + wp_editor($initial_content, $editor_id, $editor_settings); + } + + public function login_submit_callback() + { + $current = WA_Integration::get_login_settings('submit'); + ?> + /> WA_Integration::LOGIN_DEFAULT_TITLE, + 'intro' => WA_Integration::LOGIN_DEFAULT_INTRO, + 'submit' => WA_Integration::LOGIN_DEFAULT_SUBMIT + ); + } + + $input['title'] = sanitize_text_field($input['title']); + $input['intro'] = sanitize_textarea_field($input['intro']); + $input['submit'] = sanitize_text_field($input['submit']); + + return $input; + + } + + public function login_settings_print_info() + { + print 'Enter custom text for elements of the user login page here.'; + } + + + // settings on the content restriction options tab /** * Register settings and add fields for the login button menu location. @@ -1063,6 +1177,82 @@ private function register_logfile_option() ); } + private function register_user_style() + { + $register_args = array( + 'type' => 'string', + 'sanitize_callback' => array( $this, 'user_style_sanitize'), + 'default' => null + ); + + register_setting( + self::STYLE_OPTION_GROUP, + self::STYLE_OPTION_NAME, + $register_args + ); + + add_settings_section( + self::STYLE_SECTION, + 'Customize Look and Feel', + array($this, 'user_styles_print_section_info'), + self::STYLE_SUBMENU_PAGE + ); + + add_settings_field( + self::STYLE_OPTION_NAME, + 'Custom CSS', + array($this, 'user_style_callback'), + self::STYLE_SUBMENU_PAGE, + self::STYLE_SECTION + ); + } + + private function register_login_page_settings() + { + $register_args = array( + 'type' => 'string', + 'sanitize_callback' => array( $this, 'login_settings_sanitize'), + 'default' => null + ); + + register_setting( + 'wap_login_settings_group', + WA_Integration::LOGIN_SETTINGS, + $register_args + ); + + add_settings_section( + 'wap_login_settings_section', + 'Customize Page Text', + array($this, 'login_settings_print_info'), + 'login_settings_submenu' + ); + + add_settings_field( + WA_Integration::LOGIN_SETTINGS_TITLE, + 'Page Title', + array($this, 'login_title_callback'), + 'login_settings_submenu', + 'wap_login_settings_section' + ); + + add_settings_field( + WA_Integration::LOGIN_SETTINGS_INTRO, + 'Introduction', + array($this, 'login_intro_callback'), + 'login_settings_submenu', + 'wap_login_settings_section' + ); + + add_settings_field( + WA_Integration::LOGIN_SETTINGS_SUBMIT, + 'Submit button', + array($this, 'login_submit_callback'), + 'login_settings_submenu', + 'wap_login_settings_section' + ); + } + /** * Render content for the content restriction tab. * @@ -1162,6 +1352,47 @@ private function create_plugin_options_tab() +
+
+
+ +
+
+ +
+
+
'string', - 'sanitize_callback' => array( $this, 'sanitize'), - 'default' => null - ); - - register_setting( - self::OPTION_GROUP, - self::OPTION_NAME, - $register_args - ); - - add_settings_section( - self::SECTION, - '', - array($this, 'user_styles_print_section_info'), - self::SUBMENU_PAGE - ); - - add_settings_field( - self::OPTION_NAME, - 'Custom CSS', - array($this, 'user_style_callback'), - self::SUBMENU_PAGE, - self::SECTION - ); + echo '

Support

Support for this plugin is available in two ways:

Community support: https://talk.newpathconsulting.com
In-person real-time support: https://newpathconsulting.com/hero
'; } - - public function create_user_styles_page() - { - - - // create form w/ content - - ?> -
-

Custom User Style

-
-
- -
-
-
-
-accept_terms_of_use($valid_login['email']); + // first need to accept terms of use // Get decrypted credentials $decrypted_credentials = self::load_user_credentials(); @@ -738,4 +740,25 @@ private function get_yesterdays_date() { return date('Y-m-d', strtotime("-1 days")); } -} \ No newline at end of file + + private function accept_terms_of_use($user_email) + { + // find contact with this email + $url = self::API_URL . self::ADMIN_API_VERSION . '/accounts/' . + $this->wa_user_id . '/contacts?%24async=false&filter=Email%20eq%20' . $user_email; + + $args = $this->request_data_args(); + $response = wp_remote_get($url, $args); + $data = self::response_to_data($response); + + $user_id = ''; + if (array_key_exists('Contacts', $data)) { + $user_id = $data['Contacts']['MembershipLevel']['Id']; + } else { + return; + } + + $rpc_url = self::API_URL . self::ADMIN_API_VERSION . '/rpc/' . $user_id . 'AcceptTermsOfUse'; + wp_remote_post($rpc_url, $args); + } +} diff --git a/src/class-wa-integration.php b/src/class-wa-integration.php index 5fd22e6..c69d8e3 100644 --- a/src/class-wa-integration.php +++ b/src/class-wa-integration.php @@ -280,6 +280,17 @@ class WA_Integration */ public const LOGIN_PAGE_ID_OPT = 'wawp_wal_page_id'; + // TODO: add comments + public const LOGIN_SETTINGS = 'wap_login_settings'; + + public const LOGIN_SETTINGS_TITLE = 'title'; + public const LOGIN_SETTINGS_INTRO = 'intro'; + public const LOGIN_SETTINGS_SUBMIT = 'submit'; + + public const LOGIN_DEFAULT_TITLE = 'Login with your WildApricot credentials'; + public const LOGIN_DEFAULT_INTRO = 'Log into your WildApricot account here to access content exclusive to WildApricot members!'; + public const LOGIN_DEFAULT_SUBMIT = 'Submit'; + // Custom hook names /** * User data refresh hook. Scheduled to run daily. @@ -555,6 +566,31 @@ public static function setup_license_check_cron() } } + public static function get_login_settings(?string $idx = '') + { + $login = get_option(self::LOGIN_SETTINGS); + + $default = array( + 'title' => self::LOGIN_DEFAULT_TITLE, + 'intro' => self::LOGIN_DEFAULT_INTRO, + 'submit' => self::LOGIN_DEFAULT_SUBMIT + ); + + if (!$idx && !$login) { + // return whole array and login option is not set yet --> default array + return $default; + } elseif ($idx && !$login) { + return $default[$idx]; + } elseif ($idx && $login && (!array_key_exists($idx, $login) || empty($login[$idx]))) { + // return option for idx but index option is not set --> default idx + return $default[$idx]; + } + + // return login option for idx + return $login[$idx]; + + } + /** * Creates user-facing WildApricot login page. Runs when both API key * and license key are found to be valid. @@ -572,7 +608,7 @@ public function create_login_page() // schedule cron update for updating the membership levels and groups Settings::setup_cron_job(); - $login_title = 'Login with your WildApricot credentials'; + $login_title = self::get_login_settings('title'); $login_content = '[wawp_custom_login_form]'; $post_details = array( @@ -1330,6 +1366,8 @@ public function add_user_to_wp_database($login_data, $login_email, $remember_use // Get user ID $wild_apricot_user_id = $contact_info['Id']; + // TODO: roles not synced + // Check if WA email exists in the WP user database $current_wp_user_id = 0; if (email_exists($login_email)) { // email exists; we will update user @@ -1504,7 +1542,11 @@ public function create_user_and_redirect() // Check if login is valid and add the user to wp database if it is try { - $login_attempt = WA_API::login_email_password($valid_login); + $verified_data = WA_API::verify_valid_access_token(); + $admin_access_token = $verified_data['access_token']; + $admin_account_id = $verified_data['wa_account_id']; + $wawp_api = new WA_API($admin_access_token, $admin_account_id); + $login_attempt = $wawp_api->login_email_password($valid_login); if (!$login_attempt) { add_filter('the_content', array($this, 'add_login_error')); return; @@ -1557,10 +1599,10 @@ public function custom_login_form_shortcode() // if WA user is not logged in, display login form if (!self::is_wa_user_logged_in()) { // Create page content -> login form + $login_settings = self::get_login_settings(); ?>
-

Log into your WildApricot account here to access content exclusive to WildApricot - members!

+

@@ -1579,7 +1621,8 @@ public function custom_login_form_shortcode() href="" target="_blank" rel="noopener noreferrer">Forgot Password? -
+
/>