' . esc_html( $notice ) . '

'; return; } echo '

' . esc_html( $notice ) . '

'; } /** * Render the settings section: notice, safe-mode banner and the save form. * Shared by this page and the Settings tab of the Connect page. * * @param string $return_tab Connect page tab to return to after saving. * @return void */ public static function render_settings( $return_tab = 'settings' ) { $safe_mode = Aha_Is_Woocommerce_Site_Identity::is_safe_mode(); $sync_paused = class_exists( 'Aha_Is_Woocommerce_Connect_Credentials' ) && Aha_Is_Woocommerce_Connect_Credentials::is_paused(); $sync_on = Aha_Is_Woocommerce_Settings::is_product_sync_enabled(); if ( $safe_mode ) { printf( '

%s %s

', esc_html__( 'Safe mode is on.', 'aha-is-for-woocommerce' ), esc_html( sprintf( /* translators: %s: reason code */ __( 'The plugin sends nothing to Aha and refuses Aha orders. Reason: %s', 'aha-is-for-woocommerce' ), Aha_Is_Woocommerce_Site_Identity::safe_mode_reason() ) ) ); } echo '
'; wp_nonce_field( self::NONCE_SAVE ); echo ''; self::return_tab_field( $return_tab ); if ( ! $sync_paused ) { echo ''; } printf( '', checked( true, $sync_on, false ), $sync_paused ? 'disabled' : '', esc_html__( 'Product sync', 'aha-is-for-woocommerce' ), esc_html__( 'Keep your products on Aha up to date. Changes to product details, prices and stock sync automatically.', 'aha-is-for-woocommerce' ), $sync_paused ? '' . esc_html__( 'Paused by Aha. Contact Aha support.', 'aha-is-for-woocommerce' ) . '' : ( $sync_on ? '' : '' . esc_html__( 'Automatic sync is off. Use "Sync products now" on the Overview tab to update Aha.', 'aha-is-for-woocommerce' ) . '' ) ); echo '

