Skip to content

Instantly share code, notes, and snippets.

@danielpataki
Last active October 7, 2018 17:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save danielpataki/125eac6213f3bb67ef9510fb8978f597 to your computer and use it in GitHub Desktop.
Save danielpataki/125eac6213f3bb67ef9510fb8978f597 to your computer and use it in GitHub Desktop.
Advanced WP: The Basics Of Object Oriented Programming
// Cut text down to required length
function get_chirp_text( $text ) {
return substr( $text, 0, 200 );
}
// Parse hashtags from text
function get_hashtags( $text ) {
preg_match_all("/S*#((?:\[[^\]]+\]|\S+))/", $text, $matches);
return $matches;
}
// Create the final chirp text
function create_chirp( $text ) {
$chirp_text = get_chirp_text( $text );
$hastags = get_hashtags( $chirp_text );
if( !empty( $hastags[1] ) ) {
foreach( $hastags[1] as $key => $match ) {
$hastags[1][$key] = "<a href='http://chirp.chip/hastags/" . $match . "/'>" . '#' . $match . "</a>";
}
$chirp_text = str_replace( $hastags[0], $hastags[1], $chirp_text );
}
return $chirp_text;
}
$text = 'This is a chirp with an #example hashtag created with code that is #procedural';
echo create_chirp( $text );
$chirp = new Chirp( 'This is a chirp with an #example hashtag created with code that is #procedural' );
// Hashtags will not be links here
echo $chirp->get_chirp();
$chirp->set_hashtags();
// Hashtags are now links because they have been set
echo $chirp->get_chirp();
class Chirp {
var $text;
var $length;
var $hashtags;
var $chirp;
function __construct( $text ) {
$this->hashtag_base = 'http://chirp.chip/hastags/';
$this->text = $text;
$this->set_length();
}
function set_length() {
$this->length = 200;
}
function set_hashtags() {
preg_match_all("/S*#((?:\[[^\]]+\]|\S+))/", $this->text, $matches);
$hashtags = array();
foreach( $matches[1] as $key => $match ) {
$hashtags['#' . $match] = "<a href='http://chirp.chip/hastags/" . $match . "/'>" . '#' . $match . "</a>";
}
$this->hashtags = $hashtags;
}
function get_chirp() {
$chirp = substr( $this->text, 0, $this->length );
if( !empty( $this->hashtags ) ) {
$chirp = str_replace( array_keys( $this->hashtags ) , array_values( $this->hashtags ), $chirp);
}
return $chirp;
}
}
class Chirp {
var $text;
var $length;
var $hashtag_base;
var $hashtags;
var $chirp;
function __construct( $text ) {
$this->hashtag_base = 'http://chirp.chip/hastags/';
$this->text = $text;
$this->set_length();
$this->set_hashtags();
$this->set_chirp();
}
function set_length() {
$this->length = 200;
}
function set_hashtags() {
preg_match_all("/S*#((?:\[[^\]]+\]|\S+))/", $this->text, $matches);
$hashtags = array();
foreach( $matches[1] as $key => $match ) {
$hashtags['#' . $match] = "<a href='http://chirp.chip/hastags/" . $match . "/'>" . '#' . $match . "</a>";
}
$this->hashtags = $hashtags;
}
function set_chirp() {
$chirp = substr( $this->text, 0, $this->length );
$chirp = str_replace( array_keys( $this->hashtags ) , array_values( $this->hashtags ), $chirp);
$this->chirp = $chirp;
}
}
$chirp = new Chirp( 'This is a chirp with an #example hashtag created with code that is #procedural' );
echo $chirp->chirp;
class Chirp {
// Class code
}
$chirp = new Chirp( 'This is a chirp with an #example hashtag created with code that is #procedural' );
echo $chirp->get_chirp;
$chirp_one = new Chirp( 'This is a chirp with an #example hashtag created with code that is #procedural' );
echo $chirp_one->chirp;
$chirp_two = new Chirp( 'This is a chirp with an #example hashtag created with code that is #procedural' );
echo $chirp_two->chirp;
$posts = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 8,
'post_status' => 'publish'
));
$books = new WP_Query(array(
'post_type' => 'book',
'posts_per_page' => 3,
'post_status' => 'future'
))
<p>These are just 8 of our awesome posts. Click here for <?php echo $posts->found_posts - 8 ?> more!</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment