forked from theantichris/WordPress-Plugin-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordpress-plugin-framework.php
263 lines (230 loc) · 6.48 KB
/
wordpress-plugin-framework.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/*
Plugin Name: WordPress Plugin Framework
Plugin URI: https://github.com/theantichris/WordPress-Plugin-Framework
Description: My own framework for making WordPress plugins the way I do.
Version: 5.0.1
Author: Christopher Lamm
Author URI: http://www.theantichris.com
License: GPL V2
*/
/*
* TODO: Replace plugin information header above.
* TODO: Replace "WordPress Plugin Framework" with "Plugin Name".
* TODO: Replace "WordPress_Plugin_Framework" with "Plugin_Name".
* TODO: Replace "wordpress-plugin-framework" with "plugin-name".
* TODO: Replace "WordPressPluginFramework" with "PluginName".
*/
/**
* Class WordPress_Plugin_Framework
*
* @package WordPress
* @subpackage WordPressPluginFramework
*
* @since 1.0.0
*/
class WordPress_Plugin_Framework {
/** @var null|WordPress_Plugin_Framework Refers to a single instance of this class. */
private static $instance = null;
/** @var string The path to the plugin file. */
private static $plugin_path;
/** @var string The URL to the plugin file. */
private static $plugin_url;
/** @var Custom_Post_Type[] Custom post type objects used by the plugin. */
private $custom_post_types = array();
/** @var Taxonomy[] Taxonomies objects used by the plugin. */
private $taxonomies = array();
/** @var Page[] Page objects used by the plugin. */
private $pages = array();
/** @var Settings[] Settings objects used by the plugin. */
private $settings = array();
/**
* Creates or returns an instance of this class.
*
* @since 1.0.0
*
* @return WordPress_Plugin_Framework A single instance of this class.
*/
public static function get_instance() {
// If an instance hasn't been create and set to $instance create an instance and set it to $instance.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Initializes the plugin by setting localization, hooks, filters, and administrative functions.
*
* @since 1.0.0
*
* @return WordPress_Plugin_Framework
*/
private function __construct() {
/* Includes */
include_once 'inc/custom-post-type.php';
include_once 'inc/page.php';
include_once 'inc/menu-page.php';
include_once 'inc/object-page.php';
include_once 'inc/options-page.php';
include_once 'inc/sub-menu-page.php';
include_once 'inc/utility-page.php';
include_once 'inc/settings.php';
include_once 'inc/taxonomy.php';
include_once 'inc/view.php';
/* Set properties. */
self::$plugin_path = plugin_dir_path( __FILE__ );
self::$plugin_url = plugin_dir_url( __FILE__ );
/* Load text domain. */
load_plugin_textdomain( 'wordpress-plugin-framework', false, self::$plugin_path . '/lang' );
/* Load scripts and styles in the Dashboard. */
add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_styles' ) );
/* Load scripts and styles on the front end. */
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'register_styles' ) );
/* Register activation and deactivation hooks. */
register_activation_hook( __FILE__, array( $this, 'activation' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivation' ) );
/* Run plugin. */
$this->run_plugin();
}
/**
* Returns $plugin_url.
*
* @since 5.0.1
*
* @return string
*/
public static function get_plugin_url() {
return self::$plugin_url;
}
/**
* Returns $plugin_path.
*
* @since 5.0.1
*
* @return string
*/
public static function get_plugin_path() {
return self::$plugin_path;
}
/**
* This method runs at plugin activation.
*
* @since 1.0.0
*
* @return void
*/
public function activation() {
}
/**
* This method runs at plugin deactivation.
*
* @since 1.0.0
*
* @return void
*/
public function deactivation() {
}
/**
* Place any scripts that need to be registered with WordPress in this method.
*
* @since 1.0.0
*
* @return void
*/
public function register_scripts() {
}
/**
* Place any styles that need to be registered with WordPress in this method.
*
* @since 1.0.0
*
* @return void
*/
public function register_styles() {
}
/**
* Method containing the plugin functionality. Place your code here.
*
* @since 4.1.0
*
* @return void
*/
private function run_plugin() {
}
/**
* If WordPress debugging is turned on this method will write data to debug.log located in the wp-content directory.
*
* Add the following lines to wp-config.php:
*
* define( 'WP_DEBUG', true ); // Turn debugging ON
* define( 'SAVEQUERIES', true );
* define( 'WP_DEBUG_DISPLAY', false ); // Turn forced display OFF
* define( 'WP_DEBUG_LOG', true ); // Turn logging to wp-content/debug.log ON
*
* @since 1.0.0
*
* @param mixed $message Message to pass to the error log.
*
* @return void
*/
public static function print_to_log( $message ) {
if ( true === WP_DEBUG ) {
if ( is_array( $message ) || is_object( $message ) ) {
error_log( print_r( $message ), true );
} else {
error_log( $message );
}
}
}
/**
* Takes a plural string and returns the singular version.
*
* Solution found at https://sites.google.com/site/chrelad/notes-1/pluraltosingularwithphp.
*
* @since 2.0.0
*
* @param string $word Plural string to make singular.
*
* @return string
*/
public static function make_singular( $word ) {
$rules = array(
'ss' => false,
'os' => 'o',
'ies' => 'y',
'xes' => 'x',
'oes' => 'o',
'ves' => 'f',
's' => ''
);
// Loop through all the rules and do the replacement.
foreach ( array_keys( $rules ) as $key ) {
// If the end of the word doesn't match the key, it's not a candidate for replacement. Move on to the next plural ending.
if ( substr( $word, ( strlen( $key ) * -1 ) ) != $key ) {
continue;
}
// If the value of the key is false, stop looping and return the original version of the word.
if ( $key === false ) {
return $word;
}
// We've made it this far, so we can do the replacement.
return substr( $word, 0, strlen( $word ) - strlen( $key ) ) . $rules[ $key ];
}
return $word;
}
/**
* Makes WordPress slug by making the string lowercase and replacing spaces with hyphens.
*
* @since 3.1.0
*
* @param $string String to make a slug out of.
*
* @return string
*/
public static function make_slug( $string ) {
return str_replace( ' ', '-', strtolower( $string ) );
}
}
WordPress_Plugin_Framework::get_instance();