booted ) { return; } $this->booted = true; $logger = new Logger(); $smslog = new SmsLog( $logger ); $dispatcher = new Dispatcher( $logger, $smslog ); $listener = new StatusListener( $logger ); $notes = new NoteListener( $logger ); // Order status change -> enqueue async SMS job. add_action( 'woocommerce_order_status_changed', array( $listener, 'on_status_changed' ), 10, 4 ); // Manual order note -> enqueue async SMS job. add_action( 'woocommerce_order_note_added', array( $notes, 'on_note_added' ), 10, 2 ); // Async worker (Action Scheduler passes a single payload array). add_action( SENDLY_HOOK, array( $dispatcher, 'handle' ), 10, 1 ); // Activity-log retention purge. add_action( SENDLY_PURGE_HOOK, array( $smslog, 'purge' ) ); add_action( 'init', array( $this, 'ensure_scheduled_tasks' ) ); // Suppress SMS during admin bulk status changes (when enabled). add_filter( 'handle_bulk_actions-edit-shop_order', array( $listener, 'flag_bulk' ), 1, 3 ); add_filter( 'handle_bulk_actions-woocommerce_page_wc-orders', array( $listener, 'flag_bulk' ), 1, 3 ); // Settings page + custom field renderers. add_filter( 'woocommerce_get_settings_pages', array( $this, 'register_settings_page' ) ); add_action( 'woocommerce_admin_field_sendly_info', array( Settings::class, 'render_info_field' ) ); add_action( 'woocommerce_admin_field_sendly_token', array( Settings::class, 'render_token_field' ) ); add_action( 'woocommerce_admin_field_sendly_dlr', array( Settings::class, 'render_dlr_field' ) ); add_action( 'woocommerce_admin_field_sendly_test', array( Settings::class, 'render_test_field' ) ); add_action( 'woocommerce_admin_field_sendly_register', array( Registration::class, 'render' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) ); // Test-SMS AJAX endpoint. $test = new TestSms( $logger ); add_action( 'wp_ajax_sendly_test', array( $test, 'handle' ) ); add_action( 'wp_ajax_sendly_regen_secret', array( Settings::class, 'ajax_regenerate_secret' ) ); // In-plugin Sendly signup (connect flow: prefilled hosted form + return leg). $registration = new Registration( $logger ); add_action( 'wp_ajax_sendly_connect_start', array( $registration, 'handle_connect_start' ) ); add_action( 'admin_init', array( $registration, 'handle_connect_return' ) ); // Plugin list links + onboarding notice. add_filter( 'plugin_action_links_' . SENDLY_BASENAME, array( $this, 'plugin_action_links' ) ); add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 ); add_action( 'admin_notices', array( $this, 'onboarding_notice' ) ); add_action( 'admin_init', array( $this, 'handle_onboarding_dismiss' ) ); // DLR delivery-receipt REST endpoint. $dlr = new DlrWebhook( $logger, $smslog ); add_action( 'rest_api_init', array( $dlr, 'register_routes' ) ); // Checkout consent / opt-out. $optout = new OptOut(); $optout->register(); // Privacy (export/erase) integration. $smslog->register_privacy_hooks(); } /** * Build the active SMS provider. Filterable so other gateways can be added. * * @return SmsProvider */ public static function provider() { /** * Filter the SMS provider implementation. * * @param SmsProvider $provider Default Sendly provider. */ return apply_filters( 'sendly_provider', new SendlyProvider() ); } /** * Register the WooCommerce settings page. * * @param array $pages Existing settings pages. * @return array */ public function register_settings_page( $pages ) { $pages[] = new Settings(); return $pages; } /** * Make sure the recurring purge action is scheduled. */ public function ensure_scheduled_tasks() { // Admin-only: avoids an Action Scheduler DB lookup on every front-end request. if ( ! is_admin() ) { return; } if ( ! function_exists( 'as_schedule_recurring_action' ) || ! function_exists( 'as_next_scheduled_action' ) ) { return; } if ( false === as_next_scheduled_action( SENDLY_PURGE_HOOK, array(), SENDLY_GROUP ) ) { as_schedule_recurring_action( time() + HOUR_IN_SECONDS, DAY_IN_SECONDS, SENDLY_PURGE_HOOK, array(), SENDLY_GROUP ); } } /** * Enqueue admin JS only on our settings tab. * * @param string $hook Current admin page hook. */ public function enqueue_admin_assets( $hook ) { if ( 'woocommerce_page_wc-settings' !== $hook ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only tab routing. $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : ''; if ( 'sendly' !== $tab ) { return; } wp_enqueue_script( 'sendly-admin', SENDLY_URL . 'assets/js/admin.js', array( 'jquery' ), SENDLY_VERSION, true ); wp_localize_script( 'sendly-admin', 'sendlyAdmin', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'sendly_test' ), 'regenNonce' => wp_create_nonce( 'sendly_regen' ), 'gsm7Enabled' => Options::gsm7_enabled() ? 1 : 0, 'i18n' => array( 'sending' => __( 'Sending…', 'sendly' ), /* translators: 1: encoding name (GSM-7/UCS-2), 2: character count, 3: number of SMS segments. */ 'segments' => __( 'Encoding: %1$s · %2$d chars · %3$d segment(s)', 'sendly' ), 'testEmpty' => __( 'Enter a phone number to test.', 'sendly' ), 'error' => __( 'Error', 'sendly' ), 'requestFailed' => __( 'Request failed.', 'sendly' ), 'regenConfirm' => __( 'Regenerate the webhook secret? The old URL will stop working.', 'sendly' ), ), ) ); // Registration screen (connect flow) config. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only section routing. $section = isset( $_GET['section'] ) ? sanitize_key( wp_unslash( $_GET['section'] ) ) : ''; if ( 'account' === $section ) { wp_localize_script( 'sendly-admin', 'sendlyReg', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'sendly_register' ), 'i18n' => array( 'sending' => __( 'Redirecting to sendly.link…', 'sendly' ), 'requestFailed' => __( 'Request failed.', 'sendly' ), ), ) ); } } /** * "Settings" + "Sendly panel" links on the Plugins list row. * * @param string[] $links Existing action links. * @return string[] */ public function plugin_action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'sendly' ) . '' ); return $links; } /** * Row meta: links to the Sendly panel and in-plugin registration. * * @param string[] $links Existing row meta. * @param string $file Plugin basename. * @return string[] */ public function plugin_row_meta( $links, $file ) { if ( SENDLY_BASENAME !== $file ) { return $links; } $links[] = '' . esc_html__( 'Sendly panel', 'sendly' ) . ''; $links[] = '' . esc_html__( 'Get started', 'sendly' ) . ''; return $links; } /** * Persist the onboarding-notice dismissal (nonce-protected link). */ public function handle_onboarding_dismiss() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- verified below via check_admin_referer. if ( ! isset( $_GET['sendly-dismiss-onboarding'] ) ) { return; } check_admin_referer( 'sendly_dismiss_onboarding' ); if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } update_option( Options::ONBOARDING_DISMISSED, 'yes', false ); wp_safe_redirect( remove_query_arg( array( 'sendly-dismiss-onboarding', '_wpnonce' ) ) ); exit; } /** * Onboarding notice until the plugin is configured, an account is registered, * or the notice is dismissed. */ public function onboarding_notice() { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || 'plugins' !== $screen->id ) { return; } // Self-resolving: hidden once configured, registered, or dismissed. if ( '' !== Options::token() || '' !== (string) get_option( Options::ACCOUNT_EMAIL, '' ) || 'yes' === get_option( Options::ONBOARDING_DISMISSED, 'no' ) ) { return; } // Not on the registration screen itself. // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['tab'] ) && 'sendly' === sanitize_key( wp_unslash( $_GET['tab'] ) ) ) { return; } $dismiss_url = wp_nonce_url( add_query_arg( 'sendly-dismiss-onboarding', '1' ), 'sendly_dismiss_onboarding' ); echo '

