plugin-loader.php
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Loader. |
| 4 | * |
| 5 | * @package bsf-utm-analytics |
| 6 | * @since 0.0.1 |
| 7 | */ |
| 8 | |
| 9 | namespace BSF_UTM_Analytics; |
| 10 | |
| 11 | /** |
| 12 | * Plugin_Loader |
| 13 | * |
| 14 | * @since 0.0.1 |
| 15 | */ |
| 16 | class Plugin_Loader { |
| 17 | |
| 18 | /** |
| 19 | * Instance |
| 20 | * |
| 21 | * @access private |
| 22 | * @var object Class Instance. |
| 23 | * @since 0.0.1 |
| 24 | */ |
| 25 | private static $instance; |
| 26 | |
| 27 | /** |
| 28 | * Initiator |
| 29 | * |
| 30 | * @since 0.0.1 |
| 31 | * @return object initialized object of class. |
| 32 | */ |
| 33 | public static function get_instance() { |
| 34 | if ( ! isset( self::$instance ) ) { |
| 35 | self::$instance = new self(); |
| 36 | } |
| 37 | return self::$instance; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Autoload classes. |
| 42 | * |
| 43 | * @param string $class class name. |
| 44 | */ |
| 45 | public function autoload( $class ) { |
| 46 | if ( 0 !== strpos( $class, __NAMESPACE__ ) ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | $class_to_load = $class; |
| 51 | |
| 52 | $filename = strtolower( |
| 53 | preg_replace( |
| 54 | [ '/^' . __NAMESPACE__ . '\\\/', '/([a-z])([A-Z])/', '/_/', '/\\\/' ], |
| 55 | [ '', '$1-$2', '-', DIRECTORY_SEPARATOR ], |
| 56 | $class_to_load |
| 57 | ) |
| 58 | ); |
| 59 | |
| 60 | $file = BSF_UTM_ANALYTICS_DIR . $filename . '.php'; |
| 61 | |
| 62 | // if the file readable, include it. |
| 63 | if ( is_readable( $file ) ) { |
| 64 | require_once $file; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Constructor |
| 70 | * |
| 71 | * @since 0.0.1 |
| 72 | */ |
| 73 | public function __construct() { |
| 74 | |
| 75 | spl_autoload_register( [ $this, 'autoload' ] ); |
| 76 | |
| 77 | add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Load Plugin Text Domain. |
| 82 | * This will load the translation textdomain depending on the file priorities. |
| 83 | * 1. Global Languages /wp-content/languages/bsf-utm-analytics/ folder |
| 84 | * 2. Local directory /wp-content/plugins/bsf-utm-analytics/languages/ folder |
| 85 | * |
| 86 | * @since 0.0.1 |
| 87 | * @return void |
| 88 | */ |
| 89 | public function load_textdomain() { |
| 90 | // Default languages directory. |
| 91 | $lang_dir = BSF_UTM_ANALYTICS_DIR . 'languages/'; |
| 92 | |
| 93 | /** |
| 94 | * Filters the languages directory path to use for plugin. |
| 95 | * |
| 96 | * @param string $lang_dir The languages directory path. |
| 97 | */ |
| 98 | $lang_dir = apply_filters( 'bsf_utm_analytics_languages_directory', $lang_dir ); |
| 99 | |
| 100 | // Traditional WordPress plugin locale filter. |
| 101 | global $wp_version; |
| 102 | |
| 103 | $get_locale = get_locale(); |
| 104 | |
| 105 | if ( $wp_version >= 4.7 ) { |
| 106 | $get_locale = get_user_locale(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Language Locale for plugin |
| 111 | * |
| 112 | * @var $get_locale The locale to use. |
| 113 | * Uses get_user_locale()` in WordPress 4.7 or greater, |
| 114 | * otherwise uses `get_locale()`. |
| 115 | */ |
| 116 | $locale = apply_filters( 'plugin_locale', $get_locale, 'bsf-utm-analytics' ); |
| 117 | $mofile = sprintf( '%1$s-%2$s.mo', 'bsf-utm-analytics', $locale ); |
| 118 | |
| 119 | // Setup paths to current locale file. |
| 120 | $mofile_global = WP_LANG_DIR . '/plugins/' . $mofile; |
| 121 | $mofile_local = $lang_dir . $mofile; |
| 122 | |
| 123 | if ( file_exists( $mofile_global ) ) { |
| 124 | // Look in global /wp-content/languages/bsf-utm-analytics/ folder. |
| 125 | load_textdomain( 'bsf-utm-analytics', $mofile_global ); |
| 126 | } elseif ( file_exists( $mofile_local ) ) { |
| 127 | // Look in local /wp-content/plugins/bsf-utm-analytics/languages/ folder. |
| 128 | load_textdomain( 'bsf-utm-analytics', $mofile_local ); |
| 129 | } else { |
| 130 | // Load the default language files. |
| 131 | load_plugin_textdomain( 'bsf-utm-analytics', false, $lang_dir ); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Kicking this off by calling 'get_instance()' method |
| 138 | */ |
| 139 | Plugin_Loader::get_instance(); |
| 140 |