export_service = $export_service; add_action( 'admin_post_acafs_export_flamingo_messages', array( $this, 'acafs_export_flamingo_messages' ) ); add_action( 'acafs_render_import_export_page', array( $this, 'acafs_render_export_section' ) ); add_action( 'admin_notices', array( $this, 'acafs_show_export_notice' ) ); add_action( 'admin_post_acafs_get_message_count', array( $this, 'acafs_get_message_count' ) ); } /** * Export Flamingo messages to a JSON file, optionally filtered by date range. */ public function acafs_export_flamingo_messages() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'ac-advanced-flamingo-settings' ) ); } check_admin_referer( 'acafs_export_flamingo_messages' ); $start_date = isset( $_GET['start_date'] ) ? sanitize_text_field( wp_unslash( $_GET['start_date'] ) ) : ''; $end_date = isset( $_GET['end_date'] ) ? sanitize_text_field( wp_unslash( $_GET['end_date'] ) ) : ''; $export_all = isset( $_GET['export_all'] ) ? (int) $_GET['export_all'] : 0; $args = $this->get_export_args( $start_date, $end_date, $export_all ); $is_filtered = null !== $args['from'] || null !== $args['to']; $filename = 'flamingo-messages'; if ( $is_filtered ) { $filename .= "-{$start_date}_to_{$end_date}"; } $filename .= '-' . time() . '.json'; $upload_dir = wp_upload_dir(); $file_path = trailingslashit( $upload_dir['basedir'] ) . $filename; $result = $this->export_service->export_to_file( $file_path, $args ); if ( is_wp_error( $result ) ) { wp_die( esc_html( $result->get_error_message() ) ); } set_transient( 'acafs_export_file', trailingslashit( $upload_dir['baseurl'] ) . $filename, 30 ); set_transient( 'acafs_export_success', $result['count'], 5 * MINUTE_IN_SECONDS ); wp_safe_redirect( admin_url( 'admin.php?page=acafs-message-sync&export_success=1' ) ); exit; } /** * Show a notice after export */ public function acafs_show_export_notice() { $count = get_transient( 'acafs_export_success' ); if ( ! $count ) { return; } $file_url = get_transient( 'acafs_export_file' ); ?>

export_service->count( $this->get_export_args( $start_date, $end_date, $export_all ) ); if ( is_wp_error( $count ) ) { wp_die( esc_html( $count->get_error_message() ) ); } echo (int) $count; exit; } /** * Translate legacy admin fields into transport-neutral service arguments. * * @param string $start_date Start date. * @param string $end_date End date. * @param int $export_all Whether all messages were requested. * @return array */ private function get_export_args( $start_date, $end_date, $export_all ) { return array( 'from' => $export_all ? null : $start_date, 'to' => $export_all ? null : $end_date, ); } /** * Render the export section of the import/export page */ public function acafs_render_export_section() { $count_nonce = wp_create_nonce( 'acafs_get_message_count' ); ?>