self::PAGE, 'tab' => $tab ), $args ), admin_url( 'admin.php' ) ); } public static function tabs() { $tabs = array( 'overview' => array( __( 'Overview', 'afojet-new-post-alerts' ), 'dashicons-chart-area' ), 'subscribers' => array( __( 'Subscribers', 'afojet-new-post-alerts' ), 'dashicons-groups' ), 'broadcasts' => array( __( 'Broadcasts', 'afojet-new-post-alerts' ), 'dashicons-megaphone' ), 'settings' => array( __( 'WhatsApp API', 'afojet-new-post-alerts' ), 'dashicons-admin-generic' ), 'form' => array( __( 'Subscribe form', 'afojet-new-post-alerts' ), 'dashicons-feedback' ), 'guide' => array( __( 'Setup guide', 'afojet-new-post-alerts' ), 'dashicons-book' ), ); $tabs = apply_filters( 'afpp_admin_tabs', $tabs ); if ( ! has_action( 'afpp_admin_tab_pro' ) && ! isset( $tabs['license'] ) ) { $tabs['pro'] = array( __( 'Go Pro', 'afojet-new-post-alerts' ), 'dashicons-star-filled' ); } return $tabs; } private static function current_tab() { $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'overview'; // phpcs:ignore WordPress.Security.NonceVerification return array_key_exists( $tab, self::tabs() ) ? $tab : 'overview'; } public static function status_label( $status ) { $labels = array( 'active' => __( 'Active', 'afojet-new-post-alerts' ), 'pending' => __( 'Waiting for code', 'afojet-new-post-alerts' ), 'unsubscribed' => __( 'Unsubscribed', 'afojet-new-post-alerts' ), 'bounced' => __( 'Not on WhatsApp', 'afojet-new-post-alerts' ), 'queued' => __( 'Queued', 'afojet-new-post-alerts' ), 'sending' => __( 'Sending', 'afojet-new-post-alerts' ), 'done' => __( 'Done', 'afojet-new-post-alerts' ), 'cancelled' => __( 'Cancelled', 'afojet-new-post-alerts' ), ); return isset( $labels[ $status ] ) ? $labels[ $status ] : ucfirst( (string) $status ); } public static function kind_label( $kind ) { $labels = apply_filters( 'afpp_kind_labels', array( 'manual' => __( 'Manual', 'afojet-new-post-alerts' ), 'auto' => __( 'Automatic', 'afojet-new-post-alerts' ), 'test' => __( 'Test', 'afojet-new-post-alerts' ), ) ); return isset( $labels[ $kind ] ) ? $labels[ $kind ] : ucfirst( str_replace( '_', ' ', (string) $kind ) ); } public function menu() { $icon = 'data:image/svg+xml;base64,' . base64_encode( str_replace( array( ' admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'afpp_admin' ), 'i18n' => array( 'confirmSend' => __( 'Click again to send now', 'afojet-new-post-alerts' ), 'confirmResend' => __( 'Already sent – click again to send it again', 'afojet-new-post-alerts' ), 'confirmCancel' => __( 'Click again to stop', 'afojet-new-post-alerts' ), 'sending' => __( 'Sending…', 'afojet-new-post-alerts' ), /* translators: 1: messages handled, 2: total, 3: sent, 4: failed */ 'progress' => __( '%1$s of %2$s handled · %3$s sent · %4$s failed', 'afojet-new-post-alerts' ), 'done' => __( 'Finished.', 'afojet-new-post-alerts' ), 'copied' => __( 'Copied!', 'afojet-new-post-alerts' ), 'network' => __( 'Request failed. Please try again.', 'afojet-new-post-alerts' ), ), ) ); } public function assets( $hook ) { if ( 'toplevel_page_' . self::PAGE === $hook ) { self::enqueue_admin_assets(); } } public function action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'afojet-new-post-alerts' ) . '' ); if ( ! AFPP_Plugin::is_pro() && ! has_action( 'afpp_admin_tab_pro' ) ) { $links[] = '' . esc_html__( 'Go Pro', 'afojet-new-post-alerts' ) . ''; } return $links; } public function notices() { $screen = get_current_screen(); if ( ! current_user_can( 'manage_options' ) || ! $screen || 'plugins' !== $screen->id || AFPP_Settings::api_configured() ) { return; } printf( '

New Post Alerts: %1$s %3$s

', esc_html__( 'connect your WhatsApp Cloud API to start sending new posts to subscribers.', 'afojet-new-post-alerts' ), esc_url( self::url( 'guide' ) ), esc_html__( 'Open setup guide', 'afojet-new-post-alerts' ) ); } /* ------------------------------------------------------------------ * * Actions (export, delete, status changes) * ------------------------------------------------------------------ */ public function handle_actions() { if ( empty( $_REQUEST['afpp_action'] ) || ! current_user_can( 'manage_options' ) ) { // phpcs:ignore WordPress.Security.NonceVerification return; } check_admin_referer( 'afpp_action' ); $action = sanitize_key( wp_unslash( $_REQUEST['afpp_action'] ) ); if ( 'export' === $action ) { $this->export_csv(); } $ids = isset( $_REQUEST['ids'] ) ? array_map( 'absint', (array) wp_unslash( $_REQUEST['ids'] ) ) : array(); $ids = array_filter( $ids ); foreach ( $ids as $id ) { if ( 'delete' === $action ) { AFPP_Subscribers::delete( $id ); } elseif ( in_array( $action, array( 'activate', 'unsubscribe' ), true ) ) { AFPP_Subscribers::set_status( $id, 'activate' === $action ? 'active' : 'unsubscribed' ); } } if ( 'cancel_broadcast' === $action && ! empty( $_REQUEST['broadcast'] ) ) { AFPP_Broadcast::cancel( absint( wp_unslash( $_REQUEST['broadcast'] ) ) ); wp_safe_redirect( self::url( 'broadcasts', array( 'done' => 1 ) ) ); exit; } if ( 'regen_token' === $action ) { update_option( 'afpp_webhook_token', wp_generate_password( 24, false ) ); wp_safe_redirect( self::url( 'settings', array( 'done' => 1 ) ) ); exit; } wp_safe_redirect( self::url( 'subscribers', array( 'done' => count( $ids ) ) ) ); exit; } private function export_csv() { global $wpdb; $t = AFPP_DB::subscribers(); $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT phone, name, status, frequency, created_at, consent_at FROM %i ORDER BY id ASC', $t ), ARRAY_A ); // phpcs:ignore WordPress.DB nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename=new-post-alerts-subscribers-' . gmdate( 'Y-m-d' ) . '.csv' ); $out = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions -- streaming a download. fputcsv( $out, array( 'phone', 'name', 'status', 'frequency', 'created_at_utc', 'consent_at_utc' ) ); foreach ( $rows as $row ) { // Stop spreadsheet apps from running values that start with = + - @ as formulas. $row = array_map( function ( $v ) { return preg_match( '/^[=+\-@]/', (string) $v ) ? "'" . $v : $v; }, $row ); fputcsv( $out, $row ); } fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions exit; } public function ajax_test() { check_ajax_referer( 'afpp_admin', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'forbidden' ), 403 ); } $phone = AFPP_WhatsApp::normalize_phone( isset( $_POST['phone'] ) ? sanitize_text_field( wp_unslash( $_POST['phone'] ) ) : '', AFPP_Settings::get( 'afpp_default_code' ) ); if ( ! AFPP_WhatsApp::is_valid_phone( $phone ) ) { wp_send_json_error( array( 'message' => __( 'Enter a valid number with country code.', 'afojet-new-post-alerts' ) ) ); } if ( ! AFPP_Settings::api_configured() ) { wp_send_json_error( array( 'message' => __( 'Save the Phone number ID, access token and template name first.', 'afojet-new-post-alerts' ) ) ); } $latest = get_posts( array( 'post_type' => AFPP_Broadcast::post_types(), 'numberposts' => 1, 'post_status' => 'publish', ) ); $payload = $latest ? AFPP_Broadcast::payload_for_post( $latest[0] ) : array( 'title' => get_bloginfo( 'name' ), 'link' => home_url( '/' ), 'image' => '', ); $broadcast = (object) array( 'id' => 0, 'kind' => 'test', 'post_id' => $latest ? $latest[0]->ID : 0, 'payload' => $payload, ); $subscriber = (object) array( 'id' => 0, 'phone' => $phone, 'token' => str_repeat( '0', 32 ), ); $message = AFPP_Broadcast::post_message( $broadcast, $subscriber ); if ( is_wp_error( $message ) ) { wp_send_json_error( array( 'message' => $message->get_error_message() ) ); } $result = AFPP_WhatsApp::send_template( $phone, $message['template'], $message['lang'], $message['components'] ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => __( 'WhatsApp said:', 'afojet-new-post-alerts' ) . ' ' . $result->get_error_message() ) ); } update_option( 'afpp_test_ok', time(), false ); /* translators: %s post title */ wp_send_json_success( array( 'message' => sprintf( __( 'Sent! Check WhatsApp for "%s".', 'afojet-new-post-alerts' ), $payload['title'] ) ) ); } /* ------------------------------------------------------------------ * * Page * ------------------------------------------------------------------ */ public function page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $tab = self::current_tab(); ?>

