| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: A Random Number |
| 4 |
* Plugin URI: https://www.calculator.io/random-number-generator/ |
| 5 |
* Description: Outputs a random number via shortcode. It's magic. |
| 6 |
* Version: 1.1 |
| 7 |
* Author: Calculator.io |
| 8 |
* Author URI: https://www.calculator.io/ |
| 9 |
* License: GPL2 |
| 10 |
*/ |
| 11 |
|
| 12 |
/* Random Number Function */ |
| 13 |
function randomNumber( $atts ){ |
| 14 |
$atts = shortcode_atts( array( |
| 15 |
'min' => '1', |
| 16 |
'max' => '100', |
| 17 |
'comma' => 'yes' |
| 18 |
), $atts, 'randomnumber' ); |
| 19 |
$arandomnumber = mt_rand( $atts['min'], $atts['max'] ); |
| 20 |
$prettynumber = number_format($arandomnumber); |
| 21 |
|
| 22 |
if($atts['comma'] == "no"){ |
| 23 |
return $arandomnumber; |
| 24 |
} |
| 25 |
else { |
| 26 |
return $prettynumber; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
/* Assigning Shortcode */ |
| 31 |
add_shortcode('arandomnumber', 'randomNumber'); |
| 32 |
|
| 33 |
/* Adding Button to Editor */ |
| 34 |
add_action('admin_head', 'a_random_number_button'); |
| 35 |
|
| 36 |
function a_random_number_button() { |
| 37 |
global $typenow; |
| 38 |
// check user permissions |
| 39 |
if ( !current_user_can('edit_posts') && !current_user_can('edit_pages') ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
// verify the post type |
| 43 |
if( ! in_array( $typenow, array( 'post', 'page' ) ) ) |
| 44 |
return; |
| 45 |
// check if WYSIWYG is enabled |
| 46 |
if ( get_user_option('rich_editing') == 'true') { |
| 47 |
add_filter("mce_external_plugins", "arandomnumber_add_tinymce_plugin"); |
| 48 |
add_filter('mce_buttons', 'arandomnumber_register_my_tc_button'); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
function arandomnumber_add_tinymce_plugin($plugin_array) { |
| 53 |
$plugin_array['arandomnumber_tc_button'] = plugins_url( '/arandomnumber-button.js', __FILE__ ); |
| 54 |
return $plugin_array; |
| 55 |
} |
| 56 |
|
| 57 |
function arandomnumber_register_my_tc_button($buttons) { |
| 58 |
array_push($buttons, "arandomnumber_tc_button"); |
| 59 |
return $buttons; |
| 60 |
} |
| 61 |
|
| 62 |
/* Adding QuickTag Button */ |
| 63 |
function arn_quicktags() { |
| 64 |
|
| 65 |
if ( wp_script_is( 'quicktags' ) ) { |
| 66 |
?> |
| 67 |
<script type="text/javascript"> |
| 68 |
QTags.addButton( 'arandomnumber', 'A Random Number', '[arandomnumber min=1 max=100]' ); |
| 69 |
</script> |
| 70 |
<?php |
| 71 |
} |
| 72 |
|
| 73 |
} |
| 74 |
add_action( 'admin_print_footer_scripts', 'arn_quicktags' ); |
| 75 |
|
| 76 |
?> |