bourbonjournal /wp-content/plugins/capsman/framework/plugins.php

Language PHP Lines 638
MD5 Hash 8842d34b9cac25fad3d425b07c398188
Repository https://github.com/arconis25/bourbonjournal.git View Raw File View Project SPDX
  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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
<?php
/**
 * Plugins related functions and classes.
 *
 * @version		$Rev: 63 $
 * @author		Jordi Canals
 * @package		Alkivia
 * @subpackage	Framework
 * @link 		http://alkivia.org
 * @license 	http://www.gnu.org/licenses/gpl.html GNU General Public License v3

	Copyright 2009 Jordi Canals <alkivia@jcanals.net>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

if ( ! class_exists('cmanPlugin') ) :
	/**
	 * Abtract class to be used as a plugin template.
	 * Must be implemented before using this class and it's recommended to prefix the class to prevent collissions.
	 * There are some special functions thay can declared (as protected) in implementations to perform main actions:
	 * 		- activate: (Protected) Actions to run when activating the plugin.
	 * 		- _deactivate: (Hook, must be public) Actions to run when deactivating the plugin.
	 * 		- update: (Protected) Actions to update the plugin to a new version. (Updating version on DB is done after this).
	 * 						Takes plugin running version as a parameter.
	 * 		- setDefaults: (Protected) Fills the $defaults class var with the default settings.
	 * 		- init: (Protected) Actions to run when plugins initialization is performed (In plugins loaded).
	 * 		- widgetsInit: (Protected) Actions to init plugin widgets (In widgets_init).
	 * 		- startUp: (Protected) Actions to run at system startup (before plugins are loaded).
	 * 		- _adminMenus: (Hook, must be public) Set the menus in WordPress Dashboard.
	 *
	 * @uses		plugins.php
	 * @author		Jordi Canals
	 * @package		Alkivia
	 * @subpackage	Framework
	 * @link		http://alkivia.org
	 */
	abstract class cmanPlugin
	{
		/**
		 * Plugin ID. Is the plugin short name.
		 * Filled in constructor (as a constructor param).
		 * @var string
		 */
		protected $ID;

		/**
		 * Plugin main file.
		 * Filled in constructor (as a constructor param).
		 * Cointains the full path to the main plugin's file. The one cointaining plugin's data header.
		 * @var string
		 */
		protected $p_file;

		/**
		 * Plugin data. Readed from the main plugin file header and the readme file.
		 * Filled in loadPluginData(). Called in constructor.
		 * From the filename:
		 * 		- 'ID' - Plugin internal short name. Taken from main plugin's file name.
		 * Fom the file header:
		 *		- 'Name' - Name of the plugin, must be unique.
		 *		- 'Title' - Title of the plugin and the link to the plugin's web site.
		 *		- 'Description' - Description of what the plugin does and/or notes from the author.
		 *		- 'Author' - The author's name
		 *		- 'AuthorURI' - The author's web site address.
		 *		- 'Version' - The plugin's version number.
		 *		- 'PluginURI' - Plugin's web site address.
		 *		- 'TextDomain' - Plugin's text domain for localization.
		 *		- 'DomainPath' - Plugin's relative directory path to .mo files.
		 * From readme.txt file :
		 * 		- 'Contributors' - An array with all contributors nicknames.
		 * 		- 'Tags' - An array with all plugin tags.
		 * 		- 'DonateURI' - The donations page address.
		 *      - 'Requires' - Minimum required WordPress version.
		 *      - 'Tested' - Higher WordPress version this plugin has been tested.
		 *      - 'Stable' - Last stable tag when this was released.
		 * @var array
		 */
		protected $p_data;

		/**
		 * Plugin paths: folder name, absolute path to plugin's folder and folder url.
		 * Filled in loadPaths(). Called in constructor.
		 *		- 'subdir' - The base plugin subdirectory.
		 *		- 'path' - The full path to plugin's folder.
		 *		- 'url' - The full URL to plugin's folder.
		 * @var array
		 */
		protected $p_dirs;

		/**
		 * Plugin saved data.
		 *		- 'post' - Saves the current post.
		 *		- 'more' - Saves the read more status.
		 * @var array
		 */
		protected $saved;

		/**
		 * Plugin settings (from DB)
		 * @var array
		 */
		protected $settings;

		/**
		 * Plugin default settings.
		 * This settings can difer from install defaults and are used to fill settings loaded from DB
		 * @var array
		 */
		protected $defaults = array();

		/**
		 * Flag to see if we are installing (activating for first time) or reactivating the plugin.
		 * @var boolean
		 */
		protected $installing = false;

		/**
		 * Flag to see if plugin needs to be updated.
		 * @var boolean
		 */
		protected $needs_update = false;

		/**
		 * Class constructor.
		 * Calls the implementated method 'startUp' if it exists. This is done at plugins loading time.
		 * Prepares admin menus by seting an action for the implemented method '_adminMenus' if it exists.
		 *
		 * @param string $plugin_file	Full main plugin's filename (absolute to root).
		 * @param string $ID  Plugin short name (known as plugin ID).
		 * @return cmanPlugin|false	The plugin object or false if not compatible.
		 */
		final function __construct( $plugin_file, $ID = '' ) {

			$this->p_file = trim($plugin_file);
			$this->ID = ( empty($ID) ) ? strtolower(basename($this->p_file, '.php')) : trim($ID) ;

			// Load component data and settings.
			if ( method_exists($this, 'setDefaults') ) {
				$this->setDefaults();
			}
			$this->loadPluginData();
			$this->loadPaths();

			if ( $this->isCompatible() ) {
				// Activation and deactivation hooks.
				register_activation_hook($this->p_file, array($this, '_activatePlugin'));
				if ( method_exists($this, '_deactivate') ) {
					register_deactivation_hook($this->p_file, array($this, '_deactivate'));
				}

				// Load style files.
				if ( is_admin() ) {
					add_action('admin_print_styles', array($this, '_enqueueStyles'));	// For Compatibility with WP 2.8
				} else {
					add_action('wp_print_styles', array($this, '_enqueueStyles'));
				}

				// Init plugins at plugins and widgets
				add_action('plugins_loaded', array($this, '_initPlugin'));
				add_action('widgets_init', array($this, '_initWidgets'));

				// Add administration menus.
				if ( method_exists($this, '_adminMenus') ) {
					add_action('admin_menu', array($this, '_adminMenus'));		// Add Panel menus.
				}

				// Startup the plugin.
				if ( method_exists($this, 'startUp') ) {
					$this->startUp();
				}
			}
		}

		/**
		 * Activates the plugin. Only runs on first activation.
		 * Saves the plugin version in DB, and calls the 'activate' method.
		 *
		 * @hook register_activation_hook
		 * @access private
		 * @return void
		 */
		final function _activatePlugin() {

			if ( method_exists($this, 'setDefaults') ) {
				$this->setDefaults();
			}

			// If there is an additional function to perform on activate.
			if ( method_exists($this, 'activate') ) {
				$this->activate();
			}

			$this->settings = $this->defaults;
			add_option($this->ID . '_settings', $this->settings);
			add_option($this->ID . '_version', $this->p_data['Version']);
		}

		/**
		 * Init the plugin (In action 'plugins_loaded')
		 * Here whe call the 'update' and 'init' functions. This is done after the plugins are loaded.
		 * Also the plugin version and settings are updated here.
		 *
		 * @hook action plugins_loaded
		 * @access private
		 * @return void
		 */
		final function _initPlugin() {
			$this->loadTranslations();

			// First, check if the plugin needs to be updated.
			if ( $this->needs_update ) {
				if ( method_exists($this, 'update') ) {
					$version = get_option($this->ID . '_version');
					$this->update($version);
				}
				update_option($this->ID . '_version', $this->p_data['Version']);
				update_option($this->ID . '_settings', $this->settings);
			}

			// Call the custom init for the plugin.
			if ( method_exists($this, 'init') ) {
				$this->init();
			}
		}

		/**
		 * Inits the widgets (In action 'widgets_init')
		 * Before loading the widgets, we check that standard sidebar is present.
		 *
		 * @hook action 'widgets_init'
		 * @return void
		 */
		final function _initWidgets() {
			if ( method_exists($this, 'widgetsInit') && $this->isStandardSidebar() ) {
				$this->widgetsInit();
			}
		}

		/**
		 * Loads translations file, located on the plugin's lang subdir.
		 *
		 * @return void
		 */
		final protected function loadTranslations() {
			load_plugin_textdomain($this->ID, false, $this->p_dirs['subdir'] . '/lang');
		}

		/**
		 * Prepares and enqueues plugin styles.
		 * Filters used:
		 * 		- 'pluginID_style_admin' - For the admin style URL.
		 * 		- 'pluginID_style_url' - For the public style URL.
		 *
		 * @uses apply_filters() Calls the 'ID_style_url' and 'ID_style_admin' on the style file URL.
		 * @hook action wp_print_styles and admin_print_styles
		 * @access private
		 * @return void
		 */
		final function _enqueueStyles() {

			$url = '';
			if ( is_admin() ) {
				if ( file_exists($this->p_dirs['path'] . 'admin.css') ) {
					$url = $this->p_dirs['url'] . 'admin.css';
				}
				$url = apply_filters($this->ID . '_style_admin', $url);
			} else {
				if ( file_exists($this->p_dirs['path'] . 'style.css') ) {
					$url = $this->p_dirs['url'] . 'style.css';
				}
				$url = apply_filters($this->ID . '_style_url', $url);
			}

			if ( ! empty($url) ) {
    			wp_register_style($this->ID, $url, false, $this->p_data['Version']);
    			wp_enqueue_style($this->ID);
	    	}
    	}

		/**
		 * Returns the plguin Folder basename.
		 *
		 * @return string
		 */
		final public function getFolder() {
			if ( empty($p_dirs) ) {
				$this->loadPaths();
			}
			return $this->p_dirs['subdir'];
		}

		/**
		 * Returns the URL to the plugin folder (with trailing slash).
		 *
		 * @return string
		 */
		final public function getURL() {
			if ( empty($p_dirs) ) {
				$this->loadPaths();
			}
			return $this->p_dirs['url'];
		}

		/**
		 * Returns the Absolute path to plugin folder (with trailing slash).
		 *
		 * @return string
		 */
		final public function getPath() {
			if ( empty($p_dirs) ) {
				$this->loadPaths();
			}
			return $this->p_dirs['path'];
		}

    	/**
    	 * Returns private or protected values.
    	 * @since 0.6
    	 *
    	 * @param $name	Name of the value.
    	 * @return mixed Requested value.
    	 */
    	public function __get( $name ) {

    		if ( empty($this->p_data) ) {
				$this->loadPluginData();
    		}

    		$name = strtolower($name);
    		switch ( $name ) {
    			case 'id':
    				return $this->ID;
    				break;
    			case 'file':
    				return $this->p_file;
    				break;
    			case 'version':
    				return $this->p_data['Version'];
    				break;
    			default:
   					return false;
    		}

    	}

    	/**
    	 * Returns a plugin setting.
    	 * If no specific settings is requested, returns all settings.
    	 * If requested a non existent settings, returns $default.
    	 *
    	 * @param $name	Name for the settings to return.
    	 * @param $default Default value to use if setting does not exists.
    	 * @return mixed	The settings value or an array with all settings.
    	 */
    	public function getOption( $name = '', $default = false ) {
    		if ( empty($name) ) {
    			return $this->settings;
    		} elseif ( isset($this->settings[$name]) ) {
    			return $this->settings[$name];
    		} else {
    			return $default;
    		}

    	}

		/**
		 * Returns plugin data.
		 * This data is loaded from the main plugin's file.
		 *
		 * @see $p_data
		 * @return mixed The parameter requested or an array wil all data.
		 */
		final public function getPluginData( $name = '' ) {
			if ( empty($name) ) {
				return $this->p_data;
			} elseif ( isset( $rhis->p_data[$name]) ) {
				return $this->p_data['name'];
			} else {
				return false;
			}
		}

		/**
		 * Loads plugin data and settings.
		 * Data is loaded from plugin and readme file headers. Settings from Database.
		 *
		 * @return void
		 */
		final private function loadPluginData() {
			if ( empty($this->p_data) ) {
				if ( ! function_exists('get_plugin_data') ) {
					require_once ( ABSPATH . 'wp-admin/includes/plugin.php' );
				}

				$p_data = get_plugin_data($this->p_file);
				$r_data = plugin_readme_data($this->p_file);
				$this->p_data = array_merge($r_data, $p_data);
			}

			$this->settings = get_option($this->ID . '_settings');
			if ( ! empty($this->defaults) && is_array($this->defaults) ) {
				if ( is_array($this->settings) ) {
					$this->settings = array_merge($this->defaults, $this->settings);
				} else {
					$this->settings = $this->defaults;
				}
			}

			$ver = get_option($this->ID . '_version');
			if ( false === $ver ) {
				$this->installing = true;
			} elseif ( version_compare($ver, $this->p_data['Version'], 'ne') ) {
				$this->needs_update = true;
			}
		}

		/**
		 * Saves the current post state.
		 *
		 * @return void
		 */
		final protected function savePost() {
			global $post, $more;

			$this->saved['post'] = $post;
			$this->saved['more'] = $more;
		}

		/**
		 * Restores the current post state.
		 * Saved in savePost()
		 *
		 * @return void
		 */
		final protected function restorePost() {
			global $post, $more;

			$more = $this->saved['more'];
			$post = $this->saved['post'];
			setup_postdata($post);
		}

		/**
		 * Checks if the plugin is compatible with the current WordPress version.
		 * If it's not compatible, sets an admin warning.
		 *
		 * @return boolean	Plugin is compatible with this WordPress version or not.
		 */
		final private function isCompatible() {
			global $wp_version;

			if ( version_compare($wp_version, $this->p_data['Requires'] , '>=') ) {
				return true;
			} else {
				add_action('admin_notices', array($this, '_compatibleWarning'));
				return false;
			}
		}

		/**
		 * Shows a warning message when the plugin is not compatible with current WordPress version.
		 * This is used by calling the action 'admin_notices' in isCompatible()
		 *
		 * @hook action admin_notices
		 * @access private
		 * @return void
		 */
		final function _compatibleWarning() {
			$this->loadTranslations(); // We have not loaded translations yet.

			echo '<div class="error"><p><strong>' . __('Warning:', $this->ID) . '</strong> '
				. sprintf(__('The active plugin %s is not compatible with your WordPress version.', $this->ID),
					'&laquo;' . $this->p_data['Name'] . ' ' . $this->p_data['Version'] . '&raquo;')
				. '</p><p>' . sprintf(__('WordPress %s is required to run this plugin.', $this->ID), $this->p_data['Requires'])
				. '</p></div>';
		}

		/**
		 * Checks if standard functions for Widgets are present.
		 * If them are not present, we are not using the standard sidebar: an admin warning is set.
		 *
		 * MAYBE: Move to a new Widget Class ?
		 *
		 * @return boolean	Standard widget functions were found ot not.
		 */
		final private function isStandardSidebar() {

			if (	function_exists('wp_register_sidebar_widget') &&
					function_exists('wp_register_widget_control') &&
					function_exists('wp_get_sidebars_widgets') &&
					function_exists('wp_set_sidebars_widgets') )
			{
				return true;
			} else {
				add_action('admin_notices', array($this, '_standardSidebarWarning'));
				return false;
			}
		}

		/**
		 * Shows an admin warning when not using the WordPress standard sidebar.
		 * This is done by calling the action 'admin_notices' in isStandardSidebar()
		 *
		 * MAYBE: Move to a new Widget Class ?
		 *
		 * @hook action admin_notices
		 * @access private
		 * @return void
		 */
		final function _standardSidebarWarning() {
			$this->loadTranslations(); // We have not loaded translations yet.

			echo '<div class="error"><p><strong>' . __('Warning:', $this->ID) . '</strong> '
				. __('Standard sidebar functions are not present.', $this->ID) . '</p><p>'
				. sprintf(__('It is required to use the standard sidebar to run %s', $this->ID),
					'&laquo;' . $this->p_data['Name'] . ' ' . $this->p_data['Version'] . '&raquo;')
				. '</p></div>';
		}

		/**
		 * Loads the plugin paths based on the plugin main file.
		 * Paths are set as $this->p_dirs.
		 *
		 * @see $p_dirs
		 * @return void
		 */
		final private function loadPaths() {

			$this->p_dirs['path']	= dirname($this->p_file) .'/';
			$this->p_dirs['subdir']	= basename($this->p_dirs['path']);
			$this->p_dirs['url']	= WP_PLUGIN_URL . '/' . $this->p_dirs['subdir'] .'/';
		}
	}
