), which * starts with that template's fields, mail settings and messages. Opening * post-new.php without a template goes to the picker. */ final class Template_Picker { /** Admin page slug. */ public const PAGE = 'tflexforms-new'; /** Query argument naming the template on post-new.php. */ public const ARG = 'tflexforms_template'; /** * Registers admin hooks. */ public static function register_hooks(): void { add_action( 'admin_menu', array( self::class, 'add_page' ) ); add_action( 'load-post-new.php', array( self::class, 'maybe_redirect' ) ); add_action( 'wp_insert_post', array( self::class, 'apply_to_new_form' ), 10, 3 ); add_filter( 'default_title', array( self::class, 'default_title' ), 10, 2 ); add_filter( 'submenu_file', array( self::class, 'submenu_file' ) ); add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue' ) ); } /** * Replaces the "Add New Form" menu item with the picker, in its place. */ public static function add_page(): void { $parent = 'edit.php?post_type=' . Form_Post_Type::POST_TYPE; remove_submenu_page( $parent, self::new_form_path() ); add_submenu_page( $parent, __( 'Add New Form', 'flexsolutions-form-builder' ), __( 'Add New Form', 'flexsolutions-form-builder' ), self::capability(), self::PAGE, array( self::class, 'render' ), 1 ); } /** * Picker URL. */ public static function url(): string { return add_query_arg( 'page', self::PAGE, admin_url( 'edit.php?post_type=' . Form_Post_Type::POST_TYPE ) ); } /** * URL that starts a new form from a template. * * @param string $id Template id. */ public static function new_form_url( string $id ): string { return add_query_arg( self::ARG, $id, admin_url( self::new_form_path() ) ); } /** * Sends post-new.php for forms to the picker unless a known template is named. */ public static function maybe_redirect(): void { global $typenow; if ( Form_Post_Type::POST_TYPE !== $typenow || ! current_user_can( self::capability() ) ) { return; } if ( null !== self::requested() ) { return; } wp_safe_redirect( self::url() ); exit; } /** * Gives the new form (the auto-draft post-new.php creates) the chosen template. * * @param int $post_id Post ID. * @param \WP_Post $post Post. * @param bool $update Whether an existing post was updated. */ public static function apply_to_new_form( int $post_id, \WP_Post $post, bool $update ): void { global $pagenow; if ( $update || 'post-new.php' !== $pagenow || Form_Post_Type::POST_TYPE !== $post->post_type || 'auto-draft' !== $post->post_status ) { return; } $id = self::requested(); if ( null !== $id && current_user_can( 'edit_post', $post_id ) ) { Templates::apply( $post_id, $id ); } } /** * The template's title as the new form's title (none for the blank form). * * @param string $title Default title. * @param \WP_Post $post New post. */ public static function default_title( string $title, \WP_Post $post ): string { if ( Form_Post_Type::POST_TYPE !== $post->post_type || '' !== $title ) { return $title; } $id = self::requested(); return null !== $id && 'blank' !== $id ? (string) Templates::get( $id )['title'] : $title; } /** * Highlights "Add New Form" in the menu while a new form is edited. * * @param string|null $submenu_file Current submenu file. */ public static function submenu_file( ?string $submenu_file ): ?string { return self::new_form_path() === $submenu_file ? self::PAGE : $submenu_file; } /** * Loads the picker's styles on its page. * * @param string $hook_suffix Current admin page. */ public static function enqueue( string $hook_suffix ): void { if ( Form_Post_Type::POST_TYPE . '_page_' . self::PAGE === $hook_suffix ) { wp_enqueue_style( 'tflexforms-admin', plugins_url( 'assets/css/admin.css', PLUGIN_FILE ), array(), VERSION ); } } /** * Renders the picker. */ public static function render(): void { $templates = Templates::all(); $categories = Templates::categories(); $by = array(); foreach ( $templates as $template ) { $by[ $template['category'] ][] = $template; } ?>