-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathgplc-shared-choices.php
79 lines (61 loc) · 1.98 KB
/
gplc-shared-choices.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* Gravity Perks // Limit Choices // Shared Choices
*
* Share limits across choices in the same field.
*
* @version 1.0
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/documentation/gravity-forms-limit-choices/
*
* Plugin Name: GP Limit Choices — Shared Choices
* Plugin URI: http://gravitywiz.com/documentation/gravity-forms-limit-choices/
* Description: Share limits across choices in the same field.
* Author: Gravity Wiz
* Version: 1.0
* Author URI: http://gravitywiz.com
*/
class GPLC_Shared_Choices {
public $_args = array();
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_id' => 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() {
add_filter( 'gplc_choice_counts', array( $this, 'modify_gplc_choice_counts' ), 10, 3 );
}
public function is_applicable_field( $field ) {
$form_id = isset( $field->formId ) ? $field->formId : null;
return (int) $form_id === (int) $this->_args['form_id'] && isset( $field->id ) && (int) $field->id === (int) $this->_args['field_id'];
}
public function modify_gplc_choice_counts( $counts, $form_id, $field ) {
if ( ! $this->is_applicable_field( $field ) ) {
return $counts;
}
$shared_count = 0;
if ( is_array( $counts ) ) {
foreach ( $counts as $count ) {
$shared_count += $count;
}
}
if ( is_array( $field->choices ) ) {
foreach ( $field->choices as $choice ) {
if ( isset( $choice['limit'] ) && isset( $choice['value'] ) ) {
$counts[ $choice['value'] ] = $shared_count;
}
}
}
return $counts;
}
}
# Configuration
new GPLC_Shared_Choices( array(
'form_id' => 123,
'field_id' => 4,
) );