settings = new WPSlug_Settings(); $this->converter = new WPSlug_Converter(); $this->optimizer = new WPSlug_Optimizer(); add_action("admin_menu", [$this, "addAdminMenu"]); add_action("admin_init", [$this, "registerSettings"]); add_action("admin_enqueue_scripts", [$this, "enqueueScripts"]); add_action("admin_notices", [$this, "showAdminNotices"]); add_action("wp_ajax_wpslug_preview", [$this, "ajaxPreview"]); add_action("wp_ajax_wpslug_test_api", [$this, "ajaxTestApi"]); add_action("post_submitbox_start", [$this, "addPostMetaBox"]); add_filter("bulk_actions-edit-post", [$this, "addBulkAction"]); add_filter("bulk_actions-edit-page", [$this, "addBulkAction"]); add_filter( "handle_bulk_actions-edit-post", [$this, "handleBulkAction"], 10, 3 ); add_filter( "handle_bulk_actions-edit-page", [$this, "handleBulkAction"], 10, 3 ); add_action("admin_notices", [$this, "bulkActionNotice"]); add_action("load-options-permalink.php", [$this, "addPermalinkNotice"]); add_action("admin_head", [$this, "hideDefaultNotices"]); } public function hideDefaultNotices() { if (isset($_GET["page"]) && $_GET["page"] == "wpslug") { echo ""; } } public function addAdminMenu() { add_options_page( __("WPSlug Settings", "wpslug"), __("Slug", "wpslug"), "manage_options", "wpslug", [$this, "displayAdminPage"] ); } public function registerSettings() { register_setting("wpslug_settings", "wpslug_options", [ "type" => "array", "sanitize_callback" => [$this, "validateOptions"], "default" => [], "show_in_rest" => false, ]); add_filter("wp_redirect", [$this, "preventDefaultNotice"], 10, 2); } public function preventDefaultNotice($location, $status) { if (strpos($location, "options-general.php?page=wpslug") !== false) { if (strpos($location, "settings-updated=true") !== false) { return $location; } } return $location; } public function showAdminNotices() { if (isset($_GET["page"]) && $_GET["page"] == "wpslug") { if ( isset($_GET["settings-updated"]) && $_GET["settings-updated"] == "true" ) { $message = __("Settings saved successfully!", "wpslug"); echo '

' . esc_html($message) . "

"; } if (isset($_GET["wpslug-error"])) { $error_message = sanitize_text_field($_GET["wpslug-error"]); echo '

' . esc_html($error_message) . "

"; } } } public function displayAdminPage() { if (!current_user_can("manage_options")) { wp_die( __( "You do not have sufficient permissions to access this page.", "wpslug" ) ); } $options = $this->settings->getOptions(); $current_tab = isset($_GET["tab"]) ? sanitize_text_field($_GET["tab"]) : "general"; ?>

" data-section="general"> renderGeneralSettings($options); ?>
" data-section="pinyin"> renderPinyinSettings($options); ?>
" data-section="transliteration"> renderTransliterationSettings( $options ); ?>
" data-section="translation"> renderTranslationSettings($options); ?>
" data-section="seo"> renderSEOSettings($options); ?>
" data-section="media"> renderMediaSettings($options); ?>
" data-section="advanced"> renderAdvancedSettings($options); ?>

" />

" min="0" max="500" class="small-text">


" class="regular-text">

" class="regular-text">

" class="regular-text">

" min="1" max="30" class="small-text">

" class="regular-text">

true], "objects" ); foreach ($post_types as $post_type) { $checked = is_array($options["enabled_post_types"]) && in_array( $post_type->name, $options["enabled_post_types"] ) ? "checked" : ""; ?>

true], "objects" ); foreach ($taxonomies as $taxonomy) { $checked = is_array($options["enabled_taxonomies"]) && in_array( $taxonomy->name, $options["enabled_taxonomies"] ) ? "checked" : ""; ?>

id !== "options-permalink") { return; } $options = $this->settings->getOptions(); if (!$options["enable_conversion"]) { return; } ?>

- " class="button button-small" style="margin-left: 10px;">

