Skip to content

Instantly share code, notes, and snippets.

@ozh

ozh/plugin.php

Created Apr 7, 2021
Embed
What would you like to do?
YOURLS : add fake links (for test installs)
<?php
/*
Plugin Name: Insert Fake Links
Plugin URI: http://yourls.org/
Description: Populate DB with fake links for tests
Version: 1.0
Author: Ozh
Author URI: http://ozh.org/
*/
yourls_add_action( 'plugins_loaded', 'ozh_yourls_ifl_add_page' );
function ozh_yourls_ifl_add_page() {
yourls_register_plugin_page( 'ozh_ifl', 'Add Fake Links', 'ozh_yourls_ifl_do_page' );
}
// Display admin page
function ozh_yourls_ifl_do_page() {
if( isset( $_POST['action'] ) && $_POST['action'] == 'insert_fake_links' ) {
ozh_yourls_ifl_process();
} else {
ozh_yourls_ifl_form();
}
}
// Display form
function ozh_yourls_ifl_form() {
$nonce = yourls_create_nonce('insert_fake_links');
echo <<<HTML
<h2>Insert Links</h2>
<form method="post">
<input type="hidden" name="action" value="insert_fake_links" />
<input type="hidden" name="nonce" value="$nonce" />
<p>Insert <input type="text" name="number" /> links</p>
<p><input type="submit" value="Insert" /></p>
</form>
HTML;
}
function ozh_yourls_ifl_process() {
// Check nonce
yourls_verify_nonce( 'insert_fake_links' );
$number = abs( intval( $_POST['number'] ) );
for ($i = 1; $i <= $number; $i++) {
$title = $url = 'http://127.0.0.1/' . yourls_rnd_string( mt_rand(2, 10) );
yourls_add_new_link( $url, '', $title );
}
echo "<p>$number links added</p>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment