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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
<?php
class WC_Braintree_Product_Gateway_Option extends WC_Settings_API {
use WC_Braintree_Settings_Trait;
public $settings = array();
private $product;
private $payment_method;
public function __construct( $product, $payment_method ) {
if ( ! is_object( $product ) ) {
$this->product = wc_get_product( $product );
} else {
$this->product = $product;
}
$this->payment_method = $payment_method;
$this->id = $this->payment_method->id;
$this->init_form_fields();
$this->init_settings();
}
public function get_id() {
return '_' . $this->payment_method->id . '_options';
}
public function get_field_key( $key ) {
return $this->plugin_id . 'product_' . $this->id . '_' . $key;
}
public function save() {
$this->product->update_meta_data( $this->get_id(), $this->settings );
$this->product->save();
}
public function init_settings() {
$this->settings = $this->product->get_meta( $this->get_id() );
if ( ! is_array( $this->settings ) ) {
$this->settings = $this->get_default_values();
}
if ( isset( $this->settings['enabled'] ) && is_bool( $this->settings['enabled'] ) ) {
$this->settings['enabled'] = wc_bool_to_string( $this->settings['enabled'] );
}
if ( is_callable( array( $this->payment_method, 'init_product_gateway_settings' ) ) ) {
$this->payment_method->init_product_gateway_settings( $this );
}
}
public function init_form_fields() {
$this->form_fields = $this->payment_method->get_product_admin_options();
}
public function get_default_values() {
$form_fields = $this->get_form_fields();
return array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) );
}
public function set_option( $key, $value ) {
$this->settings[ $key ] = $value;
}
public function enabled() {
return $this->is_active( 'enabled' );
}
public function get_payment_method() {
return $this->payment_method;
}
public function get_form_fields() {
return array_map( array( $this, 'set_defaults' ), $this->form_fields );
}
public function validate_checkbox_field( $key, $value ) {
return empty( $value ) || is_null( $value ) ? 'no' : 'yes';
}
}