avatar.php
4 months ago
boolean.php
4 months ago
code.php
4 months ago
color.php
4 months ago
comment.php
4 months ago
currency.php
4 months ago
date.php
4 months ago
datetime.php
4 months ago
email.php
4 months ago
file.php
4 months ago
heading.php
4 months ago
html.php
4 months ago
link.php
4 months ago
number.php
4 months ago
oembed.php
4 months ago
paragraph.php
4 months ago
password.php
4 months ago
phone.php
4 months ago
pick.php
4 months ago
slug.php
4 months ago
taxonomy.php
4 months ago
text.php
4 months ago
time.php
4 months ago
website.php
4 months ago
wysiwyg.php
4 months ago
heading.php
205 lines
| 1 | <?php |
| 2 | |
| 3 | // Don't load directly. |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | die( '-1' ); |
| 6 | } |
| 7 | |
| 8 | use Pods\Whatsit\Field; |
| 9 | |
| 10 | /** |
| 11 | * @package Pods\Fields |
| 12 | */ |
| 13 | class PodsField_Heading extends PodsField { |
| 14 | |
| 15 | /** |
| 16 | * {@inheritdoc} |
| 17 | */ |
| 18 | public static $group = 'Layout Elements'; |
| 19 | |
| 20 | /** |
| 21 | * {@inheritdoc} |
| 22 | */ |
| 23 | public static $type = 'heading'; |
| 24 | |
| 25 | /** |
| 26 | * {@inheritdoc} |
| 27 | */ |
| 28 | public static $label = 'Heading'; |
| 29 | |
| 30 | /** |
| 31 | * {@inheritdoc} |
| 32 | */ |
| 33 | public static $prepare = '%s'; |
| 34 | |
| 35 | /** |
| 36 | * {@inheritdoc} |
| 37 | */ |
| 38 | public function setup() { |
| 39 | static::$group = __( 'Layout Elements', 'pods' ); |
| 40 | static::$label = __( 'Heading', 'pods' ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * {@inheritdoc} |
| 45 | */ |
| 46 | public function options() { |
| 47 | return [ |
| 48 | static::$type . '_tag' => [ |
| 49 | 'label' => __( 'Heading HTML Tag', 'pods' ), |
| 50 | 'type' => 'pick', |
| 51 | 'data' => [ |
| 52 | 'h1' => 'h1', |
| 53 | 'h2' => 'h2', |
| 54 | 'h3' => 'h3', |
| 55 | 'h4' => 'h4', |
| 56 | 'h5' => 'h5', |
| 57 | 'h6' => 'h6', |
| 58 | 'p' => 'p', |
| 59 | 'div' => 'div', |
| 60 | ], |
| 61 | 'default' => 'h2', |
| 62 | 'description' => __( 'Leave this empty to use the default heading tag for the form context the heading appears in.', 'pods' ), |
| 63 | 'help' => __( 'This is the heading HTML tag to use for the heading text. Example "h2" will output your heading as <code><h2>Heading Text</h2></code>', 'pods' ), |
| 64 | ], |
| 65 | 'output_options' => [ |
| 66 | 'label' => __( 'Output Options', 'pods' ), |
| 67 | 'type' => 'boolean_group', |
| 68 | 'boolean_group' => [ |
| 69 | static::$type . '_allow_html' => [ |
| 70 | 'label' => __( 'Allow HTML', 'pods' ), |
| 71 | 'default' => 1, |
| 72 | 'type' => 'boolean', |
| 73 | 'dependency' => true, |
| 74 | ], |
| 75 | static::$type . '_sanitize_html' => [ |
| 76 | 'label' => __( 'Sanitize HTML', 'pods' ), |
| 77 | 'default' => 1, |
| 78 | 'help' => __( 'This sanitizes things like script tags and other content not normally allowed in WordPress content. Disable this only if you trust users who will have access to enter content into this field.', 'pods' ), |
| 79 | 'type' => 'boolean', |
| 80 | 'dependency' => true, |
| 81 | ], |
| 82 | static::$type . '_wptexturize' => [ |
| 83 | 'label' => __( 'Enable wptexturize', 'pods' ), |
| 84 | 'default' => 1, |
| 85 | 'type' => 'boolean', |
| 86 | 'help' => [ |
| 87 | __( 'Transforms less-beautiful text characters into stylized equivalents.', 'pods' ), |
| 88 | 'http://codex.wordpress.org/Function_Reference/wptexturize', |
| 89 | ], |
| 90 | ], |
| 91 | static::$type . '_allow_shortcode' => [ |
| 92 | 'label' => __( 'Allow Shortcodes', 'pods' ), |
| 93 | 'default' => 0, |
| 94 | 'type' => 'boolean', |
| 95 | 'dependency' => true, |
| 96 | 'help' => [ |
| 97 | __( 'Embed [shortcodes] that help transform your static content into dynamic content.', 'pods' ), |
| 98 | 'http://codex.wordpress.org/Shortcode_API', |
| 99 | ], |
| 100 | ], |
| 101 | ], |
| 102 | ], |
| 103 | ]; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * {@inheritdoc} |
| 108 | */ |
| 109 | public function schema( $options = null ) { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * {@inheritdoc} |
| 115 | */ |
| 116 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
| 117 | $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options; |
| 118 | |
| 119 | $options[ static::$type . '_tag' ] = static::get_heading_tag( $options ); |
| 120 | |
| 121 | // Format content. |
| 122 | $options['label'] = $this->display( $options['label'], $name, $options, $pod, $id ); |
| 123 | |
| 124 | if ( isset( $options['_field_object'] ) && $options['_field_object'] instanceof Field ) { |
| 125 | $options['_field_object']->set_arg( 'label', $options['label'] ); |
| 126 | } |
| 127 | |
| 128 | $type = pods_v( 'type', $options, static::$type ); |
| 129 | |
| 130 | $args = compact( array_keys( get_defined_vars() ) ); |
| 131 | $args = (object) $args; |
| 132 | |
| 133 | $this->render_input_script( $args ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * {@inheritdoc} |
| 138 | */ |
| 139 | public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
| 140 | // Support passing label into the options for custom HTML option layouts. |
| 141 | if ( empty( $value ) && ! empty( $options['label'] ) ) { |
| 142 | $value = $options['label']; |
| 143 | } |
| 144 | |
| 145 | $value = $this->strip_html( $value, $options ); |
| 146 | $value = $this->strip_shortcodes( $value, $options ); |
| 147 | $value = $this->trim_whitespace( $value, $options ); |
| 148 | |
| 149 | if ( 1 === (int) pods_v( static::$type . '_wptexturize', $options, 1 ) ) { |
| 150 | $value = wptexturize( $value ); |
| 151 | } |
| 152 | |
| 153 | if ( 1 === (int) pods_v( static::$type . '_allow_shortcode', $options, 0 ) ) { |
| 154 | $value = do_shortcode( $value ); |
| 155 | } |
| 156 | |
| 157 | return $value; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * {@inheritdoc} |
| 162 | */ |
| 163 | public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
| 164 | $value = $this->strip_html( $value, $options ); |
| 165 | $value = $this->strip_shortcodes( $value, $options ); |
| 166 | $value = $this->trim_whitespace( $value, $options ); |
| 167 | |
| 168 | return wp_trim_words( $value ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get the heading tag from the field options and ensure it's allowed. |
| 173 | * |
| 174 | * @since 3.2.7.1 |
| 175 | * |
| 176 | * @param array|Field $options The field data. |
| 177 | * @param null|string $default The default heading tag to use. |
| 178 | * |
| 179 | * @return string The heading tag. |
| 180 | */ |
| 181 | public static function get_heading_tag( $options, ?string $default = null ): string { |
| 182 | // Only allow specific HTML tags. |
| 183 | $allowed_html_tags = [ |
| 184 | 'h1' => 'h1', |
| 185 | 'h2' => 'h2', |
| 186 | 'h3' => 'h3', |
| 187 | 'h4' => 'h4', |
| 188 | 'h5' => 'h5', |
| 189 | 'h6' => 'h6', |
| 190 | 'p' => 'p', |
| 191 | 'div' => 'div', |
| 192 | ]; |
| 193 | |
| 194 | $heading_tag = 'h2'; |
| 195 | |
| 196 | if ( ! empty( $options[ static::$type . '_tag' ] ) && isset( $allowed_html_tags[ $options[ static::$type . '_tag' ] ] ) ) { |
| 197 | $heading_tag = $options[ static::$type . '_tag' ]; |
| 198 | } elseif ( ! empty( $default ) && isset( $allowed_html_tags[ $default ] ) ) { |
| 199 | $heading_tag = $default; |
| 200 | } |
| 201 | |
| 202 | return $heading_tag; |
| 203 | } |
| 204 | } |
| 205 |