__( 'Email: All Path Messaging', 'all-path-messaging' ), 'menu_slug' => str_replace( '_', '-', SLUG ), 'menu_capability' => apply_filters( 'all_path_email_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\Email\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 Email_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 emails. * @param string $subject Message to send. * @param string $body Body of the message. * @param string $from_name From name. * @param string $from_email From email. * @param mixed[] $headers Headers to send. * @param string $adapter Adapter to use. * * @return array{ * deliveredTo: int, * type: string, * results: array> * }|array> * }>|WP_Error */ function send( array $to = [], string $subject = '', string $body = '', string $from_name = '', string $from_email = '', array $headers = [], string $adapter = '' ): array|WP_Error { // Get all adapters. $adapters = get_adapters(); // Override the settings adapter by the parameter's adapter. if ( empty( $adapter ) ) { $adapter = strval( get_option( SLUG . '_active_adapter', '' ) ); } // Get from name. if ( empty( $from_name ) ) { $from_name = strval( get_option( SLUG . '_from_name', '' ) ); } // Get from email. if ( empty( $from_email ) ) { $from_email = strval( get_option( SLUG . '_from_email', '' ) ); } // Return early if adapter not found. if ( empty( $adapters[ $adapter ] ) ) { return new WP_Error( 'adapter_not_found', __( 'Adapter not found.', 'all-path-messaging' ) ); } // Check for email common settings. if ( empty( $from_name ) || empty( $from_email ) ) { return new WP_Error( 'email_common_settings_not_configured', __( 'Email common settings not configured properly.', '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' ) ); } // Get the headers. $headers = [ 'reply_to_name' => strval( $headers['reply_to_name'] ?? '' ), 'reply_to_email' => strval( $headers['reply_to_email'] ?? '' ), 'cc' => array_filter( (array) ( $headers['cc'] ?? [] ), 'is_array' ), 'bcc' => array_filter( (array) ( $headers['bcc'] ?? [] ), 'is_array' ), 'attachments' => (array) ( $headers['attachments'] ?? [] ), ]; // Attachments. $attachments = []; // Build the attachments. foreach ( $headers['attachments'] as $key => $attachment ) { // Skip if empty attachment. if ( empty( $attachment ) || ! is_string( $attachment ) ) { continue; } // Build the attachment. $attachments[] = new Attachment( name: is_string( $key ) ? $key : $attachment, path: $attachment, type: '', ); } // Initialize the message. $message = new Email( to: $to, subject: $subject, content: $body, fromName: $from_name, fromEmail: $from_email, replyToName: $headers['reply_to_name'], replyToEmail: $headers['reply_to_email'], cc: $headers['cc'], bcc: $headers['bcc'], attachments: $attachments, html: true, ); // Send the message. return $adapter_object->send( $message ); }