'; echo '
'; } /** * Hidden field that sends a form result back to a Connect page tab. * * @param string $tab Tab name. * @return void */ private static function return_tab_field( $tab ) { if ( '' !== $tab ) { echo ''; } } /** * The connection test button. * * @param string $return_tab Connect page tab to return to. * @return void */ public static function render_test_button( $return_tab = 'diagnostics' ) { echo '
'; wp_nonce_field( self::NONCE_TEST ); echo ''; self::return_tab_field( $return_tab ); echo ''; echo '
'; } /** * Save the form. * * @return void */ public function handle_save() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'You cannot change these settings.', 'aha-is-for-woocommerce' ) ); } check_admin_referer( self::NONCE_SAVE ); $values = array(); if ( isset( $_POST['product_sync_field'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification -- checked above. // Only posted when the box was rendered enabled; a paused (disabled) box keeps the stored value. $values['product_sync_enabled'] = isset( $_POST['product_sync_enabled'] ) && 'yes' === wp_unslash( $_POST['product_sync_enabled'] ) ? 'yes' : 'no'; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput -- nonce checked by check_admin_referer() above; compared to the literal yes } $was_sync_on = Aha_Is_Woocommerce_Settings::is_product_sync_enabled(); Aha_Is_Woocommerce_Settings::update( $values ); Aha_Is_Woocommerce_Site_Identity::ensure_stored(); if ( ! $was_sync_on && Aha_Is_Woocommerce_Settings::is_product_sync_enabled() && class_exists( 'Aha_Is_Woocommerce_Connect_Product_Events' ) ) { Aha_Is_Woocommerce_Connect_Product_Events::catch_up(); // Changes made while it was off were skipped. } if ( $was_sync_on !== Aha_Is_Woocommerce_Settings::is_product_sync_enabled() && class_exists( 'Aha_Is_Woocommerce_Connect_Credentials' ) && Aha_Is_Woocommerce_Connect_Credentials::has_credentials() ) { try { Aha_Is_Woocommerce_Connect_Credentials::connection_status(); // Tell Aha the new value now; the result is ignored. } catch ( \Throwable $e ) { unset( $e ); // Never break the save. } } $notice = __( 'Settings saved.', 'aha-is-for-woocommerce' ); $this->redirect_back( $notice ); } /** * Run a connection test. * * @return void */ public function handle_test() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'You cannot change these settings.', 'aha-is-for-woocommerce' ) ); } check_admin_referer( self::NONCE_TEST ); if ( $this->test_safe_mode() ) { $this->redirect_back( __( 'Safe mode is on. The site address changed or this is a copied site, so calls to Aha are paused.', 'aha-is-for-woocommerce' ), 'error' ); } $no_answer = __( "No answer from Aha. Check the server's internet access and try again.", 'aha-is-for-woocommerce' ); $could_not = __( 'Aha could not be reached.', 'aha-is-for-woocommerce' ); $link = $this->test_connect_status(); if ( 'active' === $link && Aha_Is_Woocommerce_Connect_Credentials::is_paused() ) { $this->redirect_back( __( 'Connected, but Aha has paused sync for this store. Products and orders do not sync until Aha resumes it.', 'aha-is-for-woocommerce' ), 'success' ); } elseif ( 'active' === $link ) { $this->redirect_back( __( 'Connected. Aha answered this store.', 'aha-is-for-woocommerce' ), 'success' ); } elseif ( 'revoked' === $link ) { $this->redirect_back( $could_not . ' ' . __( 'This store was disconnected from Aha. Reconnect it from the Connect page.', 'aha-is-for-woocommerce' ), 'error' ); } elseif ( 'unavailable' === $link ) { Aha_Is_Woocommerce_Logger::warning( 'Connection test failed.', array( 'status' => 0, 'message' => 'connect_unavailable' ) ); $this->redirect_back( $could_not . ' ' . $no_answer, 'error' ); } $this->redirect_back( __( 'This store is not connected to Aha yet. Use Connect with Aha first.', 'aha-is-for-woocommerce' ), 'error' ); } /** Safe mode check for the connection test. Overridable in tests. */ protected function test_safe_mode() { return Aha_Is_Woocommerce_Site_Identity::is_safe_mode(); } /** * Connect link state for the connection test. * * @return string active, unavailable, revoked, or empty when the store is not Connect-linked. */ protected function test_connect_status() { if ( class_exists( 'Aha_Is_Woocommerce_Connect_Credentials' ) && Aha_Is_Woocommerce_Connect_Credentials::has_credentials() ) { return Aha_Is_Woocommerce_Connect_Credentials::connection_status(); } return ''; } /** * Go back to the Connect page with a message. A valid aha_is_woocommerce_return_tab picks * the tab; the default is Settings. * * @param string $notice Message. * @param string $type success or error. * @return void */ private function redirect_back( $notice, $type = 'success' ) { $tab = isset( $_POST['aha_is_woocommerce_return_tab'] ) && is_string( $_POST['aha_is_woocommerce_return_tab'] ) // phpcs:ignore WordPress.Security.NonceVerification -- nonce checked by the calling handler (check_admin_referer) before redirect_back(); value whitelisted by sanitize_tab() ? Aha_Is_Woocommerce_Connect_Admin_Page::sanitize_tab( wp_unslash( $_POST['aha_is_woocommerce_return_tab'] ) ) // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput -- nonce checked by the calling handler (check_admin_referer) before redirect_back(); value whitelisted by sanitize_tab() : ''; wp_safe_redirect( add_query_arg( array( 'page' => Aha_Is_Woocommerce_Connect_Admin_Page::PAGE_SLUG, 'tab' => '' === $tab ? 'settings' : $tab, 'aha_is_woocommerce_notice' => rawurlencode( $notice ), 'aha_is_woocommerce_notice_type' => $type, ), admin_url( 'admin.php' ) ) ); exit; } }