wir
9/16/2016 - 1:23 PM

Advanced WP: The Basics Of Object Oriented Programming

Advanced WP: The Basics Of Object Oriented Programming

<p>These are just 8 of our awesome posts. Click here for <?php echo $posts->found_posts - 8 ?> more!</p>
$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'
))
$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;


$chirp = new Chirp( 'This is a chirp with an #example hashtag created with code that is #procedural' );
echo $chirp->get_chirp;
class Chirp {
  // Class code
}
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 {
    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;
    }

}
$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();
// 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 );