PluginProbe ʕ •ᴥ•ʔ
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets / trunk
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets vtrunk
4.1.1 3.9.5 3.9.6 4.0.0 4.0.1 4.1.0 trunk 2.12 2.13 2.14 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9 3.7.0 3.7.1 3.7.2 3.7.3 3.7.3-beta-1.0 3.7.4 3.7.4-beta-1.0 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8.0 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4
wp-all-import / addon-api / classes / helpers.php
wp-all-import / addon-api / classes Last commit date
admin.php 1 month ago base.php 1 month ago data-importer.php 1 month ago helpers.php 1 month ago importer.php 1 month ago manager.php 1 month ago parser.php 1 month ago post-data-importer.php 1 month ago rest.php 1 month ago updater.php 1 month ago view.php 1 month ago
helpers.php
296 lines
1 <?php
2 namespace Wpai\AddonAPI;
3
4 if ( ! defined( 'ABSPATH' ) ) exit;
5
6 /**
7 * Takes a value and passes it to a callback, then returns the value.
8 *
9 * @param mixed $value
10 * @param callable $callback
11 *
12 * @return mixed
13 */
14 function tap( $value, $callback ) {
15 $callback( $value );
16
17 return $value;
18 }
19
20 /**
21 * Takes a value and passes it to a list of callbacks, then returns the value.
22 *
23 * @param mixed $value
24 * @param callable[] $callbacks
25 *
26 * @return mixed
27 */
28 function pipe( $value, $callbacks ) {
29 return array_reduce( $callbacks, function ( $value, $callback ) {
30 return $callback( $value );
31 }, $value );
32 }
33
34 /**
35 * Renders a view.
36 *
37 * @param string $viewPath
38 * @param array $data
39 * @param string|null $defaultView
40 * @param bool $echo
41 * @param string|null $path
42 *
43 * @return string|null
44 * @throws \Exception
45 */
46 function view(
47 string $viewPath,
48 array $data,
49 $defaultView = null,
50 $echo = true,
51 $path = null
52 ) {
53 $path = $path ?: WP_ALL_IMPORT_ROOT_DIR . '/addon-api' . '/templates/';
54 $filePath = $path . $viewPath . '.php';
55 $defaultFilePath = $path . $defaultView . '.php';
56
57 extract( $data );
58
59 $view = $filePath;
60
61 if ( ! is_file( $view ) ) {
62 if ( $defaultView ) {
63 $view = $defaultFilePath;
64 } else {
65 throw new \Exception( esc_html("The requested template file $filePath was not found.") );
66 }
67 }
68
69 if ( $echo ) {
70 include $view;
71 } else {
72 ob_start();
73 include $view;
74
75 return ob_get_clean();
76 }
77 }
78
79 function searchExistingInAttachedFile( $url, $post_id, $file_type, $importData, $logger ) {
80 $articleData = $importData['articleData'];
81 $image_name = basename( $url );
82
83 $current_xml_node = false;
84 if ( ! empty( $importData['current_xml_node'] ) ) {
85 $current_xml_node = $importData['current_xml_node'];
86 }
87
88 $import_id = false;
89 if ( ! empty( $importData['import'] ) ) {
90 $import_id = $importData['import']->id;
91 }
92
93 $uploads = wp_upload_dir();
94 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
95 $uploads = apply_filters( 'wp_all_import_images_uploads_dir', $uploads, $articleData, $current_xml_node, $import_id );
96 $targetDir = $uploads['path'];
97
98 if ( empty( $attch ) ) {
99 /* translators: 1: image URL, 2: image filename */
100 $logger and call_user_func( $logger, sprintf( __( '- Searching for existing image `%1$s` by `_wp_attached_file` `%2$s`...', 'wp-all-import' ), $url, $image_name ) );
101 $attch = wp_all_import_get_image_from_gallery( $image_name, $targetDir, $file_type );
102 }
103
104 if ( ! empty( $attch ) ) {
105 $logger and call_user_func( $logger, sprintf( __( '- Existing image was found by `_wp_attached_file` ...', 'wp-all-import' ), $url ) );
106 $imageRecord = new \PMXI_Image_Record();
107 $imageRecord->getBy( [
108 'attachment_id' => $attch->ID
109 ] );
110 $imageRecord->isEmpty() and $imageRecord->set( [
111 'attachment_id' => $attch->ID,
112 'image_url' => $url,
113 'image_filename' => $image_name
114 ] )->insert();
115 // Attach media to current post if it's currently unattached.
116 if ( empty( $attch->post_parent ) ) {
117 wp_update_post(
118 array(
119 'ID' => $attch->ID,
120 'post_parent' => $post_id
121 )
122 );
123 }
124
125 return $attch->ID;
126 }
127 }
128
129 function searchExistingImage( $url, $post_id, $mode, $file_type, $import_data, $logger ) {
130 $imageList = new \PMXI_Image_List();
131
132 switch ( $mode ) {
133 case 'by_url':
134 // trying to find existing image in images table
135 /* translators: %s: image URL */
136 $logger and call_user_func( $logger, sprintf( __( '- Searching for existing image `%s` by URL...', 'wp-all-import' ), rawurldecode( $url ) ) );
137 $attch = $imageList->getExistingImageByUrl( $url );
138
139 if ( $attch ) {
140 /* translators: %s: image URL */
141 $logger and call_user_func( $logger, sprintf( __( 'Existing image was found by URL `%s`...', 'wp-all-import' ), $url ) );
142
143 return $attch->ID;
144 }
145
146 break;
147 default:
148 /* translators: %s: image URL */
149 $logger and call_user_func( $logger, sprintf( __( '- Searching for existing image `%s` by Filename...', 'wp-all-import' ), rawurldecode( $url ) ) );
150 $attch = $imageList->getExistingImageByFilename( basename( $url ) );
151
152 if ( $attch ) {
153 /* translators: %s: image filename */
154 $logger and call_user_func( $logger, sprintf( __( 'Existing image was found by Filename `%s`...', 'wp-all-import' ), basename( $url ) ) );
155
156 return $attch->ID;
157 }
158
159 $attch = searchExistingInAttachedFile( $url, $post_id, $file_type, $import_data, $logger );
160
161 if ( $attch ) {
162 return $attch->ID;
163 }
164 }
165
166 return null;
167 }
168
169 /**
170 * Checks if an array is associative.
171 *
172 * @param array $arr
173 *
174 * @return bool
175 */
176 function isAssocArray( array $arr ) {
177 if ( $arr === [] ) {
178 return true;
179 }
180
181 return array_keys( $arr ) === range( 0, count( $arr ) - 1 );
182 }
183
184 /**
185 * Gets the current value of a field for a post.
186 *
187 * @param string $post_type
188 * @param int $post_id
189 * @param string $key
190 * @param mixed $default
191 *
192 * @return mixed
193 */
194 function currentValue( $post_type, $post_id, $key, $default ) {
195 switch ( $post_type ) {
196 case 'import_users':
197 case 'shop_customer':
198 $value = get_user_meta( $post_id, $key, true );
199 break;
200 case 'taxonomies':
201 $value = get_term_meta( $post_id, $key, true );
202 break;
203 default:
204 $value = get_post_meta( $post_id, $key, true );
205 break;
206 }
207
208 return $value ?: $default;
209 }
210
211 /**
212 * @param array $field
213 *
214 * @return string
215 */
216 function getFieldClass( $field ) {
217 $class = 'Wpai\AddonAPI\PMXI_Addon_' . ucfirst( $field['type'] ) . '_Field';
218
219 return class_exists( $class ) ? $class : PMXI_Addon_Field::class;
220 }
221
222 // Class Helpers
223
224 trait Singleton {
225 /** @var self|null */
226 private static $instance = null;
227
228 /**
229 * @return self
230 */
231 final public static function getInstance(): self {
232 if ( ! self::$instance ) {
233 self::$instance = new self();
234 }
235
236 return self::$instance;
237 }
238
239 // Prevent cloning of the instance
240 public function __clone() {
241 }
242
243 // Prevent deserialization of the instance
244 public function __wakeup() {
245 }
246 }
247
248 trait Updatable {
249 public ?PMXI_Addon_Updater $updater;
250
251 public function initEed() {
252 $api_url = $this->getApiUrl();
253 $plugin_file = $this->getPluginFile();
254
255 if ( empty( $api_url ) ) {
256 return;
257 }
258
259 $this->updater = new PMXI_Addon_Updater(
260 $api_url,
261 $plugin_file,
262 [
263 'version' => $this->version, // current version number
264 'license' => false, // license key (used get_option above to retrieve from DB)
265 'item_name' => $this->getEddName(), // name of this plugin
266 'author' => $this->getEddAuthor() // author of this plugin
267 ]
268 );
269 }
270
271 public function getEddName() {
272 return $this->name();
273 }
274
275 public function getEddAuthor() {
276 return 'Soflyy';
277 }
278
279 public function getPluginFile() {
280 return $this->rootDir . '/plugin.php';
281 }
282
283 public function getApiUrl() {
284 $options = get_option( 'PMXI_Plugin_Options' );
285 $api_url = null;
286
287 if ( ! empty( $options['info_api_url_new'] ) ) {
288 $api_url = $options['info_api_url_new'];
289 } elseif ( ! empty( $options['info_api_url'] ) ) {
290 $api_url = $options['info_api_url'];
291 }
292
293 return $api_url;
294 }
295 }
296