__( 'SMS: All Path Messaging', 'all-path-messaging' ), 'menu_slug' => str_replace( '_', '-', SLUG ), 'menu_capability' => apply_filters( 'all_path_sms_user_capability', 'manage_options' ), 'menu_renderer' => [ Admin::get_instance(), 'options_page' ], ]; // Return the services. return $services; } ); } /** * Get all adapters. * * Add new adapter like this - * add_filter( * \Souptik\AllPathMessaging\SMS\SLUG . '_adapters', * function( array $adapters = [] ): array { * $adapters[ 'your_adapter' ] = [ * 'name' => __( 'Your Adapter', 'all-path-messaging' ), * 'adapter' => new Your_Adapter_Class(), * 'options' => Your_Adapter_Class::get_settings_fields(), * ]; * * return $adapters; * } * ); * * @return array * }> */ function get_adapters(): array { // Get all adapters. $adapters = (array) apply_filters( SLUG . '_adapters', [] ); // Filtered adapters. $filtered_adapters = []; // Loop over the adapters. foreach ( $adapters as $adapter => $data ) { // Return early if invalid adapter. if ( ! is_array( $data ) || empty( $data['name'] ) || empty( $data['adapter'] ) || ! $data['adapter'] instanceof SMS_Adapter ) { continue; } // Build the adapter options. $adapter_options = []; // Check if options are present. if ( ! empty( $data['options'] ) && is_array( $data['options'] ) ) { // Insert the options. foreach ( $data['options'] as $option => $option_data ) { // Skip if any of the data is missing. if ( empty( strval( $option ) ) || empty( $option_data['label'] ) || empty( $option_data['type'] ) || empty( $option_data['sanitize_callback'] ) ) { continue; } // Add the option. $adapter_options[ strval( $option ) ] = [ 'label' => strval( $option_data['label'] ), 'type' => strval( $option_data['type'] ), 'sanitize_callback' => strval( $option_data['sanitize_callback'] ), ]; } } // Add the adapter. $filtered_adapters[ strval( $adapter ) ] = [ 'name' => $data['name'], 'adapter' => $data['adapter'], 'options' => $adapter_options, ]; } // Return the adapters. return $filtered_adapters; } /** * Send the message. * * @param string[] $to Array of mobile numbers. * @param string $message Message to send. * @param string $adapter Adapter to use. * * @return array{ * deliveredTo: int, * type: string, * results: array> * }|array> * }>|WP_Error */ function send( array $to = [], string $message = '', string $adapter = '' ): array|WP_Error { // Get all adapters. $adapters = get_adapters(); // Get the active adapter - Should this be a filter? $active_adapter = strval( get_option( SLUG . '_active_adapter', '' ) ); // Override the settings adapter by the parameter's adapter. if ( empty( $adapter ) ) { $adapter = $active_adapter; } // Return early if adapter not found. if ( empty( $adapters[ $adapter ] ) ) { return new WP_Error( 'adapter_not_found', __( 'Adapter not found.', 'all-path-messaging' ) ); } // Get the adapter. $adapter_object = $adapters[ $adapter ]['adapter']->get_adapter(); // Return early if adapter not configured properly. if ( null === $adapter_object ) { return new WP_Error( 'adapter_not_configured', __( 'Adapter not configured.', 'all-path-messaging' ) ); } // Initialize the message. $message = new SMS( to: $to, content: $message ); // Send the message. return $adapter_object->send( $message ); }