[ids], items => [ id => [ name, stages? ] ], next => int ]. * * @return array{order:int[], items:array, next:int} */ public static function data() { $d = get_option( self::OPTION, null ); if ( null === $d ) { // Pipelines made with an add-on before 1.38 (option nodebook_pro_pipelines) move here once. $old = get_option( 'nodebook_pro_pipelines', null ); $d = is_array( $old ) ? $old : array(); if ( $d ) { update_option( self::OPTION, $d, true ); } } $d = is_array( $d ) ? $d : array(); $d = wp_parse_args( $d, array( 'order' => array( 1 ), 'items' => array(), 'next' => 2, ) ); $d['items'] = (array) $d['items']; $d['order'] = array_values( array_unique( array_map( 'intval', (array) $d['order'] ) ) ); if ( ! in_array( 1, $d['order'], true ) ) { array_unshift( $d['order'], 1 ); } foreach ( array_keys( $d['items'] ) as $id ) { if ( 1 !== (int) $id && ! in_array( (int) $id, $d['order'], true ) ) { $d['order'][] = (int) $id; } } $d['order'] = array_values( array_filter( $d['order'], static function ( $id ) use ( $d ) { return 1 === $id || isset( $d['items'][ $id ] ); } ) ); $d['next'] = max( 2, (int) $d['next'], ...array_map( 'intval', array_keys( $d['items'] + array( 1 => 1 ) ) ) ); return $d; } private static function store( array $d ) { update_option( self::OPTION, $d, true ); } /** * All pipelines in their order: id => [ name, stages ]. Pipeline 1's stages are Nodebook_Deals' own. * * @param array $first_stages Stages of pipeline 1. */ public static function build( array $first_stages ) { $d = self::data(); $out = array(); foreach ( $d['order'] as $id ) { if ( 1 === $id ) { $name = trim( (string) ( $d['items'][1]['name'] ?? '' ) ); $out[1] = array( 'name' => '' !== $name ? $name : __( 'Sales', 'nodebook' ), 'stages' => $first_stages, ); continue; } $item = $d['items'][ $id ]; if ( ! empty( $item['stages'] ) ) { $out[ $id ] = array( 'name' => (string) $item['name'], 'stages' => (array) $item['stages'], ); } } return $out; } /** * Add a pipeline. * * @param string $name Name. * @param array $labels Stages in order: a list of labels, or key => label to keep keys (so deals moved * between pipelines keep their stage). Default: Nodebook's default stages. * @return int New pipeline ID, 0 when full or nameless. */ public static function add( $name, array $labels = array() ) { $name = mb_substr( trim( sanitize_text_field( (string) $name ) ), 0, 60 ); $d = self::data(); if ( '' === $name || count( $d['order'] ) >= self::MAX ) { return 0; } $id = (int) $d['next']; $labels = $labels ? $labels : Nodebook_Deals::default_stages(); $keep = array_filter( $labels, 'is_string', ARRAY_FILTER_USE_KEY ); $stages = Nodebook_Deals::save_stages( $labels, $id, $keep ); $d['items'][ $id ] = array( 'name' => $name, 'stages' => $stages, ); $d['order'][] = $id; $d['next'] = $id + 1; self::store( $d ); return $id; } /** * Rename a pipeline and save its stages (old key => label keeps deals where they are). * * @param int $id Pipeline ID. * @param string $name New name ('' keeps it). * @param array|null $labels Ordered labels, or null to leave stages alone. */ public static function update( $id, $name, $labels = null ) { $id = (int) $id; $d = self::data(); $name = mb_substr( trim( sanitize_text_field( (string) $name ) ), 0, 60 ); if ( 1 !== $id && ! isset( $d['items'][ $id ] ) ) { return false; } if ( 1 === $id ) { $d['items'][1] = array( 'name' => $name ); self::store( $d ); if ( is_array( $labels ) ) { Nodebook_Deals::save_stages( $labels, 1 ); } return true; } if ( '' !== $name ) { $d['items'][ $id ]['name'] = $name; } if ( is_array( $labels ) ) { $d['items'][ $id ]['stages'] = Nodebook_Deals::save_stages( $labels, $id, (array) $d['items'][ $id ]['stages'] ); } self::store( $d ); return true; } /** Move a pipeline one place up (-1) or down (1). */ public static function move( $id, $dir ) { $d = self::data(); $i = array_search( (int) $id, $d['order'], true ); $j = false === $i ? false : $i + ( $dir < 0 ? -1 : 1 ); if ( false === $i || $j < 0 || $j >= count( $d['order'] ) ) { return false; } list( $d['order'][ $i ], $d['order'][ $j ] ) = array( $d['order'][ $j ], $d['order'][ $i ] ); self::store( $d ); return true; } /** * Delete a pipeline; its deals move to another pipeline (same stage if it has one, else the first stage). * Nodebook's own pipeline stays. */ public static function delete( $id, $to ) { global $wpdb; $id = (int) $id; $to = (int) $to; $d = self::data(); $all = Nodebook_Deals::pipelines(); if ( 1 === $id || ! isset( $d['items'][ $id ] ) || $id === $to || ! isset( $all[ $to ] ) ) { return false; } $target = (array) $all[ $to ]['stages']; $first = (string) array_key_first( $target ); $table = Nodebook_Deals::table(); foreach ( array_keys( (array) $d['items'][ $id ]['stages'] ) as $key ) { $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- bulk move of our own rows. $table, array( 'pipeline_id' => $to, 'stage' => isset( $target[ $key ] ) ? (string) $key : $first, ), array( 'pipeline_id' => $id, 'stage' => (string) $key, ) ); } // Anything left (closed deals in odd stages): to the first stage. $wpdb->update( $table, array( 'pipeline_id' => $to, 'stage' => $first ), array( 'pipeline_id' => $id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- bulk move of our own rows. unset( $d['items'][ $id ] ); $d['order'] = array_values( array_diff( $d['order'], array( $id ) ) ); self::store( $d ); return true; } /** Open deals per pipeline. */ public static function counts() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- live counts of our own table. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT pipeline_id, COUNT(*) AS n FROM %i WHERE outcome = '' GROUP BY pipeline_id", Nodebook_Deals::table() ), ARRAY_A ); return array_map( 'intval', wp_list_pluck( (array) $rows, 'n', 'pipeline_id' ) ); } }