statuses = new StatusesTab(); } public function register(): void { add_action( 'admin_menu', array( $this, 'menu' ), 20 ); add_action( 'admin_init', array( $this, 'register_setting' ) ); add_filter( 'option_page_capability_' . self::GROUP, array( $this, 'capability' ) ); $this->statuses->register(); } /** * Current tab: '' (general and forms) or StatusesTab::TAB. */ private function current_tab(): string { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only tab switch. $tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : ''; return StatusesTab::TAB === $tab ? $tab : ''; } private function render_tabs( string $current ): void { $tabs = array( '' => __( 'General and forms', 'action-receipts' ), StatusesTab::TAB => __( 'Statuses', 'action-receipts' ), ); echo ''; } public function capability(): string { return Installer::CAP; } public function menu(): void { add_submenu_page( Admin::PAGE, __( 'Receipt settings', 'action-receipts' ), __( 'Settings', 'action-receipts' ), Installer::CAP, self::PAGE, array( $this, 'render' ) ); } public function register_setting(): void { register_setting( self::GROUP, Settings::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize' ), 'show_in_rest' => false, ) ); } /** * @param mixed $input Raw posted settings. */ public function sanitize( $input ): array { $input = is_array( $input ) ? $input : array(); $current = Settings::get(); $out = Settings::defaults(); $out['status_page_id'] = absint( $input['status_page_id'] ?? 0 ); $out['retention_days'] = min( 3650, absint( $input['retention_days'] ?? 0 ) ); $out['delete_on_uninstall'] = ! empty( $input['delete_on_uninstall'] ); // Keep settings of forms not on screen (e.g. their form plugin is deactivated). $out['forms'] = is_array( $current['forms'] ) ? $current['forms'] : array(); $forms = isset( $input['forms'] ) && is_array( $input['forms'] ) ? $input['forms'] : array(); foreach ( $forms as $key => $form ) { $key = (string) $key; if ( ! Settings::is_valid_form_key( $key ) || ! is_array( $form ) ) { continue; } if ( isset( $form['fields_shown'] ) ) { // The field list was on screen: store exactly what was ticked. $fields = isset( $form['fields'] ) && is_array( $form['fields'] ) ? $form['fields'] : array(); } elseif ( array_key_exists( 'fields', $form ) ) { // Already-sanitised settings passed through again (e.g. by add_option). $fields = $form['fields']; } else { $fields = $current['forms'][ $key ]['fields'] ?? null; } if ( is_array( $fields ) ) { $fields = array_values( array_map( 'sanitize_text_field', $fields ) ); } else { $fields = null; } $pattern = sanitize_text_field( $form['pattern'] ?? '' ); if ( '' === $pattern || ( false === strpos( $pattern, '{SEQ' ) && false === strpos( $pattern, '{RAND' ) ) ) { $pattern = ReferenceGenerator::DEFAULT_PATTERN; } $out['forms'][ $key ] = array( 'enabled' => ! empty( $form['enabled'] ), 'prefix' => ReferenceGenerator::clean_prefix( (string) ( $form['prefix'] ?? '' ) ), 'pattern' => substr( $pattern, 0, 64 ), 'fields' => $fields, 'email_field' => sanitize_text_field( $form['email_field'] ?? '' ), 'show_in_message' => ! empty( $form['show_in_message'] ), 'send_confirmation' => ! empty( $form['send_confirmation'] ), ); } return $out; } public function render(): void { if ( ! current_user_can( Installer::CAP ) ) { wp_die( esc_html__( 'You do not have permission to change these settings.', 'action-receipts' ) ); } $tab = $this->current_tab(); ?>

render_tabs( $tab ); settings_errors(); if ( StatusesTab::TAB === $tab ) { $this->statuses->render(); echo '
'; return; } $settings = Settings::get(); $adapters = Plugin::instance()->active_adapters(); $name = Settings::OPTION; ?>

label() ); ?>

list_forms(); ?>

$title ) { $this->render_form_card( $adapter, (string) $form_id, (string) $title ); } ?>

key() . ':' . $form_id; $cfg = Settings::form( $key ); $base = Settings::OPTION . '[forms][' . $key . ']'; $fields = $adapter->list_fields( $form_id ); $id = 'actr-' . preg_replace( '/[^a-z0-9]+/i', '-', $key ); $enabled = ! empty( $cfg['enabled'] ); // Show a field's code only when two fields share a label, so they can be told apart. $label_counts = array_count_values( array_map( static function ( $info ) { return strtolower( (string) $info['label'] ); }, $fields ) ); $has_email = false; foreach ( $fields as $info ) { if ( 'email' === strtolower( (string) $info['type'] ) ) { $has_email = true; } } ?>
>
ReferenceGenerator::clean_prefix( (string) ( $cfg['prefix'] ?? '' ) ), '{YYYY}' => wp_date( 'Y' ), '{YY}' => wp_date( 'y' ), '{MM}' => wp_date( 'm' ), '{DD}' => wp_date( 'd' ), ) ); $out = (string) preg_replace_callback( '/\{SEQ(?::(\d+))?\}/', static function ( $m ) { return str_pad( '1', isset( $m[1] ) ? min( 12, (int) $m[1] ) : 5, '0', STR_PAD_LEFT ); }, $out ); $out = (string) preg_replace_callback( '/\{RAND(?::(\d+))?\}/', static function ( $m ) { return substr( 'K7QM4P2XR9TB', 0, isset( $m[1] ) ? min( 12, (int) $m[1] ) : 6 ); }, $out ); // Same clean-up as ReferenceGenerator::generate(). return substr( strtoupper( (string) preg_replace( '/[^A-Za-z0-9\-_]/', '', $out ) ), 0, 64 ); } }