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