New Post Alerts


New Post Alerts

· v

' . esc_html__( 'Saved.', 'afojet-new-post-alerts' ) . '
'; } ?>
$method(); } else { do_action( 'afpp_admin_tab_' . $tab ); } ?>
%4$s', esc_html( $tag ), esc_attr( $state ), $url ? ' href="' . esc_url( $url ) . '"' : '', esc_html( $text ) ); } public static function card_open( $title, $desc = '', $id = '' ) { echo '

' . esc_html( $title ) . '

'; if ( $desc ) { echo '

' . wp_kses_post( $desc ) . '

'; } echo '
'; } public static function card_close() { echo '
'; } /* ---------- Overview ---------- */ private function tab_overview() { global $wpdb; $q = AFPP_DB::queue(); $sent30 = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE status = 'sent' AND sent_at >= %s", $q, gmdate( 'Y-m-d H:i:s', time() - 30 * DAY_IN_SECONDS ) ) ); // phpcs:ignore WordPress.DB $stats = array( array( __( 'Active subscribers', 'afojet-new-post-alerts' ), AFPP_Subscribers::count( 'active' ) ), array( __( 'Unsubscribed', 'afojet-new-post-alerts' ), AFPP_Subscribers::count( 'unsubscribed' ) ), array( __( 'Messages sent (30 days)', 'afojet-new-post-alerts' ), $sent30 ), array( __( 'Broadcasts', 'afojet-new-post-alerts' ), AFPP_Broadcast::recent( 1, 1 )['total'] ), ); echo '
'; foreach ( $stats as $s ) { echo '
' . esc_html( $s[0] ) . '' . esc_html( number_format_i18n( $s[1] ) ) . '
'; } echo '
'; $steps = array( array( AFPP_Settings::get( 'afpp_phone_number_id' ) && AFPP_Settings::get( 'afpp_access_token' ), __( 'Connect the WhatsApp Cloud API', 'afojet-new-post-alerts' ), self::url( 'guide' ) . '#api' ), array( (bool) get_option( 'afpp_test_ok' ) || AFPP_Broadcast::recent( 1, 1 )['total'] > 0, __( 'Create and approve the message template, then send a test', 'afojet-new-post-alerts' ), self::url( 'guide' ) . '#template' ), array( (bool) get_option( 'afpp_webhook_last_seen' ), __( 'Connect the webhook so STOP replies unsubscribe people', 'afojet-new-post-alerts' ), self::url( 'guide' ) . '#webhook' ), array( AFPP_Subscribers::count( 'all' ) > 0, __( 'Get your first subscriber (the form shows under every post)', 'afojet-new-post-alerts' ), self::url( 'form' ) ), array( AFPP_Broadcast::recent( 1, 1 )['total'] > 0, __( 'Open a published post and click "Send on WhatsApp"', 'afojet-new-post-alerts' ), admin_url( 'edit.php' ) ), ); self::card_open( __( 'Getting started', 'afojet-new-post-alerts' ) ); echo '
    '; foreach ( $steps as $step ) { printf( '
  1. %4$s
  2. ', $step[0] ? 'is-done' : '', $step[0] ? 'dashicons-yes-alt' : 'dashicons-marker', esc_url( $step[2] ), esc_html( $step[1] ) ); } echo '
