<?php
/**
 * Assorted.
 *
 * @package MadeByAura\Prime\Helpers
 * @author  MadeByAura.com
 */

namespace MadeByAura\Prime\Helpers;

defined( 'ABSPATH' ) || die();

/**
 * Assorted.
 */
class Assorted {
	/**
	 * Remove invalid characters from a string, and replace underscores,
	 * whitespace, forward slash, and back slash characters with dashes.
	 *
	 * @param  string $str  String that needs to dashified.
	 * @return string $str
	 */
	public static function dashify( $str ) {
		// Remove whitespace from the front and end, and lowercase all characters.
		$str = strtolower( trim( $str ) );

		// Remove all characters other than alphabets, numbers, dash, underscore,
		// whitespace, forward slash, and back slash.
		$str = preg_replace( '/[^a-z0-9-_\s\/\\\]/', '', $str );

		// Replace dash, underscore, whitespace, forward slash, and back slash
		// characters with dashes.
		$str = preg_replace( '/[-_\s\/\\\]+/', '-', $str );

		return $str;
	}

	/**
	 * Get file content.
	 *
	 * @param  string $file_path  File path.
	 * @return string
	 */
	public static function get_file_content( $file_path ) {
		// Early exit if file doesn't exist.
		if ( ! file_exists( $file_path ) ) {
			return '';
		}

		// Return file contents using output butter.
		ob_start();
			include_once $file_path;
		return ob_get_clean();
	}

	/**
	 * Check if a plugin is active.
	 *
	 * @param  string $plugin_base_file  Base plugin path.
	 * @return bool
	 */
	public static function is_plugin_active( $plugin_base_file ) {
		// Do not proceed if the base file of the plugin does not exist.
		if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin_base_file ) ) {
			return false;
		}

		// This filed is required in order to make is_plugin_active in front end.
		require_once ABSPATH . 'wp-admin/includes/plugin.php';

		// Check if the plugin is active.
		return is_plugin_active( $plugin_base_file );
	}
}
