PageRenderTime 26ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/public_html/teams/pm-includes/functions-plugins.php

https://bitbucket.org/smr/omc
PHP | 822 lines | 494 code | 110 blank | 218 comment | 89 complexity | 17e896f87f8a989804013de70b3ae92e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. if(!defined('access')) die ('You are not allowed to execute this file directly.');
  3. /**
  4. * @package AdvPlugin - Advanced Plugin System
  5. * @author Sourcegeek
  6. *
  7. *
  8. */
  9. /**
  10. *@var int
  11. *
  12. */
  13. $version = 1.1;
  14. /**
  15. *@var array
  16. *
  17. */
  18. //$hooks = array();
  19. /**
  20. *@var array
  21. *
  22. */
  23. $filters = array();
  24. /**
  25. *@var string
  26. *
  27. */
  28. $plugins_dir = '';
  29. /**
  30. * private handle
  31. * Error handler
  32. *
  33. */
  34. function handle($function, $err) {
  35. if ( pmdb::connect()->is_error() ) {
  36. pmdb::connect()->last_error = "$function - $err. Function not connected to MySQL";
  37. return false;
  38. }
  39. return true;
  40. }
  41. /**
  42. * public check_plugin
  43. * Checks if the specified plugin exists
  44. *
  45. *@param string plugin
  46. *
  47. */
  48. function check_plugin($plugin) {
  49. global $plugins_dir;
  50. if(substr($plugin, 0, strlen($plugins_dir)) == $plugins_dir) {
  51. $plugin = substr($plugin, strlen($plugins_dir), strlen($plugin));
  52. }
  53. if(substr($plugin, -4) != '.php')
  54. $plugin .= '.php';
  55. if(file_exists($plugins_dir.$plugin))
  56. return true;
  57. return false;
  58. }
  59. /**
  60. * public set_plugins_dir
  61. * Sets new plugins dir
  62. *
  63. *@param string dir
  64. *
  65. */
  66. function set_plugins_dir($dir) {
  67. global $plugins_dir;
  68. if(substr($dir, -1) != '/')
  69. $plugins_dir = $dir.'/';
  70. else
  71. $plugins_dir = $dir;
  72. return true;
  73. }
  74. /**
  75. * public get_plugin_info
  76. * Returns the plugin info
  77. *
  78. *@param string plugin
  79. *
  80. */
  81. function get_plugin_info($plugin) {
  82. global $plugins_dir;
  83. if(substr($plugin, 0, strlen($plugins_dir)) == $plugins_dir) {
  84. $plugin = substr($plugin, strlen($plugins_dir), strlen($plugin));
  85. }
  86. if(substr($plugin, -4) != '.php')
  87. $plugin .= '.php';
  88. $plugin_name = $plugin;
  89. if(check_plugin($plugin) == false) {
  90. pmdb::connect()->last_error = "get_plugin_info - '$plugin' plugin doesn't exists";
  91. return false;
  92. }
  93. // Load it
  94. $plugin = file_get_contents($plugins_dir.$plugin);
  95. if(!$plugin) {
  96. pmdb::connect()->last_error = "get_plugin_info - Cannot load plugin info";
  97. return false;
  98. }
  99. // Get plugin info with regexp
  100. preg_match('/\/\*+(.*)\*\//ims', $plugin, $res);
  101. $plugin_header = $res[1];
  102. $info_types = array(
  103. 'Name: ' => 'name',
  104. 'Plugin URI: ' => 'url',
  105. 'Version: ' => 'version',
  106. 'Description: ' => 'description',
  107. 'Author: ' => 'author',
  108. 'Author URI: ' => 'author_url'
  109. );
  110. foreach($info_types as $regex => $var) {
  111. preg_match("/$regex(.*)/", $plugin_header, $$var);
  112. }
  113. foreach($info_types as $regex => $var) {
  114. if(!empty($$var))
  115. $$var = trim(${$var}[1]);
  116. else
  117. $$var = '';
  118. }
  119. $plugin_info = array(
  120. 'Name' => $name,
  121. 'Plugin URI' => $url,
  122. 'Version' => $version,
  123. 'Description' => $description,
  124. 'Author' => $author,
  125. 'Author URI' => $author_url,
  126. 'Activated' => is_activated($plugin_name)
  127. );
  128. return $plugin_info;
  129. }
  130. /**
  131. * public get_plugins_info
  132. * Returns the plugins info of all the plugins in
  133. * the plugins dir
  134. *
  135. */
  136. function get_plugins_info() {
  137. global $plugins_dir;
  138. $res = array();
  139. foreach(glob($plugins_dir.'*.php') as $file)
  140. $res[$file] = get_plugin_info(str_replace($plugins_dir, '', $file));
  141. if(count($res) == 0)
  142. return false;
  143. return $res;
  144. }
  145. /**
  146. * public get_activated_plugins_info
  147. * Returns the plugin info of all the activated plugins
  148. * in MySQL
  149. *
  150. */
  151. function get_activated_plugins_info() {
  152. global $plugins_dir;
  153. if(handle('get_activated_plugins_info', 'Cannot get plugins info') === false)
  154. return false;
  155. $res = array();
  156. $qry = pmdb::connect()->select( DB . 'plugins', '*', null, null ) or die ('AdvPlugin - get_activated_plugins_info - Cannot get info from MySQL');
  157. while($res = $qry->fetch_assoc()) {
  158. $plugin = $res['plugin_loc'];
  159. if(!file_exists($plugin)) {
  160. pmdb::connect()->last_error = "get_activated_plugins_info - '$plugin' plugin doesn't exists";
  161. return false;
  162. }
  163. $res[$file] = get_plugin_info(str_replace($plugins_dir, '', $plugin));
  164. }
  165. return $res;
  166. }
  167. /**
  168. * public activate_plugin
  169. * Activates the specified plugin
  170. *
  171. *@param string plugin
  172. *
  173. */
  174. function activate_plugin($plugin) {
  175. global $plugins_dir;
  176. if(handle('activate_plugin', 'Cannot activate plugin') === false)
  177. return false;
  178. if(substr($plugin, 0, strlen($plugins_dir)) == $plugins_dir) {
  179. $plugin = substr($plugin, strlen($plugins_dir), strlen($plugin));
  180. }
  181. if(substr($plugin, -4) != '.php')
  182. $plugin .= '.php';
  183. $plugindir = $plugins_dir.$plugin;
  184. if(check_plugin($plugin) == false) {
  185. pmdb::connect()->last_error = "activate_plugin - '$plugin' plugin doesn't exists";
  186. return false;
  187. }
  188. // All were ok, let's activate
  189. // Check if already exist
  190. $qry = pmdb::connect()->query("SELECT * FROM " . DB . "plugins WHERE plugin_loc = '$plugindir'") or die ('AdvPlugin - activate_plugin
  191. - Cannot activate plugin in MySQL');
  192. if($qry->num_rows > 0)
  193. return true;
  194. $qry = pmdb::connect()->query("INSERT INTO " . DB . "plugins(plugin_loc) VALUES ('$plugindir')") or die ('AdvPlugin - activate_plugin
  195. - Cannot activate plugin in MySQL');
  196. return true;
  197. }
  198. /**
  199. * public deactivate_plugin
  200. * Deactivates the specified plugin
  201. *
  202. *@param string plugin
  203. *
  204. */
  205. function deactivate_plugin($plugin) {
  206. global $plugins_dir;
  207. if(handle('deactivate_plugin', 'Cannot deactivate plugin') == false)
  208. return false;
  209. if(substr($plugin, 0, strlen($plugins_dir)) == $plugins_dir) {
  210. $plugin = substr($plugin, strlen($plugins_dir), strlen($plugin));
  211. }
  212. if(substr($plugin, -4) != '.php')
  213. $plugin .= '.php';
  214. $plugindir = $plugins_dir.$plugin;
  215. if(check_plugin($plugin) == false) {
  216. pmdb::connect()->last_error = "deactivate_plugin - '$plugin' plugin doesn't exists";
  217. return false;
  218. }
  219. // Is not activated, so, true...
  220. $qry = pmdb::connect()->query("SELECT * FROM " . DB . "plugins WHERE plugin_loc = '$plugindir'") or die ('AdvPlugin - activate_plugin
  221. - Cannot deactivate plugin in MySQL');
  222. if($qry->num_rows == 0)
  223. return true;
  224. // Activate, delete!
  225. $qry = pmdb::connect()->query("DELETE FROM " . DB . "plugins WHERE plugin_loc = '$plugindir'") or die ('AdvPlugin - activate_plugin
  226. - Cannot deactivate plugin in MySQL');
  227. return true;
  228. }
  229. /**
  230. * public load_plugin
  231. * Loads (includes) the specified plugin (activated or deactivated)
  232. *
  233. *@param string plugin
  234. *
  235. */
  236. function load_plugin($plugin) {
  237. global $plugins_dir;
  238. if(handle('load_plugin', 'Cannot load plugin') == false)
  239. return false;
  240. if(substr($plugin, 0, strlen($plugins_dir)) == $plugins_dir) {
  241. $plugin = substr($plugin, strlen($plugins_dir), strlen($plugin));
  242. }
  243. if(substr($plugin, -4) != '.php')
  244. $plugin .= '.php';
  245. $plugindir = $plugins_dir.$plugin;
  246. if(check_plugin($plugin) == false) {
  247. pmdb::connect()->last_error = "load_plugin - '$plugin' plugin doesn't exists";
  248. return false;
  249. }
  250. if(check_plugin($plugin) == false) {
  251. pmdb::connect()->last_error = "load_plugin - '$plugindir' plugin doesn't exists";
  252. return false;
  253. }
  254. require_once $plugindir;
  255. return true;
  256. }
  257. /**
  258. * public load_activated_plugin
  259. * Loads (includes) the specified plugin ONLY if it is
  260. * activated
  261. *
  262. *@param string plugin
  263. *
  264. */
  265. function load_activated_plugin($plugin) {
  266. global $plugins_dir;
  267. if(handle('load_activated_plugin', 'Cannot load plugin') == false)
  268. return false;
  269. if(substr($plugin, 0, strlen($plugins_dir)) == $plugins_dir) {
  270. $plugin = substr($plugin, strlen($plugins_dir), strlen($plugin));
  271. }
  272. if(substr($plugin, -4) != '.php')
  273. $plugin .= '.php';
  274. $plugindir = $plugins_dir.$plugin;
  275. if(check_plugin($plugin) == false) {
  276. pmdb::connect()->last_error = "load_plugin - '$plugindir' plugin doesn't exists";
  277. return false;
  278. }
  279. $qry = pmdb::connect()->query("SELECT * FROM " . DB . "plugins WHERE plugin_loc = '$plugindir'") or die ('AdvPlugin -
  280. load_activated_plugin - Cannot check active plugin in MySQL');
  281. if($qry->num_rows > 0) {
  282. if(check_plugin($plugin) == false) {
  283. pmdb::connect()->last_error = "load_activated_plugin - '$plugin' plugin doesn't exists";
  284. return false;
  285. }
  286. require_once $plugindir;
  287. }
  288. return true;
  289. }
  290. /**
  291. * public load_activated_plugins
  292. * Loads (includes) all the activated plugins
  293. *
  294. */
  295. function load_activated_plugins() {
  296. if(handle('load_activated_plugins', 'Cannot load plugins') == false)
  297. return false;
  298. $qry = pmdb::connect()->query("SELECT * FROM " . DB . "plugins") or die ('AdvPlugin - load_activated_plugins - Cannot get
  299. loaded plugins');
  300. while($res = $qry->fetch_assoc()) {
  301. $tmpPlugin = $res['plugin_loc'];
  302. if(!file_exists($tmpPlugin)) {
  303. pmdb::connect()->last_error = "load_activated_plugins - '$tmpPlugin' plugin doesn't exists";
  304. return false;
  305. }
  306. require_once $tmpPlugin;
  307. }
  308. }
  309. /**
  310. * public load_plugins
  311. * Loads all the plugins in the plugins dir
  312. *
  313. */
  314. function load_plugins() {
  315. global $plugins_dir;
  316. foreach(glob($plugins_dir.'*.php') as $file)
  317. require_once $file;
  318. return true;
  319. }
  320. /**
  321. * public get_activated_plugins
  322. * Returns all the activated plugins in MySQL
  323. *
  324. */
  325. function get_activated_plugins() {
  326. global $plugins_dir;
  327. if(handle('get_activated_plugins', 'Cannot get activated plugins') == false)
  328. return false;
  329. $qry = pmdb::connect()->query("SELECT * FROM " . DB . "plugins") or die ('AdvPlugin - get_activated_plugins - Cannot get
  330. activated plugins');
  331. $result = array();
  332. while($res = $qry->fetch_assoc()) {
  333. if(file_exists($res['plugin_loc'])) {
  334. if(substr($res['plugin_loc'], 0, strlen($plugins_dir)) == $plugins_dir)
  335. $result[] = substr($res['plugin_loc'], strlen($plugins_dir), strlen($res['plugin_loc']));
  336. }
  337. }
  338. if(count($result) == 0)
  339. return false;
  340. return $result;
  341. }
  342. /**
  343. * public get_plugins
  344. * Returns all the plugins in the plugins dir
  345. *
  346. */
  347. function get_plugins() {
  348. global $plugins_dir;
  349. $res = array();
  350. foreach(glob($plugins_dir.'*.php') as $file)
  351. $res[] = $file;
  352. if(count($res) == 0)
  353. return false;
  354. return $res;
  355. }
  356. /**
  357. * public get_plugins_specific
  358. * Returns all the plugins in the plugins dir
  359. * and specifies if are activated
  360. *
  361. */
  362. function get_plugins_specific() {
  363. global $plugins_dir;
  364. $res = array();
  365. $counter = 0;
  366. foreach(glob($plugins_dir.'*.php') as $file) {
  367. $qry = pmdb::connect()->query("SELECT * FROM " . DB . "plugins WHERE plugin_loc='$file'") or die ('AdvPlugin - get_
  368. plugins_specific - Cannot get plugins from MySQL');
  369. $res[$counter]['file'] = $file;
  370. $res[$counter]['file_nodir'] = str_replace($plugins_dir, '', $file);
  371. if($qry->num_rows > 0)
  372. $res[$counter]['activated'] = true;
  373. else
  374. $res[$counter]['activated'] = false;
  375. $counter++;
  376. }
  377. if(count($res) == 0)
  378. return false;
  379. return $res;
  380. }
  381. /**
  382. * public is_activated
  383. * Checks if the plugin is activated
  384. *
  385. *@param string plugin
  386. *
  387. */
  388. function is_activated($plugin) {
  389. global $plugins_dir;
  390. if(substr($plugin, 0, strlen($plugins_dir)) == $plugins_dir) {
  391. $plugin = substr($plugin, strlen($plugins_dir), strlen($plugin));
  392. }
  393. $plugin = $plugins_dir.$plugin;
  394. $qry = pmdb::connect()->query("SELECT * FROM " . DB . "plugins WHERE plugin_loc='$plugin'") or die ('AdvPlugin -
  395. is_activated - Cannot check activated in MySQL');
  396. if($qry->num_rows > 0)
  397. return true;
  398. return false;
  399. }
  400. /**
  401. * public add_func
  402. * Allows to call a function with another
  403. * name
  404. *
  405. * @param string new_name
  406. * @param string old_name
  407. * @param int params
  408. *
  409. */
  410. function add_func($new_name, $old_name, $params = 0) {
  411. $var_names = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 'r', 't', 'u', 'v', 'w', 'x', 'y', 'z');
  412. if(!is_callable($old_name)) {
  413. pmdb::connect()->last_error[] = "add_func - '$old_name' function is not callable";
  414. return false;
  415. }
  416. $str = "function $new_name(";
  417. $params_ = null;
  418. for($i = 0; $i < $params; $i++)
  419. $params_ .= '$'.$var_names[$i].',';
  420. $params_ = substr($params_, 0, strlen($params_) - 1);
  421. $str .= $params_.") { return $old_name($params_); }";
  422. eval($str);
  423. return true;
  424. }
  425. /**
  426. * Registers a filtering function
  427. *
  428. * Typical use: add_filter('some_hook', 'function_handler_for_hook');
  429. *
  430. * @global array $filters Storage for all of the filters
  431. * @param string $hook the name of the PM element to be filtered or PM action to be triggered
  432. * @param callback $function the name of the function that is to be called.
  433. * @param integer $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default=10, lower=earlier execution, and functions with the same priority are executed in the order in which they were added to the filter)
  434. * @param int $accepted_args optional. The number of arguments the function accept (default is the number provided).
  435. */
  436. function add_filter( $hook, $function, $priority = 10, $accepted_args = NULL ) {
  437. global $filters;
  438. // At this point, we cannot check if the function exists, as it may well be defined later (which is OK)
  439. $id = filter_unique_id( $hook, $function, $priority );
  440. $filters[$hook][$priority][$id] = array(
  441. 'function' => $function,
  442. 'accepted_args' => $accepted_args,
  443. );
  444. }
  445. /**
  446. * add_action
  447. * Adds a hook
  448. *
  449. * @param string hook
  450. * @param string function
  451. * @param integer priority (optional)
  452. *
  453. */
  454. function add_action($hook, $function, $priority = 10, $accepted_args = 1) {
  455. return add_filter( $hook, $function, $priority, $accepted_args );
  456. }
  457. /**
  458. * Build Unique ID for storage and retrieval.
  459. *
  460. * Simply using a function name is not enough, as several functions can have the same name when they are enclosed in classes.
  461. *
  462. * @global array $filters storage for all of the filters
  463. * @param string $hook hook to which the function is attached
  464. * @param string|array $function used for creating unique id
  465. * @param int|bool $priority used in counting how many hooks were applied. If === false and $function is an object reference, we return the unique id only if it already has one, false otherwise.
  466. * @param string $type filter or action
  467. * @return string unique ID for usage as array key
  468. */
  469. function filter_unique_id( $hook, $function, $priority ) {
  470. global $filters;
  471. // If function then just skip all of the tests and not overwrite the following.
  472. if ( is_string($function) )
  473. return $function;
  474. // Object Class Calling
  475. else if (is_object($function[0]) ) {
  476. $obj_idx = get_class($function[0]).$function[1];
  477. if ( !isset($function[0]->_filters_id) ) {
  478. if ( false === $priority )
  479. return false;
  480. $count = isset($filters[$hook][$priority]) ? count((array)$filters[$hook][$priority]) : 0;
  481. $function[0]->_filters_id = $count;
  482. $obj_idx .= $count;
  483. unset($count);
  484. } else
  485. $obj_idx .= $function[0]->_filters_id;
  486. return $obj_idx;
  487. }
  488. // Static Calling
  489. else if ( is_string($function[0]) )
  490. return $function[0].$function[1];
  491. }
  492. /**
  493. * Performs a filtering operation on a PM element or event.
  494. *
  495. * Typical use:
  496. *
  497. * 1) Modify a variable if a function is attached to hook 'hook'
  498. * $var = "default value";
  499. * $var = apply_filter( 'hook', $var );
  500. *
  501. * 2) Trigger functions is attached to event 'pm_event'
  502. * apply_filter( 'event' );
  503. * (see do_action() )
  504. *
  505. * Returns an element which may have been filtered by a filter.
  506. *
  507. * @global array $filters storage for all of the filters
  508. * @param string $hook the name of the PM element or action
  509. * @param mixed $value the value of the element before filtering
  510. * @return mixed
  511. */
  512. function apply_filter( $hook, $value = '' ) {
  513. global $filters;
  514. if ( !isset( $filters[$hook] ) )
  515. return $value;
  516. $args = func_get_args();
  517. // Sort filters by priority
  518. ksort( $filters[$hook] );
  519. // Loops through each filter
  520. reset( $filters[$hook] );
  521. do {
  522. foreach( (array) current($filters[$hook]) as $the_ )
  523. if ( !is_null($the_['function']) ){
  524. $args[1] = $value;
  525. $count = $the_['accepted_args'];
  526. if (is_null($count)) {
  527. $value = call_user_func_array($the_['function'], array_slice($args, 1));
  528. } else {
  529. $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $count));
  530. }
  531. }
  532. } while ( next($filters[$hook]) !== false );
  533. return $value;
  534. }
  535. function do_action( $hook, $arg = '' ) {
  536. $args = array();
  537. if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
  538. $args[] =& $arg[0];
  539. else
  540. $args[] = $arg;
  541. for ( $a = 2; $a < func_num_args(); $a++ )
  542. $args[] = func_get_arg($a);
  543. apply_filter( $hook, $args );
  544. }
  545. /**
  546. * Removes a function from a specified filter hook.
  547. *
  548. * This function removes a function attached to a specified filter hook. This
  549. * method can be used to remove default functions attached to a specific filter
  550. * hook and possibly replace them with a substitute.
  551. *
  552. * To remove a hook, the $function_to_remove and $priority arguments must match
  553. * when the hook was added.
  554. *
  555. * @global array $filters storage for all of the filters
  556. * @param string $hook The filter hook to which the function to be removed is hooked.
  557. * @param callback $function_to_remove The name of the function which should be removed.
  558. * @param int $priority optional. The priority of the function (default: 10).
  559. * @param int $accepted_args optional. The number of arguments the function accepts (default: 1).
  560. * @return boolean Whether the function was registered as a filter before it was removed.
  561. */
  562. function remove_filter( $hook, $function_to_remove, $priority = 10, $accepted_args = 1 ) {
  563. global $filters;
  564. $function_to_remove = filter_unique_id($hook, $function_to_remove, $priority);
  565. $remove = isset ($filters[$hook][$priority][$function_to_remove]);
  566. if ( $remove === true ) {
  567. unset ($filters[$hook][$priority][$function_to_remove]);
  568. if ( empty($filters[$hook][$priority]) )
  569. unset ($filters[$hook]);
  570. }
  571. return $remove;
  572. }
  573. /**
  574. * Check if any filter has been registered for a hook.
  575. *
  576. * @global array $filters storage for all of the filters
  577. * @param string $hook The name of the filter hook.
  578. * @param callback $function_to_check optional. If specified, return the priority of that function on this hook or false if not attached.
  579. * @return int|boolean Optionally returns the priority on that hook for the specified function.
  580. */
  581. function has_filter( $hook, $function_to_check = false ) {
  582. global $filters;
  583. $has = !empty($filters[$hook]);
  584. if ( false === $function_to_check || false == $has ) {
  585. return $has;
  586. }
  587. if ( !$idx = filter_unique_id($hook, $function_to_check, false) ) {
  588. return false;
  589. }
  590. foreach ( (array) array_keys($filters[$hook]) as $priority ) {
  591. if ( isset($filters[$hook][$priority][$idx]) )
  592. return $priority;
  593. }
  594. return false;
  595. }
  596. function has_action( $hook, $function_to_check = false ) {
  597. return has_filter( $hook, $function_to_check );
  598. }
  599. /**
  600. * Return number of active plugins
  601. *
  602. * @return integer Number of activated plugins
  603. */
  604. function has_active_plugins( ) {
  605. if( !property_exists( pmdb::connect(), 'plugins' ) || !pmdb::connect()->plugins )
  606. pmdb::connect()->plugins = array();
  607. return count( pmdb::connect()->plugins );
  608. }
  609. /**
  610. * Display list of links to plugin admin pages, if any
  611. */
  612. function list_plugin_admin_pages() {
  613. if( !property_exists( pmdb::connect(), 'plugin_pages' ) || !pmdb::connect()->plugin_pages )
  614. return;
  615. foreach( (array)pmdb::connect()->plugin_pages as $page ) {
  616. _e( '<a href="plugins.php?page='.$page['slug'].'">'.$page['title']."</a>" );
  617. }
  618. }
  619. /**
  620. * Display list of links to plugin user pages, if any
  621. */
  622. function list_plugin_user_pages() {
  623. if( !property_exists( pmdb::connect(), 'user_pages' ) || !pmdb::connect()->user_pages )
  624. return;
  625. foreach( (array)pmdb::connect()->user_pages as $uPage ) {
  626. _e( '<li><img src="'.$uPage['icon_url'].'" alt="'.$uPage['title'].'" /><a href="'.PM_URI.'/user_page.php?page='.$uPage['slug'].'">'.$uPage['title']."</a></li>" );
  627. }
  628. }
  629. /**
  630. * Register a plugin administration page
  631. */
  632. function register_plugin_page( $slug, $title, $function ) {
  633. if( !property_exists( pmdb::connect(), 'plugin_pages' ) || !pmdb::connect()->plugin_pages )
  634. pmdb::connect()->plugin_pages = array();
  635. pmdb::connect()->plugin_pages[ $slug ] = array(
  636. 'slug' => $slug,
  637. 'title' => $title,
  638. 'function' => $function,
  639. );
  640. }
  641. /**
  642. * Register a plugin user page
  643. */
  644. function register_user_page( $slug, $title, $function, $icon_url = '' ) {
  645. if ( empty($icon_url) )
  646. $icon_url = PM_URI . '/images/cog.png';
  647. if( !property_exists( pmdb::connect(), 'user_pages' ) || !pmdb::connect()->user_pages )
  648. pmdb::connect()->user_pages = array();
  649. pmdb::connect()->user_pages[ $slug ] = array(
  650. 'slug' => $slug,
  651. 'title' => $title,
  652. 'function' => $function,
  653. 'icon_url' => $icon_url,
  654. );
  655. }
  656. /**
  657. * Handle plugin administration page
  658. */
  659. function plugin_admin_page( $plugin_page ) {
  660. // Check the plugin page is actually registered
  661. if( !isset( pmdb::connect()->plugin_pages[$plugin_page] ) ) {
  662. pm_die( 'This page does not exist. Maybe a plugin you thought was activated is inactive?', 'Inactive Plugin' );
  663. }
  664. // Draw the page itself
  665. do_action( 'load-' . $plugin_page);
  666. call_user_func( pmdb::connect()->plugin_pages[$plugin_page]['function'] );
  667. include(PM_DIR . 'pm-includes/footer.php');
  668. die();
  669. }
  670. /**
  671. * Handle plugin user page
  672. */
  673. function plugin_user_page( $user_page ) {
  674. // Check the plugin page is actually registered
  675. if( !isset( pmdb::connect()->user_pages[$user_page] ) ) {
  676. pm_die( 'This page does not exist. Maybe a plugin you thought was activated is inactive?', 'Inactive Plugin' );
  677. }
  678. // Draw the page itself
  679. do_action( 'load-' . $user_page);
  680. call_user_func( pmdb::connect()->user_pages[$user_page]['function'] );
  681. }