'; self::card_close(); do_action( 'afpp_overview' ); if ( ! AFPP_Plugin::is_pro() ) { $this->pro_teaser(); } } private function pro_teaser() { echo '

' . esc_html__( 'Do more with New Post Alerts Pro', 'afojet-new-post-alerts' ) . '

' . esc_html__( 'Get New Post Alerts Pro', 'afojet-new-post-alerts' ) . '
'; } public static function pro_features() { return array( array( 'dashicons-controls-repeat', __( 'Auto-send new posts', 'afojet-new-post-alerts' ), __( 'Every post you publish goes out on WhatsApp by itself, with an optional delay and a per-post "don\'t send" switch.', 'afojet-new-post-alerts' ) ), array( 'dashicons-category', __( 'Category segments', 'afojet-new-post-alerts' ), __( 'Readers pick the topics they want. Each post only goes to people who chose its category.', 'afojet-new-post-alerts' ) ), array( 'dashicons-calendar-alt', __( 'Daily & weekly digests', 'afojet-new-post-alerts' ), __( 'Readers can choose one summary a day or a week instead of a message per post.', 'afojet-new-post-alerts' ) ), array( 'dashicons-chart-bar', __( 'Click analytics', 'afojet-new-post-alerts' ), __( 'See delivered, read and clicked numbers for every broadcast, plus subscriber growth.', 'afojet-new-post-alerts' ) ), array( 'dashicons-shield', __( 'OTP-verified opt-in', 'afojet-new-post-alerts' ), __( 'New subscribers confirm their number with a WhatsApp code, so your list has only real numbers.', 'afojet-new-post-alerts' ) ), ); } private function tab_pro() { $this->pro_teaser(); } /* ---------- Subscribers ---------- */ private function tab_subscribers() { // phpcs:disable WordPress.Security.NonceVerification $status = isset( $_GET['status'] ) ? sanitize_key( wp_unslash( $_GET['status'] ) ) : ''; $search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; $paged = isset( $_GET['paged'] ) ? max( 1, absint( $_GET['paged'] ) ) : 1; // phpcs:enable $result = AFPP_Subscribers::query( array( 'status' => $status, 'search' => $search, 'page' => $paged, ) ); $columns = apply_filters( 'afpp_subscriber_columns', array() ); self::card_open( __( 'Subscribers', 'afojet-new-post-alerts' ) ); ?>
__( 'All', 'afojet-new-post-alerts' ), 'active' => self::status_label( 'active' ), 'pending' => self::status_label( 'pending' ), 'unsubscribed' => self::status_label( 'unsubscribed' ), 'bounced' => self::status_label( 'bounced' ) ) as $key => $label ) { $n = AFPP_Subscribers::count( $key ? $key : 'all' ); printf( '%3$s %4$s', $status === $key ? 'is-active' : '', esc_url( self::url( 'subscribers', array( 'status' => $key ) ) ), esc_html( $label ), esc_html( number_format_i18n( $n ) ) ); } ?>
+phone ); ?> name ? $row->name : '—' ); ?> status ) ); ?> source && get_post( (int) $row->source ) ? '' . esc_html( get_the_title( (int) $row->source ) ) . '' : '—'; ?> created_at . ' UTC' ) ) ); ?>
$status, 's' => $search ) ); ?>
'; for ( $i = 1; $i <= $pages; $i++ ) { if ( $i > 3 && $i < $pages - 2 && abs( $i - $paged ) > 2 ) { if ( 4 === $i || $pages - 3 === $i ) { echo '…'; } continue; } printf( '%3$d', $i === $paged ? 'is-active' : '', esc_url( self::url( $tab, array_merge( $args, array( 'paged' => $i ) ) ) ), (int) $i ); } echo ''; } /* ---------- Broadcasts ---------- */ private function tab_broadcasts() { $paged = isset( $_GET['paged'] ) ? max( 1, absint( $_GET['paged'] ) ) : 1; // phpcs:ignore WordPress.Security.NonceVerification $result = AFPP_Broadcast::recent( $paged, 20 ); $extra = apply_filters( 'afpp_broadcast_columns', array() ); self::card_open( __( 'Broadcasts', 'afojet-new-post-alerts' ), __( 'To send a post, open it in the editor and click Send on WhatsApp in the "WhatsApp subscribers" box, or use the link under the post in the Posts list.', 'afojet-new-post-alerts' ) ); ?> payload, true ); $title = $b->post_id ? get_the_title( $b->post_id ) : ( isset( $payload['title'] ) ? $payload['title'] : '—' ); $errors = $b->failed ? AFPP_Broadcast::top_errors( $b->id ) : array(); ?>
post_id ) : ?>
error . ' (' . $e->n . ')' ); ?>
kind ) ); ?> status ) ); ?> total ) ); ?> sent ) ); ?> failed ) ); ?> created_at . ' UTC' ) ) ); ?> status, array( 'queued', 'sending' ), true ) ) : ?>
'; switch ( $type ) { case 'secret': echo ''; break; case 'toggle': echo ''; break; case 'select': echo ''; break; case 'textarea': echo ''; break; case 'color': echo ''; break; case 'number': echo ''; break; case 'time': echo ''; break; default: echo ''; } if ( $help ) { echo '

