From 328bfc6c70227285d2a4ee4b6ce70f7ecdaa370c Mon Sep 17 00:00:00 2001 From: jung Date: Sat, 1 Jun 2024 02:07:28 +0800 Subject: [PATCH] add setting interface in admin panel --- plugin.php | 84 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 69 insertions(+), 15 deletions(-) diff --git a/plugin.php b/plugin.php index 7404900..cfb2848 100644 --- a/plugin.php +++ b/plugin.php @@ -3,7 +3,7 @@ Plugin Name: Limit Custom Keyword Length Plugin URI: https://github.com/suryatanjung/yourls-limit-custom-keyword-length/ Description: This plugin limits the min and max number of characters for custom keyword -Version: 1.0 +Version: 1.1 Author: Surya Tanjung Author URI: https://jung.bz/ */ @@ -11,20 +11,74 @@ // Hook custom function into the 'shunt_add_new_link' filter yourls_add_filter( 'shunt_add_new_link', 'limit_keyword_length' ); +// Register the plugin settings page +yourls_add_action( 'plugins_loaded', 'limit_keyword_length_add_settings' ); + +// Add settings page +function limit_keyword_length_add_settings() { + yourls_register_plugin_page( 'limit_keyword_length_settings', 'Limit Keyword Length Settings', 'limit_keyword_length_settings_page' ); +} + +// Display settings page +function limit_keyword_length_settings_page() { + // Check if form was submitted + if( isset( $_POST['min_length'] ) && isset( $_POST['max_length'] ) ) { + // Verify nonce + yourls_verify_nonce( 'limit_keyword_length_settings' ); + // Update settings + limit_keyword_length_update_settings(); + } + + // Get current settings + $min_length = yourls_get_option('limit_keyword_length_min', 4); + $max_length = yourls_get_option('limit_keyword_length_max', 15); + $nonce = yourls_create_nonce( 'limit_keyword_length_settings' ); + + echo <<Limit Keyword Length Settings +

Plugin by Surya Tanjung

+
+ +

+ + +

+

+ + +

+

+
+HTML; +} + +// Update settings +function limit_keyword_length_update_settings() { + $min_length = intval($_POST['min_length']); + $max_length = intval($_POST['max_length']); + + if( $min_length > 0 && $max_length > 0 && $min_length <= $max_length ) { + yourls_update_option( 'limit_keyword_length_min', $min_length ); + yourls_update_option( 'limit_keyword_length_max', $max_length ); + } else { + echo "Error: Invalid length values."; + } +} + // Check custom keyword length and return an error if it exceeds the max or min length limit function limit_keyword_length( $error, $url, $keyword ) { - $max_length = 30; // Set max length limit for custom keyword - $min_length = 3; // Set min length limit for custom keyword - $length = strlen( $keyword ); - - if ( $length > $max_length || ( $length < $min_length && $length > 0 ) ) { - $error['status'] = 'fail'; - $error['code'] = 'error:keyword'; - $error['message'] = ( $length > $max_length ) - ? "the keyword is too long. It can't be more than {$max_length} characters" - : "the keyword is too short. It needs to have at least {$min_length} characters"; - return yourls_apply_filter( 'add_new_link_keyword_length_error', $error ); - } - - return false; + $max_length = yourls_get_option('limit_keyword_length_max', 15); // Default max length + $min_length = yourls_get_option('limit_keyword_length_min', 4); // Default min length + $length = strlen( $keyword ); + + if ( $length > $max_length || ( $length < $min_length && $length > 0 ) ) { + $error['status'] = 'fail'; + $error['code'] = 'error:keyword'; + $error['message'] = ( $length > $max_length ) + ? "The keyword is too long. It can't be more than {$max_length} characters." + : "The keyword is too short. It needs to have at least {$min_length} characters."; + return yourls_apply_filter( 'add_new_link_keyword_length_error', $error ); + } + + return false; }