• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

timber / timber / 5690057835

pending completion
5690057835

push

github

nlemoine
Merge branch '2.x' of github.com:timber/timber into 2.x-refactor-file-models

# Conflicts:
#	src/Attachment.php
#	src/ExternalImage.php
#	src/FileSize.php
#	src/URLHelper.php

1134 of 1134 new or added lines in 55 files covered. (100.0%)

3923 of 4430 relevant lines covered (88.56%)

59.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.0
/src/Integration/CoAuthorsPlusIntegration.php
1
<?php
2

3
namespace Timber\Integration;
4

5
use CoAuthors_Plus;
6
use Timber\Integration\CoAuthorsPlus\CoAuthorsPlusUser;
7

8
use WP_User;
9

10
class CoAuthorsPlusIntegration implements IntegrationInterface
11
{
12
    public function should_init(): bool
13
    {
14
        return \class_exists(CoAuthors_Plus::class);
×
15
    }
16

17
    /**
18
     * @codeCoverageIgnore
19
     */
20
    public function init(): void
21
    {
22
        \add_filter('timber/post/authors', [$this, 'authors'], 10, 2);
23

24
        \add_filter('timber/user/class', function ($class, WP_User $user) {
25
            return CoAuthorsPlusUser::class;
26
        }, 10, 2);
27
    }
28

29
    /**
30
     * Filters {{ post.authors }} to return authors stored from Co-Authors Plus
31
     * @since 1.1.4
32
     * @param array $author
33
     * @param \Timber\Post $post
34
     * @return array of User objects
35
     */
36
    public function authors($author, $post)
37
    {
38
        $authors = [];
5✔
39
        $cauthors = \get_coauthors($post->ID);
5✔
40
        foreach ($cauthors as $author) {
5✔
41
            $uid = $this->get_user_uid($author);
5✔
42
            if ($uid) {
5✔
43
                $authors[] = \Timber\Timber::get_user($uid);
3✔
44
            } else {
45
                $wp_user = new WP_User($author);
4✔
46
                $user = \Timber\Timber::get_user($wp_user);
4✔
47
                $user->import($wp_user->data);
4✔
48
                unset($user->user_pass);
4✔
49
                $user->id = $user->ID = (int) $wp_user->ID;
4✔
50
                $authors[] = $user;
4✔
51
            }
52
        }
53
        return $authors;
5✔
54
    }
55

56
    /**
57
     * return the user id for normal authors
58
     * the user login for guest authors if it exists and self::prefer_users == true
59
     * or null
60
     * @internal
61
     * @param object $cauthor
62
     * @return int|null
63
     */
64
    protected function get_user_uid($cauthor)
65
    {
66
        // if is guest author
67
        if (\is_object($cauthor) && isset($cauthor->type) && $cauthor->type == 'guest-author') {
5✔
68
            // if have have a linked user account
69
            global $coauthors_plus;
70
            if (!$coauthors_plus->force_guest_authors && isset($cauthor->linked_account) && !empty($cauthor->linked_account)) {
4✔
71
                $wp_user = \get_user_by('slug', $cauthor->linked_account);
1✔
72
                return $wp_user->ID;
1✔
73
            } else {
74
                return null;
4✔
75
            }
76
        } else {
77
            return $cauthor->ID;
2✔
78
        }
79
    }
80
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc