Exemplo n.º 1
0
    public static function localize($form_id)
    {
        $capability = apply_filters('ninja_forms_display_test_values_capabilities', 'read');
        if (isset($_GET['ninja_forms_test_values']) && current_user_can($capability)) {
            self::$use_test_values = TRUE;
        }
        if (!has_action('wp_footer', 'NF_Display_Render::output_templates', 9999)) {
            add_action('wp_footer', 'NF_Display_Render::output_templates', 9999);
        }
        $form = Ninja_Forms()->form($form_id)->get();
        if ($form->get_setting('logged_in') && !is_user_logged_in()) {
            echo $form->get_setting('not_logged_in_msg');
            return;
        }
        if ($form->get_setting('sub_limit_number')) {
            $subs = Ninja_Forms()->form($form_id)->get_subs();
            // TODO: Optimize Query
            global $wpdb;
            $count = 0;
            $subs = $wpdb->get_results("SELECT post_id FROM wp_postmeta WHERE `meta_key` = '_form_id' AND `meta_value` = {$form_id}");
            foreach ($subs as $sub) {
                if ('publish' == get_post_status($sub->post_id)) {
                    $count++;
                }
            }
            if ($count >= $form->get_setting('sub_limit_number')) {
                echo apply_filters('nf_sub_limit_reached_msg', $form->get_setting('sub_limit_msg'), $form_id);
                return;
            }
        }
        $currency = $form->get_setting('currency', Ninja_Forms()->get_setting('currency'));
        $currency_symbol = Ninja_Forms::config('CurrencySymbol');
        $form->update_setting('currency_symbol', isset($currency_symbol[$currency]) ? $currency_symbol[$currency] : '');
        $title = apply_filters('ninja_forms_form_title', $form->get_setting('title'), $form_id);
        $form->update_setting('title', $title);
        $before_form = apply_filters('ninja_forms_display_before_form', '', $form_id);
        $form->update_setting('beforeForm', $before_form);
        $before_fields = apply_filters('ninja_forms_display_before_fields', '', $form_id);
        $form->update_setting('beforeFields', $before_fields);
        $after_fields = apply_filters('ninja_forms_display_after_fields', '', $form_id);
        $form->update_setting('afterFields', $after_fields);
        $after_form = apply_filters('ninja_forms_display_after_form', '', $form_id);
        $form->update_setting('afterForm', $after_form);
        $form_fields = Ninja_Forms()->form($form_id)->get_fields();
        $fields = array();
        if (empty($form_fields)) {
            echo __('No Fields Found.', 'ninja-forms');
        } else {
            // TODO: Replace unique field key checks with a refactored model/factory.
            $unique_field_keys = array();
            $form_cache = get_option('nf_form_' . $form_id, false);
            $cache_updated = false;
            foreach ($form_fields as $field) {
                $field_id = $field->get_id();
                /*
                 * Duplicate field check.
                 * TODO: Replace unique field key checks with a refactored model/factory.
                 */
                $field_key = $field->get_setting('key');
                if (in_array($field_key, $unique_field_keys) || '' == $field_key) {
                    // Delete the field.
                    Ninja_Forms()->request('delete-field')->data(array('field_id' => $field_id))->dispatch();
                    // Remove the field from cache.
                    if ($form_cache) {
                        if (isset($form_cache['fields'])) {
                            foreach ($form_cache['fields'] as $cached_field_key => $cached_field) {
                                if (!isset($cached_field['id'])) {
                                    continue;
                                }
                                if ($field_id != $cached_field['id']) {
                                    continue;
                                }
                                // Flag cache to update.
                                $cache_updated = true;
                                unset($form_cache['fields'][$cached_field_key]);
                                // Remove the field.
                            }
                        }
                    }
                    continue;
                    // Skip the duplicate field.
                }
                array_push($unique_field_keys, $field_key);
                // Log unique key.
                /* END Duplicate field check. */
                $field_type = $field->get_settings('type');
                if (!isset(Ninja_Forms()->fields[$field_type])) {
                    $field = NF_Fields_Unknown::create($field);
                    $field_type = $field->get_setting('type');
                }
                $field = apply_filters('ninja_forms_localize_fields', $field);
                $field = apply_filters('ninja_forms_localize_field_' . $field_type, $field);
                $field_class = Ninja_Forms()->fields[$field_type];
                if (self::$use_test_values) {
                    $field->update_setting('value', $field_class->get_test_value());
                }
                $field->update_setting('id', $field->get_id());
                /*
                 * TODO: For backwards compatibility, run the original action, get contents from the output buffer, and return the contents through the filter. Also display a PHP Notice for a deprecate filter.
                 */
                $display_before = apply_filters('ninja_forms_display_before_field_type_' . $field->get_setting('type'), '');
                $display_before = apply_filters('ninja_forms_display_before_field_key_' . $field->get_setting('key'), $display_before);
                $field->update_setting('beforeField', $display_before);
                $display_after = apply_filters('ninja_forms_display_after_field_type_' . $field->get_setting('type'), '');
                $display_after = apply_filters('ninja_forms_display_after_field_key_' . $field->get_setting('key'), $display_after);
                $field->update_setting('afterField', $display_after);
                $templates = $field_class->get_templates();
                if (!array($templates)) {
                    $templates = array($templates);
                }
                foreach ($templates as $template) {
                    self::load_template('fields-' . $template);
                }
                $settings = $field->get_settings();
                foreach ($settings as $key => $setting) {
                    if (is_numeric($setting)) {
                        $settings[$key] = floatval($setting);
                    }
                }
                if (!isset($settings['label_pos']) || 'default' == $settings['label_pos']) {
                    $settings['label_pos'] = $form->get_setting('default_label_pos');
                }
                $settings['parentType'] = $field_class->get_parent_type();
                if ('list' == $settings['parentType'] && isset($settings['options']) && is_array($settings['options'])) {
                    $settings['options'] = apply_filters('ninja_forms_render_options', $settings['options'], $settings);
                    $settings['options'] = apply_filters('ninja_forms_render_options_' . $field_type, $settings['options'], $settings);
                }
                if (isset($settings['default'])) {
                    $default_value = apply_filters('ninja_forms_render_default_value', $settings['default'], $field_type, $settings);
                    $default_value = preg_replace('/{.*}/', '', $default_value);
                    if ($default_value) {
                        $settings['value'] = $default_value;
                        ob_start();
                        do_shortcode($settings['value']);
                        $ob = ob_get_clean();
                        if (!$ob) {
                            $settings['value'] = do_shortcode($settings['value']);
                        }
                    }
                }
                // TODO: Find a better way to do this.
                if ('shipping' == $settings['type']) {
                    $settings['shipping_cost'] = preg_replace('/[^\\d,\\.]/', '', $settings['shipping_cost']);
                    $settings['shipping_cost'] = str_replace(Ninja_Forms()->get_setting('currency_symbol'), '', $settings['shipping_cost']);
                    $settings['shipping_cost'] = number_format($settings['shipping_cost'], 2);
                } elseif ('product' == $settings['type']) {
                    $settings['product_price'] = preg_replace('/[^\\d,\\.]/', '', $settings['product_price']);
                    $settings['product_price'] = str_replace(Ninja_Forms()->get_setting('currency_symbol'), '', $settings['product_price']);
                    $settings['product_price'] = number_format($settings['product_price'], 2);
                } elseif ('total' == $settings['type'] && isset($settings['value'])) {
                    $settings['value'] = number_format($settings['value'], 2);
                }
                $settings['element_templates'] = $templates;
                $settings['old_classname'] = $field_class->get_old_classname();
                $settings['wrap_template'] = $field_class->get_wrap_template();
                $fields[] = apply_filters('ninja_forms_localize_field_settings_' . $field_type, $settings, $form);
            }
            if ($cache_updated) {
                update_option('nf_form_' . $form_id, $form_cache);
                // Update form cache without duplicate fields.
            }
        }
        $fields = apply_filters('ninja_forms_display_fields', $fields);
        // Output Form Container
        do_action('ninja_forms_before_container', $form_id, $form->get_settings(), $form_fields);
        Ninja_Forms::template('display-form-container.html.php', compact('form_id'));
        ?>
        <!-- TODO: Move to Template File. -->
        <script>
            var formDisplay = 1;

            // Maybe initialize nfForms object
            var nfForms = nfForms || [];

            // Build Form Data
            var form = [];
            form.id = '<?php 
        echo $form_id;
        ?>
';
            form.settings = <?php 
        echo wp_json_encode($form->get_settings());
        ?>
;

            form.fields = <?php 
        echo wp_json_encode($fields);
        ?>
;

            // Add Form Data to nfForms object
            nfForms.push( form );
        </script>

        <?php 
        self::enqueue_scripts($form_id);
    }
