function wpuf_custom_fields_edit()
{
    global $wpdb, $custom_fields;
    $id = intval($_GET['id']);
    ?>

    <div class="wrap">

        <?php 
    //update the fields
    if (isset($_POST['wpuf_edit_custom'])) {
        check_admin_referer('wpuf_edit', 'wpuf_edit');
        $error = false;
        if ($_POST['field'] == '') {
            $error = 'Please enter field name';
        } else {
            if ($_POST['label'] == '') {
                $error = 'Please enter label name';
            }
        }
        if (!$error) {
            //no errors
            //whatever, insert the values
            if (!wpuf_starts_with($_POST['field'], 'cf_')) {
                $_POST['field'] = 'cf_' . $_POST['field'];
            }
            $data = array('field' => $_POST['field'], 'label' => $_POST['label'], 'desc' => $_POST['help'], 'required' => $_POST['required'], 'region' => $_POST['region'], 'order' => $_POST['order'], 'type' => $_POST['type'], 'values' => $_POST['field_values']);
            //var_dump($data);
            $result = $wpdb->update($wpdb->prefix . 'wpuf_customfields', $data, array('id' => $id), array('%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s'), array('%d'));
            //if row inserted
            if ($result) {
                echo '<div class="updated"><p><strong>Field Updated</strong></p></div>';
            } else {
                echo "<div class='error'><p><strong>Something went wrong or you didn't changed anything</strong></p></div>";
            }
        } else {
            //we got some error
            echo '<div class="error"><p><strong>' . $error . '</strong></p></div>';
        }
    }
    //finished updating
    //now show it
    $row = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}wpuf_customfields WHERE `id`={$id}", OBJECT);
    $values = array("field" => "{$row->field}", "label" => "{$row->label}", "help" => "{$row->desc}", "required" => "{$row->required}", "region" => "{$row->region}", "order" => "{$row->order}", "type" => "{$row->type}");
    ?>


        <?php 
    if ($row) {
        ?>
            <form action="" method="post" style="margin-top: 20px;">
                <?php 
        wp_nonce_field('wpuf_edit', 'wpuf_edit');
        ?>
                <table class="widefat meta" style="width: 850px">
                    <thead>
                        <tr>
                            <th scope="col" colspan="2" style="font-size: 14px;">Edit Custom Field</th>
                        </tr>
                    </thead>

                    <?php 
        wpuf_build_form($custom_fields, $values, false);
        ?>

                    <tr valign="top" id="wpuf_field_values_row" style="display: none;">
                        <td scope="row" class="label"><label for="wpuf_field_values">Values</label></td>
                        <td>
                            <textarea name="field_values" id="wpuf_field_values" cols="30"><?php 
        echo $row->values;
        ?>
</textarea>
                            <span class="description"><br>This will be used as option fields. Please separate values with comma</span>
                        </td>
                    </tr>
                </table>

                <input name="wpuf_edit_custom" type="submit" class="button-primary" value="<?php 
        _e('Update Field');
        ?>
" style="margin-top: 10px;" />

            </form>
        <?php 
    } else {
        ?>
            <h2>Nothing found</h2>
        <?php 
    }
    ?>

    </div>

    <?php 
}
function wpuf_custom_fields_edit()
{
    global $wpdb, $custom_fields;
    $id = intval($_GET['id']);
    ?>

    <div class="wrap wpuf-admin">

        <?php 
    //update the fields
    if (isset($_POST['wpuf_edit_custom'])) {
        check_admin_referer('wpuf_edit', 'wpuf_edit');
        $error = false;
        if ($_POST['field'] == '') {
            $error = 'Please enter field name';
        } else {
            if ($_POST['label'] == '') {
                $error = 'Please enter label name';
            }
        }
        if (!$error) {
            //no errors
            //whatever, insert the values
            if (!wpuf_starts_with($_POST['field'], 'cf_')) {
                $_POST['field'] = 'cf_' . $_POST['field'];
            }
            $data = array('field' => $_POST['field'], 'label' => $_POST['label'], 'desc' => $_POST['help'], 'required' => $_POST['required'], 'region' => $_POST['region'], 'order' => $_POST['order'], 'type' => $_POST['type'], 'values' => $_POST['field_values']);
            //var_dump($data);
            $result = $wpdb->update($wpdb->prefix . 'wpuf_customfields', $data, array('id' => $id), array('%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s'), array('%d'));
            //if row inserted
            if ($result) {
                echo '<div class="updated"><p><strong>Field Updated</strong></p></div>';
            } else {
                echo "<div class='error'><p><strong>Something went wrong or you didn't changed anything</strong></p></div>";
            }
        } else {
            //we got some error
            echo '<div class="error"><p><strong>' . $error . '</strong></p></div>';
        }
    }
    //finished updating
    //now show it
    $row = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}wpuf_customfields WHERE `id`={$id}");
    //var_dump( $row );
    ?>


        <?php 
    if ($row) {
        ?>
            <form action="" method="post" style="margin-top: 20px;">
                <?php 
        wp_nonce_field('wpuf_edit', 'wpuf_edit');
        ?>
                <table class="widefat meta" style="width: 850px">
                    <thead>
                        <tr>
                            <th scope="col" colspan="2" style="font-size: 14px;">Edit Custom Field</th>
                        </tr>
                    </thead>

                    <tbody>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="field"><?php 
        _e('Field Name', 'wpuf');
        ?>
</label></td>
                            <td>
                                <input type="text" size="25" style="" id="field" value="<?php 
        echo esc_attr($row->field);
        ?>
" name="field" />
                                <span class="description"><?php 
        _e('Name without space. Will be used to store the value in this custom field', 'wpuf');
        ?>
</span>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="label"><?php 
        _e('Label', 'wpuf');
        ?>
</label></td>
                            <td>
                                <input type="text" size="25" style="" id="label" value="<?php 
        echo esc_attr($row->label);
        ?>
" name="label" />
                                <span class="description"><?php 
        _e('This will be used as your input fields title', 'wpuf');
        ?>
</span>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="help"><?php 
        _e('Help Text', 'wpuf');
        ?>
</label></td>
                            <td>
                                <input type="text" size="25" style="" id="help" value="<?php 
        echo esc_attr($row->desc);
        ?>
" name="help" />
                                <span class="description"><?php 
        _e('Text will be shown to user as help text', 'wpuf');
        ?>
</span>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="required"><?php 
        _e('Required', 'wpuf');
        ?>
</label></td>
                            <td>
                                <select id="required" name="required">
                                    <option value="no"<?php 
        selected($row->required, 'no');
        ?>
><?php 
        _e('No', 'wpuf');
        ?>
</option>
                                    <option value="yes"<?php 
        selected($row->required, 'yes');
        ?>
><?php 
        _e('Yes', 'wpuf');
        ?>
</option>
                                </select>
                                <span class="description"><?php 
        _e('A validation criteria. User must provide input in that field', 'wpuf');
        ?>
</span>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="region"><?php 
        _e('Region', 'wpuf');
        ?>
</label></td>
                            <td>
                                <select id="region" name="region">
                                    <option value="top"<?php 
        selected($row->region, 'top');
        ?>
><?php 
        _e('Top', 'wpuf');
        ?>
</option>
                                    <option value="description"<?php 
        selected($row->region, 'description');
        ?>
><?php 
        _e('Before Description', 'wpuf');
        ?>
</option>
                                    <option value="tag"<?php 
        selected($row->region, 'tag');
        ?>
><?php 
        _e('After Description', 'wpuf');
        ?>
</option>
                                    <option value="bottom"<?php 
        selected($row->region, 'bottom');
        ?>
><?php 
        _e('Bottom', 'wpuf');
        ?>
</option>
                                </select>
                                <span class="description"><?php 
        _e('Where do you want to show this input field?', 'wpuf');
        ?>
</span>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="order"><?php 
        _e('Order', 'wpuf');
        ?>
</label></td>
                            <td>
                                <input name="order" value="<?php 
        echo esc_attr($row->order);
        ?>
" id="order" style="" size="2" type="text">
                                <span class="description"><?php 
        _e('Which order this input field will show in a region', 'wpuf');
        ?>
</span>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="type"><?php 
        _e('Type', 'wpuf');
        ?>
</label></td>
                            <td>
                                <select name="type" id="type" onchange="wpuf_show(this)">
                                    <option value="text"<?php 
        selected($row->type, 'text');
        ?>
><?php 
        _e('Text Box', 'wpuf');
        ?>
</option>
                                    <option value="textarea"<?php 
        selected($row->type, 'textarea');
        ?>
><?php 
        _e('Text Area', 'wpuf');
        ?>
</option>
                                    <option value="select"<?php 
        selected($row->type, 'select');
        ?>
><?php 
        _e('Dropdown', 'wpuf');
        ?>
</option>
                                </select>
                                <span class="description"></span>
                            </td>
                        </tr>
                        <tr valign="top" id="wpuf_field_values_row" style="display: none;">
                            <td scope="row" class="label"><label for="wpuf_field_values"><?php 
        _e('Values', 'wpuf');
        ?>
</label></td>
                            <td>
                                <textarea name="field_values" id="wpuf_field_values" cols="30"><?php 
        echo esc_textarea($row->values);
        ?>
</textarea>
                                <span class="description"><br><?php 
        _e('This will be used as option fields. Please separate values with comma', 'wpuf');
        ?>
</span>
                            </td>
                        </tr>
                    </tbody>
                </table>

                <input name="wpuf_edit_custom" type="submit" class="button-primary" value="<?php 
        _e('Update Field', 'wpuf');
        ?>
" style="margin-top: 10px;" />

            </form>
        <?php 
    } else {
        ?>
            <h2><?php 
        _e('Nothing found', 'wpuf');
        ?>
</h2>
        <?php 
    }
    ?>

    </div>

    <?php 
}
Example #3
0
/**
 * Handles admin options settings submission with ajax
 *
 */
