' . esc_html__( 'A pipeline is a set of stages for one kind of deal, like sales, rentals or partnerships. Each has its own board under Deals.', 'nodebook' ) . '

'; if ( ! Nodebook_Deals::on() ) { echo '

' . esc_html__( 'Deals are switched off. Switch them on under Settings to use pipelines.', 'nodebook' ) . '

'; Nodebook_Admin::footer(); return; } echo '
'; foreach ( $ids as $n => $id ) { self::card( (int) $id, $all[ $id ], $counts[ $id ] ?? 0, $all, 0 === $n, count( $ids ) - 1 === $n ); } if ( count( $ids ) < Nodebook_Pipelines::MAX ) { self::add_card( $all ); } echo '
'; Nodebook_Admin::footer(); } private static function action( $op, $id ) { echo ''; wp_nonce_field( 'nodebook_pipeline' ); } private static function card( $id, array $p, $open, array $all, $top, $bottom ) { $post = esc_url( admin_url( 'admin-post.php' ) ); echo '
'; echo '

' . esc_html( $p['name'] ) . '

' . esc_html( sprintf( /* translators: %s: number of open deals */ _n( '%s open deal', '%s open deals', $open, 'nodebook' ), number_format_i18n( $open ) ) ) . ' ' . esc_html__( 'Open board', 'nodebook' ) . ''; echo ''; foreach ( array( 'up' => array( '↑', __( 'Move up', 'nodebook' ), $top ), 'down' => array( '↓', __( 'Move down', 'nodebook' ), $bottom ), ) as $op => $b ) { echo '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped above. self::action( $op, $id ); echo '
'; } echo '
'; echo '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped above. self::action( 'save', $id ); echo '

'; echo '

' . esc_html__( 'Stages, in order. Won and lost are not stages. Deals in a stage you remove move to the first stage.', 'nodebook' ) . '

'; echo '
    '; $i = 0; foreach ( (array) $p['stages'] as $key => $label ) { self::stage_row( $id . '_' . $i++, $key, $label ); } echo '

'; if ( 1 !== $id ) { echo '
' . esc_html__( 'Delete pipeline', 'nodebook' ) . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped above. self::action( 'delete', $id ); echo '

'; } echo '
'; } private static function stage_row( $i, $key, $label ) { $name = 'stages[' . $i . ']'; echo '
  • '; echo '
  • '; } private static function add_card( array $all ) { echo '

    ' . esc_html__( 'New pipeline', 'nodebook' ) . '

    '; self::action( 'add', 0 ); echo '

    '; echo '

    '; } /* ------------------------------------------------------------------ * * Saving * ------------------------------------------------------------------ */ public static function save() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'nodebook' ), 403 ); } check_admin_referer( 'nodebook_pipeline' ); $op = isset( $_POST['op'] ) ? sanitize_key( wp_unslash( $_POST['op'] ) ) : ''; $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; $back = self::url(); if ( 'add' === $op ) { $from = isset( $_POST['from'] ) ? absint( $_POST['from'] ) : 0; $all = Nodebook_Deals::pipelines(); $labels = $from && isset( $all[ $from ] ) ? (array) $all[ $from ]['stages'] : array(); $new = Nodebook_Pipelines::add( $name, $labels ); Nodebook_Admin::redirect( $new ? $back . '#nb-pipe-' . $new : $back, $new ? 'saved' : 'pipefail' ); } if ( 'save' === $op ) { Nodebook_Pipelines::update( $id, $name, self::labels_from_post() ); } elseif ( 'up' === $op || 'down' === $op ) { Nodebook_Pipelines::move( $id, 'up' === $op ? -1 : 1 ); } elseif ( 'delete' === $op ) { $to = isset( $_POST['to'] ) ? absint( $_POST['to'] ) : 0; Nodebook_Admin::redirect( $back, Nodebook_Pipelines::delete( $id, $to ) ? 'deleted' : 'pipefail' ); } Nodebook_Admin::redirect( $back . '#nb-pipe-' . $id, 'saved' ); } /** Stage rows from the form, in screen order: [ key-or-new => label ]. */ private static function labels_from_post() { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- nonce and capability checked in save(); each value is sanitized below. $rows = isset( $_POST['stages'] ) && is_array( $_POST['stages'] ) ? wp_unslash( $_POST['stages'] ) : array(); $out = array(); $n = 0; foreach ( $rows as $r ) { if ( ! is_array( $r ) ) { continue; } $key = sanitize_key( (string) ( $r['key'] ?? '' ) ); $label = sanitize_text_field( (string) ( $r['label'] ?? '' ) ); $out[ '' !== $key ? $key : 'new_' . $n ] = $label; ++$n; } return array_filter( $out, 'strlen' ) ? $out : null; } }