From 343465d85479ff699b542337166f2d3280e35ec1 Mon Sep 17 00:00:00 2001 From: David Stone Date: Sat, 11 Jan 2025 09:39:16 -0700 Subject: [PATCH] Add newsletter --- inc/class-newsletter.php | 74 ++++++++++++++++++++++++++++++++++++++ inc/class-settings.php | 40 +++++++++++++-------- inc/class-wp-ultimo.php | 2 ++ inc/functions/settings.php | 4 +-- 4 files changed, 104 insertions(+), 16 deletions(-) create mode 100644 inc/class-newsletter.php diff --git a/inc/class-newsletter.php b/inc/class-newsletter.php new file mode 100644 index 0000000..b96c72c --- /dev/null +++ b/inc/class-newsletter.php @@ -0,0 +1,74 @@ + __('Signup for WP Multisite WaaS Newsletter', 'wp-ultimo'), + 'desc' => __('Be informed of new releases and all things related to running a WaaS Network.', 'wp-ultimo'), + 'type' => 'toggle', + 'value' => '1', + ), + 45 + ); + } + + + /** + * Fix stripe settings + * + * @since 2.0.18 + * + * @param array $settings The final settings array being saved, containing ALL options. + * @param array $settings_to_save Array containing just the options being updated. + * @param array $saved_settings Array containing the original settings. + * @return array + */ + public function maybe_update_newsletter_subscription($settings, $settings_to_save, $saved_settings) { + + if ( isset($settings_to_save[ self::SETTING_FIELD_SLUG ]) && $settings_to_save[ self::SETTING_FIELD_SLUG ] && $settings_to_save[ self::SETTING_FIELD_SLUG ] != $saved_settings[ self::SETTING_FIELD_SLUG ] ) { + + $response = wp_remote_post( 'https://wpmultisitewaas.org/wp-json/newsletter/v2/subscribers', [ + 'method' => 'PUT', + 'body' => wp_json_encode( [ + 'email' => $settings['company_email'], + 'status' => 'confirmed', + 'first_name' => $settings['company_name'], + 'country' => $settings['company_country'], + ] ), + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + 'Authorization' => 'Basic ' . base64_encode( '30220d7fb4ec49a7410b3a309b9346c18410bd56:0407cd731d6f074cd0b96f2643b7619e89af1ed2' ), + ], + ] ); + } elseif( empty($settings_to_save[ self::SETTING_FIELD_SLUG ]) && ! empty( $saved_settings[ self::SETTING_FIELD_SLUG ] ) ) { + $response = wp_remote_post( 'https://wpmultisitewaas.org/wp-json/newsletter/v2/subscribers', [ + 'method' => 'PUT', + 'body' => wp_json_encode( [ + 'email' => $settings['company_email'], + 'status' => 'unsubscribed', + ] ), + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + 'Authorization' => 'Basic ' . base64_encode( '30220d7fb4ec49a7410b3a309b9346c18410bd56:0407cd731d6f074cd0b96f2643b7619e89af1ed2' ), + ], + ] ); + } + + return $settings; + + } // end fix_saving_settings; +} diff --git a/inc/class-settings.php b/inc/class-settings.php index bc39288..f29cdf1 100644 --- a/inc/class-settings.php +++ b/inc/class-settings.php @@ -412,7 +412,7 @@ class Settings { * @param array $atts Field attributes such as title, description, tooltip, default value, etc. * @return void */ - public function add_field($section_slug, $field_slug, $atts) { + public function add_field($section_slug, $field_slug, $atts, $priority = 10) { /* * Adds the field to the desired fields array. */ @@ -529,7 +529,7 @@ class Settings { return $fields; - }); + }, $priority ); $settings = $this->get_all(); /* @@ -567,28 +567,36 @@ class Settings { 'title' => __('Your Business', 'wp-ultimo'), 'desc' => __('General information about your business..', 'wp-ultimo'), 'type' => 'header', - )); + ), + 10 + ); $this->add_field('general', 'company_name', array( 'title' => __('Company Name', 'wp-ultimo'), 'desc' => __('This name is used when generating invoices, for example.', 'wp-ultimo'), 'type' => 'text', 'default' => get_network_option(null, 'site_name'), - )); + ), + 20 + ); $this->add_field('general', 'company_logo', array( 'title' => __('Upload Company Logo', 'wp-ultimo'), 'desc' => __('Add your company logo to be used on the login page and other places.', 'wp-ultimo'), 'type' => 'image', 'default' => '', - )); + ), + 30 + ); $this->add_field('general', 'company_email', array( 'title' => __('Company Email Address', 'wp-ultimo'), 'desc' => __('This email is used when generating invoices, for example.', 'wp-ultimo'), 'type' => 'text', 'default' => get_network_option(null, 'admin_email'), - )); + ), + 40 + ); $this->add_field('general', 'company_address', array( 'title' => __('Company Address', 'wp-ultimo'), @@ -599,7 +607,8 @@ class Settings { 'html_attr' => array( 'rows' => 5, ), - )); + ), + 50); $this->add_field('general', 'company_country', array( 'title' => __('Company Country', 'wp-ultimo'), @@ -607,13 +616,14 @@ class Settings { 'type' => 'select', 'options' => 'wu_get_countries', 'default' => array($this, 'get_default_company_country'), - )); + ), 60); $this->add_field('general', 'currency_header', array( 'title' => __('Currency Options', 'wp-ultimo'), 'desc' => __('The following options affect how prices are displayed on the frontend, the backend and in reports.', 'wp-ultimo'), 'type' => 'header', - )); + ), 70 + ); $this->add_field('general', 'currency_symbol', array( 'title' => __('Currency', 'wp-ultimo'), @@ -621,7 +631,8 @@ class Settings { 'type' => 'select', 'default' => 'USD', 'options' => 'wu_get_currencies', - )); + ), 80 + ); $this->add_field('general', 'currency_position', array( 'title' => __('Currency Position', 'wp-ultimo'), @@ -635,14 +646,15 @@ class Settings { '%s %v' => __('Left with space ($ 99.99)', 'wp-ultimo'), '%v %s' => __('Right with space (99.99 $)', 'wp-ultimo'), ) - )); + ), 90 + ); $this->add_field('general', 'decimal_separator', array( 'title' => __('Decimal Separator', 'wp-ultimo'), 'desc' => __('This setting affects all prices displayed across the plugin elements.', 'wp-ultimo'), 'type' => 'text', 'default' => '.', - )); + ), 100); $this->add_field('general', 'thousand_separator', array( 'title' => __('Thousand Separator', 'wp-ultimo'), @@ -650,7 +662,7 @@ class Settings { 'type' => 'text', 'default' => ',', 'raw' => true - )); + ),110); $this->add_field('general', 'precision', array( 'title' => __('Number of Decimals', 'wp-ultimo'), @@ -658,7 +670,7 @@ class Settings { 'type' => 'number', 'default' => '2', 'min' => 0, - )); + ), 120); /* * Login & Registration diff --git a/inc/class-wp-ultimo.php b/inc/class-wp-ultimo.php index adcdd4c..a9fb191 100644 --- a/inc/class-wp-ultimo.php +++ b/inc/class-wp-ultimo.php @@ -145,6 +145,8 @@ final class WP_Ultimo { */ $this->settings = WP_Ultimo\Settings::get_instance(); + WP_Ultimo\Newsletter::get_instance(); + /* * Check if the WP Multisite WaaS requirements are present. * diff --git a/inc/functions/settings.php b/inc/functions/settings.php index 5beba8b..55a3001 100644 --- a/inc/functions/settings.php +++ b/inc/functions/settings.php @@ -88,9 +88,9 @@ function wu_register_settings_section($section_slug, $atts) { * @param array $atts Field attributes such as title, description, tooltip, default value, etc. * @return void */ -function wu_register_settings_field($section_slug, $field_slug, $atts) { +function wu_register_settings_field($section_slug, $field_slug, $atts, $priority = 10) { - WP_Ultimo()->settings->add_field($section_slug, $field_slug, $atts); + WP_Ultimo()->settings->add_field($section_slug, $field_slug, $atts, $priority); } // end wu_register_settings_field;