| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
/** |
| 5 |
* This class wraps all integrations with ACF such as custom location rules and fields |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* |
| 9 |
*/ |
| 10 |
class AF_ACF_Additions { |
| 11 |
|
| 12 |
function __construct() { |
| 13 |
|
| 14 |
add_action('acf/render_field_settings', array( $this, 'field_settings'), 100 ); |
| 15 |
add_action('acf/prepare_field', array( $this, 'hide_field_from_admin'), 100 ); |
| 16 |
|
| 17 |
add_filter( 'acf/location/rule_types', array( $this, 'add_form_location_type' ), 10, 1 ); |
| 18 |
add_filter( 'acf/location/rule_values/af_form', array( $this, 'form_location_rule_values' ), 10, 1 ); |
| 19 |
add_filter( 'acf/location/rule_match/af_form', array( $this, 'form_location_rule_match' ), 10, 3 ); |
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* Add extra global field settings |
| 26 |
* |
| 27 |
* @since 1.3.1 |
| 28 |
* |
| 29 |
*/ |
| 30 |
function field_settings( $field ) { |
| 31 |
|
| 32 |
acf_render_field_setting( $field, array( |
| 33 |
'label' => __('Hide from admin?'), |
| 34 |
'instructions' => 'Only show this field in forms', |
| 35 |
'name' => 'hide_admin', |
| 36 |
'type' => 'true_false', |
| 37 |
'ui' => 1, |
| 38 |
), true); |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* Hide fields with the "Hide from admin?" setting |
| 45 |
* |
| 46 |
* @since 1.3.1 |
| 47 |
* |
| 48 |
*/ |
| 49 |
function hide_field_from_admin( $field ) { |
| 50 |
|
| 51 |
if ( empty( $field['hide_admin'] ) ) { |
| 52 |
return $field; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
if ( $field['hide_admin'] && is_admin() ) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
return $field; |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Adds a new location rule type "Form" to ACF |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
* |
| 71 |
*/ |
| 72 |
function add_form_location_type( $choices ) { |
| 73 |
|
| 74 |
$choices['Advanced Forms']['af_form'] = 'Form'; |
| 75 |
|
| 76 |
return $choices; |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* Populates the choices for the location rule type "Form" |
| 83 |
* |
| 84 |
* @since 1.0.0 |
| 85 |
* |
| 86 |
*/ |
| 87 |
function form_location_rule_values( $choices ) { |
| 88 |
|
| 89 |
$forms = af_get_forms(); |
| 90 |
|
| 91 |
foreach ( $forms as $form ) { |
| 92 |
$choices[ $form['key'] ] = $form['title']; |
| 93 |
} |
| 94 |
|
| 95 |
return $choices; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
function form_location_rule_match( $match, $rule, $options ) { |
| 101 |
|
| 102 |
|
| 103 |
// Match with form object |
| 104 |
if ( 'af_form' == $rule['param'] && isset( $options['af_form'] ) ) { |
| 105 |
|
| 106 |
if( isset( $rule['value'] ) && $rule['value'] == $options['af_form'] ) { |
| 107 |
|
| 108 |
$match = true; |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
// Match with entry |
| 116 |
if ( isset( $options['post_type'] ) && 'af_entry' == $options['post_type'] ) { |
| 117 |
|
| 118 |
$entry_form = get_post_meta( $options['post_id'], 'entry_form', true ); |
| 119 |
|
| 120 |
if ( $entry_form && $entry_form == $rule['value'] ) { |
| 121 |
|
| 122 |
$match = true; |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
return $match; |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
return new AF_ACF_Additions(); |