100, 'last_name' => 100, 'email' => 190, 'phone' => 40, 'city' => 100, 'country' => 100, 'company' => 190, 'message' => 5000, ); const TRANSLATABLE = array( 'form_title', 'form_intro', 'button_text', 'company_label', 'success_message', 'consent_text', 'consent_link_text', 'privacy_text', 'privacy_link_text', 'ack_subject', 'ack_body', ); public static function get( $raw = false, $lang = null ) { $stored = get_option( self::OPTION_KEY, array() ); if ( ! is_array( $stored ) ) { $stored = array(); } $s = array_merge( self::defaults(), $stored ); if ( $raw ) { return $s; } $s = self::translate( $s, $lang ); return self::fill_i18n_defaults( $s ); } public static function defaults() { return array( 'enabled' => 'yes', 'recipients' => '', 'staff_subject' => '', 'ack_enabled' => 'yes', 'ack_subject' => '', 'ack_body' => '', 'success_message' => '', 'redirect_url' => '', 'form_title' => '', 'form_intro' => '', 'button_text' => '', 'company_label' => '', 'consent_enabled' => 'no', 'consent_text' => '', 'consent_link_text' => '', 'consent_url' => '', 'privacy_enabled' => 'yes', 'privacy_text' => '', 'privacy_link_text' => '', 'default_country' => '', 'name_layout' => 'inline', 'message_rows' => 6, 'show_first_name' => 'yes', 'req_first_name' => 'yes', 'show_last_name' => 'yes', 'req_last_name' => 'yes', 'show_phone' => 'yes', 'req_phone' => 'no', 'show_city' => 'yes', 'req_city' => 'no', 'show_country' => 'yes', 'req_country' => 'no', 'show_company' => 'yes', 'req_company' => 'no', 'show_message' => 'yes', 'req_message' => 'yes', ); } public static function i18n_labels() { return array( 'button_text' => __( 'Send message', 'ai-eshop-optimizer' ), 'company_label' => __( 'Company', 'ai-eshop-optimizer' ), 'success_message' => __( 'Thank you. Your message has been sent and we will get back to you soon.', 'ai-eshop-optimizer' ), 'consent_text' => __( 'I accept the privacy policy', 'ai-eshop-optimizer' ), 'consent_link_text' => __( 'privacy policy', 'ai-eshop-optimizer' ), 'privacy_text' => __( 'How we use these details: Privacy policy.', 'ai-eshop-optimizer' ), 'privacy_link_text' => __( 'Privacy policy', 'ai-eshop-optimizer' ), 'ack_subject' => __( 'Thank you for contacting {site_name}', 'ai-eshop-optimizer' ), 'ack_body' => __( "Dear {first_name},\n\nThank you for your message. We have received it and will reply as soon as possible.\n\nKind regards,\n{site_name}", 'ai-eshop-optimizer' ), 'staff_subject' => __( '[{site_name}] New contact message from {first_name} {last_name}', 'ai-eshop-optimizer' ), ); } private static function fill_i18n_defaults( array $s ) { if ( ! did_action( 'init' ) ) { return $s; } foreach ( self::i18n_labels() as $k => $v ) { if ( ! isset( $s[ $k ] ) || '' === trim( (string) $s[ $k ] ) ) { $s[ $k ] = $v; } } return $s; } public static function wpml_active() { return defined( 'WPML_ST_VERSION' ) || defined( 'ICL_SITEPRESS_VERSION' ) || function_exists( 'icl_register_string' ); } private static function translate( array $s, $lang = null ) { $wpml = self::wpml_active(); $pll = function_exists( 'pll_translate_string' ); if ( ! $wpml && ! $pll ) { return $s; } $lang = ( null === $lang || '' === (string) $lang ) ? null : (string) $lang; foreach ( self::TRANSLATABLE as $k ) { $v = isset( $s[ $k ] ) ? (string) $s[ $k ] : ''; if ( '' === trim( $v ) ) { continue; } if ( $wpml ) { $s[ $k ] = (string) apply_filters( 'wpml_translate_single_string', $v, self::WPML_DOMAIN, $k, $lang ); } elseif ( null !== $lang ) { $s[ $k ] = (string) pll_translate_string( $v, $lang ); } elseif ( function_exists( 'pll__' ) ) { $s[ $k ] = (string) pll__( $v ); } } return $s; } public static function register_strings( array $s ) { foreach ( self::TRANSLATABLE as $k ) { $v = isset( $s[ $k ] ) ? trim( (string) $s[ $k ] ) : ''; if ( '' === $v ) { continue; } if ( self::wpml_active() ) { do_action( 'wpml_register_single_string', self::WPML_DOMAIN, $k, $v ); } if ( function_exists( 'pll_register_string' ) ) { pll_register_string( $k, $v, self::WPML_DOMAIN, false !== strpos( $v, "\n" ) ); } } } public static function current_lang() { $code = ''; if ( self::wpml_active() ) { $code = (string) apply_filters( 'wpml_current_language', null ); } if ( '' === $code && function_exists( 'pll_current_language' ) ) { $code = (string) pll_current_language( 'slug' ); } if ( '' === $code ) { $locale = function_exists( 'determine_locale' ) ? determine_locale() : get_locale(); $code = strtolower( substr( (string) $locale, 0, 2 ) ); } return substr( sanitize_key( $code ), 0, 10 ); } const ROWS_MIN = 3; const ROWS_MAX = 15; public static function placement( array $s, array $args = array() ) { $fields = self::csv_keys( $args['fields'] ?? '' ); $required = self::csv_keys( $args['required'] ?? '' ); $hide = self::csv_keys( $args['hide'] ?? '' ); $show = array(); $req = array(); foreach ( self::FIELDS as $f ) { if ( 'email' === $f ) { $show[] = $f; continue; } $on = $fields ? in_array( $f, $fields, true ) : 'yes' === (string) ( $s[ 'show_' . $f ] ?? 'yes' ); if ( $on && ! in_array( $f, $hide, true ) ) { $show[] = $f; } } foreach ( self::FIELDS as $f ) { if ( ! in_array( $f, $show, true ) ) { continue; } if ( 'email' === $f ) { $req[] = $f; continue; } $on = $required ? in_array( $f, $required, true ) : 'yes' === (string) ( $s[ 'req_' . $f ] ?? 'no' ); if ( $on ) { $req[] = $f; } } $names = sanitize_key( (string) ( $args['names'] ?? '' ) ); if ( ! in_array( $names, array( 'inline', 'stacked' ), true ) ) { $names = ( 'stacked' === (string) ( $s['name_layout'] ?? 'inline' ) ) ? 'stacked' : 'inline'; } $rows = (int) ( $args['rows'] ?? 0 ); if ( $rows <= 0 ) { $rows = (int) ( $s['message_rows'] ?? 6 ); } $rows = max( self::ROWS_MIN, min( self::ROWS_MAX, $rows ) ); return array( 'show' => $show, 'req' => $req, 'names' => $names, 'rows' => $rows ); } public static function fields_from_config( array $cfg ) { $out = array(); foreach ( self::FIELDS as $f ) { $show = 'email' === $f || in_array( $f, (array) ( $cfg['show'] ?? array() ), true ); $out[ $f ] = array( 'show' => $show, 'required' => $show && ( 'email' === $f || in_array( $f, (array) ( $cfg['req'] ?? array() ), true ) ), ); } return $out; } public static function sign( array $cfg ) { $payload = 'show=' . implode( ',', (array) $cfg['show'] ) . '|req=' . implode( ',', (array) $cfg['req'] ) . '|names=' . (string) $cfg['names'] . '|rows=' . (int) $cfg['rows']; return array( 'payload' => $payload, 'sig' => self::signature( $payload ) ); } public static function verify( $payload, $sig ) { $payload = (string) $payload; $sig = (string) $sig; if ( '' === $payload || '' === $sig || ! hash_equals( self::signature( $payload ), $sig ) ) { return null; } $parts = array(); foreach ( explode( '|', $payload ) as $bit ) { $kv = explode( '=', $bit, 2 ); if ( 2 === count( $kv ) ) { $parts[ $kv[0] ] = $kv[1]; } } $show = self::csv_keys( $parts['show'] ?? '' ); if ( ! in_array( 'email', $show, true ) ) { $show[] = 'email'; } $req = array_values( array_intersect( self::csv_keys( $parts['req'] ?? '' ), $show ) ); if ( ! in_array( 'email', $req, true ) ) { $req[] = 'email'; } $names = ( 'stacked' === ( $parts['names'] ?? '' ) ) ? 'stacked' : 'inline'; $rows = max( self::ROWS_MIN, min( self::ROWS_MAX, (int) ( $parts['rows'] ?? 6 ) ) ); return array( 'show' => $show, 'req' => $req, 'names' => $names, 'rows' => $rows ); } private static function signature( $payload ) { return substr( wp_hash( 'aieo_cf_cfg|' . $payload ), 0, 24 ); } public static function csv_keys( $v ) { if ( is_array( $v ) ) { $v = implode( ',', $v ); } $v = trim( (string) $v ); if ( '' === $v ) { return array(); } $out = array(); foreach ( explode( ',', $v ) as $bit ) { $k = sanitize_key( trim( $bit ) ); if ( in_array( $k, self::FIELDS, true ) && ! in_array( $k, $out, true ) ) { $out[] = $k; } } return $out; } public static function is_enabled() { $s = self::get( true ); return 'yes' === (string) $s['enabled']; } public static function placeholders( $text, $msg ) { $m = is_object( $msg ) ? get_object_vars( $msg ) : (array) $msg; $site = wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ); $map = array( '{site_name}' => $site, '{site_title}' => $site, '{message_id}' => isset( $m['id'] ) ? (string) (int) $m['id'] : '', ); foreach ( self::FIELDS as $f ) { $map[ '{' . $f . '}' ] = isset( $m[ $f ] ) ? (string) $m[ $f ] : ''; } return strtr( (string) $text, $map ); } } }