/
class-contributors.php
867 lines (792 loc) · 25.8 KB
/
class-contributors.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
<?php
/**
* @author Pressbooks <code@pressbooks.com>
* @license GPLv3 (or any later version)
*/
// TODO: Security audit
// @phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
namespace Pressbooks;
use function Pressbooks\Metadata\init_book_data_models;
use function Pressbooks\Utility\explode_remove_and;
use function Pressbooks\Utility\str_starts_with;
use Pressbooks\PostType\BackMatter;
use Pressbooks\Utility\AutoDisplayable;
use Pressbooks\Utility\HandlesTransfers;
/**
*
*/
class Contributors implements BackMatter, Transferable {
use AutoDisplayable;
use HandlesTransfers;
public const TAXONOMY = 'contributor';
const PICTURE_MIN_PIXELS = 400;
/**
* @var Contributors
*/
static $instance = null;
/**
* Valid contributor slugs ordered by preference
*
* @var array
*/
public $valid = [
'pb_editors',
'pb_authors',
'pb_contributors',
'pb_translators',
'pb_reviewers',
'pb_illustrators',
];
/**
* @var array
*/
public $deprecated = [
'pb_author',
'pb_section_author',
'pb_contributing_authors',
'pb_author_file_as',
'pb_editor',
'pb_translator',
];
/**
* @var bool
*/
private $exporting;
/**
* Function to init our class, set filters & hooks, set a singleton instance
*
* @return Contributors
*/
public static function init() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
self::hooks( self::$instance );
}
return self::$instance;
}
/**
* @param Contributors $obj
*/
public static function hooks( Contributors $obj ) {
add_action( 'delete_' . self::TAXONOMY, [ $obj, 'deleteContributor' ], 10, 3 );
add_action(
'pb_pre_export', function () use ( $obj ) {
$obj->exporting = true; //only set this variable during exports
}
);
add_filter( 'the_content', [ $obj, 'overrideDisplay' ], 13 ); // Run after wpautop to avoid unwanted breaklines.
$obj->bootExportable( $obj );
}
/**
* @param string $contributor_type
*
* @return bool
*/
public function isValid( $contributor_type ) {
return in_array( $contributor_type, $this->valid, true );
}
/**
* @param string $contributor_type
*
* @return bool
*/
public function isDeprecated( $contributor_type ) {
return in_array( $contributor_type, $this->deprecated, true );
}
/**
* Retrieve all author/editor/etc lists for a given Post ID
*
* @param int $post_id
* @param bool $as_strings
* @param bool $include_term_meta
*
* @return array
*/
public function getAll( $post_id, $as_strings = true, $include_term_meta = false ) {
$contributors = [];
foreach ( $this->valid as $contributor_type ) {
if ( $as_strings ) {
$contributors[ $contributor_type ] = $this->get( $post_id, $contributor_type );
} else {
$contributors[ $contributor_type ] = $this->getArray( $post_id, $contributor_type, $include_term_meta );
}
}
return $contributors;
}
/**
* Retrieve author/editor/etc lists for a given Post ID and Contributor type, returns string
*
* @param int $post_id
* @param string $contributor_type
*
* @return string
*/
public function get( $post_id, $contributor_type ) {
$contributors = $this->getArray( $post_id, $contributor_type );
return \Pressbooks\Utility\implode_add_and( ';', $contributors );
}
/**
* Retrieve author/editor/etc lists for a given Post ID and Contributor type, returns array
*
* @param int $post_id
* @param bool $include_term_meta
* @param string $contributor_type
*
* @return array
*/
public function getArray( $post_id, $contributor_type, $include_term_meta = false ) {
if ( ! str_starts_with( $contributor_type, 'pb_' ) ) {
$contributor_type = 'pb_' . $contributor_type;
}
if ( ! $this->isValid( $contributor_type ) ) {
return [];
}
// Look if contributors exist as taxonomies (new data model)
$contributors = [];
$meta = get_post_meta( $post_id, $contributor_type, false );
if ( is_array( $meta ) ) {
foreach ( $meta as $slug ) {
$term = get_term_by( 'slug', $slug, self::TAXONOMY );
if ( $term ) {
$contributor = get_term_meta( $term->term_id );
if ( ! $include_term_meta ) {
$contributors[] = $term->name;
} else {
if ( $term ) {
foreach ( $contributor as $field => $property ) {
$contributor[ $field ] = is_array( $property ) ? $property[0] : $property;
}
$contributor['name'] = $term->name;
$contributor['slug'] = $term->slug;
$contributors[] = $contributor;
}
}
}
}
}
if ( empty( $contributors ) ) {
// Look if contributors exist as metadata (old data model)
// If yes then convert to taxonomies (new data model)
$map = [
'pb_authors' => [ 'pb_author', 'pb_section_author', 'pb_author_file_as' ],
'pb_editors' => [ 'pb_editor' ],
'pb_translators' => [ 'pb_translator' ],
'pb_contributors' => [ 'pb_contributing_authors' ],
];
if ( isset( $map[ $contributor_type ] ) ) {
foreach ( $map[ $contributor_type ] as $slug ) {
$meta = get_post_meta( $post_id, $slug, false );
if ( is_array( $meta ) ) {
foreach ( $meta as $name ) {
$result = $this->insert( $name );
if ( $result !== false ) {
$added = $this->link( $result['term_id'], $post_id, $contributor_type );
if ( $added !== false ) {
$contributors[] = $name;
delete_post_meta( $post_id, $slug, $name );
}
}
}
}
}
}
}
return $contributors;
}
/**
* Insert a new contributor by name or fields data and associate it by post_id if present.
* If the contributor's already exists by $compare_by parameter and/or name field it will be replaced by the new one.
* if $post_id is present ($post_id != 0) and the metadata will be updated anyway.
* The contributor_picture will be downloaded to the media if the field and $downloads object is present.
*
* @param string | array $data
* @param int $post_id
* @param string $contributor_type
* @param \Pressbooks\Cloner\Downloads $downloads (optional)
* @return array|false|int|mixed
*/
public function insert( $data, $post_id = 0, $contributor_type = 'pb_authors', $downloads = null, $compare_by = 'name' ) {
if ( is_string( $data ) ) {
return $this->insertByFullName( $data, $post_id, $contributor_type );
}
$term_id = false;
$term = false;
if ( is_array( $data ) && isset( $data['name'] ) ) {
if ( $compare_by !== 'disambiguate' ) {
$term_by_name = get_term_by( 'name', $data['name'], self::TAXONOMY );
if ( $term_by_name !== false ) {
$term = $term_by_name;
} elseif ( $compare_by !== 'name' && array_key_exists( $compare_by, $data ) ) {
$term_by_custom = get_term_by( $compare_by, $data[ $compare_by ], self::TAXONOMY );
if ( $term_by_custom !== false ) {
$term = $term_by_custom;
}
}
}
if ( ! $term ) {
$results = wp_insert_term(
$data['name'],
self::TAXONOMY,
[
'slug' => $data['slug'],
]
);
if ( $results instanceof \WP_Error && isset( $results->error_data['term_exists'] ) ) {
if ( $compare_by === 'disambiguate' ) {
$results = wp_insert_term(
$data['name'],
self::TAXONOMY,
[
'slug' => $data['slug'] . '-' . str_random( 10 ),
]
);
$term_id = $results['term_id'];
} else {
$term_id = $results->error_data['term_exists'];
}
} else {
$term_id = $results['term_id'];
}
} else {
$term_id = $term->term_id;
}
unset( $data['name'] );
unset( $data['slug'] );
$contributor_fields = self::getContributorFields();
$terms_contributor_meta = get_term_meta( $term_id );
foreach ( $data as $field => $property ) {
if (
array_key_exists( $field, $contributor_fields ) &&
(
! array_key_exists( $field, $terms_contributor_meta ) ||
$terms_contributor_meta[ $field ][0] !== $property
)
) {
if ( ! is_null( $downloads ) && $field === self::TAXONOMY . '_picture' ) {
$image_id = $downloads->fetchAndSaveUniqueImage( $property );
if ( $image_id <= 0 ) {
continue;
} else {
$property = wp_get_attachment_url( $image_id );
}
}
if ( ! array_key_exists( $field, $terms_contributor_meta ) ) {
add_term_meta( $term_id, $field, $property );
} else {
update_term_meta( $term_id, $field, $property );
}
}
}
if ( $term_id && $post_id ) {
$this->link( $term_id, $post_id, $contributor_type );
}
}
return $term_id;
}
/**
* @param string $full_name
* @param int $post_id (optional)
* @param string $contributor_type (optional)
*
* @return array|false An array containing the `term_id` and `term_taxonomy_id`, false otherwise.
*/
public function insertByFullName( $full_name, $post_id = 0, $contributor_type = 'pb_authors' ) {
$full_name = trim( $full_name );
$slug = sanitize_title_with_dashes( remove_accents( $full_name ), '', 'save' );
$term = get_term_by( 'slug', $slug, self::TAXONOMY );
if ( $term ) {
$results = [
'term_id' => $term->term_id,
'term_taxonomy_id' => $term->term_taxonomy_id,
];
} else {
$results = wp_insert_term(
$full_name, self::TAXONOMY, [
'slug' => $slug,
]
);
}
if ( $post_id && is_array( $results ) ) {
$this->link( $results['term_id'], $post_id, $contributor_type );
}
return is_array( $results ) ? $results : false;
}
/**
* Associate a Contributor's Term ID to a Post ID (Taxonomy + Meta)
* Technically we are assigning the Term Slug to the Post ID. This function handles either.
*
* @param int|string $term_id
* @param int $post_id
* @param string $contributor_type
*
* @return bool
*/
public function link( $term_id, $post_id, $contributor_type = 'pb_authors' ) {
global $wpdb;
if ( ! str_starts_with( $contributor_type, 'pb_' ) ) {
$contributor_type = 'pb_' . $contributor_type;
}
if ( $this->isValid( $contributor_type ) ) {
if ( preg_match( '/\d+/', $term_id ) ) {
$term = get_term( $term_id, self::TAXONOMY ); // Get slug by Term ID
} else {
$term = get_term_by( 'slug', $term_id, self::TAXONOMY ); // Verify that slug is valid
}
if ( $term && ! is_wp_error( $term ) ) {
wp_set_object_terms( $post_id, $term->term_id, self::TAXONOMY, true );
$meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $contributor_type, $term->slug ) );
if ( $meta_id ) {
return true;
} else {
return add_post_meta( $post_id, $contributor_type, $term->slug ) !== false;
}
}
}
return false;
}
/**
* Disassociate a Contributor's Term ID to a Post ID (Taxonomy + Meta)
*
* @param int|string $term_id
* @param int $post_id
* @param string $contributor_type
*
* @return bool
*/
public function unlink( $term_id, $post_id, $contributor_type = 'pb_authors' ) {
if ( ! str_starts_with( $contributor_type, 'pb_' ) ) {
$contributor_type = 'pb_' . $contributor_type;
}
if ( $this->isValid( $contributor_type ) ) {
if ( preg_match( '/\d+/', $term_id ) ) {
$term = get_term( $term_id, self::TAXONOMY ); // Get slug by Term ID
} else {
$term = get_term_by( 'slug', $term_id, self::TAXONOMY ); // Verify that slug is valid
}
if ( $term && ! is_wp_error( $term ) ) {
wp_remove_object_terms( $post_id, $term->term_id, self::TAXONOMY );
delete_post_meta( $post_id, $contributor_type, $term->slug );
return true;
}
}
return false;
}
/**
* Remove deleted contributor's post meta references
* @param int $term
* @param int $tt_id
* @param \WP_Term $deleted_term
*/
public function deleteContributor( $term, $tt_id, $deleted_term ) {
global $wpdb;
$placeholder = implode( ', ', array_fill( 0, count( $this->valid ), '%s' ) );
$wpdb->query(
$wpdb->prepare(
//phpcs:ignore
"DELETE FROM $wpdb->postmeta WHERE meta_key IN ( $placeholder ) AND meta_value = %s",
array_merge( $this->valid, [ $deleted_term->slug ] )
)
);
}
/**
* Return contributors' taxonomy terms as an associative array with meta HTML tag information.
*
* @param string $field
* @return array
*/
public static function getContributorFields( $field = '' ) {
$allowed_fields = [
self::TAXONOMY . '_prefix' => [
'label' => esc_html__( 'Prefix', 'pressbooks' ),
'tag' => self::TAXONOMY . '-prefix',
'input_type' => 'text',
'description' => esc_html__( 'Prefix to be displayed before this contributor\'s name, e.g. Dr., Prof., Ms., Rev., Capt.', 'pressbooks' ),
'sanitization_method' => 'sanitize_text_field',
],
self::TAXONOMY . '_first_name' => [
'label' => esc_html__( 'First Name', 'pressbooks' ),
'tag' => self::TAXONOMY . '-first-name',
'input_type' => 'text',
'sanitization_method' => 'sanitize_text_field',
],
self::TAXONOMY . '_last_name' => [
'label' => esc_html__( 'Last Name', 'pressbooks' ),
'tag' => self::TAXONOMY . '-last-name',
'input_type' => 'text',
'sanitization_method' => 'sanitize_text_field',
],
self::TAXONOMY . '_suffix' => [
'label' => esc_html__( 'Suffix', 'pressbooks' ),
'tag' => self::TAXONOMY . '-suffix',
'input_type' => 'text',
'description' => esc_html__( 'Suffix to be displayed after this contributors\'s name, e.g. Jr., Sr., IV, PhD, MD, USN (Ret.).', 'pressbooks' ),
'sanitization_method' => 'sanitize_text_field',
],
self::TAXONOMY . '_picture' => [
'label' => esc_html__( 'Picture', 'pressbooks' ),
'tag' => self::TAXONOMY . '-picture',
'input_type' => 'picture',
'sanitization_method' => '\Pressbooks\Sanitize\validate_url_field',
],
self::TAXONOMY . '_description' => [
'label' => esc_html__( 'Biographical Info', 'pressbooks' ),
'tag' => self::TAXONOMY . '-biography',
'input_type' => 'tinymce',
],
self::TAXONOMY . '_institution' => [
'label' => esc_html__( 'Institution', 'pressbooks' ),
'tag' => self::TAXONOMY . '-institution',
'input_type' => 'text',
'description' => esc_html__( 'Institution this contributor is associated with, e.g. Rebus Foundation, Open University, Amnesty International.', 'pressbooks' ),
'sanitization_method' => 'sanitize_text_field',
],
self::TAXONOMY . '_user_url' => [
'label' => esc_html__( 'Website', 'presbooks' ),
'tag' => self::TAXONOMY . '-website',
'input_type' => 'text',
'description' => esc_html__( 'Website for this contributor. Must be a valid URL.', 'pressbooks' ),
'sanitization_method' => '\Pressbooks\Sanitize\validate_url_field',
],
self::TAXONOMY . '_twitter' => [
'label' => esc_html__( 'Twitter', 'pressbooks' ),
'tag' => self::TAXONOMY . '-twitter',
'input_type' => 'text',
'description' => esc_html__( 'Twitter profile for this contributor. Must be a valid URL.', 'pressbooks' ),
'sanitization_method' => '\Pressbooks\Sanitize\validate_url_field',
],
self::TAXONOMY . '_linkedin' => [
'label' => esc_html__( 'LinkedIn', 'pressbooks' ),
'tag' => self::TAXONOMY . '-linkedin',
'input_type' => 'text',
'description' => esc_html__( 'LinkedIn profile for this contributor. Must be a valid URL.', 'pressbooks' ),
'sanitization_method' => '\Pressbooks\Sanitize\validate_url_field',
],
self::TAXONOMY . '_github' => [
'label' => esc_html__( 'GitHub', 'pressbooks' ),
'tag' => self::TAXONOMY . '-github',
'input_type' => 'text',
'description' => esc_html__( 'GitHub profile for this contributor. Must be a valid URL.', 'pressbooks' ),
'sanitization_method' => '\Pressbooks\Sanitize\validate_url_field',
],
];
return array_key_exists( self::TAXONOMY . '_' . $field, $allowed_fields ) ?
$allowed_fields[ self::TAXONOMY . '_' . $field ] :
$allowed_fields;
}
/**
* Get the list of fields that should be exported.
*
* @return array
*/
public function getTransferableFields() {
return array_keys( self::getContributorFields() );
}
/**
* Get the list of fields that should be stored as URLs.
*
* @return array
*/
public function getUrlFields() {
$fields = self::getContributorFields();
return array_keys(
array_filter(
$fields, function( $field ) {
if ( ! isset( $field['sanitization_method'] ) ) {
return false;
}
return $field['sanitization_method'] === '\Pressbooks\Sanitize\validate_url_field';
}
)
);
}
/**
* Sanitize input when importing data.
*
* @param string $name
* @param string $value
* @return string
*/
public function sanitizeField( $name, $value ) {
$field = self::getContributorFields( str_replace( self::TAXONOMY . '_', '', $name ) );
// if the given field does not have a sanitization method we simply return the given value.
if ( ! isset( $field['sanitization_method'] ) ) {
return $value;
}
// If the given field is a URL, we return the given value if it's a valid URL and an empty string otherwise.
if ( in_array( $name, $this->getUrlFields(), true ) ) {
return $field['sanitization_method']( $value ) ? $value : '';
}
// Apply the sanitization method.
return $field['sanitization_method']( $value );
}
/**
* Returns the form title and the hint for the file input.
*
* @return array
*/
public function getFormMessages() {
$guide_chapter = esc_url( 'https://guide.pressbooks.com/chapter/creating-and-displaying-contributors/#importingcontributors' );
$hint = __( 'Import multiple contributors at once by uploading a valid JSON file. See <a href="%s" target="_blank">our guide</a> for details.', 'pressbooks' );
return [
'title' => '<h2>' . esc_html__( 'Import Contributors', 'pressbooks' ) . '</h2>',
'hint' => '<p>' . sprintf( $hint, $guide_chapter ) . '</p>',
];
}
/**
* Create a matching Contributor term for a given User ID. Used when a user is added to a blog.
*
* @param int $user_id
*
* @return array|false An array containing the `term_id` and `term_taxonomy_id`, false otherwise.
*/
public function addBlogUser( $user_id ) {
$user = get_userdata( $user_id );
if ( $user && user_can( $user, 'edit_posts' ) ) {
$slug = $user->user_nicename;
$name = trim( "{$user->first_name} {$user->last_name}" );
if ( empty( $name ) ) {
$name = $user->display_name;
if ( empty( $name ) ) {
$name = $slug;
}
}
$results = wp_insert_term(
$name, self::TAXONOMY, [
'slug' => $slug,
]
);
if ( is_array( $results ) ) {
$contributors_terms = array_keys( self::getContributorFields() );
foreach ( $contributors_terms as $term ) {
$user_term = str_replace( 'contributor_', '', $term );
if ( $user_term === 'picture' ) {
// handle gravatar profile picture
$hash = md5( strtolower( $user->user_email ) );
$src = \Pressbooks\Utility\handle_image_upload( "https://secure.gravatar.com/avatar/{$hash}?s=400&d=404" );
if ( $src ) {
add_term_meta( $results['term_id'], $term, $src, true );
}
continue;
}
if ( $user->$user_term ) {
add_term_meta( $results['term_id'], $term, $user->$user_term, true );
}
}
return $results;
}
}
return false;
}
/**
* Update a matching Contributor term given a User ID. Used when a blog user is updated
*
* @param int $user_id
* @param \WP_User $old_user_data
*
* @return array|false An array containing the `term_id` and `term_taxonomy_id`, false otherwise.
*/
public function updateBlogUser( $user_id, $old_user_data ) {
$user = get_userdata( $user_id );
if ( $user && user_can( $user, 'edit_posts' ) ) {
$slug = $user->user_nicename;
$name = trim( "{$user->first_name} {$user->last_name}" );
if ( empty( $name ) ) {
$name = $user->display_name;
if ( empty( $name ) ) {
$name = $slug;
}
}
$term = get_term_by( 'slug', $old_user_data->user_nicename, self::TAXONOMY );
if ( $term ) {
$results = wp_update_term(
$term->term_id, self::TAXONOMY, [
'name' => $name,
'slug' => $slug,
]
);
update_term_meta( $results['term_id'], 'contributor_first_name', $user->first_name );
update_term_meta( $results['term_id'], 'contributor_last_name', $user->last_name );
} else {
$results = wp_insert_term(
$name, self::TAXONOMY, [
'slug' => $slug,
]
);
add_term_meta( $results['term_id'], 'contributor_first_name', $user->first_name, true );
add_term_meta( $results['term_id'], 'contributor_last_name', $user->last_name, true );
}
if ( is_array( $results ) ) {
return $results;
}
}
return false;
}
/**
* Get personal name based on available data
*
* Returns empty string is we can't find anything useful
*
* A personal name is the set of names by which an individual is known and that can be recited as a word-group,
* with the understanding that, taken together, they all relate to that one individual. In many cultures, the
* term is synonymous with the birth name or legal name of the individual.
*
* @param string $slug
*
* @return string
*/
public function personalName( $slug ) {
init_book_data_models();
$name = '';
$term = get_term_by( 'slug', $slug, self::TAXONOMY );
if ( $term ) {
$prefix = get_term_meta( $term->term_id, 'contributor_prefix', true );
$suffix = get_term_meta( $term->term_id, 'contributor_suffix', true );
$first_name = get_term_meta( $term->term_id, 'contributor_first_name', true );
$last_name = get_term_meta( $term->term_id, 'contributor_last_name', true );
if ( ! empty( $first_name ) && ! empty( $last_name ) ) {
$name = $prefix ? "{$prefix} {$first_name} {$last_name}" : "{$first_name} {$last_name}";
if ( ! empty( $suffix ) ) {
$does_contains_roman_number = false;
$roman_numbers_suffix = [ 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII' ];
foreach ( $roman_numbers_suffix as $roman_number ) {
if ( strpos( $suffix, $roman_number ) !== false ) {
$does_contains_roman_number = true;
break;
}
}
$suffix = $does_contains_roman_number ? " $suffix" : ", $suffix";
} else {
$suffix = '';
}
$name = $suffix ? "{$name}{$suffix}" : $name;
} elseif ( ! empty( $term->name ) ) {
$name = $term->name;
}
}
return $name;
}
/**
* @param string $old_slug
*
* @return string
*/
public function maybeUpgradeSlug( $old_slug ) {
$map = [
'pb_author' => 'pb_authors',
'pb_section_author' => 'pb_authors',
'pb_contributing_authors' => 'pb_contributors',
'pb_author_file_as' => 'pb_authors',
'pb_editor' => 'pb_editors',
'pb_translator' => 'pb_translators',
];
if ( isset( $map[ $old_slug ] ) ) {
return $map[ $old_slug ];
}
return $old_slug;
}
/**
* @param string $old_slug
* @param string|array $names
* @param int $post_id
*
* @return array|false An array containing `term_id` and `term_taxonomy_id`, false otherwise.
*/
public function convert( $old_slug, $names, $post_id ) {
$new_slug = $this->maybeUpgradeSlug( $old_slug );
if ( $new_slug === $old_slug ) {
return false; // Nothing to convert
}
if ( ! is_array( $names ) ) {
$names = [ $names ];
}
$result = false;
foreach ( $names as $contributors ) {
$values = explode_remove_and( ';', $contributors );
foreach ( $values as $v ) {
$result = $this->insert( $v, $post_id, $new_slug );
}
}
return $result;
}
/**
* @param int $post_id
* @param string $contributor_type
*
* @return array An array containing a set of matching contributor arrays
*/
public function getContributorsWithMeta( $post_id, $contributor_type ) {
if ( ! str_starts_with( $contributor_type, 'pb_' ) ) {
$contributor_type = 'pb_' . $contributor_type;
}
if ( ! $this->isValid( $contributor_type ) ) {
return [];
}
$full_contributors = [];
$contributors = get_post_meta( $post_id, $contributor_type, false );
foreach ( $contributors as $key => $contributor ) {
$term = get_term_by( 'slug', $contributor, self::TAXONOMY );
if ( $term ) {
foreach ( self::getContributorFields() as $field => $value ) {
$full_contributors[ $key ]['name'] = $this->personalName( $contributor );
$full_contributors[ $key ][ $field ] = get_term_meta( $term->term_id, $field, true );
}
}
}
return $full_contributors;
}
/**
* This function returns an array with all the contributors registered in a book ordered by $valid array default ordering.
* @return array
*/
public function getAllContributors() {
$meta = new Metadata();
$meta_post = $meta->getMetaPost();
$records = [];
foreach ( $this->valid as $contributor_type ) {
$contributors = $this->getContributorsWithMeta( $meta_post->ID, $contributor_type );
$contributors_count = count( $contributors );
if ( $contributors_count > 0 ) {
$records[ $contributor_type ] = [
'title' => $this->getContributorTypeLabel( $contributor_type, $contributors_count ),
'records' => $contributors,
];
}
}
return $records;
}
public function getContributorTypeLabel( string $type, int $count ): string {
return match ($type) {
'pb_editors' => _n( 'Editor', 'Editors', $count, 'pressbooks' ),
'pb_authors' => _n( 'Author', 'Authors', $count, 'pressbooks' ),
'pb_contributors' => _n( 'Contributor', 'Contributors', $count, 'pressbooks' ),
'pb_translators' => _n( 'Translator', 'Translators', $count, 'pressbooks' ),
'pb_reviewers' => _n( 'Reviewer', 'Reviewers', $count, 'pressbooks' ),
'pb_illustrators' => _n( 'Illustrator', 'Illustrators', $count, 'pressbooks' ),
default => '',
};
}
/**
* Automatically displays a contributors page if the back-matter content is empty.
*
* @param string $content
*
* @return string
*/
public function overrideDisplay( $content ) {
return $this->display(
$content, function() {
$blade = Container::get( 'Blade' );
return $blade->render(
'posttypes/contributors', [
'contributors' => $this->getAllContributors(),
'exporting' => $this->exporting,
]
);
}, 'contributors'
);
}
public static function changeContributorName( \WP_Roles $roles ) {
$roles->roles['contributor']['name'] = 'Collaborator';
$roles->role_names['contributor'] = 'Collaborator';
}
}