Completed
Pull Request — 2.x (#4569)
by Scott Kingsley
04:42
created

PodsField_Code::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
		self::$label = __( 'Code (Syntax Highlighting)', 'pods' );
36
	}
37
38
	/**
39
	 * {@inheritdoc}
40
	 */
41
	public function options() {
42
43
		$options = array(
44
			static::$type . '_repeatable' => array(
45
				'label'             => __( 'Repeatable Field', 'pods' ),
46
				'default'           => 0,
47
				'type'              => 'boolean',
48
				'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' ),
49
				'boolean_yes_label' => '',
50
				'dependency'        => true,
51
				'developer_mode'    => true,
52
			),
53
			'output_options'              => array(
54
				'label' => __( 'Output Options', 'pods' ),
55
				'group' => array(
56
					static::$type . '_allow_shortcode' => array(
57
						'label'      => __( 'Allow Shortcodes?', 'pods' ),
58
						'default'    => 0,
59
						'type'       => 'boolean',
60
						'dependency' => true,
61
					),
62
				),
63
			),
64
			static::$type . '_max_length' => array(
65
				'label'   => __( 'Maximum Length', 'pods' ),
66
				'default' => 0,
67
				'type'    => 'number',
68
				'help'    => __( 'Set to -1 for no limit', 'pods' ),
69
			),
70
		);
71
72
		return $options;
73
	}
74
75
	/**
76
	 * {@inheritdoc}
77
	 */
78
	public function schema( $options = null ) {
79
80
		$length = (int) pods_v( static::$type . '_max_length', $options, 0 );
81
82
		$schema = 'LONGTEXT';
83
84
		if ( 0 < $length ) {
85
			$schema = 'VARCHAR(' . $length . ')';
86
		}
87
88
		return $schema;
89
	}
90
91
	/**
92
	 * {@inheritdoc}
93
	 */
94
	public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
95
96
		if ( 1 === (int) pods_v( static::$type . '_allow_shortcode', $options, 0 ) ) {
97
			$value = do_shortcode( $value );
98
		}
99
100
		return $value;
101
	}
102
103
	/**
104
	 * {@inheritdoc}
105
	 */
106
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
107
108
		$options         = (array) $options;
109
		$form_field_type = PodsForm::$field_type;
110
111
		if ( is_array( $value ) ) {
112
			$value = implode( "\n", $value );
113
		}
114
115
		$field_type = 'codemirror';
116
117
		do_action( 'pods_form_ui_field_code_' . $field_type, $name, $value, $options, $pod, $id );
118
		do_action( 'pods_form_ui_field_code', $field_type, $name, $value, $options, $pod, $id );
119
120
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
121
	}
122
123
	/**
124
	 * {@inheritdoc}
125
	 */
126 View Code Duplication
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
128
		$length = (int) pods_v( static::$type . '_max_length', $options, 0 );
129
130
		if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
131
			$value = pods_mb_substr( $value, 0, $length );
132
		}
133
134
		return $value;
135
	}
136
}
137