PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.1.36
Gallery : FooGallery v3.1.36
3.3.2 3.3.3 3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / foopluginbase / classes / class-foo-plugin-base.php
foogallery / includes / foopluginbase / classes Last commit date
class-foo-friendly-dates.php 2 months ago class-foo-plugin-base.php 2 months ago class-foo-plugin-file-locator.php 2 months ago class-foo-plugin-metabox-sanity.php 2 months ago class-foo-plugin-options.php 2 months ago class-foo-plugin-settings.php 2 months ago class-foo-plugin-textdomain.php 2 months ago index.php 2 months ago
class-foo-plugin-base.php
330 lines
1 <?php
2 /**
3 * Foo_Plugin_Base
4 * A base class for WordPress plugins. Get up and running quickly with this opinionated, convention based, plugin framework
5 *
6 * A note about class versioning: to avoid running into issues when multiple plugins are using different versions of this base class,
7 * we append a version number to the class name. This avoids situations where multiple versions of the same class are loaded into memory and things no longer work as expected.
8 * This situation is extremely difficult to debug, and results in weird errors only when multiple plugins using the base class are activated on a single install
9 *
10 * Version: 2.4
11 * Author: Brad Vincent
12 * Author URI: https://fooplugins.com
13 * License: GPL2
14 */
15
16 if ( !class_exists( 'Foo_Plugin_Base_v2_4' ) ) {
17
18 abstract class Foo_Plugin_Base_v2_4 {
19
20 /**
21 * Unique identifier for your plugin.
22 *
23 * @var string
24 */
25 protected $plugin_slug = false; //the slug (identifier) of the plugin
26
27 /**
28 * The full name of your plugin.
29 *
30 * @var string
31 */
32 protected $plugin_title = false; //the friendly title of the plugin
33
34 /**
35 * Plugin version, used for cache-busting of style and script file references.
36 *
37 * @var string
38 */
39 protected $plugin_version = false; //the version number of the plugin
40
41 /* internal variables */
42 protected $plugin_file; //the filename of the plugin
43 protected $plugin_dir; //the folder path of the plugin
44 protected $plugin_dir_name; //the folder name of the plugin
45 protected $plugin_url; //the plugin url
46
47 /* internal class dependencies */
48
49 /** @var Foo_Plugin_Settings_v2_2 */
50 protected $_settings = false; //a ref to our settings helper class
51
52 /** @var Foo_Plugin_Options_v2_1 */
53 protected $_options = false; //a ref to our options helper class
54
55 /*
56 * @return Foo_Plugin_Settings_v2_2
57 */
58 public function settings() {
59 return $this->_settings;
60 }
61
62 /*
63 * @return Foo_Plugin_Options_v2_1
64 */
65 public function options() {
66 return $this->_options;
67 }
68
69 /*
70 * @return string
71 */
72 function get_slug() {
73 return $this->plugin_slug;
74 }
75
76 function get_plugin_info() {
77 return array(
78 'slug' => $this->plugin_slug,
79 'title' => $this->plugin_title,
80 'version' => $this->plugin_version,
81 'dir' => $this->plugin_dir,
82 'url' => $this->plugin_url,
83 );
84 }
85
86 /*
87 * Initializes the plugin.
88 */
89 function init( $file, $slug = false, $version = '0.0.1', $title = false ) {
90
91 //check to make sure the mandatory plugin fields have been set
92 if ( empty( $file ) ) {
93 throw new Exception( 'Required plugin variable not set : \'plugin_file\'. Please set this in the init() function of your plugin.' );
94 }
95 if ( empty( $version ) ) {
96 throw new Exception( 'Required plugin variable not set : \'plugin_version\'. Please set this in the init() function of your plugin.' );
97 }
98
99 $this->plugin_file = $file;
100 $this->plugin_dir = plugin_dir_path( $file );
101 $this->plugin_dir_name = plugin_basename( $this->plugin_dir );
102 $this->plugin_url = plugin_dir_url( $file );
103 $this->plugin_slug = $slug !== false ? $slug : plugin_basename( $file );
104 $this->plugin_title = $title !== false ? $title : foo_title_case( $this->plugin_slug );
105 $this->plugin_version = $version;
106
107 //instantiate our option class
108 $this->_options = new Foo_Plugin_Options_v2_1( $this->plugin_slug );
109
110 //check we are using php 5
111 foo_check_php_version( $this->plugin_title, '5.4.0' );
112
113 // Load plugin text domain
114 add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
115
116 // Render any inline styles that need to go at the end of the head tag
117 add_action( 'wp_head', array( $this, 'inline_styles' ), 100 );
118
119 // Render any inline scripts at the bottom of the page just before the closing body tag
120 add_action( 'wp_footer', array( $this, 'inline_scripts' ), 200 );
121
122 if ( is_admin() ) {
123 //instantiate our settings class
124 $this->_settings = new Foo_Plugin_Settings_v2_2( $this->plugin_slug );
125
126 //instantiate our metabox sanity class
127 new Foo_Plugin_Metabox_Sanity_v1( $this->plugin_slug );
128
129 // Add a settings page menu item
130 add_action( 'admin_menu', array( $this, 'admin_settings_page_menu' ) );
131
132 // Add a links to the plugin listing
133 add_filter( 'plugin_action_links_' . plugin_basename( $this->plugin_file ), array(
134 $this,
135 'admin_plugin_listing_actions',
136 ) );
137
138 // output CSS to the admin pages
139 add_action( 'admin_print_styles', array( $this, 'admin_print_styles' ) );
140
141 // output JS to the admin pages
142 add_action( 'admin_print_scripts', array( $this, 'admin_print_scripts' ) );
143 }
144
145 do_action( $this->plugin_slug . ( is_admin() ? '_admin' : '' ) . '_init' );
146 }
147
148 /**
149 * Loads the plugin language files for translation
150 *
151 * @param string $languages_directory The default language directory location. Default location is /languages/
152 */
153 public function load_plugin_textdomain( $languages_directory = '/languages/' ) {
154 Foo_Plugin_TextDomain_v1_0::load_textdomain( $this->plugin_file, $this->plugin_slug, $languages_directory );
155 }
156
157 //wrapper around the apply_filters function that appends the plugin slug to the tag
158 function apply_filters( $tag, $value ) {
159 if ( ! foo_starts_with( $tag, $this->plugin_slug ) ) {
160 $tag = $this->plugin_slug . '-' . $tag;
161 }
162
163 return apply_filters( $tag, $value );
164 }
165
166 // register and enqueue a script
167 function register_and_enqueue_js( $file, $d = array( 'jquery' ), $v = false, $f = false ) {
168 if ( $v === false ) {
169 $v = $this->plugin_version;
170 }
171
172 $js_src_url = $file;
173 if ( ! foo_contains( $file, '://' ) ) {
174
175 //check for the file in plugin root js directory
176 $js_src_url = $this->plugin_url . 'js/' . $file;
177 if ( ! file_exists( $this->plugin_dir . 'js/' . $file ) ) {
178
179 //check for the file in relative js directory
180 $js_src_url = plugin_dir_url( __FILE__ ) . 'js/' . $file;
181 if ( ! file_exists( plugin_dir_path( __FILE__ ) . 'js/' . $file ) ) {
182 return;
183 }
184
185 }
186 }
187 $h = str_replace( '.', '-', pathinfo( $file, PATHINFO_FILENAME ) );
188
189 wp_register_script( $handle = $h, $src = $js_src_url, $deps = $d, $ver = $v, $in_footer = $f );
190
191 wp_enqueue_script( $h );
192
193 return $h;
194 }
195
196 // register and enqueue a CSS
197 function register_and_enqueue_css( $file, $d = array(), $v = false ) {
198 if ( $v === false ) {
199 $v = $this->plugin_version;
200 }
201
202 $css_src_url = $file;
203 if ( ! foo_contains( $file, '://' ) ) {
204 $css_src_url = $this->plugin_url . 'css/' . $file;
205 if ( ! file_exists( $this->plugin_dir . 'css/' . $file ) ) {
206 return;
207 }
208 }
209
210 $h = str_replace( '.', '-', pathinfo( $file, PATHINFO_FILENAME ) );
211
212 wp_register_style( $handle = $h, $src = $css_src_url, $deps = $d, $ver = $v );
213
214 wp_enqueue_style( $h );
215
216 return $h;
217 }
218
219 // enqueue the admin scripts
220 function admin_print_scripts() {
221
222 //add a general admin script
223 $this->register_and_enqueue_js( 'admin.js' );
224
225 //if we are on the current plugin settings page then check for file named /js/admin-settings.js
226 if ( foo_check_plugin_settings_page( $this->plugin_slug ) ) {
227 $this->register_and_enqueue_js( 'admin-settings.js' );
228
229 //check if we are using an upload setting and add media uploader scripts
230 if ( $this->_settings->has_setting_of_type( 'image' ) ) {
231 //wp_enqueue_script( 'media-upload' );
232 //wp_enqueue_script( 'thickbox' );
233 //$this->register_and_enqueue_js( 'admin-uploader.js', array('jquery', 'media-upload', 'thickbox') );
234 }
235 }
236
237 //add any scripts for the current post type
238 $post_type = foo_current_screen_post_type();
239 if ( ! empty( $post_type ) ) {
240 $this->register_and_enqueue_js( 'admin-' . $post_type . '.js' );
241 }
242
243 //finally try add any scripts for the current screen id /css/admin-screen-id.css
244 $this->register_and_enqueue_js( 'admin-' . foo_current_screen_id() . '.js' );
245
246 do_action( $this->plugin_slug . '_admin_print_scripts' );
247 }
248
249 // register the admin stylesheets
250 function admin_print_styles() {
251
252 //add a general admin stylesheet
253 $this->register_and_enqueue_css( 'admin.css' );
254
255 //if we are on the current plugin's settings page then check for file /css/admin-settings.css
256 if ( foo_check_plugin_settings_page( $this->plugin_slug ) ) {
257 $this->register_and_enqueue_css( 'admin-settings.css' );
258
259 //Media Uploader Style
260 wp_enqueue_style( 'thickbox' );
261 }
262
263 //add any scripts for the current post type /css/admin-foobar.css
264 $post_type = foo_current_screen_post_type();
265 if ( ! empty( $post_type ) ) {
266 $this->register_and_enqueue_css( 'admin-' . $post_type . '.css' );
267 }
268
269 //finally try add any styles for the current screen id /css/admin-screen-id.css
270 $this->register_and_enqueue_css( 'admin-' . foo_current_screen_id() . '.css' );
271
272 do_action( $this->plugin_slug . '_admin_print_styles' );
273 }
274
275 function admin_plugin_listing_actions( $links ) {
276 if ( $this->has_admin_settings_page() ) {
277 // Add the 'Settings' link to the plugin page
278 $links[] = '<a href="options-general.php?page=' . $this->plugin_slug . '"><b>' . __( 'Settings', $this->plugin_slug ) . '</b></a>';
279 }
280
281 return apply_filters( $this->plugin_slug . '_admin_plugin_action_links', $links );
282 }
283
284 function has_admin_settings_page() {
285 return apply_filters( $this->plugin_slug . '_admin_has_settings_page', true );
286 }
287
288 // add a settings admin menu
289 function admin_settings_page_menu() {
290 if ( $this->has_admin_settings_page() ) {
291
292 register_setting( $this->plugin_slug, $this->plugin_slug );
293
294 $page_title = $this->apply_filters( $this->plugin_slug . '_admin_settings_page_title', $this->plugin_title . __( ' Settings', $this->plugin_slug ) );
295 $menu_title = $this->apply_filters( $this->plugin_slug . '_admin_settings_menu_title', $this->plugin_title );
296
297 add_options_page( $page_title, $menu_title, 'manage_options', $this->plugin_slug, array(
298 $this,
299 'admin_settings_render_page',
300 ) );
301 }
302 }
303
304 // render the setting page
305 function admin_settings_render_page() {
306 $settings = apply_filters( $this->plugin_slug . '_admin_settings', array() );
307
308 $this->_settings->add_settings( $settings );
309
310 $current_directory = trailingslashit( dirname( plugin_dir_path( __FILE__ ) ) );
311
312 //check if a settings.php file exists in the views folder. If so then include it
313 if ( file_exists( $current_directory . 'views/settings.php' ) ) {
314
315 //global variable that can be used by the included settings pages
316 include_once( $current_directory . 'views/settings.php' );
317 }
318
319 do_action( $this->plugin_slug . '_admin_settings_render_page', $this->_settings );
320 }
321
322 function inline_styles() {
323 do_action( $this->plugin_slug . ( is_admin() ? '_admin' : '' ) . '_inline_styles', $this );
324 }
325
326 function inline_scripts() {
327 do_action( $this->plugin_slug . ( is_admin() ? '_admin' : '' ) . '_inline_scripts', $this );
328 }
329 }
330 }