[ option => [ default, type ] ]. * Pro adds its own groups through the afpp_settings_schema filter. */ public static function schema() { $schema = array( 'afpp_api_group' => array( 'afpp_phone_number_id' => array( '', 'text' ), 'afpp_access_token' => array( '', 'secret' ), 'afpp_graph_version' => array( 'v23.0', 'graph_version' ), 'afpp_template_name' => array( 'new_blog_post', 'slug' ), 'afpp_template_lang' => array( 'en_US', 'text' ), 'afpp_link_mode' => array( 'body', 'link_mode' ), 'afpp_unsub_var' => array( 0, 'bool' ), 'afpp_header_image' => array( 0, 'bool' ), 'afpp_webhook_token' => array( '', 'text' ), 'afpp_app_secret' => array( '', 'secret' ), 'afpp_batch_size' => array( 40, 'batch' ), ), 'afpp_form_group' => array( 'afpp_form_title' => array( __( 'Get new posts on WhatsApp', 'afojet-new-post-alerts' ), 'text' ), 'afpp_form_text' => array( __( 'One message when we publish something new. No spam.', 'afojet-new-post-alerts' ), 'text' ), 'afpp_button_text' => array( __( 'Subscribe', 'afojet-new-post-alerts' ), 'text' ), 'afpp_consent_text' => array( __( 'I agree to receive new post alerts on WhatsApp. I can reply STOP at any time to unsubscribe.', 'afojet-new-post-alerts' ), 'text' ), 'afpp_success_text' => array( __( 'You are subscribed! We will message you on WhatsApp when a new post is published.', 'afojet-new-post-alerts' ), 'text' ), 'afpp_default_code' => array( '91', 'calling_code' ), 'afpp_auto_insert' => array( 1, 'bool' ), 'afpp_floating' => array( 0, 'bool' ), 'afpp_color' => array( '#25d366', 'color' ), 'afpp_delete_data' => array( 0, 'bool' ), ), ); return apply_filters( 'afpp_settings_schema', $schema ); } private static function find( $name ) { foreach ( self::schema() as $options ) { if ( isset( $options[ $name ] ) ) { return $options[ $name ]; } } return null; } public static function get( $name ) { $def = self::find( $name ); if ( ! $def ) { return get_option( $name ); } $value = get_option( $name, $def[0] ); if ( ( '' === $value || null === $value ) && in_array( $def[1], array( 'graph_version', 'color', 'batch', 'link_mode', 'slug' ), true ) ) { return $def[0]; } return $value; } public static function enabled( $name ) { return (bool) (int) self::get( $name ); } public static function api_configured() { return self::get( 'afpp_phone_number_id' ) && self::get( 'afpp_access_token' ) && self::get( 'afpp_template_name' ); } public static function register() { foreach ( self::schema() as $group => $options ) { foreach ( $options as $name => $def ) { register_setting( $group, $name, array( 'default' => $def[0], 'sanitize_callback' => function ( $value ) use ( $def, $name ) { return AFPP_Settings::sanitize( $value, $def[1], $def[0], $name ); }, ) ); } } } public static function sanitize( $value, $type, $default, $name ) { switch ( $type ) { case 'bool': return empty( $value ) ? 0 : 1; case 'color': $c = sanitize_hex_color( (string) $value ); return $c ? $c : $default; case 'batch': return max( 5, min( 200, absint( $value ) ) ); case 'int': return absint( $value ); case 'calling_code': $v = preg_replace( '/\D+/', '', (string) $value ); return ( '' !== $v && strlen( $v ) <= 4 ) ? $v : ''; case 'graph_version': $v = trim( (string) $value ); return preg_match( '/^v\d{1,2}\.\d$/', $v ) ? $v : $default; case 'slug': // WhatsApp template names: lowercase letters, numbers and underscores. return preg_replace( '/[^a-z0-9_]/', '', strtolower( trim( (string) $value ) ) ); case 'link_mode': return in_array( $value, array( 'body', 'button' ), true ) ? $value : $default; case 'time': return preg_match( '/^([01]\d|2[0-3]):[0-5]\d$/', (string) $value ) ? $value : $default; case 'array_int': return array_values( array_filter( array_map( 'absint', (array) $value ) ) ); case 'array_key': return array_values( array_filter( array_map( 'sanitize_key', (array) $value ) ) ); case 'secret': $v = trim( (string) $value ); if ( '' !== $v && false !== strpos( $v, '••••' ) ) { return get_option( $name, '' ); // Masked value = unchanged. } return sanitize_text_field( $v ); default: return sanitize_text_field( (string) $value ); } } public static function mask( $secret ) { $secret = (string) $secret; if ( '' === $secret ) { return ''; } return strlen( $secret ) < 12 ? '••••••••' : substr( $secret, 0, 4 ) . '••••••••' . substr( $secret, -4 ); } /** A random token Meta sends back when you register the webhook. */ public static function ensure_webhook_token() { if ( ! get_option( 'afpp_webhook_token' ) ) { update_option( 'afpp_webhook_token', wp_generate_password( 24, false ) ); } } /** Number of {{n}} body variables the main template must have, in order. */ public static function template_variables() { $vars = array( __( 'Post title', 'afojet-new-post-alerts' ) ); if ( 'body' === self::get( 'afpp_link_mode' ) ) { $vars[] = __( 'Post link', 'afojet-new-post-alerts' ); } if ( self::enabled( 'afpp_unsub_var' ) ) { $vars[] = __( 'Unsubscribe link', 'afojet-new-post-alerts' ); } return $vars; } }