time() ) { return null; } $first = (int) get_option( self::FIRST_SEEN, 0 ); if ( !$first ) { update_option( self::FIRST_SEEN, time(), false ); return null; } if ( $first > time() - self::MIN_AGE_DAYS * DAY_IN_SECONDS ) { return null; } // Something went wrong this week (an email could not be sent): not a happy moment. Read fresh, not from the cache. global $wpdb; if ( (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE status=%s AND created>=%s', MailLog::table(), 'failed', gmdate( 'Y-m-d H:i:s', time() - 7 * DAY_IN_SECONDS ) ) ) ) { return null; } $signal = get_transient( self::SIGNAL ); if ( !is_array( $signal ) ) { $signal = self::signal(); set_transient( self::SIGNAL, $signal, 12 * HOUR_IN_SECONDS ); } if ( empty( $signal['key'] ) ) { return null; } return array( 'signal' => $signal['key'], 'count' => (int) $signal['count'], 'text' => self::text( $signal['key'], (int) $signal['count'] ), 'url' => self::URL, ); } /** "rated" (the review page was opened), "later" (ask again in 30 days) or "never". */ public static function respond( array $data ) : array { Store::require_cap( 'manage_options' ); $answer = Store::text( $data, 'answer' ); if ( !in_array( $answer, array('rated', 'later', 'never'), true ) ) { throw new \InvalidArgumentException(I18n::t( 'Choose an answer.' )); } update_option( self::STATE, array( 'state' => $answer, 'until' => ( $answer === 'later' ? time() + self::LATER_DAYS * DAY_IN_SECONDS : 0 ), 'at' => time(), 'actor' => get_current_user_id(), ), false ); return array( 'saved' => true, ); } /** The strongest sign of success in the last 30 days, or none while something is wrong. */ private static function signal() : array { global $wpdb; $since = gmdate( 'Y-m-d H:i:s', time() - 30 * DAY_IN_SECONDS ); // Reminder times are stored in UTC. // Order dates and register days are the site's local time (no date filter in between). $local = ( new \DateTimeImmutable('now', wp_timezone()) )->modify( '-30 days' ); if ( get_option( 'storesuit_order_index_ready' ) === '1' ) { $done = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE status=%s AND created_at>=%s', OrderIndex::table(), 'completed', $local->format( 'Y-m-d H:i:s' ) ) ); if ( $done >= 20 ) { return array( 'key' => 'orders', 'count' => $done, ); } } return array(); } private static function text( string $key, int $count ) : string { if ( $key === 'recovered' ) { return ( $count === 1 ? I18n::t( 'A cart reminder won back a sale in the last 30 days.' ) : I18n::format( 'Cart reminders won back %s sales in the last 30 days.', number_format_i18n( $count ) ) ); } if ( $key === 'register' ) { return I18n::format( 'You closed the register with StoreRuler on %s of the last 30 days.', number_format_i18n( $count ) ); } // The store's own success, not a claim about StoreRuler: every order completed in the last 30 days counts (guideline 9). return I18n::format( 'You completed %s orders in the last 30 days.', number_format_i18n( $count ) ); } }