DownloadCsvTemplate::required()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Commands;
4
5
use GeminiLabs\League\Csv\CannotInsertRecord;
6
use GeminiLabs\League\Csv\EscapeFormula;
7
use GeminiLabs\League\Csv\Writer;
8
use GeminiLabs\SiteReviews\Modules\Notice;
9
use GeminiLabs\SiteReviews\Modules\Rating;
10
11
class DownloadCsvTemplate extends AbstractCommand
12
{
13
    public function data(): array
14
    {
15
        return [ // order is intentional
16
            'date' => '2023-01-13 12:01:13',
17
            'date_gmt' => '',
18
            'rating' => 5,
19
            'title' => 'Eclectic, Cozy, and Highly Recommended!',
20
            'content' => 'I will definitely stay here again. It was a wonderful experience!',
21
            'name' => 'Matt Mullenweg',
22
            'email' => '[email protected]',
23
            'avatar' => 'https://gravatar.com/avatar/767fc9c115a1b989744c755db47feb60?s=128',
24
            'ip_address' => '198.143.164.252',
25
            'terms' => 1,
26
            'author_id' => '',
27
            'is_approved' => 1,
28
            'is_pinned' => 0,
29
            'is_verified' => 0,
30
            'response' => '',
31
            'assigned_posts' => '',
32
            'assigned_terms' => '',
33
            'assigned_users' => '',
34
            'score' => 0,
35
        ];
36
    }
37
38
    public function handle(): void
39
    {
40
        try {
41
            $writer = Writer::createFromString('');
0 ignored issues
show
Deprecated Code introduced by
The function GeminiLabs\League\Csv\Ab...Csv::createFromString() has been deprecated: since version 9.27.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

41
            $writer = /** @scrutinizer ignore-deprecated */ Writer::createFromString('');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
42
            $writer->addFormatter(new EscapeFormula());
43
            $writer->insertOne(array_keys($this->data()));
44
            $writer->insertOne(array_values($this->data()));
45
            nocache_headers();
46
            $writer->output('reviews-template.csv');
0 ignored issues
show
Deprecated Code introduced by
The function GeminiLabs\League\Csv\AbstractCsv::output() has been deprecated: since version 9.18.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

46
            /** @scrutinizer ignore-deprecated */ $writer->output('reviews-template.csv');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
47
            exit;
48
        } catch (CannotInsertRecord $e) {
49
            $this->fail();
50
            glsr(Notice::class)->addError($e->getMessage());
51
            glsr_log()
52
                ->warning('Unable to insert row into CSV template file')
53
                ->debug($e->getRecord());
54
        }
55
    }
56
57
    public function required(): array
58
    {
59
        return [
60
            'date', 'rating',
61
        ];
62
    }
63
64
    public function tableColumns(): array
65
    {
66
        return [
67
            'default' => [
68
                'assigned_posts' => _x('The Post ID or "post_type:slug" of the page that the review is assigned to (separate multiple values with a comma)', 'admin-text', 'site-reviews'),
69
                'assigned_terms' => _x('The Term ID or "slug" of the category that the review is assigned to (separate multiple values with a comma)', 'admin-text', 'site-reviews'),
70
                'assigned_users' => _x('The User ID or "username" of the user that the review is assigned to (separate multiple values with a comma)', 'admin-text', 'site-reviews'),
71
                'author_id' => _x('The User ID or "username" of the reviewer', 'admin-text', 'site-reviews'),
72
                'avatar' => _x('The avatar URL of the reviewer', 'admin-text', 'site-reviews'),
73
                'content' => _x('The review', 'admin-text', 'site-reviews'),
74
                'date' => _x('The review date', 'admin-text', 'site-reviews'),
75
                'date_gmt' => _x('The review GMT date', 'admin-text', 'site-reviews'),
76
                'email' => _x('The reviewer\'s email', 'admin-text', 'site-reviews'),
77
                'ip_address' => _x('The IP address of the reviewer', 'admin-text', 'site-reviews'),
78
                'is_approved' => sprintf(_x('%s or %s', 'admin-text', 'site-reviews'), 'TRUE', 'FALSE'),
79
                'is_pinned' => sprintf(_x('%s or %s', 'admin-text', 'site-reviews'), 'TRUE', 'FALSE'),
80
                'is_verified' => sprintf(_x('%s or %s', 'admin-text', 'site-reviews'), 'TRUE', 'FALSE'),
81
                'name' => _x('The reviewer\'s name', 'admin-text', 'site-reviews'),
82
                'rating' => sprintf(_x('A number from %d-%d', 'admin-text', 'site-reviews'), Rating::min(), Rating::max()),
83
                'response' => _x('The review response', 'admin-text', 'site-reviews'),
84
                'terms' => sprintf(_x('%s or %s', 'admin-text', 'site-reviews'), 'TRUE', 'FALSE'),
85
                'title' => _x('The title of the review', 'admin-text', 'site-reviews'),
86
            ],
87
            'site-reviews-actions' => [
88
                'language' => sprintf(_x('The ISO 639-1 language code of the review. See %s for a list of all supported languages.', 'admin-text', 'site-reviews'), '<a href="https://developers.deepl.com/docs/getting-started/supported-languages#translation-source-languages" target="_blank">DeepL</a>'),
89
                'score' => _x('The number of times the review was upvoted.', 'admin-text', 'site-reviews'),
90
            ],
91
            'site-reviews-forms' => [
92
                'custom_*' => _x('The value of a custom review field (replace the asterisk <code>*</code> of the column name with the name of your custom field).', 'admin-text', 'site-reviews'),
93
                'form' => _x('The Post ID or slug of the Review Form used to submit the review', 'admin-text', 'site-reviews'),
94
            ],
95
            'site-reviews-images' => [
96
                'images' => _x('The URLs of the review images (separate multiple URLs with a pipe "|" character or comma)', 'admin-text', 'site-reviews'),
97
            ],
98
            'site-reviews-themes' => [
99
                'type' => _x('The lowercase name of the platform that the review was exported from (only use this if the review was exported from another review platform, i.e. google, tripadvisor, etc.)', 'admin-text', 'site-reviews'),
100
                'url' => _x('The review URL (only use this if the review was exported from another review platform, i.e. google, tripadvisor, etc.)', 'admin-text', 'site-reviews'),
101
            ],
102
        ];
103
    }
104
105
    public function tableData(): array
106
    {
107
        $data = [];
108
        foreach ($this->tableColumns() as $group => $columns) {
109
            foreach ($columns as $name => $description) {
110
                $required = in_array($name, $this->required())
111
                    ? sprintf('<span class="glsr-tag glsr-tag-required">%s</span>', _x('Yes', 'admin-text', 'site-reviews'))
112
                    : sprintf('<span class="glsr-tag">%s</span>', _x('No', 'admin-text', 'site-reviews'));
113
                $notice = '';
114
                if ('default' !== $group) {
115
                    $text = _x('%s addon required.', 'link to addon page (admin-text)', 'site-reviews');
116
                    $notice = sprintf('<div class="glsr-notice-inline components-notice is-warning">%s</div>',
117
                        sprintf($text, glsr_premium_link($group))
118
                    );
119
                }
120
                $data[$name] = compact('description', 'notice', 'required');
121
            }
122
        }
123
        ksort($data);
124
        return $data;
125
    }
126
}
127