admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'afpp_subscribe' ),
'i18n' => array(
'wait' => __( 'Please wait…', 'afojet-new-post-alerts' ),
'network' => __( 'Something went wrong. Please try again.', 'afojet-new-post-alerts' ),
),
)
);
}
/** Block "New Post Alerts subscribe form" (server-rendered, no build step). */
public function register_block() {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
wp_register_script( 'afpp-block', AFPP_URL . 'assets/js/block.js', array( 'wp-blocks', 'wp-element', 'wp-server-side-render', 'wp-block-editor' ), AFPP_VERSION, true );
register_block_type(
'afojet/new-post-alerts-subscribe',
array(
'editor_script' => 'afpp-block',
'editor_style' => 'afpp-front',
'render_callback' => function () {
return $this->render();
},
)
);
}
public function shortcode( $atts ) {
$atts = shortcode_atts(
array(
'title' => '',
'text' => '',
),
$atts,
'afpp_subscribe_form'
);
return $this->render( $atts );
}
public function after_content( $content ) {
if ( ! AFPP_Settings::enabled( 'afpp_auto_insert' ) || ! is_singular( AFPP_Broadcast::post_types() ) || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
if ( has_shortcode( $content, 'afpp_subscribe_form' ) || has_block( 'afojet/new-post-alerts-subscribe' ) ) {
return $content;
}
return $content . $this->render();
}
public function floating() {
if ( ! AFPP_Settings::enabled( 'afpp_floating' ) || is_admin() ) {
return;
}
?>
render( array( 'context' => 'modal' ) ), self::allowed_form_html() ); ?>
';
}
/** The plugin's own icon (a bell in a speech bubble), used for branding in the admin. */
public static function plugin_icon() {
return '';
}
/** Tags allowed in the inline SVG icon (used with wp_kses when it is printed). */
public static function allowed_svg() {
return array(
'svg' => array(
'class' => true,
'viewbox' => true,
'aria-hidden' => true,
'focusable' => true,
'width' => true,
'height' => true,
'xmlns' => true,
),
'path' => array(
'fill' => true,
'fill-rule' => true,
'd' => true,
),
);
}
/** Tags allowed in the subscribe form and unsubscribe page (used with wp_kses when printed). */
public static function allowed_form_html() {
$common = array(
'class' => true,
'id' => true,
'hidden' => true,
'role' => true,
'aria-live' => true,
'aria-hidden' => true,
'aria-label' => true,
'data-*' => true,
);
$allowed = array(
'form' => $common + array(
'method' => true,
'action' => true,
'novalidate' => true,
),
'div' => $common,
'p' => $common,
'span' => $common,
'strong' => $common,
'em' => $common,
'h1' => $common,
'br' => array(),
'a' => $common + array(
'href' => true,
'target' => true,
'rel' => true,
),
'label' => $common + array( 'for' => true ),
'input' => $common + array(
'type' => true,
'name' => true,
'value' => true,
'placeholder' => true,
'required' => true,
'checked' => true,
'tabindex' => true,
'autocomplete' => true,
'inputmode' => true,
'maxlength' => true,
'pattern' => true,
),
'select' => $common + array(
'name' => true,
'required' => true,
),
'option' => array(
'value' => true,
'selected' => true,
),
'button' => $common + array(
'type' => true,
'name' => true,
'value' => true,
),
'fieldset' => $common,
'legend' => $common,
);
/** Add-ons that add fields to the form can allow more tags here. */
return apply_filters( 'afpp_form_allowed_html', array_merge( $allowed, self::allowed_svg() ) );
}
/**
* The form markup.
*
* @param array $args title, text, context.
*/
public function render( $args = array() ) {
wp_enqueue_style( 'afpp-front' );
wp_enqueue_script( 'afpp-front' );
++self::$rendered;
$id = 'afpp-' . self::$rendered;
$title = ! empty( $args['title'] ) ? $args['title'] : AFPP_Settings::get( 'afpp_form_title' );
$text = ! empty( $args['text'] ) ? $args['text'] : AFPP_Settings::get( 'afpp_form_text' );
$code = AFPP_Settings::get( 'afpp_default_code' );
ob_start();
?>
= (int) apply_filters( 'afpp_subscribe_limit_per_hour', 10 ) ) {
return false;
}
set_transient( $key, $n + 1, HOUR_IN_SECONDS );
return true;
}
private static function normalize_posted( $raw ) {
return AFPP_WhatsApp::normalize_phone( $raw, AFPP_Settings::get( 'afpp_default_code' ) );
}
/**
* The form nonce failed. Cached pages can carry an expired nonce, so send a fresh one and the
* script retries once. Abuse is also limited by the consent box, a honeypot and a per-IP limit.
*/
private static function nonce_failed() {
wp_send_json_error(
array(
'code' => 'stale_nonce',
'nonce' => wp_create_nonce( 'afpp_subscribe' ),
'message' => __( 'Please try again.', 'afojet-new-post-alerts' ),
)
);
}
public function ajax_subscribe() {
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'afpp_subscribe' ) ) {
self::nonce_failed();
}
if ( ! empty( $_POST['website'] ) ) {
wp_send_json_success( array( 'message' => AFPP_Settings::get( 'afpp_success_text' ) ) ); // Bot: pretend it worked.
}
if ( empty( $_POST['consent'] ) ) {
wp_send_json_error( array( 'message' => __( 'Please tick the box to agree to receive messages.', 'afojet-new-post-alerts' ) ) );
}
$phone = self::normalize_posted( isset( $_POST['phone'] ) ? sanitize_text_field( wp_unslash( $_POST['phone'] ) ) : '' );
if ( ! AFPP_WhatsApp::is_valid_phone( $phone ) ) {
wp_send_json_error( array( 'message' => __( 'Please enter a valid WhatsApp number.', 'afojet-new-post-alerts' ) ) );
}
if ( ! self::rate_ok() ) {
wp_send_json_error( array( 'message' => __( 'Too many attempts. Please try again later.', 'afojet-new-post-alerts' ) ) );
}
$name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
$source = isset( $_POST['source'] ) ? absint( $_POST['source'] ) : 0;
$fields = map_deep( wp_unslash( $_POST ), 'sanitize_text_field' ); // Passed to add-ons for their own fields.
$existing = AFPP_Subscribers::by_phone( $phone );
$verify = (bool) apply_filters( 'afpp_requires_verification', false, $phone );
if ( $existing && 'active' === $existing->status && ! $verify ) {
do_action( 'afpp_subscribed', (int) $existing->id, $fields );
wp_send_json_success( array( 'message' => __( 'You are already subscribed. Your choices have been saved.', 'afojet-new-post-alerts' ) ) );
}
$id = AFPP_Subscribers::upsert(
$phone,
array(
'name' => $name,
'status' => ( $existing && 'active' === $existing->status ) ? 'active' : ( $verify ? 'pending' : 'active' ),
'source' => $source ? (string) $source : '',
'ip' => isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '',
)
);
if ( $verify ) {
do_action( 'afpp_subscriber_pending', $id, $fields );
$sent = apply_filters( 'afpp_send_verification', new WP_Error( 'unsupported', __( 'Verification is not available.', 'afojet-new-post-alerts' ) ), $phone, $id );
if ( is_wp_error( $sent ) ) {
wp_send_json_error( array( 'message' => $sent->get_error_message() ) );
}
wp_send_json_success(
array(
'step' => 'verify',
/* translators: %s masked phone number */
'message' => sprintf( __( 'We sent a 6-digit code to +%s on WhatsApp. Enter it to confirm.', 'afojet-new-post-alerts' ), AFPP_WhatsApp::mask( $phone ) ),
)
);
}
do_action( 'afpp_subscribed', $id, $fields );
wp_send_json_success( array( 'message' => AFPP_Settings::get( 'afpp_success_text' ) ) );
}
public function ajax_verify() {
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'afpp_subscribe' ) ) {
self::nonce_failed();
}
$phone = self::normalize_posted( isset( $_POST['phone'] ) ? sanitize_text_field( wp_unslash( $_POST['phone'] ) ) : '' );
$code = isset( $_POST['code'] ) ? sanitize_text_field( wp_unslash( $_POST['code'] ) ) : '';
$row = AFPP_Subscribers::by_phone( $phone );
if ( ! $row ) {
wp_send_json_error( array( 'message' => __( 'Please start again.', 'afojet-new-post-alerts' ) ) );
}
$ok = apply_filters( 'afpp_verify_subscription', new WP_Error( 'unsupported', __( 'Verification is not available.', 'afojet-new-post-alerts' ) ), $phone, $code, (int) $row->id );
if ( is_wp_error( $ok ) ) {
wp_send_json_error( array( 'message' => $ok->get_error_message() ) );
}
AFPP_Subscribers::set_status( $row->id, 'active' );
do_action( 'afpp_verified', (int) $row->id );
wp_send_json_success( array( 'message' => AFPP_Settings::get( 'afpp_success_text' ) ) );
}
/* ------------------------------------------------------------------ *
* Unsubscribe link (?afpp_unsub=TOKEN). Asks for a click so link previews don't unsubscribe people.
* ------------------------------------------------------------------ */
public function unsubscribe_page() {
if ( empty( $_GET['afpp_unsub'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
return;
}
$token = preg_replace( '/[^a-f0-9]/', '', strtolower( sanitize_text_field( wp_unslash( $_GET['afpp_unsub'] ) ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
$row = $token ? AFPP_Subscribers::by_token( $token ) : null;
$site = get_bloginfo( 'name' );
if ( ! $row ) {
wp_die( esc_html__( 'This unsubscribe link is not valid any more.', 'afojet-new-post-alerts' ), esc_html( $site ), array( 'response' => 404 ) );
}
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] && isset( $_POST['afpp_confirm'] ) ) {
check_admin_referer( 'afpp_unsub_' . $token, 'afpp_unsub_nonce' );
AFPP_Subscribers::set_status( $row->id, 'unsubscribed' );
wp_die(
'' . esc_html__( 'You are unsubscribed', 'afojet-new-post-alerts' ) . '
' . esc_html__( 'You will not get any more WhatsApp messages from us.', 'afojet-new-post-alerts' ) . '
' . esc_html( $site ) . '
',
esc_html( $site ),
array( 'response' => 200 )
);
}
$html = '' . esc_html__( 'Stop WhatsApp updates?', 'afojet-new-post-alerts' ) . '
';
$html .= '' . sprintf( /* translators: 1 masked number, 2 site name */ esc_html__( 'Number +%1$s will stop getting new post alerts from %2$s.', 'afojet-new-post-alerts' ), esc_html( AFPP_WhatsApp::mask( $row->phone ) ), esc_html( $site ) ) . '
';
if ( 'unsubscribed' === $row->status ) {
$html .= '' . esc_html__( 'This number is already unsubscribed.', 'afojet-new-post-alerts' ) . '
';
} else {
$html .= '';
}
wp_die( wp_kses( $html, self::allowed_form_html() ), esc_html( $site ), array( 'response' => 200 ) );
}
}