namespace, '/settings', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_settings' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'save_settings' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); // Export / Import endpoints. register_rest_route( $this->namespace, '/export', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'export_data' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); register_rest_route( $this->namespace, '/import', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'import_data' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); // Danger Zone — explicit, confirmed "Delete all data". register_rest_route( $this->namespace, '/data/erase-all', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'erase_all_data' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); } public function get_settings( $request ) { return $this->success( array( 'brand' => get_option( 'mailpuff_brand_settings', array( 'company_name' => Mailpuff_Mailer::site_name(), 'logo_url' => '', 'address' => '', 'admin_email' => get_option( 'admin_email' ), ) ), 'email' => get_option( 'mailpuff_email_settings', array() ), 'compliance' => get_option( 'mailpuff_compliance_settings', array() ), 'setup_complete' => get_option( 'mailpuff_setup_complete', '0' ), 'delivery' => Mailpuff_Mailer::get_instance()->status(), ) ); } /** * Save settings by section. All fields sent at top level alongside 'section'. */ public function save_settings( $request ) { $section = sanitize_text_field( $request->get_param( 'section' ) ); $params = $request->get_json_params(); if ( empty( $params ) || ! is_array( $params ) ) { $params = $request->get_params(); } if ( empty( $section ) ) { return $this->error( 'missing_section', __( 'Section parameter is required.', 'mailpuff' ), 400 ); } try { switch ( $section ) { case 'brand': $social_in = isset( $params['social'] ) && is_array( $params['social'] ) ? $params['social'] : array(); $social_out = array(); foreach ( array( 'facebook', 'twitter', 'instagram', 'linkedin', 'youtube' ) as $net ) { $social_out[ $net ] = isset( $social_in[ $net ] ) ? esc_url_raw( $social_in[ $net ] ) : ''; } $sanitized = array( 'company_name' => isset( $params['company_name'] ) ? sanitize_text_field( $params['company_name'] ) : '', 'logo_url' => isset( $params['logo_url'] ) ? esc_url_raw( $params['logo_url'] ) : '', 'address' => isset( $params['address'] ) ? sanitize_textarea_field( $params['address'] ) : '', 'admin_email' => isset( $params['admin_email'] ) ? sanitize_text_field( $params['admin_email'] ) : '', 'social' => $social_out, ); update_option( 'mailpuff_brand_settings', $sanitized ); break; case 'email': $current = get_option( 'mailpuff_email_settings', array() ); $sanitized = array( 'from_name' => isset( $params['from_name'] ) ? sanitize_text_field( $params['from_name'] ) : '', 'from_email' => isset( $params['from_email'] ) ? sanitize_email( $params['from_email'] ) : '', 'reply_to_name' => isset( $params['reply_to_name'] ) ? sanitize_text_field( $params['reply_to_name'] ) : '', 'reply_to' => isset( $params['reply_to'] ) ? sanitize_email( $params['reply_to'] ) : '', 'footer' => isset( $params['footer'] ) ? wp_kses_post( $params['footer'] ) : '', // Batch size and pause: a save that does not send them keeps // what is stored. 'batch_size' => array_key_exists( 'batch_size', $params ) ? min( 50, max( 1, absint( $params['batch_size'] ) ) ) : ( is_array( $current ) && isset( $current['batch_size'] ) ? absint( $current['batch_size'] ) : 10 ), 'batch_pause' => array_key_exists( 'batch_pause', $params ) ? min( 3600, max( 1, absint( $params['batch_pause'] ) ) ) : ( is_array( $current ) && isset( $current['batch_pause'] ) ? absint( $current['batch_pause'] ) : 5 ), // Hourly cap: 0 is no cap. A save that does not send the field keeps // what is stored, and an unset cap stays unset. 'max_per_hour' => array_key_exists( 'max_per_hour', $params ) ? max( 0, absint( $params['max_per_hour'] ) ) : ( is_array( $current ) && isset( $current['max_per_hour'] ) ? absint( $current['max_per_hour'] ) : null ), // The owner's confirmation that the host delivers mail. A save // that does not send the field keeps what is stored. 'host_delivers_mail' => array_key_exists( 'host_delivers_mail', $params ) ? ( ! empty( $params['host_delivers_mail'] ) ? '1' : '0' ) : ( is_array( $current ) && ! empty( $current['host_delivers_mail'] ) ? (string) $current['host_delivers_mail'] : '0' ), ); if ( null === $sanitized['max_per_hour'] ) { unset( $sanitized['max_per_hour'] ); } // The option also holds settings saved elsewhere (the signup // boxes, the welcome email switch): change only what was sent. $sent = array_intersect_key( $sanitized, $params ); foreach ( array( 'max_per_hour', 'host_delivers_mail' ) as $kept ) { if ( array_key_exists( $kept, $sanitized ) ) { $sent[ $kept ] = $sanitized[ $kept ]; } } update_option( 'mailpuff_email_settings', array_merge( is_array( $current ) ? $current : array(), $sent ) ); break; case 'compliance': $flags = array( 'open_tracking', 'one_click_unsubscribe', 'delete_on_user_delete', 'wp_data_export', ); $sanitized = get_option( 'mailpuff_compliance_settings', array() ); $sanitized = is_array( $sanitized ) ? $sanitized : array(); foreach ( $flags as $flag ) { // A flag the request does not send keeps its stored value. if ( array_key_exists( $flag, $params ) ) { $sanitized[ $flag ] = ! empty( $params[ $flag ] ) ? '1' : '0'; } } update_option( 'mailpuff_compliance_settings', $sanitized ); break; case 'setup': update_option( 'mailpuff_setup_complete', '1' ); break; default: return $this->error( 'invalid_section', __( 'Unknown settings section.', 'mailpuff' ), 400 ); } } catch ( \Exception $e ) { return $this->error( 'save_error', $e->getMessage(), 500 ); } catch ( \Error $e ) { return $this->error( 'save_error', $e->getMessage(), 500 ); } return $this->success( array( 'saved' => true ) ); } /** * GET /export — returns all plugin data as JSON. */ public function export_data( $request ) { $include = $request->get_param( 'include' ); $include = is_array( $include ) ? $include : array(); $data = Mailpuff_Export_Service::get_instance()->export( $include ); return $this->success( $data ); } /** * POST /import — accepts JSON export data, imports it. */ public function import_data( $request ) { $files = $request->get_file_params(); $body = null; // Accept either a file upload or JSON body. if ( ! empty( $files['file']['tmp_name'] ) && is_uploaded_file( $files['file']['tmp_name'] ) ) { $contents = file_get_contents( $files['file']['tmp_name'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $body = json_decode( $contents, true ); } else { $body = $request->get_json_params(); if ( empty( $body ) ) { $body = $request->get_param( 'data' ); if ( is_string( $body ) ) { $body = json_decode( $body, true ); } } } if ( ! is_array( $body ) || empty( $body['data'] ) ) { return $this->error( 'invalid_import', __( 'Invalid import file or data.', 'mailpuff' ), 400 ); } $mode = sanitize_text_field( $request->get_param( 'mode' ) ); if ( ! in_array( $mode, array( 'merge', 'replace' ), true ) ) { $mode = 'merge'; } $result = Mailpuff_Export_Service::get_instance()->import( $body, $mode ); if ( is_wp_error( $result ) ) { return $result; } return $this->success( $result ); } /** * POST /data/erase-all — irreversibly delete ALL Mailpuff data. * * Requires an explicit confirmation token (`confirm` === 'DELETE') in the * request body so this can never fire accidentally. Drops all tables, * deletes all options, unschedules jobs, removes imported files, then * recreates empty tables so the plugin is left clean and working. */ public function erase_all_data( $request ) { $confirm = $request->get_param( 'confirm' ); if ( 'DELETE' !== $confirm ) { return $this->error( 'confirm_required', __( 'Type DELETE to confirm erasing all data.', 'mailpuff' ), 400 ); } Mailpuff_Data_Deleter::delete_all_data(); return $this->success( array( 'erased' => true ) ); } }