avatar.php
21 hours ago
boolean.php
21 hours ago
code.php
21 hours ago
color.php
21 hours ago
comment.php
21 hours ago
currency.php
21 hours ago
date.php
21 hours ago
datetime.php
21 hours ago
email.php
21 hours ago
file.php
21 hours ago
heading.php
21 hours ago
html.php
21 hours ago
link.php
21 hours ago
number.php
21 hours ago
oembed.php
21 hours ago
paragraph.php
21 hours ago
password.php
21 hours ago
phone.php
21 hours ago
pick.php
21 hours ago
slug.php
21 hours ago
taxonomy.php
21 hours ago
text.php
21 hours ago
time.php
21 hours ago
website.php
21 hours ago
wysiwyg.php
21 hours ago
code.php
161 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Handles code field data and operations |
| 5 | * |
| 6 | * @package Pods\Fields |
| 7 | */ |
| 8 | class PodsField_Code extends PodsField { |
| 9 | |
| 10 | /** |
| 11 | * {@inheritdoc} |
| 12 | */ |
| 13 | public static $group = 'Paragraph'; |
| 14 | |
| 15 | /** |
| 16 | * {@inheritdoc} |
| 17 | */ |
| 18 | public static $type = 'code'; |
| 19 | |
| 20 | /** |
| 21 | * {@inheritdoc} |
| 22 | */ |
| 23 | public static $label = 'Code (Syntax Highlighting)'; |
| 24 | |
| 25 | /** |
| 26 | * {@inheritdoc} |
| 27 | */ |
| 28 | public static $prepare = '%s'; |
| 29 | |
| 30 | /** |
| 31 | * {@inheritdoc} |
| 32 | */ |
| 33 | public function setup() { |
| 34 | |
| 35 | static::$group = __( 'Paragraph', 'pods' ); |
| 36 | static::$label = __( 'Code (Syntax Highlighting)', 'pods' ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * {@inheritdoc} |
| 41 | */ |
| 42 | public function options() { |
| 43 | return [ |
| 44 | 'output_options' => [ |
| 45 | 'label' => __( 'Output Options', 'pods' ), |
| 46 | 'type' => 'boolean_group', |
| 47 | 'boolean_group' => [ |
| 48 | static::$type . '_trim' => [ |
| 49 | 'label' => __( 'Trim extra whitespace before/after contents', 'pods' ), |
| 50 | 'default' => 1, |
| 51 | 'type' => 'boolean', |
| 52 | ], |
| 53 | static::$type . '_trim_lines' => [ |
| 54 | 'label' => __( 'Trim whitespace at the end of lines', 'pods' ), |
| 55 | 'default' => 0, |
| 56 | 'type' => 'boolean', |
| 57 | ], |
| 58 | static::$type . '_trim_p_brs' => [ |
| 59 | 'label' => __( 'Remove blank lines including empty "p" tags and "br" tags', 'pods' ), |
| 60 | 'default' => 0, |
| 61 | 'type' => 'boolean', |
| 62 | ], |
| 63 | static::$type . '_trim_extra_lines' => [ |
| 64 | 'label' => __( 'Remove extra blank lines (when there are 3+ blank lines, replace with a maximum of 2)', 'pods' ), |
| 65 | 'default' => 0, |
| 66 | 'type' => 'boolean', |
| 67 | ], |
| 68 | static::$type . '_sanitize_html' => [ |
| 69 | 'label' => __( 'Sanitize HTML', 'pods' ), |
| 70 | 'default' => 1, |
| 71 | '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' ), |
| 72 | 'type' => 'boolean', |
| 73 | 'dependency' => true, |
| 74 | ], |
| 75 | static::$type . '_allow_shortcode' => [ |
| 76 | 'label' => __( 'Allow Shortcodes', 'pods' ), |
| 77 | 'default' => 0, |
| 78 | 'type' => 'boolean', |
| 79 | 'dependency' => true, |
| 80 | ], |
| 81 | ], |
| 82 | ], |
| 83 | static::$type . '_max_length' => [ |
| 84 | 'label' => __( 'Maximum Length', 'pods' ), |
| 85 | 'default' => - 1, |
| 86 | 'type' => 'number', |
| 87 | 'help' => __( 'Set to -1 for no limit', 'pods' ), |
| 88 | ], |
| 89 | ]; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * {@inheritdoc} |
| 94 | */ |
| 95 | public function schema( $options = null ) { |
| 96 | |
| 97 | $length = (int) pods_v( static::$type . '_max_length', $options, 0 ); |
| 98 | |
| 99 | $schema = 'LONGTEXT'; |
| 100 | |
| 101 | if ( 0 < $length ) { |
| 102 | $schema = 'VARCHAR(' . $length . ')'; |
| 103 | } |
| 104 | |
| 105 | return $schema; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * {@inheritdoc} |
| 110 | */ |
| 111 | public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
| 112 | if ( 1 === (int) pods_v( static::$type . '_allow_shortcode', $options, 0 ) ) { |
| 113 | $value = do_shortcode( $value ); |
| 114 | } |
| 115 | |
| 116 | return $this->maybe_sanitize_output( $value, $options ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * {@inheritdoc} |
| 121 | */ |
| 122 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
| 123 | |
| 124 | $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options; |
| 125 | $form_field_type = PodsForm::$field_type; |
| 126 | |
| 127 | $value = $this->normalize_value_for_input( $value, $options, "\n" ); |
| 128 | |
| 129 | $field_type = 'codemirror'; |
| 130 | |
| 131 | do_action( "pods_form_ui_field_code_{$field_type}", $name, $value, $options, $pod, $id ); |
| 132 | do_action( 'pods_form_ui_field_code', $field_type, $name, $value, $options, $pod, $id ); |
| 133 | |
| 134 | if ( ! empty( $options['disable_dfv'] ) ) { |
| 135 | return pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); |
| 136 | } |
| 137 | |
| 138 | $type = pods_v( 'type', $options, static::$type ); |
| 139 | |
| 140 | $args = compact( array_keys( get_defined_vars() ) ); |
| 141 | $args = (object) $args; |
| 142 | |
| 143 | $this->render_input_script( $args ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * {@inheritdoc} |
| 148 | */ |
| 149 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
| 150 | $value = $this->trim_whitespace( $value, $options ); |
| 151 | |
| 152 | $length = (int) pods_v( static::$type . '_max_length', $options, 0 ); |
| 153 | |
| 154 | if ( 0 < $length && $length < pods_mb_strlen( $value ) ) { |
| 155 | $value = pods_mb_substr( $value, 0, $length ); |
| 156 | } |
| 157 | |
| 158 | return $value; |
| 159 | } |
| 160 | } |
| 161 |