| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Active_Campaign\Methods; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Actions\Methods\Abstract_Modifier; |
| 7 |
use Jet_Form_Builder\Actions\Methods\Base_Object_Property; |
| 8 |
use Jet_Form_Builder\Actions\Methods\Object_Dynamic_Property; |
| 9 |
use Jet_Form_Builder\Exceptions\Action_Exception; |
| 10 |
use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 11 |
use JFB_Modules\Active_Campaign\Api\Retrieve_Custom_Fields_Action; |
| 12 |
|
| 13 |
// If this file is called directly, abort. |
| 14 |
if ( ! defined( 'WPINC' ) ) { |
| 15 |
die; |
| 16 |
} |
| 17 |
|
| 18 |
class Custom_Contact_Property extends Base_Object_Property implements Object_Dynamic_Property { |
| 19 |
|
| 20 |
protected $action; |
| 21 |
|
| 22 |
public function get_label(): string { |
| 23 |
return ''; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_id(): string { |
| 27 |
return 'fieldValues'; |
| 28 |
} |
| 29 |
|
| 30 |
public function is_supported( string $key, $value ): bool { |
| 31 |
return true; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @param string $key |
| 36 |
* @param $value |
| 37 |
* @param Abstract_Modifier|Contact_Modifier $modifier |
| 38 |
* |
| 39 |
* @throws Action_Exception |
| 40 |
*/ |
| 41 |
public function do_before( string $key, $value, Abstract_Modifier $modifier ) { |
| 42 |
if ( ! is_array( $this->value ) ) { |
| 43 |
$this->value = array(); |
| 44 |
} |
| 45 |
|
| 46 |
if ( is_numeric( $key ) ) { |
| 47 |
$this->value[] = array( |
| 48 |
'field' => $key, |
| 49 |
'value' => $value, |
| 50 |
); |
| 51 |
|
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$key = $this->legacy_convert( $key ); |
| 56 |
$id = $this->get_field_id( $modifier, $key ); |
| 57 |
|
| 58 |
$this->value[] = array( |
| 59 |
'field' => $id, |
| 60 |
'value' => $value, |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Ex: 'field[%WEBSITE%,0]', where WEBSITE is correct field |
| 66 |
* |
| 67 |
* @param string $property |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
protected function legacy_convert( string $property ): string { |
| 72 |
if ( 0 !== strpos( $property, 'field[%' ) ) { |
| 73 |
return $property; |
| 74 |
} |
| 75 |
|
| 76 |
preg_match( |
| 77 |
'/^field\[%(\w+)%\,0\]$/i', |
| 78 |
$property, |
| 79 |
$matches |
| 80 |
); |
| 81 |
|
| 82 |
return $matches[1] ?? $property; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @param Contact_Modifier $modifier |
| 87 |
* @param string $field |
| 88 |
* |
| 89 |
* @return int |
| 90 |
* @throws Action_Exception |
| 91 |
*/ |
| 92 |
protected function get_field_id( Contact_Modifier $modifier, string $field ): int { |
| 93 |
$fields = $this->get_fields( $modifier ); |
| 94 |
|
| 95 |
return $fields->get_field_id( $field ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @param Contact_Modifier $modifier |
| 100 |
* |
| 101 |
* @return Retrieve_Custom_Fields_Action |
| 102 |
* @throws Action_Exception |
| 103 |
*/ |
| 104 |
protected function get_fields( Contact_Modifier $modifier ): Retrieve_Custom_Fields_Action { |
| 105 |
if ( is_a( $this->action, Retrieve_Custom_Fields_Action::class ) ) { |
| 106 |
return $this->action; |
| 107 |
} |
| 108 |
|
| 109 |
/** @var Retrieve_Custom_Fields_Action $fields */ |
| 110 |
$fields = ( new Retrieve_Custom_Fields_Action() ) |
| 111 |
->set_base( $modifier->url ) |
| 112 |
->set_token( $modifier->token ); |
| 113 |
|
| 114 |
try { |
| 115 |
$fields->request()->check_response_code()->response_body_as_array(); |
| 116 |
} catch ( Gateway_Exception $exception ) { |
| 117 |
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 118 |
throw new Action_Exception( 'internal_error', $fields->get_request_args() ); |
| 119 |
} |
| 120 |
|
| 121 |
$this->action = $fields; |
| 122 |
|
| 123 |
return $this->action; |
| 124 |
} |
| 125 |
} |
| 126 |
|