如何循环设置插件选项字段?

时间:2014-02-24 作者:penguin429

问题/背景目前,我正在为客户端创建一个插件,使其能够标记和修改仪表板中预先确定的Google地图多边形(显示在站点上)。

我想让用户将多边形标记为重要多边形,设置自定义颜色、不透明度和信息窗口文本,所有这些我都已经能够做到。问题是,所有180个ish多边形都需要存在这些场集。使用WP设置API,我可以创建和保存字段,但创建500多个回调来覆盖每个特定字段将是荒谬的。所以在过去的几天里,我一直在尝试循环特定次数的字段集,并附加一个变量。

当前代码

这是一组多边形(存储在类中)的当前选项字段注册:

<?php
public function page_init() {
    //[. . . ]
    add_settings_section(
        \'rangers_txs\',
        \'Texas Senate District Options\',
        array($this, \'print_txs_info\'),
        \'rangers_txs_option\'
    );
    register_setting(
        \'rangers_txs\',
        \'rangers_txs_option\',
        array($this, \'ranger_txs_sanitize\')
    );

    $i=1;
    while($i <= 31) {
        add_settings_field(
            \'rangers_txs_checkbox_\'.$i, //id
            \'Is District \'.$i.\' important?\', //Title
            array( $this, \'rangers_txs_check_callback_\'.$i), //callback
            \'rangers_txs_option\', //page
            \'rangers_txs\', //section
            array(\'field\' => \'txs_checkbox\')
        );
        add_settings_field(
            \'rangers_txs_colorpicker_\'.$i, //id
            \'District Color\', //Title
            array( $this, \'rangers_txs_color_callback_\'.$i ), //callback
            \'rangers_txs_option\', //page
            \'rangers_txs\', //section
            array(\'field\' => \'txs_colorpicker\')
        );
        add_settings_field(
            \'rangers_txs_opacity_\'.$i, //id
            \'How important is it?\', //Title
            array( $this, \'rangers_txs_opacity_callback_\'.$i ), //callback
            //array($this, \'rangers_txs_input_array\'),
            //\'rangers_txs_opacity_callback_\'.$i, //callback
            \'rangers_txs_option\', //page
            \'rangers_txs\', //section
            array(\'field\' => \'txs_opacity\')
        );
        add_settings_field(
            \'rangers_txs_info_\'.$i, //id
            \'District Information\', //Title
            array( $this, \'rangers_txs_info_callback_\'.$i ), //callback
            \'rangers_txs_option\', //page
            \'rangers_txs\', //section
            array(\'field\' => \'txs_info\')
        );

        $i++;
    }

}
?>
这里有一组回调,我已经测试过并且工作正常:

<?php
public function colorpicker_init() {
    echo \'<script type="text/javascript">
            jQuery(document).ready(function($) {
              $(".tr-color-picker").wpColorPicker();
            });
          </script>\';
}
public function rangers_txs_check_callback_1() {
    printf(
        \'<input type="checkbox" id="rangers_txs_checkbox_1" name="rangers_txs_option[rangers_txs_checkbox_1]" %1$s />
        <label for="rangers_txs_option[rangers_txs_checkbox_1]">Yep, it\\\'s important.\', 
        checked( isset($this->txsoptions[\'rangers_txs_checkbox_1\']), true, false)
    );
}
public function rangers_txs_color_callback_1() {
    $this->colorpicker_init();

    $color = $this->txsoptions[\'rangers_txs_colorpicker_1\'] != \'\' ? sanitize_text_field($this->txsoptions[\'rangers_txs_colorpicker_1\']) : \'#0A64A4\';
    printf(
        \'<input type="text" name="rangers_txs_option[%1$s]" id="%1$s" class="tr-color-picker" data-default-color="#0A64A4" value="\'.$color.\'" />\',
        \'rangers_txs_colorpicker_1\'
    );
}
public function rangers_txs_opacity_callback_1() {
    print \'<p><em>On a scale of 1-10 (determines opacity of district, default: 5).</em></p>\';
    printf(
        \'<input type="text" id="rangers_txs_opacity_1" name="rangers_txs_option[rangers_txs_opacity_1]" value="%s" />\', 
        isset( $this->txsoptions[\'rangers_txs_opacity_1\'] ) ? esc_attr( $this->txsoptions[\'rangers_txs_opacity_1\']) : \'\' 
    );
}
public function rangers_txs_info_callback_1() {
    isset($this->txsoptions[\'rangers_txs_info_1\']) ? $content = $this->txsoptions[\'rangers_txs_info_1\'] : $content = \'\';

    echo \'<textarea id="rangers_txs_info_1" name="rangers_txs_option[rangers_txs_info_1]" rows="6" cols="50">\'.$content.\'</textarea>\';
}
?>
如您所见,我有一个复选框、一个颜色选择器、一个不透明度输入和一个文本区域。我希望为每个多边形循环整个集合。我试图在一个回调中运行这些字段,但它只是将所有相同的选项集中在一起(即,31个复选框,然后是31个颜色选择器,等等)。我还尝试在类外设置回调并循环while 围绕函数集,但使用$i 函数名中的变量返回了错误,在调用函数之前和调用函数时追加该变量也返回了错误。

如果有任何建议可以帮助我实现字段集循环的结果,我将不胜感激。

1 个回复
最合适的回答,由SO网友:penguin429 整理而成

我在做了一点工作后就明白了。

首先,我使用了一个回调:array($this, \'txs_loop_callbacks\') 使用相同的add_settings_field 以上注册。然后,我修改了字段重新注册中的最后一个参数,使每个参数对该特定字段都是唯一的:array(\'field\' => \'txs_opacity_\'.$i).

然后在循环回调中,我将每个字段的HTML存储在一个定义为变量的匿名函数中。像这样:

$txs_checkbox = function($num) {
    $is_checked = checked( isset($this->txsoptions[\'rangers_txs_checkbox_\'.$num]), true, false);
    printf(
       \'<input type="checkbox" class="is-important-\'.$num.\'" id="rangers_txs_checkbox_\'.$i.\'" name="rangers_txs_option[rangers_txs_checkbox_\'.$num.\']" %1$s />
        <label for="rangers_txs_option[rangers_txs_checkbox_\'.$num.\']">District \'.$num.\' is important</label><br /><br />\', 
        $is_checked
    );
};
我为每个字段类型定义了这种类型的变量(即。,$txs_checkbox, $txs_colorpicker, 和上)。

之后,我调用了循环中的匿名函数,并进行了检查,以在每个集合中只显示一种字段。

$n=1;
while($n <= 31) {
    switch($args[\'field\']):
        case(\'txs_checkbox_\'.$n):
            $txs_checkbox($n);
            break;

        case(\'txs_colorpicker_\'.$n):
            $txs_colorpicker($n);
            break;

        case(\'txs_opacity_\'.$n):
            $txs_opacity($n);
            break;

        case(\'txs_info_\'.$n):
            $txs_info($n);
            break;
    endswitch;

    $n++;
}
将这些元素结合在一起,我就能够获得重复字段集的结果。

结束