php 重力Wiz //重力形式//时间敏感选择

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 重力Wiz //重力形式//时间敏感选择相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Gravity Wiz // Gravity Forms // Time Sensitive Choices
 *
 * Provide a drop down of times and automatically filter which choices are available based on the current time.
 *
 * @version	  0.1
 * @author    David Smith <david@gravitywiz.com>
 * @license   GPL-2.0+
 * @link      http://gravitywiz.com/...
 * @copyright 2015 Gravity Wiz
 *
 * # WordPress Plugin Header
 *
 * Plugin Name: Gravity Forms Time Sensitive Choices
 * Plugin URI: http://ounceoftalent.com
 * Description: Provide a drop down of times and automatically filter which choices are avialable based on the current time.
 * Author: David Smith
 * Version: 0.1
 * Author URI: http://ounceoftalent.com
 *
 */
class GW_Time_Sensitive_Choices {

    public function __construct( $args = array() ) {

        // set our default arguments, parse against the provided arguments, and store for use throughout the class
        $this->_args = wp_parse_args( $args, array(
            'form_id'   => false,
            'field_ids' => array(),
            'time_mod'  => false
        ) );

        // do version check in the init to make sure if GF is going to be loaded, it is already loaded
        add_action( 'init', array( $this, 'init' ) );

    }

    public function init() {

        // make sure we're running the required minimum version of Gravity Forms
        if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
            return;
        }

        add_filter( 'gform_pre_render', array( $this, 'filter_form_by_time' ) );

    }

    public function filter_form_by_time( $form ) {

        if( ! $this->is_applicable_form( $form ) ) {
            return $form;
        }

        foreach( $form['fields'] as &$field ) {
            if( $this->is_applicable_field( $field ) ) {
                $field->choices = $this->filter_choices_by_time( $field->choices );
            }
        }

        return $form;
    }

    public function filter_choices_by_time( $choices ) {

        $filtered_choices = array();

        $current_time = current_time( 'timestamp' );
        $max_time     = strtotime( '23:59', $current_time );
        $time_cutoff  = strtotime( $this->_args['time_mod'], $current_time );

        if( $time_cutoff > $max_time ) {
            $time_cutoff = $max_time;
        }

        foreach( $choices as $choice ) {
            $time = strtotime( $choice['value'], $current_time );
            if( $time > $time_cutoff ) {
                $filtered_choices[] = $choice;
            }
        }

        return $filtered_choices;
    }

    public function is_applicable_form( $form ) {
        return $this->_args['form_id'] == $form['id'];
    }

    public function is_applicable_field( $field ) {
        return in_array( $field->id, $this->_args['field_ids'] );
    }

}

# Configuration

new GW_Time_Sensitive_Choices( array(
	'form_id' => 964,
	'field_ids' => array( 10, 12, 13 ),
	'time_mod' => '+1 hours'
) );

以上是关于php 重力Wiz //重力形式//时间敏感选择的主要内容,如果未能解决你的问题,请参考以下文章

php 重力Wiz //重力形式//签到

php 重力Wiz //重力形式//货币转换器

php 重力Wiz //重力形式//条件提交按钮:全部必需

php 重力Wiz //重力形式//修改自定义字段的日期格式

php 重力Wiz //重力形式//修改条目中的日期字段格式

php 重力Wiz //重力形式//通过[gravityforms]短代码自定义字段属性