Skip to content

Instantly share code, notes, and snippets.

@olirabt
Created November 14, 2017 10:56
Show Gist options
  • Save olirabt/93306fe9d57406891ee51cac40b7c771 to your computer and use it in GitHub Desktop.
Save olirabt/93306fe9d57406891ee51cac40b7c771 to your computer and use it in GitHub Desktop.
wordpress-helpers
<?php
class Helpers {
protected $_post_id;
protected $_category_names_arr = array();
protected $_category_slugs_arr = array();
public function __construct($post_id = '') {
$this->setPostId($post_id);
}
public function setPostId($post_id) {
if(!empty($post_id) && is_numeric($post_id)) {
$this->_post_id = $post_id;
return $this;
} else {
$this->_post_id = '';
return $this;
}
}
public function getPostId() {
return $this->_post_id;
}
public function replacePipeWithBreak(string $text_to_format) {
$formatted_text = str_replace('|', '&nbsp;<br>', $text_to_format);
return $formatted_text;
}
public function remove_lang_suffix($slug) {
$suffix = substr($slug, -3);
if (strpos($suffix, '-') !== false) {
return str_replace($suffix, '', $slug);
} else {
return $slug;
}
}
public function getCategoriesNames() {
foreach(get_the_category($this->_post_id) as $value) {
array_push($this->_category_names_arr, $value->name);
}
return $this->_category_names_arr;
}
public function getCategoriesSlugs() {
foreach(get_the_category($this->_post_id) as $value) {
array_push($this->_category_slugs_arr, $this->remove_lang_suffix($value->slug));
}
return $this->_category_slugs_arr;
}
public function get_formatted_title() {
return $this->replacePipeWithBreak(get_the_title(($this->_post_id)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment