' . 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 '';
}
echo ' ';
echo '';
if ( 1 !== $id ) {
echo '' . esc_html__( 'Delete pipeline', 'nodebook' ) . ' ';
}
echo ' ';
}
private static function stage_row( $i, $key, $label ) {
$name = 'stages[' . $i . ']';
echo '' . esc_html__( 'Stage name', 'nodebook' ) . ' ';
echo '↑ ' . esc_html__( 'Move up', 'nodebook' ) . ' ↓ ' . esc_html__( 'Move down', 'nodebook' ) . ' ' . wp_kses( Nodebook_Icons::svg( 'trash', 15 ), Nodebook_Icons::kses() ) . '' . esc_html__( 'Remove', 'nodebook' ) . ' ';
}
private static function add_card( array $all ) {
echo '' . esc_html__( 'New pipeline', 'nodebook' ) . ' ';
}
/* ------------------------------------------------------------------ *
* 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;
}
}