Elgg  Version 3.0
ElggPlugin.php
Go to the documentation of this file.
1 <?php
2 
4 use Elgg\Includer;
5 
12 class ElggPlugin extends ElggObject {
13 
17  protected $package;
18 
22  protected $manifest;
23 
27  protected $path;
28 
34  protected $static_config;
35 
39  protected $errorMsg = '';
40 
44  protected $activated;
45 
49  protected function initializeAttributes() {
50  parent::initializeAttributes();
51 
52  $this->attributes['subtype'] = "plugin";
53  }
54 
65  public static function fromId($plugin_id, $path = null) {
66  if (empty($plugin_id)) {
67  throw new InvalidArgumentException('Plugin ID must be set');
68  }
69 
71 
72  if (!$plugin) {
73  $ia = _elgg_services()->session->setIgnoreAccess(true);
74  $plugin = new ElggPlugin();
75  $plugin->title = $plugin_id;
76  $plugin->save();
77 
78  _elgg_services()->session->setIgnoreAccess($ia);
79  }
80 
81  if (!$path) {
83  }
84 
85  $path = rtrim($path, '/');
86  $plugin->setPath($path . '/' . $plugin_id);
87 
88  return $plugin;
89  }
90 
94  public function save() {
95 
97 
98  $this->attributes['owner_guid'] = $site->guid;
99  $this->attributes['container_guid'] = $site->guid;
100  $this->attributes['access_id'] = ACCESS_PUBLIC;
101 
102  $new = !$this->guid;
103  $priority = null;
104  if ($new) {
105  $name = _elgg_services()->plugins->namespacePrivateSetting('internal', 'priority');
106  $priority = elgg_extract($name, $this->temp_private_settings, 'new');
107  } elseif ($this->getPriority() === null) {
108  $priority = 'last';
109  }
110 
111  if ($priority) {
112  $this->setPriority($priority);
113  }
114 
115  return parent::save();
116  }
117 
123  public function getID() {
124  return $this->title;
125  }
126 
133  public function getDisplayName() {
134  $manifest = $this->getManifest();
135  if ($manifest instanceof ElggPluginManifest) {
136  return $manifest->getName();
137  }
138 
139  return $this->getID();
140  }
141 
150  public function setPath($path) {
151  $this->path = \Elgg\Project\Paths::sanitize($path, true);
152  }
153 
159  public function getPath() {
160  if (isset($this->path)) {
161  return $this->path;
162  }
163 
164  $this->setPath(elgg_get_plugins_path() . $this->getID());
165  return $this->path;
166  }
167 
174  public function getLanguagesPath() {
175  $languages_path = $this->getPath() . 'languages/';
176  if (!is_dir($languages_path)) {
177  return false;
178  }
179 
180  return $languages_path;
181  }
182 
194  public function getStaticConfig($key, $default = null) {
195  if ($this->static_config === null) {
196  $this->static_config = [];
197 
198  try {
200  $this->static_config = $this->includeFile(ElggPluginPackage::STATIC_CONFIG_FILENAME);
201  }
202  } catch (PluginException $ex) {
203  elgg_log($ex, \Psr\Log\LogLevel::ERROR);
204  }
205  }
206 
207  if (isset($this->static_config[$key])) {
208  return $this->static_config[$key];
209  } else {
210  return $default;
211  }
212  }
213 
219  public function getAvailableTextFiles() {
220  $filenames = $this->getPackage()->getTextFilenames();
221 
222  $files = [];
223  foreach ($filenames as $filename) {
224  if ($this->canReadFile($filename)) {
225  $files[$filename] = "{$this->getPath()}{$filename}";
226  }
227  }
228 
229  return $files;
230  }
231 
232  // Load Priority
233 
239  public function getPriority() {
240  $name = _elgg_services()->plugins->namespacePrivateSetting('internal', 'priority');
241 
243  if (isset($priority)) {
244  return (int) $priority;
245  }
246 
247  return null;
248  }
249 
262  public function setPriority($priority) {
264 
265  return _elgg_services()->plugins->setPriority($this, $priority);
266  }
267 
276  public function normalizePriority($priority) {
277  // if no priority assume a priority of 1
278  $old_priority = $this->getPriority();
279  $old_priority = $old_priority ? : 1;
280  $max_priority = _elgg_get_max_plugin_priority() ? : 1;
281 
282  // can't use switch here because it's not strict and php evaluates +1 == 1
283  if ($priority === '+1') {
284  $priority = $old_priority + 1;
285  } else if ($priority === '-1') {
286  $priority = $old_priority - 1;
287  } else if ($priority === 'first') {
288  $priority = 1;
289  } else if ($priority === 'last') {
290  $priority = $max_priority;
291  } else if ($priority === 'new') {
292  $max_priority++;
293  $priority = $max_priority;
294  }
295 
296  return min($max_priority, max(1, (int) $priority));
297  }
298 
299  // Plugin settings
300 
309  public function getSetting($name, $default = null) {
310  $values = $this->getAllSettings();
311  return elgg_extract($name, $values, $default);
312  }
313 
321  public function getAllSettings() {
322 
323  try {
324  $defaults = [];
325  if ($this->isActive()) {
326  // only load settings from static config for active plugins to prevent issues
327  // with internal plugin references ie. classes and language keys
328  $defaults = $this->getStaticConfig('settings', []);
329  }
330 
331  if (!$this->guid) {
333  } else {
334  $settings = _elgg_services()->plugins->getAllSettings($this);
335  }
336 
337  return array_merge($defaults, $settings);
338  } catch (DatabaseException $ex) {
339  return [];
340  }
341  }
342 
351  public function setSetting($name, $value) {
352 
353  $value = _elgg_services()->hooks->trigger('setting', 'plugin', [
354  'plugin_id' => $this->getID(),
355  'plugin' => $this,
356  'name' => $name,
357  'value' => $value,
358  ], $value);
359 
360  if (is_array($value)) {
361  elgg_log('Plugin settings cannot store arrays.', 'ERROR');
362 
363  return false;
364  }
365 
366  return $this->setPrivateSetting($name, $value);
367  }
368 
376  public function unsetSetting($name) {
377  return $this->removePrivateSetting($name);
378  }
379 
384  public function unsetAllSettings() {
385  $settings = $this->getAllSettings();
386 
387  foreach ($settings as $name => $value) {
388  if (strpos($name, 'elgg:internal:') === 0) {
389  continue;
390  }
391  $this->unsetSetting($name);
392  }
393 
394  return true;
395  }
396 
397 
398  // User settings
399 
410  public function getUserSetting($name, $user_guid = 0, $default = null) {
411  $values = $this->getAllUserSettings($user_guid);
412  if ($values === false) {
413  return false;
414  }
415 
416  return elgg_extract($name, $values, $default);
417  }
418 
429  public function getAllUserSettings($user_guid = 0) {
430 
431  $user = _elgg_services()->entityTable->getUserForPermissionsCheck($user_guid);
432  if (!$user instanceof ElggUser) {
433  return [];
434  }
435 
436  $defaults = $this->getStaticConfig('user_settings', []);
437 
438  $settings = _elgg_services()->plugins->getAllUserSettings($this, $user);
439 
440  return array_merge($defaults, $settings);
441  }
442 
452  public function setUserSetting($name, $value, $user_guid = 0) {
453  $user = _elgg_services()->entityTable->getUserForPermissionsCheck($user_guid);
454  if (!$user instanceof ElggUser) {
455  return false;
456  }
457 
458  $value = _elgg_services()->hooks->trigger('usersetting', 'plugin', [
459  'user' => $user,
460  'plugin' => $this,
461  'plugin_id' => $this->getID(),
462  'name' => $name,
463  'value' => $value
464  ], $value);
465 
466  if (is_array($value)) {
467  elgg_log('Plugin user settings cannot store arrays.', 'ERROR');
468 
469  return false;
470  }
471 
472  $name = _elgg_services()->plugins->namespacePrivateSetting('user_setting', $name, $this->getID());
473 
474  return $user->setPrivateSetting($name, $value);
475  }
476 
485  public function unsetUserSetting($name, $user_guid = 0) {
486  $user = _elgg_services()->entityTable->getUserForPermissionsCheck($user_guid);
487 
488  if (!$user instanceof ElggUser) {
489  return false;
490  }
491 
492  $name = _elgg_services()->plugins->namespacePrivateSetting('user_setting', $name, $this->getID());
493 
494  return $user->removePrivateSetting($name);
495  }
496 
505  public function unsetAllUserSettings($user_guid = 0) {
506  $user = _elgg_services()->entityTable->getUserForPermissionsCheck($user_guid);
507 
508  if (!$user instanceof ElggUser) {
509  return false;
510  }
511 
513 
514  foreach ($settings as $name => $value) {
515  $name = _elgg_services()->plugins->namespacePrivateSetting('user_setting', $name, $this->getID());
516  $user->removePrivateSetting($name);
517  }
518 
519  return true;
520  }
521 
528  public function isValid() {
529  if (!$this->getID()) {
530  $this->errorMsg = elgg_echo('ElggPlugin:MissingID', [$this->guid]);
531 
532  return false;
533  }
534 
535  if (!$this->getPackage() instanceof ElggPluginPackage) {
536  $this->errorMsg = elgg_echo('ElggPlugin:NoPluginPackagePackage', [
537  $this->getID(),
538  $this->guid
539  ]);
540 
541  return false;
542  }
543 
544  if (!$this->getPackage()->isValid()) {
545  $this->errorMsg = $this->getPackage()->getError();
546 
547  return false;
548  }
549 
550  return true;
551  }
552 
558  public function isActive() {
559  if (isset($this->activated)) {
560  return $this->activated;
561  }
562 
563  $this->activated = elgg_is_active_plugin($this->getID());
564  return $this->activated;
565  }
566 
573  public function canActivate() {
574  if ($this->isActive()) {
575  return false;
576  }
577 
578  if ($this->getPackage()) {
579  $result = $this->getPackage()->isValid() && $this->getPackage()->checkDependencies();
580  if (!$result) {
581  $this->errorMsg = $this->getPackage()->getError();
582  }
583 
584  return $result;
585  }
586 
587  return false;
588  }
589 
590 
591  // activating and deactivating
592 
600  public function activate() {
601  if ($this->isActive()) {
602  return false;
603  }
604 
605  if (!$this->canActivate()) {
606  return false;
607  }
608 
609  // Check this before setting status because the file could potentially throw
610  if (!$this->isStaticConfigValid()) {
611  return false;
612  }
613 
614  if (!$this->setStatus(true)) {
615  return false;
616  }
617 
618  // perform tasks and emit events
619  // emit an event. returning false will make this not be activated.
620  // we need to do this after it's been fully activated
621  // or the deactivate will be confused.
622  $params = [
623  'plugin_id' => $this->getID(),
624  'plugin_entity' => $this,
625  ];
626 
627  $return = _elgg_services()->events->trigger('activate', 'plugin', $params);
628 
629  // if there are any on_enable functions, start the plugin now and run them
630  // Note: this will not run re-run the init hooks!
631  if ($return) {
632  try {
633  _elgg_services()->events->trigger('cache:flush', 'system');
634 
635  $this->register();
636 
637  // directly load languages to have them available during runtime
638  $this->loadLanguages();
639 
640  $setup = $this->boot();
641  if ($setup instanceof Closure) {
642  $setup();
643  }
644 
645  $this->getBootstrap()->activate();
646 
647  if ($this->canReadFile('activate.php')) {
648  $return = $this->includeFile('activate.php');
649  }
650 
651  $this->init();
652  } catch (PluginException $ex) {
653  elgg_log($ex, \Psr\Log\LogLevel::ERROR);
654 
655  $return = false;
656  }
657  }
658 
659  if ($return === false) {
660  $this->deactivate();
661  } else {
662  elgg_delete_admin_notice("cannot_start {$this->getID()}");
663 
664  _elgg_services()->events->trigger('cache:flush', 'system');
665  _elgg_services()->logger->notice("Plugin {$this->getID()} has been activated");
666  }
667 
668  return $return;
669  }
670 
678  public function canDeactivate() {
679  if (!$this->isActive()) {
680  return false;
681  }
682 
683  $dependents = [];
684 
685  $active_plugins = elgg_get_plugins();
686 
687  foreach ($active_plugins as $plugin) {
688  $manifest = $plugin->getManifest();
689  if (!$manifest) {
690  continue;
691  }
692  $requires = $manifest->getRequires();
693 
694  foreach ($requires as $required) {
695  if ($required['type'] == 'plugin' && $required['name'] == $this->getID()) {
696  // there are active dependents
697  $dependents[$manifest->getPluginID()] = $plugin;
698  }
699  }
700  }
701 
702  if (!empty($dependents)) {
703  $list = array_map(function (\ElggPlugin $plugin) {
704  $css_id = preg_replace('/[^a-z0-9-]/i', '-', $plugin->getManifest()->getID());
705 
706  return elgg_view('output/url', [
707  'text' => $plugin->getDisplayName(),
708  'href' => "#$css_id",
709  ]);
710  }, $dependents);
711  $name = $this->getDisplayName();
712  $list = implode(', ', $list);
713  $this->errorMsg = elgg_echo('ElggPlugin:Dependencies:ActiveDependent', [$name, $list]);
714 
715  return false;
716  }
717 
718  return true;
719  }
720 
727  public function deactivate() {
728  if (!$this->isActive()) {
729  return false;
730  }
731 
732  if (!$this->canDeactivate()) {
733  return false;
734  }
735 
736  // emit an event. returning false will cause this to not be deactivated.
737  $params = [
738  'plugin_id' => $this->getID(),
739  'plugin_entity' => $this,
740  ];
741 
742  $return = _elgg_services()->events->trigger('deactivate', 'plugin', $params);
743  if ($return === false) {
744  return false;
745  }
746 
747  $this->getBootstrap()->deactivate();
748 
749  // run any deactivate code
750  if ($this->canReadFile('deactivate.php')) {
751  // allows you to prevent disabling a plugin by returning false in a deactivate.php file
752  if ($this->includeFile('deactivate.php') === false) {
753  return false;
754  }
755  }
756 
757  $this->deactivateEntities();
758 
759  _elgg_services()->events->trigger('cache:flush', 'system');
760 
761  _elgg_services()->logger->notice("Plugin {$this->getID()} has been deactivated");
762 
763  return $this->setStatus(false);
764  }
765 
772  public function getBootstrap() {
773  $bootstrap = $this->getStaticConfig('bootstrap');
774  if ($bootstrap) {
775  if (!is_subclass_of($bootstrap, \Elgg\PluginBootstrapInterface::class)) {
777  'InvalidBootstrap',
778  $this,
779  elgg_echo('LogicException:InterfaceNotImplemented', [
780  $bootstrap,
781  \Elgg\PluginBootstrapInterface::class
782  ])
783  );
784  }
785 
786  return new $bootstrap($this, _elgg_services()->dic);
787  }
788 
789  return new \Elgg\DefaultPluginBootstrap($this, _elgg_services()->dic);
790  }
791 
799  public function autoload() {
800  $this->registerClasses();
801 
802  $autoload_file = 'vendor/autoload.php';
803  if (!$this->canReadFile($autoload_file)) {
804  return;
805  }
806 
807  $autoloader = Application::requireSetupFileOnce("{$this->getPath()}{$autoload_file}");
808 
809  if (!$autoloader instanceof \Composer\Autoload\ClassLoader) {
810  return;
811  }
812 
813  $autoloader->unregister();
814 
815  // plugins should be appended, composer defaults to prepend
816  $autoloader->register(false);
817  }
818 
828  public function register() {
829  $this->autoload();
830 
831  $this->activateEntities();
832  $this->registerLanguages();
833  $this->registerViews();
834 
835  $this->getBootstrap()->load();
836  }
837 
845  public function boot() {
846  $result = null;
847  if ($this->canReadFile('start.php')) {
848  $result = Application::requireSetupFileOnce("{$this->getPath()}start.php");
849  }
850 
851  $this->getBootstrap()->boot();
852 
853  return $result;
854  }
855 
863  public function init() {
864  $this->registerRoutes();
865  $this->registerActions();
866  $this->registerEntities();
867  $this->registerWidgets();
868 
869  $this->getBootstrap()->init();
870  }
871 
880  protected function includeFile($filename) {
881  $filepath = "{$this->getPath()}{$filename}";
882 
883  if (!$this->canReadFile($filename)) {
884  $msg = elgg_echo(
885  'ElggPlugin:Exception:CannotIncludeFile',
886  [$filename, $this->getID(), $this->guid, $this->getPath()]
887  );
888 
889  throw PluginException::factory('CannotIncludeFile', $this, $msg);
890  }
891 
892  try {
893  $ret = Application::requireSetupFileOnce($filepath);
894  } catch (Exception $e) {
895  $msg = elgg_echo(
896  'ElggPlugin:Exception:IncludeFileThrew',
897  [$filename, $this->getID(), $this->guid, $this->getPath()]
898  );
899 
900  throw PluginException::factory('IncludeFileThrew', $this, $msg, $e);
901  }
902 
903  return $ret;
904  }
905 
913  protected function canReadFile($filename) {
914  $path = "{$this->getPath()}{$filename}";
915 
916  return is_file($path) && is_readable($path);
917  }
918 
925  private function isStaticConfigValid() {
927  return true;
928  }
929 
930  ob_start();
932  if (ob_get_clean() !== '') {
933  $this->errorMsg = elgg_echo('ElggPlugin:activate:ConfigSentOutput');
934 
935  return false;
936  }
937 
938  // make sure can serialize
940  if (!is_array($value)) {
941  $this->errorMsg = elgg_echo('ElggPlugin:activate:BadConfigFormat');
942 
943  return false;
944  }
945 
946  return true;
947  }
948 
955  protected function registerViews() {
956  if (_elgg_config()->system_cache_loaded) {
957  return;
958  }
959 
960  $views = _elgg_services()->views;
961 
962  // Declared views first
963  $file = "{$this->getPath()}views.php";
964  if (is_file($file)) {
965  $spec = Includer::includeFile($file);
966  if (is_array($spec)) {
967  $views->mergeViewsSpec($spec);
968  }
969  }
970 
971  $spec = $this->getStaticConfig('views');
972  if ($spec) {
973  $views->mergeViewsSpec($spec);
974  }
975 
976  // Allow /views directory files to override
977  if (!$views->registerPluginViews($this->getPath(), $failed_dir)) {
978  $key = 'ElggPlugin:Exception:CannotRegisterViews';
979  $args = [$this->getID(), $this->guid, $failed_dir];
980  $msg = elgg_echo($key, $args);
981 
982  throw PluginException::factory('CannotRegisterViews', $this, $msg);
983  }
984  }
985 
991  protected function registerEntities() {
992 
993  $spec = (array) $this->getStaticConfig('entities', []);
994  if (empty($spec)) {
995  return;
996  }
997 
998  foreach ($spec as $entity) {
999  if (isset($entity['type'], $entity['subtype'], $entity['searchable']) && $entity['searchable']) {
1000  elgg_register_entity_type($entity['type'], $entity['subtype']);
1001  }
1002  }
1003  }
1004 
1010  protected function registerActions() {
1011  self::addActionsFromStaticConfig($this->getStaticConfig('actions', []), $this->getPath());
1012  }
1013 
1025  public static function addActionsFromStaticConfig(array $spec, $root_path) {
1026  $actions = _elgg_services()->actions;
1027  $root_path = rtrim($root_path, '/\\');
1028 
1029  foreach ($spec as $action => $action_spec) {
1030  if (!is_array($action_spec)) {
1031  continue;
1032  }
1033 
1034  $access = elgg_extract('access', $action_spec, 'logged_in');
1035  $handler = elgg_extract('controller', $action_spec);
1036  if (!$handler) {
1037  $handler = elgg_extract('filename', $action_spec);
1038  if (!$handler) {
1039  $handler = "$root_path/actions/{$action}.php";
1040  }
1041  }
1042 
1043  $actions->register($action, $handler, $access);
1044  }
1045  }
1046 
1053  protected function registerRoutes() {
1054  $routes = _elgg_services()->routes;
1055 
1056  $spec = (array) $this->getStaticConfig('routes', []);
1057 
1058  foreach ($spec as $name => $route_spec) {
1059  if (!is_array($route_spec)) {
1060  continue;
1061  }
1062 
1063  $routes->register($name, $route_spec);
1064  }
1065  }
1066 
1073  protected function registerWidgets() {
1074  $widgets = _elgg_services()->widgets;
1075 
1076  $spec = (array) $this->getStaticConfig('widgets', []);
1077  foreach ($spec as $widget_id => $widget_definition) {
1078  if (!is_array($widget_definition)) {
1079  continue;
1080  }
1081  if (!isset($widget_definition['id'])) {
1082  $widget_definition['id'] = $widget_id;
1083  }
1084 
1085  $definition = \Elgg\WidgetDefinition::factory($widget_definition);
1086 
1087  $widgets->registerType($definition);
1088  }
1089  }
1090 
1098  public function registerLanguages() {
1099  $languages_path = $this->getLanguagesPath();
1100  if (empty($languages_path)) {
1101  return;
1102  }
1103 
1104  _elgg_services()->translator->registerLanguagePath($languages_path);
1105  }
1106 
1116  protected function loadLanguages() {
1117  $languages_path = $this->getLanguagesPath();
1118  if (empty($languages_path)) {
1119  return;
1120  }
1121 
1122  _elgg_services()->translator->registerTranslations($languages_path);
1123  }
1124 
1130  protected function registerClasses() {
1131  $classes_path = "{$this->getPath()}classes";
1132 
1133  _elgg_services()->autoloadManager->addClasses($classes_path);
1134  }
1135 
1141  protected function activateEntities() {
1142  $spec = (array) $this->getStaticConfig('entities', []);
1143  if (empty($spec)) {
1144  return;
1145  }
1146 
1147  foreach ($spec as $entity) {
1148  if (isset($entity['type'], $entity['subtype'], $entity['class'])) {
1149  elgg_set_entity_class($entity['type'], $entity['subtype'], $entity['class']);
1150  }
1151  }
1152  }
1153 
1159  protected function deactivateEntities() {
1160  $spec = (array) $this->getStaticConfig('entities', []);
1161  if (empty($spec)) {
1162  return;
1163  }
1164 
1165  foreach ($spec as $entity) {
1166  if (isset($entity['type'], $entity['subtype'], $entity['class'])) {
1167  elgg_set_entity_class($entity['type'], $entity['subtype']);
1168  }
1169  }
1170  }
1171 
1180  public function __get($name) {
1181  // See if its in our base attribute
1182  if (array_key_exists($name, $this->attributes)) {
1183  return $this->attributes[$name];
1184  }
1185 
1186  // object title and description are stored as metadata
1187  if (in_array($name, ['title', 'description'])) {
1188  return parent::__get($name);
1189  }
1190 
1191  $result = $this->getPrivateSetting($name);
1192  if ($result !== null) {
1193  return $result;
1194  }
1195 
1196  $defaults = $this->getStaticConfig('settings', []);
1197 
1198  return elgg_extract($name, $defaults, $result);
1199  }
1200 
1211  public function __set($name, $value) {
1212  if (array_key_exists($name, $this->attributes)) {
1213  // Check that we're not trying to change the guid!
1214  if ((array_key_exists('guid', $this->attributes)) && ($name == 'guid')) {
1215  return;
1216  }
1217 
1218  $this->attributes[$name] = $value;
1219 
1220  return;
1221  }
1222 
1223  // object title and description are stored as metadata
1224  if (in_array($name, ['title', 'description'])) {
1225  parent::__set($name, $value);
1226 
1227  return;
1228  }
1229 
1230  // to make sure we trigger the correct hooks
1231  $this->setSetting($name, $value);
1232  }
1233 
1241  private function setStatus($active) {
1242  if (!$this->guid) {
1243  return false;
1244  }
1245 
1247  if ($active) {
1248  $result = add_entity_relationship($this->guid, 'active_plugin', $site->guid);
1249  } else {
1250  $result = remove_entity_relationship($this->guid, 'active_plugin', $site->guid);
1251  }
1252 
1253  if ($result) {
1254  $this->activated = $active;
1255  }
1256 
1257  $this->invalidateCache();
1258 
1259  return $result;
1260  }
1261 
1267  public function getError() {
1268  return $this->errorMsg;
1269  }
1270 
1276  public function getManifest() {
1277  if ($this->manifest instanceof ElggPluginManifest) {
1278  return $this->manifest;
1279  }
1280 
1281  try {
1282  $package = $this->getPackage();
1283  if (!$package) {
1284  throw PluginException::factory('InvalidPackage', $this);
1285  }
1286 
1287  $this->manifest = $package->getManifest();
1288 
1289  return $this->manifest;
1290  } catch (PluginException $e) {
1291  _elgg_services()->logger->warning("Failed to load manifest for plugin $this->guid. " . $e->getMessage());
1292  $this->errorMsg = $e->getMessage();
1293 
1294  elgg_log($e, \Psr\Log\LogLevel::ERROR);
1295  }
1296  }
1297 
1303  public function getPackage() {
1304  if ($this->package instanceof ElggPluginPackage) {
1305  return $this->package;
1306  }
1307 
1308  try {
1309  $this->package = new ElggPluginPackage($this->getPath(), false);
1310 
1311  return $this->package;
1312  } catch (Exception $e) {
1313  _elgg_services()->logger->warning("Failed to load package for $this->guid. " . $e->getMessage());
1314  $this->errorMsg = $e->getMessage();
1315 
1316  elgg_log($e, \Psr\Log\LogLevel::ERROR);
1317  }
1318  }
1319 
1323  public function isCacheable() {
1324  return true;
1325  }
1326 
1330  public function cache($persist = true) {
1331  _elgg_services()->plugins->cache($this);
1332 
1333  parent::cache($persist);
1334  }
1335 
1339  public function invalidateCache() {
1340 
1341  _elgg_services()->boot->invalidateCache();
1342  _elgg_services()->plugins->invalidateCache($this->getID());
1343 
1344  parent::invalidateCache();
1345  }
1346 }
getSetting($name, $default=null)
Returns a plugin setting.
Definition: ElggPlugin.php:309
registerViews()
Registers the plugin&#39;s views.
Definition: ElggPlugin.php:955
elgg_delete_admin_notice($id)
Remove an admin notice by ID.
Definition: admin.php:71
registerEntities()
Registers the plugin&#39;s entities.
Definition: ElggPlugin.php:991
getID()
Returns the ID (dir name) of this plugin.
Definition: ElggPlugin.php:123
$action
Definition: full.php:111
$plugin
registerActions()
Registers the plugin&#39;s actions provided in the plugin config file.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
if(!array_key_exists($filename, $text_files)) $file
$params
Saves global plugin settings.
Definition: save.php:13
add_entity_relationship($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
getLanguagesPath()
Returns the plugin&#39;s languages directory full path with trailing slash.
Definition: ElggPlugin.php:174
registerClasses()
Registers the plugin&#39;s classes.
removePrivateSetting($name)
Removes private setting.
Definition: ElggEntity.php:655
remove_entity_relationship($guid_one, $relationship, $guid_two)
Delete a relationship between two entities.
setPrivateSetting($name, $value)
Adds a private setting to this entity.
Definition: ElggEntity.php:589
$defaults
getAllSettings()
Returns an array of all settings saved for this plugin.
Definition: ElggPlugin.php:321
getPrivateSetting($name)
Returns a private setting value.
Definition: ElggEntity.php:625
invalidateCache()
{}
cache($persist=true)
{}
$guid
Removes an admin notice.
$args
Some servers don&#39;t allow PHP to check the rewrite, so try via AJAX.
registerRoutes()
Registers the plugin&#39;s routes provided in the plugin config file.
setPriority($priority)
Sets the priority of the plugin Returns the new priority or false on error.
Definition: ElggPlugin.php:262
isCacheable()
{}
activateEntities()
Activates the plugin&#39;s entities.
unsetAllUserSettings($user_guid=0)
Removes all plugin settings for a given user.
Definition: ElggPlugin.php:505
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$CONFIG path
Legacy documentation for the old $CONFIG object.
Definition: config.php:17
unsetUserSetting($name, $user_guid=0)
Removes a user setting name and value.
Definition: ElggPlugin.php:485
static addActionsFromStaticConfig(array $spec, $root_path)
Register a plugin&#39;s actions provided in the config file.
deactivateEntities()
Deactivates the plugin&#39;s entities.
PluginException.
getUserSetting($name, $user_guid=0, $default=null)
Returns a user&#39;s setting for this plugin.
Definition: ElggPlugin.php:410
includeFile($filename)
Includes one of the plugins files.
Definition: ElggPlugin.php:880
canDeactivate()
Checks if this plugin can be deactivated on the current Elgg installation.
Definition: ElggPlugin.php:678
canReadFile($filename)
Checks whether a plugin file with the given name exists.
Definition: ElggPlugin.php:913
getDisplayName()
Returns the manifest&#39;s name if available, otherwise the ID.
Definition: ElggPlugin.php:133
$user_guid
Validate a user.
Definition: validate.php:6
static factory($reason, ElggPlugin $plugin=null, $message=null, Throwable $previous=null)
Create a new plugin exception.
Configuration exception.
isActive()
Is this plugin active?
Definition: ElggPlugin.php:558
getPackage()
Returns this plugin&#39;s object.
setUserSetting($name, $value, $user_guid=0)
Sets a user setting for a plugin.
Definition: ElggPlugin.php:452
$entity
Definition: reset.php:8
$title
Definition: user.php:20
static factory(array $options)
Create an WidgetDefinition from an associative array.
registerLanguages()
Registers the plugin&#39;s languages.
getAvailableTextFiles()
Returns an array of available markdown files for this plugin.
Definition: ElggPlugin.php:219
$plugin_id
Definition: save.php:15
$active
Definition: full.php:18
init()
Init the plugin.
Definition: ElggPlugin.php:863
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:786
initializeAttributes()
{}
Definition: ElggPlugin.php:49
normalizePriority($priority)
Normalize and validate new priority.
Definition: ElggPlugin.php:276
$user
Definition: ban.php:7
unsetAllSettings()
Removes all settings for this plugin.
Definition: ElggPlugin.php:384
getStaticConfig($key, $default=null)
Get a value from the plugins&#39;s static config file.
Definition: ElggPlugin.php:194
if(!$menu instanceof\Elgg\Menu\PreparedMenu) $actions
Definition: user_hover.php:16
_elgg_get_max_plugin_priority()
Returns the highest priority of the plugins.
Definition: plugins.php:54
getBootstrap()
Bootstrap object.
Definition: ElggPlugin.php:772
isValid()
Returns if the plugin is complete, meaning has all required files and Elgg can read them and they mak...
Definition: ElggPlugin.php:528
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:130
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif(is_callable([$item, 'getType']))
Definition: item.php:39
$default
Definition: checkbox.php:35
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
getError()
Returns the last error message registered.
$widgets
Definition: widgets.php:35
setSetting($name, $value)
Set a plugin setting for the plugin.
Definition: ElggPlugin.php:351
elgg_get_plugins($status= 'active')
Returns an ordered list of plugins.
Definition: plugins.php:76
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
elgg_set_entity_class($type, $subtype, $class="")
Sets class constructor name for entities with given type and subtype.
Definition: entities.php:39
unserialize($serialized)
{}
Definition: ElggData.php:270
activate()
Actives the plugin for the current site.
Definition: ElggPlugin.php:600
elgg_is_active_plugin($plugin_id)
Returns if a plugin is active for a current site.
Definition: plugins.php:65
registerWidgets()
Registers the plugin&#39;s widgets provided in the plugin config file.
getManifest()
Returns this plugin&#39;s object.
boot()
Boot the plugin.
Definition: ElggPlugin.php:845
$temp_private_settings
Definition: ElggEntity.php:91
deactivate()
Deactivates the plugin.
Definition: ElggPlugin.php:727
__get($name)
Get an attribute, metadata or private setting value.
_elgg_config()
Get the Elgg config service.
$value
Definition: debugging.php:7
getAllUserSettings($user_guid=0)
Returns an array of all user settings saved for this plugin for the user.
Definition: ElggPlugin.php:429
setPath($path)
Set path.
Definition: ElggPlugin.php:150
$required
Definition: label.php:12
unsetSetting($name)
Removes a plugin setting name and value.
Definition: ElggPlugin.php:376
getPriority()
Gets the plugin&#39;s load priority.
Definition: ElggPlugin.php:239
__set($name, $value)
Set a value as attribute, metadata or private setting.
$filename
class
Definition: placeholder.php:21
static sanitize($path, $append_slash=true)
Sanitise file paths ensuring that they begin and end with slashes etc.
Definition: Paths.php:76
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
loadLanguages()
Loads the plugin&#39;s translations.
elgg ajax ERROR
Definition: ajax.js:33
serialize()
{}
Definition: ElggData.php:263
const ACCESS_PUBLIC
Definition: constants.php:14
elgg_get_plugins_path()
Get the plugin path for this installation, ending with slash.
$handler
Definition: add.php:7
elgg_register_entity_type($type, $subtype=null)
Registers an entity type and subtype as a public-facing entity that should be shown in search and by ...
Definition: entities.php:657
$settings
Definition: settings.php:3
canActivate()
Checks if this plugin can be activated on the current Elgg installation.
Definition: ElggPlugin.php:573
foreach($resources as $id=> $href) if(!empty($resources_html)) $files
Definition: details.php:142
elgg_view($view, $vars=[], $viewtype= '')
Return a parsed view.
Definition: views.php:246
static fromId($plugin_id, $path=null)
Load a plugin object from its ID Create a new plugin entity if doesn&#39;t exist.
Definition: ElggPlugin.php:65
getPath()
Returns the plugin&#39;s full path with trailing slash.
Definition: ElggPlugin.php:159
autoload()
Register plugin classes and require composer autoloader.
Definition: ElggPlugin.php:799
$priority
$views
Definition: item.php:17
elgg_get_plugin_from_id($plugin_id)
Returns an object with the path $path.
Definition: plugins.php:28
$access
Definition: save.php:18