2, 'attributes' => array( 'formId' => array( 'type' => 'number', 'default' => 0, ), 'widgetId' => array( 'type' => 'number', 'default' => 0, ), ), 'render_callback' => array( __CLASS__, 'render' ), ) ); } /** * Rendering on the site. It goes through the same path as the shortcode, so * the look is the same. * * @param array $attributes The block settings. * * @return string */ public static function render( $attributes ) { // The widget number wins. When a whole section is chosen, the form inside it // renders on its own, so it must not be rendered a second time. $widget_id = isset( $attributes['widgetId'] ) ? absint( $attributes['widgetId'] ) : 0; if ( $widget_id ) { return Ranq_Forms_Widget::shortcode( array( 'id' => $widget_id ) ); } $form_id = isset( $attributes['formId'] ) ? absint( $attributes['formId'] ) : 0; if ( ! $form_id ) { return ''; } return Ranq_Forms_Shortcode::render( array( 'id' => $form_id ) ); } /** * The editor assets. Loaded only in the editor, never on the site. * * @return void */ public static function editor_assets() { wp_enqueue_script( 'ranq-forms-block', RANQ_FORMS_URL . 'assets/js/block.js', array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-block-editor', 'wp-server-side-render', 'wp-i18n' ), RANQ_FORMS_VERSION, true ); // The form style sheet, so the block in the editor looks the way it will look. wp_enqueue_style( 'ranq-forms', RANQ_FORMS_URL . 'assets/css/form.css', array(), RANQ_FORMS_VERSION ); wp_localize_script( 'ranq-forms-block', 'ranqFormsBlock', array( 'forms' => self::form_list(), 'widgets' => Ranq_Forms_Widget::all_widgets(), 'newUrl' => admin_url( 'admin.php?page=' . Ranq_Forms_Admin::PAGE_NEW ), 'i18n' => array( 'title' => __( '2ranq Form', '2ranq-forms' ), 'description' => __( 'Drop one of your forms into the page.', '2ranq-forms' ), 'chooseForm' => __( 'Which form', '2ranq-forms' ), 'withSection' => __( 'with contact section', '2ranq-forms' ), 'noForms' => __( 'You have no forms yet.', '2ranq-forms' ), 'createOne' => __( 'Create your first form', '2ranq-forms' ), 'pickOne' => __( 'Choose a form to show here.', '2ranq-forms' ), 'settings' => __( 'Form', '2ranq-forms' ), 'editForm' => __( 'Edit this form', '2ranq-forms' ), ), ) ); } /** * The list of forms for the drop down. * * @return array */ public static function form_list() { $forms = get_posts( array( 'post_type' => Ranq_Forms_Post_Type::POST_TYPE, 'post_status' => array( 'publish', 'draft' ), 'numberposts' => 200, 'orderby' => 'title', 'order' => 'ASC', 'no_found_rows' => true, ) ); $list = array(); foreach ( $forms as $form ) { $list[] = array( 'id' => (int) $form->ID, 'title' => get_the_title( $form ), 'fields' => count( Ranq_Forms_Fields::get_form_fields( $form->ID ) ), 'editUrl' => admin_url( 'admin.php?page=' . Ranq_Forms_Admin::PAGE . '&form_id=' . $form->ID ), ); } return $list; } }