[ 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
   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  */
  19  
  20  // No direct call
  21  if( !defined( 'YOURLS_ABSPATH' ) ) die();
  22  
  23  // Only register things if the old third-party plugin is not present
  24  if( function_exists('ozh_random_keyword') ) {
  25      yourls_add_notice( "<b>Random ShortURLs</b> plugin cannot function unless <b>Random Keywords</b> is removed first." );
  26  } else {
  27      // filter registration happens conditionally, to avoid conflicts
  28      // settings action is left out here, as it allows checking settings before deleting the old plugin
  29      yourls_add_filter( 'random_keyword', 'ozh_random_shorturl' );
  30      yourls_add_filter( 'get_next_decimal', 'ozh_random_shorturl_next_decimal' );
  31  }
  32  
  33  // Generate a random keyword
  34  function ozh_random_shorturl() {
  35      $possible = yourls_get_shorturl_charset() ;
  36      $str='';
  37      while( strlen( $str ) < yourls_get_option( 'random_shorturls_length', 5 ) ) {
  38          $str .= substr($possible, rand( 0, strlen( $possible ) - 1 ), 1 );
  39      }
  40      return $str;
  41  }
  42  
  43  // Don't increment sequential keyword tracker
  44  function ozh_random_shorturl_next_decimal( $next ) {
  45      return ( $next - 1 );
  46  }
  47  
  48  // Plugin settings page etc.
  49  yourls_add_action( 'plugins_loaded', 'ozh_random_shorturl_add_settings' );
  50  function ozh_random_shorturl_add_settings() {
  51      yourls_register_plugin_page( 'random_shorturl_settings', 'Random ShortURLs Settings', 'ozh_random_shorturl_settings_page' );
  52  }
  53  
  54  function ozh_random_shorturl_settings_page() {
  55      // Check if form was submitted
  56      if( isset( $_POST['random_length'] ) ) {
  57          // If so, verify nonce
  58          yourls_verify_nonce( 'random_shorturl_settings' );
  59          // and process submission if nonce is valid
  60          ozh_random_shorturl_settings_update();
  61      }
  62  
  63      $random_length = yourls_get_option('random_shorturls_length', 5);
  64      $nonce = yourls_create_nonce( 'random_shorturl_settings' );
  65  
  66      echo <<<HTML
  67          <main>
  68              <h2>Random ShortURLs Settings</h2>
  69              <form method="post">
  70              <input type="hidden" name="nonce" value="$nonce" />
  71              <p>
  72                  <label>Random Keyword Length</label>
  73                  <input type="number" name="random_length" min="1" max="128" value="$random_length" />
  74              </p>
  75              <p><input type="submit" value="Save" class="button" /></p>
  76              </form>
  77          </main>
  78  HTML;
  79  }
  80  
  81  function ozh_random_shorturl_settings_update() {
  82      $random_length = $_POST['random_length'];
  83  
  84      if( $random_length ) {
  85          if( is_numeric( $random_length ) ) {
  86              yourls_update_option( 'random_shorturls_length', intval( $random_length ) );
  87          } else {
  88              echo "Error: Length given was not a number.";
  89          }
  90      } else {
  91          echo "Error: No length value given.";
  92      }
  93  }


Generated: Wed Sep 28 05:10:02 2022 Cross-referenced by PHPXref 0.7.1