avatar.php
2 weeks ago
boolean.php
2 weeks ago
code.php
2 weeks ago
color.php
2 weeks ago
comment.php
2 weeks ago
currency.php
2 weeks ago
date.php
2 weeks ago
datetime.php
2 weeks ago
email.php
2 weeks ago
file.php
2 weeks ago
heading.php
2 weeks ago
html.php
2 weeks ago
link.php
2 weeks ago
number.php
2 weeks ago
oembed.php
2 weeks ago
paragraph.php
2 weeks ago
password.php
2 weeks ago
phone.php
2 weeks ago
pick.php
2 weeks ago
slug.php
2 weeks ago
taxonomy.php
2 weeks ago
text.php
2 weeks ago
time.php
2 weeks ago
website.php
2 weeks ago
wysiwyg.php
2 weeks ago
code.php
154 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 | |
| 44 | $options = array( |
| 45 | static::$type . '_repeatable' => array( |
| 46 | 'label' => __( 'Repeatable Field', 'pods' ), |
| 47 | 'default' => 0, |
| 48 | 'type' => 'boolean', |
| 49 | 'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ), |
| 50 | 'boolean_yes_label' => '', |
| 51 | 'dependency' => true, |
| 52 | 'developer_mode' => true, |
| 53 | ), |
| 54 | 'output_options' => array( |
| 55 | 'label' => __( 'Output Options', 'pods' ), |
| 56 | 'type' => 'boolean_group', |
| 57 | 'boolean_group' => array( |
| 58 | static::$type . '_trim' => array( |
| 59 | 'label' => __( 'Trim extra whitespace before/after contents', 'pods' ), |
| 60 | 'default' => 1, |
| 61 | 'type' => 'boolean', |
| 62 | 'dependency' => true, |
| 63 | ), |
| 64 | static::$type . '_allow_shortcode' => array( |
| 65 | 'label' => __( 'Allow Shortcodes', 'pods' ), |
| 66 | 'default' => 0, |
| 67 | 'type' => 'boolean', |
| 68 | 'dependency' => true, |
| 69 | ), |
| 70 | ), |
| 71 | ), |
| 72 | static::$type . '_max_length' => array( |
| 73 | 'label' => __( 'Maximum Length', 'pods' ), |
| 74 | 'default' => -1, |
| 75 | 'type' => 'number', |
| 76 | 'help' => __( 'Set to -1 for no limit', 'pods' ), |
| 77 | ), |
| 78 | ); |
| 79 | |
| 80 | return $options; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * {@inheritdoc} |
| 85 | */ |
| 86 | public function schema( $options = null ) { |
| 87 | |
| 88 | $length = (int) pods_v( static::$type . '_max_length', $options, 0 ); |
| 89 | |
| 90 | $schema = 'LONGTEXT'; |
| 91 | |
| 92 | if ( 0 < $length ) { |
| 93 | $schema = 'VARCHAR(' . $length . ')'; |
| 94 | } |
| 95 | |
| 96 | return $schema; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * {@inheritdoc} |
| 101 | */ |
| 102 | public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
| 103 | if ( 1 === (int) pods_v( static::$type . '_allow_shortcode', $options, 0 ) ) { |
| 104 | $value = do_shortcode( $value ); |
| 105 | } |
| 106 | |
| 107 | return $this->maybe_sanitize_output( $value, $options ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * {@inheritdoc} |
| 112 | */ |
| 113 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
| 114 | |
| 115 | $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options; |
| 116 | $form_field_type = PodsForm::$field_type; |
| 117 | |
| 118 | if ( is_array( $value ) ) { |
| 119 | $value = implode( "\n", $value ); |
| 120 | } |
| 121 | |
| 122 | $field_type = 'codemirror'; |
| 123 | |
| 124 | do_action( "pods_form_ui_field_code_{$field_type}", $name, $value, $options, $pod, $id ); |
| 125 | do_action( 'pods_form_ui_field_code', $field_type, $name, $value, $options, $pod, $id ); |
| 126 | |
| 127 | if ( ! empty( $options['disable_dfv'] ) ) { |
| 128 | return pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); |
| 129 | } |
| 130 | |
| 131 | $type = pods_v( 'type', $options, static::$type ); |
| 132 | |
| 133 | $args = compact( array_keys( get_defined_vars() ) ); |
| 134 | $args = (object) $args; |
| 135 | |
| 136 | $this->render_input_script( $args ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * {@inheritdoc} |
| 141 | */ |
| 142 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
| 143 | $value = $this->trim_whitespace( $value, $options ); |
| 144 | |
| 145 | $length = (int) pods_v( static::$type . '_max_length', $options, 0 ); |
| 146 | |
| 147 | if ( 0 < $length && $length < pods_mb_strlen( $value ) ) { |
| 148 | $value = pods_mb_substr( $value, 0, $length ); |
| 149 | } |
| 150 | |
| 151 | return $value; |
| 152 | } |
| 153 | } |
| 154 |