' . wp_kses_post( $help ) . '

'; } echo '
'; } public static function copy_field( $value ) { echo '
' . esc_html( $value ) . '
'; } private function tab_settings() { AFPP_Settings::ensure_webhook_token(); ?>
Setup guide.', 'afojet-new-post-alerts' ), esc_url( self::url( 'guide' ) ) ) ); self::field( 'afpp_phone_number_id', __( 'Phone number ID', 'afojet-new-post-alerts' ), 'text', __( 'A long number like 1234567890123456 – not your phone number.', 'afojet-new-post-alerts' ) ); self::field( 'afpp_access_token', __( 'Permanent access token', 'afojet-new-post-alerts' ), 'secret', __( 'Use a System User token so it never expires (Setup guide, step 4).', 'afojet-new-post-alerts' ) ); self::field( 'afpp_graph_version', __( 'Graph API version', 'afojet-new-post-alerts' ), 'text', __( 'Leave as is unless Meta tells you otherwise.', 'afojet-new-post-alerts' ) ); self::card_close(); self::card_open( __( 'New post template', 'afojet-new-post-alerts' ), __( 'The approved template used for every post alert. Its variables must match the order below.', 'afojet-new-post-alerts' ) ); self::field( 'afpp_template_name', __( 'Template name', 'afojet-new-post-alerts' ), 'text', __( 'Exactly as in WhatsApp Manager, e.g. new_blog_post', 'afojet-new-post-alerts' ) ); self::field( 'afpp_template_lang', __( 'Template language code', 'afojet-new-post-alerts' ), 'text', __( 'e.g. en_US, en_GB, hi, es', 'afojet-new-post-alerts' ) ); self::field( 'afpp_link_mode', __( 'Where is the post link?', 'afojet-new-post-alerts' ), 'select', sprintf( /* translators: %s site url */ __( 'For a button, set the button URL in the template to %s{{1}} (dynamic).', 'afojet-new-post-alerts' ), esc_html( trailingslashit( home_url() ) ) ), array( 'body' => __( 'In the message text as a variable', 'afojet-new-post-alerts' ), 'button' => __( 'In a "Read post" URL button', 'afojet-new-post-alerts' ), ) ); self::field( 'afpp_unsub_var', __( 'Add unsubscribe link variable', 'afojet-new-post-alerts' ), 'toggle', __( 'Adds one more body variable with the reader\'s personal unsubscribe link.', 'afojet-new-post-alerts' ) ); self::field( 'afpp_header_image', __( 'Template has an image header', 'afojet-new-post-alerts' ), 'toggle', __( 'Sends the featured image (or site icon) as the header.', 'afojet-new-post-alerts' ) ); echo '
    '; foreach ( AFPP_Settings::template_variables() as $i => $label ) { echo '
  1. {{' . esc_html( $i + 1 ) . '}} ' . esc_html( $label ) . '
  2. '; } echo '

