Completed
Branch develop (1becb4)
by Aristeides
05:50 queued 02:46
created

Kirki_Toolkit::clean_file_path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * The main Kirki object
4
 *
5
 * @package     Kirki
6
 * @category    Core
7
 * @author      Aristeides Stathopoulos
8
 * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
9
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
10
 * @since       1.0
11
 */
12
13
// Exit if accessed directly
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
if ( ! class_exists( 'Kirki_Toolkit' ) ) {
19
	final class Kirki_Toolkit {
20
21
		/**
22
		 * @static
23
		 * @access protected
24
		 * @var object
25
		 */
26
		protected static $instance = null;
27
28
		/**
29
		 * @static
30
		 * @access protected
31
		 * @var string
32
		 */
33
		protected static $version = '2.2.0.beta.3';
34
35
		/**
36
		 * Access the single instance of this class
37
		 *
38
		 * @static
39
		 * @access public
40
		 * @return Kirki_Toolkit
41
		 */
42
		public static function get_instance() {
43
			if ( null == self::$instance ) {
44
				self::$instance = new self();
45
			}
46
			return self::$instance;
47
		}
48
49
		/**
50
		 * Return true if we are debugging Kirki
51
		 *
52
		 * @access public
53
		 * @return bool
54
		 */
55
		public static function is_debug() {
56
			return (bool) ( ( defined( 'KIRKI_DEBUG' ) && KIRKI_DEBUG ) || ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) );
57
		}
58
59
		/**
60
		 * Take a path and return it clean
61
		 *
62
		 * @param string $path
63
		 * @return string
64
		 */
65
		public static function clean_file_path( $path ) {
66
			$path = wp_normalize_path( $path );
67
			return rtrim( $path, '/' );
68
		}
69
70
		/**
71
		 * Determine if we're on a parent theme
72
		 *
73
		 * @param $file string
74
		 * @return bool
75
		 */
76
		public static function is_parent_theme( $file ) {
77
			$file = self::clean_file_path( $file );
78
			$dir  = self::clean_file_path( get_template_directory() );
79
			return ( false !== strpos( $file, $dir ) );
80
		}
81
82
		/**
83
		 * Determine if we're on a child theme.
84
		 *
85
		 * @param $file string
86
		 * @return bool
87
		 */
88
		public static function is_child_theme( $file ) {
89
			$file = self::clean_file_path( $file );
90
			$dir  = self::clean_file_path( get_stylesheet_directory() );
91
			return ( false !== strpos( $file, $dir ) );
92
		}
93
94
		/**
95
		 * Determine if we're running as a plugin
96
		 */
97
		public static function is_plugin() {
98
			if ( false !== strpos( self::clean_file_path( __FILE__ ), self::clean_file_path( get_stylesheet_directory() ) ) ) {
99
				return false;
100
			}
101
			if ( false !== strpos( self::clean_file_path( __FILE__ ), self::clean_file_path( get_template_directory_uri() ) ) ) {
102
				return false;
103
			}
104
			return ( false !== strpos( self::clean_file_path( __FILE__ ), self::clean_file_path( WP_CONTENT_DIR . '/themes/' ) ) );
105
		}
106
107
		/**
108
		 * Determine if we're on a theme
109
		 *
110
		 * @param $file string
111
		 * @return bool
112
		 */
113
		public static function is_theme( $file ) {
114
			return ( true === self::is_child_theme( $file ) || true === self::is_parent_theme( $file ) );
115
		}
116
117
		/**
118
		 * Get the version
119
		 *
120
		 * @return string
121
		 */
122
		public static function get_version() {
123
			return self::$version;
124
		}
125
126
	}
127
128
}
129