TJS Childtheme helpers scripts
<?php
/**
* -------------------------------------------------------------------------------------------------
* WP Query Helpers
* -------------------------------------------------------------------------------------------------
* This class provides quick handy functions for interacting with the WP_Query object.
*
* Contents
* 1. Page and Post functions ............... functions for interaction with Posts, Pages and CPT's
* 2. Category functions .................... Functions for interaction with WP categories
* 3. Meta functions ........................ Functions for retrieving meta values
*
* @author David Fravigar <david.fravigar@me.com>
* @version 1.0.0
* -------------------------------------------------------------------------------------------------
*/
/**
* -------------------------------------------------------------------------------------------------
* Stop Direct Access
* -------------------------------------------------------------------------------------------------
*/
if ( !defined( 'ABSPATH' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit();
}
/**
* -------------------------------------------------------------------------------------------------
* The Class
* -------------------------------------------------------------------------------------------------
*/
class CO_WPQueryHelpers
{
/**
* -----------------------------------------------------------------------------------------------
* Fetch Posts
* -----------------------------------------------------------------------------------------------
* Fetch posts by post type
* @param string $posttype
* @param int $limit
* @param array $passedArgs
* @return array $query
* -----------------------------------------------------------------------------------------------
*/
public static function fetchPosts($posttype='posts', $limit=-1, $passedArgs = array())
{
global $wp_query;
$args = array_merge(array(
'post_type' => $posttype,
'post_status' => 'publish',
'posts_per_page' => $limit,
), $passedArgs);
$query = new WP_Query($args);
wp_reset_postdata();
wp_reset_query();
return $query;
}
/**
* -----------------------------------------------------------------------------------------------
* List Posts
* -----------------------------------------------------------------------------------------------
* A function that returns a list of all posts in wordpress.
* @param string $postType
* @param int $limit
* @return array $list
* -----------------------------------------------------------------------------------------------
*/
public static function listPosts($postType, $limit)
{
global $wp_query;
$posts = self::fetch_posts($postType, $limit);
$list = array();
$posts = new WP_Query($args);
while($posts->have_posts()) : $posts->the_post();
$name = get_the_title();
$id = get_the_id();
$list[$name] = $id;
endwhile;
$posts->reset_postdata();
return $list;
}
/**
* -----------------------------------------------------------------------------------------------
* [getPostByTitle description]
* -----------------------------------------------------------------------------------------------
* @param string $page_title
* @param string $post_type
* @param $output $output
* @return object
* -----------------------------------------------------------------------------------------------
*/
public static function getPostByTitle($page_title, $post_type ='post' , $output = OBJECT) {
global $wpdb;
$post = $wpdb->get_var($wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type));
if ($post)
return get_post($post, $output);
return null;
}
/**
* -----------------------------------------------------------------------------------------------
* getCategories
* -----------------------------------------------------------------------------------------------
* get the categories.
* @return array $categories
* -----------------------------------------------------------------------------------------------
*/
public static function getCategories()
{
$categories = get_categories();
wp_reset_query();
return $categories;
}//end getCategories
/**
* -----------------------------------------------------------------------------------------------
* getCategoryByName
* -----------------------------------------------------------------------------------------------
* Get category by name
* @param string $name
* @return array $category
* -----------------------------------------------------------------------------------------------
*/
public static function getCategoryByName($name)
{
$category = get_term_by('name', $name, 'category');
wp_reset_query();
return $category;
}//end getCategoryByName
/**
* -----------------------------------------------------------------------------------------------
* getCategoryBySlug
* -----------------------------------------------------------------------------------------------
* get a category by it's slug
* @param string $slug
* @return array $category
* -----------------------------------------------------------------------------------------------
*/
public static function getCategoryBySlug($slug)
{
$category = get_term_by('slug', $slug, 'category');
wp_reset_query();
return $category;
}
/**
* -----------------------------------------------------------------------------------------------
* getCategoryByMetaKey
* -----------------------------------------------------------------------------------------------
* Get category meta by category meta type.
* @param string $meta
* @return array $returnedCategories
* -----------------------------------------------------------------------------------------------
*/
public function getCategoryByMetaKey($meta)
{
$returnedcategories = array();
$categories = self::getCategories();
foreach($categories as $category) {
$t_id = $category->term_id;
$cat_meta = get_option("category_$t_id");
if($cat_meta['type'] == $meta) {
$returnedcategories[] = $category;
}
}
wp_reset_query();
return $returnedcategories;
}
/**
* -----------------------------------------------------------------------------------------------
* Get Category meta by category slug
* -----------------------------------------------------------------------------------------------
* @param [type] $slug [description]
* @return [type] [description]
* -----------------------------------------------------------------------------------------------
*/
public static function getCategoryMetaBySlug($slug) {
$category = get_category_by_slug($slug);
$t_id = $category->term_id;
$cat_meta = get_option("category_$t_id");
return $cat_meta;
}
}<?php
/**
* -------------------------------------------------------------------------------------------------
* General Helpers Class
* -------------------------------------------------------------------------------------------------
* A static Class with general functions in it that don't fit in to other classes
*
* @author David Fravigar <david.fravigar@me.com>
* @version 0.0.1
* -------------------------------------------------------------------------------------------------
*/
/**
* -------------------------------------------------------------------------------------------------
* Stop Direct Access
* -------------------------------------------------------------------------------------------------
*/
if ( !defined( 'ABSPATH' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit();
}
/**
* -------------------------------------------------------------------------------------------------
* The class
* -------------------------------------------------------------------------------------------------
*/
class CO_GeneralHelpers extends CO_WPQueryHelpers
{
/**
* -----------------------------------------------------------------------------------------------
* If array key exists
* -----------------------------------------------------------------------------------------------
*/
public static function inArray($key, $array)
{
if(array_key_exists($key, $array)) {
return true;
} else {
return false;
}
}
/**
* -----------------------------------------------------------------------------------------------
* Modify String Function
* -----------------------------------------------------------------------------------------------
* Use the built in php string functions
* @param string $string The string to change
* @param string $method The method to use
* @return string $changedString The modified String
* -----------------------------------------------------------------------------------------------
*/
public static function modifyString($string, $method)
{
$changedString = '';
switch ($method) {
case 'lowercase':
$changedString = strtolower($string);
break;
case 'uppercase':
$changedString = lcfirst($string);
break;
case 'ucfirst':
$changedString = strtoupper($string);
break;
case 'ucwords':
$changedString = ucwords($string);
break;
default:
break;
}
return $changedString;
}//end modify_string
/**
* -----------------------------------------------------------------------------------------------
* Replace Function
* -----------------------------------------------------------------------------------------------
* @param string $string the string we want to change
* @param string $with what we want to replace
* @param string $what what to replace with
* @return string
* -----------------------------------------------------------------------------------------------
*/
public static function replace($string, $with, $what)
{
return $string = str_replace($with, $what, $string);
}
/**
* -----------------------------------------------------------------------------------------------
* Uglify a string
* -----------------------------------------------------------------------------------------------
* @param [type] $string [description]
* @param [type] $method [description]
* @return [type] [description]
* -----------------------------------------------------------------------------------------------
*/
public static function ugliyString($string, $method)
{
$string = self::modifyString($string, 'lowercase');
switch($method) {
case 'param':
$string = self::replace($string, ' ', '_');
$string = self::replace($string, '_', '_');
break;
case 'url':
$string = self::replace($string, ' ', '-');
$string = self::replace($string, '_', '-');
break;
}
return $string;
}
/**
* -----------------------------------------------------------------------------------------------
* Handsome String function
* -----------------------------------------------------------------------------------------------
* @param [type] $string [description]
* @return [type] [description]
* -----------------------------------------------------------------------------------------------
*/
public static function handsomeString($string)
{
$string = self::replace($string, '-', ' ');
$string = self::replace($string, '_', ' ');
$string = self::modifyString($string, 'ucwords');
return $string;
}
/**
* -----------------------------------------------------------------------------------------------
* [puralise description]
* -----------------------------------------------------------------------------------------------
* @param [type] $string [description]
* @return [type] [description]
* -----------------------------------------------------------------------------------------------
*/
public static function puralise($string)
{
$string = $string . 's';
return $string;
}
/**
* -----------------------------------------------------------------------------------------------
* [getDataBetween description]
* -----------------------------------------------------------------------------------------------
* @param [type] $string [description]
* @param [type] $start [description]
* @param [type] $end [description]
* @return [type] [description]
* -----------------------------------------------------------------------------------------------
*/
public static function getDataBetween($string, $start, $end)
{
$sp = strpos($string, $start)+strlen($start);
$ep = strpos($string, $end)-strlen($start);
$data = trim(substr($string, $sp, $ep));
return trim($data);
}
/**
* -----------------------------------------------------------------------------------------------
* [createSlug description]
* -----------------------------------------------------------------------------------------------
* @param [type] $name [description]
* @return [type] [description]
* -----------------------------------------------------------------------------------------------
*/
public static function createSlug($name, $prefix='co_')
{
$name = self::ugliyString($prefix . $name, 'url');
return $name;
}
/**
* -----------------------------------------------------------------------------------------------
* Convert Term slug to ID
* -----------------------------------------------------------------------------------------------
* Convert a taxonomy slug to it's ID
* @param string $slug [description]
* @param string $slug [description]
* @return string | bool
* -----------------------------------------------------------------------------------------------
*/
public static function convertTermSlugToId($slug, $taxonomy='category')
{
$term = get_term_by('slug', $slug, 'category');
if($term && !is_wp_error($term)) {
return $term->term_id;
}
return false;
}
/**
* -----------------------------------------------------------------------------------------------
* Include Files Function
* -----------------------------------------------------------------------------------------------
* require files in a direction by extension.
* -----------------------------------------------------------------------------------------------
*/
public static function includeFiles($sourceFolder, $extension='.php')
{
if(!is_dir($sourceFolder)) {
die ("Invalid directory.\n\n");
}
//$path = rtrim($extension, '/');
$extension = '*'.$extension;
$FILES = glob("$sourceFolder/{$extension}", GLOB_BRACE);
foreach($FILES as $key => $file) {
require_once($file);
}
}
}//end class<?php
/**
* ---------------------------------------------------------------------------------------------------------------------
* Admin Helpers
* ---------------------------------------------------------------------------------------------------------------------
* A static class for common Admin functions. Class extends the WP Query helper class
*
* @see \framework\helpers\wp_query_helpers.php
* @author David Fravigar <david@tjs.co.uk>
* @version 0.0.1
* ---------------------------------------------------------------------------------------------------------------------
*/
/**
* ---------------------------------------------------------------------------------------------------------------------
* Stop Direct Access
* ---------------------------------------------------------------------------------------------------------------------
*/
if ( !defined( 'ABSPATH' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit('These are not the scripts you are looking for... move along, move along.');
}
/**
* ---------------------------------------------------------------------------------------------------------------------
* The Class
* ---------------------------------------------------------------------------------------------------------------------
*/
class TJS_AdminHelpers extends TJS_WPQueryHelpers
{
/**
* -------------------------------------------------------------------------------------------------------------------
* List Directory Files
* -------------------------------------------------------------------------------------------------------------------
* @param string $path - the path of the directory we want to scan
* @param string $type - set either file or directory listings defaults to directory listing
* @return array $files - the directories or files that are in the path
* -------------------------------------------------------------------------------------------------------------------
*/
public static function tjs_listDirectoryFiles($path, $type='dir')
{
if($path == '') {
throw new Exception('No path defined in: ' . __FILE__ . ' on ' . __LINE__);
}
$files = array();
$openDir = opendir($path);
while(($file = readdir($openDir)) !== false) {
$fullFilePath = realpath("$path/$file");
if($file[0] != ".") {
switch($type) {
case 'dirs':
if(is_dir($fullFilePath)){
$files[$file] = $fullFilePath;
}
break;
case 'files':
if(is_file($fullFilePath)){
$files[$file] = $fullFilePath;
}
break;
default:
if(is_dir($fullFilePath)){
$files[$file] = $fullFilePath;
}
break;
}
}
}
return $files;
}
/**
* -------------------------------------------------------------------------------------------------------------------
* Include Files
* -------------------------------------------------------------------------------------------------------------------
* include files from a directory passed to it.
* Throws an exception if the directory does not exist.
* @param string $path - the directory of the files we want to include.
* -------------------------------------------------------------------------------------------------------------------
*/
public static function includeFiles($path)
{
if($path == '') {
throw new Exception('No path defined in: ' . __FILE__ . ' on ' . __LINE__);
}
$files = self::tjs_listDirectoryFiles($path, 'files');
foreach($files as $file) {
require_once($file);
}
}
/**
* -------------------------------------------------------------------------------------------------------------------
* Register Assets
* -------------------------------------------------------------------------------------------------------------------
* NOTE This function needs to be reworked to use uri not dir
* -------------------------------------------------------------------------------------------------------------------
*/
// public static function registerAssets($path)
// {
// if($path == '') {
// throw new Exception('No path defined in: ' . __FILE__ . ' on ' . __LINE__);
// }
// $allowedFiles = array('css', 'js');
// $files = self::tjs_listDirectoryFiles($path, 'files');
// foreach($files as $file => $path)
// {
// $fileExt = TJS_GeneralHelpers::getFileExtension($file);
// if(TJS_GeneralHelpers::inArray($fileExt, $allowedFiles)) {
// if($fileExt == 'js') {
// wp_register_script($file, $path, array('jquery'), '0.0.1', true);
// }
// if($fileExt == 'css') {
// wp_register_style($file, $path, '', '', 'all');
// }
// }
// }
// }
/**
* -------------------------------------------------------------------------------------------------------------------
*
* -------------------------------------------------------------------------------------------------------------------
* * NOTE This function needs to be reworked to use uri not dir
* -------------------------------------------------------------------------------------------------------------------
*/
// public static function enqueueAssets($path)
// {
// if($path == '') {
// throw new Exception('No path defined in: ' . __FILE__ . ' on ' . __LINE__);
// }
// $allowedFiles = array('css', 'js');
// $files = self::tjs_listDirectoryFiles($path, 'files');
// foreach($files as $file => $path)
// {
// $fileExt = TJS_GeneralHelpers::getFileExtension($file);
// if(TJS_GeneralHelpers::inArray($fileExt, $allowedFiles)) {
// if($fileExt == 'js') {
// wp_enqueue_script($file);
// }
// if($fileExt == 'css') {
// wp_enqueue_style($file);
// }
// }
// }
// }
/**
* -------------------------------------------------------------------------------------------------------------------
* Register Shortcode Assets
* -------------------------------------------------------------------------------------------------------------------
* * NOTE This function needs to be reworked to use uri not dir
* -------------------------------------------------------------------------------------------------------------------
*/
// public static function registerShortcodeAssets($shortcodesDir)
// {
// if($shortcodesDir == '') {
// throw new Exception('No path defined in: ' . __FILE__ . ' on ' . __LINE__);
// }
// $shortcodes = self::tjs_listDirectoryFiles($shortcodesDir);
// foreach($shortcodes as $shortcodeDir) {
// $shortcodeFolder = basename($shortcodeDir);
// $shortcode = explode('_', $shortcodeFolder);
// if($shortcode[0] == 'tjs') {
// self::registerAssets($shortcodeDir);
// self::enqueueAssets($shortcodeDir);
// }
// }
// }
}//end class