\\d+)/export-funnel', [[ 'methods' => 'GET', 'callback' => [$this, 'export_funnel'], 'permission_callback' => [$this, 'can_manage_funnels'], ]] ); register_rest_route( Api::NAMESPACE, '/funnels/import', [[ 'methods' => 'POST', 'callback' => [$this, 'import_funnel'], 'permission_callback' => [$this, 'can_manage_funnels'], ]] ); register_rest_route( Api::NAMESPACE, '/templates', [[ 'methods' => 'GET', 'callback' => [$this, 'list_templates'], 'permission_callback' => [$this, 'can_manage_funnels'], ]] ); register_rest_route( Api::NAMESPACE, '/funnels/from-template', [[ 'methods' => 'POST', 'callback' => [$this, 'create_from_template'], 'permission_callback' => [$this, 'can_manage_funnels'], ]] ); } public function export_funnel( WP_REST_Request $request ) : WP_REST_Response|WP_Error { $id = absint( $request['id'] ); $post = get_post( $id ); if ( !$post instanceof \WP_Post || FunnelCpt::POST_TYPE !== $post->post_type ) { return new WP_Error('funnelnode_not_found', __( 'Funnel not found.', 'funnelnode' ), [ 'status' => 404, ]); } $graph = $this->decode_meta( $id, FunnelCpt::META_GRAPH_DRAFT ); if ( !$graph || empty( $graph['nodes'] ) ) { $graph = $this->decode_meta( $id, FunnelCpt::META_GRAPH ); } $settings = $this->decode_meta( $id, FunnelCpt::META_SETTINGS ); $settings = self::strip_secrets( $settings ); $export = [ 'format' => 'funnelnode-funnel', 'version' => self::FORMAT_VERSION, 'min_version' => FUNNELNODE_VERSION, 'exported_at' => gmdate( 'c' ), 'title' => $post->post_title, 'graph' => $graph, 'settings' => $settings, ]; $json = (string) wp_json_encode( $export, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); $filename = sanitize_file_name( (( $post->post_title ?: 'funnel' )) . '-funnelnode.json' ); add_filter( 'rest_pre_serve_request', static function () use($json, $filename) : bool { header( 'Content-Type: application/json; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $json; return true; } ); return new WP_REST_Response(null, 200); } public function import_funnel( WP_REST_Request $request ) : WP_REST_Response|WP_Error { $payload = $request->get_json_params(); if ( !is_array( $payload ) ) { return new WP_Error('funnelnode_invalid', __( 'Invalid request.', 'funnelnode' ), [ 'status' => 400, ]); } $data = ( is_array( $payload['funnel'] ?? null ) ? $payload['funnel'] : $payload ); $title = sanitize_text_field( (string) ($payload['title'] ?? '') ); $id = $this->create_from_data( $data, $title ); if ( is_wp_error( $id ) ) { return $id; } return new WP_REST_Response([ 'id' => $id, ], 201); } public function list_templates() : WP_REST_Response { $templates = []; foreach ( $this->local_templates() as $slug => $data ) { $templates[] = [ 'id' => $slug, 'source' => 'local', 'title' => (string) ($data['meta']['title'] ?? $slug), 'description' => (string) ($data['meta']['description'] ?? ''), 'use_case' => (string) ($data['meta']['use_case'] ?? ''), 'icon' => (string) ($data['meta']['icon'] ?? 'ti-filter'), 'step_count' => ( is_array( $data['graph']['nodes'] ?? null ) ? count( $data['graph']['nodes'] ) : 0 ), 'locale' => (string) ($data['meta']['locale'] ?? ''), ]; } $lang = strtolower( substr( (string) get_user_locale(), 0, 2 ) ); usort( $templates, static function ( array $a, array $b ) use($lang) : int { $rank = static fn( array $t ): int => ( strtolower( substr( (string) $t['locale'], 0, 2 ) ) === $lang ? 0 : 1 ); return $rank( $a ) <=> $rank( $b ); } ); return new WP_REST_Response($templates, 200); } public function create_from_template( WP_REST_Request $request ) : WP_REST_Response|WP_Error { $template = sanitize_text_field( (string) $request->get_param( 'template' ) ); $title = sanitize_text_field( (string) $request->get_param( 'title' ) ); $data = $this->local_templates()[$template] ?? null; if ( !is_array( $data ) ) { return new WP_Error('funnelnode_not_found', __( 'Template not found.', 'funnelnode' ), [ 'status' => 404, ]); } $id = $this->create_from_data( $data, $title ); if ( is_wp_error( $id ) ) { return $id; } return new WP_REST_Response([ 'id' => $id, ], 201); } private function create_from_data( array $data, string $title ) : int|WP_Error { $graph = ( is_array( $data['graph'] ?? null ) ? $data['graph'] : null ); $settings = ( is_array( $data['settings'] ?? null ) ? $data['settings'] : [] ); $min = (string) ($data['min_version'] ?? ''); if ( '' !== $min && preg_match( '/^\\d+(\\.\\d+){0,2}$/', $min ) && version_compare( FUNNELNODE_VERSION, $min, '<' ) ) { return new WP_Error('funnelnode_version', sprintf( /* translators: 1: required plugin version, 2: installed plugin version */ __( 'This funnel requires FunnelNode %1$s or newer — this site runs version %2$s. Please update the plugin, then import again.', 'funnelnode' ), $min, FUNNELNODE_VERSION ), [ 'status' => 400, ]); } if ( !$graph || empty( $graph['nodes'] ) || !is_array( $graph['nodes'] ) ) { return new WP_Error('funnelnode_invalid', __( 'This file is not a valid funnel export.', 'funnelnode' ), [ 'status' => 400, ]); } if ( count( $graph['nodes'] ) > 200 ) { return new WP_Error('funnelnode_invalid', __( 'This file is not a valid funnel export.', 'funnelnode' ), [ 'status' => 400, ]); } $graph = FunnelData::sanitize_graph( $graph ); $settings = FunnelData::sanitize_settings( self::strip_secrets( $settings ) ); $encoded = (string) wp_json_encode( [ 'graph' => $graph, 'settings' => $settings, ] ); foreach ( $this->collect_ids( $graph ) as $old_id ) { $prefix = (string) strstr( $old_id, '_', true ); $new_id = $prefix . '_' . strtolower( wp_generate_password( 8, false ) ); $encoded = str_replace( $old_id, $new_id, $encoded ); } $decoded = json_decode( $encoded, true ); $graph = $decoded['graph'] ?? $graph; $settings = $decoded['settings'] ?? $settings; if ( '' === $title ) { $title = (string) ($data['title'] ?? $data['meta']['title'] ?? __( 'New Funnel', 'funnelnode' )); } $id = wp_insert_post( [ 'post_type' => FunnelCpt::POST_TYPE, 'post_title' => $title, 'post_status' => 'draft', ], true ); if ( is_wp_error( $id ) ) { return $id; } update_post_meta( $id, FunnelCpt::META_GRAPH, wp_slash( (string) wp_json_encode( $graph ) ) ); update_post_meta( $id, FunnelCpt::META_SETTINGS, wp_slash( (string) wp_json_encode( $settings ) ) ); update_post_meta( $id, FunnelCpt::META_VERSION, FUNNELNODE_VERSION ); return (int) $id; } private function collect_ids( array $graph ) : array { $ids = []; foreach ( (array) ($graph['nodes'] ?? []) as $node ) { if ( !empty( $node['id'] ) ) { $ids[] = (string) $node['id']; } $config = (array) ($node['data']['config'] ?? []); foreach ( [ 'options', 'fields', 'steps', 'variants', 'rules', 'blocks' ] as $list ) { foreach ( (array) ($config[$list] ?? []) as $item ) { if ( !empty( $item['id'] ) ) { $ids[] = (string) $item['id']; } foreach ( (array) ($item['items'] ?? []) as $sub ) { if ( !empty( $sub['id'] ) ) { $ids[] = (string) $sub['id']; } } } } } foreach ( (array) ($graph['edges'] ?? []) as $edge ) { if ( !empty( $edge['id'] ) ) { $ids[] = (string) $edge['id']; } } usort( $ids, static fn( string $a, string $b ): int => strlen( $b ) <=> strlen( $a ) ); return array_values( array_unique( $ids ) ); } private function local_templates() : array { $templates = []; $dirs = [FUNNELNODE_DIR . 'includes/templates/']; $files = []; foreach ( $dirs as $dir ) { $files = array_merge( $files, ( glob( $dir . '*.json' ) ?: [] ) ); } foreach ( $files as $file ) { $data = json_decode( (string) file_get_contents( $file ), true ); if ( is_array( $data ) && !empty( $data['graph']['nodes'] ) ) { $templates[basename( $file, '.json' )] = $data; } } return $templates; } private static function strip_secrets( array $settings ) : array { unset($settings['secret']); foreach ( $settings as $key => $value ) { if ( is_array( $value ) ) { $settings[$key] = self::strip_secrets( $value ); } } return $settings; } private function decode_meta( int $id, string $key ) : array { $raw = (string) get_post_meta( $id, $key, true ); if ( '' === $raw ) { return []; } $decoded = json_decode( $raw, true ); return ( is_array( $decoded ) ? $decoded : [] ); } }