Detail => share of enquiries without it. */ public static function missing() { $cached = get_transient( self::CACHE ); if ( is_array( $cached ) ) { return $cached; } global $wpdb; $rows = (array) $wpdb->get_col( $wpdb->prepare( "SELECT ai_details FROM %i WHERE ai_state = 'done' AND ai_kind IN ('enquiry', 'order') AND form_plugin NOT IN ('manual', 'woocommerce') AND created_at >= %s ORDER BY id DESC LIMIT 400", Installer::table(), gmdate( 'Y-m-d H:i:s', time() - 180 * DAY_IN_SECONDS ) ) ); $missing = array(); if ( count( $rows ) >= self::MIN_LEADS ) { foreach ( array( 'when', 'place', 'quantity' ) as $detail ) { $without = 0; foreach ( $rows as $row ) { $details = json_decode( (string) $row, true ); $without += '' === trim( (string) ( $details[ $detail ] ?? '' ) ) ? 1 : 0; } $share = $without / count( $rows ); if ( $share >= self::MIN_SHARE ) { $missing[ $detail ] = round( $share, 2 ); } } arsort( $missing ); } set_transient( self::CACHE, $missing, DAY_IN_SECONDS ); return $missing; } /** * Texts of the hints, for visitors, in the language of the site. * * @return array */ public static function texts() { return array( 'when' => __( 'Tip: mention the date or by when you need it – you will get an offer faster.', 'dealroot' ), 'place' => __( 'Tip: mention the town or postcode – you will get an offer faster.', 'dealroot' ), 'quantity' => __( 'Tip: mention how many or how large – you will get an offer faster.', 'dealroot' ), ); } /** * Loads the hints on the pages of the site. */ public static function enqueue() { if ( ! Settings::get( 'smart_form' ) ) { return; } $missing = self::missing(); /** * Filters the details hinted at in forms, e.g. to always ask for the date. * * @param array $missing Detail => share of enquiries without it. */ $missing = (array) apply_filters( 'dealroot_form_hints', $missing ); if ( ! $missing ) { return; } wp_enqueue_script( 'dealroot-form-hints', DEALROOT_URL . 'assets/js/form-hints.js', array(), Plugin::asset_version( 'assets/js/form-hints.js' ), array( 'strategy' => 'defer', 'in_footer' => true, ) ); wp_add_inline_script( 'dealroot-form-hints', 'window.dealrootHints = ' . wp_json_encode( array( 'hints' => array_intersect_key( self::texts(), $missing ), 'order' => array_keys( $missing ), ) ) . ';', 'before' ); } }