endif;

// ======================================================= FUNCTIONS ==========

if ( ! function_exists('plugin_readme_data') ) :
	/**
	 * Parse the plugin readme.txt file to retrieve plugin's metadata.
	 *
	 * The metadata of the plugin's readme searches for the following in the readme.txt
	 * header. All metadata must be on its own line. The below is formatted for printing.
	 *
	 * <code>
	 * Contributors: contributors nicknames, comma delimited
	 * Donate link: Link to plugin donate page
	 * Tags: Plugin tags, comma delimited
	 * Requires at least: Minimum WordPress version required
	 * Tested up to: Higher WordPress version the plugin has been tested.
	 * Stable tag: Latest stable tag in repository.
	 * </code>
	 *
	 * Readme data returned array cointains the following:
	 * 		- 'Contributors' - An array with all contributors nicknames.
	 * 		- 'Tags' - An array with all plugin tags.
	 * 		- 'DonateURI' - The donations page address.
	 *      - 'Required' - Minimum required WordPress version.
	 *      - 'Tested' - Higher WordPress version this plugin has been tested.
	 *      - 'Stable' - Last stable tag when this was released.
	 *
	 * The first 8kiB of the file will be pulled in and if the readme data is not
	 * within that first 8kiB, then the plugin author should correct their plugin
	 * and move the plugin data headers to the top.
	 *
	 * The readme file is assumed to have permissions to allow for scripts to read
	 * the file. This is not checked however and the file is only opened for
	 * reading.
	 *
	 * @param string $plugin_file Path to the plugin file (not the readme file)
	 * @return array See above for description.
	 */
	function plugin_readme_data( $plugin_file ) {

		$file = dirname($plugin_file) . '/readme.txt';

		$fp = fopen($file, 'r');	// Open just for reading.
		$data = fread( $fp, 8192 );	// Pull the first 8kiB of the file in.
		fclose($fp);				// Close the file.

		preg_match( '|Contributors:(.*)$|mi', $data, $contributors );
		preg_match( '|Donate link:(.*)$|mi', $data, $uri );
		preg_match( '|Tags:(.*)|i', $data, $tags );
		preg_match( '|Requires at least:(.*)$|mi', $data, $required );
		preg_match( '|Tested up to:(.*)$|mi', $data, $tested );
		preg_match( '|Stable tag:(.*)$|mi', $data, $stable );

		foreach ( array( 'contributors', 'uri', 'tags', 'required', 'tested', 'stable' ) as $field ) {
			if ( !empty( ${$field} ) ) {
				${$field} = trim(${$field}[1]);
			} else {
				${$field} = '';
			}
		}

		$readme_data = array(
			'Contributors' => array_map('trim', explode(',', $contributors)),
			'Tags' => array_map('trim', explode(',', $tags)),
			'DonateURI' => trim($uri),
			'Requires' => trim($required),
			'Tested' => trim($tested),
			'Stable' => trim($stable) );

		return $readme_data;
	}
endif;

if ( ! function_exists('deactivate_plugin') ) :
	/**
	 * Deactivated the plugin. Normally in case incompatibilities were detected.
	 *
	 * TODO: Run Deactivation HOOK
	 *
	 * @param string $name	Plugin name.
	 * @return void
	 */
	function deactivate_plugin( $name ) {
		$plugins = get_option('active_plugins');

		if ( in_array($name, $plugins)) {
			array_splice($plugins, array_search($name, $plugins), 1);
			update_option('active_plugins', $plugins);
		}
	}
endif;
Back to Top