trim( (string) $c['first_name'] ),
'name' => Nodebook_Contacts::display_name( $c ),
'site' => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
'my_name' => $user && $user->exists() ? ( $user->first_name ? $user->first_name : $user->display_name ) : wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
'review_link' => self::url(),
);
return array( Nodebook_Recipes::fill( (string) $r['subject'], $tags ), Nodebook_Recipes::fill( (string) $r['body'], $tags ) );
}
/**
* Record that someone was asked, and tick off the open "Ask for a review" step.
*
* @param int $contact_id Contact.
* @param string $channel email|whatsapp.
* @param int $user_id Who asked.
*/
public static function mark( $contact_id, $channel, $user_id ) {
$contact_id = (int) $contact_id;
$channel = in_array( $channel, array( 'email', 'whatsapp' ), true ) ? $channel : 'email';
$labels = array(
'email' => __( 'Asked for a review by email', 'nodebook' ),
'whatsapp' => __( 'Asked for a review on WhatsApp', 'nodebook' ),
);
Nodebook_Activities::add(
$contact_id,
'review',
$labels[ $channel ],
array(
'user_id' => (int) $user_id,
'url' => self::url(),
'data' => array( 'channel' => $channel ),
)
);
foreach ( Nodebook_Tasks::for_contact( $contact_id, 0 )['open'] as $t ) {
if ( self::is_review_task( $t ) ) {
Nodebook_Tasks::complete( (int) $t['id'], $labels[ $channel ], (int) $user_id );
}
}
}
/** A next step the review automation added. */
public static function is_review_task( array $t ) {
global $wpdb;
$like = '%' . $wpdb->esc_like( '"recipe":"review_ask","task":' . (int) $t['id'] ) . '%';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- the plugin's own table.
return (bool) $wpdb->get_var( $wpdb->prepare( "SELECT 1 FROM %i WHERE contact_id = %d AND type = 'auto' AND data LIKE %s LIMIT 1", Nodebook_Activities::table(), (int) $t['contact_id'], $like ) );
}
/**
* The "Review" button(s) for a contact: email (compose sheet) and WhatsApp. Nothing without a review link.
*
* @param array $c Contact.
* @param string $size tile|button-small.
*/
public static function buttons( array $c, $size = 'tile' ) {
if ( '' === self::url() || ! self::can_ask( $c ) ) {
return;
}
list( $subject, $body ) = self::message( $c );
$asked = self::asked_at( (int) $c['id'] );
/* translators: %s: date */
$title = $asked ? sprintf( __( 'Asked for a review on %s', 'nodebook' ), date_i18n( get_option( 'date_format' ), strtotime( $asked . ' UTC' ) ) ) : __( 'Ask for a review', 'nodebook' );
$star = wp_kses( Nodebook_Icons::svg( 'star', 'tile' === $size ? 22 : 17 ), Nodebook_Icons::kses() );
$label = __( 'Review', 'nodebook' );
$cls = 'tile' === $size ? 'nb-tilebtn' : 'button button-small nb-act--round';
$intl = Nodebook_Phone::international( (string) $c['phone'], (string) ( $c['country'] ?? '' ) );
if ( is_email( (string) $c['email'] ) && 'complained' !== (string) ( $c['email_status'] ?? '' ) ) {
$attrs = array(
'data-wa' => $intl ? 'https://wa.me/' . $intl . '?text=' . rawurlencode( $body ) : '',
'data-nb-compose' => (string) (int) $c['id'],
'data-kind' => 'review',
'data-to' => (string) $c['email'],
'data-name' => Nodebook_Contacts::display_name( $c ),
'data-first-name' => trim( (string) $c['first_name'] ),
'data-subject' => $subject,
'data-body' => $body,
'data-warn' => $asked ? $title : '',
);
$out = '';
foreach ( array_filter( $attrs, 'strlen' ) as $k => $v ) {
$out .= ' ' . sanitize_key( $k ) . '="' . esc_attr( $v ) . '"';
}
echo '' . $star . '' . esc_html( $label ) . ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $star is run through wp_kses above.
return;
}
if ( $intl ) {
echo '' . $star . '' . esc_html( $label ) . ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $star is run through wp_kses above.
}
}
/** admin-ajax: a review request went out on WhatsApp. */
public static function ajax() {
check_ajax_referer( 'nodebook_ajax', 'nonce' );
Nodebook_Admin::require_cap();
$id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0;
if ( ! $id || ! Nodebook_Contacts::get( $id ) || ! Nodebook_Team::can_see( $id ) ) {
wp_send_json_error( null, 404 );
}
self::mark( $id, 'whatsapp', get_current_user_id() );
wp_send_json_success();
}
}