-
Notifications
You must be signed in to change notification settings - Fork 2
/
Post.php
1202 lines (1037 loc) · 41.3 KB
/
Post.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* The Post class is used to import posts into WordPres.
*/
namespace Geniem\Importer;
use Geniem\Importer\Exception\PostException as PostException;
use Geniem\Importer\Localization\Polylang as Polylang;
use WP_Post;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Post
*
* @package Geniem\Importer
*/
class Post {
/**
* A unique id for external identification.
*
* @var string
*/
protected $gi_id;
/**
* If this is an existing posts, the WP id is stored here.
*
* @var int|boolean
*/
protected $post_id;
/**
* An object resembling the WP_Post class instance.
*
* @var object The post data object.
*/
protected $post;
/**
* Attachments in an indexed array.
*
* @var array
*/
protected $attachments = [];
/**
* Holds attachments ids in an associative array
* after is has been uploaded and saved.
*
* @var array $attachment_ids = [
* [ gi_attachment_{$id} => {$post_id} ]
* ]
*/
protected $attachment_ids = [];
/**
* Metadata in an associative array.
*
* @var array
*/
protected $meta = [];
/**
* Taxonomies in a multidimensional associative array.
*
* @see $this->set_taxonomies() For description.
* @var array
*/
protected $taxonomies = [];
/**
* An array for locale data.
*
* @var array
*/
protected $i18n = [];
/**
* An array of Advanced Custom Fields data.
*
* @var array
*/
protected $acf = [];
/**
* An array holding save functions already run.
*
* @var array
*/
protected $save_state = [];
/**
* This value is true when rolling back a previous import state.
* The rollback mode skips validations and logging.
*
* @var bool
*/
protected $rollback_mode = false;
/**
* Get all save functions that have been run.
*
* @return array
*/
public function get_savestate() {
return $this->save_state;
}
/**
* Use this to save the state of run save functions.
*
* @param string $save_state The object key for the saved data.
*/
public function set_save_state( $save_state ) {
$this->save_state[ $save_state ] = $save_state;
}
/**
* Check if a specific object has been saved.
*
* @param string $saved The object key.
* @return boolean
*/
public function is_saved( $saved ) {
return isset( $this->save_state[ $saved ] );
}
/**
* Getter for post_id
*
* @return integer
*/
public function get_post_id() {
return $this->post_id;
}
/**
* Getter for ig_id
*
* @return string
*/
public function get_gi_id() {
return $this->gi_id;
}
/**
* Getter for i18n
*
* @return array
*/
public function get_i18n() {
return $this->i18n;
}
/**
* Error messages under correspondings scopes as the key.
* Example:
* [
* 'post' => [
* 'post_title' => 'The post title is not valid.'
* ]
* ]
*
* @var array
*/
protected $errors = [];
/**
* Sets a single error message or a full error array depending on the $key value.
*
* @param string $scope The error scope.
* @param mixed $data The data related to the error.
* @param string $error The error message.
*/
public function set_error( $scope = '', $data = '', $error = '' ) {
// Get needed variables
$gi_id = $this->gi_id;
$this->errors[ $scope ] = $this->errors[ $scope ] ?? [];
$message = '(' . Settings::get( 'id_prefix' ) . $gi_id . ') ' . $error;
$this->errors[ $scope ][] = [
'message' => $message,
'data' => $data,
];
// Maybe log errors.
if ( Settings::get( 'log_errors' ) ) {
// @codingStandardsIgnoreStart
error_log( 'Geniem Importer: ' . $error );
// @codingStandardsIgnoreEnd
}
}
/**
* Returns all errors.
*
* @return array
*/
public function get_errors() {
return $this->errors;
}
/**
* Encode an instance into JSON.
*
* @return array
*/
public function to_json() {
return wp_json_encode( get_object_vars( $this ) );
}
/**
* Post constructor.
*
* @param string|null $gi_id The external API id.
*/
public function __construct( $gi_id = null ) {
if ( null === $gi_id ) {
// @codingStandardsIgnoreStart
$this->set_error( 'id', 'gi_id', __( 'A unique id must be set for the Post constructor.', 'geniem-importer' ) );
// @codingStandardsIgnoreEnd
} else {
// Set the Importer id.
$this->gi_id = $gi_id;
// Fetch the WP post id, if it exists.
$this->post_id = Api::get_post_id_by_api_id( $gi_id );
if ( $this->post_id ) {
// Fetch the existing WP post object.
$this->post = get_post( $this->post_id );
// Unset the time values to ensure updates.
unset( $this->post->post_date );
unset( $this->post->post_date_gmt );
unset( $this->post->post_modified );
unset( $this->post->post_modified_gmt );
}
}
}
/**
* Handles a full importer object data setting.
*
* @param object $raw_post An object following the plugin specification.
*/
public function set_data( $raw_post ) {
$this->set_post( $raw_post->post );
// Attachments
if ( isset( $raw_post->attachments ) && is_array( $raw_post->attachments ) ) {
$this->set_attachments( $raw_post->attachments );
}
// Post meta
if ( isset( $raw_post->meta ) ) {
$this->set_meta( $raw_post->meta );
}
// Taxonomies
if ( isset( $raw_post->taxonomies ) && is_array( $raw_post->taxonomies ) ) {
$this->set_taxonomies( $raw_post->taxonomies );
}
// Advanced custom fields
if ( isset( $raw_post->acf ) && is_array( $raw_post->acf ) ) {
$this->set_acf( $raw_post->acf );
}
// If post object has i18n object property set post language
if ( isset( $raw_post->i18n ) && is_array( $raw_post->i18n ) ) {
$this->set_i18n( $raw_post->i18n );
}
}
/**
* Sets the basic data of a post.
*
* @param WP_Post|object $post_obj Post object.
* @return WP_Post|object Post object.
*/
public function set_post( $post_obj ) {
// If the post already exists, update values.
if ( ! empty( $this->post ) ) {
foreach ( get_object_vars( $post_obj ) as $attr => $value ) {
$this->post->{$attr} = $value;
}
} else {
// Set the post object.
$this->post = new \WP_Post( $post_obj );
$this->post_id = null;
}
// Filter values before validating.
foreach ( get_object_vars( $this->post ) as $attr => $value ) {
$this->post->{$attr} = apply_filters( "geniem_importer_post_value_{$attr}", $value );
}
// Validate it.
$this->validate_post( $this->post );
return $this->post;
}
/**
* Validates the post object data.
*
* @param \WP_Post $post_obj An WP_Post instance.
*/
public function validate_post( $post_obj ) {
$err_scope = 'post';
// Validate the author.
if ( isset( $post_obj->author ) ) {
$user = \get_userdata( $post_obj->author );
if ( false === $user ) {
$err = __( 'Error in the "author" column. The value must be a valid user id.', 'geniem-importer' );
$this->set_error( $err_scope, 'author', $err );
}
}
// Validate date values
if ( isset( $post_obj->post_date ) ) {
$this->validate_date( $post_obj->post_date, 'post_date', $err_scope );
}
if ( isset( $post_obj->post_date_gmt ) ) {
$this->validate_date( $post_obj->post_date_gmt, 'post_date_gmt', $err_scope );
}
if ( isset( $post_obj->post_modified ) ) {
$this->validate_date( $post_obj->post_modified, 'post_modified', $err_scope );
}
if ( isset( $post_obj->post_modified_gtm ) ) {
$this->validate_date( $post_obj->post_modified_gtm, 'post_modified_gtm', $err_scope );
}
// Validate the post status.
if ( isset( $post_obj->post_status ) ) {
$post_statuses = \get_post_statuses();
if ( 'trash' === $post_obj->post_status ) {
// @codingStandardsIgnoreStart
$err = __( 'Error in the "post_status" column. The post is currently trashed, please solve before importing.', 'geniem-importer' );
// @codingStandardsIgnoreEnd
$this->set_error( $err_scope, 'post_status', $err );
} elseif ( ! array_key_exists( $post_obj->post_status, $post_statuses ) ) {
// @codingStandardsIgnoreStart
$err = __( 'Error in the "post_status" column. The value is not a valid post status.', 'geniem-importer' );
// @codingStandardsIgnoreEnd
$this->set_error( $err_scope, 'post_status', $err );
}
}
// Validate the comment status.
if ( isset( $post_obj->comment_status ) ) {
$comment_statuses = [ 'hold', 'approve', 'spam', 'trash', 'open', 'closed' ];
if ( ! in_array( $post_obj->comment_status, $comment_statuses, true ) ) {
// @codingStandardsIgnoreStart
$err = __( 'Error in the "comment_status" column. The value is not a valid comment status.', 'geniem-importer' );
// @codingStandardsIgnoreEnd
$this->set_error( $err_scope, 'comment_status', $err );
}
}
// Validate the post parent.
if ( isset( $post_obj->post_parent ) && $post_obj->post_parent !== 0 ) {
$parent_id = Api::is_query_id( $post_obj->post_parent );
if ( $parent_id !== false ) {
// Check if parent exists.
$parent_post_id = Api::get_post_id_by_api_id( $parent_id );
if ( $parent_post_id === false ) {
// @codingStandardsIgnoreStart
$err = __( 'Error in the "post_parent" column. The queried post parent was not found.', 'geniem-importer' );
// @codingStandardsIgnoreEnd
$this->set_error( $err_scope, 'menu_order', $err );
} else {
// Set parent post id.
$post_obj->post_parent = $parent_post_id;
}
} else {
// The parent is a WP post id.
if ( \get_post( $parent_id ) === null ) {
// @codingStandardsIgnoreStart
$err = __( 'Error in the "post_parent" column. The parent id did not match any post.', 'geniem-importer' );
// @codingStandardsIgnoreEnd
$this->set_error( $err_scope, 'menu_order', $err );
}
}
}
// Validate the menu order.
if ( isset( $post_obj->menu_order ) ) {
if ( ! is_integer( $post_obj->menu_order ) ) {
$err = __( 'Error in the "menu_order" column. The value must be an integer.', 'geniem-importer' );
$this->set_error( $err_scope, 'menu_order', $err );
}
}
// Validate the post type.
if ( isset( $post_obj->post_type ) ) {
$post_types = get_post_types();
if ( ! array_key_exists( $post_obj->post_type, $post_types ) ) {
// @codingStandardsIgnoreStart
$err = __( 'Error in the "post_type" column. The value does not match a registered post type.', 'geniem-importer' );
// @codingStandardsIgnoreEnd
$this->set_error( $err_scope, 'post_type', $err );
}
}
}
/**
* Validate a mysql datetime value.
*
* @param string $date_string The datetime string.
* @param string $col_name The posts table column name.
* @param string $err_scope The error scope name.
*/
public function validate_date( $date_string = '', $col_name = '', $err_scope = '' ) {
$valid = \DateTime::createFromFormat( 'Y-m-d H:i:s', $date_string );
if ( ! $valid ) {
// @codingStandardsIgnoreStart
$err = __( "Error in the \"$col_name\" column. The value is not a valid datetime string.", 'geniem-importer' );
// @codingStandardsIgnoreEnd
$this->set_error( $err_scope, $col_name, $err );
}
}
/**
* @todo doc / validation?
* @param [type] $attachments [description]
*/
public function set_attachments( $attachments ) {
$this->attachments = $attachments;
}
/**
* Sets the post meta data.
*
* @param array $meta_data The meta data in an associative array.
*/
public function set_meta( $meta_data = [] ) {
// Force type to array.
$this->meta = (array) $meta_data;
// Filter values before validating.
foreach ( $this->meta as $key => $value ) {
$this->meta[ $key ] = apply_filters( "geniem_importer_meta_value_{$key}", $value );
}
$this->validate_meta( $this->meta );
}
/**
* Validate postmeta.
*
* @param array $meta Post meta.
* @todo Validations and filters.
*/
public function validate_meta( $meta ) {
$errors = [];
if ( ! empty( $errors ) ) {
$this->set_error( 'meta', $errors );
}
}
/**
* Set the taxonomies of the post.
* The taxonomies must be passed as an associative array
* where the key is the taxonomy slug and values are associative array
* with the name and the slug of the taxonomy term.
* Example:
* $tax_array = [
* 'category' => [
* [
* 'name' => 'My category',
* 'slug' => 'my-category',
* ]
* ];
*
* @param array $tax_array The taxonomy data.
*/
public function set_taxonomies( $tax_array = [] ) {
// Force type to array.
$this->taxonomies = (array) $tax_array;
// Filter values before validating.
foreach ( $this->taxonomies as $key => $value ) {
$this->taxonomies[ $key ] = apply_filters( "geniem_importer_taxonomy_{$key}", $value );
}
$this->validate_taxonomies( $this->taxonomies );
}
/**
* Validate the taxonomy array.
*
* @param array $taxonomies The set taxonomies for the post.
*/
public function validate_taxonomies( $taxonomies ) {
if ( ! is_array( $taxonomies ) ) {
// @codingStandardsIgnoreStart
$err = __( "Error in the taxonomies. Taxonomies must be passed in an associative array.", 'geniem-importer' );
// @codingStandardsIgnoreEnd
$this->set_error( 'taxonomy', $taxonomy, $err );
return;
}
// The passed taxonomies must be currently registered.
$registered_taxonomies = \get_taxonomies();
foreach ( $taxonomies as $term ) {
if ( ! in_array( $term['taxonomy'], $registered_taxonomies, true ) ) {
// @codingStandardsIgnoreStart
$err = __( "Error in the \"{$term['taxonomy']}\" taxonomy. The taxonomy is not registerd.", 'geniem-importer' );
// @codingStandardsIgnoreEnd
$this->set_error( 'taxonomy', $taxonomy, $err );
}
apply_filters( 'geniem_importer_validate_taxonomies', $taxonomies );
}
}
/**
* Sets the post acf data.
*
* @param array $acf_data The acf data in an associative array.
*/
public function set_acf( $acf_data = [] ) {
// Force type to array.
$this->acf = (array) $acf_data;
// Filter values before validating.
/* @todo filtering (by name, not $key?)
foreach ( $this->acf as $key => $value ) {
$this->acf[$key] = apply_filters( "geniem_importer_acf_value_{$key}", $value );
}
*/
$this->validate_acf( $this->acf );
}
/**
* Validate acf.
*
* @param array $acf Post acf fields.
* @todo Validations and filters.
*/
public function validate_acf( $acf ) {
$errors = [];
if ( ! empty( $errors ) ) {
$this->set_error( 'acf', $errors );
}
}
/**
* Sets the post localization data.
*
* @param array $i18n_data The polylang data in an associative array.
*/
public function set_i18n( $i18n_data ) {
$this->i18n = $i18n_data;
$this->validate_i18n( $this->i18n );
}
/**
* Validate the locale array.
*
* @param array $i18n The set pll data for the post.
*/
public function validate_i18n( $i18n ) {
// Check if the polylang plugin is activated.
if ( Localization\Controller::get_activated_i18n_plugin( $this ) === false ) {
return;
}
// Check if data is an array.
if ( ! is_array( $i18n ) ) {
// @codingStandardsIgnoreStart
$err = __( 'Error in the i18n data. The locale data must be passed in an associative array.', 'geniem-importer' );
// @codingStandardsIgnoreEnd
$this->set_error( 'i18n', $i18n, $err );
return;
}
// Check if locale is set and in the current installation.
if ( ! isset( $i18n['locale'] ) ) {
$err = __( 'Error in the polylang data. The locale is not set.', 'geniem-importer' );
$this->set_error( 'i18n', $i18n, $err );
} elseif ( ! in_array( $i18n['locale'], Polylang::language_list(), true ) ) {
// @codingStandardsIgnoreStart
$err = __( 'Error in the polylang data. The locale doesn\'t exist in the current WP installation', 'geniem-importer' );
// @codingStandardsIgnoreEnd
$this->set_error( 'i18n', $i18n, $err );
}
// If a master post is set for the current post, check its validity.
if ( isset( $i18n['master'] ) ) {
if ( Api::is_query_id( $i18n['master']['query_key'] ?? '' ) === false ) {
$err = __( 'Error in the i18n data. The master query id is missing or invalid.', 'geniem-importer' );
$this->set_error( 'i18n', $i18n, $err );
}
}
}
/**
* Stores the post instance and all its data into the database.
*
* @throws PostException If the post data is not valid.
*
* @param boolean $force_save Force saving even if errors occurred.
*
* @return int Post id.
*/
public function save( $force_save = false ) {
// If this is not forced or a rollback save, check for errors before the saving process.
if ( ! $force_save || ! $this->rollback_mode ) {
$valid = $this->validate();
if ( ! $valid ) {
// Log this import.
new Log( $this );
throw new PostException(
__( 'The post data was not valid. The import was canceled.', 'geniem-importer' ),
0,
$this->errors
);
}
}
$post_arr = (array) $this->post;
// Add filters for data modifications before and after importer related database actions.
add_filter( 'wp_insert_post_data', [ $this, 'pre_post_save' ], 1 );
add_filter( 'wp_insert_post', [ $this, 'after_post_save' ], 1 );
// Run the WP save function.
$post_id = wp_insert_post( $post_arr );
// Identify the post, if not yet done.
if ( empty( $this->post_id ) ) {
$this->post_id = $post_id;
$this->identify();
}
// Save attachments.
if ( ! empty( $this->attachments ) ) {
$this->save_attachments();
}
// Save metadata.
if ( ! empty( $this->meta ) ) {
$this->save_meta();
}
// Save taxonomies.
if ( ! empty( $this->taxonomies ) ) {
$this->save_taxonomies();
}
// Save acf data.
if ( ! empty( $this->acf ) ) {
$this->save_acf();
}
// Save localization data.
if ( ! empty( $this->i18n ) ) {
Localization\Controller::save_locale( $this );
}
// If this is not forced or a rollback save, check for errors after save process.
if ( ! $force_save || ! $this->rollback_mode ) {
$valid = $this->validate();
if ( ! $valid ) {
// Log this import.
new Log( $this );
$rolled_back = $this->rollback();
// Set the correct error message.
$err = $rolled_back ?
// Rollback error message
__( 'An error occurred while saving the import data. Rolled back the last successful import.', 'geniem-importer' ) :
// Default error message
__( 'An error occurred while saving the import data. Set the post status to "draft".', 'geniem-importer' );
throw new PostException(
$err,
0,
$this->errors
);
}
}
// This logs a successful import.
new Log( $this );
// Remove the custom filters.
remove_filter( 'wp_insert_post_data', [ $this, 'pre_post_save' ] );
remove_filter( 'wp_insert_post', [ $this, 'after_post_save' ] );
return $post_id;
}
/**
* Delete all data related to a single post.
* Note: This keeps the basic post data intact int the posts table.
*/
public function delete_data() {
// This removes most of data related to a post.
Api::delete_post_meta_data( $this->post_id );
// Delete all term relationships.
\wp_delete_object_term_relationships( $this->post_id, \get_taxonomies() );
// Run custom action for custom data.
// Use this if the data is not in the postmeta table.
do_action( 'geniem_importer_delete_data', $this->post_id );
}
/**
* Saves the attachments of the post.
* Currently supports images.
*
* @todo add support for other media formats too
*/
protected function save_attachments() {
// All of the following are required for the media_sideload_image function.
if ( ! function_exists( '\media_sideload_image' ) ) {
require_once( ABSPATH . 'wp-admin/includes/media.php' );
}
if ( ! function_exists( '\download_url' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
if ( ! function_exists( '\wp_read_image_metadata' ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
}
$attachment_prefix = Settings::get( 'attachment_prefix' );
$attachment_language = Api::get_prop( $this->i18n, 'locale' );
foreach ( $this->attachments as &$attachment ) {
$attachment_id = Api::get_prop( $attachment, 'id' );
$attachment_src = Api::get_prop( $attachment, 'src' );
$attachment_post_id = Api::get_attachment_post_id_by_attachment_id( $attachment_id );
if ( empty( $attachment_src ) || empty( $attachment_id ) ) {
// @codingStandardsIgnoreStart
$this->set_error( 'attachment', $attachment, __( 'The attachment object has missing parameters.', 'geniem_importer' ) );
// @codingStandardsIgnoreEnd
continue;
}
// Check if attachment doesn't exists, and upload it.
if ( ! $attachment_post_id ) {
// Insert upload attachment from url
$attachment_post_id = $this->insert_attachment_from_url( $attachment_src, $attachment, $this->post_id );
// Something went wrong.
if ( is_wp_error( $attachment_post_id ) ) {
// @codingStandardsIgnoreStart
$this->set_error( 'attachment', $attachment, __( 'An error occurred uploading the file.', 'geniem_importer' ) );
// @codingStandardsIgnoreEnd
}
if ( $attachment_post_id ) {
// Set indexed meta for fast queries.
// Depending on the attachment prefix this would look something like:
// meta_key | meta_value
// gi_attachment_{1234} | 1234
update_post_meta( $attachment_post_id, $attachment_prefix . $attachment_id, $attachment_id );
// Set the generally queryable id.
// Depending on the attachment prefix this would look something like:
// meta_key | meta_value
// gi_attachment | 1234
update_post_meta( $attachment_post_id, rtrim( $attachment_prefix, '_' ), $attachment_id );
// Polylang mananages languages.
// @todo, I think this is set automatically
if ( Polylang::pll() ) {
$attachment_language = Api::get_prop( $this->i18n, 'locale' );
if ( $attachment_language ) {
Polylang::set_attachment_language( $attachment_post_id, $attachment_language );
}
}
} // End if().
} // End if().
// Update attachment meta and handle translations
if ( $attachment_post_id ) {
// Get attachment translations.
if ( Polylang::pll() ) {
$attachment_post_id = Polylang::get_attachment_by_language(
$attachment_post_id,
$attachment_language
);
}
// Update attachment info.
$attachment_args = [
'ID' => $attachment_post_id,
'post_title' => Api::get_prop( $attachment, 'title' ),
'post_content' => Api::get_prop( $attachment, 'description' ),
'post_excerpt' => Api::get_prop( $attachment, 'caption' ),
];
// Save the attachement post object data
wp_update_post( $attachment_args );
// Use caption as an alternative text.
$alt_text = Api::get_prop( $attachment, 'caption' );
if ( $alt_text ) {
// Save image alt text into attachment post meta
update_post_meta( $attachment_post_id, '_wp_attachment_image_alt', $alt_text );
}
// Set the attachment post_id.
$this->attachment_ids[ $attachment_prefix . $attachment_id ] =
Api::set_prop( $attachment, 'post_id', $attachment_post_id );
} // End if().
} // End foreach().
// This functions is done.
$this->set_save_state( 'attachments' );
}
/**
* Insert an attachment from an URL address.
*
* @param string $attachment_src Source file url.
* @param object $attachment Post class instances attachment.
* @param int $post_id Attachments may be associated with a parent post or page.
* Specify the parent's post ID, or 0 if unattached.
*
* @return int $attachment_id
*/
protected function insert_attachment_from_url( $attachment_src, $attachment, $post_id ) {
// Get filename from the url.
$file_name = basename( $attachment_src );
// Exif related variables
$exif_imagetype = exif_imagetype( $attachment_src );
$exif_supported_imagetypes = array(
IMAGETYPE_JPEG,
IMAGETYPE_TIFF_II,
IMAGETYPE_TIFF_MM
);
// Construct file local url.
$tmp_folder = Settings::get( 'tmp_folder' );
$local_image = $tmp_folder . $file_name;
// Copy file to local image location
copy( $attachment_src, $local_image );
// If exif_read_data is callable and file type could contain exif data.
if ( is_callable( 'exif_read_data' ) && in_array( $exif_imagetype, $exif_supported_imagetypes ) ) {
// Manipulate image exif data to prevent.
$this->strip_unsupported_exif_data( $local_image );
}
// Get file from local temp folder.
$file_content = file_get_contents( $local_image );
// Upload file to uploads.
$upload = wp_upload_bits( $file_name, null, $file_content );
// After upload process we are free to delete the tmp image.
unlink( $local_image );
// If error occured during upload return false.
if ( ! empty( $upload['error'] ) ) {
return false;
}
// File variables
$file_path = $upload['file'];
$file_type = wp_check_filetype( $file_name, null );
$wp_upload_dir = wp_upload_dir();
// wp_insert_attachment post info
$post_info = array(
'guid' => $wp_upload_dir['url'] . '/' . $file_name,
'post_mime_type' => $file_type['type'],
'post_title' => Api::get_prop( $attachment, 'title' ),
'post_content' => Api::get_prop( $attachment, 'description' ),
'post_excerpt' => Api::get_prop( $attachment, 'caption' ),
'post_status' => 'inherit',
);
// Insert attachment to the database.
$attachment_id = wp_insert_attachment( $post_info, $file_path, $post_id, true );
// Generate post thumbnail attachment meta data.
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path );
// Assign metadata to an attachment.
wp_update_attachment_metadata( $attachment_id, $attachment_data );
return $attachment_id;
}
/**
* If exif_read_data() fails, remove exif data from the image file
* to prevent errors in WordPress core.
*
* @param string $local_image Local url for an image.
* @return void No return.
*/
protected function strip_unsupported_exif_data( $local_image ) {
// Variable for exif data errors in PHP
$php_exif_data_error_exists = false;
// Check for PHP exif_read_data function errors!
try {
exif_read_data( $local_image );
} catch ( \Exception $e ) {
$php_exif_data_error_exists = true;
}
// If image magic is installed and exif_data_error exists
if ( class_exists( 'Imagick' ) && $php_exif_data_error_exists === true ) {
// Run image through image magick
$imagick_object = new \Imagick( realpath( $local_image ) );
// Strip off all exif data to prevent PHP 5.6 and PHP 7.0 exif errors!
$imagick_object->stripImage();
// Write manipulated file to the tmp folder
$imagick_file = $imagick_object->writeImage( $local_image );
}
}
/**
* Saves the metadata of the post.
*
* @return void
*/
protected function save_meta() {
if ( is_array( $this->meta ) ) {
foreach ( $this->meta as $key => $value ) {
// Check if post has a attachment thumbnail
if ( $key === '_thumbnail_id' ) {
// First check if attachments have been saved.
// If not, set an error and skip thumbnail setting.
if ( ! $this->is_saved( 'attachments' ) ) {
// @codingStandardsIgnoreStart
$err = __( 'Attachments must be saved before saving the thumbnail id for a post. Discarding saving meta for the key "_thumbnail_id".', 'geniem-importer' );
// @codingStandardsIgnoreEnd
$this->set_error( 'meta', $key, $err );
continue;
}
// If attachment id exists
$attachment_post_id = $this->attachment_ids[ $value ] ?? '';
// If not empty set _thumbnail_id
if ( ! empty( $attachment_post_id ) ) {
$value = $attachment_post_id;
}
// Set error: attachment did not exist.
else {
// @codingStandardsIgnoreStart
$this->set_error( 'meta', $key, __( 'Can not save the thumbnail data. The attachment was not found.', 'geniem_importer' ) );
// @codingStandardsIgnoreEnd
unset( $this->meta[ $key ] );
continue;
}
}
// Update post meta
update_post_meta( $this->post_id, $key, $value );
}
}
// This functions is done.
$this->set_save_state( 'meta' );
}
/**
* Sets the terms of a post and create taxonomy terms
* if they do not exist yet.
*
* @todo make similar to acf taxonomies
*/
protected function save_taxonomies() {
if ( is_array( $this->taxonomies ) ) {
$term_ids_by_tax = [];
foreach ( $this->taxonomies as &$term ) {
// Safely get values from the term.
$slug = Api::get_prop( $term, 'slug' );
$taxonomy = Api::get_prop( $term, 'taxonomy' );
// Fetch the term object.