From 25b7d2aab61ae6421398d3abae5da6ffe590333d Mon Sep 17 00:00:00 2001 From: s1n Date: Sat, 28 Mar 2020 10:36:41 -0700 Subject: 3/28/2020, 10:36 --- user/plugins/random-shorturls/plugin.php | 93 ++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 user/plugins/random-shorturls/plugin.php (limited to 'user/plugins/random-shorturls/plugin.php') diff --git a/user/plugins/random-shorturls/plugin.php b/user/plugins/random-shorturls/plugin.php new file mode 100644 index 0000000..4f381af --- /dev/null +++ b/user/plugins/random-shorturls/plugin.php @@ -0,0 +1,93 @@ +Random ShortURLs plugin cannot function unless Random Keywords is removed first." ); +} else { + // filter registration happens conditionally, to avoid conflicts + // settings action is left out here, as it allows checking settings before deleting the old plugin + yourls_add_filter( 'random_keyword', 'ozh_random_shorturl' ); + yourls_add_filter( 'get_next_decimal', 'ozh_random_shorturl_next_decimal' ); +} + +// Generate a random keyword +function ozh_random_shorturl() { + $possible = yourls_get_shorturl_charset() ; + $str=''; + while( strlen( $str ) < yourls_get_option( 'random_shorturls_length', 5 ) ) { + $str .= substr($possible, rand( 0, strlen( $possible ) - 1 ), 1 ); + } + return $str; +} + +// Don't increment sequential keyword tracker +function ozh_random_shorturl_next_decimal( $next ) { + return ( $next - 1 ); +} + +// Plugin settings page etc. +yourls_add_action( 'plugins_loaded', 'ozh_random_shorturl_add_settings' ); +function ozh_random_shorturl_add_settings() { + yourls_register_plugin_page( 'random_shorturl_settings', 'Random ShortURLs Settings', 'ozh_random_shorturl_settings_page' ); +} + +function ozh_random_shorturl_settings_page() { + // Check if form was submitted + if( isset( $_POST['random_length'] ) ) { + // If so, verify nonce + yourls_verify_nonce( 'random_shorturl_settings' ); + // and process submission if nonce is valid + ozh_random_shorturl_settings_update(); + } + + $random_length = yourls_get_option('random_shorturls_length'); + $nonce = yourls_create_nonce( 'random_shorturl_settings' ); + + echo << +

Random ShortURLs Settings

+
+ +

+ + +

+

+
+ +HTML; +} + +function ozh_random_shorturl_settings_update() { + $random_length = $_POST['random_length']; + + if( $random_length ) { + if( is_numeric( $random_length ) ) { + yourls_update_option( 'random_shorturls_length', intval( $random_length ) ); + } else { + echo "Error: Length given was not a number."; + } + } else { + echo "Error: No length value given."; + } +} -- cgit v1.2.3