Exemplo n.º 2
0
    private function _localize_form_data($form_id)
    {
        $form = Ninja_Forms()->form($form_id)->get();
        if (!$form->get_tmp_id()) {
            $fields = $form_id ? Ninja_Forms()->form($form_id)->get_fields() : array();
            $actions = $form_id ? Ninja_Forms()->form($form_id)->get_actions() : array();
        } else {
            $fields = array();
            $actions = array();
        }
        $fields_settings = array();
        // echo "<pre>";
        // print_r( $fields );
        // echo "</pre>";
        // die();
        if (!empty($fields)) {
            // TODO: Replace unique field key checks with a refactored model/factory.
            $unique_field_keys = array();
            $form_cache = get_option('nf_form_' . $form_id, false);
            $cache_updated = false;
            foreach ($fields as $field) {
                $field_id = $field->get_id();
                /*
                 * Duplicate field check.
                 * TODO: Replace unique field key checks with a refactored model/factory.
                 */
                $field_key = $field->get_setting('key');
                if (in_array($field_key, $unique_field_keys) || '' == $field_key) {
                    // Delete the field.
                    Ninja_Forms()->request('delete-field')->data(array('field_id' => $field_id))->dispatch();
                    // Remove the field from cache.
                    if ($form_cache) {
                        if (isset($form_cache['fields'])) {
                            foreach ($form_cache['fields'] as $cached_field_key => $cached_field) {
                                if (!isset($cached_field['id'])) {
                                    continue;
                                }
                                if ($field_id != $cached_field['id']) {
                                    continue;
                                }
                                // Flag cache to update.
                                $cache_updated = true;
                                unset($form_cache['fields'][$cached_field_key]);
                                // Remove the field.
                            }
                        }
                    }
                    continue;
                    // Skip the duplicate field.
                }
                array_push($unique_field_keys, $field_key);
                // Log unique key.
                /* END Duplicate field check. */
                $type = $field->get_setting('type');
                if (!isset(Ninja_Forms()->fields[$type])) {
                    $field = NF_Fields_Unknown::create($field);
                }
                $settings = $field->get_settings();
                $settings['id'] = $field_id;
                foreach ($settings as $key => $setting) {
                    if (is_numeric($setting)) {
                        $settings[$key] = floatval($setting);
                    }
                }
                $fields_settings[] = $settings;
            }
            if ($cache_updated) {
                update_option('nf_form_' . $form_id, $form_cache);
                // Update form cache without duplicate fields.
            }
        }
        $actions_settings = array();
        if (!empty($actions)) {
            foreach ($actions as $action) {
                $type = $action->get_setting('type');
                if (!isset(Ninja_Forms()->actions[$type])) {
                    continue;
                }
                $settings = $action->get_settings();
                $settings['id'] = $action->get_id();
                $actions_settings[] = $settings;
            }
        }
        if ($form->get_tmp_id()) {
            $actions_settings = Ninja_Forms()->config('FormActionDefaults');
        }
        $form_data = array();
        $form_data['id'] = $form_id;
        // Use form cache for form settings.
        // TODO: Defer to refactor of factory/model.
        if (isset($form_cache['settings'])) {
            $form_data['settings'] = $form_cache['settings'];
        } else {
            $form_data['settings'] = $form->get_settings();
        }
        $form_data['fields'] = $fields_settings;
        $form_data['actions'] = $actions_settings;
        ?>
        <script>
            var preloadedFormData = <?php 
        echo wp_json_encode($form_data);
        ?>
;
            // console.log( preloadedFormData );
        </script>
        <?php 
    }