' . esc_html__( 'Settings', 'dst-donate' ) . ''; array_unshift( $links, $settings ); if ( ! dst_donate_pro_is_active() ) { $links[] = '' . esc_html__( 'Get Pro', 'dst-donate' ) . ''; } return $links; } /* ------------------------------------------------------------------ */ /* Save settings */ /* ------------------------------------------------------------------ */ /** * Persist settings. */ public function maybe_save_settings() { if ( empty( $_POST['dst_donate_settings_save'] ) ) { return; } if ( ! current_user_can( DST_DONATE_CAP ) ) { wp_die( esc_html__( 'Permission denied.', 'dst-donate' ) ); } check_admin_referer( 'dst_donate_settings', 'dst_donate_settings_nonce' ); $in = wp_unslash( $_POST ); $out = DST_Donate_Settings::all(); $out['test_mode'] = isset( $in['test_mode'] ) ? 1 : 0; $out['currency'] = isset( $in['currency'] ) ? sanitize_text_field( $in['currency'] ) : 'NGN'; $out['test_public_key'] = isset( $in['test_public_key'] ) ? sanitize_text_field( $in['test_public_key'] ) : ''; $out['live_public_key'] = isset( $in['live_public_key'] ) ? sanitize_text_field( $in['live_public_key'] ) : ''; if ( ! empty( $in['test_secret_key'] ) ) { $out['test_secret_key'] = DST_Donate_Settings::encrypt( sanitize_text_field( $in['test_secret_key'] ) ); } if ( ! empty( $in['live_secret_key'] ) ) { $out['live_secret_key'] = DST_Donate_Settings::encrypt( sanitize_text_field( $in['live_secret_key'] ) ); } if ( ! empty( $in['clear_test_secret'] ) ) { $out['test_secret_key'] = ''; } if ( ! empty( $in['clear_live_secret'] ) ) { $out['live_secret_key'] = ''; } $amounts = array(); if ( isset( $in['default_amounts'] ) ) { foreach ( explode( ',', $in['default_amounts'] ) as $a ) { $a = (float) preg_replace( '/[^0-9.]/', '', $a ); if ( $a > 0 ) { $amounts[] = $a; } } } $out['default_amounts'] = $amounts; $out['min_amount'] = isset( $in['min_amount'] ) ? max( 1, (float) $in['min_amount'] ) : 100; $out['collect_phone'] = isset( $in['collect_phone'] ) ? 1 : 0; $out['form_title'] = isset( $in['form_title'] ) ? sanitize_text_field( $in['form_title'] ) : ''; $out['form_description'] = isset( $in['form_description'] ) ? sanitize_textarea_field( $in['form_description'] ) : ''; $out['button_label'] = isset( $in['button_label'] ) ? sanitize_text_field( $in['button_label'] ) : __( 'Donate now', 'dst-donate' ); $out['thank_you_message'] = isset( $in['thank_you_message'] ) ? sanitize_textarea_field( $in['thank_you_message'] ) : ''; update_option( DST_Donate_Settings::OPTION, $out ); // Validate the stored secret keys against Paystack and record the result, // so the admin is warned before relying on an invalid key. $this->validate_keys( $out ); $status = DST_Donate_Settings::key_status(); wp_safe_redirect( add_query_arg( array( 'page' => self::SETTINGS_SLUG, 'updated' => 'true', 'keycheck' => $status[ DST_Donate_Settings::mode() ], ), admin_url( 'admin.php' ) ) ); exit; } /** * Validate each stored secret key and record its status. * * @param array $out The settings just saved (holds encrypted secrets). */ private function validate_keys( $out ) { foreach ( array( 'test', 'live' ) as $mode ) { $enc = 'test' === $mode ? $out['test_secret_key'] : $out['live_secret_key']; if ( '' === (string) $enc ) { DST_Donate_Settings::set_key_status( $mode, '' ); continue; } $result = DST_Donate_Paystack::verify_secret( DST_Donate_Settings::decrypt( $enc ) ); if ( true === $result ) { DST_Donate_Settings::set_key_status( $mode, 'valid' ); } elseif ( is_wp_error( $result ) && 'dst_key_invalid' === $result->get_error_code() ) { DST_Donate_Settings::set_key_status( $mode, 'invalid' ); } else { DST_Donate_Settings::set_key_status( $mode, 'unknown' ); } } } /** * A small status badge for a stored secret key. * * @param string $mode 'test' or 'live'. * @return string */ private function key_badge( $mode ) { if ( ! DST_Donate_Settings::has_secret( $mode ) ) { return ''; } $status = DST_Donate_Settings::key_status(); $state = isset( $status[ $mode ] ) ? $status[ $mode ] : ''; $map = array( 'valid' => array( '#0a7c42', '#d1e7dd', __( 'Verified', 'dst-donate' ) ), 'invalid' => array( '#842029', '#f8d7da', __( 'Invalid', 'dst-donate' ) ), 'unknown' => array( '#664d03', '#fff3cd', __( 'Not verified', 'dst-donate' ) ), ); if ( ! isset( $map[ $state ] ) ) { return ''; } list( $fg, $bg, $label ) = $map[ $state ]; return '' . esc_html( $label ) . ''; } /** * A site-wide, admin-only notice when the active mode's secret key is invalid. */ public function key_status_notice() { if ( ! current_user_can( DST_DONATE_CAP ) ) { return; } if ( ! DST_Donate_Settings::active_key_invalid() ) { return; } $mode_label = 'test' === DST_Donate_Settings::mode() ? __( 'test', 'dst-donate' ) : __( 'live', 'dst-donate' ); $url = admin_url( 'admin.php?page=' . self::SETTINGS_SLUG ); echo '
' . esc_html__( 'DST Donate', 'dst-donate' ) . ': '; printf( /* translators: 1: test or live, 2: opening link tag, 3: closing link tag */ esc_html__( 'The %1$s Paystack secret key is invalid, so donations cannot be taken. Anyone who tries to donate will get an error and no payment will start. %2$sOpen settings%3$s to enter a valid key.', 'dst-donate' ), esc_html( $mode_label ), '', '' ); echo '
| | ' . esc_html__( 'Pro', 'dst-donate' ) . '' ); ?>
| created_at ) ); ?> | donor_name ? $row->donor_name : '-' ); ?> donor_email ); ?> |
currency ) ); ?>amount, 2 ) ); ?> | frequency ); ?> | status ) ); ?> | reference ); ?> |