PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.12.2
Translate and Go multilingual – Automatic AI translation – wpLingua v2.12.2
2.16.7 2.16.6 2.16.5 2.16.4 2.16.3 2.16.2 2.16.1 2.16.0 2.15.2 2.15.1 2.15.0 2.14.3 2.14.2 2.14.1 2.14.0 2.13.1 2.13.0 2.12.3 2.12.2 2.12.1 trunk 1.0.3 1.0.4 1.0.5 1.1.0 All 112 releases
wplingua / inc / admin / translation-edit-modal.php

translation-edit-modal.php in Translate and Go multilingual – Automatic AI translation – wpLingua 2.12.2, at inc/admin/translation-edit-modal.php

182 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // If this file is called directly, abort.
4 if ( ! defined( 'WPINC' ) ) {
5 die;
6 }
7
8
9 /**
10 * Get HTML of translation edit modal
11 *
12 * @return void
13 */
14 function wplng_translation_edit_modal_get_html() {
15
16 $return_button = '<a';
17 $return_button .= ' href="javascript:void(0);"';
18 $return_button .= ' title="' . esc_attr__( 'Return on page', 'wplingua' ) . '"';
19 $return_button .= ' class="wplng-button-icon wplng-button-return"';
20 $return_button .= ' id="wplng-modal-edit-return"';
21 $return_button .= '>';
22 $return_button .= '<span class="dashicons dashicons-no"></span>';
23 $return_button .= '</a>';
24
25 $all_languages_button = '<a';
26 $all_languages_button .= ' href="javascript:void(0);"';
27 $all_languages_button .= ' id="wplng-modal-edit-show-all"';
28 $all_languages_button .= '>';
29 $all_languages_button .= esc_attr__( 'All languages', 'wplingua' );
30 $all_languages_button .= '</a>';
31
32 $edit_link_template = add_query_arg(
33 array(
34 'post' => 'WPLNG_TRANSLATION_ID',
35 'action' => 'edit',
36 ),
37 get_admin_url() . 'post.php'
38 );
39
40 $edit_link_button = '<a';
41 $edit_link_button .= ' href="#"';
42 $edit_link_button .= ' title="' . esc_attr__( 'Open edit page', 'wplingua' ) . '"';
43 $edit_link_button .= ' class="wplng-button-icon"';
44 $edit_link_button .= ' id="wplng-modal-edit-post"';
45 $edit_link_button .= ' target="_blank"';
46 $edit_link_button .= ' data-wplng-edit-template="' . esc_attr( $edit_link_template ) . '"';
47 $edit_link_button .= '>';
48 $edit_link_button .= '<span class="dashicons dashicons-external"></span>';
49 $edit_link_button .= '</a>';
50
51 $html = '<div id="wplng-modal-edit-container">';
52
53 $html .= '<div id="wplng-modal-edit">';
54
55 $html .= '<div id="wplng-modal-header">';
56 $html .= '<span id="wplng-modal-title">';
57 $html .= '<span class="dashicons dashicons-translation wplng-modal-header-icon"></span> ';
58 $html .= esc_html__( 'Edit translation', 'wplingua' );
59 $html .= '</span>';
60 $html .= $all_languages_button;
61 $html .= $edit_link_button;
62 $html .= $return_button;
63
64 $html .= '</div>';
65
66 $html .= '<div id="wplng-modal-edit-main">';
67 $html .= '<div id="wplng-translation-editor">';
68 $html .= '</div>'; // End #wplng-translation-editor
69
70 $html .= '<button id="wplng-modal-edit-save" disabled>';
71 $html .= esc_html__( 'Save', 'wplingua' );
72 $html .= '</button>';
73
74 $html .= '</div>'; // End #wplng-modal-edit-main
75 $html .= '</div>'; // End #wplng-modal-edit
76 $html .= '</div>'; // End #wplng-modal-edit-container
77
78 return $html;
79 }
80
81
82 /**
83 * Send the translation editor HTML for AJAX call
84 *
85 * @return void
86 */
87 function wplng_ajax_edit_modal() {
88
89 /**
90 * Check and sanitize data
91 */
92
93 if ( empty( $_POST['post_id'] )
94 || ! current_user_can( 'edit_post', $_POST['post_id'] )
95 ) {
96 wp_send_json_error( __( 'Invalid translation ID', 'wplingua' ) );
97 return;
98 }
99
100 /**
101 * Get and check the translation post
102 */
103
104 $translation_post = get_post( $_POST['post_id'] );
105
106 if ( empty( $translation_post ) ) {
107 wp_send_json_error( __( 'Invalid translation post', 'wplingua' ) );
108 return;
109 }
110
111 /**
112 * Get the translation editor HTML
113 */
114
115 $data['wplng_edit_html'] = esc_html(
116 wplng_translation_editor_get_html(
117 $translation_post
118 )
119 );
120
121 /**
122 * Send the translation editor HTML
123 */
124
125 wp_send_json_success(
126 wp_json_encode(
127 $data,
128 JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
129 )
130 );
131 }
132
133
134 /**
135 * Save translation on AJAX call of modal edit
136 *
137 * Process the AJAX request to save a translation from modal edit,
138 * validate and sanitize the input data, and call the function to
139 * save the translation.
140 *
141 * @return void
142 */
143 function wplng_ajax_save_modal() {
144
145 /**
146 * Check and sanitize data
147 *
148 * Check that the post ID and current user can edit the post
149 * before attempting to save the translation.
150 */
151
152 if ( empty( $_POST['post_id'] )
153 || ! current_user_can( 'edit_post', $_POST['post_id'] )
154 ) {
155 wp_send_json_error( __( 'Invalid translation ID', 'wplingua' ) );
156 return;
157 }
158
159 /**
160 * Try to save the post
161 *
162 * Call the function to save the translation and store the result
163 * in variable $saved.
164 */
165
166 $saved = wplng_translation_save_meta_boxes_data( $_POST['post_id'] );
167
168 /**
169 * Send AJAX success or error
170 *
171 * If the saving was successful, send a JSON success response.
172 * Otherwise, send a JSON error response with a message.
173 */
174
175 if ( $saved ) {
176 wp_send_json_success( null );
177 } else {
178 wp_send_json_error( __( 'Translation saving failed', 'wplingua' ) );
179 return;
180 }
181 }
182