settings->validateOptions($input); if (defined("WP_DEBUG") && WP_DEBUG) { error_log( "WP Slug - Validated data: " . json_encode($validated, JSON_PRETTY_PRINT) ); } if (!empty($validated)) { $current_options = $this->settings->getOptions(); $merged_options = array_merge($current_options, $validated); if (defined("WP_DEBUG") && WP_DEBUG) { error_log( "WP Slug - Final merged options: " . json_encode($merged_options, JSON_PRETTY_PRINT) ); } if ( isset($_POST["wpslug_current_tab"]) && !empty($_POST["wpslug_current_tab"]) ) { $tab = sanitize_text_field($_POST["wpslug_current_tab"]); set_transient("wpslug_admin_tab", $tab, 60); } return $merged_options; } return $input; } public function enqueueScripts($hook) { if ("settings_page_wpslug" !== $hook) { return; } wp_enqueue_script( "wpslug-admin", WPSLUG_PLUGIN_URL . "assets/admin.js", ["jquery"], WPSLUG_VERSION, true ); wp_enqueue_style( "wpslug-admin", WPSLUG_PLUGIN_URL . "assets/admin.css", [], WPSLUG_VERSION ); $saved_tab = get_transient("wpslug_admin_tab"); if ($saved_tab) { delete_transient("wpslug_admin_tab"); } wp_localize_script("wpslug-admin", "wpslug_ajax", [ "ajax_url" => admin_url("admin-ajax.php"), "nonce" => wp_create_nonce("wpslug_nonce"), "current_tab" => $saved_tab ?: "general", "strings" => [ "preview" => __("Preview", "wpslug"), "converting" => __("Converting...", "wpslug"), "testing" => __("Testing...", "wpslug"), "test_api" => __("Test API", "wpslug"), "reset_confirm" => __( "Are you sure you want to reset all settings to default values?", "wpslug" ), "api_test_success" => __( "API connection successful!", "wpslug" ), "api_test_failed" => __( "API connection failed. Please check your credentials.", "wpslug" ), "no_text" => __("Please enter some text to preview.", "wpslug"), "conversion_error" => __( "Conversion failed. Please check your settings.", "wpslug" ), ], ]); } public function ajaxPreview() { check_ajax_referer("wpslug_nonce", "nonce"); $text = sanitize_text_field($_POST["text"]); $options = $this->settings->getOptions(); if (empty($text)) { wp_send_json_error([ "message" => __("Please enter some text to preview.", "wpslug"), ]); } try { $converted = $this->converter->convert($text, $options); $optimized = $this->optimizer->optimize($converted, $options); wp_send_json_success([ "original" => $text, "converted" => $converted, "optimized" => $optimized, "final" => $optimized, "mode" => $options["conversion_mode"], "detected_language" => $this->converter->detectLanguage($text), ]); } catch (Exception $e) { wp_send_json_error([ "message" => __( "Conversion failed. Please check your settings.", "wpslug" ), ]); } } public function ajaxTestApi() { check_ajax_referer("wpslug_nonce", "nonce"); $service = sanitize_text_field($_POST["service"]); $options = $this->settings->getOptions(); if ($service === "google") { $api_key = trim($options["google_api_key"]); if (empty($api_key)) { wp_send_json_error([ "message" => __( "Google API key is required for testing.", "wpslug" ), ]); return; } } elseif ($service === "baidu") { $app_id = trim($options["baidu_app_id"]); $secret_key = trim($options["baidu_secret_key"]); if (empty($app_id) || empty($secret_key)) { wp_send_json_error([ "message" => __( "Both Baidu App ID and Secret Key are required for testing.", "wpslug" ), ]); return; } } else { wp_send_json_error([ "message" => __("Invalid service selected.", "wpslug"), ]); return; } $test_text = "Hello World"; $translator = new WPSlug_Translator(); try { $result = $translator->translate( $test_text, array_merge($options, ["translation_service" => $service]) ); if (!empty($result) && $result !== $test_text) { wp_send_json_success([ "message" => __("API connection successful!", "wpslug"), ]); } else { wp_send_json_error([ "message" => __( "API connection failed. Please check your credentials.", "wpslug" ), ]); } } catch (Exception $e) { wp_send_json_error([ "message" => __("API connection failed: ", "wpslug") . $e->getMessage(), ]); } } public function addPostMetaBox() { global $post; if (!$post || !current_user_can("edit_post", $post->ID)) { return; } $options = $this->settings->getOptions(); if ( !$options["enable_conversion"] || !$this->settings->isPostTypeEnabled($post->post_type) ) { return; } echo '
'; echo ""; echo "
"; } public function addBulkAction($bulk_actions) { $options = $this->settings->getOptions(); if (!$options["enable_conversion"]) { return $bulk_actions; } $modes = $this->settings->getConversionModes(); $action_text = sprintf( __("Convert Slugs (%s)", "wpslug"), $modes[$options["conversion_mode"]] ); $bulk_actions["wpslug-convert"] = $action_text; return $bulk_actions; } public function handleBulkAction($redirect_url, $action, $post_ids) { if ($action !== "wpslug-convert") { return $redirect_url; } $options = $this->settings->getOptions(); $converted_count = 0; foreach ($post_ids as $post_id) { $post = get_post($post_id); if (!$post) { continue; } $new_slug = $this->converter->convert($post->post_title, $options); $new_slug = $this->optimizer->optimize($new_slug, $options); if (!empty($new_slug) && $new_slug !== $post->post_name) { $unique_slug = $this->optimizer->generateUniqueSlug( $new_slug, $post->ID, $post->post_type ); wp_update_post([ "ID" => $post->ID, "post_name" => $unique_slug, ]); $converted_count++; } } $redirect_url = add_query_arg( "wpslug-converted", $converted_count, $redirect_url ); return $redirect_url; } public function bulkActionNotice() { if (isset($_GET["wpslug-converted"])) { $count = intval($_GET["wpslug-converted"]); echo '
'; echo "

" . sprintf( __("Successfully converted %d slug(s).", "wpslug"), $count ) . "

"; echo "
"; } } }