wpml-tf-frontend-feedback-view.php
6.2 KB
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
<?php
/**
* Class WPML_TF_Frontend_Feedback_View
*
* @author OnTheGoSystems
*/
class WPML_TF_Frontend_Feedback_View {
const TEMPLATE_FOLDER = '/templates/translation-feedback/frontend/';
const FORM_TEMPLATE = 'feedback-form.twig';
const OPEN_TEMPLATE = 'feedback-open-button.twig';
const CUSTOM_OPEN_LINK_TEMPLATE = 'feedback-custom-open-link.twig';
const JS_OPEN_NODE_CLASS = 'js-wpml-tf-feedback-icon';
/** @var IWPML_Template_Service $template_service */
private $template_service;
/** @var SitePress $sitepress */
private $sitepress;
/** @var WPML_Queried_Object $queried_object */
private $queried_object;
/** @var WPML_TF_Settings $settings */
private $settings;
/**
* WPML_TF_Frontend_Hooks constructor.
*
* @param IWPML_Template_Service $template_service
* @param SitePress $sitepress
* @param WPML_Queried_Object $queried_object
* @param WPML_TF_Settings $settings
*/
public function __construct(
IWPML_Template_Service $template_service,
SitePress $sitepress,
WPML_Queried_Object $queried_object,
WPML_TF_Settings $settings
) {
$this->template_service = $template_service;
$this->sitepress = $sitepress;
$this->queried_object = $queried_object;
$this->settings = $settings;
}
/**
* @return string
*/
public function render_form() {
$jsAssets = [
includes_url() . 'js/jquery/jquery.min.js',
includes_url() . 'js/jquery/jquery-migrate.min.js',
includes_url() . 'js/jquery/ui/core.min.js',
includes_url() . 'js/jquery/ui/mouse.min.js',
includes_url() . 'js/jquery/ui/resizable.min.js',
includes_url() . 'js/jquery/ui/draggable.min.js',
includes_url() . 'js/jquery/ui/controlgroup.min.js',
includes_url() . 'js/jquery/ui/checkboxradio.min.js',
includes_url() . 'js/jquery/ui/button.min.js',
includes_url() . 'js/jquery/ui/dialog.min.js',
includes_url() . 'js/underscore.min.js',
includes_url() . 'js/wp-util.min.js',
];
$model = array(
'strings' => array(
'dialog_title' => __( 'Rate translation', 'sitepress' ),
'thank_you_rating' => __( 'Thank you for your rating!', 'sitepress' ),
'thank_you_comment' => __( 'Thank you for your rating and comment!', 'sitepress' ),
'translated_from' => __( 'This page was translated from:', 'sitepress' ),
'please_rate' => __( 'Please rate this translation:', 'sitepress' ),
'your_rating' => __( 'Your rating:', 'sitepress' ),
'star5_title' => __( 'It is perfect!', 'sitepress' ),
'star4_title' => __( 'It is OK', 'sitepress' ),
'star3_title' => __( 'It could be improved', 'sitepress' ),
'star2_title' => __( 'I can see a lot of language errors', 'sitepress' ),
'star1_title' => __( "I can't understand anything", 'sitepress' ),
'change_rating' => __( 'Change', 'sitepress' ),
'error_examples' => __( 'Please give some examples of errors and how would you improve them:', 'sitepress' ),
'send_button' => __( 'Send', 'sitepress' ),
'honeypot_label' => __( 'If you are a human, do not fill in this field.', 'sitepress' ),
),
'flag_url' => $this->sitepress->get_flag_url( $this->queried_object->get_source_language_code() ),
'language_name' => $this->sitepress->get_display_language_name( $this->queried_object->get_source_language_code() ),
'document_id' => $this->queried_object->get_id(),
'document_type' => $this->queried_object->get_element_type(),
'action' => WPML_TF_Frontend_AJAX_Hooks_Factory::AJAX_ACTION,
'nonce' => wp_create_nonce( WPML_TF_Frontend_AJAX_Hooks_Factory::AJAX_ACTION ),
'source_url' => $this->queried_object->get_source_url(),
'asset_url' => [
'js' => implode(
';',
$jsAssets
),
],
'ajax' => array(
'url' => admin_url( 'admin-ajax.php', 'relative' ),
),
);
return $this->template_service->show( $model, self::FORM_TEMPLATE );
}
/**
* @return string
*/
public function render_open_button() {
$rendering = '';
if ( WPML_TF_Settings::BUTTON_MODE_CUSTOM !== $this->settings->get_button_mode() ) {
$model = array(
'strings' => array(
'form_open_title' => __( 'Rate translation of this page', 'sitepress' ),
),
'wrapper_css_classes' => $this->get_wrapper_css_classes(),
'icon_css_class' => $this->get_icon_css_class(),
);
$rendering = $this->template_service->show( $model, self::OPEN_TEMPLATE );
}
return $rendering;
}
/** @return string */
private function get_wrapper_css_classes() {
$base_classes = self::JS_OPEN_NODE_CLASS . ' wpml-tf-feedback-icon ';
$class = $base_classes . 'wpml-tf-feedback-icon-left';
if ( WPML_TF_Settings::BUTTON_MODE_RIGHT === $this->settings->get_button_mode() ) {
$class = $base_classes . 'wpml-tf-feedback-icon-right';
}
return $class;
}
/** @return string */
private function get_icon_css_class() {
$icon_style = $this->settings->get_icon_style();
$css_classes = self::get_icon_css_classes();
if ( array_key_exists( $icon_style, $css_classes ) ) {
return $css_classes[ $icon_style ];
}
return $css_classes[ WPML_TF_Settings::ICON_STYLE_LEGACY ];
}
/**
* @param string|array $args
*
* @return string
*/
public function render_custom_open_link( $args ) {
$model = wp_parse_args( $args, self::get_default_arguments_for_open_link() );
$model['classes'] = self::JS_OPEN_NODE_CLASS . ' ' . $model['classes'];
return $this->template_service->show( $model, self::CUSTOM_OPEN_LINK_TEMPLATE );
}
/** @return array */
public static function get_default_arguments_for_open_link() {
return array(
'title' => __( 'Rate translation of this page', 'sitepress' ),
'classes' => 'wpml-tf-feedback-custom-link',
);
}
/** @return array */
public static function get_icon_css_classes() {
return array(
WPML_TF_Settings::ICON_STYLE_LEGACY => 'otgs-ico-translation',
WPML_TF_Settings::ICON_STYLE_STAR => 'otgs-ico-star',
WPML_TF_Settings::ICON_STYLE_THUMBSUP => 'otgs-ico-thumbsup',
WPML_TF_Settings::ICON_STYLE_BULLHORN => 'otgs-ico-bullhorn',
WPML_TF_Settings::ICON_STYLE_COMMENT => 'otgs-ico-comment',
WPML_TF_Settings::ICON_STYLE_QUOTE => 'otgs-ico-quote',
);
}
}