[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/user/plugins/random-shorturls/ -> plugin.php (source)

   1  <?php
   2  /*
   3  Plugin Name: Random ShortURLs
   4  Plugin URI: https://yourls.org/
   5  Description: Assign random keywords to shorturls, like bitly (sho.rt/hJudjK)
   6  Version: 1.2.1
   7  Author: Ozh
   8  Author URI: https://ozh.org/
   9  */
  10  
  11  /* Release History:
  12  *
  13  * 1.0 Initial release
  14  * 1.1 Added: don't increment sequential keyword counter & save one SQL query
  15  * Fixed: plugin now complies to character set defined in config.php
  16  * 1.2 Adopted as YOURLS core plugin under a new name
  17  * Now configured via YOURLS options instead of editing plugin file
  18  * 1.2.1 Update HTML tag in notice
  19  */
  20  
  21  // No direct call
  22  if( !defined( 'YOURLS_ABSPATH' ) ) die();
  23  
  24  // Only register things if the old third-party plugin is not present
  25  if( function_exists('ozh_random_keyword') ) {
  26      yourls_add_notice( "<strong>Random ShortURLs</strong> plugin cannot function unless <strong>Random Keywords</strong> is removed first." );
  27  } else {
  28      // filter registration happens conditionally, to avoid conflicts
  29      // settings action is left out here, as it allows checking settings before deleting the old plugin
  30      yourls_add_filter( 'random_keyword', 'ozh_random_shorturl' );
  31      yourls_add_filter( 'get_next_decimal', 'ozh_random_shorturl_next_decimal' );
  32  }
  33  
  34  // Generate a random keyword
  35  function ozh_random_shorturl() {
  36      $possible = yourls_get_shorturl_charset() ;
  37      $str='';
  38      while( strlen( $str ) < yourls_get_option( 'random_shorturls_length', 5 ) ) {
  39          $str .= substr($possible, rand( 0, strlen( $possible ) - 1 ), 1 );
  40      }
  41      return $str;
  42  }
  43  
  44  // Don't increment sequential keyword tracker
  45  function ozh_random_shorturl_next_decimal( $next ) {
  46      return ( $next - 1 );
  47  }
  48  
  49  // Plugin settings page etc.
  50  yourls_add_action( 'plugins_loaded', 'ozh_random_shorturl_add_settings' );
  51  function ozh_random_shorturl_add_settings() {
  52      yourls_register_plugin_page( 'random_shorturl_settings', 'Random ShortURLs Settings', 'ozh_random_shorturl_settings_page' );
  53  }
  54  
  55  function ozh_random_shorturl_settings_page() {
  56      // Check if form was submitted
  57      if( isset( $_POST['random_length'] ) ) {
  58          // If so, verify nonce
  59          yourls_verify_nonce( 'random_shorturl_settings' );
  60          // and process submission if nonce is valid
  61          ozh_random_shorturl_settings_update();
  62      }
  63  
  64      $random_length = yourls_get_option('random_shorturls_length', 5);
  65      $nonce = yourls_create_nonce( 'random_shorturl_settings' );
  66  
  67      echo <<<HTML
  68          <main>
  69              <h2>Random ShortURLs Settings</h2>
  70              <form method="post">
  71              <input type="hidden" name="nonce" value="$nonce" />
  72              <p>
  73                  <label>Random Keyword Length</label>
  74                  <input type="number" name="random_length" min="1" max="128" value="$random_length" />
  75              </p>
  76              <p><input type="submit" value="Save" class="button" /></p>
  77              </form>
  78          </main>
  79  HTML;
  80  }
  81  
  82  function ozh_random_shorturl_settings_update() {
  83      $random_length = $_POST['random_length'];
  84  
  85      if( $random_length ) {
  86          if( is_numeric( $random_length ) ) {
  87              yourls_update_option( 'random_shorturls_length', intval( $random_length ) );
  88          } else {
  89              echo "Error: Length given was not a number.";
  90          }
  91      } else {
  92          echo "Error: No length value given.";
  93      }
  94  }


Generated: Sat Jul 11 05:10:54 2026 Cross-referenced by PHPXref 0.7.1