register_block( new Form_Embed_Block() ); } /** @return array id => title of published forms. */ public static function form_options() { static $opts = null; if ( null === $opts ) { $opts = array( '' => __( '— Choose a form —', 'fieldwren' ) ); foreach ( get_posts( array( 'post_type' => Plugin::POST_TYPE, 'numberposts' => 300, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC' ) ) as $p ) { /* translators: 1: title, 2: id */ $opts[ (string) $p->ID ] = sprintf( __( '%1$s (#%2$d)', 'fieldwren' ), $p->post_title ? $p->post_title : __( '(no title)', 'fieldwren' ), $p->ID ); } } return $opts; } /** Elementor widget. */ public static function elementor( $manager ) { require_once FWREN_PATH . 'includes/class-elementor-widget.php'; $manager->register( new Elementor_Widget() ); } } /** * Block that shows a form (outside the form editor). */ class Form_Embed_Block extends Block_Base { public function get_name() { return 'form'; } public function get_block_name() { return 'fwren/form'; } public function get_title() { return __( 'Fieldwren Form', 'fieldwren' ); } public function get_icon() { return \FWREN\Icons::svg( 'form' ); } public function get_category() { return 'widgets'; } public function get_keywords() { return array( 'form', 'contact', 'fieldwren', 'lead' ); } public function get_wrapper_prefix() { return 'fwren-embed-'; } protected function register_controls() { $this->start_controls_section( 'section_form', array( 'label' => __( 'Form', 'fieldwren' ) ) ); $this->add_control( 'form_id', array( 'label' => __( 'Form', 'fieldwren' ), 'type' => Controls::SELECT, 'default' => '', 'options' => Embed::form_options() ) ); $this->add_control( 'show_title', array( 'label' => __( 'Show form title', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) ); $this->add_control( 'edit_link', array( 'type' => Controls::RAW_HTML, 'raw' => '' . esc_html__( 'Create a new form', 'fieldwren' ) . ' ↗' ) ); $this->end_controls_section(); } protected function render( $content = '' ) { $id = (int) $this->settings['form_id']; if ( ! $id ) { if ( Renderer::is_editor() ) { echo '
'; } return; } echo Renderer::render( $id, array( 'title' => 'yes' === $this->settings['show_title'] ? 'yes' : '' ) ); // phpcs:ignore WordPress.Security.EscapeOutput } }