1 | <?php |
||
44 | final class Auto_Load_Next_Post { |
||
|
|||
45 | |||
46 | /** |
||
47 | * The single instance of the class |
||
48 | * |
||
49 | * @since 1.0.0 |
||
50 | * @access private |
||
51 | * @var object |
||
52 | */ |
||
53 | private static $_instance = null; |
||
54 | |||
55 | /** |
||
56 | * Main Auto Load Next Post Instance |
||
57 | * |
||
58 | * Ensures only one instance of Auto Load Next Post is loaded or can be loaded. |
||
59 | * |
||
60 | * @since 1.0.0 |
||
61 | * @access public static |
||
62 | * @see Auto_Load_Next_Post() |
||
63 | * @return Auto Load Next Post instance |
||
64 | */ |
||
65 | public static function instance() { |
||
66 | if (is_null(self::$_instance)) { |
||
67 | self::$_instance = new Auto_Load_Next_Post; |
||
68 | self::$_instance->setup_constants(); |
||
69 | self::$_instance->load_plugin_textdomain(); |
||
70 | self::$_instance->includes(); |
||
71 | } |
||
72 | return self::$_instance; |
||
73 | } // END instance() |
||
74 | |||
75 | /** |
||
76 | * Throw error on object clone |
||
77 | * |
||
78 | * The whole idea of the singleton design pattern is that there is a single |
||
79 | * object therefore, we don't want the object to be cloned. |
||
80 | * |
||
81 | * @since 1.0.0 |
||
82 | * @access public |
||
83 | * @return void |
||
84 | */ |
||
85 | public function __clone() { |
||
89 | |||
90 | /** |
||
91 | * Disable unserializing of the class |
||
92 | * |
||
93 | * @since 1.0.0 |
||
94 | * @access public |
||
95 | * @return void |
||
96 | */ |
||
97 | public function __wakeup() { |
||
101 | |||
102 | /** |
||
103 | * Constructor |
||
104 | * |
||
105 | * @since 1.0.0 |
||
106 | * @access public |
||
107 | */ |
||
108 | public function __construct() { |
||
109 | // Auto-load classes on demand |
||
110 | if (function_exists("__autoload")) { |
||
111 | spl_autoload_register("__autoload"); |
||
112 | } |
||
113 | |||
114 | spl_autoload_register(array($this, 'autoload')); |
||
115 | |||
116 | // Hooks |
||
117 | add_action('init', array($this, 'init_auto_load_next_post'), 0); |
||
118 | add_action('wp_enqueue_scripts', array($this, 'front_scripts_and_styles')); |
||
119 | } // END __construct() |
||
120 | |||
121 | /** |
||
122 | * Auto-load Auto Load Next Post classes on demand to reduce memory consumption. |
||
123 | * |
||
124 | * @since 1.0.0 |
||
125 | * @access public |
||
126 | * @param mixed $class |
||
127 | * @return void |
||
128 | */ |
||
129 | public function autoload($class) { |
||
130 | $path = null; |
||
131 | $file = strtolower('class-'.str_replace('_', '-', $class)).'.php'; |
||
132 | |||
133 | if (strpos($class, 'auto_load_next_post_admin') === 0) { |
||
134 | $path = AUTO_LOAD_NEXT_POST_FILE_PATH.'/includes/admin/'; |
||
135 | } else if (strpos($class, 'auto_load_next_post_') === 0) { |
||
136 | $path = AUTO_LOAD_NEXT_POST_FILE_PATH.'/includes/'; |
||
137 | } |
||
138 | |||
139 | if ($path !== null && is_readable($path.$file)) { |
||
140 | include_once($path.$file); |
||
141 | return true; |
||
142 | } |
||
143 | } // END autoload() |
||
144 | |||
145 | /** |
||
146 | * Setup Constants |
||
147 | * |
||
148 | * @since 1.4.3 |
||
149 | * @access private |
||
150 | */ |
||
151 | private function setup_constants() { |
||
152 | $this->define('AUTO_LOAD_NEXT_POST_VERSION', '1.4.5'); |
||
153 | $this->define('AUTO_LOAD_NEXT_POST_FILE', __FILE__); |
||
154 | $this->define('AUTO_LOAD_NEXT_POST_SLUG', 'auto-load-next-post'); |
||
155 | |||
156 | $this->define('AUTO_LOAD_NEXT_POST_URL_PATH', untrailingslashit(plugins_url('/', __FILE__))); |
||
157 | $this->define('AUTO_LOAD_NEXT_POST_FILE_PATH', untrailingslashit(plugin_dir_path(__FILE__))); |
||
158 | $this->define('AUTO_LOAD_NEXT_POST_TEMPLATE_PATH', 'auto-load-next-post/'); |
||
159 | |||
160 | $this->define('AUTO_LOAD_NEXT_POST_WP_VERSION_REQUIRE', '4.0'); |
||
161 | |||
162 | $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
||
163 | $this->define('AUTO_LOAD_NEXT_POST_SCRIPT_MODE', $suffix); |
||
164 | } // END setup_constants() |
||
165 | |||
166 | /** |
||
167 | * Define constant if not already set. |
||
168 | * |
||
169 | * @param string $name |
||
170 | * @param string|bool $value |
||
171 | * @access private |
||
172 | * @since 1.4.3 |
||
173 | */ |
||
174 | private function define($name, $value) { |
||
175 | if ( ! defined($name)) { |
||
176 | define($name, $value); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Include required core files used in admin and on the frontend. |
||
182 | * |
||
183 | * @since 1.0.0 |
||
184 | * @access public |
||
185 | * @return void |
||
186 | */ |
||
187 | public function includes() { |
||
188 | include_once('includes/auto-load-next-post-core-functions.php'); // Contains core functions for the front/back end. |
||
189 | |||
190 | if (is_admin()) { |
||
191 | include_once('includes/admin/class-auto-load-next-post-admin.php'); // Admin section |
||
192 | } |
||
193 | } // END includes() |
||
194 | |||
195 | /** |
||
196 | * Runs when the plugin is initialized. |
||
197 | * |
||
198 | * @since 1.0.0 |
||
199 | * @access public |
||
200 | */ |
||
201 | public function init_auto_load_next_post() { |
||
202 | add_rewrite_endpoint('partial', EP_PERMALINK); |
||
203 | |||
204 | // Refresh permalinks |
||
205 | flush_rewrite_rules(); |
||
206 | } // END init_auto_load_next_post() |
||
207 | |||
208 | /** |
||
209 | * Load Localisation files. |
||
210 | * |
||
211 | * Note: the first-loaded translation file overrides any |
||
212 | * following ones if the same translation is present. |
||
213 | * |
||
214 | * @since 1.0.0 |
||
215 | * @access public |
||
216 | * @filter auto_load_next_post_languages_directory |
||
217 | * @filter plugin_locale |
||
218 | * @return void |
||
219 | */ |
||
220 | public function load_plugin_textdomain() { |
||
221 | // Set filter for plugin's languages directory |
||
222 | $lang_dir = dirname(plugin_basename(AUTO_LOAD_NEXT_POST_FILE)).'/languages/'; |
||
223 | $lang_dir = apply_filters('auto_load_next_post_languages_directory', $lang_dir); |
||
224 | |||
225 | // Traditional WordPress plugin locale filter |
||
226 | $locale = apply_filters('plugin_locale', get_locale(), 'auto-load-next-post'); |
||
227 | $mofile = sprintf('%1$s-%2$s.mo', 'auto-load-next-post', $locale); |
||
228 | |||
229 | // Setup paths to current locale file |
||
230 | $mofile_local = $lang_dir.$mofile; |
||
231 | $mofile_global = WP_LANG_DIR.'/auto-load-next-post/'.$mofile; |
||
232 | |||
233 | if (file_exists($mofile_global)) { |
||
234 | // Look in global /wp-content/languages/auto-load-next-post/ folder |
||
235 | load_textdomain('auto-load-next-post', $mofile_global); |
||
236 | } else if (file_exists($mofile_local)) { |
||
237 | // Look in local /wp-content/plugins/auto-load-next-post/languages/ folder |
||
238 | load_textdomain('auto-load-next-post', $mofile_local); |
||
239 | } else { |
||
240 | // Load the default language files |
||
241 | load_plugin_textdomain('auto-load-next-post', false, $lang_dir); |
||
242 | } |
||
243 | } // END load_plugin_textdomain() |
||
244 | |||
245 | /** |
||
246 | * Registers and enqueues stylesheets and javascripts for the front of the site. |
||
247 | * |
||
248 | * @since 1.3.2 |
||
249 | * @access public |
||
250 | */ |
||
251 | public function front_scripts_and_styles() { |
||
271 | |||
272 | /** |
||
273 | * Helper function for registering and enqueueing scripts and styles. |
||
274 | * |
||
275 | * @since 1.0.0 |
||
276 | * @access public static |
||
277 | * @param string $name The ID to register with WordPress. |
||
278 | * @param string $file_path The path to the actual file. |
||
279 | * @param bool $is_script Optional, argument for if the incoming file_path is a JavaScript source file. |
||
280 | * @param array $support Optional, for requiring other javascripts for the source file you are calling. |
||
281 | * @param string $version Optional, can match the version of the plugin or version of the source file. |
||
282 | * @global string $wp_version |
||
283 | */ |
||
284 | public static function load_file($name, $file_path, $is_script = false, $support = array(), $version = '') { |
||
300 | |||
301 | } // END Auto_Load_Next_Post() |
||
302 | |||
331 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.