content_input_hooks(); parent::__construct( $init ); } /** * Register hooks. * * @since 1.9.4 */ private function content_input_hooks(): void { add_action( 'wpforms_builder_enqueues', [ $this, 'builder_enqueues' ] ); add_action( 'wpforms_builder_print_footer_scripts', [ $this, 'content_editor_tools_template' ] ); add_filter( 'wpforms_builder_field_option_class', [ $this, 'builder_field_option_class' ], 10, 2 ); add_filter( 'wpforms_builder_strings', [ $this, 'content_builder_strings' ], 10, 2 ); add_filter( 'editor_stylesheets', [ $this, 'editor_stylesheets' ] ); add_filter( 'media_view_strings', [ $this, 'edit_media_view_strings' ], 10, 2 ); add_filter( 'teeny_mce_buttons', [ $this, 'teeny_mce_buttons' ], 10, 2 ); } /** * Content field option. * * @since 1.9.4 * * @param array $field Field data and settings. */ private function field_option_content( array $field ): void { $value = ( isset( $field['content'] ) && ! wpforms_is_empty_string( $field['content'] ) ) ? wp_kses( $field['content'], $this->get_allowed_html_tags() ) : ''; $output = $this->field_element( 'row', $field, [ 'slug' => 'content', 'content' => $this->get_content_editor( $value, $field ), ], false ); $output .= wpforms_render( 'fields/content/action-buttons', [ 'id' => $field['id'], 'preview' => $this->get_input_string( 'preview' ), 'expand' => $this->get_input_string( 'expand' ), ], true ); printf( '
', $output ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * Add class name to the field option top element. * * @since 1.9.4 * * @param string|mixed $css_class CSS classes. * @param array $field Field data. * * @return string */ public function builder_field_option_class( $css_class, $field ): string { $css_class = (string) $css_class; return $this->type === $field['type'] ? $css_class . ' wpforms-field-has-tinymce' : $css_class; } /** * Localized strings for `content-field` JS script. * * @since 1.9.4 * * @param array|mixed $strings Localized strings. * @param array $form The form element. * * @return array * @noinspection PhpUnusedParameterInspection */ public function content_builder_strings( $strings, $form ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed $strings = (array) $strings; $strings['content_field'] = [ 'collapse' => wp_strip_all_tags( $this->get_input_string( 'collapse' ) ), 'expand' => wp_strip_all_tags( $this->get_input_string( 'expand' ) ), 'editor_default_value' => wp_kses( $this->get_input_string( 'editor_default_value' ), $this->get_allowed_html_tags() ), 'content_editor_plugins' => $this->content_editor_plugins(), 'content_editor_toolbar' => $this->content_editor_toolbar(), 'content_editor_css_url' => $this->content_css_url(), 'editor_height' => $this->get_editor_height(), 'allowed_html' => array_keys( $this->get_allowed_html_tags() ), 'invalid_elements' => $this->get_invalid_elements(), 'quicktags_buttons' => $this->get_quicktags_buttons(), 'body_class' => $this->get_editor_body_class(), ]; return $this->add_supported_field_type( $strings, $this->type ); } /** * Add editor stylesheet. * * @since 1.9.4 * * @param array|mixed $stylesheets Editor stylesheets. * * @return array */ public function editor_stylesheets( $stylesheets ): array { $stylesheets = (array) $stylesheets; if ( wpforms_is_admin_page( 'builder' ) ) { $stylesheets[] = $this->content_css_url(); } return $stylesheets; } /** * Edit some media view strings to reference a form instead of a page/post. * * @since 1.9.4 * * @param array|mixed $strings List of media view strings. * @param WP_Post $post Post object. * * @return array Modified media view strings. * @noinspection SqlResolve * @noinspection PhpMissingParamTypeInspection * @noinspection PhpUnusedParameterInspection */ public function edit_media_view_strings( $strings, $post ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed $strings = (array) $strings; if ( wpforms_is_admin_page( 'builder' ) ) { $strings['insertIntoPost'] = esc_html__( /** @lang text */ 'Insert into form', 'wpforms-lite' ); $strings['uploadedToThisPost'] = esc_html__( 'Uploaded to this form', 'wpforms-lite' ); } return $strings; } /** * Remove fullscreen button if this is other tinymce editor instance than content field editor. * * @since 1.9.4 * * @param array|mixed $buttons Array of editor buttons. * @param string $editor_id Editor textarea ID. * * @return array */ public function teeny_mce_buttons( $buttons, $editor_id ): array { $buttons = (array) $buttons; $is_other_editor = strpos( $editor_id, 'wpforms_panel_' ) === 0 || $editor_id === 'entry_note'; $key = array_search( 'fullscreen', $buttons, true ); if ( $is_other_editor && $key !== false ) { unset( $buttons[ $key ] ); } return $buttons; } /** * Get default content editor plugins. * * @since 1.9.4 * * @return array Plugins array. */ private function content_editor_plugins(): array { $plugins = [ 'charmap', 'colorpicker', 'hr', 'link', 'image', 'lists', 'paste', 'tabfocus', 'textcolor', 'wordpress', 'wpemoji', 'wptextpattern', 'wpeditimage', ]; /** * Get content editor plugins filter. * * @since 1.7.8 * * @param array $plugins Plugins array. */ return (array) apply_filters( 'wpforms_builder_content_input_get_content_editor_plugins', $plugins ); } /** * Get default content editor toolbar. * * @since 1.9.4 * * @return array Toolbar buttons array. */ private function content_editor_toolbar(): array { $toolbar = [ 'formatselect', 'bold', 'italic', 'underline', 'strikethrough', 'forecolor', 'link', 'bullist', 'numlist', 'blockquote', 'alignleft', 'aligncenter', 'alignright', ]; /** * Get content editor toolbar buttons filter. * * @since 1.7.8 * * @param array $toolbar Toolbar buttons array. */ return (array) apply_filters( 'wpforms_builder_content_input_get_content_editor_toolbar', $toolbar ); } /** * Enqueue wpforms-content-field script. * * @since 1.9.4 * * @param string $view Current view. * * @noinspection PhpUnusedParameterInspection */ public function builder_enqueues( $view ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found // Enqueue editor styles explicitly. Hack for broken styles when the Content field is deleted and Settings > Confirmation editor get broken. // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion wp_enqueue_style( 'wpforms-editor-styles', includes_url( 'css/editor.css' ) ); } /** * Content editor tools template. * * @since 1.9.4 */ public function content_editor_tools_template(): void { ?> array_merge( $other_supported_field_types, [ $type ] ), ]; return $strings; } /** * Get translatable string. * * @since 1.9.4 * * @param string $key String key. * * @return string */ private function get_input_string( $key ): string { if ( ! self::$translatable_strings ) { self::$translatable_strings = [ 'editor_default_value' => __( 'To get started, replace this text with your own.
', 'wpforms-lite' ), 'expand' => __( 'Expand Editor', 'wpforms-lite' ), 'collapse' => __( 'Collapse Editor', 'wpforms-lite' ), 'preview' => __( 'Update Preview', 'wpforms-lite' ), ]; } return self::$translatable_strings[ $key ] ?? ''; } /** * Show field preview in the right builder panel. * * @since 1.9.4 * * @param array $field Field data. */ private function content_input_preview( $field ): void { $content = $field['content'] ?? $this->get_input_string( 'editor_default_value' ); ?>