Skip to content

Commit

Permalink
Added enqueue method
Browse files Browse the repository at this point in the history
  • Loading branch information
zohaib87 committed Oct 4, 2023
1 parent 6321789 commit f870031
Showing 1 changed file with 66 additions and 12 deletions.
78 changes: 66 additions & 12 deletions helpers/class-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,58 @@
class Helpers {

/**
* Auto load files from a directory.
* # Enqueue style or script with auto version control
*
* @link https://developer.wordpress.org/reference/functions/wp_enqueue_style/
* @link https://developer.wordpress.org/reference/functions/wp_enqueue_script/
*
* @param string $script Accepts 'style' or 'script'
* @param string $handle Name of the script. Should be unique.
* @param string $src Path of the script relative to plugins folder.
* @param array $deps An array of registered script handles this script depends on.
* @param string $media The media for which this stylesheet has been defined.
* @param bool $in_footer Whether to enqueue the script before </body> instead of in the <head>.
* @param string $ver Version of the script.
*/
public static function auto_load_files($path) {
public static function enqueue( $script, $handle, $src = '', $deps = array(), $media = 'all', $in_footer = true, $ver = '' ) {

$files = glob($path);
$ver = empty($ver) ? filemtime( get_template_directory() . $src ) : $ver;

if ( $script == 'style' ) {

wp_enqueue_style( esc_attr($handle), get_template_directory_uri() . esc_attr($src), $deps, esc_attr($ver), esc_attr($media) );

} elseif ( $script == 'script' ) {

wp_enqueue_script( esc_attr($handle), get_template_directory_uri() . esc_attr($src), $deps, esc_attr($ver), $in_footer );

}

}

/**
* # Auto load files from a directory
*
* @param string $path Path to files (*.php) that needs to be auto loaded.
*/
public static function auto_load_files( $path ) {

$files = glob( $path );

foreach ( $files as $file ) {

foreach ($files as $file) {
if (basename($file) == 'index.php') continue;

require($file);

}

}

/**
* Array of header or footer styles.
*/
public static function files_array($prefix, $override = false) {
public static function files_array( $prefix, $override = false ) {

$files = glob( get_stylesheet_directory() . '/views/'.$prefix.'*.php' );
$files = glob( get_template_directory() . '/views/'.$prefix.'*.php' );
Expand Down Expand Up @@ -108,20 +143,26 @@ public static function get_sidebars() {
global $wp_registered_sidebars;
$data = array();

foreach ($GLOBALS['wp_registered_sidebars'] as $sidebar) {
foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) {

$key = $sidebar['id'];
$val = $sidebar['name'];
$data[$key] = $val;

}

return $data;

}

/**
* Minifying styles
* # Minifying styles
*
* @param string $css Not compressed css.
*
* @return string of minified css.
*/
public static function minify_css($css) {
public static function minify_css( $css ) {

$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);
$css = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $css);
Expand All @@ -140,9 +181,13 @@ public static function minify_css($css) {
}

/**
* Hex color to rgb conversion
* # Hex color to rgb conversion
*
* @param string $color Hex color code.
*
* @return string of RGB color.
*/
public static function hex2rgb($color) {
public static function hex2rgb( $color ) {

if ( $color[0] == '#' ) {
$color = substr( $color, 1 );
Expand All @@ -164,9 +209,14 @@ public static function hex2rgb($color) {
}

/**
* Darken or Lighten Color
* # Darken or Lighten Color
*
* @param string $color Hex color code.
* @param int $dif Number amount of lightning or darkening.
*
* @return string of lighter or darker color.
*/
public static function darken($color, $dif=20) {
public static function darken( $color, $dif = 20 ) {

$color = str_replace('#','', $color);
$rgb = '';
Expand Down Expand Up @@ -202,6 +252,10 @@ public static function darken($color, $dif=20) {

/**
* # Adjusting spacing of classes
*
* @param array $classes An array of classes
*
* @return string of classes with single space in between.
*/
public static function classes( $classes = array() ) {

Expand Down

0 comments on commit f870031

Please sign in to comment.