WP_REST_Server::READABLE, 'callback' => [$this, 'list_funnels'], 'permission_callback' => [$this, 'can_edit'], ], [ 'methods' => WP_REST_Server::CREATABLE, 'callback' => [$this, 'create_funnel'], 'permission_callback' => [$this, 'can_edit'], ]] ); register_rest_route( self::NAMESPACE, '/funnels/(?P\\d+)', [[ 'methods' => WP_REST_Server::READABLE, 'callback' => [$this, 'get_funnel'], 'permission_callback' => [$this, 'can_edit'], 'args' => [ 'id' => $this->id_arg(), ], ], [ 'methods' => WP_REST_Server::EDITABLE, 'callback' => [$this, 'update_funnel'], 'permission_callback' => [$this, 'can_edit'], 'args' => [ 'id' => $this->id_arg(), ], ], [ 'methods' => WP_REST_Server::DELETABLE, 'callback' => [$this, 'delete_funnel'], 'permission_callback' => [$this, 'can_edit'], 'args' => [ 'id' => $this->id_arg(), ], ]] ); register_rest_route( self::NAMESPACE, '/funnels/(?P\\d+)/duplicate', [[ 'methods' => WP_REST_Server::CREATABLE, 'callback' => [$this, 'duplicate_funnel'], 'permission_callback' => [$this, 'can_edit'], 'args' => [ 'id' => $this->id_arg(), ], ]] ); register_rest_route( self::NAMESPACE, '/onboarding/complete', [[ 'methods' => WP_REST_Server::CREATABLE, 'callback' => [$this, 'complete_onboarding'], 'permission_callback' => [$this, 'can_edit'], ]] ); register_rest_route( self::NAMESPACE, '/onboarding/reset-tours', [[ 'methods' => WP_REST_Server::CREATABLE, 'callback' => [$this, 'reset_tours'], 'permission_callback' => [$this, 'can_edit'], ]] ); } public function can_edit() : bool { return current_user_can( Plugin::capability() ); } public function list_funnels() : WP_REST_Response { $posts = get_posts( [ 'post_type' => FunnelCpt::POST_TYPE, 'post_status' => ['publish', 'draft'], 'numberposts' => -1, 'orderby' => 'modified', 'order' => 'DESC', ] ); $global_recipients = '' !== trim( (string) (Settings::all()['email']['default_recipients'] ?? '') ); $items = array_map( function ( \WP_Post $p ) use($global_recipients) : array { $settings = $this->decode_meta( $p->ID, FunnelCpt::META_SETTINGS ); $email = (array) ($settings['email'] ?? []); $draft = $this->decode_meta( $p->ID, FunnelCpt::META_GRAPH_DRAFT ); $live = $this->decode_meta( $p->ID, FunnelCpt::META_GRAPH ); $graph = ( !empty( $draft['nodes'] ) ? $draft : $live ); $item = [ 'id' => $p->ID, 'title' => $p->post_title, 'status' => $this->funnel_status( $p->ID ), 'submission_count' => $this->submission_count( $p->ID ), 'lead_count' => $this->lead_count( $p->ID ), 'modified' => $p->post_modified_gmt, 'step_count' => ( is_array( $graph['nodes'] ?? null ) ? count( $graph['nodes'] ) : 0 ), 'draft_differs' => !empty( $draft ) && wp_json_encode( $draft ) !== wp_json_encode( $live ), 'checklist' => [ 'recipients' => $global_recipients || '' !== trim( (string) ($email['admin_email'] ?? '') ), ], ]; return $item; }, $posts ); return new WP_REST_Response($items, 200); } public function create_funnel( WP_REST_Request $request ) : WP_REST_Response|WP_Error { $title = sanitize_text_field( (string) ($request->get_param( 'title' ) ?? '') ); if ( '' === $title ) { $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( wp_json_encode( $this->default_graph() ) ) ); update_post_meta( $id, FunnelCpt::META_SETTINGS, wp_slash( wp_json_encode( $this->default_settings() ) ) ); update_post_meta( $id, FunnelCpt::META_VERSION, FUNNELNODE_VERSION ); return new WP_REST_Response($this->funnel_payload( (int) $id ), 201); } public function complete_onboarding( WP_REST_Request $request ) : WP_REST_Response|WP_Error { $key = sanitize_key( (string) $request->get_param( 'key' ) ); if ( 'wizard' === $key ) { if ( !current_user_can( 'manage_options' ) ) { return new WP_Error('funnelnode_forbidden', __( 'Invalid request.', 'funnelnode' ), [ 'status' => 403, ]); } update_option( 'funnelnode_onboarding_done', 1 ); return new WP_REST_Response([ 'ok' => true, ], 200); } if ( in_array( $key, ['tour_list', 'tour_builder'], true ) ) { update_user_meta( get_current_user_id(), 'funnelnode_' . $key . '_done', 1 ); return new WP_REST_Response([ 'ok' => true, ], 200); } return new WP_Error('funnelnode_invalid', __( 'Invalid request.', 'funnelnode' ), [ 'status' => 400, ]); } public function reset_tours() : WP_REST_Response { delete_user_meta( get_current_user_id(), 'funnelnode_tour_list_done' ); delete_user_meta( get_current_user_id(), 'funnelnode_tour_builder_done' ); return new WP_REST_Response([ 'ok' => true, ], 200); } public function duplicate_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, ]); } /* translators: %s: title of the duplicated funnel */ $copy_title = sprintf( __( '%s (Copy)', 'funnelnode' ), $post->post_title ); $new_id = wp_insert_post( [ 'post_type' => FunnelCpt::POST_TYPE, 'post_title' => $copy_title, 'post_status' => 'draft', ], true ); if ( is_wp_error( $new_id ) ) { return $new_id; } foreach ( [FunnelCpt::META_GRAPH, FunnelCpt::META_GRAPH_DRAFT, FunnelCpt::META_SETTINGS] as $meta_key ) { $raw = (string) get_post_meta( $id, $meta_key, true ); if ( '' !== $raw ) { update_post_meta( (int) $new_id, $meta_key, wp_slash( $raw ) ); } } update_post_meta( (int) $new_id, FunnelCpt::META_VERSION, FUNNELNODE_VERSION ); return new WP_REST_Response([ 'id' => (int) $new_id, ], 201); } public function get_funnel( WP_REST_Request $request ) : WP_REST_Response|WP_Error { $id = (int) $request['id']; if ( !$this->funnel_exists( $id ) ) { return $this->not_found(); } return new WP_REST_Response($this->funnel_payload( $id ), 200); } public function update_funnel( WP_REST_Request $request ) : WP_REST_Response|WP_Error { $id = (int) $request['id']; if ( !$this->funnel_exists( $id ) ) { return $this->not_found(); } $params = $request->get_json_params() ?? []; if ( isset( $params['title'] ) ) { wp_update_post( [ 'ID' => $id, 'post_title' => sanitize_text_field( (string) $params['title'] ), ] ); } $publish = !empty( $params['publish'] ); if ( isset( $params['graph'] ) && is_array( $params['graph'] ) ) { $encoded = wp_slash( wp_json_encode( FunnelData::sanitize_graph( $params['graph'] ) ) ); update_post_meta( $id, FunnelCpt::META_GRAPH_DRAFT, $encoded ); if ( $publish ) { update_post_meta( $id, FunnelCpt::META_GRAPH, $encoded ); } } if ( isset( $params['settings'] ) && is_array( $params['settings'] ) ) { $params['settings'] = FunnelData::sanitize_settings( $params['settings'] ); update_post_meta( $id, FunnelCpt::META_SETTINGS, wp_slash( wp_json_encode( $params['settings'] ) ) ); } if ( $publish ) { wp_update_post( [ 'ID' => $id, 'post_status' => 'publish', ] ); } elseif ( !empty( $params['unpublish'] ) ) { wp_update_post( [ 'ID' => $id, 'post_status' => 'draft', ] ); } return new WP_REST_Response($this->funnel_payload( $id ), 200); } public function delete_funnel( WP_REST_Request $request ) : WP_REST_Response|WP_Error { global $wpdb; $id = (int) $request['id']; if ( !$this->funnel_exists( $id ) ) { return $this->not_found(); } do_action( 'funnelnode_funnel_deleting', $id ); $wpdb->delete( Migrations::table( Migrations::TABLE_SUBMISSIONS ), [ 'funnel_id' => $id, ], ['%d'] ); wp_delete_post( $id, true ); return new WP_REST_Response([ 'deleted' => true, 'id' => $id, ], 200); } private function funnel_payload( int $id ) : array { $post = get_post( $id ); $live = $this->decode_meta( $id, FunnelCpt::META_GRAPH ); $draft = $this->decode_meta( $id, FunnelCpt::META_GRAPH_DRAFT ); $settings = $this->decode_meta( $id, FunnelCpt::META_SETTINGS ); $settings = ( !empty( $settings ) ? $settings : $this->default_settings() ); return [ 'id' => $id, 'title' => ( $post ? $post->post_title : '' ), 'status' => $this->funnel_status( $id ), 'graph' => ( !empty( $draft ) ? $draft : $live ), 'settings' => $settings, 'draft_differs' => !empty( $draft ) && wp_json_encode( $draft ) !== wp_json_encode( $live ), 'version' => ( (string) get_post_meta( $id, FunnelCpt::META_VERSION, true ) ?: FUNNELNODE_VERSION ), ]; } 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 : [] ); } private function funnel_exists( int $id ) : bool { $post = get_post( $id ); return $post instanceof \WP_Post && FunnelCpt::POST_TYPE === $post->post_type; } private function funnel_status( int $id ) : string { return ( 'publish' === get_post_status( $id ) ? 'published' : 'draft' ); } private function submission_count( int $id ) : int { global $wpdb; $table = Migrations::table( Migrations::TABLE_SUBMISSIONS ); return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$table} WHERE funnel_id = %d", $id ) ); } private function lead_count( int $id ) : int { global $wpdb; $table = Migrations::table( Migrations::TABLE_SUBMISSIONS ); return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$table} WHERE funnel_id = %d AND completed = 1", $id ) ); } private function id_arg() : array { return [ 'required' => true, 'validate_callback' => static fn( $value ): bool => is_numeric( $value ), 'sanitize_callback' => 'absint', ]; } private function not_found() : WP_Error { return new WP_Error('funnelnode_funnel_not_found', __( 'Funnel not found.', 'funnelnode' ), [ 'status' => 404, ]); } private function default_graph() : array { return [ 'nodes' => [], 'edges' => [], ]; } private function default_settings() : array { $settings = [ 'redirect_url' => '', 'progress' => 'count', 'email' => [ 'notify_admin' => true, 'admin_email' => '', 'subject_admin' => '', ], 'privacy' => [ 'consent_required' => true, 'retention_days' => 365, 'anonymize_ip' => true, 'store_user_agent' => false, ], 'spam' => [ 'honeypot' => true, 'min_completion_seconds' => 3, ], 'i18n' => [ 'locale' => str_replace( '_', '-', get_locale() ), 'rtl' => is_rtl(), ], 'status' => 'draft', ]; return $settings; } }