PluginProbe
WPGet API – Connect to any external REST API / 1.0.1
WPGet API – Connect to any external REST API v1.0.1
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.0.1, at includes/class-fields.php

153 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><!--
54
55 --><div class="value input-wrap">
56 <label for="<?php esc_attr_e( $this->_id( '_value' ) ); ?>">
57 <?php esc_html_e( $this->_text( 'parameter_value_text', __( 'Value', 'wpgetapi' ) ) ); ?>
58 </label>
59
60 <?php echo $this->types->input( array(
61 'name' => $this->_name( '[value]' ),
62 'id' => $this->_id( '_value' ),
63 'value' => $value['value'],
64 'desc' => '',
65 'placeholder' => __( 'Value of the parameter', 'wpgetapi' ),
66 ) ); ?>
67 </div>
68
69 <?php
70
71 // grab the data from the output buffer.
72 return $this->rendered( ob_get_clean() );
73 }
74
75
76 /**
77 * Optionally save the values into separate fields
78 */
79 public static function maybe_save_split_values( $override_value, $value, $object_id, $field_args ) {
80
81 // Don't do the override
82 if ( ! isset( $field_args['split_values'] ) || ! $field_args['split_values'] )
83 return $override_value;
84
85 $encryption = new WpGetApi_Encryption();
86
87 $parameter_keys = array( 'name', 'value' );
88 foreach ( $parameter_keys as $key ) {
89 if ( ! empty( $value[ $key ] ) ) {
90 $updated_value = $encryption->encrypt( $value[ $key ] );
91 update_post_meta( $object_id, $field_args['id'] . 'parameter_'. $key, $updated_value );
92 }
93 }
94
95 remove_filter( 'cmb2_sanitize_parameter', array( __CLASS__, 'sanitize' ), 10, 5 );
96 // Tell CMB2 we already did the update
97 return true;
98 }
99
100
101 public static function sanitize( $check, $meta_value, $object_id, $field_args, $sanitize_object ) {
102
103 // if not repeatable, bail out.
104 if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] )
105 return $check;
106
107 $encryption = new WpGetApi_Encryption();
108
109 foreach ( $meta_value as $i => $param ) {
110
111 // if both empty, unset and continue
112 if( $param['name'] == '' && $param['value'] == '' ) {
113 unset( $meta_value[ $i ] );
114 continue;
115 }
116
117 foreach ( $param as $key => $val ) {
118 $meta_value[ $i ][ $key ] = $key == 'name' ? $val : $encryption->encrypt( $val );
119 }
120
121 }
122
123 return array_filter($meta_value);
124 }
125
126
127 public static function escape( $check, $meta_value, $field_args, $field_object ) {
128
129 // if not repeatable, bail out.
130 if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] )
131 return $check;
132
133 $encryption = new WpGetApi_Encryption();
134
135 foreach ( $meta_value as $i => $param ) {
136
137 // if both empty, unset and continue
138 if( $param['name'] == '' && $param['value'] == '' ) {
139 unset( $meta_value[ $i ] );
140 continue;
141 }
142
143 foreach ( $param as $key => $val ) {
144 $meta_value[ $i ][ $key ] = $key == 'name' ? $val : $encryption->decrypt( $val );
145 }
146
147 }
148
149 return array_filter($meta_value);
150 }
151
152
153 }