function wpuf_admin_ajax()
{
    foreach ($_POST as $key => $val) {
        $_POST[$key] = esc_attr($val);
    }
    foreach ($_POST as $key => $value) {
        //update the input fields, whose names starts with symple_
        if (wpuf_starts_with($key, 'wpuf_')) {
            //echo "$key => $value <br>";
            update_option($key, wpuf_clean_tags($value));
            //echo "$key => $value \n";
        }
        //starts with
    }
    //foreach
    echo __('Settings Saved', 'wpuf');
    //print_r($_POST);
    exit;
}
/**
 * Build custom field form for add posting form
 *
 * @global type $wpdb
 * @param type $position
 */
function wpuf_build_custom_field_form($position = 'top', $edit = false, $post_id = 0)
{
    global $wpdb;
    //check, if custom field is enabled
    $enabled = get_option('wpuf_enable_custom_field');
    //var_dump( $enabled );
    if ($enabled == 'no') {
        return false;
    }
    $table = $wpdb->prefix . 'wpuf_customfields';
    $results = $wpdb->get_results("SELECT * FROM {$table} WHERE `region`='{$position}' ORDER BY `order`", OBJECT);
    if (is_array($results)) {
        foreach ($results as $field) {
            if (wpuf_starts_with($field->field, 'cf_')) {
                if ($edit && $post_id) {
                    $value = get_post_meta($post_id, $field->field, true);
                } else {
                    $value = '';
                }
                switch ($field->type) {
                    case 'text':
                        ?>
                            <li>
                                <label for="<?php 
                        echo $field->field;
                        ?>
">
                                    <?php 
                        echo stripslashes($field->label);
                        ?>
                                    <?php 
                        if ($field->required == 'yes') {
                            ?>
                                        <span class="required">*</span>
                                    <?php 
                        }
                        ?>
                                </label>
                                <?php 
                        $class = $field->required == 'yes' ? 'requiredField' : '';
                        ?>
                                <input class="<?php 
                        echo $class;
                        ?>
" type="text" name="<?php 
                        echo $field->field;
                        ?>
" id="<?php 
                        echo $field->field;
                        ?>
" minlength="2" value="<?php 
                        echo stripslashes($value);
                        ?>
">
                                <div class="clear"></div>

                                <?php 
                        if ($field->desc) {
                            ?>
                                    <p class="description"><?php 
                            echo stripslashes($field->desc);
                            ?>
</p>
                                    <div class="clear"></div>
                                <?php 
                        }
                        ?>

                            </li>

                            <?php 
                        break;
                    case 'textarea':
                        ?>
                            <li>
                                <label for="<?php 
                        echo $field->field;
                        ?>
">
                                    <?php 
                        echo stripslashes($field->label);
                        ?>
                                    <?php 
                        if ($field->required == 'yes') {
                            ?>
                                        <span class="required">*</span>
                                    <?php 
                        }
                        ?>
                                </label>
                                <?php 
                        $class = $field->required == 'yes' ? 'requiredField' : '';
                        ?>
                                <textarea class="<?php 
                        echo $class;
                        ?>
" name="<?php 
                        echo $field->field;
                        ?>
" id="<?php 
                        echo $field->field;
                        ?>
"><?php 
                        echo stripslashes($value);
                        ?>
</textarea>
                                <div class="clear"></div>

                                <?php 
                        if ($field->desc) {
                            ?>
                                    <p class="description"><?php 
                            echo stripslashes($field->desc);
                            ?>
</p>
                                    <div class="clear"></div>
                                <?php 
                        }
                        ?>

                            </li>

                            <?php 
                        break;
                    case 'select':
                        ?>
                            <li>
                                <label for="<?php 
                        echo $field->field;
                        ?>
">
                                    <?php 
                        echo stripslashes($field->label);
                        ?>
                                    <?php 
                        if ($field->required == 'yes') {
                            ?>
                                        <span class="required">*</span>
                                    <?php 
                        }
                        ?>
                                </label>
                                <select name="<?php 
                        echo $field->field;
                        ?>
">
                                    <?php 
                        $options = explode(',', $field->values);
                        if (is_array($options)) {
                            foreach ($options as $opt) {
                                $opt = trim(strip_tags($opt));
                                echo "<option value='{$opt}' " . selected($value, $opt, false) . ">{$opt}</option>";
                            }
                        }
                        ?>
                                </select>
                                <div class="clear"></div>

                                <?php 
                        if ($field->desc) {
                            ?>
                                    <p class="description"><?php 
                            echo stripslashes($field->desc);
                            ?>
</p>
                                    <div class="clear"></div>
                                <?php 
                        }
                        ?>

                            </li>

                            <?php 
                        break;
                    default:
                }
                //switch
            } else {
                switch ($field->type) {
                    case 'text':
                        ?>
                            <li>
                                <label for="<?php 
                        echo $field->field;
                        ?>
">
                                    <?php 
                        echo stripslashes($field->label);
                        ?>
                                    <?php 
                        if ($field->required == 'yes') {
                            ?>
                                        <span class="required">*</span>
                                    <?php 
                        }
                        ?>
                                </label>
                                <?php 
                        $class = $field->required == 'yes' ? 'requiredField' : '';
                        ?>
                                <input class="<?php 
                        echo $class;
                        ?>
" type="text" name="<?php 
                        echo $field->field;
                        ?>
" id="<?php 
                        echo $field->field;
                        ?>
" minlength="2" value="<?php 
                        echo stripslashes($value);
                        ?>
">
                                <div class="clear"></div>

                                <?php 
                        if ($field->desc) {
                            ?>
                                    <p class="description"><?php 
                            echo stripslashes($field->desc);
                            ?>
</p>
                                    <div class="clear"></div>
                                <?php 
                        }
                        ?>

                            </li>

                            <?php 
                        break;
                    case 'select':
                        $fld = substr($field->field, 3);
                        $terms = get_terms($fld);
                        //var_dump( $fld );
                        if ($terms) {
                            foreach ($terms as $t) {
                                $term_option .= '<option  value="' . $t->term_id . '">' . $t->name . '</option>';
                            }
                        }
                        ?>
                            <li>
                                <label for="<?php 
                        echo $field->field;
                        ?>
">
                                    <?php 
                        echo stripslashes($field->label);
                        ?>
                                    <?php 
                        if ($field->required == 'yes') {
                            ?>
                                        <span class="required">*</span>
                                    <?php 
                        }
                        ?>
                                </label>
                                <select name="<?php 
                        echo $field->field;
                        ?>
">
                                    <?php 
                        echo $term_option;
                        ?>
                                </select>
                                <div class="clear"></div>

                                <?php 
                        if ($field->desc) {
                            ?>
                                    <p class="description"><?php 
                            echo stripslashes($field->desc);
                            ?>
</p>
                                    <div class="clear"></div>
                                <?php 
                        }
                        ?>

                            </li>

                        <?php 
                    default:
                }
            }
        }
        //foreach
    }
    // is_array
}