Skip to content

Instantly share code, notes, and snippets.

@sybrew
Last active July 24, 2016 19:08
Show Gist options
  • Save sybrew/99edbb8c9bc593ab0245c65ecf62426c to your computer and use it in GitHub Desktop.
Save sybrew/99edbb8c9bc593ab0245c65ecf62426c to your computer and use it in GitHub Desktop.
Hello Dolly OOP example (untested)
<?php
/*
Plugin Name: Hello Dolly OOP example
Plugin URI: https://wordpress.org/plugins/hello-dolly/
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
Author: Matt Mullenweg (and set to OOP by Sybre Waaijer)
Version: 1.6.oop
Author URI: http://ma.tt/
*/
new Hello_Dolly();
class Hello_Dolly {
//* Store current lyric here.
public $current_lyric;
public function __construct() {
add_action( 'admin_head', array( $this, 'dolly_css' ) );
add_action( 'admin_notices', array( $this, 'hello_dolly' ) );
}
//* Needs to be public as it's attached to an external action.
public function dolly_css() {
$x = is_rtl() ? 'left' : 'right';
echo "
<style type='text/css'>
#dolly {
float: $x;
padding-$x: 15px;
padding-top: 5px;
margin: 0;
font-size: 11px;
}
</style>
";
}
//* Can be protected as it's called directly from within the class
protected function hello_dolly_set_lyric() {
/** These are the lyrics to Hello Dolly */
$lyrics = "Hello, Dolly\r\n"
. "Well, hello, Dolly\r\n"
. "It's so nice to have you back where you belong\r\n"
. "You're lookin' swell, Dolly\r\n"
. "I can tell, Dolly\r\n"
. "You're still glowin', you're still crowin'\r\n"
. "You're still goin' strong\r\n"
. "We feel the room swayin'\r\n"
. "While the band's playin'\r\n"
. "One of your old favourite songs from way back when\r\n"
. "So, take her wrap, fellas\r\n"
. "Find her an empty lap, fellas\r\n"
. "Dolly'll never go away again\r\n"
. "Hello, Dolly\r\n"
. "Well, hello, Dolly\r\n"
. "It's so nice to have you back where you belong\r\n"
. "You're lookin' swell, Dolly\r\n"
. "I can tell, Dolly\r\n"
. "You're still glowin', you're still crowin'\r\n"
. "You're still goin' strong\r\n"
. "We feel the room swayin'\r\n"
. "While the band's playin'\r\n"
. "One of your old favourite songs from way back when\r\n"
. "Golly, gee, fellas\r\n"
. "Find her a vacant knee, fellas\r\n"
. "Dolly'll never go away\r\n"
. "Dolly'll never go away\r\n"
. "Dolly'll never go away again"
;
// Here we split it into lines
$lyrics = explode( "\n", $lyrics );
// And then randomly choose a line
return $this->current_lyric = wptexturize( $lyrics[ mt_rand( 0, count( $lyrics ) - 1 ) ] );
}
// This just echoes the chosen line, we'll position it later
public function hello_dolly() {
//* Set lyric in $this->current_lyric
$this->hello_dolly_set_lyric();
echo '<p id='dolly'>' . $this->current_lyric . '</p>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment