array( 'name' => __( 'Rule Sets', 'visigate' ), 'singular_name' => __( 'Rule Set', 'visigate' ), 'menu_name' => visigate_brand(), 'add_new' => __( 'Add Rule Set', 'visigate' ), 'add_new_item' => __( 'Add New Rule Set', 'visigate' ), 'edit_item' => __( 'Edit Rule Set', 'visigate' ), 'all_items' => __( 'Rule Sets', 'visigate' ), ), 'public' => false, 'show_ui' => true, 'show_in_rest' => false, 'supports' => array( 'title' ), 'menu_icon' => 'dashicons-visibility', 'menu_position' => 58, 'rewrite' => false, 'capabilities' => array( 'edit_post' => $cap, 'read_post' => $cap, 'delete_post' => $cap, 'edit_posts' => $cap, 'edit_others_posts' => $cap, 'delete_posts' => $cap, 'publish_posts' => $cap, 'read_private_posts' => $cap, 'create_posts' => $cap, ), ) ); } public static function all_sets() { $posts = get_posts( array( 'post_type' => self::CPT, 'post_status' => 'publish', 'numberposts' => 200, 'orderby' => 'title', 'order' => 'ASC', 'no_found_rows' => true, ) ); $out = array(); foreach ( $posts as $p ) { $out[] = array( 'id' => $p->ID, 'title' => $p->post_title ? $p->post_title : '#' . $p->ID ); } return $out; } /* ---------------------------- Meta box ---------------------------- */ public static function add_metabox() { add_meta_box( 'visigate-rules', __( 'Visibility rules', 'visigate' ), array( __CLASS__, 'render_metabox' ), self::CPT, 'normal', 'high' ); } public static function render_metabox( $post ) { $rules = get_post_meta( $post->ID, self::META, true ); if ( ! is_array( $rules ) ) { $rules = array( 'action' => 'show', 'groups' => array() ); } wp_nonce_field( 'visigate_save_set', 'visigate_nonce' ); ?>
ID && 'auto-draft' !== $post->post_status ) : ?>
[visigate set="ID; ?>"]…[/visigate]
[visigate set="' . (int) $post_id . '"]';
}
if ( 'visigate_rule' === $col ) {
$r = get_post_meta( $post_id, self::META, true );
if ( empty( $r['groups'] ) ) {
esc_html_e( 'No conditions (always visible)', 'visigate' );
return;
}
$n = 0;
foreach ( $r['groups'] as $g ) {
$n += count( $g['conditions'] ?? array() );
}
printf(
/* translators: 1: Show/Hide, 2: condition count, 3: group count */
esc_html__( '%1$s — %2$d condition(s) in %3$d group(s)', 'visigate' ),
'hide' === ( $r['action'] ?? 'show' ) ? esc_html__( 'Hide when', 'visigate' ) : esc_html__( 'Show when', 'visigate' ),
(int) $n,
count( $r['groups'] )
);
}
}
/* ------------------------------ REST ------------------------------ */
public static function register_routes() {
register_rest_route(
'visigate/v1',
'/search',
array(
'methods' => 'GET',
'permission_callback' => function () {
return current_user_can( visigate_capability() );
},
'args' => array(
'type' => array( 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_key' ),
'q' => array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field' ),
),
'callback' => array( __CLASS__, 'rest_search' ),
)
);
}
/** Async token-field lookups. Add-ons supply rows via `visigate_rest_search` => [ [id, label], … ]. */
public static function rest_search( WP_REST_Request $req ) {
$rows = apply_filters( 'visigate_rest_search', array(), $req['type'], $req['q'] );
return rest_ensure_response( is_array( $rows ) ? array_slice( $rows, 0, 20 ) : array() );
}
}