/libraries/joomla/plugin/helper.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 287 lines · 156 code · 34 blank · 97 comment · 32 complexity · 3447bd43123977bcf6df6f86537d4042 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Plugin
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Plugin helper class
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Plugin
  15. * @since 11.1
  16. */
  17. abstract class JPluginHelper
  18. {
  19. /**
  20. * A persistent cache of the loaded plugins.
  21. *
  22. * @var array
  23. * @since 11.3
  24. */
  25. protected static $plugins = null;
  26. /**
  27. * Get the path to a layout from a Plugin
  28. *
  29. * @param string $type Plugin type
  30. * @param string $name Plugin name
  31. * @param string $layout Layout name
  32. *
  33. * @return string Layout path
  34. *
  35. * @since 12.2
  36. */
  37. public static function getLayoutPath($type, $name, $layout = 'default')
  38. {
  39. $template = JFactory::getApplication()->getTemplate();
  40. $defaultLayout = $layout;
  41. if (strpos($layout, ':') !== false)
  42. {
  43. // Get the template and file name from the string
  44. $temp = explode(':', $layout);
  45. $template = ($temp[0] == '_') ? $template : $temp[0];
  46. $layout = $temp[1];
  47. $defaultLayout = ($temp[1]) ? $temp[1] : 'default';
  48. }
  49. // Build the template and base path for the layout
  50. $tPath = JPATH_THEMES . '/' . $template . '/html/plg_' . $type . '_' . $name . '/' . $layout . '.php';
  51. $bPath = JPATH_BASE . '/plugins/' . $type . '/' . $name . '/tmpl/' . $defaultLayout . '.php';
  52. $dPath = JPATH_BASE . '/plugins/' . $type . '/' . $name . '/tmpl/default.php';
  53. // If the template has a layout override use it
  54. if (file_exists($tPath))
  55. {
  56. return $tPath;
  57. }
  58. elseif (file_exists($bPath))
  59. {
  60. return $bPath;
  61. }
  62. else
  63. {
  64. return $dPath;
  65. }
  66. }
  67. /**
  68. * Get the plugin data of a specific type if no specific plugin is specified
  69. * otherwise only the specific plugin data is returned.
  70. *
  71. * @param string $type The plugin type, relates to the sub-directory in the plugins directory.
  72. * @param string $plugin The plugin name.
  73. *
  74. * @return mixed An array of plugin data objects, or a plugin data object.
  75. *
  76. * @since 11.1
  77. */
  78. public static function getPlugin($type, $plugin = null)
  79. {
  80. $result = array();
  81. $plugins = self::_load();
  82. // Find the correct plugin(s) to return.
  83. if (!$plugin)
  84. {
  85. foreach ($plugins as $p)
  86. {
  87. // Is this the right plugin?
  88. if ($p->type == $type)
  89. {
  90. $result[] = $p;
  91. }
  92. }
  93. }
  94. else
  95. {
  96. foreach ($plugins as $p)
  97. {
  98. // Is this plugin in the right group?
  99. if ($p->type == $type && $p->name == $plugin)
  100. {
  101. $result = $p;
  102. break;
  103. }
  104. }
  105. }
  106. return $result;
  107. }
  108. /**
  109. * Checks if a plugin is enabled.
  110. *
  111. * @param string $type The plugin type, relates to the sub-directory in the plugins directory.
  112. * @param string $plugin The plugin name.
  113. *
  114. * @return boolean
  115. *
  116. * @since 11.1
  117. */
  118. public static function isEnabled($type, $plugin = null)
  119. {
  120. $result = self::getPlugin($type, $plugin);
  121. return (!empty($result));
  122. }
  123. /**
  124. * Loads all the plugin files for a particular type if no specific plugin is specified
  125. * otherwise only the specific plugin is loaded.
  126. *
  127. * @param string $type The plugin type, relates to the sub-directory in the plugins directory.
  128. * @param string $plugin The plugin name.
  129. * @param boolean $autocreate Autocreate the plugin.
  130. * @param JEventDispatcher $dispatcher Optionally allows the plugin to use a different dispatcher.
  131. *
  132. * @return boolean True on success.
  133. *
  134. * @since 11.1
  135. */
  136. public static function importPlugin($type, $plugin = null, $autocreate = true, JEventDispatcher $dispatcher = null)
  137. {
  138. static $loaded = array();
  139. // Check for the default args, if so we can optimise cheaply
  140. $defaults = false;
  141. if (is_null($plugin) && $autocreate == true && is_null($dispatcher))
  142. {
  143. $defaults = true;
  144. }
  145. if (!isset($loaded[$type]) || !$defaults)
  146. {
  147. $results = null;
  148. // Load the plugins from the database.
  149. $plugins = self::_load();
  150. // Get the specified plugin(s).
  151. for ($i = 0, $t = count($plugins); $i < $t; $i++)
  152. {
  153. if ($plugins[$i]->type == $type && ($plugin === null || $plugins[$i]->name == $plugin))
  154. {
  155. self::_import($plugins[$i], $autocreate, $dispatcher);
  156. $results = true;
  157. }
  158. }
  159. // Bail out early if we're not using default args
  160. if (!$defaults)
  161. {
  162. return $results;
  163. }
  164. $loaded[$type] = $results;
  165. }
  166. return $loaded[$type];
  167. }
  168. /**
  169. * Loads the plugin file.
  170. *
  171. * @param object $plugin The plugin.
  172. * @param boolean $autocreate True to autocreate.
  173. * @param JEventDispatcher $dispatcher Optionally allows the plugin to use a different dispatcher.
  174. *
  175. * @return void
  176. *
  177. * @since 11.1
  178. */
  179. protected static function _import($plugin, $autocreate = true, JEventDispatcher $dispatcher = null)
  180. {
  181. static $paths = array();
  182. $plugin->type = preg_replace('/[^A-Z0-9_\.-]/i', '', $plugin->type);
  183. $plugin->name = preg_replace('/[^A-Z0-9_\.-]/i', '', $plugin->name);
  184. $path = JPATH_PLUGINS . '/' . $plugin->type . '/' . $plugin->name . '/' . $plugin->name . '.php';
  185. if (!isset($paths[$path]))
  186. {
  187. if (file_exists($path))
  188. {
  189. if (!isset($paths[$path]))
  190. {
  191. require_once $path;
  192. }
  193. $paths[$path] = true;
  194. if ($autocreate)
  195. {
  196. // Makes sure we have an event dispatcher
  197. if (!is_object($dispatcher))
  198. {
  199. $dispatcher = JEventDispatcher::getInstance();
  200. }
  201. $className = 'plg' . $plugin->type . $plugin->name;
  202. if (class_exists($className))
  203. {
  204. // Load the plugin from the database.
  205. if (!isset($plugin->params))
  206. {
  207. // Seems like this could just go bye bye completely
  208. $plugin = self::getPlugin($plugin->type, $plugin->name);
  209. }
  210. // Instantiate and register the plugin.
  211. new $className($dispatcher, (array) ($plugin));
  212. }
  213. }
  214. }
  215. else
  216. {
  217. $paths[$path] = false;
  218. }
  219. }
  220. }
  221. /**
  222. * Loads the published plugins.
  223. *
  224. * @return array An array of published plugins
  225. *
  226. * @since 11.1
  227. */
  228. protected static function _load()
  229. {
  230. if (self::$plugins !== null)
  231. {
  232. return self::$plugins;
  233. }
  234. $user = JFactory::getUser();
  235. $cache = JFactory::getCache('com_plugins', '');
  236. $levels = implode(',', $user->getAuthorisedViewLevels());
  237. if (!self::$plugins = $cache->get($levels))
  238. {
  239. $db = JFactory::getDbo();
  240. $query = $db->getQuery(true)
  241. ->select('folder AS type, element AS name, params')
  242. ->from('#__extensions')
  243. ->where('enabled >= 1')
  244. ->where('type =' . $db->quote('plugin'))
  245. ->where('state >= 0')
  246. ->where('access IN (' . $levels . ')')
  247. ->order('ordering');
  248. self::$plugins = $db->setQuery($query)->loadObjectList();
  249. $cache->store(self::$plugins, $levels);
  250. }
  251. return self::$plugins;
  252. }
  253. }