' . esc_html__( 'Save to refresh this list.', 'afojet-new-post-alerts' ) . '

'; self::card_close(); self::card_open( __( 'Webhook (unsubscribe & delivery)', 'afojet-new-post-alerts' ), __( 'Paste these in your app → WhatsApp → Configuration → Webhook, then subscribe to the "messages" field.', 'afojet-new-post-alerts' ), 'webhook' ); echo '
'; self::copy_field( AFPP_Webhook::url() ); echo '
'; self::copy_field( (string) get_option( 'afpp_webhook_token' ) ); echo '

' . esc_html__( 'Make a new token', 'afojet-new-post-alerts' ) . '

'; self::field( 'afpp_app_secret', __( 'App secret (required for the webhook)', 'afojet-new-post-alerts' ), 'secret', __( 'App settings → Basic → App secret. Every webhook call must be signed with it; calls are refused until it is saved.', 'afojet-new-post-alerts' ) ); $seen = (int) get_option( 'afpp_webhook_last_seen' ); /* translators: %s time ago */ echo '

' . ( $seen ? esc_html( sprintf( __( 'Last webhook call: %s ago.', 'afojet-new-post-alerts' ), human_time_diff( $seen ) ) ) : esc_html__( 'No webhook call received yet.', 'afojet-new-post-alerts' ) ) . '

