| 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_html__( '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', absint( $_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( absint( $_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'] = wplng_translation_editor_get_html( |
| 116 |
$translation_post |
| 117 |
); |
| 118 |
|
| 119 |
/** |
| 120 |
* Send the translation editor HTML |
| 121 |
*/ |
| 122 |
|
| 123 |
wp_send_json_success( |
| 124 |
wp_json_encode( |
| 125 |
$data, |
| 126 |
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES |
| 127 |
) |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/** |
| 133 |
* Save translation on AJAX call of modal edit |
| 134 |
* |
| 135 |
* Process the AJAX request to save a translation from modal edit, |
| 136 |
* validate and sanitize the input data, and call the function to |
| 137 |
* save the translation. |
| 138 |
* |
| 139 |
* @return void |
| 140 |
*/ |
| 141 |
function wplng_ajax_save_modal() { |
| 142 |
|
| 143 |
/** |
| 144 |
* Check and sanitize data |
| 145 |
* |
| 146 |
* Check that the post ID and current user can edit the post |
| 147 |
* before attempting to save the translation. |
| 148 |
*/ |
| 149 |
|
| 150 |
if ( empty( $_POST['post_id'] ) |
| 151 |
|| ! current_user_can( 'edit_post', absint( $_POST['post_id'] ) ) |
| 152 |
) { |
| 153 |
wp_send_json_error( __( 'Invalid translation ID', 'wplingua' ) ); |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Try to save the post |
| 159 |
* |
| 160 |
* Call the function to save the translation and store the result |
| 161 |
* in variable $saved. |
| 162 |
*/ |
| 163 |
|
| 164 |
$saved = wplng_translation_save_meta_boxes_data( absint( $_POST['post_id'] ) ); |
| 165 |
|
| 166 |
/** |
| 167 |
* Send AJAX success or error |
| 168 |
* |
| 169 |
* If the saving was successful, send a JSON success response. |
| 170 |
* Otherwise, send a JSON error response with a message. |
| 171 |
*/ |
| 172 |
|
| 173 |
if ( $saved ) { |
| 174 |
wp_send_json_success( null ); |
| 175 |
} else { |
| 176 |
wp_send_json_error( __( 'Translation saving failed', 'wplingua' ) ); |
| 177 |
return; |
| 178 |
} |
| 179 |
} |
| 180 |
|