Passed
Push — master ( f510e9...9822a2 )
by Paul
10:29
created

AssignedPostsMetabox   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 16
dl 0
loc 33
ccs 0
cts 23
cp 0
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 16 1
A register() 0 6 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Metaboxes;
4
5
use GeminiLabs\SiteReviews\Contracts\MetaboxContract;
6
use GeminiLabs\SiteReviews\Database\Query;
7
use GeminiLabs\SiteReviews\Helper;
8
use GeminiLabs\SiteReviews\Modules\Html\Template;
9
use GeminiLabs\SiteReviews\Review;
10
11
class AssignedPostsMetabox implements MetaboxContract
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function register($post)
17
    {
18
        if (Review::isReview($post)) {
19
            $id = glsr()->post_type.'-postsdiv';
20
            $title = _x('Assigned Posts', 'admin-text', 'site-reviews');
21
            add_meta_box($id, $title, [$this, 'render'], null, 'side');
22
        }
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function render($post)
29
    {
30
        $review = glsr(Query::class)->review($post->ID);
31
        wp_nonce_field('assigned_posts', '_nonce-assigned-posts', false);
32
        $templates = array_reduce($review->assigned_posts, function ($carry, $postId) {
33
            return $carry.glsr(Template::class)->build('partials/editor/assigned-entry', [
34
                'context' => [
35
                    'data.id' => $postId,
36
                    'data.name' => 'post_ids[]',
37
                    'data.url' => (string) get_permalink($postId),
38
                    'data.title' => Helper::ifEmpty(get_the_title($postId), _x('(no title)', 'admin-text', 'site-reviews')),
39
                ],
40
            ]);
41
        });
42
        glsr()->render('partials/editor/metabox-assigned-posts', [
43
            'templates' => $templates,
44
        ]);
45
    }
46
}
47