diff --git a/inc/admin/admin-bar.php b/inc/admin/admin-bar.php index e00c384..c08d2ab 100644 --- a/inc/admin/admin-bar.php +++ b/inc/admin/admin-bar.php @@ -45,8 +45,8 @@ function wplng_admin_bar_menu() { return; } - $url = wplng_get_url_current(); - $url_original = remove_query_arg( + $url = wplng_get_url_current(); + $url_clean = remove_query_arg( array( 'wplng-mode', 'wplng-load', 'wplng-nocache' ), $url ); @@ -63,7 +63,7 @@ function wplng_admin_bar_menu() { 'id' => 'wplingua-return', 'parent' => 'wplingua-menu', 'title' => __( 'Return on page', 'wplingua' ), - 'href' => $url_original, + 'href' => $url_clean, ) ); } @@ -111,7 +111,7 @@ function wplng_admin_bar_menu() { 'wplng-mode', 'editor', wplng_url_translate( - wplng_get_url_original( $url_original ), + wplng_get_url_original( $url_clean ), $language['id'] ) ), @@ -127,7 +127,7 @@ function wplng_admin_bar_menu() { 'wplng-mode', 'list', wplng_url_translate( - wplng_get_url_original( $url_original ), + wplng_get_url_original( $url_clean ), $language['id'] ) ), @@ -137,4 +137,71 @@ function wplng_admin_bar_menu() { } // End foreach ( $languages_target as $language ) } // End if ( ! empty( $languages_target ) ) + /** + * Edit slug translation link + */ + + if ( is_404() ) { + return; + } + + $url_original = wplng_get_url_original( $url_clean ); + $url_original_parsed = parse_url( $url_original ); + + if ( ! isset( $url_original_parsed['path'] ) ) { + return; + } + + $path = $url_original_parsed['path']; + + // Return path if no contains slug + + if ( '/' === $path || '' === $path ) { + return; + } + + // Transform the multi part slug to an slug array + + $slugs = explode( '/', $path ); + $slugs = array_filter( $slugs ); + + $last_slug = ''; + + foreach ( $slugs as $slug ) { + if ( '/' === $slug || '' === $slug ) { + continue; + } + $last_slug = $slug; + } + + if ( '/' === $last_slug + || '' === $last_slug + || ! wplng_text_is_translatable( $last_slug ) + ) { + return; + } + + $slug_id = wplng_get_slug_saved_from_original( $last_slug ); + + if ( false === $slug_id ) { + $slug_id = wplng_create_slug( $slug ); + } + + $edit_slug_link = get_edit_post_link( $slug_id ); + + if ( ! is_string( $edit_slug_link ) ) { + return; + } + + $wp_admin_bar->add_menu( + array( + 'id' => 'wplingua-slug', + 'parent' => 'wplingua-menu', + 'title' => __( 'Edit the page slug', 'wplingua' ), + 'href' => $edit_slug_link, + 'meta' => array( + 'target' => '_blank', + ), + ) + ); } diff --git a/inc/admin/assets.php b/inc/admin/assets.php index 315848f..ac15ae0 100644 --- a/inc/admin/assets.php +++ b/inc/admin/assets.php @@ -349,6 +349,72 @@ function wplng_translation_assets() { } +/** + * Register wpLingua assets on slugs edit pages + * + * @return void + */ +function wplng_slug_assets() { + + global $post_type; + + if ( 'wplng_slug' === $post_type ) { + + /** + * Enqueue jQuery + */ + + wp_enqueue_script( 'jquery' ); + + /** + * Enqueue wpLingua JS scripts + */ + + wp_enqueue_script( + 'wplingua-slug', + plugins_url() . '/wplingua/assets/js/admin/slug.js', + array( 'jquery' ), + WPLNG_PLUGIN_VERSION + ); + + /** + * Localize script + */ + + wp_localize_script( + 'wplingua-slug', + 'wplngLocalize', + array( + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), + 'currentLanguage' => false, + 'message' => array( + 'exitPage' => esc_html__( + 'You are about to leave the page without saving your changes. They will be lost if you continue. Would you like to leave the page anyway?', + 'wplingua' + ), + 'exitEditorModal' => '', + 'buttonSave' => '', + 'buttonSaveInProgress' => '', + ), + ) + ); + + /** + * Enqueue wpLingua CSS styles + */ + + wp_enqueue_style( + 'wplingua-slug', + plugins_url() . '/wplingua/assets/css/admin/slug.css', + array(), + WPLNG_PLUGIN_VERSION + ); + + } + +} + + /** * Print wpLingua head script (JSON with all languages informations) * diff --git a/inc/admin/option-page.php b/inc/admin/option-page.php index 16ec0ee..adf1148 100644 --- a/inc/admin/option-page.php +++ b/inc/admin/option-page.php @@ -78,6 +78,15 @@ function wplng_create_menu() { 'wplng_option_page_exclusions' ); + add_submenu_page( + 'wplingua-settings', + __( 'wplingua: Website slugs', 'wplingua' ), + __( 'Website slugs', 'wplingua' ), + 'administrator', + 'edit.php?post_type=wplng_slug', + false + ); + add_submenu_page( 'wplingua-settings', __( 'wplingua: Translations', 'wplingua' ), diff --git a/inc/admin/slug-cpt.php b/inc/admin/slug-cpt.php new file mode 100644 index 0000000..a4c8992 --- /dev/null +++ b/inc/admin/slug-cpt.php @@ -0,0 +1,498 @@ + array( + 'name' => __( 'Website slugs', 'wplingua' ), + 'singular_name' => __( 'Website slug', 'wplingua' ), + 'all_items' => __( 'All slugs', 'wplingua' ), + 'edit_item' => __( 'Edit slug', 'wplingua' ), + 'menu_name' => __( 'Website slugs', 'wplingua' ), + ), + 'public' => false, + 'publicly_queryable' => false, + 'show_ui' => true, + 'exclude_from_search' => true, + 'show_in_nav_menus' => false, + 'show_in_menu' => false, + 'has_archive' => false, + 'rewrite' => false, + 'menu_icon' => 'dashicons-translation', + 'supports' => array( + 'title', + ), + 'capability_type' => 'post', + 'capabilities' => array( + 'create_posts' => false, + ), + 'map_meta_cap' => true, + ) + ); +} + + +/** + * Remove quick edit on wpLingua slugs list + * + * @param array $actions + * @param object $post + * @return array + */ +function wplng_slug_remove_quick_edit( $actions, $post ) { + + if ( $post->post_type != 'wplng_slug' ) { + return $actions; + } + + unset( $actions['view'] ); + unset( $actions['inline hide-if-no-js'] ); + + return $actions; +} + + +/** + * Display 100 translations by default in admin area + * + * @param mixed $result + * @return mixed + */ +function wplng_slug_per_page( $result ) { + if ( false === $result ) { + $result = '100'; + } + + return $result; +} + + + +/** + * Filter slugs by status: Display option on CPT list + * + * @return void + */ +function wplng_restrict_manage_posts_slug_status() { + + if ( empty( $_GET['post_type'] ) + || 'wplng_slug' !== $_GET['post_type'] + ) { + return; + } + + $languages_target = wplng_get_languages_target_ids(); + $options = array(); + $slug_status = ''; + + if ( ! empty( $_GET['slug_status'] ) ) { + $slug_status = sanitize_title( $_GET['slug_status'] ); + } + + if ( count( $languages_target ) === 1 ) { + $options = array( + '' => __( 'All slug status', 'wplingua' ), + 'reviewed' => __( 'Reviewed', 'wplingua' ), + 'unreviewed' => __( 'Unreviewed', 'wplingua' ), + ); + } else { + $options = array( + '' => __( 'All slug status', 'wplingua' ), + 'full-reviewed' => __( 'Full reviewed', 'wplingua' ), + 'partially-reviewed' => __( 'Partially reviewed', 'wplingua' ), + 'reviewed' => __( 'Reviewed', 'wplingua' ), + 'unreviewed' => __( 'Full unreviewed', 'wplingua' ), + ); + } + + $html = '