*/ function acscf_get_default_rules() { return array( 'estimate' => _x( "quote\nquotation\nestimate\npricing\nprice\ncost\nbudget\nfee\nhow much\ninvoice\npurchase order\nlead time\ndeadline\nplace an order\nwould like to order", 'default classification keywords, one per line', 'acs-contact-form' ), 'consult' => _x( "question\ninquiry\nenquiry\nsupport\nhelp\nhow do i\nhow to\nissue\nproblem\nerror\nbug\nnot working\ndocumentation\nsetup\nspecification\nis it possible", 'default classification keywords, one per line', 'acs-contact-form' ), 'recruit' => _x( "job\ncareer\nhiring\napply\napplication\nresume\ncurriculum vitae\ninternship\ninterview\nposition\nvacancy\nrecruitment\nsaw your job posting", 'default classification keywords, one per line', 'acs-contact-form' ), 'sales' => _x( "our services\nour company\nwe offer\npartnership\nreseller\naffiliate\nagency\nseo services\nsaw your website\ncame across your site\nmarketing services\nlead generation\noutsourcing\nspecial offer\nfree of charge\ndiscount\nlink exchange\nguest post\nbulk email\nbrochure\nintroduce our\nappointment", 'default classification keywords, one per line', 'acs-contact-form' ), 'spam' => _x( "backlink\nbacklinks\nguaranteed ranking\nfirst page of google\nmake money\nearn money fast\nwork from home income\ndating\nadult\ninvestment opportunity\ncrypto\ncasino\nviagra\ncialis\nseo service\ncrypto investment", 'default classification keywords, one per line', 'acs-contact-form' ), ); } /** * Read the effective keyword rules. * * @return array> */ function acscf_get_rules() { $settings = acscf_get_settings(); $rules = array(); foreach ( acscf_get_default_rules() as $category => $default ) { $raw = isset( $settings['rules'][ $category ] ) ? $settings['rules'][ $category ] : $default; // Split on real line breaks only. \R would also match the raw byte 0x85, // which is part of common Japanese characters such as 内. $lines = preg_split( '/\r\n|\r|\n/', (string) $raw ); $lines = array_filter( array_map( 'trim', is_array( $lines ) ? $lines : array() ) ); $rules[ $category ] = array_values( $lines ); } return $rules; } /** * Count how many URLs appear in a text. * * @param string $text Text to inspect. * @return int */ function acscf_count_urls( $text ) { return (int) preg_match_all( '#https?://#i', (string) $text ); } /** * Whether a text contains no Japanese characters at all. * * Used as a spam signal on a form that is meant for Japanese visitors. * * @param string $text Text to inspect. * @return bool */ function acscf_is_latin_only( $text ) { $text = trim( (string) $text ); if ( '' === $text ) { return false; } // Hiragana, Katakana, CJK ideographs, full width forms. return ! preg_match( '/[\x{3040}-\x{30FF}\x{3400}-\x{4DBF}\x{4E00}-\x{9FFF}\x{FF00}-\x{FFEF}]/u', $text ); } /** * Classify a submission. * * Scores every category by keyword hits against the subject, the body and the * sender's mail domain. The highest score wins. On a tie the category that does * NOT suppress the notification wins, so an ambiguous message is never silently * dropped into the sales or spam bucket. * * @param array $args { * Submission data. * * @type string $subject Subject line. * @type string $body Message body plus any other field values. * @type string $email Sender email address. * @type bool $honeypot True when the honeypot field was filled in. * } * @return string Category slug. */ function acscf_classify( $args ) { $args = wp_parse_args( $args, array( 'subject' => '', 'body' => '', 'email' => '', 'honeypot' => false, ) ); $settings = acscf_get_settings(); if ( ! empty( $args['honeypot'] ) && '1' === $settings['spam_honeypot'] ) { return 'spam'; } $domain = ''; if ( is_string( $args['email'] ) && false !== strpos( $args['email'], '@' ) ) { $parts = explode( '@', $args['email'] ); $domain = end( $parts ); } $haystack = $args['subject'] . "\n" . $args['body'] . "\n" . $domain; $haystack = function_exists( 'mb_strtolower' ) ? mb_strtolower( $haystack, 'UTF-8' ) : strtolower( $haystack ); $scores = array( 'estimate' => 0, 'recruit' => 0, 'consult' => 0, 'sales' => 0, 'spam' => 0, ); foreach ( acscf_get_rules() as $category => $keywords ) { if ( ! isset( $scores[ $category ] ) ) { continue; } foreach ( $keywords as $keyword ) { $needle = function_exists( 'mb_strtolower' ) ? mb_strtolower( $keyword, 'UTF-8' ) : strtolower( $keyword ); if ( '' !== $needle && false !== strpos( $haystack, $needle ) ) { ++$scores[ $category ]; } } } $url_threshold = (int) $settings['spam_url_threshold']; if ( $url_threshold > 0 && acscf_count_urls( $args['body'] ) >= $url_threshold ) { $scores['spam'] += 2; } if ( '1' === $settings['spam_latin_only'] && acscf_is_latin_only( $args['subject'] . $args['body'] ) ) { $scores['spam'] += 2; } $max = max( $scores ); if ( $max < 1 ) { return 'consult'; } // Tie break order: never suppress a notification because of a tie. foreach ( array( 'estimate', 'recruit', 'consult', 'sales', 'spam' ) as $category ) { if ( $scores[ $category ] === $max ) { /** * Filters the classification result. * * @param string $category Category slug. * @param array $args Submission data. * @param array $scores Score per category. */ return apply_filters( 'acscf_classified_category', $category, $args, $scores ); } } return 'consult'; } /** * Whether the site owner should be notified about this category. * * @param string $category Category slug. * @return bool */ function acscf_should_notify( $category ) { $settings = acscf_get_settings(); $category = acscf_sanitize_category( $category ); $notify = in_array( $category, (array) $settings['notify_categories'], true ); /** * Filters whether the admin notification mail is sent for this category. * * @param bool $notify Whether to notify. * @param string $category Category slug. */ return (bool) apply_filters( 'acscf_should_notify', $notify, $category ); }