Passed
Push — master ( bd5512...75cbf8 )
by Paul
04:33
created

Labels   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 9.38%

Importance

Changes 0
Metric Value
dl 0
loc 118
ccs 6
cts 64
cp 0.0938
rs 10
c 0
b 0
f 0
wmc 17

6 Methods

Rating   Name   Duplication   Size   Complexity  
A customizePostStatusLabels() 0 11 4
A canModifyTranslation() 0 7 4
A getStatusLabels() 0 14 2
A getReviewLabels() 0 15 1
B filterUpdateMessages() 0 24 3
A filterPostStatusLabels() 0 9 3
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Editor;
4
5
use GeminiLabs\SiteReviews\Application;
6
use WP_Post;
7
8
class Labels
9
{
10
	/**
11
	 * @return void
12
	 */
13
	public function customizePostStatusLabels()
14
	{
15
		global $wp_scripts;
16
		$strings = [
17
			'savePending' => __( 'Save as Unapproved', 'site-reviews' ),
18
			'published' => __( 'Approved', 'site-reviews' ),
19
		];
20
		if( $this->canModifyTranslation() && isset( $wp_scripts->registered['post']->extra['data'] )) {
21
			$l10n = &$wp_scripts->registered['post']->extra['data'];
22
			foreach( $strings as $search => $replace ) {
23
				$l10n = preg_replace( '/("'.$search.'":")([^"]+)/', "$1".$replace, $l10n );
24
			}
25
		}
26
	}
27
28
	/**
29
	 * @param string $translation
30
	 * @param string $test
31
	 * @param string $domain
32
	 * @return string
33
	 */
34 7
	public function filterPostStatusLabels( $translation, $text, $domain )
35
	{
36 7
		if( $this->canModifyTranslation( $domain )) {
37
			$replacements = $this->getStatusLabels();
38
			if( array_key_exists( $text, $replacements )) {
39
				$translation = $replacements[$text];
40
			}
41
		}
42 7
		return $translation;
43
	}
44
45
	/**
46
	 * @return array
47
	 */
48
	public function filterUpdateMessages( array $messages )
49
	{
50
		$post = get_post();
51
		if( !( $post instanceof WP_Post ))return;
52
		$strings = $this->getReviewLabels();
53
		$restored = filter_input( INPUT_GET, 'revision' );
54
		if( $revisionTitle = wp_post_revision_title( intval( $restored ), false )) {
55
			$restored = sprintf( $strings['restored'], $revisionTitle );
56
		}
57
		$scheduled_date = date_i18n( 'M j, Y @ H:i', strtotime( $post->post_date ));
58
		$messages[ Application::POST_TYPE ] = [
59
			 1 => $strings['updated'],
60
			 4 => $strings['updated'],
61
			 5 => $restored,
62
			 6 => $strings['published'],
63
			 7 => $strings['saved'],
64
			 8 => $strings['submitted'],
65
			 9 => sprintf( $strings['scheduled'], '<strong>'.$scheduled_date.'</strong>' ),
66
			10 => $strings['draft_updated'],
67
			50 => $strings['approved'],
68
			51 => $strings['unapproved'],
69
			52 => $strings['reverted'],
70
		];
71
		return $messages;
72
	}
73
74
	/**
75
	 * @param string $domain
76
	 * @return bool
77
	 */
78 7
	protected function canModifyTranslation( $domain = 'default' )
79
	{
80 7
		if( $domain != 'default' || empty( glsr_current_screen()->base )) {
81 7
			return false;
82
		}
83
		return get_current_screen()->post_type == Application::POST_TYPE
84
			&& in_array( get_current_screen()->base, ['edit', 'post'] );
85
	}
86
87
	/**
88
	 * @return array
89
	 */
90
	protected function getReviewLabels()
91
	{
92
		return [
93
			'approved' => __( 'Review has been approved and published.', 'site-reviews' ),
94
			'draft_updated' => __( 'Review draft updated.', 'site-reviews' ),
95
			'preview' => __( 'Preview review', 'site-reviews' ),
96
			'published' => __( 'Review approved and published.', 'site-reviews' ),
97
			'restored' => __( 'Review restored to revision from %s.', 'site-reviews' ),
98
			'reverted' => __( 'Review has been reverted to its original submission state.', 'site-reviews' ),
99
			'saved' => __( 'Review saved.', 'site-reviews' ),
100
			'scheduled' => __( 'Review scheduled for: %s.', 'site-reviews' ),
101
			'submitted' => __( 'Review submitted.', 'site-reviews' ),
102
			'unapproved' => __( 'Review has been unapproved and is now pending.', 'site-reviews' ),
103
			'updated' => __( 'Review updated.', 'site-reviews' ),
104
			'view' => __( 'View review', 'site-reviews' ),
105
		];
106
	}
107
108
	/**
109
	 * Store the labels to avoid unnecessary loops
110
	 * @return array
111
	 */
112
	protected function getStatusLabels()
113
	{
114
		static $labels;
115
		if( empty( $labels )) {
116
			$labels = [
117
				'Pending' => __( 'Unapproved', 'site-reviews' ),
118
				'Pending Review' => __( 'Unapproved', 'site-reviews' ),
119
				'Privately Published' => __( 'Privately Approved', 'site-reviews' ),
120
				'Publish' => __( 'Approve', 'site-reviews' ),
121
				'Published' => __( 'Approved', 'site-reviews' ),
122
				'Save as Pending' => __( 'Save as Unapproved', 'site-reviews' ),
123
			];
124
		}
125
		return $labels;
126
	}
127
}
128