| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Addon; |
| 4 |
|
| 5 |
/** |
| 6 |
* @deprecated 2.14.0 |
| 7 |
*/ |
| 8 |
class AddonCustomFieldsApi |
| 9 |
{ |
| 10 |
protected $_name; |
| 11 |
|
| 12 |
protected $_prefix; |
| 13 |
|
| 14 |
protected $_fields = []; |
| 15 |
|
| 16 |
protected $_init_callback; |
| 17 |
|
| 18 |
protected $_reg_fields_callback; |
| 19 |
|
| 20 |
protected $_save_callback; |
| 21 |
|
| 22 |
protected $_custom_fields; |
| 23 |
|
| 24 |
protected $_template; |
| 25 |
|
| 26 |
protected $_importer_model; |
| 27 |
|
| 28 |
public function __construct($name, $init_callback) |
| 29 |
{ |
| 30 |
$this->_name = $name; |
| 31 |
$this->_init_callback = $init_callback; |
| 32 |
|
| 33 |
add_filter('iwp/custom_field_key', [$this, '_get_custom_field_key'], 10); |
| 34 |
} |
| 35 |
|
| 36 |
public function get_name() |
| 37 |
{ |
| 38 |
return $this->_name; |
| 39 |
} |
| 40 |
|
| 41 |
public function set_prefix($prefix) |
| 42 |
{ |
| 43 |
$this->_prefix = $prefix; |
| 44 |
} |
| 45 |
|
| 46 |
public function add_prefix($id) |
| 47 |
{ |
| 48 |
return $this->_prefix !== false ? $this->_prefix . '::' . $id : $id; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @param \ImportWP\Pro\Importer\Template\CustomFields $custom_fields |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function _init($custom_fields) |
| 57 |
{ |
| 58 |
$this->_custom_fields = $custom_fields; |
| 59 |
$this->_template = $custom_fields->template(); |
| 60 |
|
| 61 |
call_user_func($this->_init_callback, $this); |
| 62 |
} |
| 63 |
|
| 64 |
public function add_field($name, $id) |
| 65 |
{ |
| 66 |
$value = $this->add_prefix($id); |
| 67 |
$this->_fields[] = ['value' => $value, 'label' => $this->get_name() . ' - ' . $name]; |
| 68 |
} |
| 69 |
|
| 70 |
public function _get_fields() |
| 71 |
{ |
| 72 |
return $this->_fields; |
| 73 |
} |
| 74 |
|
| 75 |
public function template() |
| 76 |
{ |
| 77 |
return $this->_template; |
| 78 |
} |
| 79 |
|
| 80 |
public function _set_importer_model($importer_model) |
| 81 |
{ |
| 82 |
$this->_importer_model = $importer_model; |
| 83 |
} |
| 84 |
|
| 85 |
public function register_fields($callback) |
| 86 |
{ |
| 87 |
$this->_reg_fields_callback = $callback; |
| 88 |
} |
| 89 |
|
| 90 |
public function _register_fields($importer_model) |
| 91 |
{ |
| 92 |
call_user_func($this->_reg_fields_callback, $importer_model); |
| 93 |
} |
| 94 |
|
| 95 |
public function save($callback) |
| 96 |
{ |
| 97 |
$this->_save_callback = $callback; |
| 98 |
} |
| 99 |
|
| 100 |
public function _save($api, $post_id, $key, $value) |
| 101 |
{ |
| 102 |
if (!$this->_key_contains_prefix($key)) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
$field_key = $this->_get_custom_field_key($key); |
| 107 |
|
| 108 |
call_user_func_array($this->_save_callback, [$api, $post_id, $field_key, $value]); |
| 109 |
} |
| 110 |
|
| 111 |
public function _key_contains_prefix($key) |
| 112 |
{ |
| 113 |
if ($this->_prefix && strpos($key, "{$this->_prefix}::") !== 0) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @param string $key |
| 121 |
* @param TemplateInterface $template |
| 122 |
* @return string |
| 123 |
*/ |
| 124 |
public function _get_custom_field_key($key) |
| 125 |
{ |
| 126 |
if (!$this->_key_contains_prefix($key)) { |
| 127 |
return $key; |
| 128 |
} |
| 129 |
|
| 130 |
if (!strpos($key, '::')) { |
| 131 |
return $key; |
| 132 |
} |
| 133 |
|
| 134 |
$field_key = substr($key, strrpos($key, '::') + strlen('::')); |
| 135 |
$matches = []; |
| 136 |
if (preg_match('/^([^-]+)-/', $field_key, $matches) === false) { |
| 137 |
return $key; |
| 138 |
} |
| 139 |
|
| 140 |
return $field_key; |
| 141 |
} |
| 142 |
} |
| 143 |
|