Nodebook_Admin::url( 'nodebook-segments', array( 'action' => 'edit' ) ) ) ); $all = Nodebook_Segments::all(); echo '

' . esc_html__( 'Smart segments update themselves from rules (for example “customers who spent more than 100 and visited in the last 30 days”). Static segments are lists you fill by hand, by bulk action or on import.', 'nodebook' ) . '

'; echo ''; if ( ! $all ) { echo ''; } foreach ( $all as $s ) { $sid = (int) $s['id']; $view = Nodebook_Admin::url( 'nodebook', array( 'segment' => $sid ) ); $export = wp_nonce_url( admin_url( 'admin-post.php?action=nodebook_export&entity=contacts&segment=' . $sid ), 'nodebook_export' ); $delete = wp_nonce_url( admin_url( 'admin-post.php?action=nodebook_delete_segment&id=' . $sid ), 'nodebook_delete_segment_' . $sid ); $pinned = in_array( $sid, self::pinned(), true ); $pin = wp_nonce_url( admin_url( 'admin-post.php?action=nodebook_pin_segment&id=' . $sid ), 'nodebook_pin_segment_' . $sid ); echo ''; echo ''; echo ''; echo ''; } echo '
' . esc_html__( 'Name', 'nodebook' ) . '' . esc_html__( 'Type', 'nodebook' ) . '' . esc_html__( 'Contacts', 'nodebook' ) . '
' . esc_html__( 'No segments yet.', 'nodebook' ) . '
' . ( $pinned ? '★' : '☆' ) . ' ' . esc_html( $s['name'] ) . '' . esc_html( 'smart' === $s['type'] ? __( 'Smart', 'nodebook' ) : __( 'Static', 'nodebook' ) ) . '' . esc_html( number_format_i18n( self::count( $sid ) ) ) . '' . esc_html__( 'View contacts', 'nodebook' ) . '' . esc_html__( 'Edit', 'nodebook' ) . '' . ( Nodebook_Team::can_export() ? '' . wp_kses( Nodebook_Icons::svg( 'download', 15 ), Nodebook_Icons::kses() ) . '' : '' ) . '' . wp_kses( Nodebook_Icons::svg( 'trash', 15 ), Nodebook_Icons::kses() ) . '
'; $names = array_map( 'strtolower', wp_list_pluck( $all, 'name' ) ); $tpls = array_filter( Nodebook_Segments::templates(), static function ( $t ) use ( $names ) { return ! in_array( strtolower( $t[0] ), $names, true ); } ); if ( $tpls ) { echo '

' . esc_html__( 'Start from a template', 'nodebook' ) . '

'; foreach ( $tpls as $key => $t ) { echo '
'; wp_nonce_field( 'nodebook_segment_template' ); echo '' . esc_html( $t[0] ) . '

' . esc_html( $t[1] ) . '

'; } echo '
'; } Nodebook_Admin::footer(); } const PINS = 'nodebook_pinned_segments'; /** Segments the current user pinned above the contacts list (at most 8, deleted ones dropped). */ /** Contacts in a segment, as far as this user can see them. */ private static function count( $sid ) { return Nodebook_Team::restricted() ? (int) Nodebook_Contacts::query( array( 'segment' => (int) $sid, 'per_page' => 1 ) )['total'] : Nodebook_Segments::cached_count( $sid ); } public static function pinned() { $ids = get_user_meta( get_current_user_id(), self::PINS, true ); $ids = is_array( $ids ) ? array_map( 'intval', $ids ) : array(); $all = array_map( 'intval', wp_list_pluck( Nodebook_Segments::all(), 'id' ) ); return array_values( array_intersect( $ids, $all ) ); } public static function pin() { $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_pin_segment_' . $id ); $ids = self::pinned(); if ( in_array( $id, $ids, true ) ) { $ids = array_values( array_diff( $ids, array( $id ) ) ); } elseif ( Nodebook_Segments::get( $id ) && count( $ids ) < 8 ) { $ids[] = $id; } update_user_meta( get_current_user_id(), self::PINS, $ids ); $back = wp_get_referer() ? wp_get_referer() : Nodebook_Admin::url( 'nodebook-segments' ); Nodebook_Admin::redirect( $back ); } /** Pinned segments as quick filters above the contacts list. */ public static function pinned_bar( $current = 0 ) { $ids = self::pinned(); if ( ! $ids ) { return; } echo ''; } public static function from_template() { Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_segment_template' ); $key = isset( $_POST['template'] ) ? sanitize_key( wp_unslash( $_POST['template'] ) ) : ''; $tpls = Nodebook_Segments::templates(); if ( ! isset( $tpls[ $key ] ) ) { Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook-segments' ) ); } $t = $tpls[ $key ]; $id = Nodebook_Segments::save( array( 'name' => $t[0], 'type' => 'smart', 'rules' => array( 'match' => isset( $t[3] ) ? $t[3] : 'all', 'conditions' => $t[2], ), ) ); Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook', array( 'segment' => $id ) ), 'template' ); } private static function edit( $id ) { $seg = $id ? Nodebook_Segments::get( $id ) : null; if ( ! $seg ) { $seg = array( 'id' => 0, 'name' => '', 'type' => 'smart', 'rules' => array( 'match' => 'all', 'conditions' => array(), ), ); } Nodebook_Admin::header( $seg['id'] ? __( 'Edit segment', 'nodebook' ) : __( 'New segment', 'nodebook' ), array( __( '← All segments', 'nodebook' ) => Nodebook_Admin::url( 'nodebook-segments' ) ) ); ?>
>

'; /* translators: %s: "all" or "any" dropdown */ $sentence = sprintf( esc_html__( 'Contacts that match %s of these conditions:', 'nodebook' ), $select ); echo wp_kses( $sentence, array( 'select' => array( 'name' => true, 'aria-label' => true, ), 'option' => array( 'value' => true, 'selected' => true, ), ) ); ?>

>

' . esc_html( sprintf( /* translators: %s: number of contacts */ __( 'View %s matching contacts', 'nodebook' ), number_format_i18n( Nodebook_Segments::count( $seg['id'] ) ) ) ) . '

'; } Nodebook_Admin::footer(); } public static function save() { Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_save_segment' ); $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; $rules = isset( $_POST['rules'] ) && is_array( $_POST['rules'] ) ? map_deep( wp_unslash( $_POST['rules'] ), 'sanitize_text_field' ) : array(); if ( isset( $rules['conditions'] ) && is_array( $rules['conditions'] ) ) { $rules['conditions'] = array_values( $rules['conditions'] ); } $id = Nodebook_Segments::save( array( 'name' => isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '', 'type' => isset( $_POST['type'] ) ? sanitize_key( wp_unslash( $_POST['type'] ) ) : 'static', 'rules' => $rules, ), $id ); Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook-segments', array( 'action' => 'edit', 'id' => $id ) ), 'saved' ); } public static function delete() { $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_delete_segment_' . $id ); Nodebook_Segments::delete( $id ); Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook-segments' ), 'deleted' ); } }