PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.8.3
Import WP – CSV & XML Import Export for WordPress v2.8.3
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Addon / AddonCustomFieldsApi.php

AddonCustomFieldsApi.php in Import WP – CSV & XML Import Export for WordPress 2.8.3, at class/Common/Addon/AddonCustomFieldsApi.php

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