PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/types/embedded/common/toolset-forms/classes/class.types.php

https://github.com/ngocphuuit/wordpress-tax
PHP | 496 lines | 218 code | 35 blank | 243 comment | 35 complexity | 518919b18aee46260a122c04496d0148 MD5 | raw file
  1<?php
  2/**
  3 * Types fields specific
  4 *
  5 * $HeadURL: https://www.onthegosystems.com/misc_svn/common/tags/Views-1.6.1-Types-1.5.7/toolset-forms/classes/class.types.php $
  6 * $LastChangedDate: 2014-04-16 10:42:22 +0200 (Wed, 16 Apr 2014) $
  7 * $LastChangedRevision: 21561 $
  8 * $LastChangedBy: marcin $
  9 *
 10 */
 11
 12/**
 13 * Class description
 14 *
 15 * @author Srdjan
 16 */
 17class WPToolset_Types
 18{
 19
 20    /**
 21     * Filters Types field to match data structure needed for shared code.
 22     *
 23     * @global type $pagenow
 24     * @staticvar array $cache
 25     * @param type $field array|string $field settings array (as stored in DB) or field ID
 26     * @param type $post_id Post or user ID used for conditional
 27     * @return array
 28     */
 29    static function filterField($field, $post_id = null)
 30    {
 31        // Get field settings (as when using get_option('wpcf-fields') from DB)
 32        $field = self::getConfig( $field );
 33        if ( is_null( $field ) ) return array();
 34
 35        // Caching
 36        static $cache = array();
 37        $cache_key = md5( serialize( $field ) . $post_id );
 38        if ( isset( $cache[$cache_key] ) ) {
 39            return $cache[$cache_key];
 40        }
 41
 42        // Prefix - used to construct ID of field @see at bottom 'General field settings'
 43        $prefix = self::getPrefix( $field );
 44
 45        /* Suffix - used to construct ID of this field and other fields connected
 46         * to it via conditionals.
 47         *
 48         * @see at bottom 'General field settings'
 49         *
 50         * Reason to use it - Types have child posts forms inside same form as
 51         * main fields. It's like having same sets of fields inside same form.
 52         * Main post fields do not have suffix.
 53         *
 54         * Example main field:
 55         * ID: wpcf-text
 56         * conditional: '$wpcf-date > DATE(01,02,2014)'
 57         *
 58         * Example child field:
 59         * ID: wpcf-text-123
 60         * conditional: '$wpcf-date-123 > DATE(01,02,2014)'
 61         * Suffix is child post ID (wpcf-text-$child_post_id).
 62         *
 63         * That way right triggers and conditional fields are mapped.
 64         */
 65        $suffix = isset( $field['suffix'] ) ? $field['suffix'] : '';
 66
 67        /* General field settings
 68         *
 69         * Main settings that are returned.
 70         */
 71        $_field = array(
 72            'id' => $prefix . $field['id'] . $suffix, // Used as main ID (raw date wpt-id), used to connect conditional relations
 73            'meta_key' => $prefix . $field['id'], // Used by Types (meta key of field saved to DB)
 74            'meta_type' => isset( $field['meta_type'] ) ? $field['meta_type'] : 'postmeta', // Used by Types (postmeta|usermeta)
 75            'type' => $field['type'], // Type of field (textfield, skype, email etc)
 76            'slug' => $field['id'], // Not sure if used by Types TODO REVISE
 77            'title' => self::translate( "field {$field['id']} name",
 78                    $field['name'] ), // Translated title
 79            'description' => !empty( $field['description'] ) ? self::translate( "field {$field['id']} description",
 80                    $field['description'] ) : '', // Translated description
 81            'name' => isset( $field['form_name'] ) ? $field['form_name'] : "wpcf[{$field['id']}]", // Form element name, default wpcf[$no-prefix-slug]
 82            'repetitive' => self::isRepetitive( $field ), // Is repetitive?
 83            'validation' => self::filterValidation( $field ), // Validation settings
 84            'conditional' => self::filterConditional( $field, $post_id ), // Conditional settings
 85        );
 86
 87        /* Specific field settings
 88         *
 89         * There are some field types that needs extra data or adjusted data
 90         * for readibility.
 91         */
 92        switch( $field['type'] ) {
 93            // Checkbox set default_value
 94        case 'checkbox':
 95            $_field['default_value'] = $field['data']['set_value'];
 96            $_field['checked'] = array_key_exists( 'checked', $field['data'] )? $field['data']['checked']:false;
 97            break;
 98            // Checkboxes set field [options]
 99        case 'checkboxes':
100            if ( !empty( $field['data']['options'] ) ) {
101                global $pagenow;
102                foreach ( $field['data']['options'] as $option_key => $option ) {
103                    // Set value
104                    $_field['options'][$option_key] = array(
105                        'value' => $option['set_value'],
106                        'title' => self::translate( 'field ' . $field['id'] . ' option '
107                        . $option_key . ' title', $option['title'] ),
108                        'name' => 'wpcf[' . $field['id'] . '][' . $option_key . ']',
109                    );
110                    if ( $pagenow == 'post-new.php' && !empty( $option['checked'] ) ) {
111                        $_field['options'][$option_key]['checked'] = true;
112                    }
113                }
114            }
115            break;
116        case 'date':
117            $_field['add_time'] = !empty( $field['data']['date_and_time'] ) && $field['data']['date_and_time'] == 'and_time';
118            break;
119            // Radio and Select set field [options]
120        case 'radio':
121        case 'select':
122            if ( !empty( $field['data']['options'] ) ) {
123                foreach ( $field['data']['options'] as $k => $option ) {
124                    if ( $k == 'default' ) {
125                        continue;
126                    }
127                    if ( array_key_exists( 'default', $field['data']['options'] ) && $field['data']['options']['default'] == $k ) {
128                        $_field['default_value'] = $option['value'];
129                    }
130                    $_field['options'][] = array(
131                        'value' => $option['value'],
132                        'title' => self::translate( 'field ' . $field['id'] . ' option '
133                        . $k . ' title', $option['title'] ),
134                    );
135                }
136            }
137            break;
138        }
139        // Radio adjust type name because form_factory class name contains 'Radios'
140        if ( $field['type'] == 'radio' ) {
141            $_field['type'] = 'radios';
142        }
143        return $cache[$cache_key] = $_field;
144    }
145
146    /**
147     * Filters validation.
148     *
149     * Loop over validation settings and create array of validation rules.
150     * array( $rule => array( 'args' => array, 'message' => string ), ... )
151     *
152     * @param array|string $field settings array (as stored in DB) or field ID
153     * @return array array( $rule => array( 'args' => array, 'message' => string ), ... )
154     */
155    public static function filterValidation($config)
156    {
157        $config = self::getConfig( $config );
158        if ( is_null( $config ) ) return array();
159        /* Placeholder for field value '$value'.
160         *
161         * Used for validation settings.
162         * Field value is not processed here, instead string '$value' is used
163         * to be replaced with actual value when needed.
164         *
165         * For example:
166         * validation['rangelength'] = array(
167         *     'args' => array( '$value', 5, 12 ),
168         *     'message' => 'Value length between %s and %s required'
169         * );
170         * validation['reqiuired'] = array(
171         *     'args' => array( '$value', true ),
172         *     'message' => 'This field is required'
173         * );
174         *
175         * Types have default and custom messages defined on it's side.
176         */
177        $value = '$value';
178        $validation = array();
179        if ( isset( $config['data']['validate'] ) ) {
180            foreach ( $config['data']['validate'] as $rule => $settings ) {
181                if ( $settings['active'] ) {
182                    $validation[$rule] = array(
183                        'args' => isset( $settings['args'] ) ? array_unshift( $value,
184                                        $settings['args'] ) : array($value, true),
185                        'message' => $settings['message']
186                    );
187                }
188            }
189        }
190        return $validation;
191    }
192
193    /**
194     * Filters conditional.
195     *
196     * There are two types of conditionals:
197     * 1. Regular conditionals created using Types GUI
198     * 2. Custom onditionals (user entered manually)
199     *
200     * 1. Regular conditional
201     *
202     * Main properties:
203     * [relation] - AND|OR evaluate as true if all or any condition is TRUE
204     * [conditions] - array of conditions
205     * [values] - values to check against (used only by PHP), evaluate method
206     *      should not be aware if it's checking post meta or user meta,
207     *      instead array of needed values (or missing) are passed to method.
208     *      Types use filteres get_post_meta() and get_user_meta().
209     *
210     * [conditions]
211     * Each conditions is array with properties:
212     * id: ID of trigger field (this field value is checked) to evaluate
213     *      this field as TRUE or FALSE (corresponds to main IDs set here)
214     * type: type of trigger field. JS and PHP needs at least this information
215     *      when processing condtitional evaluation
216     * operator: which operation to perform (=|>|<|>=|<=|!=|between)
217     * args: arguments passed to checking functions
218     *
219     * Example of reguar conditional
220     *
221     * [conditional] => Array(
222            [relation] => OR
223            [conditions] => Array(
224                [0] => Array(
225                    [id] => wpcf-my-date
226                    [type] => date
227                    [operator] => =
228                    [args] => Array(
229                        [0] => 02/01/2014
230                    )
231                )
232                [1] => Array(
233                    [id] => wpcf-my-date
234                    [type] => date
235                    [operator] => between
236                    [args] => Array(
237                        [0] => 02/07/2014
238                        [1] => 02/10/2014
239                    )
240                 )
241            )
242      [values] => Array(
243        [wpcf-my-date] => 32508691200
244        )
245      )
246     *
247     *
248     * 2. Custom conditional
249     * Main properties:
250     * [custom] - custom statement made by user, note that $xxxx should match
251     *      IDs of fields that passed this filter.
252     * [values] - same as for regular conditional
253     *
254     * [conditional] => Array(
255            [custom] => ($wpcf-my-date = DATE(01,02,2014)) OR ($wpcf-my-date > DATE(07,02,2014))
256            [values] => Array(
257                [wpcf-my-date] => 32508691200
258            )
259      )
260     *
261     * @param array|string $field settings array (as stored in DB) or field ID
262     * @param int $post_id Post or user ID to fetch meta data to check against
263     * @return array
264     */
265    public static function filterConditional($field, $post_id)
266    {
267        $field = self::getConfig( $field );
268        if ( is_null( $field ) ) return array();
269
270        // Caching
271        static $cache = array();
272        $cache_key = md5( serialize( $field ) . $post_id );
273        if ( isset( $cache[$cache_key] ) ) {
274            return $cache[$cache_key];
275        }
276
277        /* Suffix - used to construct ID of this field and other fields connected
278         * to it via conditionals.
279         *
280         * @see at bottom 'General field settings'
281         *
282         * Reason to use it - Types have child posts forms inside same form as
283         * main fields. It's like having same sets of fields inside same form.
284         * Main post fields do not have suffix.
285         *
286         * Example main field:
287         * ID: wpcf-text
288         * conditional: '$wpcf-date > DATE(01,02,2014)'
289         *
290         * Example child field:
291         * ID: wpcf-text-123
292         * conditional: '$wpcf-date-123 > DATE(01,02,2014)'
293         * Suffix is child post ID (wpcf-text-$child_post_id).
294         *
295         * That way right triggers and conditional fields are mapped.
296         */
297        $suffix = isset( $field['suffix'] ) ? $field['suffix'] : '';
298        $cond = array();
299        $cond_values = array();
300        if ( empty( $field['meta_type'] ) ) {
301            $field['meta_type'] = 'postmeta';
302        }
303
304        // Get [values]
305        if ( !empty( $post_id ) ) {
306            $cond_values = $field['meta_type'] == 'usermeta' ? get_user_meta( $post_id ) : get_post_custom( $post_id );
307        }
308
309        // Unserialize [values] and do not allow array (take first value from array
310        if ( is_array( $cond_values ) ) {
311            foreach ( $cond_values as $k => &$v ) {
312                $v = maybe_unserialize( $v[0] );
313                $v = self::getStringFromArray($v);
314            }
315        }
316
317        // Set custom conditional
318        if ( !empty( $field['data']['conditional_display']['custom_use'] ) ) {
319            require_once 'class.conditional.php';
320            $custom = $field['data']['conditional_display']['custom'];
321
322            // Extract field values ('${$field_name}')
323            $c_fields = WPToolset_Forms_Conditional::extractFields( $custom );
324            $c_values = array();
325
326            // Loop over extracted fields and adjust custom statement
327            foreach ( $c_fields as $c_field_id ) {
328
329                // Get field settings
330                $c_field = self::getConfig( $c_field_id );
331
332                // If it's Types field
333                if ( !empty( $c_field ) ) {
334
335                    // Adjust custom statement
336                    $custom = preg_replace( '/\$' . $c_field_id . '[\s\)]/',
337                            "\${$c_field['meta_key']}{$suffix} ", $custom );
338
339                    // Apply filters from field (that is why we set 'type' property)
340                    wptoolset_form_field_add_filters( $c_field['type'] );
341                    $c_key = $c_field['meta_key'];
342                    if ( isset( $cond_values[$c_key] ) ) {
343                        $c_values[$c_key . $suffix] = apply_filters( 'wptoolset_conditional_value_php',
344                                $cond_values[$c_key], $c_field['type'] );
345                    }
346
347                    // Otherwise do nothing (leave statement as it is and just add [values])
348                    // That allows checking for non-Types field
349                } elseif ( isset( $cond_values[$c_field_id] ) ) {
350                    $c_values[$c_field_id . $suffix] = $cond_values[$c_field_id];
351                }
352            }
353
354            // Set conditional setting
355            $cond = array(
356                'custom' => $custom,
357                'values' => $c_values,
358            );
359
360            // Regular conditional
361        } elseif ( !empty( $field['data']['conditional_display']['conditions'] ) ) {
362            $cond = array(
363                'relation' => $field['data']['conditional_display']['relation'],
364                'conditions' => array(),
365                'values' => array(),
366            );
367
368            // Loop over conditions and collect settings
369            foreach ( $field['data']['conditional_display']['conditions'] as $d ) {
370
371                // Get field settings
372                $c_field = self::getConfig( $d['field'] );
373
374                // If it's Types field
375                if ( !empty( $c_field ) ) {
376                    $_c = array(
377                        'id' => self::getPrefix( $c_field ) . $d['field'] . $suffix,
378                        'type' => $c_field['type'],
379                        'operator' => $d['operation'],
380                        'args' => array($d['value']),
381                    );
382                    $cond['conditions'][] = $_c;
383
384                    // Apply filters from field (that is why we set 'type' property)
385                    wptoolset_form_field_add_filters( $field['type'] );
386                    $key = $c_field['meta_key'];
387                    if ( isset( $cond_values[$key] ) ) {
388                        $cond['values'][$key . $suffix] = apply_filters( 'wptoolset_conditional_value_php',
389                                $cond_values[$key], $c_field['type'] );
390                    }
391
392                    // Otherwise do nothing add [values]
393                    // That allows checking for non-Types field
394                    // TODO REVIEW THIS
395                } elseif ( isset( $cond_values[$d['field']] ) ) {
396                    $cond['values'][$d['field'] . $suffix] = $cond_values[$d['field']];
397                }
398            }
399        }
400        unset( $cond_values, $c_values, $c_field );
401        return $cache[$cache_key] = $cond;
402    }
403
404    /**
405     * Checks if field is repetitive.
406     *
407     * @param type $field
408     * @return null|boolean
409     */
410    public static function isRepetitive($config)
411    {
412        $config = self::getConfig( $config );
413        if ( is_null( $config ) ) return null;
414        return !empty( $config['data']['repetitive'] );
415    }
416
417    /**
418     * Returns all fields configurations created by Types.
419     *
420     * @return array Array of field settings
421     */
422    public static function getFields()
423    {
424        return get_option( 'wpcf-fields', array() );
425    }
426
427    /**
428     * Get specific field configuration created by Types.
429     *
430     * @param type $field_id
431     * @return array|null
432     */
433    public static function getConfig($field_id)
434    {
435        if ( !is_string( $field_id ) ) {
436            return is_array( $field_id ) ? $field_id : null;
437        }
438        $fields = self::getFields();
439        return isset( $fields[$field_id] ) ? $fields[$field_id] : null;
440    }
441
442    /**
443     * Returns prefix for specific field meta_key or ID.
444     *
445     * @param type $field
446     * @return string
447     */
448    public static function getPrefix($config)
449    {
450        return !empty( $config['data']['controlled'] ) ? '' : 'wpcf-';
451    }
452
453    /**
454     * Translates various strings connected to Types using WPML icl_t().
455     *
456     * @param type $name
457     * @param type $string
458     * @param type $context
459     * @return string
460     */
461    public static function translate($name, $string, $context = 'plugin Types')
462    {
463        if ( !function_exists( 'icl_t' ) ) {
464            return $string;
465        }
466        return icl_t( $context, $name, stripslashes( $string ) );
467    }
468
469    /**
470     * Returns field meta key.
471     *
472     * @param type $config
473     * @return type
474     */
475    public static function getMetakey($config)
476    {
477        return self::getPrefix( $config ) . $config['id'];
478    }
479
480    /**
481     * return first string value
482     *
483     * @param type $string
484     * @return string
485     */
486    private static function getStringFromArray($array)
487    {
488        if ( is_object( $array ) ) {
489            return $array;
490        }
491        if ( is_array( $array ) ) {
492            return self::getStringFromArray(array_shift($array));
493        }
494        return strval( $array );
495    }
496}