'; self::card_close(); self::card_open( __( 'Sending', 'afojet-new-post-alerts' ) ); self::field( 'afpp_batch_size', __( 'Messages per batch', 'afojet-new-post-alerts' ), 'number', __( 'Sent every minute in the background. 40 is safe for most hosts.', 'afojet-new-post-alerts' ) ); self::card_close(); submit_button(); ?>
'; self::copy_field( '[afpp_subscribe_form]' ); echo '

' . esc_html__( 'Use this shortcode, or add the "New Post Alerts subscribe form" block.', 'afojet-new-post-alerts' ) . '

'; self::card_close(); self::card_open( __( 'Text & look', 'afojet-new-post-alerts' ) ); self::field( 'afpp_form_title', __( 'Title', 'afojet-new-post-alerts' ) ); self::field( 'afpp_form_text', __( 'Text', 'afojet-new-post-alerts' ) ); self::field( 'afpp_button_text', __( 'Button', 'afojet-new-post-alerts' ) ); self::field( 'afpp_consent_text', __( 'Consent checkbox', 'afojet-new-post-alerts' ), 'textarea', __( 'WhatsApp requires clear opt-in. Keep "reply STOP" in this text.', 'afojet-new-post-alerts' ) ); self::field( 'afpp_success_text', __( 'Success message', 'afojet-new-post-alerts' ), 'textarea' ); self::field( 'afpp_default_code', __( 'Default country code', 'afojet-new-post-alerts' ), 'text', __( 'Digits only, e.g. 91 for India, 1 for USA. Added when a reader types a local number.', 'afojet-new-post-alerts' ) ); self::field( 'afpp_color', __( 'Colour', 'afojet-new-post-alerts' ), 'color' ); self::card_close(); self::card_open( __( 'Data', 'afojet-new-post-alerts' ) ); self::field( 'afpp_delete_data', __( 'Delete all data when the plugin is deleted', 'afojet-new-post-alerts' ), 'toggle' ); self::card_close(); do_action( 'afpp_form_settings' ); submit_button(); ?>