array(
'name' => __( 'Forms', 'fieldwren' ),
'singular_name' => __( 'Form', 'fieldwren' ),
'add_new' => __( 'Add New Form', 'fieldwren' ),
'add_new_item' => __( 'Add New Form', 'fieldwren' ),
'edit_item' => __( 'Edit Form', 'fieldwren' ),
'new_item' => __( 'New Form', 'fieldwren' ),
'view_item' => __( 'View Form Page', 'fieldwren' ),
'search_items' => __( 'Search Forms', 'fieldwren' ),
'not_found' => __( 'No forms yet.', 'fieldwren' ),
'not_found_in_trash' => __( 'No forms in the trash.', 'fieldwren' ),
'all_items' => __( 'All Forms', 'fieldwren' ),
'menu_name' => __( 'Forms', 'fieldwren' ),
),
'public' => false,
'publicly_queryable' => true, // Landing pages (/form/{slug}/); guard_single() 404s the rest.
'exclude_from_search' => true,
'show_ui' => true,
'show_in_menu' => Admin::MENU_SLUG,
'show_in_rest' => true,
'rest_base' => 'fwren-forms',
'has_archive' => false,
'hierarchical' => false,
'rewrite' => array(
'slug' => apply_filters( 'fwren/landing_slug', 'form' ),
'with_front' => false,
),
'query_var' => true,
'supports' => array( 'title', 'editor', 'custom-fields', 'revisions' ),
'capability_type' => 'post',
'capabilities' => array(
'edit_post' => 'fwren_manage_forms',
'read_post' => 'read',
'delete_post' => 'fwren_manage_forms',
'edit_posts' => 'fwren_manage_forms',
'edit_others_posts' => 'fwren_manage_forms',
'delete_posts' => 'fwren_manage_forms',
'publish_posts' => 'fwren_manage_forms',
'read_private_posts' => 'fwren_manage_forms',
'create_posts' => 'fwren_manage_forms',
),
'map_meta_cap' => false,
'template' => self::default_template(),
'template_lock' => false,
)
);
$auth = static function () {
return current_user_can( 'fwren_manage_forms' );
};
foreach ( array( '_fwren_settings', '_fwren_feeds' ) as $key ) {
register_post_meta(
Plugin::POST_TYPE,
$key,
array(
'type' => 'string',
'single' => true,
'default' => '',
'show_in_rest' => true,
'auth_callback' => $auth,
)
);
}
}
/** @return array Starter fields for a new form. */
public static function default_template() {
return apply_filters(
'fwren/default_template',
array(
array( 'fwren/name', array( 'label' => __( 'Name', 'fieldwren' ), 'name' => 'names', 'required' => 'yes' ) ),
array( 'fwren/email', array( 'label' => __( 'Email', 'fieldwren' ), 'name' => 'email', 'required' => 'yes' ) ),
array( 'fwren/textarea', array( 'label' => __( 'Message', 'fieldwren' ), 'name' => 'message' ) ),
array( 'fwren/submit', array() ),
)
);
}
/** Forms always use the block editor, even when Classic Editor is active. */
public static function force_block_editor( $use, $post_type ) {
return Plugin::POST_TYPE === $post_type ? true : $use;
}
/**
* Only form blocks inside a form; field blocks stay out of every other editor (the Form embed
* block is what goes there).
*
* @param bool|string[] $allowed Allowed blocks.
* @param \WP_Block_Editor_Context $context Editor context.
* @return bool|string[]
*/
public static function allowed_blocks( $allowed, $context ) {
$post = $context->post ?? null;
$all = array_keys( \WP_Block_Type_Registry::get_instance()->get_all_registered() );
if ( $post && Plugin::POST_TYPE === $post->post_type ) {
$form_blocks = Fields::block_names();
$form_blocks[] = Fields::CONTAINER;
return apply_filters( 'fwren/allowed_form_blocks', array_values( array_merge( $form_blocks, array( 'core/paragraph', 'core/heading', 'core/image', 'core/list', 'core/list-item', 'core/separator', 'core/spacer' ) ) ) );
}
$fields = Fields::block_names();
$fields[] = Fields::CONTAINER; // Forms only: other editors use their own containers.
if ( true === $allowed ) {
return array_values( array_diff( $all, $fields ) );
}
if ( is_array( $allowed ) ) {
return array_values( array_diff( $allowed, $fields ) );
}
return $allowed;
}
/** Compile the schema whenever the form is saved. */
public static function on_save( $post_id, $post ) {
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
return;
}
Schema::compile( $post_id, $post->post_content );
}
/** REST saves: meta is written after save_post, so recompile once everything is stored. */
public static function on_rest_insert( $post ) {
Schema::compile( $post->ID, $post->post_content );
}
/** Form pages are only reachable when the landing page is on (or for editors, as preview). */
public static function guard_single() {
if ( ! is_singular( Plugin::POST_TYPE ) ) {
return;
}
$id = get_queried_object_id();
$s = Form::settings( $id );
if ( empty( $s['landing']['enabled'] ) && ! current_user_can( 'fwren_manage_forms' ) ) {
global $wp_query;
$wp_query->set_404();
status_header( 404 );
nocache_headers();
}
}
/** Admin list columns. */
public static function columns( $cols ) {
$new = array();
foreach ( $cols as $k => $v ) {
$new[ $k ] = $v;
if ( 'title' === $k ) {
$new['fwren_shortcode'] = __( 'Shortcode', 'fieldwren' );
$new['fwren_entries'] = __( 'Entries', 'fieldwren' );
$new['fwren_views'] = __( 'Views', 'fieldwren' );
$new['fwren_conv'] = __( 'Conversion', 'fieldwren' );
}
}
return $new;
}
/** Admin list column values. */
public static function column( $col, $post_id ) {
switch ( $col ) {
case 'fwren_shortcode':
echo '[fieldwren_form id="' . (int) $post_id . '"]';
break;
case 'fwren_entries':
$n = Entries::count( $post_id );
$u = Entries::count( $post_id, 'unread' );
echo '' . (int) $n . '';
if ( $u ) {
echo ' ' . (int) $u . ' ' . esc_html__( 'new', 'fieldwren' ) . '';
}
break;
case 'fwren_views':
$t = Analytics::totals( $post_id );
echo (int) $t['views'];
break;
case 'fwren_conv':
$t = Analytics::totals( $post_id );
echo $t['views'] ? esc_html( round( $t['submissions'] / $t['views'] * 100, 1 ) . '%' ) : '—';
break;
}
}
/** Row actions: entries, settings, duplicate, export. */
public static function row_actions( $actions, $post ) {
if ( Plugin::POST_TYPE !== $post->post_type ) {
return $actions;
}
unset( $actions['inline hide-if-no-js'] );
$id = $post->ID;
$actions['fwren_preview'] = '' . esc_html__( 'Preview', 'fieldwren' ) . '';
$actions['fwren_entries'] = '' . esc_html__( 'Entries', 'fieldwren' ) . '';
$actions['fwren_settings'] = '' . esc_html__( 'Settings & Integrations', 'fieldwren' ) . '';
$actions['fwren_dup'] = '' . esc_html__( 'Duplicate', 'fieldwren' ) . '';
$actions['fwren_export'] = '' . esc_html__( 'Export', 'fieldwren' ) . '';
return $actions;
}
/** Deleting a form deletes its entries. */
public static function on_delete( $post_id ) {
if ( Plugin::POST_TYPE === get_post_type( $post_id ) ) {
Entries::delete_form_entries( $post_id );
}
}
}