PluginProbe
WPGet API – Connect to any external REST API / 1.9.7
WPGet API – Connect to any external REST API v1.9.7
1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.10 2.2.2 2.2.3 All 97 releases
wpgetapi / includes / class-fields.php

class-fields.php in WPGet API – Connect to any external REST API 1.9.7, at includes/class-fields.php

152 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5
6 /**
7 * Handles custom field typea.
8 */
9 class WpGetApi_Parameter_Field extends CMB2_Type_Base {
10
11
12 public static function init_parameter() {
13 add_filter( 'cmb2_render_class_parameter', array( __CLASS__, 'class_name' ) );
14 add_filter( 'cmb2_sanitize_parameter', array( __CLASS__, 'maybe_save_split_values' ), 12, 4 );
15 /**
16 * The following snippets are required for allowing the users field
17 * to work as a repeatable field, or in a repeatable group
18 */
19 add_filter( 'cmb2_sanitize_parameter', array( __CLASS__, 'sanitize' ), 10, 5 );
20 add_filter( 'cmb2_types_esc_parameter', array( __CLASS__, 'escape' ), 10, 4 );
21 }
22
23 public static function class_name() { return __CLASS__; }
24
25
26 /**
27 * Handles outputting the users field.
28 */
29 public function render() {
30
31 // make sure we assign each part of the value we need.
32 $value = wp_parse_args( $this->field->escaped_value(), array(
33 'name' => '',
34 'value' => '',
35 ) );
36
37 ob_start();
38 // Do html
39 ?>
40
41 <div class="name input-wrap">
42 <label for="<?php esc_attr_e( $this->_id( '_name' ) ); ?>">
43 <?php esc_html_e( $this->_text( 'parameter_name_text', __( 'Name', 'wpgetapi' ) ) ); ?>
44 </label>
45
46 <?php echo $this->types->input( array(
47 'name' => $this->_name( '[name]' ),
48 'id' => $this->_id( '_name' ),
49 'value' => $value['name'],
50 'desc' => '',
51 'placeholder' => __( 'Name of the parameter', 'wpgetapi' ),
52 ) ); ?>
53 </div><span class="colon">:</span><div class="value input-wrap">
54 <label for="<?php esc_attr_e( $this->_id( '_value' ) ); ?>">
55 <?php esc_html_e( $this->_text( 'parameter_value_text', __( 'Value', 'wpgetapi' ) ) ); ?>
56 </label>
57
58 <?php echo $this->types->textarea( array(
59 'name' => $this->_name( '[value]' ),
60 'id' => $this->_id( '_value' ),
61 'value' => stripslashes($value['value']),
62 'desc' => '',
63 'rows' => 1,
64 'placeholder' => __( 'Value of the parameter', 'wpgetapi' ),
65 ) ); ?>
66 </div>
67
68 <?php
69
70 // grab the data from the output buffer.
71 return $this->rendered( ob_get_clean() );
72 }
73
74
75 /**
76 * Optionally save the values into separate fields
77 */
78 public static function maybe_save_split_values( $override_value, $value, $object_id, $field_args ) {
79
80 // Don't do the override
81 if ( ! isset( $field_args['split_values'] ) || ! $field_args['split_values'] )
82 return $override_value;
83
84 $encryption = new WpGetApi_Encryption();
85
86 $parameter_keys = array( 'name', 'value' );
87 foreach ( $parameter_keys as $key ) {
88 if ( ! empty( $value[ $key ] ) ) {
89 $updated_value = $encryption->encrypt( $value[ $key ] );
90 update_post_meta( $object_id, $field_args['id'] . 'parameter_'. $key, $updated_value );
91 }
92 }
93
94 remove_filter( 'cmb2_sanitize_parameter', array( __CLASS__, 'sanitize' ), 10, 5 );
95 // Tell CMB2 we already did the update
96 return true;
97 }
98
99
100 public static function sanitize( $check, $meta_value, $object_id, $field_args, $sanitize_object ) {
101
102 // if not repeatable, bail out.
103 if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] )
104 return $check;
105
106 $encryption = new WpGetApi_Encryption();
107
108 foreach ( $meta_value as $i => $param ) {
109
110 // if both empty, unset and continue
111 if( $param['name'] == '' && $param['value'] == '' ) {
112 unset( $meta_value[ $i ] );
113 continue;
114 }
115
116 foreach ( $param as $key => $val ) {
117 $meta_value[ $i ][ $key ] = $key === 'name' ? $val : $encryption->encrypt( $val );
118 }
119
120 }
121
122 return array_filter($meta_value);
123 }
124
125
126 public static function escape( $check, $meta_value, $field_args, $field_object ) {
127
128 // if not repeatable, bail out.
129 if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] )
130 return $check;
131
132 $encryption = new WpGetApi_Encryption();
133
134 foreach ( $meta_value as $i => $param ) {
135
136 // if both empty, unset and continue
137 if( $param['name'] == '' && $param['value'] == '' ) {
138 unset( $meta_value[ $i ] );
139 continue;
140 }
141
142 foreach ( $param as $key => $val ) {
143 $meta_value[ $i ][ $key ] = $key === 'name' ? $val : $encryption->decrypt( $val );
144 }
145
146 }
147
148 return array_filter($meta_value);
149 }
150
151
152 }