-
Notifications
You must be signed in to change notification settings - Fork 4
/
odin-toolkit.php
123 lines (112 loc) · 2.97 KB
/
odin-toolkit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* Plugin Name: Odin Toolkit
* Plugin URI: https://github.com/wpbrasil/odin-toolkit
* Description: Use to add functionalities to be used in plugin or theme, or override the files to make your awesome new plugin.
* Version: 0.0.1
* Author: WPBrasil
* Author URI: https://github.com/wpbrasil/
* License: GPLv2
* Text Domain: odin-toolkit
* Domain Path: languages/
*/
// Prevents direct access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if( ! class_exists( 'Odin_Toolkit' ) ) {
class Odin_Toolkit {
/**
* Current version number
*
* @var string
* @since 1.0.0
*/
const VERSION = '1.0.0';
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
/**
* Plugin directory path
*
* @var string
*/
private $plugin_dir = null;
/**
* Initialize the plugin.
*/
function __construct() {
$this->plugin_dir = plugin_dir_path( __FILE__ );
add_action( 'init', array( $this, 'load_textdomain' ) );
add_action( 'init', array( $this, 'includes' ), 0 );
}
/**
* Return the plugin instance.
*/
public static function init()
{
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* A final check if Odin Toolkit exists before kicking off our Odin Toolkit loading.
* Odin_Toolkit_VERSION is defined at this point.
*
* @since 1.0.0
*/
public function includes() {
if ( ! defined( 'Odin_Toolkit_VERSION' ) ) {
define( 'Odin_Toolkit_VERSION', self::VERSION );
}
// Now kick off the class autoloader.
spl_autoload_register( array( $this, 'autoload_classes' ) );
// Load the functions.php
require_once $this->plugin_dir . '/functions.php';
}
/**
* Autoloads files with Odin classes when needed
*
* @since 1.0.0
* @param string $class_name Name of the class being requested
*/
public function autoload_classes( $class_name ) {
$file = $this->get_file_name_from_class( $class_name );
$path = 'includes/classes';
if ( 'Odin_Front_End_Form' === $class_name ) {
$path .= '/abstracts';
}
if ( file_exists( $this->plugin_dir . "$path/$file" ) ) {
include $this->plugin_dir . "$path/$file";
}
}
/**
* Return file name in Odin pattern from class name. {Odin_Class_Name}
*
* @param string $class
* @return string
* @since 1.0.0
*/
private function get_file_name_from_class( $class ) {
$class = str_replace( 'Odin_', '', $class );
$class = str_replace( '_', '-', $class );
$class = strtolower( $class );
return 'class-' . $class . '.php';
}
/**
* Load plugin translation
*/
public function load_textdomain() {
load_plugin_textdomain( 'odin-toolkit', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
}
}
/**
* Initialize the plugin actions.
*/
add_action( 'plugins_loaded', array( 'Odin_Toolkit', 'init' ) );