1 <?php
2
3 4 5 6 7 8 9 10 11 12
13 class Fieldmanager_Datasource {
14
15 16 17
18 public $options = array();
19
20 21 22
23 public $options_callback = Null;
24
25 26 27
28 public $use_ajax = False;
29
30 31 32
33 public $allow_optgroups = True;
34
35 36 37
38 public $ajax_action = '';
39
40 41 42 43
44 public static $counter = 0;
45
46 47 48 49
50 public $grouped = False;
51
52 53 54
55 public function __construct( $options = array() ) {
56
57 foreach ( $options as $k => $v ) {
58 try {
59 $reflection = new ReflectionProperty( $this, $k );
60 if ( $reflection->isPublic() ) $this->$k = $v;
61 else throw new FM_Developer_Exception;
62 } catch ( Exception $e ) {
63 $message = sprintf(
64 __( 'You attempted to set a property "%1$s" that is nonexistant or invalid for an instance of "%2$s" named "%3$s".', 'fieldmanager' ),
65 $k, __CLASS__, !empty( $options['name'] ) ? $options['name'] : 'NULL'
66 );
67 $title = esc_html__( 'Nonexistant or invalid option' );
68 if ( !Fieldmanager_Field::$debug ) {
69 wp_die( esc_html( $message ), $title );
70 } else {
71 throw new FM_Developer_Exception( esc_html( $message ) );
72 }
73 }
74 }
75
76 if ( get_class( $this ) == __CLASS__ && empty( $options ) ) {
77 $message = esc_html__( 'Invalid options for Datasource; must use the options parameter to supply an array.', 'fieldmanager' );
78 if ( Fieldmanager_Field::$debug ) {
79 throw new FM_Developer_Exception( $message );
80 } else {
81 wp_die( $message, esc_html__( 'Invalid Datasource Options', 'fieldmanager' ) );
82 }
83 }
84
85 if ( !empty( $this->options ) ) {
86 $keys = array_keys( $this->options );
87 if ( ( array_keys( $keys ) === $keys ) ) {
88 foreach ( $this->options as $k => $v ) {
89 $this->options[$v] = $v;
90 unset( $this->options[$k] );
91 }
92 }
93 }
94
95 if ( $this->use_ajax ) {
96 add_action( 'wp_ajax_' . $this->get_ajax_action(), array( $this, 'autocomplete_search' ) );
97 }
98 }
99
100 101 102 103 104 105
106 public function get_value( $id ) {
107 return isset( $this->options[ $id ] ) ? $this->options[ $id ] : '';
108 }
109
110 111 112 113 114
115 public function get_items( $fragment = Null ) {
116 if ( !$fragment ) {
117 return $this->options;
118 }
119 $ret = array();
120 foreach ( $this->options as $k => $v ) {
121 if ( strpos( $v, $fragment ) !== False ) $ret[$k] = $v;
122 }
123 return $ret;
124 }
125
126 127 128 129 130
131 public function get_ajax_action() {
132 if ( !empty( $this->ajax_action ) ) return $this->ajax_action;
133 return 'fm_datasource_' . crc32( 'base' . json_encode( $this->options ) . $this->options_callback );
134 }
135
136 137 138 139 140
141 public function get_items_for_ajax( $fragment = null ) {
142 $items = $this->get_items( $fragment );
143 $return = array();
144
145 foreach ( $items as $id => $label ) {
146 $return[] = array( 'label' => $label, 'value' => $id );
147 }
148
149 return $return;
150 }
151
152 153 154 155
156 public function autocomplete_search() {
157
158 check_ajax_referer( 'fm_search_nonce', 'fm_search_nonce' );
159 $items = $this->get_items_for_ajax( sanitize_text_field( $_POST['fm_autocomplete_search'] ) );
160
161
162 if ( ! empty( $items ) ) {
163 wp_send_json( $items );
164 } else {
165 wp_send_json( 0 );
166 }
167 }
168
169 170 171 172 173 174 175
176 public function presave_alter_values( Fieldmanager_Field $field, $values, $current_values ) {
177
178 return $values;
179 }
180
181 182 183 184 185 186
187 public function preload_alter_values( Fieldmanager_Field $field, $values ) {
188 return $values;
189 }
190
191 192 193 194 195 196 197
198 public function presave( Fieldmanager_Field $field, $value, $current_value ) {
199 if ( is_array( $value ) ) {
200 return array_map( 'sanitize_text_field', $value );
201 }
202 return sanitize_text_field( $value );
203 }
204
205 206 207 208 209
210 public function get_view_link( $value ) {
211 return '';
212 }
213
214 215 216 217 218
219 public function get_edit_link( $value ) {
220 return '';
221 }
222
223 }
224