Skip to content

Instantly share code, notes, and snippets.

@sjerpbouwtsites
Last active May 29, 2018 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjerpbouwtsites/246d262a3230309bb21e9ab291142d4b to your computer and use it in GitHub Desktop.
Save sjerpbouwtsites/246d262a3230309bb21e9ab291142d4b to your computer and use it in GitHub Desktop.
bootstrap card class - NEDERLANDS
<?php
Class Bs_card {
// je kan direct een wp post als 'post' in deze class hangen,
// per img, titel, tekst het apart zetten of door elkaar.
// direct ingestelde img, titel, tekst en link waarden overschrijven altijd de
// uit de post afgeleide waarde.
function __construct($ar = array()){
$this->initialiseer($ar);
}
private function initialiseer($ar) {
// ieder waarde => sleutel paar wordt als zodanig in de class, in $this, gezet
foreach ($ar as $sleutel => $waarde) {
$this->$sleutel = $waarde;
}
if (!$this->geen_lege_eigenschap('card_class')) $this->card_class = '';
$this->controleer_href();
$this->bouw_img();
}
private function controleer_href () {
if (!$this->geen_lege_eigenschap('link') and $this->geen_lege_eigenschap('post')) {
$this->link = get_the_permalink($this->post->ID);
}
$this->heeft_link = $this->geen_lege_eigenschap('link');
}
public function samenvatting() {
//zet in de initialisatie de waarde tekst_limiet voor een handmatige tekenlimiet.
if ($this->geen_lege_eigenschap('post_excerpt')) {
return $this->post->post_excerpt;
} else {
$charlength = ($this->geen_lege_eigenschap('tekst_limiet') ? $this->tekst_limiet : 100);
$r = "";
$w = strip_tags($this->post->post_content);
if ( mb_strlen( $w ) > $charlength ) {
$subex = mb_substr( $w, 0, $charlength - 3 );
$exwords = explode( ' ', $subex );
$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
if ( $excut < 0 ) {
$r.= mb_substr( $subex, 0, $excut );
} else {
$r.= $subex;
}
$r = rtrim($r);
$r.= '...';
return $r;
} else {
return $this->post->post_content;
}
}
}
private function open_link($echo = false) {
// open en sluit link staan op meerdere plekken in de class zodat er tussendoor
// afwijkende links geplaatst kunnen worden. <a> tags mogen namelijk niet genest worden
// en als de hele card klikbaar is sluit dat dus afwijkende extra links in de card uit.
$r = ($this->heeft_link ? "<a href='{$this->link}'>" : '');
if ($echo) {
echo $r;
} else {
return $r;
}
}
private function sluit_link($echo = false) {
$r = ($this->heeft_link ? "</a>" : '');
if ($echo) {
echo $r;
} else {
return $r;
}
}
private function card_class() {
// uitgedraaid in class attribuut van div.card
if ($this->geen_lege_eigenschap('card_class')) {
echo $this->card_class;
}
}
private function geen_lege_eigenschap ($eigenschap = '') {
// stel dat $eigenschap = 'titel', dan hebben we het over $this->titel
// deze functie controleert of uberhaupt $this->eigenschap bestaat,
// en zo ja, of die dan niet leeg is.
// functie geeft dit als een booleaan terug.
return (property_exists($this, $eigenschap) and $this->$eigenschap !== '');
}
private function terugval_afbeelding() {
$terugval = (get_field('terugval_afbeelding', 'option'))['sizes']['card'];
if ($terugval and $terugval !== '') {
$this->img = "<img src='".$terugval."'/>";
} else {
$this->card_class = $this->card_class .= ' geen-afbeelding';
}
}
private function bouw_img() {
// dient volledige <img src='' alt='' width='' height='' /> draad te zijn.
// als niet gegeven, wordt opgehaald uit post, indien die afb heeft.
$extra_class = $this->geen_lege_eigenschap('img_class') ? $this->img_class : '' ;
ob_start();
echo "<div class='card-img $extra_class'>";
if ($this->geen_lege_eigenschap('img')) {
echo $this->open_link() . $this->img . $this->sluit_link();
} else if ($this->geen_lege_eigenschap('post')) {
if (has_post_thumbnail($this->post)) {
//nu kan de thumb leeg zijn...
$tpt = get_the_post_thumbnail($this->post, 'card');
if ($tpt and $tpt !== '') {
$this->img = $tpt;
} else {
if (!!strpos($this->post->post_content, '<img')) {
// begin stringstring<img src='' alt='' width='' height='' />stringstring
$expl1 = explode('<img', $this->post->post_content);
// nu kan de afbeelding als eerste zijn gekomen, of als tweede.
if (!!strpos($expl1[0], '<img')) {
$e1p = $expl1[0];
} else {
$e1p = $expl1[1];
}
// src='' alt='' width='' height='' />stringstringstring
$expl2 = explode('/>', $e1p);
// src='' alt='' width='' height=''
$e2p = $expl2[0];
$this->img = "<img " . $e2p . " />";
} else {
$this->terugval_afbeelding();
}
}
} else {
$this->terugval_afbeelding();
}
echo $this->img;
} else {
$this->card_class = $this->card_class .= ' geen-afbeelding';
}
echo "</div>";
$this->img_HTML = ob_get_clean();
}
private function print_img (){
echo $this->img_HTML;
}
private function titel() {
if ($this->geen_lege_eigenschap('titel')) {
echo "<h3 class='card-title'>{$this->titel}</h3>";
} else if ($this->geen_lege_eigenschap('post')) {
$this->titel = $this->post->post_title;
echo "<h3 class='card-title'>{$this->titel}</h3>";
}
}
private function sub_titel() {
if ($this->geen_lege_eigenschap('sub_titel')) {
echo $this->sub_titel;
}
}
private function tekst() {
if ($this->geen_lege_eigenschap('tekst')) {
echo apply_filters('the_content', $this->tekst);
} else if ($this->geen_lege_eigenschap('post')) {
$this->tekst = $this->samenvatting();
echo apply_filters('the_content', $this->tekst);
}
}
private function knop_class() {
if ($this->geen_lege_eigenschap('knop_class')) {
echo $this->knop_class;
} else {
echo "btn btn-primary";
}
}
private function knop(){
if ($this->geen_lege_eigenschap('knop_tekst')) {?>
<footer class="row">
<div class="col pt-2">
<?=$this->open_link()?>
<span class="<?=$this->knop_class()?>">
<?=$this->knop_tekst?>
</span>
<?=$this->sluit_link()?>
</div>
</footer>
<?php }
}
public function maak_html(){
// ob_start = output buffer start.
// maw je produceert normale HTML, maar met de intentie om die later op te vangen. Dit gaat dus nog niet
// naar de browser. Met ob_get_clean wordt het straks opgevangen en als een draad opgeslagen.
ob_start();
?>
<div class="card <?=$this->card_class()?>">
<?=$this->print_img()?>
<div class="card-body">
<?php
echo $this->open_link();
$this->titel();
$this->sub_titel();
$this->tekst();
echo $this->sluit_link();
$this->knop();
?>
</div>
</div>
<?php
$this->html = ob_get_clean();
return $this->html;
}
public function print(){
if (!$this->geen_lege_eigenschap('html')) {
$this->maak_html();
}
echo $this->html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment