polylang.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | add_action( |
| 4 | 'rest_api_init', |
| 5 | function () { |
| 6 | $namespace = 'kubio/v1/polylang'; |
| 7 | |
| 8 | register_rest_route( |
| 9 | $namespace, |
| 10 | '/add-page-translation', |
| 11 | array( |
| 12 | 'methods' => 'POST', |
| 13 | 'callback' => 'kubio_api_polylang_add_page_translation', |
| 14 | 'permission_callback' => function () { |
| 15 | return current_user_can( 'edit_theme_options' ); |
| 16 | }, |
| 17 | ) |
| 18 | ); |
| 19 | } |
| 20 | ); |
| 21 | |
| 22 | add_action( |
| 23 | 'rest_api_init', |
| 24 | function () { |
| 25 | $namespace = 'kubio/v1/polylang'; |
| 26 | |
| 27 | register_rest_route( |
| 28 | $namespace, |
| 29 | '/add-template-translation', |
| 30 | array( |
| 31 | 'methods' => 'POST', |
| 32 | 'callback' => 'kubio_api_polylang_add_template_translation', |
| 33 | 'permission_callback' => function () { |
| 34 | return current_user_can( 'edit_theme_options' ); |
| 35 | }, |
| 36 | ) |
| 37 | ); |
| 38 | } |
| 39 | ); |
| 40 | |
| 41 | add_action( |
| 42 | 'rest_api_init', |
| 43 | function () { |
| 44 | $namespace = 'kubio/v1/polylang'; |
| 45 | |
| 46 | register_rest_route( |
| 47 | $namespace, |
| 48 | '/quick-translate', |
| 49 | array( |
| 50 | 'methods' => 'POST', |
| 51 | 'callback' => 'kubio_api_polylang_quick_translate', |
| 52 | 'permission_callback' => function () { |
| 53 | return current_user_can( 'edit_theme_options' ); |
| 54 | }, |
| 55 | ) |
| 56 | ); |
| 57 | } |
| 58 | ); |
| 59 | |
| 60 | function kubio_api_polylang_add_page_translation( WP_REST_Request $request ) { |
| 61 | //check_ajax_referer('kubio_api_polylang_add_page_translation'); // TODO: check nonce |
| 62 | |
| 63 | $post_id = $request->get_param( 'postId' ); |
| 64 | $new_lang = $request->get_param( 'newLang' ); |
| 65 | |
| 66 | // Duplicate page |
| 67 | $new_post_id = kubio_polylang_translate_page( $post_id, $new_lang ); |
| 68 | |
| 69 | // Duplicate template |
| 70 | $new_template = kubio_polylang_translate_page_template( $post_id, $new_lang ); |
| 71 | |
| 72 | // Duplicate template-parts |
| 73 | kubio_polylang_translate_page_template_parts( $post_id, $new_lang ); |
| 74 | |
| 75 | // Update page template with new one |
| 76 | if ( $new_post_id && $new_template ) { |
| 77 | update_post_meta( $new_post_id, '_wp_page_template', $new_template['slug'] ); |
| 78 | } elseif ( get_post_meta( $post_id, '_wp_page_template', true ) === 'default' ) { |
| 79 | update_post_meta( $new_post_id, '_wp_page_template', 'default' ); |
| 80 | } |
| 81 | |
| 82 | wp_send_json_success(); |
| 83 | } |
| 84 | |
| 85 | function kubio_api_polylang_add_template_translation( WP_REST_Request $request ) { |
| 86 | //check_ajax_referer('kubio_api_polylang_add_template_translation'); // TODO: check nonce |
| 87 | |
| 88 | $post_id = $request->get_param( 'postId' ); |
| 89 | $new_lang = $request->get_param( 'newLang' ); |
| 90 | |
| 91 | // Duplicate template |
| 92 | $new_template = kubio_polylang_translate_page_template( null, $new_lang, intval( $post_id ) ); |
| 93 | |
| 94 | if ( ! $new_template ) { |
| 95 | wp_send_json_error(); |
| 96 | } |
| 97 | wp_send_json_success(); |
| 98 | } |
| 99 | |
| 100 | function kubio_api_polylang_quick_translate( WP_REST_Request $request ) { |
| 101 | //check_ajax_referer('nonce_name'); // TODO: check nonce |
| 102 | $post_type = $request->get_param( 'postType' ); |
| 103 | |
| 104 | $need_translation = \Kubio\Flags::getSetting( 'pll_templates_need_translation' ); |
| 105 | |
| 106 | if ( isset( $need_translation[ $post_type ] ) ) { |
| 107 | foreach ( $need_translation[ $post_type ] as $template_id => $language_ids ) { |
| 108 | foreach ( $language_ids as $lang_id ) { |
| 109 | kubio_polylang_translate_page_template( null, get_term( $lang_id )->slug, $template_id ); |
| 110 | } |
| 111 | } |
| 112 | \Kubio\Flags::setSetting( 'pll_templates_need_translation', null ); |
| 113 | } |
| 114 | |
| 115 | wp_send_json_success(); |
| 116 | } |
| 117 |