|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Plugin Name: FartScroll |
| 4 | + * Plugin URI: http://planetozh.com/blog/my-projects/wordpress-plugin-fart-scroll-theonion/ |
| 5 | + * GitHub Plugin URI: https://github.com/ozh/fartscroll |
| 6 | + * Description: "You want fart noises as you scroll? We've got you covered." A WordPress implementation of TheOnion's <a href="http://theonion.github.io/fartscroll.js/">Fartscroll.js</a> elegant piece of software |
| 7 | + * Version: 1.0.1 |
| 8 | + * Requires at least: 3.0 |
| 9 | + * Requires PHP: 5.6 |
| 10 | + * Author: Ozh & TheOnion |
| 11 | + * Author URI: http://ozh.org/ |
| 12 | + */ |
| 13 | + |
| 14 | + |
| 15 | +/****************** Public stuff */ |
| 16 | + |
| 17 | +// Add JS to pages of the blog |
| 18 | +add_action( 'template_redirect', 'fartscroll_add_script' ); |
| 19 | +function fartscroll_add_script() { |
| 20 | + $options = get_option( 'fartscroll_options' ); |
| 21 | + $fart_chance = ( isset( $options['fart_chance'] ) ? $options['fart_chance'] : 100 ); |
| 22 | + if( mt_rand( 0, 100 ) <= $fart_chance ) { |
| 23 | + wp_enqueue_script( 'fartscroll', plugin_dir_url( __FILE__) . 'fartscroll.js' ); |
| 24 | + add_action( 'wp_footer', 'fartscroll_add_footer' ); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// Add JS to footer |
| 29 | +function fartscroll_add_footer() { |
| 30 | + $options = get_option( 'fartscroll_options' ); |
| 31 | + $fart_scroll = ( isset( $options['fart_scroll'] ) ? $options['fart_scroll'] : 800 ); |
| 32 | + echo <<<FART |
| 33 | +<script type="text/javascript"> |
| 34 | +fartscroll( $fart_scroll ); |
| 35 | +</script> |
| 36 | +
|
| 37 | +FART; |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +/****************** Admin stuff */ |
| 42 | + |
| 43 | +// Add a menu for our option page |
| 44 | +add_action('admin_menu', 'fartscroll_add_page'); |
| 45 | +function fartscroll_add_page() { |
| 46 | + add_options_page( 'Fartscroll', 'Fartscroll', 'manage_options', 'fartscroll', 'fartscroll_option_page' ); |
| 47 | +} |
| 48 | + |
| 49 | +// Draw the option page |
| 50 | +function fartscroll_option_page() { |
| 51 | + ?> |
| 52 | + <div class="wrap"> |
| 53 | + <?php screen_icon(); ?> |
| 54 | + <h2>Fartscroll. Pfffrrrtf.</h2> |
| 55 | + <form action="options.php" method="post"> |
| 56 | + <?php settings_fields('fartscroll_options'); ?> |
| 57 | + <?php do_settings_sections('fartscroll'); ?> |
| 58 | + <input name="Submit" type="submit" value="Save Changes" /> |
| 59 | + </form> |
| 60 | + </div> |
| 61 | + <?php |
| 62 | +} |
| 63 | + |
| 64 | +// Register and define the settings |
| 65 | +add_action('admin_init', 'fartscroll_admin_init'); |
| 66 | +function fartscroll_admin_init(){ |
| 67 | + register_setting( |
| 68 | + 'fartscroll_options', |
| 69 | + 'fartscroll_options', |
| 70 | + 'fartscroll_validate_options' |
| 71 | + ); |
| 72 | + add_settings_section( |
| 73 | + 'fartscroll_main', |
| 74 | + 'Fartscroll Settings', |
| 75 | + 'fartscroll_section_text', |
| 76 | + 'fartscroll' |
| 77 | + ); |
| 78 | + add_settings_field( |
| 79 | + 'fartscroll_fart_scroll', |
| 80 | + 'Scrolling', |
| 81 | + 'fartscroll_setting_input_scroll', |
| 82 | + 'fartscroll', |
| 83 | + 'fartscroll_main' |
| 84 | + ); |
| 85 | + add_settings_field( |
| 86 | + 'fartscroll_fart_chance', |
| 87 | + 'Probability', |
| 88 | + 'fartscroll_setting_input_chance', |
| 89 | + 'fartscroll', |
| 90 | + 'fartscroll_main' |
| 91 | + ); |
| 92 | +} |
| 93 | + |
| 94 | +// Draw the section header |
| 95 | +function fartscroll_section_text() { |
| 96 | + echo '<p>Configure how much and how often you want farts on scrolls.</p>'; |
| 97 | +} |
| 98 | + |
| 99 | +// Display and fill the form field |
| 100 | +function fartscroll_setting_input_scroll() { |
| 101 | + // get option 'fart_scroll' value from the database |
| 102 | + $options = get_option( 'fartscroll_options' ); |
| 103 | + $fart_scroll = ( isset( $options['fart_scroll'] ) ? $options['fart_scroll'] : 800 ); |
| 104 | + // echo the field |
| 105 | + echo "<input id='fart_scroll' name='fartscroll_options[fart_scroll]' type='text' value='$fart_scroll' />"; |
| 106 | + echo "<br/>How many pixels scrolled to emit a fart ? (default: 800)"; |
| 107 | +} |
| 108 | + |
| 109 | +// Display and fill the form field |
| 110 | +function fartscroll_setting_input_chance() { |
| 111 | + // get option 'fart_chance' value from the database |
| 112 | + $options = get_option( 'fartscroll_options' ); |
| 113 | + $fart_chance = ( isset( $options['fart_chance'] ) ? $options['fart_chance'] : 100 ); |
| 114 | + // echo the field |
| 115 | + echo "<input id='fart_chance' name='fartscroll_options[fart_chance]' type='text' value='$fart_chance' />"; |
| 116 | + echo "<br/>Probability, in percent, that the script is loaded on a page. 100 = all pages, 10 = 1 page out of ten (default: 100)"; |
| 117 | +} |
| 118 | + |
| 119 | +// Validate user input |
| 120 | +function fartscroll_validate_options( $input ) { |
| 121 | + $valid = array(); |
| 122 | + $valid['fart_scroll'] = intval( $input['fart_scroll'] ); |
| 123 | + $valid['fart_chance'] = min( absint( $input['fart_chance'] ), 100 ); |
| 124 | + |
| 125 | + return $valid; |
| 126 | +} |
| 127 | + |
| 128 | +// Settings link in plugin management screen |
| 129 | +function fartscroll_settings_link($actions, $file) { |
| 130 | + if( false !== strpos($file, 'fartscroll' ) ) |
| 131 | + $actions['settings'] = '<a href="options-general.php?page=fartscroll">Fart settings</a>'; |
| 132 | + return $actions; |
| 133 | +} |
| 134 | +add_filter( 'plugin_action_links', 'fartscroll_settings_link', 2, 2 ); |
| 135 | + |
0 commit comments