PluginProbe
Import WP – CSV & XML Import Export for WordPress / trunk
Import WP – CSV & XML Import Export for WordPress vtrunk
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 trunk 0.1.6 All 142 releases
jc-importer / class / Common / Addon / AddonCustomFieldsApi.php

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

143 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 /**
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