__( 'Text', '23h-product-features' ),
'textarea' => __( 'Long text', '23h-product-features' ),
'number' => __( 'Number', '23h-product-features' ),
'email' => __( 'Email', '23h-product-features' ),
'url' => __( 'URL', '23h-product-features' ),
'password' => __( 'Password', '23h-product-features' ),
'date' => __( 'Date', '23h-product-features' ),
'color' => __( 'Color', '23h-product-features' ),
'select' => __( 'Dropdown list', '23h-product-features' ),
'radio' => __( 'Single choice (cards)', '23h-product-features' ),
'checkbox' => __( 'Checkboxes (multiple choice)', '23h-product-features' ),
'hidden' => __( 'Hidden field', '23h-product-features' ),
'heading' => __( 'Title / text', '23h-product-features' ),
'separator' => __( 'Separator', '23h-product-features' ),
);
}
public static function choice_types() {
return array( 'select', 'radio', 'checkbox' );
}
public static function layout_types() {
return array( 'heading', 'separator' );
}
public static function register() {
register_post_type(
self::CPT,
array(
'labels' => array(
'name' => __( 'Product option forms', '23h-product-features' ),
'singular_name' => __( 'Form', '23h-product-features' ),
'menu_name' => __( 'Product options', '23h-product-features' ),
'all_items' => __( 'Forms', '23h-product-features' ),
'add_new' => __( 'Add form', '23h-product-features' ),
'add_new_item' => __( 'New form', '23h-product-features' ),
'edit_item' => __( 'Edit form', '23h-product-features' ),
'new_item' => __( 'New form', '23h-product-features' ),
'search_items' => __( 'Search forms', '23h-product-features' ),
'not_found' => __( 'No forms yet.', '23h-product-features' ),
'not_found_in_trash' => __( 'No forms in the trash.', '23h-product-features' ),
),
'public' => false,
'show_ui' => true,
'show_in_menu' => false,
'supports' => array( 'title', 'page-attributes' ),
'capability_type' => 'product',
'map_meta_cap' => true,
'exclude_from_search' => true,
'publicly_queryable' => false,
'show_in_rest' => false,
'rewrite' => false,
)
);
}
/* ------------------------------------------------------------------
* Data
* ---------------------------------------------------------------- */
public static function get_fields( $form_id ) {
$json = get_post_meta( $form_id, self::META_FIELDS, true );
$fields = is_string( $json ) ? json_decode( $json, true ) : $json;
return is_array( $fields ) ? $fields : array();
}
/** All published forms (cached per request). */
public static function published() {
if ( null === self::$published ) {
$posts = get_posts(
array(
'post_type' => self::CPT,
'post_status' => 'publish',
'numberposts' => -1,
'orderby' => array(
'menu_order' => 'ASC',
'title' => 'ASC',
),
'suppress_filters' => true,
)
);
self::$published = array();
foreach ( $posts as $p ) {
self::$published[ $p->ID ] = array(
'id' => $p->ID,
'title' => $p->post_title,
'all' => 'yes' === get_post_meta( $p->ID, self::META_ALL, true ),
'cats' => array_map( 'intval', (array) get_post_meta( $p->ID, self::META_CATS, true ) ),
'prods' => array_map( 'intval', (array) get_post_meta( $p->ID, self::META_PRODS, true ) ),
'fields' => self::get_fields( $p->ID ),
);
}
}
return self::$published;
}
/**
* The forms shown on a product.
*
* @param int $product_id Product ID (for variations, the parent's ID).
* @return array
*/
public static function for_product( $product_id ) {
$product_id = (int) $product_id;
if ( isset( self::$by_product[ $product_id ] ) ) {
return self::$by_product[ $product_id ];
}
$direct = array_map( 'intval', (array) get_post_meta( $product_id, '_h23pf_forms', true ) );
$exclude = 'yes' === get_post_meta( $product_id, '_h23pf_exclude_global', true );
$cats = wc_get_product_term_ids( $product_id, 'product_cat' );
$out = array();
foreach ( self::published() as $id => $form ) {
$match = in_array( $id, $direct, true );
if ( ! $match && ! $exclude ) {
$match = $form['all'] || in_array( $product_id, $form['prods'], true ) || array_intersect( $cats, $form['cats'] );
}
if ( $match && ! empty( $form['fields'] ) ) {
$out[ $id ] = $form;
}
}
self::$by_product[ $product_id ] = apply_filters( 'h23pf_product_forms', $out, $product_id );
return self::$by_product[ $product_id ];
}
/** Does the product have fields to fill in (not only titles / separators)? */
public static function product_has_inputs( $product_id ) {
foreach ( self::for_product( $product_id ) as $form ) {
foreach ( $form['fields'] as $f ) {
if ( ! in_array( $f['type'], array( 'heading', 'separator', 'hidden' ), true ) ) {
return true;
}
}
}
return false;
}
/* ------------------------------------------------------------------
* Admin: meta boxes
* ---------------------------------------------------------------- */
public static function meta_boxes( $post ) {
add_meta_box( 'h23pf-builder', __( 'Form fields', '23h-product-features' ), array( __CLASS__, 'box_builder' ), self::CPT, 'normal', 'high' );
add_meta_box( 'h23pf-assign', __( 'Where the form is shown', '23h-product-features' ), array( __CLASS__, 'box_assign' ), self::CPT, 'side', 'default' );
add_meta_box( 'h23pf-help', __( 'How it works', '23h-product-features' ), array( __CLASS__, 'box_help' ), self::CPT, 'side', 'low' );
}
public static function box_builder( $post ) {
wp_nonce_field( 'h23pf_save_form', 'h23pf_form_nonce' );
$fields = self::get_fields( $post->ID );
?>
ID, self::META_ALL, true );
$cats = array_map( 'intval', (array) get_post_meta( $post->ID, self::META_CATS, true ) );
$prods = array_filter( array_map( 'intval', (array) get_post_meta( $post->ID, self::META_PRODS, true ) ) );
$terms = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
)
);
?>
>
%3$s', (int) $t->term_id, selected( in_array( (int) $t->term_id, $cats, true ), true, false ), esc_html( $t->name ) );
}
}
?>
%2$s', (int) $pid, esc_html( wp_strip_all_tags( $p->get_formatted_name() ) ) );
}
}
?>
array(),
'em' => array(),
);
?>
Example: a "Single choice" field with the options 30 cm × 6 cm diam. , 30 cm × 7.5 cm diam. and 40 cm × 6 cm diam. ', '23h-product-features' ), $allowed ); ?>
The fields are shown on the product page, before the "Add to cart" button.', '23h-product-features' ), $allowed ); ?>
Validation messages are edited in Products → Product Features → Messages.', '23h-product-features' ), $allowed ); ?>
self::clean_id( self::text( $f, 'id' ), 'f_', $used ),
'type' => $type,
'label' => sanitize_text_field( self::text( $f, 'label' ) ),
'desc' => sanitize_textarea_field( self::text( $f, 'desc' ) ),
'placeholder' => sanitize_text_field( self::text( $f, 'placeholder' ) ),
'default' => sanitize_textarea_field( self::text( $f, 'default' ) ),
'required' => ! empty( $f['required'] ),
'req_msg' => sanitize_text_field( self::text( $f, 'req_msg' ) ),
'maxlength' => '' !== self::text( $f, 'maxlength' ) ? absint( $f['maxlength'] ) : '',
'min' => self::num_or_empty( isset( $f['min'] ) ? $f['min'] : '' ),
'max' => self::num_or_empty( isset( $f['max'] ) ? $f['max'] : '' ),
'step' => self::num_or_empty( isset( $f['step'] ) ? $f['step'] : '' ),
'width' => 'half' === self::text( $f, 'width' ) ? 'half' : 'full',
'options' => array(),
);
if ( 0 === $field['maxlength'] ) {
$field['maxlength'] = '';
}
if ( in_array( $type, self::choice_types(), true ) ) {
$opt_used = array();
$opts = isset( $f['options'] ) && is_array( $f['options'] ) ? $f['options'] : array();
foreach ( $opts as $o ) {
if ( ! is_array( $o ) ) {
continue;
}
$label = sanitize_text_field( self::text( $o, 'label' ) );
if ( '' === $label ) {
continue;
}
$field['options'][] = array(
'id' => self::clean_id( self::text( $o, 'id' ), 'o_', $opt_used ),
'label' => $label,
'selected' => ! empty( $o['selected'] ),
);
}
// Only one default option for dropdowns and single choice.
if ( 'checkbox' !== $type ) {
$found = false;
foreach ( $field['options'] as &$o ) {
if ( $o['selected'] && ! $found ) {
$found = true;
} else {
$o['selected'] = false;
}
}
unset( $o );
}
}
if ( in_array( $type, self::layout_types(), true ) ) {
$field['required'] = false;
}
$out[] = $field;
}
return $out;
}
/* ------------------------------------------------------------------
* Forms list
* ---------------------------------------------------------------- */
public static function columns( $cols ) {
$new = array();
foreach ( $cols as $k => $v ) {
$new[ $k ] = $v;
if ( 'title' === $k ) {
$new['h23pf_fields'] = __( 'Fields', '23h-product-features' );
$new['h23pf_where'] = __( 'Shown on', '23h-product-features' );
}
}
return $new;
}
public static function column( $col, $post_id ) {
if ( 'h23pf_fields' === $col ) {
$labels = array();
foreach ( self::get_fields( $post_id ) as $f ) {
if ( ! in_array( $f['type'], self::layout_types(), true ) ) {
$labels[] = $f['label'] ? $f['label'] : $f['type'];
}
}
echo esc_html( $labels ? implode( ', ', $labels ) : '—' );
return;
}
if ( 'h23pf_where' !== $col ) {
return;
}
if ( 'yes' === get_post_meta( $post_id, self::META_ALL, true ) ) {
echo '' . esc_html__( 'All products', '23h-product-features' ) . ' ';
return;
}
$parts = array();
foreach ( array_filter( (array) get_post_meta( $post_id, self::META_CATS, true ) ) as $c ) {
$t = get_term( (int) $c, 'product_cat' );
if ( $t && ! is_wp_error( $t ) ) {
$parts[] = $t->name;
}
}
$prods = array_filter( (array) get_post_meta( $post_id, self::META_PRODS, true ) );
if ( $prods ) {
/* translators: %d: number of products. */
$parts[] = sprintf( _n( '%d product', '%d products', count( $prods ), '23h-product-features' ), count( $prods ) );
}
$direct = self::count_direct_products( $post_id );
if ( $direct ) {
/* translators: %d: number of products. */
$parts[] = sprintf( _n( '%d chosen on the product', '%d chosen on the products', $direct, '23h-product-features' ), $direct );
}
if ( $parts ) {
echo esc_html( implode( ' · ', $parts ) );
} else {
echo '' . esc_html__( 'Not assigned', '23h-product-features' ) . ' ';
}
}
private static function count_direct_products( $form_id ) {
global $wpdb;
$like = '%' . $wpdb->esc_like( 'i:' . (int) $form_id . ';' ) . '%';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- count shown in the forms list.
return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE meta_key = %s AND meta_value LIKE %s', $wpdb->postmeta, '_h23pf_forms', $like ) );
}
public static function row_actions( $actions, $post ) {
if ( self::CPT === $post->post_type && current_user_can( 'edit_post', $post->ID ) ) {
$url = wp_nonce_url( admin_url( 'admin.php?action=h23pf_duplicate&post=' . $post->ID ), 'h23pf_dup_' . $post->ID );
$actions['h23pf_duplicate'] = '' . esc_html__( 'Duplicate', '23h-product-features' ) . ' ';
}
return $actions;
}
public static function duplicate() {
$id = isset( $_GET['post'] ) ? absint( wp_unslash( $_GET['post'] ) ) : 0;
check_admin_referer( 'h23pf_dup_' . $id );
if ( ! $id || ! current_user_can( 'edit_post', $id ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', '23h-product-features' ) );
}
$src = get_post( $id );
if ( ! $src || self::CPT !== $src->post_type ) {
wp_die( esc_html__( 'This form does not exist.', '23h-product-features' ) );
}
$new_id = wp_insert_post(
array(
'post_type' => self::CPT,
/* translators: %s: form title. */
'post_title' => sprintf( __( '%s (copy)', '23h-product-features' ), $src->post_title ),
'post_status' => 'draft',
'menu_order' => $src->menu_order,
)
);
if ( $new_id && ! is_wp_error( $new_id ) ) {
update_post_meta( $new_id, self::META_FIELDS, wp_slash( wp_json_encode( self::get_fields( $id ) ) ) );
foreach ( array( self::META_ALL, self::META_CATS, self::META_PRODS ) as $k ) {
update_post_meta( $new_id, $k, get_post_meta( $id, $k, true ) );
}
}
wp_safe_redirect( admin_url( 'post.php?action=edit&post=' . (int) $new_id ) );
exit;
}
}