Sendly SMS: '; printf( /* translators: %s: settings screen URL. */ wp_kses_post( __( 'Add your API token to start sending order status SMS. Open settings.', 'sendly' ) ), esc_url( admin_url( 'admin.php?page=wc-settings&tab=sendly' ) ) ); echo ' ' . esc_html__( 'Don\'t show again', 'sendly' ) . ''; echo '

'; } /** * Activation: generate a webhook secret and schedule recurring tasks. */ public static function activate() { if ( '' === (string) get_option( Options::WEBHOOK_SECRET, '' ) ) { add_option( Options::WEBHOOK_SECRET, wp_generate_password( 40, false, false ), '', false ); } else { // Ensure the existing secret is not autoloaded (sites upgraded from an // older version) without needing wp_set_option_autoload() (WP 6.4+). $existing_secret = get_option( Options::WEBHOOK_SECRET ); delete_option( Options::WEBHOOK_SECRET ); add_option( Options::WEBHOOK_SECRET, $existing_secret, '', false ); } if ( function_exists( 'as_schedule_recurring_action' ) && function_exists( 'as_next_scheduled_action' ) ) { if ( false === as_next_scheduled_action( SENDLY_PURGE_HOOK, array(), SENDLY_GROUP ) ) { as_schedule_recurring_action( time() + HOUR_IN_SECONDS, DAY_IN_SECONDS, SENDLY_PURGE_HOOK, array(), SENDLY_GROUP ); } } } /** * Deactivation: clear our scheduled actions. */ public static function deactivate() { if ( function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( SENDLY_PURGE_HOOK, array(), SENDLY_GROUP ); } } }