bach 3 lat temu
rodzic
commit
fd5d68d5e9
75 zmienionych plików z 5004 dodań i 1283 usunięć
  1. 1 1
      sites/all/modules/rules/DEVELOPER.txt
  2. 10 10
      sites/all/modules/rules/README.txt
  3. 41 29
      sites/all/modules/rules/includes/faces.inc
  4. 372 122
      sites/all/modules/rules/includes/rules.core.inc
  5. 421 0
      sites/all/modules/rules/includes/rules.event.inc
  6. 190 57
      sites/all/modules/rules/includes/rules.plugins.inc
  7. 56 33
      sites/all/modules/rules/includes/rules.processor.inc
  8. 80 46
      sites/all/modules/rules/includes/rules.state.inc
  9. 111 30
      sites/all/modules/rules/includes/rules.upgrade.inc
  10. 41 6
      sites/all/modules/rules/modules/comment.rules.inc
  11. 76 10
      sites/all/modules/rules/modules/data.eval.inc
  12. 68 20
      sites/all/modules/rules/modules/data.rules.inc
  13. 17 5
      sites/all/modules/rules/modules/entity.eval.inc
  14. 32 7
      sites/all/modules/rules/modules/entity.rules.inc
  15. 115 58
      sites/all/modules/rules/modules/events.inc
  16. 114 12
      sites/all/modules/rules/modules/node.eval.inc
  17. 43 53
      sites/all/modules/rules/modules/node.rules.inc
  18. 6 2
      sites/all/modules/rules/modules/path.eval.inc
  19. 8 6
      sites/all/modules/rules/modules/path.rules.inc
  20. 50 17
      sites/all/modules/rules/modules/php.eval.inc
  21. 7 6
      sites/all/modules/rules/modules/php.rules.inc
  22. 46 8
      sites/all/modules/rules/modules/rules_core.eval.inc
  23. 34 7
      sites/all/modules/rules/modules/rules_core.rules.inc
  24. 30 7
      sites/all/modules/rules/modules/system.eval.inc
  25. 23 4
      sites/all/modules/rules/modules/system.rules.inc
  26. 59 9
      sites/all/modules/rules/modules/taxonomy.rules.inc
  27. 35 2
      sites/all/modules/rules/modules/user.eval.inc
  28. 46 5
      sites/all/modules/rules/modules/user.rules.inc
  29. 198 67
      sites/all/modules/rules/rules.api.php
  30. 252 0
      sites/all/modules/rules/rules.drush.inc
  31. 30 11
      sites/all/modules/rules/rules.features.inc
  32. 13 5
      sites/all/modules/rules/rules.info
  33. 127 6
      sites/all/modules/rules/rules.install
  34. 419 113
      sites/all/modules/rules/rules.module
  35. 37 13
      sites/all/modules/rules/rules.rules.inc
  36. 28 12
      sites/all/modules/rules/rules_admin/rules_admin.inc
  37. 7 6
      sites/all/modules/rules/rules_admin/rules_admin.info
  38. 2 1
      sites/all/modules/rules/rules_admin/rules_admin.module
  39. 126 0
      sites/all/modules/rules/rules_admin/tests/rules_admin.test
  40. 5 3
      sites/all/modules/rules/rules_i18n/rules_i18n.i18n.inc
  41. 4 4
      sites/all/modules/rules/rules_i18n/rules_i18n.info
  42. 19 0
      sites/all/modules/rules/rules_i18n/rules_i18n.install
  43. 4 2
      sites/all/modules/rules/rules_i18n/rules_i18n.module
  44. 9 2
      sites/all/modules/rules/rules_i18n/rules_i18n.rules.inc
  45. 14 7
      sites/all/modules/rules/rules_i18n/rules_i18n.test
  46. 104 0
      sites/all/modules/rules/rules_scheduler/includes/rules_scheduler.handler.inc
  47. 4 3
      sites/all/modules/rules/rules_scheduler/includes/rules_scheduler.views.inc
  48. 3 3
      sites/all/modules/rules/rules_scheduler/includes/rules_scheduler.views_default.inc
  49. 5 2
      sites/all/modules/rules/rules_scheduler/includes/rules_scheduler_views_filter.inc
  50. 5 6
      sites/all/modules/rules/rules_scheduler/rules_scheduler.admin.inc
  51. 81 0
      sites/all/modules/rules/rules_scheduler/rules_scheduler.drush.inc
  52. 10 11
      sites/all/modules/rules/rules_scheduler/rules_scheduler.info
  53. 71 5
      sites/all/modules/rules/rules_scheduler/rules_scheduler.install
  54. 89 40
      sites/all/modules/rules/rules_scheduler/rules_scheduler.module
  55. 8 5
      sites/all/modules/rules/rules_scheduler/rules_scheduler.rules.inc
  56. 63 14
      sites/all/modules/rules/rules_scheduler/tests/rules_scheduler.test
  57. 24 0
      sites/all/modules/rules/rules_scheduler/tests/rules_scheduler_test.inc
  58. 12 0
      sites/all/modules/rules/rules_scheduler/tests/rules_scheduler_test.info
  59. 6 0
      sites/all/modules/rules/rules_scheduler/tests/rules_scheduler_test.module
  60. 247 140
      sites/all/modules/rules/tests/rules.test
  61. 3 5
      sites/all/modules/rules/tests/rules_test.info
  62. 5 1
      sites/all/modules/rules/tests/rules_test.module
  63. 140 2
      sites/all/modules/rules/tests/rules_test.rules.inc
  64. 6 3
      sites/all/modules/rules/tests/rules_test.rules_defaults.inc
  65. 7 7
      sites/all/modules/rules/tests/rules_test.test.inc
  66. 11 0
      sites/all/modules/rules/tests/rules_test_invocation.info
  67. 13 0
      sites/all/modules/rules/tests/rules_test_invocation.module
  68. 12 5
      sites/all/modules/rules/ui/rules.autocomplete.js
  69. 9 7
      sites/all/modules/rules/ui/rules.ui.css
  70. 27 15
      sites/all/modules/rules/ui/ui.controller.inc
  71. 213 87
      sites/all/modules/rules/ui/ui.core.inc
  72. 205 24
      sites/all/modules/rules/ui/ui.data.inc
  73. 129 57
      sites/all/modules/rules/ui/ui.forms.inc
  74. 43 13
      sites/all/modules/rules/ui/ui.plugins.inc
  75. 23 4
      sites/all/modules/rules/ui/ui.theme.inc

+ 1 - 1
sites/all/modules/rules/DEVELOPER.txt

@@ -23,4 +23,4 @@ Terminology & Overview
     outside of the rule admin module too. In fact the rules admin module is
     pretty small, as it just relies on the provided UI of the components.
   * The UI is incorporated using the faces object extension mechanism, see
-    rules_rules_plugin_info() for an overview of the used UI extenders.
+    rules_rules_plugin_info() for an overview of the used UI extenders.

+ 10 - 10
sites/all/modules/rules/README.txt

@@ -9,7 +9,7 @@ Maintainers:
 The Rules module allows site administrators to define conditionally executed
 actions based on occurring events (ECA-rules).
 
-Project homepage: http://drupal.org/project/rules
+Project homepage: https://www.drupal.org/project/rules
 
 
 Installation
@@ -18,10 +18,10 @@ Installation
 *Before* starting, make sure that you have read at least the introduction - so
 you know at least the basic concepts. You can find it here:
 
-                      http://drupal.org/node/298480
+                 https://www.drupal.org/node/298480
 
  * Rules depends on the Entity API module, download and install it from
-   http://drupal.org/project/entity
+   https://www.drupal.org/project/entity
  * Copy the whole rules directory to your modules directory
    (e.g. DRUPAL_ROOT/sites/all/modules) and activate the Rules and Rules UI
    modules.
@@ -30,8 +30,8 @@ you know at least the basic concepts. You can find it here:
 
 Documentation
 -------------
-* Check out the general docs at http://drupal.org/node/298476
-* Check out the developer targeted docs at http://drupal.org/node/878718
+* Check out the general docs at https://www.drupal.org/node/298476
+* Check out the developer targeted docs at https://www.drupal.org/node/878718
 
 
 Rules Scheduler
@@ -41,9 +41,9 @@ Rules Scheduler
    to schedule the execution of Rules components.
  * Make sure that you have configured cron for your drupal installation as cron
    is used for scheduling the Rules components. For help see
-   http://drupal.org/cron
- * If the Views module (http://drupal.org/project/views) is installed, the module
-   displays the list of scheduled tasks in the UI.
+   https://www.drupal.org/cron
+ * If the Views module (https://www.drupal.org/project/views) is installed, the
+   module displays the list of scheduled tasks in the UI.
 
 
 Upgrade from Rules 6.x-1.x to Rules 7.x-2.x
@@ -60,7 +60,7 @@ Upgrade from Rules 6.x-1.x to Rules 7.x-2.x
      * Note that for importing an export the export needs to pass the
        configuration integrity check, what might be troublesome if the
        conversion was not 100% successful. In that case, try choosing the
-       immediate saving method and correct the configuration after conversion.  
+       immediate saving method and correct the configuration after conversion.
      * A rule configuration might require multiple modules to be in place and
        upgraded to work properly. E.g. if you used an action provided
        by a third party module, make sure the module is in place and upgraded
@@ -85,7 +85,7 @@ Upgrade from Rules 6.x-1.x to Rules 7.x-2.x
     for Drupal 7. The Drupal 6 tasks are preserved in the database as long as
     you do not clear your Rules 1.x configuration though.
   * The Rules Forms module has not been updated to Drupal 7 and there are no
-    plans to do so, as unfortuntely the module's design does not allow for
+    plans to do so, as unfortunately the module's design does not allow for
     automatic configuration updates.
     Thus, a possible future Rules 2.x Forms module is likely to work
     different, e.g. by working only for entity forms on the field level.

+ 41 - 29
sites/all/modules/rules/includes/faces.inc

@@ -1,7 +1,8 @@
 <?php
 
 /**
- * @file Extendable Object Faces API. Provided by the faces module.
+ * @file
+ * Extendable Object Faces API. Provided by the faces module.
  */
 
 if (!interface_exists('FacesExtenderInterface', FALSE)) {
@@ -14,12 +15,13 @@ if (!interface_exists('FacesExtenderInterface', FALSE)) {
     /**
      * Constructs an instance of the extender.
      */
-    function __construct(FacesExtendable $object);
+    public function __construct(FacesExtendable $object);
 
     /**
      * Returns the extended object.
      */
     public function getExtendable();
+
   }
 
   /**
@@ -31,9 +33,10 @@ if (!interface_exists('FacesExtenderInterface', FALSE)) {
 
 if (!class_exists('FacesExtender', FALSE)) {
   /**
-   * A common base class for FacesExtenders. Extenders may access protected
-   * methods and properties of the extendable using the property() and call()
-   * methods.
+   * A common base class for FacesExtenders.
+   *
+   * Extenders may access protected methods and properties of the extendable
+   * using the property() and call() methods.
    */
   abstract class FacesExtender implements FacesExtenderInterface {
 
@@ -42,8 +45,7 @@ if (!class_exists('FacesExtender', FALSE)) {
      */
     protected $object;
 
-
-    function __construct(FacesExtendable $object) {
+    public function __construct(FacesExtendable $object) {
       $this->object = $object;
     }
 
@@ -63,17 +65,17 @@ if (!class_exists('FacesExtender', FALSE)) {
     }
 
     /**
-     * Invokes any method on the extended object. May be used to invoke
-     * protected methods.
+     * Invokes any method on the extended object, including protected methods.
      *
-     * @param $name
+     * @param string $name
      *   The method name.
-     * @param $arguments
+     * @param array $args
      *   An array of arguments to pass to the method.
      */
     protected function call($name, array $args = array()) {
       return $this->object->call($name, $args);
     }
+
   }
 }
 
@@ -108,7 +110,7 @@ if (!class_exists('FacesExtendable', FALSE)) {
     /**
      * Magic method: Invoke the dynamically implemented methods.
      */
-    function __call($name, $arguments = array()) {
+    public function __call($name, $arguments = array()) {
       if (isset($this->facesMethods[$name])) {
         $method = $this->facesMethods[$name];
         // Include code, if necessary.
@@ -134,9 +136,11 @@ if (!class_exists('FacesExtendable', FALSE)) {
     }
 
     /**
-     * Returns the extender object for the given class. May be used to
-     * explicitly invoke a specific extender, e.g. a function overriding a
-     * method may use that to explicitly invoke the original extender.
+     * Returns the extender object for the given class.
+     *
+     * May be used to explicitly invoke a specific extender, e.g. a function
+     * overriding a method may use that to explicitly invoke the original
+     * extender.
      */
     public function extender($class) {
       if (!isset($this->facesClassInstances[$class])) {
@@ -146,14 +150,17 @@ if (!class_exists('FacesExtendable', FALSE)) {
     }
 
     /**
+     * Returns whether the object can face as the given interface.
+     *
      * Returns whether the object can face as the given interface, thus it
-     * returns TRUE if this oject has been extended by an appropriate
+     * returns TRUE if this object has been extended by an appropriate
      * implementation.
      *
      * @param $interface
-     *   Optional. A interface to test for. If it's omitted, all interfaces that
-     *   the object can be faced as are returned.
-     * @return
+     *   Optional. An interface to test for. If it's omitted, all interfaces
+     *   that the object can be faced as are returned.
+     *
+     * @return bool
      *   Whether the object can face as the interface or an array of interface
      *   names.
      */
@@ -169,9 +176,9 @@ if (!class_exists('FacesExtendable', FALSE)) {
      *
      * @param $interface
      *   The interface name or an array of interface names.
-     * @param $class
+     * @param $className
      *   The extender class, which has to implement the FacesExtenderInterface.
-     * @param $include
+     * @param array $includes
      *   An optional array describing the file to include before invoking the
      *   class. The array entries known are 'type', 'module', and 'name'
      *   matching the parameters of module_load_include(). Only 'module' is
@@ -206,10 +213,10 @@ if (!class_exists('FacesExtendable', FALSE)) {
      * @param $interface
      *   The interface name or FALSE to extend the object without a given
      *   interface.
-     * @param $methods
+     * @param array $callbacks
      *   An array, where the keys are methods of the given interface and the
      *   values the callback functions to use.
-     * @param $includes
+     * @param array $includes
      *   An optional array to describe files to include before invoking the
      *   callbacks. You may pass a single array describing one include for all
      *   callbacks or an array of arrays, keyed by the method names. Look at the
@@ -234,11 +241,11 @@ if (!class_exists('FacesExtendable', FALSE)) {
     /**
      * Override the implementation of an extended method.
      *
-     * @param $methods
-     *   An array of methods of the interface, that should be overriden, where
+     * @param array $callbacks
+     *   An array of methods of the interface, that should be overridden, where
      *   the keys are methods to override and the values the callback functions
      *   to use.
-     * @param $includes
+     * @param array $includes
      *   An optional array to describe files to include before invoking the
      *   callbacks. You may pass a single array describing one include for all
      *   callbacks or an array of arrays, keyed by the method names. Look at the
@@ -257,6 +264,7 @@ if (!class_exists('FacesExtendable', FALSE)) {
 
     /**
      * Adds in include files for the given methods while removing any old files.
+     *
      * If a single include file is described, it's added for all methods.
      */
     protected function addIncludes($methods, $includes) {
@@ -272,6 +280,8 @@ if (!class_exists('FacesExtendable', FALSE)) {
     }
 
     /**
+     * Destroys all references to created instances.
+     *
      * Destroys all references to created instances so that PHP's garbage
      * collection can do its work. This is needed as PHP's gc has troubles with
      * circular references until PHP < 5.3.
@@ -296,9 +306,9 @@ if (!class_exists('FacesExtendable', FALSE)) {
      * This also allows to pass arguments by reference, so it may be used to
      * pass arguments by reference to dynamically extended methods.
      *
-     * @param $name
+     * @param string $name
      *   The method name.
-     * @param $arguments
+     * @param array $args
      *   An array of arguments to pass to the method.
      */
     public function call($name, array $args = array()) {
@@ -307,5 +317,7 @@ if (!class_exists('FacesExtendable', FALSE)) {
       }
       return $this->__call($name, $args);
     }
+
   }
-}
+
+}

Plik diff jest za duży
+ 372 - 122
sites/all/modules/rules/includes/rules.core.inc


+ 421 - 0
sites/all/modules/rules/includes/rules.event.inc

@@ -0,0 +1,421 @@
+<?php
+
+/**
+ * @file
+ * Contains event handler interface and base classes.
+ */
+
+/**
+ * Interface for handling rules events.
+ *
+ * Configurable events (i.e. events making use of settings) have a custom
+ * event suffix, which gets appended to the base event name. The configured
+ * event name of, e.g. the event for viewing an article node, would be
+ * node_view--article, whereas "node_view" is the base event name and "article"
+ * the event suffix as returned from
+ * RulesEventHandlerInterface::getEventNameSuffix(). The event suffix is
+ * generated based upon the event settings and must map to this settings, i.e.
+ * each set of event settings must always generate the same suffix.
+ * For a configurable event to be invoked, rules_invoke_event() has to be called
+ * with the configured event name, e.g.
+ * @code
+ * rules_invoke_event('node_view--' . $node->type, $node, $view_mode);
+ * @endcode
+ * If the event settings are optional, both events have to be invoked whereas
+ * usually the more general event is invoked last. E.g.:
+ * @code
+ * rules_invoke_event('node_view--' . $node->type, $node, $view_mode);
+ * rules_invoke_event('node_view', $node, $view_mode);
+ * @endcode
+ *
+ * Rules event handlers have to be declared using the 'class' key in
+ * hook_rules_event_info(), or may be discovered automatically, see
+ * rules_discover_plugins() for details.
+ *
+ * @see RulesEventHandlerBase
+ * @see RulesEventDefaultHandler
+ */
+interface RulesEventHandlerInterface {
+
+  /**
+   * Constructs the event handler.
+   *
+   * @param string $event_name
+   *   The base event string.
+   * @param array $info
+   *   The event info of the given event.
+   */
+  public function __construct($event_name, $info);
+
+  /**
+   * Sets the event settings.
+   *
+   * @param array $settings
+   *   An array of settings to set.
+   *
+   * @return RulesEventHandlerInterface
+   *   The handler itself for chaining.
+   */
+  public function setSettings(array $settings);
+
+  /**
+   * Gets the event settings.
+   *
+   * @return array
+   *   The array of settings.
+   */
+  public function getSettings();
+
+  /**
+   * Returns an array of default settings.
+   *
+   * @return array
+   *   The array of default settings.
+   */
+  public function getDefaults();
+
+  /**
+   * Returns a user-facing summary of the settings.
+   *
+   * @return string
+   *   The summary in HTML, i.e. properly escaped or filtered.
+   */
+  public function summary();
+
+  /**
+   * Builds the event settings form.
+   *
+   * @param array $form_state
+   *   An associative array containing the current state of the form.
+   *
+   * @return array
+   *   The form structure.
+   */
+  public function buildForm(array &$form_state);
+
+  /**
+   * Validate the event settings independent from a form submission.
+   *
+   * @throws RulesIntegrityException
+   *   In case of validation errors, RulesIntegrityExceptions are thrown.
+   */
+  public function validate();
+
+  /**
+   * Extract the form values and update the event settings.
+   *
+   * @param array $form
+   *   An associative array containing the structure of the form.
+   * @param array $form_state
+   *   An associative array containing the current state of the form.
+   */
+  public function extractFormValues(array &$form, array &$form_state);
+
+  /**
+   * Returns the suffix to be added to the base event named based upon settings.
+   *
+   * If event settings are used, the event name Rules uses for the configured
+   * event is {EVENT_NAME}--{SUFFIX}.
+   *
+   * @return string
+   *   The suffix string. Return an empty string for not appending a suffix.
+   */
+  public function getEventNameSuffix();
+
+  /**
+   * Returns info about the variables provided by this event.
+   *
+   * @return array
+   *   An array of provided variables, keyed by variable names and with the
+   *   variable info array as value.
+   */
+  public function availableVariables();
+
+  /**
+   * Returns the base name of the event the event handler belongs to.
+   *
+   * @return string
+   *   The name of the event the event handler belongs to.
+   */
+  public function getEventName();
+
+  /**
+   * Returns the info array of the event the event handler belongs to.
+   *
+   * @return string
+   *   The info array of the event the event handler belongs to.
+   */
+  public function getEventInfo();
+
+}
+
+/**
+ * Interface for event dispatchers.
+ */
+interface RulesEventDispatcherInterface extends RulesEventHandlerInterface {
+
+  /**
+   * Starts the event watcher.
+   */
+  public function startWatching();
+
+  /**
+   * Stops the event watcher.
+   */
+  public function stopWatching();
+
+  /**
+   * Returns whether the event dispatcher is currently active.
+   *
+   * @return bool
+   *   TRUE if the event dispatcher is currently active, FALSE otherwise.
+   */
+  public function isWatching();
+
+}
+
+/**
+ * Base class for event handler.
+ */
+abstract class RulesEventHandlerBase implements RulesEventHandlerInterface {
+
+  /**
+   * The event name.
+   *
+   * @var string
+   */
+  protected $eventName;
+
+  /**
+   * The event info.
+   *
+   * @var array
+   */
+  protected $eventInfo;
+
+  /**
+   * The event settings.
+   *
+   * @var array
+   */
+  protected $settings = array();
+
+  /**
+   * Implements RulesEventHandlerInterface::__construct().
+   */
+  public function __construct($event_name, $info) {
+    $this->eventName = $event_name;
+    $this->eventInfo = $info;
+    $this->settings = $this->getDefaults();
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::getSettings().
+   */
+  public function getSettings() {
+    return $this->settings;
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::setSettings().
+   */
+  public function setSettings(array $settings) {
+    $this->settings = $settings + $this->getDefaults();
+    return $this;
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::validate().
+   */
+  public function validate() {
+    // Nothing to check by default.
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::extractFormValues().
+   */
+  public function extractFormValues(array &$form, array &$form_state) {
+    foreach ($this->getDefaults() as $key => $setting) {
+      $this->settings[$key] = isset($form_state['values'][$key]) ? $form_state['values'][$key] : $setting;
+    }
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::availableVariables().
+   */
+  public function availableVariables() {
+    return isset($this->eventInfo['variables']) ? $this->eventInfo['variables'] : array();
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::getEventName().
+   */
+  public function getEventName() {
+    return $this->eventName;
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::getEventInfo().
+   */
+  public function getEventInfo() {
+    return $this->eventInfo;
+  }
+
+}
+
+/**
+ * A handler for events having no settings. This is the default handler.
+ */
+class RulesEventDefaultHandler extends RulesEventHandlerBase {
+
+  /**
+   * Implements RulesEventHandlerInterface::buildForm().
+   */
+  public function buildForm(array &$form_state) {
+    return array();
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::getConfiguredEventName().
+   */
+  public function getEventNameSuffix() {
+    return '';
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::summary().
+   */
+  public function summary() {
+    return check_plain($this->eventInfo['label']);
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::getDefaults().
+   */
+  public function getDefaults() {
+    return array();
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::getSettings().
+   */
+  public function getSettings() {
+    return NULL;
+  }
+
+}
+
+/**
+ * Exposes the bundle of an entity as event setting.
+ */
+class RulesEventHandlerEntityBundle extends RulesEventHandlerBase {
+
+  protected $entityType;
+  protected $entityInfo;
+  protected $bundleKey;
+
+  /**
+   * Implements RulesEventHandlerInterface::__construct().
+   */
+  public function __construct($event_name, $info) {
+    parent::__construct($event_name, $info);
+    // Cut off the suffix, e.g. remove 'view' from node_view.
+    $this->entityType = implode('_', explode('_', $event_name, -1));
+    $this->entityInfo = entity_get_info($this->entityType);
+    if (!$this->entityInfo) {
+      throw new InvalidArgumentException('Unsupported event name passed.');
+    }
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::summary().
+   */
+  public function summary() {
+    $bundle = &$this->settings['bundle'];
+    $bundle_label = isset($this->entityInfo['bundles'][$bundle]['label']) ? $this->entityInfo['bundles'][$bundle]['label'] : $bundle;
+    $suffix = isset($bundle) ? ' ' . t('of @bundle-key %name', array('@bundle-key' => $this->getBundlePropertyLabel(), '%name' => $bundle_label)) : '';
+    return check_plain($this->eventInfo['label']) . $suffix;
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::buildForm().
+   */
+  public function buildForm(array &$form_state) {
+    $form['bundle'] = array(
+      '#type' => 'select',
+      '#title' => t('Restrict by @bundle', array('@bundle' => $this->getBundlePropertyLabel())),
+      '#description' => t('If you need to filter for multiple values, either add multiple events or use the "Entity is of bundle" condition instead.'),
+      '#default_value' => $this->settings['bundle'],
+      '#empty_value' => '',
+      '#options' => array(),
+    );
+    foreach ($this->entityInfo['bundles'] as $name => $bundle_info) {
+      $form['bundle']['#options'][$name] = $bundle_info['label'];
+    }
+    return $form;
+  }
+
+  /**
+   * Returns the label to use for the bundle property.
+   *
+   * @return string
+   *   The label to use for the bundle property.
+   */
+  protected function getBundlePropertyLabel() {
+    return $this->entityInfo['entity keys']['bundle'];
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::extractFormValues().
+   */
+  public function extractFormValues(array &$form, array &$form_state) {
+    $this->settings['bundle'] = !empty($form_state['values']['bundle']) ? $form_state['values']['bundle'] : NULL;
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::validate().
+   */
+  public function validate() {
+    if ($this->settings['bundle'] && empty($this->entityInfo['bundles'][$this->settings['bundle']])) {
+      throw new RulesIntegrityException(t('The @bundle %bundle of %entity_type is not known.',
+        array(
+          '%bundle' => $this->settings['bundle'],
+          '%entity_type' => $this->entityInfo['label'],
+          '@bundle' => $this->getBundlePropertyLabel(),
+        )), array(NULL, 'bundle'));
+    }
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::getConfiguredEventName().
+   */
+  public function getEventNameSuffix() {
+    return $this->settings['bundle'];
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::getDefaults().
+   */
+  public function getDefaults() {
+    return array(
+      'bundle' => NULL,
+    );
+  }
+
+  /**
+   * Implements RulesEventHandlerInterface::availableVariables().
+   */
+  public function availableVariables() {
+    $variables = $this->eventInfo['variables'];
+    if ($this->settings['bundle']) {
+      // Add the bundle to all variables of the entity type.
+      foreach ($variables as $name => $variable_info) {
+        if ($variable_info['type'] == $this->entityType) {
+          $variables[$name]['bundle'] = $this->settings['bundle'];
+        }
+      }
+    }
+    return $variables;
+  }
+
+}

+ 190 - 57
sites/all/modules/rules/includes/rules.plugins.inc

@@ -1,15 +1,18 @@
 <?php
 
 /**
- * @file Contains plugin info and implementations not needed for rule evaluation.
+ * @file
+ * Contains plugin info and implementations not needed for rule evaluation.
  */
 
-
 /**
  * Implements a rules action.
  */
 class RulesAction extends RulesAbstractPlugin implements RulesActionInterface {
 
+  /**
+   * @var string
+   */
   protected $itemName = 'action';
 
   /**
@@ -69,6 +72,7 @@ class RulesAction extends RulesAbstractPlugin implements RulesActionInterface {
       }
     }
   }
+
 }
 
 /**
@@ -76,7 +80,14 @@ class RulesAction extends RulesAbstractPlugin implements RulesActionInterface {
  */
 class RulesCondition extends RulesAbstractPlugin implements RulesConditionInterface {
 
+  /**
+   * @var string
+   */
   protected $itemName = 'condition';
+
+  /**
+   * @var bool
+   */
   protected $negate = FALSE;
 
   public function providesVariables() {
@@ -132,19 +143,28 @@ class RulesCondition extends RulesAbstractPlugin implements RulesConditionInterf
 
   public function label() {
     $label = parent::label();
-    return $this->negate ? t('NOT @condition', array('@condition' => $label)) : $label;
+    return $this->negate ? t('NOT !condition', array('!condition' => $label)) : $label;
   }
+
 }
 
 /**
  * An actual rule.
+ *
  * Note: A rule also implements the RulesActionInterface (inherited).
  */
 class Rule extends RulesActionContainer {
 
   protected $conditions = NULL;
+
+  /**
+   * @var string
+   */
   protected $itemName = 'rule';
 
+  /**
+   * @var string
+   */
   public $label = 'unlabeled';
 
   public function __construct($variables = array(), $providesVars = array()) {
@@ -159,8 +179,9 @@ class Rule extends RulesActionContainer {
   }
 
   /**
-   * Get an iterator over all contained conditions. Note that this iterator also
-   * implements the ArrayAcces interface.
+   * Gets an iterator over all contained conditions.
+   *
+   * Note that this iterator also implements the ArrayAccess interface.
    *
    * @return RulesRecursiveElementIterator
    */
@@ -183,8 +204,9 @@ class Rule extends RulesActionContainer {
   }
 
   /**
-   * Get an iterator over all contained actions. Note that this iterator also
-   * implements the ArrayAcces interface.
+   * Gets an iterator over all contained actions.
+   *
+   * Note that this iterator also implements the ArrayAccess interface.
    *
    * @return RulesRecursiveElementIterator
    */
@@ -193,11 +215,12 @@ class Rule extends RulesActionContainer {
   }
 
   /**
-   * Add a condition. Pass either an instance of the RulesConditionInterface
-   * or the arguments as needed by rules_condition().
+   * Adds a condition.
+   *
+   * Pass either an instance of the RulesConditionInterface or the arguments as
+   * needed by rules_condition().
    *
-   * @return Rule
-   *   Returns $this to support chained usage.
+   * @return $this
    */
   public function condition($name, $settings = array()) {
     $this->conditions->condition($name, $settings);
@@ -309,7 +332,9 @@ class Rule extends RulesActionContainer {
   }
 
   /**
-   * Rules may not provided any variable info assertions, as Rules are only
+   * Overrides RulesPlugin::variableInfoAssertions().
+   *
+   * Rules may not provide any variable info assertions, as Rules are only
    * conditionally executed.
    */
   protected function variableInfoAssertions() {
@@ -324,7 +349,7 @@ class Rule extends RulesActionContainer {
   }
 
   /**
-   * Overriden to expose the variables of all actions for embedded rules.
+   * Overridden to expose the variables of all actions for embedded rules.
    */
   public function providesVariables() {
     $provides = parent::providesVariables();
@@ -340,6 +365,7 @@ class Rule extends RulesActionContainer {
     parent::resetInternalCache();
     $this->conditions->resetInternalCache();
   }
+
 }
 
 /**
@@ -347,22 +373,30 @@ class Rule extends RulesActionContainer {
  */
 class RulesReactionRule extends Rule implements RulesTriggerableInterface {
 
+  /**
+   * @var string
+   */
   protected $itemName = 'reaction rule';
+
+  /**
+   * @var array
+   */
   protected $events = array();
 
   /**
-   * Returns the array of events associated with that Rule.
+   * @var array
+   */
+  protected $eventSettings = array();
+
+  /**
+   * Implements RulesTriggerableInterface::events().
    */
-  public function &events() {
+  public function events() {
     return $this->events;
   }
 
   /**
-   * Removes an event from the rule configuration.
-   *
-   * @param $event
-   *   The name of the event to remove.
-   * @return RulesReactionRule
+   * Implements RulesTriggerableInterface::removeEvent().
    */
   public function removeEvent($event) {
     if (($id = array_search($event, $this->events)) !== FALSE) {
@@ -372,10 +406,43 @@ class RulesReactionRule extends Rule implements RulesTriggerableInterface {
   }
 
   /**
-   * @return RulesReactionRule
+   * Implements RulesTriggerableInterface::event().
    */
-  public function event($event) {
-    $this->events[] = $event;
+  public function event($event_name, array $settings = NULL) {
+    // Process any settings and determine the configured event's name.
+    if ($settings) {
+      $handler = rules_get_event_handler($event_name, $settings);
+      if ($suffix = $handler->getEventNameSuffix()) {
+        $event_name .= '--' . $suffix;
+        $this->eventSettings[$event_name] = $settings;
+      }
+      else {
+        // Do not store settings if there is no suffix.
+        unset($this->eventSettings[$event_name]);
+      }
+    }
+    if (array_search($event_name, $this->events) === FALSE) {
+      $this->events[] = $event_name;
+    }
+    return $this;
+  }
+
+  /**
+   * Implements RulesTriggerableInterface::getEventSettings().
+   */
+  public function getEventSettings($event_name) {
+    if (isset($this->eventSettings[$event_name])) {
+      return $this->eventSettings[$event_name];
+    }
+  }
+
+  public function integrityCheck() {
+    parent::integrityCheck();
+    // Check integrity of the configured events.
+    foreach ($this->events as $event_name) {
+      $handler = rules_get_event_handler($event_name, $this->getEventSettings($event_name));
+      $handler->validate();
+    }
     return $this;
   }
 
@@ -394,9 +461,9 @@ class RulesReactionRule extends Rule implements RulesTriggerableInterface {
   }
 
   public function access() {
-    $event_info = rules_fetch_data('event_info');
-    foreach ($this->events as $event) {
-      if (!empty($event_info[$event]['access callback']) && !call_user_func($event_info[$event]['access callback'], 'event', $event)) {
+    foreach ($this->events as $event_name) {
+      $event_info = rules_get_event_info($event_name);
+      if (!empty($event_info['access callback']) && !call_user_func($event_info['access callback'], 'event', $event_info['name'])) {
         return FALSE;
       }
     }
@@ -405,10 +472,10 @@ class RulesReactionRule extends Rule implements RulesTriggerableInterface {
 
   public function dependencies() {
     $modules = array_flip(parent::dependencies());
-    $event_info = rules_fetch_data('event_info');
-    foreach ($this->events as $event) {
-      if (isset($event_info[$event]['module'])) {
-        $modules[$event_info[$event]['module']] = TRUE;
+    foreach ($this->events as $event_name) {
+      $event_info = rules_get_event_info($event_name);
+      if (isset($event_info['module'])) {
+        $modules[$event_info['module']] = TRUE;
       }
     }
     return array_keys($modules);
@@ -433,15 +500,20 @@ class RulesReactionRule extends Rule implements RulesTriggerableInterface {
       else {
         // The intersection of the variables provided by the events are
         // available.
-        $event_info = rules_fetch_data('event_info');
-        $events = array_intersect($this->events, array_keys($event_info));
-        foreach ($events as $event) {
-          $event_info[$event] += array('variables' => array());
+        foreach ($this->events as $event_name) {
+          $handler = rules_get_event_handler($event_name, $this->getEventSettings($event_name));
+
           if (isset($this->availableVariables)) {
-            $this->availableVariables = array_intersect_key($this->availableVariables, $event_info[$event]['variables']);
+            $event_vars = $handler->availableVariables();
+            // Merge variable info by intersecting the variable-info keys also,
+            // so we have only metadata available that is valid for all of the
+            // provided variables.
+            foreach (array_intersect_key($this->availableVariables, $event_vars) as $name => $variable_info) {
+              $this->availableVariables[$name] = array_intersect_key($variable_info, $event_vars[$name]);
+            }
           }
           else {
-            $this->availableVariables = $event_info[$event]['variables'];
+            $this->availableVariables = $handler->availableVariables();
           }
         }
         $this->availableVariables = isset($this->availableVariables) ? RulesState::defaultVariables() + $this->availableVariables : RulesState::defaultVariables();
@@ -451,18 +523,38 @@ class RulesReactionRule extends Rule implements RulesTriggerableInterface {
   }
 
   public function __sleep() {
-    return parent::__sleep() + drupal_map_assoc(array('events'));
+    return parent::__sleep() + drupal_map_assoc(array('events', 'eventSettings'));
   }
 
   protected function exportChildren($key = 'ON') {
-    $export[$key] = array_values($this->events);
+    foreach ($this->events as $event_name) {
+      $export[$key][$event_name] = (array) $this->getEventSettings($event_name);
+    }
     return $export + parent::exportChildren();
   }
 
   protected function importChildren($export, $key = 'ON') {
-    $this->events = $export[$key];
+    // Detect and support old-style exports: a numerically indexed array of
+    // event names.
+    if (is_string(reset($export[$key])) && is_numeric(key($export[$key]))) {
+      $this->events = $export[$key];
+    }
+    else {
+      $this->events = array_keys($export[$key]);
+      $this->eventSettings = array_filter($export[$key]);
+    }
     parent::importChildren($export);
   }
+
+  /**
+   * Overrides optimize().
+   */
+  public function optimize() {
+    parent::optimize();
+    // No need to keep event settings for evaluation.
+    $this->eventSettings = array();
+  }
+
 }
 
 /**
@@ -470,6 +562,9 @@ class RulesReactionRule extends Rule implements RulesTriggerableInterface {
  */
 class RulesAnd extends RulesConditionContainer {
 
+  /**
+   * @var string
+   */
   protected $itemName = 'and';
 
   public function evaluate(RulesState $state) {
@@ -486,6 +581,7 @@ class RulesAnd extends RulesConditionContainer {
   public function label() {
     return !empty($this->label) ? $this->label : ($this->negate ? t('NOT AND') : t('AND'));
   }
+
 }
 
 /**
@@ -493,6 +589,9 @@ class RulesAnd extends RulesConditionContainer {
  */
 class RulesOr extends RulesConditionContainer {
 
+  /**
+   * @var string
+   */
   protected $itemName = 'or';
 
   public function evaluate(RulesState $state) {
@@ -511,6 +610,8 @@ class RulesOr extends RulesConditionContainer {
   }
 
   /**
+   * Overrides RulesContainerPlugin::stateVariables().
+   *
    * Overridden to exclude all variable assertions as in an OR we cannot assert
    * the children are successfully evaluated.
    */
@@ -527,6 +628,7 @@ class RulesOr extends RulesConditionContainer {
     }
     return $vars;
   }
+
 }
 
 /**
@@ -534,6 +636,9 @@ class RulesOr extends RulesConditionContainer {
  */
 class RulesLoop extends RulesActionContainer {
 
+  /**
+   * @var string
+   */
   protected $itemName = 'loop';
   protected $listItemInfo;
 
@@ -640,6 +745,7 @@ class RulesLoop extends RulesActionContainer {
       $this->settings['item:label'] = reset($export['ITEM']);
     }
   }
+
 }
 
 /**
@@ -647,6 +753,9 @@ class RulesLoop extends RulesActionContainer {
  */
 class RulesActionSet extends RulesActionContainer {
 
+  /**
+   * @var string
+   */
   protected $itemName = 'action set';
 
 }
@@ -656,6 +765,9 @@ class RulesActionSet extends RulesActionContainer {
  */
 class RulesRuleSet extends RulesActionContainer {
 
+  /**
+   * @var string
+   */
   protected $itemName = 'rule set';
 
   /**
@@ -672,6 +784,7 @@ class RulesRuleSet extends RulesActionContainer {
   protected function importChildren($export, $key = 'RULES') {
     parent::importChildren($export, $key);
   }
+
 }
 
 /**
@@ -679,8 +792,16 @@ class RulesRuleSet extends RulesActionContainer {
  */
 class RulesEventSet extends RulesRuleSet {
 
+  /**
+   * @var string
+   */
   protected $itemName = 'event set';
-  // Event sets may recurse as we block recursions on rule-level.
+
+  /**
+   * Event sets may recurse as we block recursions on rule-level.
+   *
+   * @var bool
+   */
   public $recursion = TRUE;
 
   public function __construct($info = array()) {
@@ -698,7 +819,10 @@ class RulesEventSet extends RulesRuleSet {
   }
 
   /**
-   * Cache event-sets per event to allow efficient usage via rules_invoke_event().
+   * Rebuilds the event cache.
+   *
+   * We cache event-sets per event in order to allow efficient usage via
+   * rules_invoke_event().
    *
    * @see rules_get_cache()
    * @see rules_invoke_event()
@@ -711,18 +835,27 @@ class RulesEventSet extends RulesRuleSet {
     $rules = rules_config_load_multiple(FALSE, array('plugin' => 'reaction rule', 'active' => TRUE));
 
     foreach ($rules as $name => $rule) {
-      foreach ($rule->events() as $event) {
+      foreach ($rule->events() as $event_name) {
+        $event_base_name = rules_get_event_base_name($event_name);
         // Skip not defined events.
-        if (empty($events[$event])) {
+        if (empty($events[$event_base_name])) {
           continue;
         }
         // Create an event set if not yet done.
-        if (!isset($sets[$event])) {
-          $event_info = $events[$event] + array(
-            'variables' => isset($events[$event]['arguments']) ? $events[$event]['arguments'] : array(),
-          );
-          $sets[$event] = new RulesEventSet($event_info);
-          $sets[$event]->name = $event;
+        if (!isset($sets[$event_name])) {
+          $handler = rules_get_event_handler($event_name, $rule->getEventSettings($event_name));
+
+          // Start the event dispatcher for this event, if any.
+          if ($handler instanceof RulesEventDispatcherInterface && !$handler->isWatching()) {
+            $handler->startWatching();
+          }
+
+          // Update the event info with the variables available based on the
+          // event settings.
+          $event_info = $events[$event_base_name];
+          $event_info['variables'] = $handler->availableVariables();
+          $sets[$event_name] = new RulesEventSet($event_info);
+          $sets[$event_name]->name = $event_name;
         }
 
         // If a rule is marked as dirty, check if this still applies.
@@ -732,23 +865,22 @@ class RulesEventSet extends RulesRuleSet {
         if (!$rule->dirty) {
           // Clone the rule to avoid modules getting the changed version from
           // the static cache.
-          $sets[$event]->rule(clone $rule);
+          $sets[$event_name]->rule(clone $rule);
         }
       }
     }
 
     // Create cache items for all created sets.
-    foreach ($sets as $event => $set) {
+    foreach ($sets as $event_name => $set) {
       $set->sortChildren();
       $set->optimize();
       // Allow modules to alter the cached event set.
-      drupal_alter('rules_event_set', $event, $set);
-      rules_set_cache('event_' . $event, $set);
+      drupal_alter('rules_event_set', $event_name, $set);
+      rules_set_cache('event_' . $event_name, $set);
     }
-    // Cache a list of empty sets so we can use it to speed up later calls.
-    // See rules_get_event_set().
-    $empty_events = array_keys(array_diff_key($events, $sets));
-    variable_set('rules_empty_sets', array_flip($empty_events));
+    // Cache a whitelist of configured events so we can use it to speed up later
+    // calls. See rules_invoke_event().
+    rules_set_cache('rules_event_whitelist', array_flip(array_keys($sets)));
   }
 
   protected function stateVariables($element = NULL) {
@@ -763,4 +895,5 @@ class RulesEventSet extends RulesRuleSet {
   public function save($name = NULL, $module = 'rules') {
     return FALSE;
   }
+
 }

+ 56 - 33
sites/all/modules/rules/includes/rules.processor.inc

@@ -1,7 +1,8 @@
 <?php
 
 /**
- * @file Contains classes for data processing.
+ * @file
+ * Contains classes for data processing.
  *
  * Data processors can be used to process element arguments on evaluation time,
  * e.g. to apply input evaluators or to apply simple calculations to number
@@ -39,25 +40,28 @@ abstract class RulesDataProcessor {
   }
 
   /**
-   * Returns whether the current user has permission to edit this chain of data
-   * processors.
+   * Determines whether the current user has permission to edit this chain of
+   * data processors.
+   *
+   * @return bool
+   *   Whether the current user has permission to edit this chain of data
+   *   processors.
    */
   public function editAccess() {
     return $this->access() && (!isset($this->processor) || $this->processor->editAccess());
   }
 
-
   /**
    * Prepares the processor for parameters.
    *
-   * It turns the settings into a suiting processor object, which gets invoked
+   * It turns the settings into a suitable processor object, which gets invoked
    * on evaluation time.
    *
    * @param $setting
    *   The processor settings which are to be prepared.
    * @param $param_info
    *   The info about the parameter to prepare the processor for.
-   * @param $var_info
+   * @param array $var_info
    *   An array of info about the available variables.
    */
   public static function prepareSetting(&$setting, $param_info, $var_info = array()) {
@@ -88,17 +92,22 @@ abstract class RulesDataProcessor {
 
   /**
    * Returns defined data processors applicable for the given parameter.
-   * Optionally also access to the processors is checked.
+   *
+   * Optionally also checks access to the processors.
    *
    * @param $param_info
    *   If given, only processors valid for this parameter are returned.
+   * @param bool $access_check
+   * @param string $hook
    */
   public static function processors($param_info = NULL, $access_check = TRUE, $hook = 'data_processor_info') {
     static $items = array();
 
     if (!isset($items[$hook]['all'])) {
       $items[$hook]['all'] = rules_fetch_data($hook);
-      uasort($items[$hook]['all'], array(__CLASS__, '_item_sort'));
+      if (isset($items[$hook]['all'])) {
+        uasort($items[$hook]['all'], array(__CLASS__, '_item_sort'));
+      }
     }
     // Data processing isn't supported for multiple types.
     if (isset($param_info) && is_array($param_info['type'])) {
@@ -182,17 +191,20 @@ abstract class RulesDataProcessor {
   }
 
   /**
-   * Processes the value. If $this->processor is set, invoke this processor
-   * first so chaining multiple processors is working.
+   * Processes the value.
+   *
+   * If $this->processor is set, invoke this processor first so chaining
+   * multiple processors is working.
    *
    * @param $value
    *   The value to process.
    * @param $info
    *   Info about the parameter for which we process the value.
-   * @param $state RulesState
+   * @param RulesState $state
    *   The rules evaluation state.
-   * @param $element RulesPlugin
+   * @param RulesPlugin $element
    *   The element for which we process the value.
+   *
    * @return
    *   The processed value.
    */
@@ -200,6 +212,9 @@ abstract class RulesDataProcessor {
 
   /**
    * Return whether the current user has permission to use the processor.
+   *
+   * @return bool
+   *   Whether the current user has permission to use the processor.
    */
   public static function access() {
     return TRUE;
@@ -210,7 +225,7 @@ abstract class RulesDataProcessor {
    *
    * @param $settings
    *   The settings of the processor.
-   * @param $var_info
+   * @param array $var_info
    *   An array of info about the available variables.
    *
    * @return
@@ -219,13 +234,15 @@ abstract class RulesDataProcessor {
   protected static function form($settings, $var_info) {
     return array();
   }
+
 }
 
 
 /**
- * A base processor for use as input evaluators. Input evaluators are not listed
- * in hook_rules_data_processor_info(). Instead they use
- * hook_rules_evaluator_info() and get attached to input forms.
+ * A base processor for use by input evaluators.
+ *
+ * Input evaluators are not listed in hook_rules_data_processor_info(). Instead
+ * they use hook_rules_evaluator_info() and get attached to input forms.
  */
 abstract class RulesDataInputEvaluator extends RulesDataProcessor {
 
@@ -266,9 +283,10 @@ abstract class RulesDataInputEvaluator extends RulesDataProcessor {
   }
 
   /**
-   * Overriden to prepare input evaluator processors. The setting is expected
-   * to be the input value to be evaluated later on and is replaced by the
-   * suiting processor.
+   * Overridden to prepare input evaluator processors.
+   *
+   * The setting is expected to be the input value to be evaluated later on
+   * and is replaced by the suitable processor.
    */
   public static function prepareSetting(&$setting, $param_info, $var_info = array()) {
     $processor = NULL;
@@ -284,7 +302,9 @@ abstract class RulesDataInputEvaluator extends RulesDataProcessor {
   }
 
   /**
-   * Overriden to just attach the help() of evaluators.
+   * Overrides RulesDataProcessor::attachForm().
+   *
+   * Overridden to just attach the help() of evaluators.
    */
   public static function attachForm(&$form, $settings, $param_info, $var_info, $access_check = TRUE) {
     foreach (self::evaluators($param_info, $access_check) as $name => $info) {
@@ -294,14 +314,15 @@ abstract class RulesDataInputEvaluator extends RulesDataProcessor {
   }
 
   /**
-   * Returns all input evaluators that can be applied to the parameters needed
-   * type.
+   * Returns all input evaluators that can be applied to the parameters type.
    */
   public static function evaluators($param_info = NULL, $access_check = TRUE) {
     return parent::processors($param_info, $access_check, 'evaluator_info');
   }
 
   /**
+   * Overrides RulesDataProcessor::processors().
+   *
    * Overridden to default to our hook, thus being equivalent to
    * self::evaluators().
    */
@@ -310,14 +331,16 @@ abstract class RulesDataInputEvaluator extends RulesDataProcessor {
   }
 
   /**
-   * Prepares the evalution, e.g. to determine whether the input evaluator has
-   * been used. If this evaluator should be skipped just unset $this->setting.
+   * Prepares the evaluation.
    *
-   * @param $text
+   * For example, to determine whether the input evaluator has been used.
+   * If this evaluator should be skipped just unset $this->setting.
+   *
+   * @param string $text
    *   The text to evaluate later on.
-   * @param $variables
+   * @param array $variables
    *   An array of info about available variables.
-   * @param $param_info
+   * @param array $param_info
    *   (optional) An array of information about the handled parameter value.
    *   For backward compatibility, this parameter is not required.
    */
@@ -326,9 +349,9 @@ abstract class RulesDataInputEvaluator extends RulesDataProcessor {
   /**
    * Apply the input evaluator.
    *
-   * @param $text
+   * @param string $text
    *   The text to evaluate.
-   * @param $options
+   * @param array $options
    *   A keyed array of settings and flags to control the processing.
    *   Supported options are:
    *   - language: A language object to be used when processing.
@@ -337,7 +360,7 @@ abstract class RulesDataInputEvaluator extends RulesDataProcessor {
    *     certain way.
    *   - sanitize: A boolean flag indicating whether incorporated replacements
    *     should be sanitized.
-   * @param RulesState
+   * @param RulesState $state
    *   The rules evaluation state.
    *
    * @return
@@ -348,13 +371,13 @@ abstract class RulesDataInputEvaluator extends RulesDataProcessor {
   /**
    * Provide some usage help for the evaluator.
    *
-   * @param $variables
+   * @param array $variables
    *   An array of info about available variables.
-   * @param $param_info
+   * @param array $param_info
    *   (optional) An array of information about the handled parameter value.
    *   For backward compatibility, this parameter is not required.
    *
-   * @return
+   * @return array
    *   A renderable array.
    */
   public static function help($variables) {

+ 80 - 46
sites/all/modules/rules/includes/rules.state.inc

@@ -1,7 +1,8 @@
 <?php
 
 /**
- * @file Contains the state and data related stuff.
+ * @file
+ * Contains the state and data related stuff.
  */
 
 /**
@@ -14,16 +15,22 @@ class RulesState {
 
   /**
    * Globally keeps the ids of rules blocked due to recursion prevention.
+   *
+   * @var array
    */
   static protected $blocked = array();
 
   /**
    * The known variables.
+   *
+   * @var array
    */
   public $variables = array();
 
   /**
    * Holds info about the variables.
+   *
+   * @var array
    */
   protected $info = array();
 
@@ -33,8 +40,9 @@ class RulesState {
   protected $save;
 
   /**
-   * Holds the arguments while an element is executed. May be used by the
-   * element to easily access the wrapped arguments.
+   * Holds the arguments while an element is executed.
+   *
+   * May be used by the element to easily access the wrapped arguments.
    */
   public $currentArguments;
 
@@ -43,7 +51,9 @@ class RulesState {
    */
   protected $currentlyBlocked;
 
-
+  /**
+   * Constructs a RulesState object.
+   */
   public function __construct() {
     // Use an object in order to ensure any cloned states reference the same
     // save information.
@@ -131,7 +141,7 @@ class RulesState {
    *
    * If necessary, the specified handler is invoked to fetch the variable.
    *
-   * @param $name
+   * @param string $name
    *   The name of the variable to return.
    *
    * @return
@@ -164,7 +174,8 @@ class RulesState {
    *
    * @param $selector
    *   The data selector of the wrapper to save or just a variable name.
-   * @param $immediate
+   * @param $wrapper
+   * @param bool $immediate
    *   Pass FALSE to postpone saving to later on. Else it's immediately saved.
    */
   public function saveChanges($selector, $wrapper, $immediate = FALSE) {
@@ -234,9 +245,11 @@ class RulesState {
   }
 
   /**
-   * Merges the info about to be saved variables form the given state into the
-   * existing state. Therefor we can aggregate saves from invoked components.
-   * Merged in saves are removed from the given state, but not mergable saves
+   * Merges info from the given state into the existing state.
+   *
+   * Merges the info about to-be-saved variables from the given state into the
+   * existing state. Therefore we can aggregate saves from invoked components.
+   * Merged-in saves are removed from the given state, but not-mergeable saves
    * remain there.
    *
    * @param $state
@@ -267,9 +280,9 @@ class RulesState {
   /**
    * Returns an entity metadata wrapper as specified in the selector.
    *
-   * @param $selector
+   * @param string $selector
    *   The selector string, e.g. "node:author:mail".
-   * @param $langcode
+   * @param string $langcode
    *   (optional) The language code used to get the argument value if the
    *   argument value should be translated. Defaults to LANGUAGE_NONE.
    *
@@ -291,7 +304,7 @@ class RulesState {
     try {
       foreach (explode(':', $parts[1]) as $name) {
         if ($wrapper instanceof EntityListWrapper || $wrapper instanceof EntityStructureWrapper) {
-          // Make sure we are usign the right language. Wrappers might be cached
+          // Make sure we are using the right language. Wrappers might be cached
           // and have previous langcodes set, so always set the right language.
           if ($wrapper instanceof EntityStructureWrapper) {
             $wrapper->language($langcode);
@@ -312,30 +325,37 @@ class RulesState {
 
   /**
    * Magic method. Only serialize variables and their info.
+   *
    * Additionally we remember currently blocked configs, so we can restore them
    * upon deserialization using restoreBlocks().
    */
-  public function __sleep () {
+  public function __sleep() {
     $this->currentlyBlocked = self::$blocked;
     return array('info', 'variables', 'currentlyBlocked');
   }
 
+  /**
+   * Magic method. Unserialize variables and their info.
+   */
   public function __wakeup() {
     $this->save = new ArrayObject();
   }
 
   /**
-   * Restore the before serialization blocked configurations.
+   * Restores the before-serialization blocked configurations.
    *
    * Warning: This overwrites any possible currently blocked configs. Thus
-   * do not invoke this method, if there might be evaluations active.
+   * do not invoke this method if there might be evaluations active.
    */
   public function restoreBlocks() {
     self::$blocked = $this->currentlyBlocked;
   }
 
   /**
-   * Defines always available variables.
+   * Defines always-available variables.
+   *
+   * @param $key
+   *   (optional)
    */
   public static function defaultVariables($key = NULL) {
     // Add a variable for accessing site-wide data properties.
@@ -350,12 +370,13 @@ class RulesState {
     );
     return isset($key) ? $vars[$key] : $vars;
   }
+
 }
 
 /**
  * A class holding static methods related to data.
  */
-class RulesData  {
+class RulesData {
 
   /**
    * Returns whether the type match. They match if type1 is compatible to type2.
@@ -364,11 +385,11 @@ class RulesData  {
    *   The name of the type to check for whether it is compatible to type2.
    * @param $param_info
    *   The type expression to check for.
-   * @param $ancestors
-   *   Whether sub-type relationships for checking type compatibility should be
-   *   taken into account. Defaults to TRUE.
+   * @param bool $ancestors
+   *   (optional) Whether sub-type relationships for checking type compatibility
+   *   should be taken into account. Defaults to TRUE.
    *
-   * @return
+   * @return bool
    *   Whether the types match.
    */
   public static function typesMatch($var_info, $param_info, $ancestors = TRUE) {
@@ -395,7 +416,7 @@ class RulesData  {
       $cache = &rules_get_cache();
       self::typeCalcAncestors($cache, $var_type);
       // If one of the types is an ancestor return TRUE.
-      return (bool)array_intersect_key($cache['data_info'][$var_type]['ancestors'], array_flip($valid_types));
+      return (bool) array_intersect_key($cache['data_info'][$var_type]['ancestors'], array_flip($valid_types));
     }
     return FALSE;
   }
@@ -417,25 +438,27 @@ class RulesData  {
   }
 
   /**
-   * Returns matching data variables or properties for the given info and the to
-   * be configured parameter.
+   * Returns data for the given info and the to-be-configured parameter.
+   *
+   * Returns matching data variables or properties for the given info and the
+   * to-be-configured parameter.
    *
    * @param $source
    *   Either an array of info about available variables or a entity metadata
    *   wrapper.
    * @param $param_info
    *   The information array about the to be configured parameter.
-   * @param $prefix
+   * @param string $prefix
    *   An optional prefix for the data selectors.
-   * @param $recursions
+   * @param int $recursions
    *   The number of recursions used to go down the tree. Defaults to 2.
-   * @param $suggestions
+   * @param bool $suggestions
    *   Whether possibilities to recurse are suggested as soon as the deepest
    *   level of recursions is reached. Defaults to TRUE.
    *
-   * @return
-   *  An array of info about matching variables or properties that match, keyed
-   *  with the data selector.
+   * @return array
+   *   An array of info about matching variables or properties that match, keyed
+   *   with the data selector.
    */
   public static function matchingDataSelector($source, $param_info, $prefix = '', $recursions = 2, $suggestions = TRUE) {
     // If an array of info is given, get entity metadata wrappers first.
@@ -467,7 +490,8 @@ class RulesData  {
           $matches += self::matchingDataSelector($wrapper, $param_info, $prefix . $name . ':', $recursions - 1, $suggestions);
         }
         elseif ($suggestions) {
-          // We may not recurse any more, but indicate the possibility to recurse.
+          // We may not recurse any more,
+          // but indicate the possibility to recurse.
           $matches[$prefix . $name . ':'] = $wrapper->info();
           if (!is_array($source) && $source instanceof EntityListWrapper) {
             // Add some more possible list items.
@@ -482,8 +506,10 @@ class RulesData  {
   }
 
   /**
-   * Adds asserted metadata to the variable info. In case there are already
-   * assertions for a variable, the assertions are merged such that both apply.
+   * Adds asserted metadata to the variable info.
+   *
+   * In case there are already assertions for a variable, the assertions are
+   * merged such that both apply.
    *
    * @see RulesData::applyMetadataAssertions()
    */
@@ -508,10 +534,9 @@ class RulesData  {
         // before the child-wrapper is created.
         if (count($parts) == 1) {
           // Support asserting a type in case of generic entity references only.
-          if (isset($assertion['type']) && $var_info[$parts[0]]['type'] == 'entity') {
-            if (entity_get_info($assertion['type'])) {
-              $var_info[$parts[0]]['type'] = $assertion['type'];
-            }
+          $var_type = &$var_info[$parts[0]]['type'];
+          if (isset($assertion['type']) && ($var_type == 'entity' || $var_type == 'list<entity>')) {
+            $var_type = $assertion['type'];
             unset($assertion['type']);
           }
           // Add any single bundle directly to the variable info, so the
@@ -547,8 +572,9 @@ class RulesData  {
   }
 
   /**
-   * Property info alter callback for the entity metadata wrapper for applying
-   * the rules metadata assertions.
+   * Property info alter callback for the entity metadata wrapper.
+   *
+   * Used for applying the rules metadata assertions.
    *
    * @see RulesData::addMetadataAssertions()
    */
@@ -586,7 +612,8 @@ class RulesData  {
         $property_info['properties'][$key]['rules assertion'] = $assertion[$key];
         $property_info['properties'][$key]['property info alter'] = array('RulesData', 'applyMetadataAssertions');
 
-        // Apply any 'type' and 'bundle' assertion directly to the propertyinfo.
+        // Apply any 'type' and 'bundle' assertion directly to the property
+        // info.
         if (isset($assertion[$key]['#info']['type'])) {
           $type = $assertion[$key]['#info']['type'];
           // Support asserting a type in case of generic entity references only.
@@ -608,9 +635,10 @@ class RulesData  {
   }
 
   /**
-   * Property info alter callback for the entity metadata wrapper to inject
-   * metadata for the 'site' variable. In contrast to doing this via
-   * hook_rules_data_info() this callback makes use of the already existing
+   * Property info alter callback for the entity metadata wrapper.
+   *
+   * Used to inject metadata for the 'site' variable. In contrast to doing this
+   * via hook_rules_data_info() this callback makes use of the already existing
    * property info cache for site information of entity metadata.
    *
    * @see RulesPlugin::availableVariables()
@@ -622,6 +650,7 @@ class RulesData  {
     // have specified further metadata.
     return RulesData::applyMetadataAssertions($wrapper, $property_info);
   }
+
 }
 
 /**
@@ -653,7 +682,7 @@ abstract class RulesIdentifiableDataWrapper extends EntityStructureWrapper {
    *   The type of the passed data.
    * @param $data
    *   Optional. The data to wrap or its identifier.
-   * @param $info
+   * @param array $info
    *   Optional. Used internally to pass info about properties down the tree.
    */
   public function __construct($type, $data = NULL, $info = array()) {
@@ -722,7 +751,7 @@ abstract class RulesIdentifiableDataWrapper extends EntityStructureWrapper {
   }
 
   /**
-   * Prepare for serializiation.
+   * Prepare for serialization.
    */
   public function __sleep() {
     $vars = parent::__sleep();
@@ -734,6 +763,9 @@ abstract class RulesIdentifiableDataWrapper extends EntityStructureWrapper {
     return $vars;
   }
 
+  /**
+   * Prepare for unserialization.
+   */
   public function __wakeup() {
     if ($this->id !== FALSE) {
       // Make sure data is set, so the data will be loaded when needed.
@@ -756,10 +788,11 @@ abstract class RulesIdentifiableDataWrapper extends EntityStructureWrapper {
    *   The loaded data object, or FALSE if loading failed.
    */
   abstract protected function load($id);
+
 }
 
 /**
- * Interface that allows custom wrapper classes to declare that they are savable.
+ * Used to declare custom wrapper classes as savable.
  */
 interface RulesDataWrapperSavableInterface {
 
@@ -767,4 +800,5 @@ interface RulesDataWrapperSavableInterface {
    * Save the currently wrapped data.
    */
   public function save();
+
 }

+ 111 - 30
sites/all/modules/rules/includes/rules.upgrade.inc

@@ -28,8 +28,8 @@ function rules_upgrade_form($form, &$form_state) {
     '#prefix' => '<p>',
     '#suffix' => '</p>',
     '#markup' => t('This form allows you to convert rules or rule sets from Rules 1.x to Rules 2.x.') . ' ' .
-      t('In order to convert a rule or rule set make sure you have all dependend modules installed and upgraded, i.e. modules which provide Rules integration that has been used in your rules or rule sets. In addition those modules may need to implement some Rules specific update hooks for the conversion to properly work.') . ' ' .
-      t('After conversion, the old rules and rule sets will stay in the database until you manually delete them. That way you can make sure the conversion has gone right before you delete the old rules and rule sets.')
+      t('In order to convert a rule or rule set make sure you have all dependent modules installed and upgraded, i.e. modules which provide Rules integration that has been used in your rules or rule sets. In addition those modules may need to implement some Rules specific update hooks for the conversion to properly work.') . ' ' .
+      t('After conversion, the old rules and rule sets will stay in the database until you manually delete them. That way you can make sure the conversion has gone right before you delete the old rules and rule sets.'),
   );
 
   $option_rules = $option_sets = array();
@@ -51,7 +51,7 @@ function rules_upgrade_form($form, &$form_state) {
     $form['clear'] = array(
       '#prefix' => '<p>',
       '#suffix' => '</p>',
-      '#markup' => t('Once you have successfully converted your configuration, you can clean up your database and <a href="!url">delete</a> all Rules 1.x configurations.', array('!url' => url('admin/config/workflow/rules/upgrade/clear')))
+      '#markup' => t('Once you have successfully converted your configuration, you can clean up your database and <a href="!url">delete</a> all Rules 1.x configurations.', array('!url' => url('admin/config/workflow/rules/upgrade/clear'))),
     );
   }
 
@@ -72,8 +72,8 @@ function rules_upgrade_form($form, &$form_state) {
     '#type' => 'radios',
     '#title' => t('Method'),
     '#options' => array(
-       'export' => t('Convert configuration and export it.'),
-       'save' => t('Convert configuration and save it.'),
+      'export' => t('Convert configuration and export it.'),
+      'save' => t('Convert configuration and save it.'),
     ),
     '#default_value' => 'export',
   );
@@ -81,7 +81,7 @@ function rules_upgrade_form($form, &$form_state) {
   $form['actions']['convert'] = array(
     '#type' => 'submit',
     '#value' => t('Convert'),
-    '#disabled' => !db_table_exists('rules_rules')
+    '#disabled' => !db_table_exists('rules_rules'),
   );
   return $form;
 }
@@ -137,6 +137,9 @@ function rules_upgrade_confirm_clear_form($form, $form_state) {
   return confirm_form($form, $confirm_question, 'admin/config/workflow/rules/upgrade', $confirm_question_long, t('Delete data'), t('Cancel'));
 }
 
+/**
+ * Submit handler for deleting data.
+ */
 function rules_upgrade_confirm_clear_form_submit($form, &$form_state) {
   db_drop_table('rules_rules');
   db_drop_table('rules_sets');
@@ -206,7 +209,7 @@ function rules_upgrade_convert_rule_set($name, $cfg_old) {
   }
 
   // Add in all rules of the set.
-  foreach(_rules_upgrade_fetch_all_rules() as $rule_name => $rule) {
+  foreach (_rules_upgrade_fetch_all_rules() as $rule_name => $rule) {
     if ($rule['#set'] == $name) {
       drupal_set_message(' >> ' . t('Converting %plugin %name...', array('%plugin' => t('rule'), '%name' => $rule_name . ': ' . $rule['#label'])));
       $new_rule = rules_upgrade_plugin_factory($rule);
@@ -220,9 +223,9 @@ function rules_upgrade_convert_rule_set($name, $cfg_old) {
 /**
  * Convert a single element.
  *
- * @param $element
+ * @param array $element
  *   The element to convert.
- * @param $target
+ * @param RulesPlugin $target
  *   The converted element to write to.
  */
 function rules_upgrade_convert_element(array $element, RulesPlugin $target) {
@@ -235,7 +238,7 @@ function rules_upgrade_convert_element(array $element, RulesPlugin $target) {
   foreach ($target->pluginParameterInfo() as $name => $info) {
     rules_upgrade_element_parameter_settings($element, $target, $name);
   }
-  // @todo: Care about php input evaluator for non-text parameters.
+  // @todo Care about php input evaluator for non-text parameters.
 
   // Take care of variable names and labels.
   foreach ($target->pluginProvidesVariables() as $name => $info) {
@@ -268,10 +271,10 @@ function rules_upgrade_convert_element(array $element, RulesPlugin $target) {
 
   // Invoke action/condition specific hooks and a general one.
   if (($element['#type'] == 'action' || $element['#type'] == 'condition')) {
-    if (function_exists($function = $element['#name'] .'_upgrade')) {
+    if (function_exists($function = $element['#name'] . '_upgrade')) {
       $element_name = $function($element, $target);
     }
-    elseif (isset($element['#info']['base']) && function_exists($function = $element['#info']['base'] .'_upgrade')) {
+    elseif (isset($element['#info']['base']) && function_exists($function = $element['#info']['base'] . '_upgrade')) {
       $element_name = $function($element, $target);
     }
   }
@@ -299,8 +302,10 @@ function rules_upgrade_plugin_factory($element) {
   switch ($element['#type']) {
     case 'OR':
       return rules_plugin_factory('or');
+
     case 'AND':
       return rules_plugin_factory('and');
+
     default:
       return rules_plugin_factory($element['#type']);
 
@@ -329,7 +334,7 @@ function rules_upgrade_plugin_factory($element) {
         }
 
         // Call the upgrade callback if one has been defined.
-        if (function_exists($function = $element['#name'] .'_upgrade_map_name') || (isset($element['#info']['base']) && function_exists($function = $element['#info']['base'] .'_upgrade_map_name'))) {
+        if (function_exists($function = $element['#name'] . '_upgrade_map_name') || (isset($element['#info']['base']) && function_exists($function = $element['#info']['base'] . '_upgrade_map_name'))) {
           $element_name = $function($element);
         }
         if (!isset($element_name)) {
@@ -380,59 +385,77 @@ function rules_upgrade_element_variable_settings($element, $target, $name, $new_
  * Upgrade callbacks for upgrading the provided Rules 1.x integration.
  */
 
-// Comment.module integration.
+/**
+ * Comment.module integration.
+ */
 function rules_action_load_comment_upgrade_map_name($element) {
   return 'entity_fetch';
 }
+
 function rules_action_load_comment_upgrade($element, $target) {
   $target->settings['type'] = 'comment';
   rules_upgrade_element_parameter_settings($element, $target, 'cid', 'id');
   rules_upgrade_element_variable_settings($element, $target, 'comment_loaded', 'entity_fetched');
 }
 
-// Node.module integration.
+/**
+ * Node.module integration.
+ */
 function rules_condition_content_is_type_upgrade_map_name($element) {
   return 'node_is_of_type';
 }
+
 function rules_condition_content_is_published_upgrade_map_name($element) {
   return 'node_is_published';
 }
+
 function rules_condition_content_is_sticky_upgrade_map_name($element) {
   return 'node_is_sticky';
 }
+
 function rules_condition_content_is_promoted_upgrade_map_name($element) {
   return 'node_is_promoted';
 }
+
 function rules_condition_content_is_new_upgrade_map_name($element) {
   return 'entity_is_new';
 }
+
 function rules_condition_content_is_new_upgrade($element, $target) {
   rules_upgrade_element_parameter_settings($element, $target, 'node', 'entity');
 }
+
 function rules_action_node_set_author_upgrade_map_name($element) {
   return 'data_set';
 }
+
 function rules_action_node_set_author_upgrade($element, $target) {
   $target->settings['data:select'] = $element['#settings']['#argument map']['node'] . ':author';
   $target->settings['value:select'] = $element['#settings']['#argument map']['author'];
 }
+
 function rules_action_node_load_author_upgrade_map_name($element) {
   return 'entity_fetch';
 }
+
 function rules_action_node_load_author_upgrade($element, $target) {
   $target->settings['type'] = 'user';
   $target->settings['id'] = $element['#settings']['#argument map']['node'] . ':author:uid';
 }
+
 function rules_action_set_node_title_upgrade_map_name($element) {
   return 'data_set';
 }
+
 function rules_action_set_node_title_upgrade($element, $target) {
   $target->settings['data:select'] = $element['#settings']['#argument map']['node'] . ':title';
   $target->settings['value'] = $element['#settings']['title'];
 }
+
 function rules_action_add_node_upgrade_map_name($element) {
   return 'entity_create';
 }
+
 function rules_action_add_node_upgrade($element, $target) {
   $target->settings['type'] = 'node';
   rules_upgrade_element_parameter_settings($element, $target, 'title', 'param_title');
@@ -443,105 +466,135 @@ function rules_action_add_node_upgrade($element, $target) {
     drupal_set_message(t('Warning: The node-access check option for the node creation action is not supported any more.'));
   }
 }
+
 function rules_action_load_node_upgrade_map_name($element) {
   return 'entity_fetch';
 }
+
 function rules_action_load_node_upgrade($element, $target) {
   $target->settings['type'] = 'node';
   rules_upgrade_element_parameter_settings($element, $target, 'nid', 'id');
   rules_upgrade_element_parameter_settings($element, $target, 'vid', 'revision_id');
   rules_upgrade_element_variable_settings($element, $target, 'node_loaded', 'entity_fetched');
 }
+
 function rules_action_delete_node_upgrade_map_name($element) {
   return 'entity_delete';
 }
+
 function rules_action_delete_node_upgrade($element, $target) {
   rules_upgrade_element_parameter_settings($element, $target, 'node', 'entity');
 }
+
 function rules_core_node_publish_action_upgrade_map_name($element) {
   return 'node_publish';
 }
+
 function rules_core_node_unpublish_action_upgrade_map_name($element) {
   return 'node_unpublish';
 }
+
 function rules_core_node_make_sticky_action_upgrade_map_name($element) {
   return 'node_make_sticky_action';
 }
+
 function rules_core_node_make_unsticky_action_upgrade_map_name($element) {
   return 'node_make_unsticky_action';
 }
+
 function rules_core_node_promote_action_upgrade_map_name($element) {
   return 'node_promote_action';
 }
+
 function rules_core_node_unpromote_action_upgrade_map_name($element) {
   return 'node_unpromote_action';
 }
 
-
-// Path.module integration.
+/**
+ * Path.module integration.
+ */
 function rules_condition_url_has_alias_upgrade_map_name($element) {
   return 'path_has_alias';
 }
+
 function rules_condition_url_has_alias_upgrade($element, $target) {
   $target->settings['source'] = $element['#settings']['src'];
   $target->settings['alias'] = $element['#settings']['dst'];
 }
+
 function rules_condition_alias_exists_upgrade_map_name($element) {
   return 'path_alias_exists';
 }
+
 function rules_condition_alias_exists_upgrade($element, $target) {
   $target->settings['alias'] = $element['#settings']['dst'];
 }
+
 function rules_action_path_alias_upgrade($element, $target) {
   $target->settings['source'] = $element['#settings']['src'];
   $target->settings['alias'] = $element['#settings']['dst'];
 }
+
 function rules_action_node_path_alias_upgrade($element, $target) {
   $target->settings['alias'] = $element['#settings']['dst'];
 }
 
-// PHP.module integration.
+/**
+ * PHP.module integration.
+ */
 function rules_condition_custom_php_upgrade_map_name($element) {
   return 'php_eval';
 }
+
 function rules_action_custom_php_upgrade_map_name($element) {
   return 'php_eval';
 }
 
-// General Rules integration.
+/**
+ * General Rules integration.
+ */
 function rules_condition_text_compare_upgrade_map_name($element) {
-  // @todo: Support regex.
+  // @todo Support regex.
   return 'data_is';
 }
+
 function rules_condition_text_compare_upgrade($element, $target) {
   rules_upgrade_element_parameter_settings($element, $target, 'text1', 'data');
   rules_upgrade_element_parameter_settings($element, $target, 'text2', 'value');
 }
+
 function rules_condition_number_compare_upgrade_map_name($element) {
   return 'data_is';
 }
+
 function rules_condition_number_compare_upgrade($element, $target) {
   rules_upgrade_element_parameter_settings($element, $target, 'number1', 'data');
   rules_upgrade_element_parameter_settings($element, $target, 'number2', 'value');
 }
+
 function rules_condition_check_boolean_upgrade_map_name($element) {
   return 'data_is';
 }
+
 function rules_condition_check_boolean_upgrade($element, $target) {
   rules_upgrade_element_parameter_settings($element, $target, 'boolean', 'data');
   $target->settings['value'] = TRUE;
 }
+
 function rules_action_invoke_set_upgrade_map_name($element) {
   return 'component_' . $element['#info']['set'];
 }
+
 function rules_action_invoke_set_upgrade($element, $target) {
   foreach ($element['#info']['arguments'] as $name => $info) {
     rules_upgrade_element_parameter_settings($element, $target, $name);
   }
 }
+
 function rules_action_save_variable_upgrade_map_name($element) {
   return isset($element['#info']['new variables']) ? 'variable_add' : 'entity_save';
 }
+
 function rules_action_save_variable_upgrade($element, $target) {
   $type = $element['#info']['arguments']['var_name']['default value'];
   if (isset($element['#info']['new variables'])) {
@@ -554,20 +607,25 @@ function rules_action_save_variable_upgrade($element, $target) {
   }
 }
 
-
-// System.module integration.
+/**
+ * System.module integration.
+ */
 function rules_action_set_breadcrumb_upgrade_map_name($element) {
-  return 'breadcumb_set';
+  return 'breadcrumb_set';
 }
+
 function rules_action_mail_to_user_upgrade_map_name($element) {
   return 'mail';
 }
+
 function rules_action_mail_to_user_upgrade($element, $target) {
   $target->settings['to:select'] = $element['#settings']['#argument map']['user'] . ':mail';
 }
+
 function rules_action_drupal_goto_upgrade_map_name($element) {
   return 'redirect';
 }
+
 function rules_action_drupal_goto_upgrade($element, $target) {
   $settings = $element['#settings'];
   $target->settings['url'] = $settings['path'];
@@ -579,86 +637,109 @@ function rules_action_drupal_goto_upgrade($element, $target) {
 }
 
 function rules_action_watchdog_upgrade_map_name($element) {
-  // @todo: Support action in Rules 2.x!
+  // @todo Support action in Rules 2.x!
   return NULL;
 }
 
-// Taxonomy.module integration.
-// @todo: Finish.
+/**
+ * Taxonomy.module integration.
+ *
+ * @todo Finish.
+ */
 function rules_action_taxonomy_load_term_upgrade_map_name($element) {
   return 'entity_fetch';
 }
+
 function rules_action_taxonomy_add_term_upgrade_map_name($element) {
   return 'entity_create';
 }
+
 function rules_action_taxonomy_delete_term_upgrade_map_name($element) {
   return 'entity_delete';
 }
+
 function rules_action_taxonomy_term_assign_to_content_upgrade_map_name($element) {
-  // @todo : list.
+  // @todo List.
   return NULL;
 }
+
 function rules_action_taxonomy_term_remove_from_content_upgrade_map_name($element) {
-  // @todo : list.
+  // @todo List.
   return NULL;
 }
+
 function rules_action_taxonomy_load_vocab_upgrade_map_name($element) {
   return 'entity_fetch';
 }
+
 function rules_action_taxonomy_add_vocab_upgrade_map_name($element) {
   return 'data_set';
 }
 
-// User.module integration.
+/**
+ * User.module integration.
+ */
 function rules_condition_user_hasrole_upgrade_map_name($element) {
   return 'user_has_role';
 }
+
 function rules_condition_user_hasrole_upgrade($element, $target) {
   rules_upgrade_element_parameter_settings($element, $target, 'user', 'account');
 }
+
 function rules_condition_user_comparison_upgrade_map_name($element) {
   return 'data_is';
 }
+
 function rules_condition_user_comparison_upgrade($element, $target) {
   rules_upgrade_element_parameter_settings($element, $target, 'user1', 'data');
   rules_upgrade_element_parameter_settings($element, $target, 'user2', 'value');
 }
+
 function rules_action_user_addrole_upgrade_map_name($element) {
   return 'user_add_role';
 }
+
 function rules_action_user_addrole_upgrade($element, $target) {
   rules_upgrade_element_parameter_settings($element, $target, 'user', 'account');
 }
+
 function rules_action_user_removerole_upgrade_map_name($element) {
   return 'user_remove_role';
 }
+
 function rules_action_user_removerole_upgrade($element, $target) {
   rules_upgrade_element_parameter_settings($element, $target, 'user', 'account');
 }
+
 function rules_action_load_user_upgrade_map_name($element) {
   if (!empty($element['#settings']['username'])) {
     drupal_set_message(t('Warning: Directly upgrading the load user by name action is not supported.'));
   }
   return 'entity_fetch';
 }
+
 function rules_action_load_user_upgrade($element, $target) {
   $target->settings['type'] = 'user';
   rules_upgrade_element_parameter_settings($element, $target, 'userid', 'id');
   rules_upgrade_element_variable_settings($element, $target, 'user_loaded', 'entity_fetched');
 }
+
 function rules_action_user_create_upgrade_map_name($element) {
   return 'entity_create';
 }
+
 function rules_action_user_create_upgrade($element, $target) {
   $target->settings['type'] = 'user';
   rules_upgrade_element_parameter_settings($element, $target, 'username', 'param_name');
   rules_upgrade_element_parameter_settings($element, $target, 'email', 'param_mail');
   rules_upgrade_element_variable_settings($element, $target, 'user_added', 'entity_created');
-
 }
+
 function rules_core_user_block_user_action_upgrade_map_name($element) {
   return 'user_block';
 }
+
 function rules_core_user_block_user_action_upgrade($element, $target) {
   $target->settings['account:select'] = $element['#settings']['#argument map']['user'];
 }

+ 41 - 6
sites/all/modules/rules/modules/comment.rules.inc

@@ -1,20 +1,23 @@
 <?php
 
 /**
- * @file rules integration for the comment module
+ * @file
+ * Rules integration for the comment module.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
 /**
- * Implementation of hook_rules_event_info().
+ * Implements hook_rules_event_info().
  */
 function rules_comment_event_info() {
   $defaults = array(
     'group' => t('comment'),
     'module' => 'comment',
     'access callback' => 'rules_comment_integration_access',
+    'class' => 'RulesCommentEventHandler',
   );
   return array(
     'comment_insert' => $defaults + array(
@@ -26,15 +29,30 @@ function rules_comment_event_info() {
     'comment_update' => $defaults + array(
       'label' => t('After updating an existing comment'),
       'variables' => array(
-        'comment' => array('type' => 'comment', 'label' => t('updated comment')),
-        'comment_unchanged' => array('type' => 'comment', 'label' => t('unchanged comment'), 'handler' => 'rules_events_entity_unchanged'),
+        'comment' => array(
+          'type' => 'comment',
+          'label' => t('updated comment'),
+        ),
+        'comment_unchanged' => array(
+          'type' => 'comment',
+          'label' => t('unchanged comment'),
+          'handler' => 'rules_events_entity_unchanged',
+        ),
       ),
     ),
     'comment_presave' => $defaults + array(
       'label' => t('Before saving a comment'),
       'variables' => array(
-        'comment' => array('type' => 'comment', 'label' => t('saved comment'), 'skip save' => TRUE),
-        'comment_unchanged' => array('type' => 'comment', 'label' => t('unchanged comment'), 'handler' => 'rules_events_entity_unchanged'),
+        'comment' => array(
+          'type' => 'comment',
+          'label' => t('saved comment'),
+          'skip save' => TRUE,
+        ),
+        'comment_unchanged' => array(
+          'type' => 'comment',
+          'label' => t('unchanged comment'),
+          'handler' => 'rules_events_entity_unchanged',
+        ),
       ),
     ),
     'comment_view' => $defaults + array(
@@ -62,6 +80,23 @@ function rules_comment_integration_access($type, $name) {
   }
 }
 
+/**
+ * Event handler support comment bundle event settings.
+ */
+class RulesCommentEventHandler extends RulesEventHandlerEntityBundle {
+
+  /**
+   * Returns the label to use for the bundle property.
+   *
+   * @return string
+   *   Returns the label to use for the bundle property.
+   */
+  protected function getBundlePropertyLabel() {
+    return t('type');
+  }
+
+}
+
 /**
  * @}
  */

+ 76 - 10
sites/all/modules/rules/modules/data.eval.inc

@@ -5,6 +5,7 @@
  * Contains rules integration for the data module needed during evaluation.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
@@ -43,7 +44,7 @@ function rules_action_data_set_info_alter(&$element_info, $element) {
   if ($wrapper = $element->applyDataSelector($element->settings['data:select'])) {
     $info = $wrapper->info();
     $element_info['parameter']['value']['type'] = $wrapper->type();
-    $element_info['parameter']['value']['options list']  = !empty($info['options list']) ? 'rules_data_selector_options_list' : FALSE;
+    $element_info['parameter']['value']['options list'] = !empty($info['options list']) ? 'rules_data_selector_options_list' : FALSE;
   }
 }
 
@@ -62,15 +63,26 @@ function rules_action_data_calc($input1, $op, $input2, $settings, $state, $eleme
     case '+':
       $result = $input1 + $input2;
       break;
+
     case '-':
       $result = $input1 - $input2;
       break;
+
     case '*':
       $result = $input1 * $input2;
       break;
+
     case '/':
       $result = $input1 / $input2;
       break;
+
+    case 'min':
+      $result = min($input1, $input2);
+      break;
+
+    case 'max':
+      $result = max($input1, $input2);
+      break;
   }
   if (isset($result)) {
     // Ensure results are valid integer values if necessary.
@@ -136,7 +148,7 @@ function rules_data_list_info_alter(&$element_info, RulesAbstractPlugin $element
     if ($type = entity_property_list_extract_type($wrapper->type())) {
       $info = $wrapper->info();
       $element_info['parameter']['item']['type'] = $type;
-      $element_info['parameter']['item']['options list']  = !empty($info['options list']) ? 'rules_data_selector_options_list' : FALSE;
+      $element_info['parameter']['item']['options list'] = !empty($info['options list']) ? 'rules_data_selector_options_list' : FALSE;
     }
   }
 }
@@ -191,9 +203,11 @@ function rules_action_data_convert($arguments, RulesPlugin $element, $state) {
       case 'up':
         $arguments['value'] = ceil($arguments['value']);
         break;
+
       case 'down':
         $arguments['value'] = floor($arguments['value']);
         break;
+
       default:
       case 'round':
         $arguments['value'] = round($arguments['value']);
@@ -205,12 +219,18 @@ function rules_action_data_convert($arguments, RulesPlugin $element, $state) {
     case 'decimal':
       $result = floatval($arguments['value']);
       break;
+
     case 'integer':
       $result = intval($arguments['value']);
       break;
+
     case 'text':
       $result = strval($arguments['value']);
       break;
+
+    case 'token':
+      $result = strval($arguments['value']);
+      break;
   }
 
   return array('conversion_result' => $result);
@@ -224,8 +244,19 @@ function rules_action_data_convert_info_alter(&$element_info, RulesAbstractPlugi
   if (isset($element->settings['type']) && $type = $element->settings['type']) {
     $element_info['provides']['conversion_result']['type'] = $type;
 
-    if ($type != 'integer') {
-      // Only support the rounding behavior option for integers.
+    // Only support the rounding behavior option for integers.
+    if ($type == 'integer') {
+      $element_info['parameter']['rounding_behavior'] = array(
+        'type' => 'token',
+        'label' => t('Rounding behavior'),
+        'description' => t('The rounding behavior the conversion should use.'),
+        'options list' => 'rules_action_data_convert_rounding_behavior_options',
+        'restriction' => 'input',
+        'default value' => 'round',
+        'optional' => TRUE,
+      );
+    }
+    else {
       unset($element_info['parameter']['rounding_behavior']);
     }
 
@@ -234,12 +265,18 @@ function rules_action_data_convert_info_alter(&$element_info, RulesAbstractPlugi
       case 'integer':
         $sources = array('decimal', 'text', 'token', 'uri', 'date', 'duration', 'boolean');
         break;
+
       case 'decimal':
         $sources = array('integer', 'text', 'token', 'uri', 'date', 'duration', 'boolean');
         break;
+
       case 'text':
         $sources = array('integer', 'decimal', 'token', 'uri', 'date', 'duration', 'boolean');
         break;
+
+      case 'token':
+        $sources = array('integer', 'decimal', 'text', 'uri', 'date', 'duration', 'boolean');
+        break;
     }
     $element_info['parameter']['value']['type'] = $sources;
   }
@@ -284,10 +321,12 @@ function rules_action_data_create_info_alter(&$element_info, RulesAbstractPlugin
     if (isset($type_info['property info'])) {
       // Add the data type's properties as parameters.
       foreach ($type_info['property info'] as $property => $property_info) {
-        // Prefix parameter names to avoid name clashes with existing parameters.
-        $element_info['parameter']['param_' . $property] = array_intersect_key($property_info, array_flip(array('type', 'label')));
+        // Prefix parameter names to avoid name clashes with
+        // existing parameters.
+        $element_info['parameter']['param_' . $property] = array_intersect_key($property_info, array_flip(array('type', 'label', 'allow null')));
         if (empty($property_info['required'])) {
           $element_info['parameter']['param_' . $property]['optional'] = TRUE;
+          $element_info['parameter']['param_' . $property]['allow null'] = TRUE;
         }
       }
     }
@@ -316,13 +355,17 @@ function rules_condition_data_is($data, $op, $value) {
         return (isset($data) && isset($value)) || (!isset($data) && !isset($value));
       }
       return $data == $value;
+
     case '<':
       return $data < $value;
+
     case '>':
       return $data > $value;
-      // Note: This is deprecated by the text comparison condition and IN below.
+
+    // Note: This is deprecated by the text comparison condition and IN below.
     case 'contains':
       return is_string($data) && strpos($data, $value) !== FALSE || is_array($data) && in_array($value, $data);
+
     case 'IN':
       return is_array($value) && in_array($data, $value);
   }
@@ -339,7 +382,7 @@ function rules_condition_data_is_info_alter(&$element_info, RulesAbstractPlugin
   if ($wrapper = $element->applyDataSelector($element->settings['data:select'])) {
     $info = $wrapper->info();
     $element_info['parameter']['value']['type'] = $element->settings['op'] == 'IN' ? 'list<' . $wrapper->type() . '>' : $wrapper->type();
-    $element_info['parameter']['value']['options list']  = !empty($info['options list']) ? 'rules_data_selector_options_list' : FALSE;
+    $element_info['parameter']['value']['options list'] = !empty($info['options list']) ? 'rules_data_selector_options_list' : FALSE;
   }
 }
 
@@ -360,6 +403,22 @@ function rules_condition_data_list_contains($list, $item, $settings, $state) {
   return in_array($item, $list);
 }
 
+/**
+ * Condition: List count comparison.
+ */
+function rules_condition_data_list_count_is($list, $op = '==', $value) {
+  switch ($op) {
+    case '==':
+      return count($list) == $value;
+
+    case '<':
+      return count($list) < $value;
+
+    case '>':
+      return count($list) > $value;
+  }
+}
+
 /**
  * Condition: Data value is empty.
  */
@@ -388,11 +447,18 @@ function rules_data_text_comparison($text, $text2, $op = 'contains') {
   switch ($op) {
     case 'contains':
       return strpos($text, $text2) !== FALSE;
+
     case 'starts':
       return strpos($text, $text2) === 0;
+
     case 'ends':
-     return strrpos($text, $text2) === (strlen($text) - strlen($text2));
+      return strrpos($text, $text2) === (strlen($text) - strlen($text2));
+
     case 'regex':
-     return (bool) preg_match('/'. str_replace('/', '\\/', $text2) .'/', $text);
+      return (bool) preg_match('/' . str_replace('/', '\\/', $text2) . '/', $text);
   }
 }
+
+/**
+ * @}
+ */

+ 68 - 20
sites/all/modules/rules/modules/data.rules.inc

@@ -1,14 +1,30 @@
 <?php
 
 /**
- * @file General data related rules integration
+ * @file
+ * General data related rules integration.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
+/**
+ * Implements hook_rules_category_info() on behalf of the pseudo data module.
+ */
+function rules_data_category_info() {
+  return array(
+    'rules_data' => array(
+      'label' => t('Data'),
+      'equals group' => t('Data'),
+      'weight' => -50,
+    ),
+  );
+}
+
 /**
  * Implements hook_rules_file_info() on behalf of the pseudo data module.
+ *
  * @see rules_core_modules()
  */
 function rules_data_file_info() {
@@ -17,6 +33,7 @@ function rules_data_file_info() {
 
 /**
  * Implements hook_rules_action_info() on behalf of the pseudo data module.
+ *
  * @see rules_core_modules()
  */
 function rules_data_action_info() {
@@ -147,7 +164,7 @@ function rules_data_action_info() {
         'type' => 'unknown',
         'label' => t('Value'),
         'optional' => TRUE,
-        'description' => t('Optionally, specify the initial value of the variable.')
+        'description' => t('Optionally, specify the initial value of the variable.'),
       ),
     ),
     'provides' => array(
@@ -203,20 +220,10 @@ function rules_data_action_info() {
         'restriction' => 'input',
       ),
       'value' => array(
-        'type' => array('decimal', 'integer', 'text'),
+        'type' => array('decimal', 'integer', 'text', 'token'),
         'label' => t('Value to convert'),
         'default mode' => 'selector',
       ),
-      // For to-integer conversion only.
-      'rounding_behavior' => array(
-        'type' => 'token',
-        'label' => t('Rounding behavior'),
-        'description' => t('The rounding behavior the conversion should use.'),
-        'options list' => 'rules_action_data_convert_rounding_behavior_options',
-        'restriction' => 'input',
-        'default value' => 'round',
-        'optional' => TRUE,
-      ),
     ),
     'provides' => array(
       'conversion_result' => array(
@@ -242,6 +249,7 @@ function rules_action_data_convert_types_options(RulesPlugin $element, $param_na
     'decimal' => t('Decimal'),
     'integer' => t('Integer'),
     'text' => t('Text'),
+    'token' => t('Token'),
   );
 }
 
@@ -298,7 +306,7 @@ function rules_action_data_set_form_alter(&$form, &$form_state, $options, RulesA
   else {
     // Change the data parameter to be not editable.
     $form['parameter']['data']['settings']['#access'] = FALSE;
-    // TODO: improve display
+    // @todo Improve display.
     $form['parameter']['data']['info'] = array(
       '#prefix' => '<p>',
       '#markup' => t('<strong>Selected data:</strong> %selector', array('%selector' => $element->settings['data:select'])),
@@ -324,8 +332,7 @@ function rules_action_data_calc_form_alter(&$form, &$form_state, $options, Rules
 }
 
 /**
- * Custom validate callback for entity create, add variable and data create
- * action.
+ * Validate callback for entity create, add variable and data create actions.
  */
 function rules_action_create_type_validate($element) {
   if (!isset($element->settings['type'])) {
@@ -365,7 +372,6 @@ function rules_data_list_form_alter(&$form, &$form_state, $options, RulesAbstrac
   }
 }
 
-
 /**
  * Form alter callback for actions relying on the entity type or the data type.
  */
@@ -396,7 +402,7 @@ function rules_action_type_form_alter(&$form, &$form_state, $options, RulesAbstr
     unset($form['submit']);
     unset($form['provides']);
     // Disable #ajax for the first step as it has troubles with lazy-loaded JS.
-    // @todo: Re-enable once JS lazy-loading is fixed in core.
+    // @todo Re-enable once JS lazy-loading is fixed in core.
     unset($form['parameter']['type']['settings']['type']['#ajax']);
     unset($form['reload']['#ajax']);
   }
@@ -442,6 +448,8 @@ function rules_action_data_calc_operator_options(RulesPlugin $element, $param_na
     '-' => '( - )',
     '*' => '( * )',
     '/' => '( / )',
+    'min' => 'min',
+    'max' => 'max',
   );
   // Only show +/- in case a date has been selected.
   if (($info = $element->getArgumentInfo('input_1')) && $info['type'] == 'date') {
@@ -473,6 +481,7 @@ function rules_data_action_data_create_options() {
 
 /**
  * Implements hook_rules_condition_info() on behalf of the pseudo data module.
+ *
  * @see rules_core_modules()
  */
 function rules_data_condition_info() {
@@ -540,6 +549,32 @@ function rules_data_condition_info() {
         'form_alter' => 'rules_data_list_form_alter',
       ),
     ),
+    'list_count_is' => array(
+      'label' => t('List count comparison'),
+      'parameter' => array(
+        'list' => array(
+          'type' => 'list',
+          'label' => t('List to check'),
+          'description' => t('A multi value data element to have its count compared, specified by using a data selector, eg node:author:roles.'),
+        ),
+        'op' => array(
+          'type' => 'text',
+          'label' => t('Operator'),
+          'description' => t('The comparison operator.'),
+          'optional' => TRUE,
+          'default value' => '==',
+          'options list' => 'rules_condition_data_list_count_is_operator_options',
+          'restriction' => 'input',
+        ),
+        'value' => array(
+          'type' => 'integer',
+          'label' => t('Count'),
+          'description' => t('The count to compare the data count with.'),
+        ),
+      ),
+      'group' => t('Data'),
+      'base' => 'rules_condition_data_list_count_is',
+    ),
     'text_matches'  => array(
       'label' => t('Text comparison'),
       'parameter' => array(
@@ -569,11 +604,13 @@ function rules_data_condition_info() {
 }
 
 /**
+ * Asserts the bundle of entities, if it's compared.
+ *
  * If the bundle is compared, add the metadata assertion so other elements
  * can make use of properties specific to the bundle.
  */
 function rules_condition_data_is_assertions($element) {
-  // Assert the bundle of entities, if its compared.
+  // Assert the bundle of entities, if it's compared.
   if ($wrapper = $element->applyDataSelector($element->settings['data:select'])) {
     $info = $wrapper->info();
     if (isset($info['parent']) && $info['parent'] instanceof EntityDrupalWrapper) {
@@ -613,7 +650,7 @@ function rules_condition_data_is_form_alter(&$form, &$form_state, $options, Rule
   else {
     // Change the data parameter to be not editable.
     $form['parameter']['data']['settings']['#access'] = FALSE;
-    // TODO: improve display
+    // @todo Improve display.
     $form['parameter']['data']['info'] = array(
       '#prefix' => '<p>',
       '#markup' => t('<strong>Selected data:</strong> %selector', array('%selector' => $element->settings['data:select'])),
@@ -704,6 +741,17 @@ function rules_data_selector_options_list(RulesAbstractPlugin $element) {
   }
 }
 
+/**
+ * Options list callback for condition list_count_is.
+ */
+function rules_condition_data_list_count_is_operator_options() {
+  return array(
+    '==' => t('equals'),
+    '<' => t('is lower than'),
+    '>' => t('is greater than'),
+  );
+}
+
 /**
  * @}
  */

+ 17 - 5
sites/all/modules/rules/modules/entity.eval.inc

@@ -5,6 +5,7 @@
  * Contains rules integration for entities needed during evaluation.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
@@ -65,10 +66,16 @@ function rules_action_entity_query_info_alter(&$element_info, RulesAbstractPlugi
     $element_info['parameter']['property']['options list'] = 'rules_action_entity_query_property_options_list';
 
     if ($element->settings['property']) {
-      $wrapper = entity_metadata_wrapper($element->settings['type']);
+      $wrapper = rules_get_entity_metadata_wrapper_all_properties($element);
       if (isset($wrapper->{$element->settings['property']}) && $property = $wrapper->{$element->settings['property']}) {
-        $element_info['parameter']['value']['type'] = $property->type();
-        $element_info['parameter']['value']['options list']  = $property->optionsList() ? 'rules_action_entity_query_value_options_list' : FALSE;
+        $property_type = $property->type();
+        // If the cardinality of the property > 1, i.e. of type 'list<{type}>',
+        // we will also accept a parameter of type {type}.
+        if (substr($property_type, 0, strlen('list<')) === 'list<' && substr($property_type, -strlen('>')) === '>') {
+          $property_type = array($property_type, substr($property_type, strlen('list<'), strlen($property_type) - strlen('list<>')));
+        }
+        $element_info['parameter']['value']['type'] = $property_type;
+        $element_info['parameter']['value']['options list'] = $property->optionsList() ? 'rules_action_entity_query_value_options_list' : FALSE;
       }
     }
   }
@@ -106,9 +113,10 @@ function rules_action_entity_create_info_alter(&$element_info, RulesAbstractPlug
       $info = $child->info();
       if (!empty($info['required'])) {
         $info += array('type' => 'text');
-        // Prefix parameter names to avoid name clashes with existing parameters.
+        // Prefix parameter names to avoid name clashes
+        // with existing parameters.
         $element_info['parameter']['param_' . $name] = array_intersect_key($info, array_flip(array('type', 'label', 'description')));
-        $element_info['parameter']['param_' . $name]['options list']  = $child->optionsList() ? 'rules_action_entity_parameter_options_list' : FALSE;
+        $element_info['parameter']['param_' . $name]['options list'] = $child->optionsList() ? 'rules_action_entity_parameter_options_list' : FALSE;
       }
     }
     $element_info['provides']['entity_created']['type'] = $element->settings['type'];
@@ -172,3 +180,7 @@ function rules_condition_entity_field_access(EntityDrupalWrapper $wrapper, $fiel
   $field = field_info_field($field_name);
   return !empty($field) && field_access($op, $field, $wrapper->type(), $wrapper->value(), $account = NULL);
 }
+
+/**
+ * @}
+ */

+ 32 - 7
sites/all/modules/rules/modules/entity.rules.inc

@@ -1,22 +1,39 @@
 <?php
 
 /**
- * @file General entity related rules integration
+ * @file
+ * General entity related rules integration.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
 /**
  * Implements hook_rules_file_info() on behalf of the entity module.
+ *
  * @see rules_core_modules()
  */
 function rules_entity_file_info() {
   return array('modules/entity.eval');
 }
 
+/**
+ * Implements hook_rules_category_info() on behalf of the pseudo entity module.
+ */
+function rules_entity_category_info() {
+  return array(
+    'rules_entity' => array(
+      'label' => t('Entities'),
+      'equals group' => t('Entities'),
+      'weight' => -50,
+    ),
+  );
+}
+
 /**
  * Implements hook_rules_action_info() on behalf of the entity module.
+ *
  * @see rules_core_modules()
  */
 function rules_entity_action_info() {
@@ -227,7 +244,7 @@ function rules_action_entity_query_value_options_list(RulesAbstractPlugin $eleme
   // Get the possible values for the selected property.
   $element->settings += array('type' => NULL, 'property' => NULL);
   if ($element->settings['type'] && $element->settings['property']) {
-    $wrapper = entity_metadata_wrapper($element->settings['type']);
+    $wrapper = rules_get_entity_metadata_wrapper_all_properties($element);
 
     if (isset($wrapper->{$element->settings['property']}) && $property = $wrapper->{$element->settings['property']}) {
       return $property->optionsList('view');
@@ -309,6 +326,7 @@ function rules_entity_action_access($type, $name) {
 
 /**
  * Implements hook_rules_condition_info() on behalf of the entity module.
+ *
  * @see rules_core_modules()
  */
 function rules_entity_condition_info() {
@@ -442,11 +460,17 @@ function rules_condition_entity_is_new_help() {
  * Returns options for choosing a field for the selected entity.
  */
 function rules_condition_entity_has_field_options(RulesAbstractPlugin $element) {
-  $options = array();
-  foreach (field_info_fields() as $field_name => $field) {
-    $options[$field_name] = $field_name;
+  // The field_info_field_map() function was introduced in Drupal 7.22. See
+  // https://www.drupal.org/node/1915646.
+  if (function_exists('field_info_field_map')) {
+    $fields = field_info_field_map();
   }
-  return $options;
+  else {
+    $fields = field_info_fields();
+  }
+  $field_list = drupal_map_assoc(array_keys($fields));
+  ksort($field_list);
+  return $field_list;
 }
 
 /**
@@ -546,12 +570,13 @@ function rules_condition_entity_is_of_bundle_form_alter(&$form, &$form_state, $o
       $form['reload']['#limit_validation_errors'] = array(array('parameter', 'entity'));
       unset($form['parameter']['type']);
       unset($form['reload']['#attributes']['class']);
-      // NO break;
+      // NO break.
     case 2:
       $form['negate']['#access'] = FALSE;
       unset($form['parameter']['bundle']);
       unset($form['submit']);
       break;
+
     case 3:
       if (($info = $element->getArgumentInfo('entity')) && $info['type'] != 'entity') {
         // Hide the entity type parameter if not needed.

+ 115 - 58
sites/all/modules/rules/modules/events.inc

@@ -1,19 +1,24 @@
 <?php
 
 /**
- * @file Invokes events on behalf core modules. Usually this should be
- *   directly in the module providing rules integration instead.
+ * @file
+ * Invokes events on behalf core modules.
+ *
+ * For non-core modules, the code to invoke events should be found in the
+ * module providing rules integration.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
-
 /**
- * Gets an unchanged entity that doesn't contain any recent changes. This
- * handler assumes the name of the variable for the changed entity is the same
- * as for the unchanged entity but without the trailing "_unchanged"; e.g., for
- * the "node_unchanged" variable the handler assumes there is a "node" variable.
+ * Gets an unchanged entity that doesn't contain any recent changes.
+ *
+ * This handler assumes the name of the variable for the changed entity is the
+ * same as for the unchanged entity but without the trailing "_unchanged"; e.g.,
+ * for the "node_unchanged" variable the handler assumes there is a "node"
+ * variable.
  */
 function rules_events_entity_unchanged($arguments, $name, $info) {
   // Cut of the trailing _unchanged.
@@ -24,25 +29,31 @@ function rules_events_entity_unchanged($arguments, $name, $info) {
   }
 }
 
-/**
+/*
  * Generic entity events, used for core-entities for which we provide Rules
- * integration only.
- * We are implementing the generic-entity hooks instead of the entity-type
- * specific hooks to ensure we come last. See http://drupal.org/node/1211946
- * for details.
+ * integration only. We are implementing the generic-entity hooks instead of
+ * the entity-type specific hooks to ensure we come last.
+ * See https://www.drupal.org/node/1211946 for details.
  */
 
 /**
  * Implements hook_entity_view().
  */
 function rules_entity_view($entity, $type, $view_mode, $langcode) {
-  $entity_types = array(
-    'comment' => TRUE,
-    'node' => TRUE,
-    'user' => TRUE,
-  );
-  if (isset($entity_types[$type])) {
-    rules_invoke_event($type . '_view', $entity, $view_mode);
+  switch ($type) {
+    case 'comment':
+      rules_invoke_event('comment_view--' . $entity->node_type, $entity, $view_mode);
+      rules_invoke_event('comment_view', $entity, $view_mode);
+      break;
+
+    case 'node':
+      rules_invoke_event('node_view--' . $entity->type, $entity, $view_mode);
+      rules_invoke_event('node_view', $entity, $view_mode);
+      break;
+
+    case 'user':
+      rules_invoke_event('user_view', $entity, $view_mode);
+      break;
   }
 }
 
@@ -50,15 +61,26 @@ function rules_entity_view($entity, $type, $view_mode, $langcode) {
  * Implements hook_entity_presave().
  */
 function rules_entity_presave($entity, $type) {
-  $entity_types = array(
-    'comment' => TRUE,
-    'node' => TRUE,
-    'taxonomy_term' => TRUE,
-    'taxonomy_vocabulary' => TRUE,
-    'user' => TRUE,
-  );
-  if (isset($entity_types[$type])) {
-    rules_invoke_event($type . '_presave', $entity);
+  switch ($type) {
+    case 'comment':
+      rules_invoke_event('comment_presave--' . $entity->node_type, $entity);
+      rules_invoke_event('comment_presave', $entity);
+      break;
+
+    case 'node':
+      rules_invoke_event('node_presave--' . $entity->type, $entity);
+      rules_invoke_event('node_presave', $entity);
+      break;
+
+    case 'taxonomy_term':
+      rules_invoke_event('taxonomy_term_presave--' . $entity->vocabulary_machine_name, $entity);
+      rules_invoke_event('taxonomy_term_presave', $entity);
+      break;
+
+    case 'taxonomy_vocabulary':
+    case 'user':
+      rules_invoke_event($type . '_presave', $entity);
+      break;
   }
 }
 
@@ -66,15 +88,26 @@ function rules_entity_presave($entity, $type) {
  * Implements hook_entity_update().
  */
 function rules_entity_update($entity, $type) {
-  $entity_types = array(
-    'comment' => TRUE,
-    'node' => TRUE,
-    'taxonomy_term' => TRUE,
-    'taxonomy_vocabulary' => TRUE,
-    'user' => TRUE,
-  );
-  if (isset($entity_types[$type])) {
-    rules_invoke_event($type . '_update', $entity);
+  switch ($type) {
+    case 'comment':
+      rules_invoke_event('comment_update--' . $entity->node_type, $entity);
+      rules_invoke_event('comment_update', $entity);
+      break;
+
+    case 'node':
+      rules_invoke_event('node_update--' . $entity->type, $entity);
+      rules_invoke_event('node_update', $entity);
+      break;
+
+    case 'taxonomy_term':
+      rules_invoke_event('taxonomy_term_update--' . $entity->vocabulary_machine_name, $entity);
+      rules_invoke_event('taxonomy_term_update', $entity);
+      break;
+
+    case 'taxonomy_vocabulary':
+    case 'user':
+      rules_invoke_event($type . '_update', $entity);
+      break;
   }
 }
 
@@ -82,15 +115,26 @@ function rules_entity_update($entity, $type) {
  * Implements hook_entity_insert().
  */
 function rules_entity_insert($entity, $type) {
-  $entity_types = array(
-    'comment' => TRUE,
-    'node' => TRUE,
-    'taxonomy_term' => TRUE,
-    'taxonomy_vocabulary' => TRUE,
-    'user' => TRUE,
-  );
-  if (isset($entity_types[$type])) {
-    rules_invoke_event($type . '_insert', $entity);
+  switch ($type) {
+    case 'comment':
+      rules_invoke_event('comment_insert--' . $entity->node_type, $entity);
+      rules_invoke_event('comment_insert', $entity);
+      break;
+
+    case 'node':
+      rules_invoke_event('node_insert--' . $entity->type, $entity);
+      rules_invoke_event('node_insert', $entity);
+      break;
+
+    case 'taxonomy_term':
+      rules_invoke_event('taxonomy_term_insert--' . $entity->vocabulary_machine_name, $entity);
+      rules_invoke_event('taxonomy_term_insert', $entity);
+      break;
+
+    case 'taxonomy_vocabulary':
+    case 'user':
+      rules_invoke_event($type . '_insert', $entity);
+      break;
   }
 }
 
@@ -98,15 +142,26 @@ function rules_entity_insert($entity, $type) {
  * Implements hook_entity_delete().
  */
 function rules_entity_delete($entity, $type) {
-  $entity_types = array(
-    'comment' => TRUE,
-    'node' => TRUE,
-    'taxonomy_term' => TRUE,
-    'taxonomy_vocabulary' => TRUE,
-    'user' => TRUE,
-  );
-  if (isset($entity_types[$type])) {
-    rules_invoke_event($type . '_delete', $entity);
+  switch ($type) {
+    case 'comment':
+      rules_invoke_event('comment_delete--' . $entity->node_type, $entity);
+      rules_invoke_event('comment_delete', $entity);
+      break;
+
+    case 'node':
+      rules_invoke_event('node_delete--' . $entity->type, $entity);
+      rules_invoke_event('node_delete', $entity);
+      break;
+
+    case 'taxonomy_term':
+      rules_invoke_event('taxonomy_term_delete--' . $entity->vocabulary_machine_name, $entity);
+      rules_invoke_event('taxonomy_term_delete', $entity);
+      break;
+
+    case 'taxonomy_vocabulary':
+    case 'user':
+      rules_invoke_event($type . '_delete', $entity);
+      break;
   }
 }
 
@@ -124,8 +179,10 @@ function rules_user_logout($account) {
   rules_invoke_event('user_logout', $account);
 }
 
-/**
- * System events. Note that rules_init() is the main module file is used to
+/*
+ * System events.
+ *
+ * Note that rules_init() is the main module file is used to
  * invoke the init event.
  */
 
@@ -147,7 +204,7 @@ function rules_watchdog($log_entry) {
  * Getter callback for the log entry message property.
  */
 function rules_system_log_get_message($log_entry) {
-  return t($log_entry['message'], (array)$log_entry['variables']);
+  return t($log_entry['message'], (array) $log_entry['variables']);
 }
 
 /**

+ 114 - 12
sites/all/modules/rules/modules/node.eval.inc

@@ -5,35 +5,137 @@
  * Contains rules integration for the node module needed during evaluation.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
 /**
- * Condition: Check for selected content types
+ * Base class providing node condition defaults.
+ */
+abstract class RulesNodeConditionBase extends RulesConditionHandlerBase {
+
+  public static function defaults() {
+    return array(
+      'parameter' => array(
+        'node' => array('type' => 'node', 'label' => t('Content')),
+      ),
+      'category' => 'node',
+      'access callback' => 'rules_node_integration_access',
+    );
+  }
+
+}
+
+/**
+ * Condition: Check for selected content types.
  */
-function rules_condition_node_is_of_type($node, $types) {
-  return in_array($node->type, $types);
+class RulesNodeConditionType extends RulesNodeConditionBase {
+
+  /**
+   * Defines the condition.
+   */
+  public static function getInfo() {
+    $info = self::defaults() + array(
+      'name' => 'node_is_of_type',
+      'label' => t('Content is of type'),
+      'help' => t('Evaluates to TRUE if the given content is of one of the selected content types.'),
+    );
+    $info['parameter']['type'] = array(
+      'type' => 'list<text>',
+      'label' => t('Content types'),
+      'options list' => 'node_type_get_names',
+      'description' => t('The content type(s) to check for.'),
+      'restriction' => 'input',
+    );
+    return $info;
+  }
+
+  /**
+   * Executes the condition.
+   */
+  public function execute($node, $types) {
+    return in_array($node->type, $types);
+  }
+
+  /**
+   * Provides the content type of a node as asserted metadata.
+   */
+  public function assertions() {
+    return array('node' => array('bundle' => $this->element->settings['type']));
+  }
+
 }
 
 /**
- * Condition: Check if the node is published
+ * Condition: Check if the node is published.
  */
-function rules_condition_node_is_published($node) {
-  return $node->status == 1;
+class RulesNodeConditionPublished extends RulesNodeConditionBase {
+
+  /**
+   * Defines the condition.
+   */
+  public static function getInfo() {
+    return self::defaults() + array(
+      'name' => 'node_is_published',
+      'label' => t('Content is published'),
+    );
+  }
+
+  /**
+   * Executes the condition.
+   */
+  public function execute($node) {
+    return $node->status == 1;
+  }
+
 }
 
 /**
- * Condition: Check if the node is sticky
+ * Condition: Check if the node is sticky.
  */
-function rules_condition_node_is_sticky($node) {
-  return $node->sticky == 1;
+class RulesNodeConditionSticky extends RulesNodeConditionBase {
+
+  /**
+   * Defines the condition.
+   */
+  public static function getInfo() {
+    return self::defaults() + array(
+      'name' => 'node_is_sticky',
+      'label' => t('Content is sticky'),
+    );
+  }
+
+  /**
+   * Executes the condition.
+   */
+  public function execute($node) {
+    return $node->sticky == 1;
+  }
+
 }
 
 /**
- * Condition: Check if the node is promoted to the frontpage
+ * Condition: Check if the node is promoted to the frontpage.
  */
-function rules_condition_node_is_promoted($node) {
-  return $node->promote == 1;
+class RulesNodeConditionPromoted extends RulesNodeConditionBase {
+
+  /**
+   * Defines the condition.
+   */
+  public static function getInfo() {
+    return self::defaults() + array(
+      'name' => 'node_is_promoted',
+      'label' => t('Content is promoted to frontpage'),
+    );
+  }
+
+  /**
+   * Executes the condition.
+   */
+  public function execute($node) {
+    return $node->promote == 1;
+  }
+
 }
 
 /**

+ 43 - 53
sites/all/modules/rules/modules/node.rules.inc

@@ -1,12 +1,26 @@
 <?php
 
 /**
- * @file rules integration for the node module
+ * @file
+ * Rules integration for the node module.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
+/**
+ * Implements hook_rules_category_info() on behalf of the node module.
+ */
+function rules_node_category_info() {
+  return array(
+    'node' => array(
+      'label' => t('Node'),
+      'equals group' => t('Node'),
+    ),
+  );
+}
+
 /**
  * Implements hook_rules_file_info() on behalf of the node module.
  */
@@ -21,25 +35,28 @@ function rules_node_event_info() {
   $items = array(
     'node_insert' => array(
       'label' => t('After saving new content'),
-      'group' => t('Node'),
+      'category' => 'node',
       'variables' => rules_events_node_variables(t('created content')),
       'access callback' => 'rules_node_integration_access',
+      'class' => 'RulesNodeEventHandler',
     ),
     'node_update' => array(
       'label' => t('After updating existing content'),
-      'group' => t('Node'),
+      'category' => 'node',
       'variables' => rules_events_node_variables(t('updated content'), TRUE),
       'access callback' => 'rules_node_integration_access',
+      'class' => 'RulesNodeEventHandler',
     ),
     'node_presave' => array(
       'label' => t('Before saving content'),
-      'group' => t('Node'),
+      'category' => 'node',
       'variables' => rules_events_node_variables(t('saved content'), TRUE),
       'access callback' => 'rules_node_integration_access',
+      'class' => 'RulesNodeEventHandler',
     ),
     'node_view' => array(
       'label' => t('Content is viewed'),
-      'group' => t('Node'),
+      'category' => 'node',
       'help' => t("Note that if drupal's page cache is enabled, this event won't be generated for pages served from cache."),
       'variables' => rules_events_node_variables(t('viewed content')) + array(
         'view_mode' => array(
@@ -51,12 +68,14 @@ function rules_node_event_info() {
         ),
       ),
       'access callback' => 'rules_node_integration_access',
+      'class' => 'RulesNodeEventHandler',
     ),
     'node_delete' => array(
       'label' => t('After deleting content'),
-      'group' => t('Node'),
+      'category' => 'node',
       'variables' => rules_events_node_variables(t('deleted content')),
       'access callback' => 'rules_node_integration_access',
+      'class' => 'RulesNodeEventHandler',
     ),
   );
   // Specify that on presave the node is saved anyway.
@@ -65,7 +84,7 @@ function rules_node_event_info() {
 }
 
 /**
- * Returns some parameter suitable for using it with a node
+ * Returns some parameter suitable for using it with a node.
  */
 function rules_events_node_variables($node_label, $update = FALSE) {
   $args = array(
@@ -83,51 +102,6 @@ function rules_events_node_variables($node_label, $update = FALSE) {
   return $args;
 }
 
-/**
- * Implements hook_rules_condition_info() on behalf of the node module.
- */
-function rules_node_condition_info() {
-  $defaults = array(
-    'parameter' => array(
-      'node' => array('type' => 'node', 'label' => t('Content')),
-    ),
-    'group' => t('Node'),
-    'access callback' => 'rules_node_integration_access',
-  );
-  $items['node_is_of_type'] = $defaults + array(
-    'label' => t('Content is of type'),
-    'help' => t('Evaluates to TRUE if the given content is of one of the selected content types.'),
-    'base' => 'rules_condition_node_is_of_type',
-  );
-  $items['node_is_of_type']['parameter']['type'] = array(
-    'type' => 'list<text>',
-    'label' => t('Content types'),
-    'options list' => 'node_type_get_names',
-    'description' => t('The content type(s) to check for.'),
-    'restriction' => 'input',
-  );
-  $items['node_is_published'] = $defaults + array(
-    'label' => t('Content is published'),
-    'base' => 'rules_condition_node_is_published',
-  );
-  $items['node_is_sticky'] = $defaults + array(
-    'label' => t('Content is sticky'),
-    'base' => 'rules_condition_node_is_sticky',
-  );
-  $items['node_is_promoted'] = $defaults + array(
-    'label' => t('Content is promoted to frontpage'),
-    'base' => 'rules_condition_node_is_promoted',
-  );
-  return $items;
-}
-
-/**
- * Provides the content type of a node as asserted metadata.
- */
-function rules_condition_node_is_of_type_assertions($element) {
-  return array('node' => array('bundle' => $element->settings['type']));
-}
-
 /**
  * Implements hook_rules_action_info() on behalf of the node module.
  */
@@ -136,7 +110,7 @@ function rules_node_action_info() {
     'parameter' => array(
       'node' => array('type' => 'node', 'label' => t('Content'), 'save' => TRUE),
     ),
-    'group' => t('Node'),
+    'category' => 'node',
     'access callback' => 'rules_node_admin_access',
   );
   // Add support for hand-picked core actions.
@@ -168,6 +142,22 @@ function rules_node_admin_access() {
   return user_access('administer nodes');
 }
 
+/**
+ * Event handler support node bundle event settings.
+ */
+class RulesNodeEventHandler extends RulesEventHandlerEntityBundle {
+
+  /**
+   * Returns the label to use for the bundle property.
+   *
+   * @return string
+   */
+  protected function getBundlePropertyLabel() {
+    return t('type');
+  }
+
+}
+
 /**
  * @}
  */

+ 6 - 2
sites/all/modules/rules/modules/path.eval.inc

@@ -5,6 +5,7 @@
  * Contains rules integration for the path module needed during evaluation.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
@@ -74,9 +75,12 @@ function rules_condition_path_alias_exists($alias, $langcode = LANGUAGE_NONE) {
 }
 
 /**
- * Cleans the given path by replacing non ASCII characters with the replacment character.
+ * Cleans the given path.
+ *
+ * A path is cleaned by replacing non ASCII characters in the path with the
+ * replacement character.
  *
- * Path cleaning may be adapted by overriding the configuration variables
+ * Path cleaning may be customized by overriding the configuration variables:
  * @code rules_clean_path @endcode,
  * @code rules_path_replacement_char @endcode and
  * @code rules_path_transliteration @endcode

+ 8 - 6
sites/all/modules/rules/modules/path.rules.inc

@@ -1,9 +1,11 @@
 <?php
 
 /**
- * @file rules integration for the path module
+ * @file
+ * Rules integration for the path module.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
@@ -26,13 +28,13 @@ function rules_path_action_info() {
         'source' => array(
           'type' => 'text',
           'label' => t('Existing system path'),
-          'description' => t('Specifies the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.') .' '. t('Leave it empty to delete URL aliases pointing to the given path alias.'),
+          'description' => t('Specifies the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.') . ' ' . t('Leave it empty to delete URL aliases pointing to the given path alias.'),
           'optional' => TRUE,
         ),
         'alias' => array(
           'type' => 'text',
           'label' => t('URL alias'),
-          'description' => t('Specify an alternative path by which this data can be accessed. For example, "about" for an about page. Use a relative path and do not add a trailing slash.') .' '. t('Leave it empty to delete URL aliases pointing to the given system path.'),
+          'description' => t('Specify an alternative path by which this data can be accessed. For example, "about" for an about page. Use a relative path and do not add a trailing slash.') . ' ' . t('Leave it empty to delete URL aliases pointing to the given system path.'),
           'optional' => TRUE,
           'cleaning callback' => 'rules_path_clean_replacement_values',
         ),
@@ -61,7 +63,7 @@ function rules_path_action_info() {
         'alias' => array(
           'type' => 'text',
           'label' => t('URL alias'),
-          'description' => t('Specify an alternative path by which the content can be accessed. For example, "about" for an about page. Use a relative path and do not add a trailing slash.') .' '. t('Leave it empty to delete the URL alias.'),
+          'description' => t('Specify an alternative path by which the content can be accessed. For example, "about" for an about page. Use a relative path and do not add a trailing slash.') . ' ' . t('Leave it empty to delete the URL alias.'),
           'optional' => TRUE,
           'cleaning callback' => 'rules_path_clean_replacement_values',
         ),
@@ -82,7 +84,7 @@ function rules_path_action_info() {
         'alias' => array(
           'type' => 'text',
           'label' => t('URL alias'),
-          'description' => t('Specify an alternative path by which the term can be accessed. For example, "content/drupal" for a Drupal term. Use a relative path and do not add a trailing slash.') .' '. t('Leave it empty to delete the URL alias.'),
+          'description' => t('Specify an alternative path by which the term can be accessed. For example, "content/drupal" for a Drupal term. Use a relative path and do not add a trailing slash.') . ' ' . t('Leave it empty to delete the URL alias.'),
           'optional' => TRUE,
           'cleaning callback' => 'rules_path_clean_replacement_values',
         ),
@@ -168,4 +170,4 @@ function rules_path_condition_info() {
 
 /**
  * @}
- */
+ */

+ 50 - 17
sites/all/modules/rules/modules/php.eval.inc

@@ -5,6 +5,7 @@
  * Contains rules integration for the php module needed during evaluation.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
@@ -13,10 +14,21 @@
  */
 class RulesPHPEvaluator extends RulesDataInputEvaluator {
 
-  public static function access() {
-    return user_access('use PHP for settings');
+  /**
+   * Overrides RulesDataProcessor::editAccess().
+   */
+  public function editAccess() {
+    return parent::editAccess() && (user_access('use PHP for settings') || drupal_is_cli());
   }
 
+  /**
+   * Helper function to find variables in PHP code.
+   *
+   * @param string $text
+   *   The PHP code.
+   * @param array $var_info
+   *   Array with variable names as keys.
+   */
   public static function getUsedVars($text, $var_info) {
     if (strpos($text, '<?') !== FALSE) {
       $used_vars = array();
@@ -29,14 +41,19 @@ class RulesPHPEvaluator extends RulesDataInputEvaluator {
     }
   }
 
+  /**
+   * Overrides RulesDataInputEvaluator::prepare().
+   */
   public function prepare($text, $var_info) {
     // A returned NULL skips the evaluator.
     $this->setting = self::getUsedVars($text, $var_info);
   }
 
   /**
-   * Evaluates PHP code contained in $text. This doesn't apply $options, thus
-   * the PHP code is responsible for behaving appropriately.
+   * Evaluates PHP code contained in $text.
+   *
+   * This method doesn't apply $options, thus the PHP code is responsible for
+   * behaving appropriately.
    */
   public function evaluate($text, $options, RulesState $state) {
     $vars['eval_options'] = $options;
@@ -46,6 +63,9 @@ class RulesPHPEvaluator extends RulesDataInputEvaluator {
     return rules_php_eval($text, rules_unwrap_data($vars));
   }
 
+  /**
+   * Overrides RulesDataInputEvaluator::help().
+   */
   public static function help($var_info) {
     module_load_include('inc', 'rules', 'rules/modules/php.rules');
 
@@ -58,6 +78,7 @@ class RulesPHPEvaluator extends RulesDataInputEvaluator {
 
     return $render;
   }
+
 }
 
 /**
@@ -65,6 +86,9 @@ class RulesPHPEvaluator extends RulesDataInputEvaluator {
  */
 class RulesPHPDataProcessor extends RulesDataProcessor {
 
+  /**
+   * Overrides RulesDataProcessor::form().
+   */
   protected static function form($settings, $var_info) {
     $settings += array('code' => '');
     $form = array(
@@ -84,14 +108,21 @@ class RulesPHPDataProcessor extends RulesDataProcessor {
     return $form;
   }
 
-  public static function access() {
-    return user_access('use PHP for settings');
+  /**
+   * Overrides RulesDataProcessor::editAccess().
+   */
+  public function editAccess() {
+    return parent::editAccess() && (user_access('use PHP for settings') || drupal_is_cli());
   }
 
+  /**
+   * Overrides RulesDataProcessor::process().
+   */
   public function process($value, $info, RulesState $state, RulesPlugin $element) {
     $value = isset($this->processor) ? $this->processor->process($value, $info, $state, $element) : $value;
     return rules_php_eval_return($this->setting['code'], array('value' => $value));
   }
+
 }
 
 /**
@@ -108,11 +139,11 @@ function rules_execute_php_eval($code, $settings, $state, $element) {
 }
 
 /**
- * Evalutes the given PHP code, with the given variables defined.
+ * Evaluates the given PHP code, with the given variables defined.
  *
- * @param $code
- *   The PHP code to run, with <?php ?>
- * @param $arguments
+ * @param string $code
+ *   The PHP code to run, including <?php and ?>
+ * @param array $arguments
  *   Array containing variables to be extracted to the code.
  *
  * @return
@@ -122,7 +153,7 @@ function rules_php_eval($code, $arguments = array()) {
   extract($arguments);
 
   ob_start();
-  print eval('?>'. $code);
+  print eval('?>' . $code);
   $output = ob_get_contents();
   ob_end_clean();
 
@@ -130,13 +161,15 @@ function rules_php_eval($code, $arguments = array()) {
 }
 
 /**
- * Evalutes the given PHP code, with the given variables defined. This is like
- * rules_php_eval() but does return the returned data from the PHP code.
+ * Evaluates the given PHP code, with the given variables defined.
  *
- * @param $code
- *   The PHP code to run, without <?php ?>
- * @param $arguments
- * Array containing variables to be extracted to the code.
+ * This is like rules_php_eval(), but does return the returned data from
+ * the PHP code.
+ *
+ * @param string $code
+ *   The PHP code to run, without <?php or ?>
+ * @param array $arguments
+ *   Array containing variables to be extracted to the code.
  *
  * @return
  *   The return value of the evaled code.

+ 7 - 6
sites/all/modules/rules/modules/php.rules.inc

@@ -1,9 +1,11 @@
 <?php
 
 /**
- * @file rules integration for the php module
+ * @file
+ * Rules integration for the php module.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
@@ -35,7 +37,7 @@ function rules_php_data_processor_info() {
   return array(
     'php' => array(
       'class' => 'RulesPHPDataProcessor',
-      'type' => array('text', 'token',  'decimal', 'integer', 'date', 'duration', 'boolean', 'uri'),
+      'type' => array('text', 'token', 'decimal', 'integer', 'date', 'duration', 'boolean', 'uri'),
       'weight' => 10,
       'module' => 'php',
     ),
@@ -124,8 +126,7 @@ function rules_php_evaluator_help($var_info, $action_help = FALSE) {
   $render['top'] = array(
     '#prefix' => '<p>',
     '#suffix' => '</p>',
-    '#markup' => t('PHP code inside of &lt;?php ?&gt; delimiters will be evaluated and replaced by its output. E.g. &lt;? echo 1+1?&gt; will be replaced by 2.')
-                 . ' ' . t('Furthermore you can make use of the following variables:'),
+    '#markup' => t('PHP code inside of &lt;?php ?&gt; delimiters will be evaluated and replaced by its output. E.g. &lt;? echo 1+1?&gt; will be replaced by 2.') . ' ' . t('Furthermore you can make use of the following variables:'),
   );
   $render['vars'] = array(
     '#theme' => 'table',
@@ -136,7 +137,7 @@ function rules_php_evaluator_help($var_info, $action_help = FALSE) {
   $cache = rules_get_cache();
   foreach ($var_info as $name => $info) {
     $row   = array();
-    $row[] = '$'. check_plain($name);
+    $row[] = '$' . check_plain($name);
     $label = isset($cache['data_info'][$info['type']]['label']) ? $cache['data_info'][$info['type']]['label'] : $info['type'];
     $row[] = check_plain(drupal_ucfirst($label));
     $row[] = check_plain($info['label']);
@@ -155,4 +156,4 @@ function rules_php_evaluator_help($var_info, $action_help = FALSE) {
 
 /**
  * @}
- */
+ */

+ 46 - 8
sites/all/modules/rules/modules/rules_core.eval.inc

@@ -5,6 +5,7 @@
  * Contains rules core integration needed during evaluation.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
@@ -46,7 +47,7 @@ function rules_element_invoke_component($arguments, RulesPlugin $element) {
     $state->mergeSaveVariables($new_state, $component, $element->settings);
     $state->unblock($component);
 
-    // Cleanup the state, what saves not mergable variables now.
+    // Cleanup the state, what saves not mergeable variables now.
     $new_state->cleanup();
     rules_log('Finished evaluation of @plugin %label.', $replacements, RulesLog::INFO, $component, FALSE);
     return $return;
@@ -57,13 +58,18 @@ function rules_element_invoke_component($arguments, RulesPlugin $element) {
 }
 
 /**
- * A class implementing a rules input evaluator processing date input. This is
- * needed to treat relative date inputs for strtotime right, consider "now".
+ * A class implementing a rules input evaluator processing date input.
+ *
+ * This is needed to treat relative date inputs for strtotime() correctly.
+ * Consider for example "now".
  */
 class RulesDateInputEvaluator extends RulesDataInputEvaluator {
 
   const DATE_REGEX_LOOSE = '/^(\d{4})-?(\d{2})-?(\d{2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})?)?$/';
 
+  /**
+   * Overrides RulesDataInputEvaluator::prepare().
+   */
   public function prepare($text, $var_info) {
     if (is_numeric($text)) {
       // Let rules skip this input evaluators in case it's already a timestamp.
@@ -71,6 +77,9 @@ class RulesDateInputEvaluator extends RulesDataInputEvaluator {
     }
   }
 
+  /**
+   * Overrides RulesDataInputEvaluator::evaluate().
+   */
   public function evaluate($text, $options, RulesState $state) {
     return self::gmstrtotime($text);
   }
@@ -89,14 +98,19 @@ class RulesDateInputEvaluator extends RulesDataInputEvaluator {
   public static function isFixedDateString($date) {
     return is_string($date) && preg_match(self::DATE_REGEX_LOOSE, $date);
   }
+
 }
 
 /**
- * A class implementing a rules input evaluator processing URI inputs to make
- * sure URIs are absolute and path aliases get applied.
+ * A class implementing a rules input evaluator processing URI inputs.
+ *
+ * Makes sure URIs are absolute and path aliases get applied.
  */
 class RulesURIInputEvaluator extends RulesDataInputEvaluator {
 
+  /**
+   * Overrides RulesDataInputEvaluator::prepare().
+   */
   public function prepare($uri, $var_info) {
     if (!isset($this->processor) && valid_url($uri, TRUE)) {
       // Only process if another evaluator is used or the url is not absolute.
@@ -104,6 +118,9 @@ class RulesURIInputEvaluator extends RulesDataInputEvaluator {
     }
   }
 
+  /**
+   * Overrides RulesDataInputEvaluator::evaluate().
+   */
   public function evaluate($uri, $options, RulesState $state) {
     if (!url_is_external($uri)) {
       // Extract the path and build the URL using the url() function, so URL
@@ -119,6 +136,7 @@ class RulesURIInputEvaluator extends RulesDataInputEvaluator {
     }
     throw new RulesEvaluationException('Input evaluation generated an invalid URI.', array(), NULL, RulesLog::WARN);
   }
+
 }
 
 /**
@@ -126,6 +144,9 @@ class RulesURIInputEvaluator extends RulesDataInputEvaluator {
  */
 class RulesDateOffsetProcessor extends RulesDataProcessor {
 
+  /**
+   * Overrides RulesDataProcessor::form().
+   */
   protected static function form($settings, $var_info) {
     $settings += array('value' => '');
     $form = array(
@@ -145,6 +166,9 @@ class RulesDateOffsetProcessor extends RulesDataProcessor {
     return $form;
   }
 
+  /**
+   * Overrides RulesDataProcessor::process().
+   */
   public function process($value, $info, RulesState $state, RulesPlugin $element) {
     $value = isset($this->processor) ? $this->processor->process($value, $info, $state, $element) : $value;
     return RulesDateOffsetProcessor::applyOffset($value, $this->setting['value']);
@@ -178,6 +202,7 @@ class RulesDateOffsetProcessor extends RulesDataProcessor {
       return $timestamp + $offset;
     }
   }
+
 }
 
 /**
@@ -185,6 +210,9 @@ class RulesDateOffsetProcessor extends RulesDataProcessor {
  */
 class RulesNumericOffsetProcessor extends RulesDataProcessor {
 
+  /**
+   * Overrides RulesDataProcessor::form().
+   */
   protected static function form($settings, $var_info) {
     $settings += array('value' => '');
     $form = array(
@@ -205,15 +233,20 @@ class RulesNumericOffsetProcessor extends RulesDataProcessor {
     return $form;
   }
 
+  /**
+   * Overrides RulesDataProcessor::process().
+   */
   public function process($value, $info, RulesState $state, RulesPlugin $element) {
     $value = isset($this->processor) ? $this->processor->process($value, $info, $state, $element) : $value;
     return $value + $this->setting['value'];
   }
-}
 
+}
 
 /**
- * A custom wrapper class for vocabularies that is capable of loading vocabularies by machine name.
+ * A custom wrapper class for vocabularies.
+ *
+ * This class is capable of loading vocabularies by machine name.
  */
 class RulesTaxonomyVocabularyWrapper extends EntityDrupalWrapper {
 
@@ -231,7 +264,7 @@ class RulesTaxonomyVocabularyWrapper extends EntityDrupalWrapper {
   }
 
   /**
-   * Overriden to permit machine names as values.
+   * Overridden to permit machine names as values.
    */
   public function validate($value) {
     if (isset($value) && is_string($value)) {
@@ -239,4 +272,9 @@ class RulesTaxonomyVocabularyWrapper extends EntityDrupalWrapper {
     }
     return parent::validate($value);
   }
+
 }
+
+/**
+ * @}
+ */

+ 34 - 7
sites/all/modules/rules/modules/rules_core.rules.inc

@@ -1,13 +1,29 @@
 <?php
 
 /**
- * @file Rules core integration providing data types and conditions and
- * actions to invoke configured components.
+ * @file
+ * Rules integration with Drupal core.
+ *
+ * Provides data types, conditions, and actions to invoke configured components.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
+/**
+ * Implements hook_rules_category_info() on behalf of the rules_core.
+ */
+function rules_rules_core_category_info() {
+  return array(
+    'rules_components' => array(
+      'label' => t('Components'),
+      'equals group' => t('Components'),
+      'weight' => 50,
+    ),
+  );
+}
+
 /**
  * Implements hook_rules_file_info() on behalf of the pseudo rules_core module.
  *
@@ -107,6 +123,12 @@ function rules_rules_core_data_info() {
       'group' => t('Entity'),
       'is wrapped' => TRUE,
     ),
+    'ip_address' => array(
+      'label' => t('IP Address'),
+      'parent' => 'text',
+      'ui class' => 'RulesDataUIIPAddress',
+      'token type' => 'rules_text',
+    ),
   );
   foreach (entity_get_info() as $type => $info) {
     if (!empty($info['label'])) {
@@ -117,6 +139,11 @@ function rules_rules_core_data_info() {
         'group' => t('Entity'),
         'ui class' => empty($info['exportable']) ? 'RulesDataUIEntity' : 'RulesDataUIEntityExportable',
       );
+      // If this entity type serves as bundle for another one, provide an
+      // options list for selecting a bundle entity.
+      if (!empty($info['bundle of'])) {
+        $return[$type]['ui class'] = 'RulesDataUIBundleEntity';
+      }
     }
   }
 
@@ -167,13 +194,13 @@ function rules_rules_core_evaluator_info() {
       'class' => 'RulesDateInputEvaluator',
       'type' => 'date',
       'weight' => -10,
-     ),
+    ),
     // Post-process any input value to absolute URIs.
     'uri' => array(
       'class' => 'RulesURIInputEvaluator',
       'type' => 'uri',
       'weight' => 50,
-     ),
+    ),
   );
 }
 
@@ -189,12 +216,12 @@ function rules_rules_core_data_processor_info() {
       'class' => 'RulesDateOffsetProcessor',
       'type' => 'date',
       'weight' => -2,
-     ),
+    ),
     'num_offset' => array(
       'class' => 'RulesNumericOffsetProcessor',
       'type' => array('integer', 'decimal'),
       'weight' => -2,
-     ),
+    ),
   );
 }
 
@@ -282,7 +309,7 @@ function rules_element_invoke_component_validate(RulesPlugin $element) {
 }
 
 /**
- * Implements the features export callback of the RulesPluginFeaturesIntegrationInterace.
+ * Implements the features export callback of the RulesPluginFeaturesIntegrationInterface.
  */
 function rules_element_invoke_component_features_export(&$export, &$pipe, $module_name = '', $element) {
   // Add the used component to the pipe.

+ 30 - 7
sites/all/modules/rules/modules/system.eval.inc

@@ -5,6 +5,7 @@
  * Contains rules integration for the system module needed during evaluation.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
@@ -42,8 +43,8 @@ function rules_action_drupal_goto($url, $force = FALSE, $destination = FALSE) {
   if ($force && isset($_GET['destination'])) {
     unset($_GET['destination']);
   }
-  // We don't invoke drupal_goto() right now, as this would end the the current
-  // page execution unpredictly for modules. So we'll take over drupal_goto()
+  // We don't invoke drupal_goto() right now, as this would end the current
+  // page execution unpredictably for modules. So we'll take over drupal_goto()
   // calls from somewhere else via hook_drupal_goto_alter() and make sure
   // a drupal_goto() is invoked before the page is output with
   // rules_page_build().
@@ -106,9 +107,8 @@ function rules_action_mail_to_users_of_role($roles, $subject, $message, $from =
     $result = db_query('SELECT mail FROM {users} WHERE uid > 0');
   }
   else {
-    $rids = implode(',', $roles);
     // Avoid sending emails to members of two or more target role groups.
-    $result = db_query('SELECT DISTINCT u.mail FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid WHERE r.rid IN ('. $rids .')');
+    $result = db_query('SELECT DISTINCT u.mail FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid WHERE r.rid IN (:rids)', array(':rids' => $roles));
   }
 
   // Now, actually send the mails.
@@ -123,11 +123,17 @@ function rules_action_mail_to_users_of_role($roles, $subject, $message, $from =
   $message = array('result' => TRUE);
   foreach ($result as $row) {
     $message = drupal_mail('rules', $key, $row->mail, language_default(), $params, $from);
-    if (!$message['result']) {
+    // If $message['result'] is FALSE, then it's likely that email sending is
+    // failing at the moment, and we should just abort sending any more. If
+    // however, $message['result'] is NULL, then it's likely that a module has
+    // aborted sending this particular email to this particular user, and we
+    // should just keep on sending emails to the other users.
+    // For more information on the result value, see drupal_mail().
+    if ($message['result'] === FALSE) {
       break;
     }
   }
-  if ($message['result']) {
+  if ($message['result'] !== FALSE) {
     $role_names = array_intersect_key(user_roles(TRUE), array_flip($roles));
     watchdog('rules', 'Successfully sent email to the role(s) %roles.', array('%roles' => implode(', ', $role_names)));
   }
@@ -136,7 +142,7 @@ function rules_action_mail_to_users_of_role($roles, $subject, $message, $from =
 /**
  * Implements hook_mail().
  *
- * Set's the message subject and body as configured.
+ * Sets the message subject and body as configured.
  */
 function rules_mail($key, &$message, $params) {
 
@@ -144,11 +150,25 @@ function rules_mail($key, &$message, $params) {
   $message['body'][] = $params['message'];
 }
 
+/**
+ * Action: Block an IP address.
+ */
+function rules_action_block_ip($ip_address = NULL) {
+  if (empty($ip_address)) {
+    $ip_address = ip_address();
+  }
+  db_insert('blocked_ips')->fields(array('ip' => $ip_address))->execute();
+  watchdog('rules', 'Banned IP address %ip', array('%ip' => $ip_address));
+}
+
 /**
  * A class implementing a rules input evaluator processing tokens.
  */
 class RulesTokenEvaluator extends RulesDataInputEvaluator {
 
+  /**
+   * Overrides RulesDataInputEvaluator::prepare().
+   */
   public function prepare($text, $var_info) {
     $text = is_array($text) ? implode('', $text) : $text;
     // Skip this evaluator if there are no tokens.
@@ -156,6 +176,8 @@ class RulesTokenEvaluator extends RulesDataInputEvaluator {
   }
 
   /**
+   * Evaluate tokens.
+   *
    * We replace the tokens on our own as we cannot use token_replace(), because
    * token usually assumes that $data['node'] is a of type node, which doesn't
    * hold in general in our case.
@@ -245,6 +267,7 @@ class RulesTokenEvaluator extends RulesDataInputEvaluator {
     }
     return $render;
   }
+
 }
 
 /**

+ 23 - 4
sites/all/modules/rules/modules/system.rules.inc

@@ -1,9 +1,11 @@
 <?php
 
 /**
- * @file rules integration for the system module
+ * @file
+ * Rules integration for the system module.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
@@ -46,6 +48,7 @@ function rules_system_event_info() {
 
 /**
  * Implements hook_rules_data_info() on behalf of the system module.
+ *
  * @see rules_core_modules()
  */
 function rules_system_data_info() {
@@ -59,8 +62,9 @@ function rules_system_data_info() {
 }
 
 /**
- * Defines property info for watchdog log entries, used by the log entry data
- * type to provide an useful metadata wrapper.
+ * Defines property info for watchdog log entries.
+ *
+ * Used by the log entry data type to provide a useful metadata wrapper.
  */
 function _rules_system_watchdog_log_entry_info() {
   return array(
@@ -242,6 +246,21 @@ function rules_system_action_info() {
       'base' => 'rules_action_mail_to_users_of_role',
       'access callback' => 'rules_system_integration_access',
     ),
+    'block_ip' => array(
+      'label' => t('Block IP address'),
+      'group' => t('System'),
+      'parameter' => array(
+        'ip_address' => array(
+          'type' => 'ip_address',
+          'label' => t('IP address'),
+          'description' => t('If not provided, the IP address of the current user will be used.'),
+          'optional' => TRUE,
+          'default value' => NULL,
+        ),
+      ),
+      'base' => 'rules_action_block_ip',
+      'access callback' => 'rules_system_integration_access',
+    ),
   );
 }
 
@@ -279,7 +298,7 @@ function rules_system_evaluator_info() {
       'class' => 'RulesTokenEvaluator',
       'type' => array('text', 'uri', 'list<text>', 'list<uri>'),
       'weight' => 0,
-     ),
+    ),
   );
 }
 

+ 59 - 9
sites/all/modules/rules/modules/taxonomy.rules.inc

@@ -1,9 +1,11 @@
 <?php
 
 /**
- * @file rules integration for the taxonomy_term module
+ * @file
+ * Rules integration for the taxonomy_term module.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
@@ -15,6 +17,7 @@ function rules_taxonomy_event_info() {
     'group' => t('Taxonomy'),
     'access callback' => 'rules_taxonomy_term_integration_access',
     'module' => 'taxonomy',
+    'class' => 'RulesTaxonomyEventHandler',
   );
   $defaults_vocab = array(
     'group' => t('Taxonomy'),
@@ -32,14 +35,26 @@ function rules_taxonomy_event_info() {
       'label' => t('After updating an existing term'),
       'variables' => array(
         'term' => array('type' => 'taxonomy_term', 'label' => t('updated term')),
-        'term_unchanged' => array('type' => 'taxonomy_term', 'label' => t('unchanged term'), 'handler' => 'rules_events_entity_unchanged'),
+        'term_unchanged' => array(
+          'type' => 'taxonomy_term',
+          'label' => t('unchanged term'),
+          'handler' => 'rules_events_entity_unchanged',
+        ),
       ),
     ),
     'taxonomy_term_presave' => $defaults_term + array(
       'label' => t('Before saving a taxonomy term'),
       'variables' => array(
-        'term' => array('type' => 'taxonomy_term', 'label' => t('saved term'), 'skip save' => TRUE),
-        'term_unchanged' => array('type' => 'taxonomy_term', 'label' => t('unchanged term'), 'handler' => 'rules_events_entity_unchanged'),
+        'term' => array(
+          'type' => 'taxonomy_term',
+          'label' => t('saved term'),
+          'skip save' => TRUE,
+        ),
+        'term_unchanged' => array(
+          'type' => 'taxonomy_term',
+          'label' => t('unchanged term'),
+          'handler' => 'rules_events_entity_unchanged',
+        ),
       ),
     ),
     'taxonomy_term_delete' => $defaults_term + array(
@@ -57,21 +72,39 @@ function rules_taxonomy_event_info() {
     'taxonomy_vocabulary_update' => $defaults_vocab + array(
       'label' => t('After updating an existing vocabulary'),
       'variables' => array(
-        'vocabulary' => array('type' => 'taxonomy_vocabulary', 'label' => t('updated vocabulary')),
-        'vocabulary_unchanged' => array('type' => 'taxonomy_vocabulary', 'label' => t('unchanged vocabulary'), 'handler' => 'rules_events_entity_unchanged'),
+        'vocabulary' => array(
+          'type' => 'taxonomy_vocabulary',
+          'label' => t('updated vocabulary'),
+        ),
+        'vocabulary_unchanged' => array(
+          'type' => 'taxonomy_vocabulary',
+          'label' => t('unchanged vocabulary'),
+          'handler' => 'rules_events_entity_unchanged',
+        ),
       ),
     ),
     'taxonomy_vocabulary_presave' => $defaults_vocab + array(
       'label' => t('Before saving a vocabulary'),
       'variables' => array(
-        'vocabulary' => array('type' => 'taxonomy_vocabulary', 'label' => t('saved vocabulary'), 'skip save' => TRUE),
-        'vocabulary_unchanged' => array('type' => 'taxonomy_vocabulary', 'label' => t('unchanged vocabulary'), 'handler' => 'rules_events_entity_unchanged'),
+        'vocabulary' => array(
+          'type' => 'taxonomy_vocabulary',
+          'label' => t('saved vocabulary'),
+          'skip save' => TRUE,
+        ),
+        'vocabulary_unchanged' => array(
+          'type' => 'taxonomy_vocabulary',
+          'label' => t('unchanged vocabulary'),
+          'handler' => 'rules_events_entity_unchanged',
+        ),
       ),
     ),
     'taxonomy_vocabulary_delete' => $defaults_vocab + array(
       'label' => t('After deleting a vocabulary'),
       'variables' => array(
-        'vocabulary' => array('type' => 'taxonomy_vocabulary', 'label' => t('deleted vocabulary')),
+        'vocabulary' => array(
+          'type' => 'taxonomy_vocabulary',
+          'label' => t('deleted vocabulary'),
+        ),
       ),
     ),
   );
@@ -95,6 +128,23 @@ function rules_taxonomy_vocabulary_integration_access($type, $name) {
   }
 }
 
+/**
+ * Event handler support taxonomy bundle event settings.
+ */
+class RulesTaxonomyEventHandler extends RulesEventHandlerEntityBundle {
+
+  /**
+   * Returns the label to use for the bundle property.
+   *
+   * @return string
+   *   The label to use for the bundle property.
+   */
+  protected function getBundlePropertyLabel() {
+    return t('vocabulary');
+  }
+
+}
+
 /**
  * @}
  */

+ 35 - 2
sites/all/modules/rules/modules/user.eval.inc

@@ -5,11 +5,12 @@
  * Contains rules integration for the user module needed during evaluation.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
 /**
- * Condition user: condition to check whether user has particular roles
+ * Condition user: condition to check whether user has particular roles.
  */
 function rules_condition_user_has_role($account, $roles, $operation = 'AND') {
   switch ($operation) {
@@ -43,7 +44,7 @@ function rules_condition_user_is_blocked($account) {
  */
 function rules_action_user_add_role($account, $roles) {
   if ($account->uid || !empty($account->is_new)) {
-    // Get role list (minus the anonymous)
+    // Get role list (minus the anonymous).
     $role_list = user_roles(TRUE);
 
     foreach ($roles as $rid) {
@@ -97,6 +98,38 @@ function rules_action_user_unblock($account) {
   $account->status = 1;
 }
 
+/**
+ * Action: Send a user account e-mail.
+ */
+function rules_action_user_send_account_email($account, $email_type) {
+  // If we received an authenticated user account...
+  if (!empty($account->uid)) {
+    module_load_include('inc', 'rules', 'modules/user.rules');
+    $types = rules_user_account_email_options_list();
+
+    // Attempt to send the account e-mail.
+    // This code is adapted from _user_mail_notify().
+    $params = array('account' => $account);
+    $language = user_preferred_language($account);
+    $mail = drupal_mail('user', $email_type, $account->mail, $language, $params);
+    if ($email_type == 'register_pending_approval') {
+      // If a user registered requiring admin approval, notify the admin, too.
+      // We use the site default language for this.
+      drupal_mail('user', 'register_pending_approval_admin', variable_get('site_mail', ini_get('sendmail_from')), language_default(), $params);
+    }
+
+    $result = empty($mail) ? NULL : $mail['result'];
+
+    // Log the success or failure.
+    if ($result) {
+      watchdog('rules', '%type e-mail sent to %recipient.', array('%type' => $types[$email_type], '%recipient' => $account->mail));
+    }
+    else {
+      watchdog('rules', 'Failed to send %type e-mail to %recipient.', array('%type' => $types[$email_type], '%recipient' => $account->mail));
+    }
+  }
+}
+
 /**
  * @}
  */

+ 46 - 5
sites/all/modules/rules/modules/user.rules.inc

@@ -1,9 +1,11 @@
 <?php
 
 /**
- * @file rules integration for the user module
+ * @file
+ * Rules integration for the user module.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
@@ -15,7 +17,7 @@ function rules_user_file_info() {
 }
 
 /**
- * Implementation of hook_rules_event_info().
+ * Implements hook_rules_event_info().
  */
 function rules_user_event_info() {
   return array(
@@ -54,6 +56,8 @@ function rules_user_event_info() {
           'type' => 'text',
           'label' => t('view mode'),
           'options list' => 'rules_get_entity_view_modes',
+          // Add the entity-type for the options list callback.
+          'options list entity type' => 'user',
         ),
       ),
       'access callback' => 'rules_user_integration_access',
@@ -88,7 +92,8 @@ function rules_user_event_info() {
 
 /**
  * Options list for user cancel methods.
- * @todo: Use for providing a user_cancel action.
+ *
+ * @todo Use for providing a user_cancel action.
  */
 function rules_user_cancel_methods() {
   module_load_include('inc', 'user', 'user.pages');
@@ -171,7 +176,7 @@ function rules_user_condition_operations() {
  */
 function rules_user_action_info() {
   $defaults = array(
-   'parameter' => array(
+    'parameter' => array(
       'account' => array(
         'type' => 'user',
         'label' => t('User'),
@@ -196,7 +201,7 @@ function rules_user_action_info() {
     'base' => 'rules_action_user_remove_role',
   );
   $defaults = array(
-   'parameter' => array(
+    'parameter' => array(
       'account' => array(
         'type' => 'user',
         'label' => t('User'),
@@ -214,6 +219,24 @@ function rules_user_action_info() {
     'label' => t('Unblock a user'),
     'base' => 'rules_action_user_unblock',
   );
+  $items['user_send_account_email'] = array(
+    'label' => t('Send account e-mail'),
+    'parameter' => array(
+      'account' => array(
+        'type' => 'user',
+        'label' => t('Account'),
+      ),
+      'email_type' => array(
+        'type' => 'text',
+        'label' => t('E-mail type'),
+        'description' => t("Select the e-mail based on your site's account settings to send to the user."),
+        'options list' => 'rules_user_account_email_options_list',
+      ),
+    ),
+    'group' => t('User'),
+    'base' => 'rules_action_user_send_account_email',
+    'access callback' => 'rules_user_integration_access',
+  );
   return $items;
 }
 
@@ -231,6 +254,24 @@ function rules_user_roles_options_list($element) {
   return entity_metadata_user_roles('roles', array(), $element instanceof RulesConditionInterface ? 'view' : 'edit');
 }
 
+/**
+ * Options list callback for user account e-mail types.
+ *
+ * @see _user_mail_notify()
+ */
+function rules_user_account_email_options_list() {
+  return array(
+    'register_admin_created' => t('Welcome (new user created by administrator)'),
+    'register_no_approval_required' => t('Welcome (no approval required)'),
+    'register_pending_approval' => t('Welcome (awaiting approval)'),
+    'password_reset' => t('Password recovery'),
+    'status_activated' => t('Account activation'),
+    'status_blocked' => t('Account blocked'),
+    'cancel_confirm' => t('Account cancellation confirmation'),
+    'status_canceled' => t('Account canceled'),
+  );
+}
+
 /**
  * @}
  */

+ 198 - 67
sites/all/modules/rules/rules.api.php

@@ -2,19 +2,20 @@
 
 /**
  * @file
+ * Documentation for hooks provided by the Rules API.
+ *
  * This file contains no working PHP code; it exists to provide additional
  * documentation for doxygen as well as to document hooks in the standard
  * Drupal manner.
  */
 
-
 /**
  * @defgroup rules Rules module integrations.
  *
  * Module integrations with the rules module.
  *
  * The Rules developer documentation describes how modules can integrate with
- * rules: http://drupal.org/node/298486.
+ * rules: https://www.drupal.org/node/298486.
  */
 
 /**
@@ -30,7 +31,11 @@
  * placed into the file MODULENAME.rules.inc, which gets automatically included
  * when the hook is invoked.
  *
- * @return
+ * However, as an alternative to implementing this hook, class based plugin
+ * handlers may be provided by implementing RulesActionHandlerInterface. See
+ * the interface for details.
+ *
+ * @return array
  *   An array of information about the module's provided rules actions.
  *   The array contains a sub-array for each action, with the action name as
  *   the key. Actions names may only contain lowercase alpha-numeric characters
@@ -62,7 +67,7 @@
  *   - 'access callback': (optional) A callback which has to return whether the
  *     currently logged in user is allowed to configure this action. See
  *     rules_node_integration_access() for an example callback.
- *  Each 'parameter' array may contain the following properties:
+ *   Each 'parameter' array may contain the following properties:
  *   - label: The label of the parameter. Start capitalized. Required.
  *   - type: The rules data type of the parameter, which is to be passed to the
  *     action. All types declared in hook_rules_data_info() may be specified, as
@@ -110,7 +115,7 @@
  *     to clean inserted replacements; e.g. this is used by the token evaluator.
  *   - wrapped: (optional) Set this to TRUE in case the data should be passed
  *     wrapped. This only applies to wrapped data types, e.g. entities.
- *  Each 'provides' array may contain the following properties:
+ *   Each 'provides' array may contain the following properties:
  *   - label: The label of the variable. Start capitalized. Required.
  *   - type: The rules data type of the variable. All types declared in
  *     hook_rules_data_info() may be specified. Types may be parametrized e.g.
@@ -118,21 +123,20 @@
  *   - save: (optional) If this is set to TRUE, the provided variable is saved
  *     by rules when the rules evaluation ends. Only possible for savable data
  *     types. Defaults to FALSE.
+ *   The module has to provide an implementation for each action, being a
+ *   function named as specified in the 'base' key or for the execution callback.
+ *   All other possible callbacks are optional.
+ *   Supported action callbacks by rules are defined and documented in the
+ *   RulesPluginImplInterface. However any module may extend the action plugin
+ *   based upon a defined interface using hook_rules_plugin_info(). All methods
+ *   defined in those interfaces can be overridden by the action implementation.
+ *   The callback implementations for those interfaces may reside in any file
+ *   specified in hook_rules_file_info().
  *
- *  The module has to provide an implementation for each action, being a
- *  function named as specified in the 'base' key or for the execution callback.
- *  All other possible callbacks are optional.
- *  Supported action callbacks by rules are defined and documented in the
- *  RulesPluginImplInterface. However any module may extend the action plugin
- *  based upon a defined interface using hook_rules_plugin_info(). All methods
- *  defined in those interfaces can be overridden by the action implementation.
- *  The callback implementations for those interfaces may reside in any file
- *  specified in hook_rules_file_info().
- *
- *  @see hook_rules_file_info()
- *  @see rules_action_execution_callback()
- *  @see hook_rules_plugin_info()
- *  @see RulesPluginImplInterface
+ * @see hook_rules_file_info()
+ * @see rules_action_execution_callback()
+ * @see hook_rules_plugin_info()
+ * @see RulesPluginImplInterface
  */
 function hook_rules_action_info() {
   return array(
@@ -151,6 +155,60 @@ function hook_rules_action_info() {
   );
 }
 
+/**
+ * Define categories for Rules items, e.g. actions, conditions or events.
+ *
+ * Categories are similar to the previously used 'group' key in e.g.
+ * hook_rules_action_info(), but have a machine name and some more optional
+ * keys like a weight, or an icon.
+ *
+ * For best compatibility, modules may keep using the 'group' key for referring
+ * to categories. However, if a 'group' key and a 'category' is given the group
+ * will be treated as grouping in the given category (e.g. group "paypal" in
+ * category "commerce payment").
+ *
+ * @return array
+ *   An array of information about the module's provided categories.
+ *   The array contains a sub-array for each category, with the category name as
+ *   the key. Names may only contain lowercase alpha-numeric characters
+ *   and underscores and should be prefixed with the providing module name.
+ *   Possible attributes for each sub-array are:
+ *   - label: The label of the category. Start capitalized. Required.
+ *   - weight: (optional) A weight for sorting the category. Defaults to 0.
+ *   - equals group: (optional) For BC, categories may be defined that equal
+ *     a previously used 'group'.
+ *   - icon: (optional) The file path of an icon to use, relative to the module
+ *     or specified icon path. The icon should be a transparent SVG containing
+ *     no colors (only #fff). See https://www.drupal.org/node/2090265 for
+ *     instructions on how to create a suitable icon.
+ *     Note that the icon is currently not used by Rules, however other UIs
+ *     building upon Rules (like fluxkraft) do, and future releases of Rules
+ *     might do as well. Consequently, the definition of an icon is optional.
+ *     However, if both an icon font and icon is given, the icon is preferred.
+ *   - icon path: (optional) The base path for the icon. Defaults to the
+ *     providing module's directory.
+ *   - icon font class: (optional) An icon font class referring to a suitable
+ *     icon. Icon font class names should map to the ones as defined by Font
+ *     Awesome, while themes might want to choose to provide another icon font.
+ *     See http://fortawesome.github.io/Font-Awesome/cheatsheet/.
+ *   - icon background color: (optional) The color used as icon background.
+ *     Should have a high contrast to white. Defaults to #ddd.
+ */
+function hook_rules_category_info() {
+  return array(
+    'rules_data' => array(
+      'label' => t('Data'),
+      'equals group' => t('Data'),
+      'weight' => -50,
+    ),
+    'fluxtwitter' => array(
+      'label' => t('Twitter'),
+      'icon font class' => 'icon-twitter',
+      'icon font background color' => '#30a9fd',
+    ),
+  );
+}
+
 /**
  * Specify files containing rules integration code.
  *
@@ -163,13 +221,34 @@ function hook_rules_action_info() {
  * plugin method callbacks in any file without having to care about file
  * inclusion.
  *
- * @return
+ * @return array
  *   An array of file names without the file ending which defaults to '.inc'.
  */
 function hook_rules_file_info() {
   return array('yourmodule.rules-eval');
 }
 
+/**
+ * Specifies directories for class-based plugin handler discovery.
+ *
+ * Implementing this hook is not a requirement, it is just one option to load
+ * the files containing the classes during discovery - see
+ * rules_discover_plugins().
+ *
+ * @return string|array
+ *   A directory relative to the module directory, which holds the files
+ *   containing rules plugin handlers, or multiple directories keyed by the
+ *   module the directory is contained in.
+ *   All files in those directories having a 'php' or 'inc' file extension will
+ *   be loaded during discovery. Optionally, wildcards ('*') may be used to
+ *   match multiple directories.
+ *
+ * @see rules_discover_plugins()
+ */
+function hook_rules_directory() {
+  return 'lib/Drupal/fluxtwitter/Rules/*';
+}
+
 /**
  * The execution callback for an action.
  *
@@ -180,10 +259,11 @@ function hook_rules_file_info() {
  *   The callback gets arguments passed as described as parameter in
  *   hook_rules_action_info() as well as an array containing the action's
  *   configuration settings.
- * @return
- *   The action may return an array containg parameter or provided variables
+ *
+ * @return array
+ *   The action may return an array containing parameter or provided variables
  *   with their names as key. This is used update the value of a parameter or to
- *   provdide the value for a provided variable.
+ *   provide the value for a provided variable.
  *   Apart from that any parameters which have the key 'save' set to TRUE will
  *   be remembered to be saved by rules unless the action returns FALSE.
  *   Conditions have to return a boolean value in any case.
@@ -203,6 +283,10 @@ function rules_action_execution_callback($node, $title, $settings) {
  * placed into the file MODULENAME.rules.inc, which gets automatically included
  * when the hook is invoked.
  *
+ * However, as an alternative to implementing this hook, class based plugin
+ * handlers may be provided by implementing RulesConditionHandlerInterface. See
+ * the interface for details.
+ *
  * Adding conditions works exactly the same way as adding actions, with the
  * exception that conditions can't provide variables and cannot save parameters.
  * Thus the 'provides' attribute is not supported. Furthermore the condition
@@ -234,7 +318,11 @@ function hook_rules_condition_info() {
  * usually it's invoked directly from the providing module but wrapped by a
  * module_exists('rules') check.
  *
- * @return
+ * However, as an alternative to implementing this hook, class based event
+ * handlers may be provided by implementing RulesEventHandlerInterface. See
+ * the interface for details.
+ *
+ * @return array
  *   An array of information about the module's provided rules events. The array
  *   contains a sub-array for each event, with the event name as the key. The
  *   name may only contain lower case alpha-numeric characters and underscores
@@ -244,13 +332,18 @@ function hook_rules_condition_info() {
  *   - group: A group for this element, used for grouping the events in the
  *     interface. Should start with a capital letter and be translated.
  *     Required.
- *   - 'access callback': An callback, which has to return whether the
+ *   - class: (optional) An event handler class implementing the
+ *     RulesEventHandlerInterface. If none is specified the
+ *     RulesEventDefaultHandler class will be used. While the default event
+ *     handler has no settings, custom event handlers may be implemented to
+ *     to make an event configurable. See RulesEventHandlerInterface.
+ *   - access callback: (optional) An callback, which has to return whether the
  *     currently logged in user is allowed to configure rules for this event.
  *     Access should be only granted, if the user at least may access all the
- *     variables provided by the event. Optional.
- *   - help: A help text for rules reaction on this event.
- *   - variables: An array describing all variables that are available for
- *     elements reaction on this event. Optional. Each variable has to be
+ *     variables provided by the event.
+ *   - help: (optional) A help text for rules reaction on this event.
+ *   - variables: (optional) An array describing all variables that are
+ *     available for elements reacting on this event. Each variable has to be
  *     described by a sub-array with the possible attributes:
  *     - label: The label of the variable. Start capitalized. Required.
  *     - type: The rules data type of the variable. All types declared in
@@ -262,17 +355,17 @@ function hook_rules_condition_info() {
  *     - 'options list': (optional) A callback that returns an array of possible
  *       values for this variable as specified for entity properties at
  *       hook_entity_property_info().
- *     - 'skip save': If the variable is saved after the event has occurred
- *       anyway, set this to TRUE. So rules won't save the variable a second
- *       time. Optional, defaults to FALSE.
- *     - handler: A handler to load the actual variable value. This is useful
- *       for lazy loading variables. The handler gets all so far available
- *       variables passed in the order as defined. Optional. Also see
- *       http://drupal.org/node/884554.
+ *     - 'skip save': (optional) If the variable is saved after the event has
+ *       occurred anyway, set this to TRUE. So rules won't save the variable a
+ *       second time. Defaults to FALSE.
+ *     - handler: (optional) A handler to load the actual variable value. This
+ *       is useful for lazy loading variables. The handler gets all so far
+ *       available variables passed in the order as defined. Also see
+ *       https://www.drupal.org/node/884554.
  *       Note that for lazy-loading entities just the entity id may be passed
  *       as variable value, so a handler is not necessary in that case.
  *
- *  @see rules_invoke_event()
+ * @see rules_invoke_event()
  */
 function hook_rules_event_info() {
   $items = array(
@@ -321,8 +414,7 @@ function hook_rules_event_info() {
  * module.
  * For a list of data types defined by rules see rules_rules_core_data_info().
  *
- *
- * @return
+ * @return array
  *   An array of information about the module's provided data types. The array
  *   contains a sub-array for each data type, with the data type name as the
  *   key. The name may only contain lower case alpha-numeric characters and
@@ -338,7 +430,9 @@ function hook_rules_event_info() {
  *     configuration UI to configure parameters of this type. The given class
  *     must extend RulesDataUI and may implement the
  *     RulesDataDirectInputFormInterface in order to allow the direct data input
- *     configuration mode. Defaults to RulesDataUI.
+ *     configuration mode. For supporting selecting values from options lists,
+ *     the UI class may implement RulesDataInputOptionsListInterface also.
+ *     Defaults to RulesDataUI.
  *   - wrap: (optional) If set to TRUE, the data is wrapped internally using
  *     wrappers provided by the entity API module. This is required for entities
  *     and data structures to support selecting a property via the data selector
@@ -352,7 +446,7 @@ function hook_rules_event_info() {
  *     makes use of the class for wrapping the data of the given type. However
  *     note that if data is already wrapped when it is passed to Rules, the
  *     existing wrappers will be kept.
- *     For modules implementing identifiable data types being non-entites the
+ *     For modules implementing identifiable data types being non-entities the
  *     class RulesIdentifiableDataWrapper is provided, which can be used as base
  *     for a custom wrapper class. See RulesIdentifiableDataWrapper for details.
  *   - property info: (optional) May be used for non-entity data structures to
@@ -376,9 +470,9 @@ function hook_rules_event_info() {
  *   - cleaning callback: (optional) A callback that input evaluators may use
  *     to clean inserted replacements; e.g. this is used by the token evaluator.
  *
- *  @see entity_metadata_wrapper()
- *  @see hook_rules_data_info_alter()
- *  @see rules_rules_core_data_info()
+ * @see entity_metadata_wrapper()
+ * @see hook_rules_data_info_alter()
+ * @see rules_rules_core_data_info()
  */
 function hook_rules_data_info() {
   return array(
@@ -403,7 +497,7 @@ function hook_rules_data_info() {
  * A rules configuration may consist of elements being instances of any rules
  * plugin. This hook can be used to define new or to extend rules plugins.
  *
- * @return
+ * @return array
  *   An array of information about the module's provided rules plugins. The
  *   array contains a sub-array for each plugin, with the plugin name as the
  *   key. The name may only contain lower case alpha-numeric characters,
@@ -449,8 +543,8 @@ function hook_rules_data_info() {
  *     of the 'or' plugin. Note that only uppercase values are allowed, as
  *     lower case values are treated as action or condition exports.
  *
- *  @see class RulesPlugin
- *  @see hook_rules_plugin_info_alter()
+ * @see RulesPlugin
+ * @see hook_rules_plugin_info_alter()
  */
 function hook_rules_plugin_info() {
   return array(
@@ -489,7 +583,7 @@ function hook_rules_plugin_info() {
  * and help() could be overridden in order to control access permissions or to
  * provide some usage help.
  *
- * @return
+ * @return array
  *   An array of information about the module's provided input evaluators. The
  *   array contains a sub-array for each evaluator, with the evaluator name as
  *   the key. The name may only contain lower case alpha-numeric characters and
@@ -503,8 +597,8 @@ function hook_rules_plugin_info() {
  *     used. Defaults to 'text'. Multiple data types may be specified using an
  *     array.
  *
- *  @see class RulesDataInputEvaluator
- *  @see hook_rules_evaluator_info_alter()
+ * @see RulesDataInputEvaluator
+ * @see hook_rules_evaluator_info_alter()
  */
 function hook_rules_evaluator_info() {
   return array(
@@ -512,7 +606,7 @@ function hook_rules_evaluator_info() {
       'class' => 'RulesTokenEvaluator',
       'type' => array('text', 'uri'),
       'weight' => 0,
-     ),
+    ),
   );
 }
 
@@ -527,7 +621,7 @@ function hook_rules_evaluator_info() {
  * access() could be overridden in order to provide a configuration form or
  * to control access permissions.
  *
- * @return
+ * @return array
  *   An array of information about the module's provided data processors. The
  *   array contains a sub-array for each processor, with the processor name as
  *   the key. The name may only contain lower case alpha-numeric characters and
@@ -542,8 +636,8 @@ function hook_rules_evaluator_info() {
  *     used. Defaults to 'text'. Multiple data types may be specified using an
  *     array.
  *
- *  @see class RulesDataProcessor
- *  @see hook_rules_data_processor_info_alter()
+ * @see RulesDataProcessor
+ * @see hook_rules_data_processor_info_alter()
  */
 function hook_rules_data_processor_info() {
   return array(
@@ -551,7 +645,7 @@ function hook_rules_data_processor_info() {
       'class' => 'RulesDateOffsetProcessor',
       'type' => 'date',
       'weight' => -2,
-     ),
+    ),
   );
 }
 
@@ -564,10 +658,10 @@ function hook_rules_data_processor_info() {
  * @param $actions
  *   The items of all modules as returned from hook_rules_action_info().
  *
- * @see hook_rules_action_info().
+ * @see hook_rules_action_info()
  */
 function hook_rules_action_info_alter(&$actions) {
-  // The rules action is more powerful, so hide the core action
+  // The rules action is more powerful, so hide the core action.
   unset($actions['rules_core_node_assign_owner_action']);
   // We prefer handling saving by rules - not by the user.
   unset($actions['rules_core_node_save_action']);
@@ -597,7 +691,7 @@ function hook_rules_condition_info_alter(&$conditions) {
  * @param $events
  *   The items of all modules as returned from hook_rules_event_info().
  *
- * @see hook_rules_event_info().
+ * @see hook_rules_event_info()
  */
 function hook_rules_event_info_alter(&$events) {
   // Change events.
@@ -669,7 +763,7 @@ function hook_rules_data_processor_info_alter(&$processor_info) {
  * This hook is invoked during rules configuration loading, which is handled
  * by entity_load(), via classes RulesEntityController and EntityCRUDController.
  *
- * @param $configs
+ * @param array $configs
  *   An array of rules configurations being loaded, keyed by id.
  */
 function hook_rules_config_load($configs) {
@@ -707,7 +801,7 @@ function hook_rules_config_insert($config) {
  *   The rules configuration that is being inserted or updated.
  */
 function hook_rules_config_presave($config) {
-  if ($config->id && $config->module == 'yours') {
+  if ($config->id && $config->owner == 'your_module') {
     // Add custom condition.
     $config->conditon(/* Your condition */);
   }
@@ -763,7 +857,7 @@ function hook_rules_config_execute($config) {
  * should be placed into the file MODULENAME.rules_defaults.inc, which gets
  * automatically included when the hook is invoked.
  *
- * @return
+ * @return array
  *   An array of rules configurations with the configuration names as keys.
  *
  * @see hook_default_rules_configuration_alter()
@@ -772,6 +866,8 @@ function hook_rules_config_execute($config) {
 function hook_default_rules_configuration() {
   $rule = rules_reaction_rule();
   $rule->label = 'example default rule';
+  // Add rules tags.
+  $rule->tags = array('Admin', 'Tag2');
   $rule->active = FALSE;
   $rule->event('node_update')
        ->condition(rules_condition('data_is', array('data:select' => 'node:status', 'value' => TRUE))->negate())
@@ -779,6 +875,7 @@ function hook_default_rules_configuration() {
        ->action('drupal_message', array('message' => 'A node has been updated.'));
 
   $configs['rules_test_default_1'] = $rule;
+
   return $configs;
 }
 
@@ -806,10 +903,10 @@ function hook_default_rules_configuration_alter(&$configs) {
  * This hook is invoked by the entity module after default rules configurations
  * have been rebuilt; i.e. defaults have been saved to the database.
  *
- * @param $rules_configs
+ * @param array $rules_configs
  *   The array of default rules configurations which have been inserted or
  *   updated, keyed by name.
- * @param $originals
+ * @param array $originals
  *   An array of original rules configurations keyed by name; i.e. the rules
  *   configurations before the current defaults have been applied. For inserted
  *   rules configurations no original is available.
@@ -888,7 +985,8 @@ function hook_rules_event_set_alter($event_name, RulesEventSet $event_set) {
  * @param $element
  *   The element array of a configured condition or action which is to be
  *   upgraded.
- * @return
+ *
+ * @return string
  *   The name of the action or condition which should be used.
  */
 function hook_rules_action_base_upgrade_map_name($element) {
@@ -896,13 +994,13 @@ function hook_rules_action_base_upgrade_map_name($element) {
 }
 
 /**
- * D6 to D7 upgrade procedure hook for mapping action or condition configuration.
+ * D6 to D7 upgrade process hook for mapping action or condition configuration.
  *
  * During upgrading Drupal 6 rule configurations to Drupal 7 Rules is taking
  * care of upgrading the configuration of all known parameters, which only works
  * if the parameter name has not changed.
  * If something changed, this callback can be used to properly apply the
- * configruation of the Drupal 6 action ($element) to the Drupal 7 version
+ * configuration of the Drupal 6 action ($element) to the Drupal 7 version
  * ($target).
  *
  * This is no real hook, but a callback that is invoked for each Drupal 6
@@ -924,7 +1022,7 @@ function hook_rules_action_base_upgrade($element, RulesPlugin $target) {
 }
 
 /**
- * D6 to D7 upgrade procedure hook for mapping action or condition configuration.
+ * D6 to D7 upgrade process hook for mapping action or condition configuration.
  *
  * A alter hook that is called after the action/condition specific callback for
  * each element of a configuration that is upgraded.
@@ -967,6 +1065,39 @@ function hook_rules_ui_menu_alter(&$items, $base_path, $base_count) {
   );
 }
 
+/**
+ * Control access to Rules configurations.
+ *
+ * Modules may implement this hook if they want to have a say in whether or not
+ * a given user has access to perform a given operation on a Rules
+ * configuration.
+ *
+ * @param string $op
+ *   The operation being performed. One of 'view', 'create', 'update' or
+ *   'delete'.
+ * @param $rules_config
+ *   (optional) A Rules configuration to check access for. If nothing is given,
+ *   access for all Rules configurations is determined.
+ * @param $account
+ *   (optional) The user to check for. If no account is passed, access is
+ *   determined for the current user.
+ *
+ * @return bool|null
+ *   Return TRUE to grant access, FALSE to explicitly deny access. Return NULL
+ *   or nothing to not affect the operation.
+ *   Access is granted as soon as a module grants access and no one denies
+ *   access. Thus if no module explicitly grants access, access will be denied.
+ *
+ * @see rules_config_access()
+ */
+function hook_rules_config_access($op, $rules_config = NULL, $account = NULL) {
+  // Instead of returning FALSE return nothing, so others still can grant
+  // access.
+  if (isset($rules_config) && $rules_config->owner == 'mymodule' && user_access('my modules permission')) {
+    return TRUE;
+  }
+}
+
 /**
  * @}
  */

+ 252 - 0
sites/all/modules/rules/rules.drush.inc

@@ -0,0 +1,252 @@
+<?php
+
+/**
+ * @file
+ * Rules module drush integration.
+ */
+
+/**
+ * Implements hook_drush_command().
+ */
+function rules_drush_command() {
+  $items = array();
+
+  $items['rules-list'] = array(
+    'description' => 'List all the active and inactive rules for your site.',
+    'drupal dependencies' => array('rules'),
+    'aliases' => array('rules'),
+    'outputformat' => array(
+      'default' => 'table',
+      'pipe-format' => 'list',
+      'field-labels' => array(
+        'rule' => dt('Rule'),
+        'label' => dt('Label'),
+        'event' => dt('Event'),
+        'active' => dt('Active'),
+        'status' => dt('Status'),
+      ),
+      'output-data-type' => 'format-table',
+    ),
+  );
+  $items['rules-enable'] = array(
+    'description' => 'Enable a rule on your site.',
+    'arguments' => array(
+      'rule' => 'Rule name to enable.',
+    ),
+    'drupal dependencies' => array('rules'),
+    'aliases' => array('re'),
+  );
+  $items['rules-disable'] = array(
+    'description' => 'Disable a rule on your site.',
+    'arguments' => array(
+      'rule' => 'Rule name to export.',
+    ),
+    'drupal dependencies' => array('rules'),
+    'aliases' => array('rd'),
+  );
+  $items['rules-revert'] = array(
+    'description' => 'Revert a rule to its original state on your site.',
+    'arguments' => array(
+      'rule' => 'Rule name to revert.',
+    ),
+    'drupal dependencies' => array('rules'),
+  );
+  $items['rules-delete'] = array(
+    'description' => 'Delete a rule on your site.',
+    'arguments' => array(
+      'rule' => 'Rules name to delete.',
+    ),
+    'drupal dependencies' => array('rules'),
+  );
+  $items['rules-export'] = array(
+    'description' => 'Export a rule.',
+    'arguments' => array(
+      'rule' => 'Rules name to export.',
+    ),
+    'drupal dependencies' => array('rules'),
+  );
+
+  return $items;
+}
+
+/**
+ * Implements hook_drush_help().
+ */
+function rules_drush_help($section) {
+  switch ($section) {
+    case 'drush:rules-list':
+      return dt('List all the rules on your site.');
+
+    case 'drush:rules-enable':
+      return dt('Enable/activate a rule on your site.');
+
+    case 'drush:rules-disable':
+      return dt('Disable/deactivate a rule on your site.');
+
+    case 'drush:rules-revert':
+      return dt('Revert a module-provided rule to its original state on your site.');
+
+    case 'drush:rules-delete':
+      return dt('Delete a rule on your site.');
+
+    case 'drush:rules-export':
+      return dt('Export a rule.');
+  }
+}
+
+/**
+ * Get a list of all rules.
+ */
+function drush_rules_list() {
+  $rules = rules_config_load_multiple(FALSE);
+  $rows = array();
+  foreach ($rules as $rule) {
+    if (!empty($rule->name) && !empty($rule->label)) {
+      $events = array();
+      $event_info = rules_fetch_data('event_info');
+      if ($rule instanceof RulesTriggerableInterface) {
+        foreach ($rule->events() as $event_name) {
+          $event_info += array(
+            $event_name => array(
+              'label' => dt('Unknown event "!event_name"', array('!event_name' => $event_name)),
+            ),
+          );
+          $events[] = check_plain($event_info[$event_name]['label']);
+        }
+      }
+      $rows[$rule->name] = array(
+        'rule' => $rule->name,
+        'label' => $rule->label,
+        'event' => implode(', ', $events),
+        'active' => $rule->active ? dt('Enabled') : dt('Disabled'),
+        'status' => $rule->status ? theme('entity_status', array('status' => $rule->status, 'html' => FALSE)) : '',
+      );
+    }
+  }
+  if (version_compare(DRUSH_VERSION, '6.0', '<')) {
+    drush_print_table($rows, TRUE);
+  }
+  return $rows;
+}
+
+/**
+ * Enable a rule on the site.
+ */
+function drush_rules_enable() {
+  $args = func_get_args();
+  $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
+  if (empty($rule_name)) {
+    return drush_set_error('', 'No rule name given.');
+  }
+
+  $rule = rules_config_load($rule_name);
+  if (empty($rule)) {
+    return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
+  }
+
+  if (empty($rule->active)) {
+    $rule->active = TRUE;
+    $rule->save();
+    drush_log(dt('The rule "!name" has been enabled.', array('!name' => $rule_name)), 'success');
+  }
+  else {
+    drush_log(dt('The rule "!name" is already enabled.', array('!name' => $rule_name)), 'warning');
+  }
+}
+
+/**
+ * Disable a rule on the site.
+ */
+function drush_rules_disable() {
+  $args = func_get_args();
+  $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
+  if (empty($rule_name)) {
+    return drush_set_error('', 'No rule name given.');
+  }
+
+  $rule = rules_config_load($rule_name);
+  if (empty($rule)) {
+    return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
+  }
+
+  if (!empty($rule->active)) {
+    $rule->active = FALSE;
+    $rule->save();
+    drush_log(dt('The rule "!name" has been disabled.', array('!name' => $rule_name)), 'success');
+  }
+  else {
+    drush_log(dt('The rule "!name" is already disabled.', array('!name' => $rule_name)), 'warning');
+  }
+}
+
+/**
+ * Reverts a rule on the site.
+ */
+function drush_rules_revert() {
+  $args = func_get_args();
+  $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
+  if (empty($rule_name)) {
+    return drush_set_error('', 'No rule name given.');
+  }
+
+  $rule = rules_config_load($rule_name);
+  if (empty($rule)) {
+    return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
+  }
+
+  if (($rule->status & ENTITY_OVERRIDDEN) == ENTITY_OVERRIDDEN) {
+    if (drush_confirm(dt('Are you sure you want to revert the rule named "!rule-name"? This action cannot be undone.', array('!rule-name' => $rule_name)))) {
+      $rule->delete();
+      drush_log(dt('The rule "!name" has been reverted to its default state.', array('!name' => $rule_name)), 'success');
+    }
+    else {
+      drush_user_abort();
+    }
+  }
+  else {
+    drush_log(dt('The rule "!name" has not been overridden and can\'t be reverted.', array('!name' => $rule_name)), 'warning');
+  }
+}
+
+/**
+ * Deletes a rule on the site.
+ */
+function drush_rules_delete() {
+  $args = func_get_args();
+  $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
+  if (empty($rule_name)) {
+    return drush_set_error('', 'No rule name given.');
+  }
+
+  $rule = rules_config_load($rule_name);
+  if (empty($rule)) {
+    return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
+  }
+
+  if (drush_confirm(dt('Are you sure you want to delete the rule named "!rule-name"? This action cannot be undone.', array('!rule-name' => $rule_name)))) {
+    $rule->delete();
+    drush_log(dt('The rule "!name" has been deleted.', array('!name' => $rule_name)), 'success');
+  }
+  else {
+    drush_user_abort();
+  }
+}
+
+/**
+ * Exports a single rule.
+ */
+function drush_rules_export() {
+  $args = func_get_args();
+  $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
+  if (empty($rule_name)) {
+    return drush_set_error('', dt('No rule name given.'));
+  }
+
+  $rule = rules_config_load($rule_name);
+  if (empty($rule)) {
+    return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
+  }
+
+  drush_print($rule->export());
+  drush_log(dt('The rule "!name" has been exported.', array('!name' => $rule_name)), 'success');
+}

+ 30 - 11
sites/all/modules/rules/rules.features.inc

@@ -2,11 +2,11 @@
 
 /**
  * @file
- * Provides Features integration for the Rules module, based upon the features
- * integration provided by the Entity API.
+ * Provides Features integration for the Rules module.
+ *
+ * This code is based upon the features integration provided by the Entity API.
  */
 
-
 /**
  * Controller handling the features integration.
  */
@@ -24,7 +24,8 @@ class RulesFeaturesController extends EntityDefaultFeaturesController {
 
   /**
    * Generates the result for hook_features_export().
-   * Overridden to add in rules specific stuff.
+   *
+   * Overridden to add in rules-specific stuff.
    */
   public function export($data, &$export, $module_name = '') {
     $pipe = parent::export($data, $export, $module_name);
@@ -34,28 +35,32 @@ class RulesFeaturesController extends EntityDefaultFeaturesController {
       // Add in plugin / element specific additions.
       $iterator = new RecursiveIteratorIterator($rules_config, RecursiveIteratorIterator::SELF_FIRST);
       foreach ($iterator as $element) {
-        if ($element->facesAs('RulesPluginFeaturesIntegrationInterace')) {
-          // Directly use __call() so we cann pass $export by reference.
+        if ($element->facesAs('RulesPluginFeaturesIntegrationInterface')) {
+          // Directly use __call() so we can pass $export by reference.
           $element->__call('features_export', array(&$export, &$pipe, $module_name));
         }
       }
     }
     return $pipe;
   }
+
 }
 
 /**
  * Default extension callback used as default for the abstract plugin class.
- * Actions / conditions may override this with their own implementation, which
+ *
+ * Actions and conditions may override this with an implementation which
  * actually does something.
  *
- * @see RulesPluginFeaturesIntegrationInterace
+ * @see RulesPluginFeaturesIntegrationInterface
  */
 function rules_features_abstract_default_features_export(&$export, &$pipe, $module_name = '', $element) {
-
+  // Do nothing.
 }
 
 /**
+ * Interface to give features access to the faces extensions mechanism.
+ *
  * Interface that allows rules plugins or actions/conditions to customize the
  * features export by implementing the interface using the faces extensions
  * mechanism.
@@ -63,10 +68,24 @@ function rules_features_abstract_default_features_export(&$export, &$pipe, $modu
  * @see hook_rules_plugin_info()
  * @see hook_rules_action_info()
  */
-interface RulesPluginFeaturesIntegrationInterace {
+interface RulesPluginFeaturesIntegrationInterface {
 
   /**
    * Allows customizing the features export for a given rule element.
    */
-  function features_export(&$export, &$pipe, $module_name = '');
+  public function features_export(&$export, &$pipe, $module_name = '');
+
+}
+
+/**
+ * Interface for backwards compatibility with older versions of Rules.
+ *
+ * Mis-spelled interface provided so that contributed modules which were
+ * implementing the wrong spelling (corrected in Rules 7.x-2.12) will not stop
+ * working now that the interface is spelled correctly.
+ *
+ * @todo Remove this when we can be sure that no contributed modules are
+ * still using the wrong spelling.
+ */
+interface RulesPluginFeaturesIntegrationInterace extends RulesPluginFeaturesIntegrationInterface {
 }

+ 13 - 5
sites/all/modules/rules/rules.info

@@ -3,25 +3,33 @@ description = React on events and conditionally evaluate actions.
 package = Rules
 core = 7.x
 files[] = rules.features.inc
-files[] = tests/rules.test
 files[] = includes/faces.inc
 files[] = includes/rules.core.inc
+files[] = includes/rules.event.inc
 files[] = includes/rules.processor.inc
 files[] = includes/rules.plugins.inc
 files[] = includes/rules.state.inc
+files[] = modules/comment.rules.inc
+files[] = modules/node.eval.inc
+files[] = modules/node.rules.inc
 files[] = modules/php.eval.inc
 files[] = modules/rules_core.eval.inc
 files[] = modules/system.eval.inc
+files[] = modules/taxonomy.rules.inc
 files[] = ui/ui.controller.inc
 files[] = ui/ui.core.inc
 files[] = ui/ui.data.inc
 files[] = ui/ui.plugins.inc
+
+; Test cases
+files[] = tests/rules.test
+files[] = tests/rules_test.rules.inc
+
 dependencies[] = entity_token
 dependencies[] = entity
 
-; Information added by drupal.org packaging script on 2013-03-27
-version = "7.x-2.3"
+; Information added by Drupal.org packaging script on 2019-01-24
+version = "7.x-2.12"
 core = "7.x"
 project = "rules"
-datestamp = "1364401818"
-
+datestamp = "1548305586"

+ 127 - 6
sites/all/modules/rules/rules.install

@@ -1,16 +1,25 @@
 <?php
 
 /**
- * @file Rules - Installation file.
+ * @file
+ * Rules - Installation file.
  */
 
+/**
+ * Implements hook_enable().
+ */
+function rules_enable() {
+  // Enable evaluation of Rules right after enabling the module.
+  rules_event_invocation_enabled(TRUE);
+}
+
 /**
  * Implements hook_install().
  */
 function rules_install() {
   module_load_include('inc', 'rules', 'modules/events');
   // Set the modules' weight to 20, see
-  // http://drupal.org/node/445084#comment-1533280 for the reasoning.
+  // https://www.drupal.org/node/445084#comment-1533280 for the reasoning.
   db_query("UPDATE {system} SET weight = 20 WHERE name = 'rules'");
 }
 
@@ -18,8 +27,22 @@ function rules_install() {
  * Implements hook_uninstall().
  */
 function rules_uninstall() {
-  variable_del('rules_empty_sets');
   variable_del('rules_debug');
+  variable_del('rules_debug_log');
+  variable_del('rules_log_errors');
+  variable_del('rules_log_level');
+
+  variable_del('rules_clean_path');
+  variable_del('rules_path_cleaning_callback');
+  variable_del('rules_path_lower_case');
+  variable_del('rules_path_replacement_char');
+  variable_del('rules_path_transliteration');
+
+  // Delete all the debug region variables and then clear the variables cache.
+  db_delete('variable')
+    ->condition('name', 'rules_debug_region_%', 'LIKE')
+    ->execute();
+  cache_clear_all('variables', 'cache_bootstrap');
 }
 
 /**
@@ -46,7 +69,7 @@ function rules_schema() {
         'description' => 'The label of the configuration.',
         'default' => 'unlabeled',
       ),
-     'plugin' => array(
+      'plugin' => array(
         'type' => 'varchar',
         'length' => 127,
         'not null' => TRUE,
@@ -87,6 +110,13 @@ function rules_schema() {
         'length' => 255,
         'not null' => FALSE,
       ),
+      'owner' => array(
+        'description' => 'The name of the module via which the rule has been configured.',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => 'rules',
+      ),
       'access_exposed' => array(
         'type' => 'int',
         'not null' => TRUE,
@@ -107,7 +137,7 @@ function rules_schema() {
       'name' => array('name'),
     ),
     'indexes' => array(
-      'plugin' => array('plugin'),
+      'plugin' => array('plugin', 'active'),
     ),
   );
   $schema['rules_trigger'] = array(
@@ -207,7 +237,7 @@ function rules_update_7200() {
         'description' => 'The label of the configuration.',
         'default' => 'unlabeled',
       ),
-     'plugin' => array(
+      'plugin' => array(
         'type' => 'varchar',
         'length' => 127,
         'not null' => TRUE,
@@ -440,3 +470,94 @@ function rules_update_7209() {
     'description' => 'Whether to use a permission to control access for using components.',
   ));
 }
+
+/**
+ * Deletes the unused rules_empty_sets variable.
+ */
+function rules_update_7210() {
+  variable_del('rules_empty_sets');
+}
+
+/**
+ * Creates the "owner" column.
+ */
+function rules_update_7211() {
+  // Create a owner column.
+  if (!db_field_exists('rules_config', 'owner')) {
+    db_add_field('rules_config', 'owner', array(
+      'description' => 'The name of the module via which the rule has been configured.',
+      'type' => 'varchar',
+      'length' => 255,
+      'not null' => TRUE,
+      'default' => 'rules',
+    ));
+  }
+}
+
+/**
+ * Make sure registry gets rebuilt to avoid upgrade troubles.
+ */
+function rules_update_7212() {
+  // Make sure module information gets refreshed and registry is rebuilt.
+  drupal_static_reset('system_rebuild_module_data');
+  registry_rebuild();
+}
+
+/**
+ * Recover the "owner" property for broken configurations.
+ */
+function rules_update_7213() {
+  $rows = db_select('rules_config', 'c')
+    ->fields('c')
+    ->condition('status', ENTITY_OVERRIDDEN)
+    ->condition('owner', 'rules', '<>')
+    ->execute()
+    ->fetchAllAssoc('id');
+
+  foreach ($rows as $id => $row) {
+    if ($row->module == $row->owner) {
+      db_update('rules_config')
+        ->condition('id', $id)
+        ->fields(array('owner' => 'rules'))
+        ->execute();
+    }
+  }
+}
+
+/**
+ * Switch out the rules_event_whitelist variable for a cache equivalent.
+ */
+function rules_update_7214() {
+  // Enable Rules if currently disabled so that this update won't fail.
+  $disable_rules = FALSE;
+  if (!module_exists('rules')) {
+    module_enable(array('rules'));
+    $disable_rules = TRUE;
+  }
+  // Set new event_whitelist cache cid.
+  rules_set_cache('rules_event_whitelist', variable_get('rules_event_whitelist', array()));
+  // Delete old conf variable.
+  variable_del('rules_event_whitelist');
+  // Avoid any missing class errors.
+  registry_rebuild();
+  // Clear and rebuild Rules caches.
+  // See: rules_admin_settings_cache_rebuild_submit.
+  rules_clear_cache();
+  rules_get_cache();
+  _rules_rebuild_component_cache();
+  RulesEventSet::rebuildEventCache();
+  // Disable Rules again if it was disabled before this update started.
+  if ($disable_rules) {
+    module_disable(array('rules'));
+  }
+}
+
+/**
+ * Add an index for retrieving active config of a certain plugin.
+ */
+function rules_update_7215() {
+  if (db_index_exists('rules_config', 'plugin')) {
+    db_drop_index('rules_config', 'plugin');
+  }
+  db_add_index('rules_config', 'plugin', array('plugin', 'active'));
+}

+ 419 - 113
sites/all/modules/rules/rules.module

@@ -1,20 +1,66 @@
 <?php
 
 /**
- * @file Rules engine module
+ * @file
+ * Rules engine module.
  */
 
+// The class autoloader may fail for classes added in 7.x-2.4 (Issue 2090511).
+if (!drupal_autoload_class('RulesEventHandlerEntityBundle')) {
+  require_once dirname(__FILE__) . '/includes/rules.event.inc';
+}
+
+// Include our hook implementations early, as they can be called even before
+// hook_init().
+require_once dirname(__FILE__) . '/modules/events.inc';
+
+/**
+ * Implements hook_module_implements_alter().
+ */
+function rules_module_implements_alter(&$implementations, $hook) {
+  // Ensures the invocation of hook_menu_get_item_alter() triggers
+  // rules_menu_get_item_alter() first so the rules invocation is ready for all
+  // sub-sequent hook implementations.
+  if ($hook == 'menu_get_item_alter' && array_key_exists('rules', $implementations)) {
+    $group = $implementations['rules'];
+    unset($implementations['rules']);
+    $implementations = array_merge(array('rules' => $group), $implementations);
+  }
+}
+
+/**
+ * Implements hook_menu_get_item_alter().
+ */
+function rules_menu_get_item_alter() {
+  // Make sure that event invocation is enabled before menu items are loaded.
+  // But make sure later calls to menu_get_item() won't automatically re-enabled
+  // the rules invocation.
+  // Example: modules that implement hook_entity_ENTITY_TYPE_load() might want
+  // to invoke Rules events in that load hook, which is also invoked for menu
+  // item loading. Since this can happen even before hook_init() we need to make
+  // sure that firing Rules events is enabled at that point. A typical use case
+  // for this is Drupal Commerce with commerce_cart_commerce_order_load().
+  if (!drupal_static('rules_init', FALSE)) {
+    rules_event_invocation_enabled(TRUE);
+  }
+}
+
 /**
  * Implements hook_init().
  */
 function rules_init() {
-  module_load_include('inc', 'rules', 'modules/events');
+  // See rules_menu_get_item_alter().
+  $rules_init = &drupal_static(__FUNCTION__, FALSE);
+  $rules_init = TRUE;
+  // Enable event invocation once hook_init() was invoked for Rules.
+  rules_event_invocation_enabled(TRUE);
   rules_invoke_event('init');
 }
 
 /**
- * Returns an instance of the rules UI controller, which eases re-using the Rules UI.
+ * Returns an instance of the rules UI controller.
  *
+ * This function is for convenience, to ease re-using the Rules UI.
  * See the rules_admin.module for example usage.
  *
  * @return RulesUIController
@@ -32,8 +78,9 @@ function rules_ui() {
  *
  * @param $name
  *   The action's name.
- * @param $settings
+ * @param array $settings
  *   The action's settings array.
+ *
  * @return RulesAction
  */
 function rules_action($name, $settings = array()) {
@@ -45,8 +92,9 @@ function rules_action($name, $settings = array()) {
  *
  * @param $name
  *   The condition's name.
- * @param $settings
+ * @param array $settings
  *   The condition's settings array.
+ *
  * @return RulesCondition
  */
 function rules_condition($name, $settings = array()) {
@@ -56,9 +104,9 @@ function rules_condition($name, $settings = array()) {
 /**
  * Creates a new rule.
  *
- * @param $variables
+ * @param array $variables
  *   The array of variables to setup in the evaluation state, making them
- *   available for the configuraion elements. Values for the variables need to
+ *   available for the configuration elements. Values for the variables need to
  *   be passed as argument when the rule is executed. Only Rule instances with
  *   no variables can be embedded in other configurations, e.g. rule sets.
  *   The array has to be keyed by variable name and contain a sub-array for each
@@ -71,9 +119,10 @@ function rules_condition($name, $settings = array()) {
  *      initially, but the "Set data value" action may be used to do so. This is
  *      in particular useful for defining variables which can be provided to the
  *      caller (see $provides argument) but need not be passed in as parameter.
- * @param $provides
+ * @param array $provides
  *   The names of variables which should be provided to the caller. Only
  *   variables contained in $variables may be specified.
+ *
  * @return Rule
  */
 function rule($variables = NULL, $provides = array()) {
@@ -92,8 +141,9 @@ function rules_reaction_rule() {
 /**
  * Creates a logical OR condition container.
  *
- * @param $variables
+ * @param array $variables
  *   An optional array as for rule().
+ *
  * @return RulesOr
  */
 function rules_or($variables = NULL) {
@@ -103,8 +153,9 @@ function rules_or($variables = NULL) {
 /**
  * Creates a logical AND condition container.
  *
- * @param $variables
+ * @param array $variables
  *   An optional array as for rule().
+ *
  * @return RulesAnd
  */
 function rules_and($variables = NULL) {
@@ -114,13 +165,14 @@ function rules_and($variables = NULL) {
 /**
  * Creates a loop.
  *
- * @param $settings
+ * @param array $settings
  *   The loop settings, containing
  *     'list:select': The data selector for the list to loop over.
  *     'item:var': Optionally a name for the list item variable.
- *     'item:label': Optionally a lebel for the list item variable.
- * @param $variables
+ *     'item:label': Optionally a label for the list item variable.
+ * @param array $variables
  *   An optional array as for rule().
+ *
  * @return RulesLoop
  */
 function rules_loop($settings = array(), $variables = NULL) {
@@ -130,10 +182,11 @@ function rules_loop($settings = array(), $variables = NULL) {
 /**
  * Creates a rule set.
  *
- * @param $variables
+ * @param array $variables
  *   An array as for rule().
- * @param $provides
+ * @param array $provides
  *   The names of variables which should be provided to the caller. See rule().
+ *
  * @return RulesRuleSet
  */
 function rules_rule_set($variables = array(), $provides = array()) {
@@ -143,10 +196,11 @@ function rules_rule_set($variables = array(), $provides = array()) {
 /**
  * Creates an action set.
  *
- * @param $variables
+ * @param array $variables
  *   An array as for rule().
- * @param $provides
+ * @param array $provides
  *   The names of variables which should be provided to the caller. See rule().
+ *
  * @return RulesActionSet
  */
 function rules_action_set($variables = array(), $provides = array()) {
@@ -158,15 +212,15 @@ function rules_action_set($variables = array(), $provides = array()) {
  *
  * @param $msg
  *   The message to log.
- * @param $args
+ * @param array $args
  *   An array of placeholder arguments as used by t().
  * @param $priority
  *   A priority as defined by the RulesLog class.
  * @param RulesPlugin $element
- *  (optional) The RulesElement causing the log entry.
- * @param boolean $scope
- *  (optional) This may be used to denote the beginning (TRUE) or the end
- *  (FALSE) of a new execution scope.
+ *   (optional) The RulesElement causing the log entry.
+ * @param bool $scope
+ *   (optional) This may be used to denote the beginning (TRUE) or the end
+ *   (FALSE) of a new execution scope.
  */
 function rules_log($msg, $args = array(), $priority = RulesLog::INFO, RulesPlugin $element = NULL, $scope = NULL) {
   static $logger, $settings;
@@ -183,7 +237,11 @@ function rules_log($msg, $args = array(), $priority = RulesLog::INFO, RulesPlugi
     if (isset($element) && isset($element->root()->name)) {
       $link = l(t('edit configuration'), RulesPluginUI::path($element->root()->name, 'edit', $element));
     }
+    // Disabled rules invocation to avoid an endless loop when using
+    // watchdog - which would trigger a rules event.
+    rules_event_invocation_enabled(FALSE);
     watchdog('rules', $msg, $args, $priority == RulesLog::WARN ? WATCHDOG_WARNING : WATCHDOG_ERROR, $link);
+    rules_event_invocation_enabled(TRUE);
   }
   // Do nothing in case debugging is totally disabled.
   if (!$settings['rules_debug_log'] && !$settings['rules_debug']) {
@@ -199,15 +257,21 @@ function rules_log($msg, $args = array(), $priority = RulesLog::INFO, RulesPlugi
 /**
  * Fetches module definitions for the given hook name.
  *
- * Used for collecting events, rules, actions and condtions from other modules.
+ * Used for collecting events, rules, actions and condition from other modules.
  *
  * @param $hook
  *   The hook of the definitions to get from invoking hook_rules_{$hook}.
  */
 function rules_fetch_data($hook) {
   $data = &drupal_static(__FUNCTION__, array());
+  static $discover = array(
+    'action_info' => 'RulesActionHandlerInterface',
+    'condition_info' => 'RulesConditionHandlerInterface',
+    'event_info' => 'RulesEventHandlerInterface',
+  );
 
   if (!isset($data[$hook])) {
+    $data[$hook] = array();
     foreach (module_implements('rules_' . $hook) as $module) {
       $result = call_user_func($module . '_rules_' . $hook);
       if (isset($result) && is_array($result)) {
@@ -217,11 +281,90 @@ function rules_fetch_data($hook) {
         }
       }
     }
-    drupal_alter('rules_'. $hook, $data[$hook]);
+    // Support class discovery.
+    if (isset($discover[$hook])) {
+      $data[$hook] += rules_discover_plugins($discover[$hook]);
+    }
+    drupal_alter('rules_' . $hook, $data[$hook]);
   }
   return $data[$hook];
 }
 
+/**
+ * Discover plugin implementations.
+ *
+ * Class based plugin handlers must be loaded when rules caches are rebuilt,
+ * such that they get discovered properly. You have the following options:
+ *  - Put it into a regular module file (discouraged)
+ *  - Put it into your module.rules.inc file
+ *  - Put it in any file and declare it using hook_rules_file_info()
+ *  - Put it in any file and declare it using hook_rules_directory()
+ *
+ * In addition to that, the class must be loadable via regular class
+ * auto-loading, thus put the file holding the class in your info file or use
+ * another class-loader.
+ *
+ * @param string $class
+ *   The class or interface the plugins must implement. For a plugin to be
+ *   discovered it must have a static getInfo() method also.
+ *
+ * @return array
+ *   An info-hook style array containing info about discovered plugins.
+ *
+ * @see RulesActionHandlerInterface
+ * @see RulesConditionHandlerInterface
+ * @see RulesEventHandlerInterface
+ */
+function rules_discover_plugins($class) {
+  // Make sure all files possibly holding plugins are included.
+  RulesAbstractPlugin::includeFiles();
+
+  $items = array();
+  foreach (get_declared_classes() as $plugin_class) {
+    if (is_subclass_of($plugin_class, $class) && method_exists($plugin_class, 'getInfo')) {
+      $info = call_user_func(array($plugin_class, 'getInfo'));
+      $info['class'] = $plugin_class;
+      $info['module'] = _rules_discover_module($plugin_class);
+      $items[$info['name']] = $info;
+    }
+  }
+  return $items;
+}
+
+/**
+ * Determines the module providing the given class.
+ *
+ * @param string $class
+ *   The name of the class or interface plugins to discover.
+ *
+ * @return string|false
+ *   The path of the class, relative to the Drupal installation root,
+ *   or FALSE if not discovered.
+ */
+function _rules_discover_module($class) {
+  $paths = &drupal_static(__FUNCTION__);
+
+  if (!isset($paths)) {
+    // Build up a map of modules keyed by their directory.
+    foreach (system_list('module_enabled') as $name => $module_info) {
+      $paths[dirname($module_info->filename)] = $name;
+    }
+  }
+
+  // Retrieve the class file and convert its absolute path to a regular Drupal
+  // path relative to the installation root.
+  $reflection = new ReflectionClass($class);
+  $path = str_replace(realpath(DRUPAL_ROOT) . DIRECTORY_SEPARATOR, '', realpath(dirname($reflection->getFileName())));
+  $path = DIRECTORY_SEPARATOR != '/' ? str_replace(DIRECTORY_SEPARATOR, '/', $path) : $path;
+
+  // Go up the path until we match a module.
+  $parts = explode('/', $path);
+  while (!isset($paths[$path]) && array_pop($parts)) {
+    $path = dirname($path);
+  }
+  return isset($paths[$path]) ? $paths[$path] : FALSE;
+}
+
 /**
  * Gets a rules cache entry.
  */
@@ -241,24 +384,39 @@ function &rules_get_cache($cid = 'data') {
     if ($get = cache_get($cid . $cid_suffix, 'cache_rules')) {
       $cache[$cid] = $get->data;
     }
-    elseif ($cid === 'data') {
-      // There is no 'data' cache so we need to rebuild it. Make sure subsequent
-      // cache gets of the main 'data' cache during rebuild get the interim
-      // cache by passing in the reference of the static cache variable.
-      _rules_rebuild_cache($cache['data']);
-    }
-    elseif (strpos($cid, 'comp_') === 0) {
-      $cache[$cid] = FALSE;
-      _rules_rebuild_component_cache();
-      return $cache[$cid];
-    }
-    elseif (strpos($cid, 'event_') === 0) {
-      $cache[$cid] = FALSE;
-      RulesEventSet::rebuildEventCache();
-      return $cache[$cid];
-    }
     else {
-      $cache[$cid] = FALSE;
+      // Prevent stampeding by ensuring the cache is rebuilt just once at the
+      // same time.
+      while (!lock_acquire(__FUNCTION__ . $cid . $cid_suffix, 60)) {
+        // Now wait until the lock is released.
+        lock_wait(__FUNCTION__ . $cid . $cid_suffix, 30);
+        // If the lock is released it's likely the cache was rebuild. Thus check
+        // again if we can fetch it from the persistent cache.
+        if ($get = cache_get($cid . $cid_suffix, 'cache_rules')) {
+          $cache[$cid] = $get->data;
+          return $cache[$cid];
+        }
+      }
+      if ($cid === 'data') {
+        // There is no 'data' cache so we need to rebuild it. Make sure
+        // subsequent cache gets of the main 'data' cache during rebuild get
+        // the interim cache by passing in the reference of the static cache
+        // variable.
+        _rules_rebuild_cache($cache['data']);
+      }
+      elseif (strpos($cid, 'comp_') === 0) {
+        $cache[$cid] = FALSE;
+        _rules_rebuild_component_cache();
+      }
+      elseif (strpos($cid, 'event_') === 0 || $cid == 'rules_event_whitelist') {
+        $cache[$cid] = FALSE;
+        RulesEventSet::rebuildEventCache();
+      }
+      else {
+        $cache[$cid] = FALSE;
+      }
+      // Ensure a set lock is released.
+      lock_release(__FUNCTION__ . $cid . $cid_suffix);
     }
   }
   return $cache[$cid];
@@ -279,7 +437,7 @@ function &rules_get_cache($cid = 'data') {
  * @see entity_defaults_rebuild()
  */
 function _rules_rebuild_cache(&$cache) {
-  foreach(array('data_info', 'plugin_info') as $hook) {
+  foreach (array('data_info', 'plugin_info') as $hook) {
     $cache[$hook] = rules_fetch_data($hook);
   }
   foreach ($cache['plugin_info'] as $name => &$info) {
@@ -322,7 +480,7 @@ function _rules_rebuild_component_cache() {
  *
  * In addition to calling cache_set(), this function makes sure the cache item
  * is immediately available via rules_get_cache() by keeping all cache items
- * in memory. That way we can garantuee rules_get_cache() is able to retrieve
+ * in memory. That way we can guarantee rules_get_cache() is able to retrieve
  * any cache item, even if all cache gets fail.
  *
  * @see rules_get_cache()
@@ -337,16 +495,14 @@ function rules_set_cache($cid, $data) {
  * Implements hook_flush_caches().
  */
 function rules_flush_caches() {
-  variable_del('rules_empty_sets');
   return array('cache_rules');
 }
 
 /**
- * Clears the rule set cache
+ * Clears the rule set cache.
  */
 function rules_clear_cache() {
   cache_clear_all('*', 'cache_rules', TRUE);
-  variable_del('rules_empty_sets');
   drupal_static_reset('rules_get_cache');
   drupal_static_reset('rules_fetch_data');
   drupal_static_reset('rules_config_update_dirty_flag');
@@ -356,16 +512,17 @@ function rules_clear_cache() {
 /**
  * Imports the given export and returns the imported configuration.
  *
- * @param $export
+ * @param string $export
  *   A serialized string in JSON format as produced by the RulesPlugin::export()
  *   method, or the PHP export as usual PHP array.
+ * @param string $error_msg
+ *
  * @return RulesPlugin
  */
 function rules_import($export, &$error_msg = '') {
   return entity_get_controller('rules_config')->import($export, $error_msg);
 }
 
-
 /**
  * Wraps the given data.
  *
@@ -373,9 +530,10 @@ function rules_import($export, &$error_msg = '') {
  *   If available, the actual data, else NULL.
  * @param $info
  *   An array of info about this data.
- * @param $force
+ * @param bool $force
  *   Usually data is only wrapped if really needed. If set to TRUE, wrapping the
  *   data is forced, so primitive data types are also wrapped.
+ *
  * @return EntityMetadataWrapper
  *   An EntityMetadataWrapper or the unwrapped data.
  *
@@ -416,11 +574,12 @@ function &rules_wrap_data($data = NULL, $info, $force = FALSE) {
 /**
  * Unwraps the given data, if it's wrapped.
  *
- * @param $data
+ * @param array $data
  *   An array of wrapped data.
- * @param $info
+ * @param array $info
  *   Optionally an array of info about how to unwrap the data. Keyed as $data.
- * @return
+ *
+ * @return array
  *   An array containing unwrapped or passed through data.
  */
 function rules_unwrap_data(array $data, $info = array()) {
@@ -460,6 +619,70 @@ function rules_unwrap_data(array $data, $info = array()) {
   return $data;
 }
 
+/**
+ * Gets event info for a given event.
+ *
+ * @param string $event_name
+ *   A (configured) event name.
+ *
+ * @return array
+ *   An array of event info. If the event is unknown, a suiting info array is
+ *   generated and returned
+ */
+function rules_get_event_info($event_name) {
+  $base_event_name = rules_get_event_base_name($event_name);
+  $events = rules_fetch_data('event_info');
+  if (isset($events[$base_event_name])) {
+    return $events[$base_event_name] + array('name' => $base_event_name);
+  }
+  return array(
+    'label' => t('Unknown event "!event_name"', array('!event_name' => $base_event_name)),
+    'name' => $base_event_name,
+  );
+}
+
+/**
+ * Returns the base name of a configured event name.
+ *
+ * For a configured event name like node_view--article the base event name
+ * node_view is returned.
+ *
+ * @param string $event_name
+ *   A (configured) event name.
+ *
+ * @return string
+ *   The event base name.
+ */
+function rules_get_event_base_name($event_name) {
+  // Cut off any suffix from a configured event name.
+  if (strpos($event_name, '--') !== FALSE) {
+    $parts = explode('--', $event_name, 2);
+    return $parts[0];
+  }
+  return $event_name;
+}
+
+/**
+ * Returns the rule event handler for the given event.
+ *
+ * Events having no settings are handled via the class RulesEventSettingsNone.
+ *
+ * @param string $event_name
+ *   The event name (base or configured).
+ * @param array $settings
+ *   (optional) An array of event settings to set on the handler.
+ *
+ * @return RulesEventHandlerInterface
+ *   The event handler.
+ */
+function rules_get_event_handler($event_name, array $settings = NULL) {
+  $event_name = rules_get_event_base_name($event_name);
+  $event_info = rules_get_event_info($event_name);
+  $class = !empty($event_info['class']) ? $event_info['class'] : 'RulesEventDefaultHandler';
+  $handler = new $class($event_name, $event_info);
+  return isset($settings) ? $handler->setSettings($settings) : $handler;
+}
+
 /**
  * Creates a new instance of a the given rules plugin.
  *
@@ -473,7 +696,7 @@ function rules_plugin_factory($plugin_name, $arg1 = NULL, $arg2 = NULL) {
 }
 
 /**
- * Implementation of hook_rules_plugin_info().
+ * Implements hook_rules_plugin_info().
  *
  * Note that the cache is rebuilt in the order of the plugins. Therefore the
  * condition and action plugins must be at the top, so that any components
@@ -485,11 +708,11 @@ function rules_rules_plugin_info() {
     'condition' => array(
       'class' => 'RulesCondition',
       'embeddable' => 'RulesConditionContainer',
-      'extenders' => array (
+      'extenders' => array(
         'RulesPluginImplInterface' => array(
           'class' => 'RulesAbstractPluginDefaults',
         ),
-        'RulesPluginFeaturesIntegrationInterace' => array(
+        'RulesPluginFeaturesIntegrationInterface' => array(
           'methods' => array(
             'features_export' => 'rules_features_abstract_default_features_export',
           ),
@@ -502,11 +725,11 @@ function rules_rules_plugin_info() {
     'action' => array(
       'class' => 'RulesAction',
       'embeddable' => 'RulesActionContainer',
-      'extenders' => array (
+      'extenders' => array(
         'RulesPluginImplInterface' => array(
           'class' => 'RulesAbstractPluginDefaults',
         ),
-        'RulesPluginFeaturesIntegrationInterace' => array(
+        'RulesPluginFeaturesIntegrationInterface' => array(
           'methods' => array(
             'features_export' => 'rules_features_abstract_default_features_export',
           ),
@@ -598,7 +821,7 @@ function rules_rules_plugin_info() {
 }
 
 /**
- * Implementation of hook_entity_info().
+ * Implements hook_entity_info().
  */
 function rules_entity_info() {
   return array(
@@ -627,10 +850,10 @@ function rules_entity_info() {
 }
 
 /**
- * Implementation of hook_hook_info().
+ * Implements hook_hook_info().
  */
 function rules_hook_info() {
-  foreach(array('plugin_info', 'data_info', 'condition_info', 'action_info', 'event_info', 'file_info', 'evaluator_info', 'data_processor_info') as $hook) {
+  foreach (array('plugin_info', 'rules_directory', 'data_info', 'condition_info', 'action_info', 'event_info', 'file_info', 'evaluator_info', 'data_processor_info') as $hook) {
     $hooks['rules_' . $hook] = array(
       'group' => 'rules',
     );
@@ -657,12 +880,12 @@ function rules_hook_info() {
  * @see hook_entity_info()
  * @see RulesEntityController
  *
- * @param $names
+ * @param array|false $names
  *   An array of rules configuration names or FALSE to load all.
- * @param $conditions
+ * @param array $conditions
  *   An array of conditions in the form 'field' => $value.
  *
- * @return
+ * @return array
  *   An array of rule configurations indexed by their ids.
  */
 function rules_config_load_multiple($names = array(), $conditions = array()) {
@@ -690,10 +913,10 @@ function rules_config_load($name) {
  *   Whether to return only the label or the whole component object.
  * @param $type
  *   Optionally filter for 'action' or 'condition' components.
- * @param $conditions
+ * @param array $conditions
  *   An array of additional conditions as required by rules_config_load().
  *
- * @return
+ * @return array
  *   An array keyed by component name containing either the label or the config.
  */
 function rules_get_components($label = FALSE, $type = NULL, $conditions = array()) {
@@ -716,7 +939,7 @@ function rules_get_components($label = FALSE, $type = NULL, $conditions = array(
 /**
  * Delete rule configurations from database.
  *
- * @param $ids
+ * @param array $ids
  *   An array of entity IDs.
  */
 function rules_config_delete(array $ids) {
@@ -726,13 +949,13 @@ function rules_config_delete(array $ids) {
 /**
  * Ensures the configuration's 'dirty' flag is up to date by running an integrity check.
  *
- * @param $update
+ * @param bool $update
  *   (optional) Whether the dirty flag is also updated in the database if
  *   necessary. Defaults to TRUE.
  */
 function rules_config_update_dirty_flag($rules_config, $update = TRUE) {
   // Keep a log of already check configurations to avoid repetitive checks on
-  // oftent used components.
+  // often used components.
   // @see rules_element_invoke_component_validate()
   $checked = &drupal_static(__FUNCTION__, array());
   if (!empty($checked[$rules_config->name])) {
@@ -779,7 +1002,7 @@ function rules_config_update_dirty_flag($rules_config, $update = TRUE) {
  * @param ...
  *   Arguments to pass to the hook / event.
  *
- * @return
+ * @return array
  *   An array of return values of the hook implementations. If modules return
  *   arrays from their implementations, those are merged into one array.
  */
@@ -822,15 +1045,16 @@ function rules_invoke_all() {
  * @see rules_invoke_event_by_args()
  */
 function rules_invoke_event() {
-  global $conf;
-
   $args = func_get_args();
   $event_name = $args[0];
   unset($args[0]);
-  // For invoking the rules event we directly acccess the global $conf. This is
-  // fast without having to introduce another static cache.
-  if (!defined('MAINTENANCE_MODE') && !isset($conf['rules_empty_sets'][$event_name]) && $event = rules_get_cache('event_' . $event_name)) {
-    $event->executeByArgs($args);
+  // We maintain a whitelist of configured events to reduces the number of cache
+  // reads. If the whitelist is not in the cache we proceed and it is rebuilt.
+  if (rules_event_invocation_enabled()) {
+    $whitelist = rules_get_cache('rules_event_whitelist');
+    if ((($whitelist === FALSE) || isset($whitelist[$event_name])) && $event = rules_get_cache('event_' . $event_name)) {
+      $event->executeByArgs($args);
+    }
   }
 }
 
@@ -839,7 +1063,7 @@ function rules_invoke_event() {
  *
  * @param $event_name
  *   The event's name.
- * @param $args
+ * @param array $args
  *   An array of parameters for the variables provided by the event, as defined
  *   in hook_rules_event_info(). Either pass an array keyed by the variable
  *   names or a numerically indexed array, in which case the ordering of the
@@ -852,12 +1076,13 @@ function rules_invoke_event() {
  * @see rules_invoke_event()
  */
 function rules_invoke_event_by_args($event_name, $args = array()) {
-  global $conf;
-
-  // For invoking the rules event we directly acccess the global $conf. This is
-  // fast without having to introduce another static cache.
-  if (!defined('MAINTENANCE_MODE') && !isset($conf['rules_empty_sets'][$event_name]) && $event = rules_get_cache('event_' . $event_name)) {
-    $event->executeByArgs($args);
+  // We maintain a whitelist of configured events to reduces the number of cache
+  // reads. If the whitelist is empty we proceed and it is rebuilt.
+  if (rules_event_invocation_enabled()) {
+    $whitelist = rules_get_cache('rules_event_whitelist');
+    if ((empty($whitelist) || isset($whitelist[$event_name])) && $event = rules_get_cache('event_' . $event_name)) {
+      $event->executeByArgs($args);
+    }
   }
 }
 
@@ -869,7 +1094,7 @@ function rules_invoke_event_by_args($event_name, $args = array()) {
  * @param $args
  *   Pass further parameters as required for the invoked component.
  *
- * @return
+ * @return array
  *   An array of variables as provided by the component, or FALSE in case the
  *   component could not be executed.
  */
@@ -883,10 +1108,12 @@ function rules_invoke_component() {
 }
 
 /**
- * Filters the given array of arrays by keeping only entries which have $key set
- * to the value of $value.
+ * Filters the given array of arrays.
+ *
+ * This filter operates by keeping only entries which have $key set to the
+ * value of $value.
  *
- * @param $array
+ * @param array $array
  *   The array of arrays to filter.
  * @param $key
  *   The key used for the comparison.
@@ -908,10 +1135,11 @@ function rules_filter_array($array, $key, $value) {
 }
 
 /**
- * Merges the $update array into $array making sure no values of $array not
- * appearing in $update are lost.
+ * Merges the $update array into $array.
+ *
+ * Makes sure no values of $array not appearing in $update are lost.
  *
- * @return
+ * @return array
  *   The updated array.
  */
 function rules_update_array(array $array, array $update) {
@@ -929,12 +1157,13 @@ function rules_update_array(array $array, array $update) {
 /**
  * Extracts the property with the given name.
  *
- * @param $arrays
+ * @param array $arrays
  *   An array of arrays from which a property is to be extracted.
  * @param $key
  *   The name of the property to extract.
  *
- * @return An array of extracted properties, keyed as in $arrays-
+ * @return array
+ *   An array of extracted properties, keyed as in $arrays.
  */
 function rules_extract_property($arrays, $key) {
   $data = array();
@@ -959,9 +1188,9 @@ function rules_array_key($array) {
  *
  * @param $replacements
  *   An array of token replacements that need to be "cleaned" for use in the URL.
- * @param $data
+ * @param array $data
  *   An array of objects used to generate the replacements.
- * @param $options
+ * @param array $options
  *   An array of options used to generate the replacements.
  *
  * @see rules_path_action_info()
@@ -1068,6 +1297,7 @@ function rules_permissions_by_component(array $components = array()) {
 
 /**
  * Menu callback for loading rules configuration elements.
+ *
  * @see RulesUIController::config_menu()
  */
 function rules_element_load($element_id, $config_name) {
@@ -1077,6 +1307,7 @@ function rules_element_load($element_id, $config_name) {
 
 /**
  * Menu callback for getting the title as configured.
+ *
  * @see RulesUIController::config_menu()
  */
 function rules_get_title($text, $element) {
@@ -1095,6 +1326,7 @@ function rules_get_title($text, $element) {
  * Menu callback for getting the title for the add element page.
  *
  * Uses a work-a-round for accessing the plugin name.
+ *
  * @see RulesUIController::config_menu()
  */
 function rules_menu_add_element_title($array) {
@@ -1187,17 +1419,24 @@ function rules_drupal_goto_alter(&$path, &$options, &$http_response_code) {
  * Returns whether the debug log should be shown.
  */
 function rules_show_debug_output() {
-  if (variable_get('rules_debug', FALSE) == RulesLog::INFO && user_access('access rules debug')) {
+  // For performance avoid unnecessary auto-loading of the RulesLog class.
+  if (!class_exists('RulesLog', FALSE)) {
+    return FALSE;
+  }
+  if (variable_get('rules_debug', 0) == RulesLog::INFO && user_access('access rules debug')) {
     return TRUE;
   }
-  // For performance avoid unnecessary auto-loading of the RulesLog class.
-  return variable_get('rules_debug', FALSE) == RulesLog::WARN && user_access('access rules debug') && class_exists('RulesLog', FALSE) && RulesLog::logger()->hasErrors();
+  return variable_get('rules_debug', 0) == RulesLog::WARN && user_access('access rules debug') && RulesLog::logger()->hasErrors();
 }
 
 /**
  * Implements hook_exit().
  */
 function rules_exit() {
+  // Bail out if this is cached request and modules are not loaded.
+  if (!module_exists('rules') || !module_exists('user')) {
+    return;
+  }
   if (rules_show_debug_output()) {
     if ($log = RulesLog::logger()->render()) {
       // Keep the log in the session so we can show it on the next page.
@@ -1294,11 +1533,13 @@ function rules_modules_disabled($modules) {
       ->fields('r')
       ->condition('id', $ids, 'IN')
       ->condition('active', 1)
-      ->execute()->rowCount();
+      ->countQuery()
+      ->execute()
+      ->fetchField();
     if ($count > 0) {
       $message = format_plural($count,
         '1 Rules configuration requires some of the disabled modules to function and cannot be executed any more.',
-        '@count Rules configuration require some of the disabled modules to function and cannot be executed any more.'
+        '@count Rules configurations require some of the disabled modules to function and cannot be executed any more.'
       );
       drupal_set_message($message, 'warning');
     }
@@ -1315,10 +1556,32 @@ function rules_config_access($op, $rules_config = NULL, $account = NULL) {
   if (user_access('bypass rules access', $account)) {
     return TRUE;
   }
-  if (!isset($rules_config) || (isset($account) && $account->uid != $GLOBALS['user']->uid)) {
+  // Allow modules to grant / deny access.
+  $access = module_invoke_all('rules_config_access', $op, $rules_config, $account);
+
+  // Only grant access if at least one module granted access and no one denied
+  // access.
+  if (in_array(FALSE, $access, TRUE)) {
     return FALSE;
   }
-  return user_access('administer rules', $account) && ($op == 'view' || $rules_config->access());
+  elseif (in_array(TRUE, $access, TRUE)) {
+    return TRUE;
+  }
+  return FALSE;
+}
+
+/**
+ * Implements hook_rules_config_access().
+ */
+function rules_rules_config_access($op, $rules_config = NULL, $account = NULL) {
+  // Instead of returning FALSE return nothing, so others still can grant
+  // access.
+  if (!isset($rules_config) || (isset($account) && $account->uid != $GLOBALS['user']->uid)) {
+    return;
+  }
+  if (user_access('administer rules', $account) && ($op == 'view' || $rules_config->access())) {
+    return TRUE;
+  }
 }
 
 /**
@@ -1357,25 +1620,25 @@ function rules_menu() {
 /**
  * Helper function to keep track of external documentation pages for Rules.
  *
- * @param $topic
+ * @param string $topic
  *   The topic key for used for identifying help pages.
  *
- * @return
+ * @return string|array|false
  *   Either a URL for the given page, or the full list of external help pages.
  */
 function rules_external_help($topic = NULL) {
   $help = array(
-    'rules' =>                'http://drupal.org/node/298480',
-    'terminology' =>          'http://drupal.org/node/1299990',
-    'condition-components' => 'http://drupal.org/node/1300034',
-    'data-selection' =>       'http://drupal.org/node/1300042',
-    'chained-tokens' =>       'http://drupal.org/node/1300042',
-    'loops' =>                'http://drupal.org/node/1300058',
-    'components' =>           'http://drupal.org/node/1300024',
-    'component-types' =>      'http://drupal.org/node/1300024',
-    'variables' =>            'http://drupal.org/node/1300024',
-    'scheduler' =>            'http://drupal.org/node/1300068',
-    'coding' =>               'http://drupal.org/node/878720',
+    'rules' =>                'https://www.drupal.org/node/298480',
+    'terminology' =>          'https://www.drupal.org/node/1299990',
+    'condition-components' => 'https://www.drupal.org/node/1300034',
+    'data-selection' =>       'https://www.drupal.org/node/1300042',
+    'chained-tokens' =>       'https://www.drupal.org/node/1300042',
+    'loops' =>                'https://www.drupal.org/node/1300058',
+    'components' =>           'https://www.drupal.org/node/1300024',
+    'component-types' =>      'https://www.drupal.org/node/1300024',
+    'variables' =>            'https://www.drupal.org/node/1300024',
+    'scheduler' =>            'https://www.drupal.org/node/1300068',
+    'coding' =>               'https://www.drupal.org/node/878720',
   );
 
   if (isset($topic)) {
@@ -1448,3 +1711,46 @@ function rules_tokens($type, $tokens, $data, $options = array()) {
     return entity_token_tokens('struct', $tokens, array('struct' => $wrapper), $options);
   }
 }
+
+/**
+ * Helper function that retrieves a metadata wrapper with all properties.
+ *
+ * Note that without this helper, bundle-specific properties aren't added.
+ */
+function rules_get_entity_metadata_wrapper_all_properties(RulesAbstractPlugin $element) {
+  return entity_metadata_wrapper($element->settings['type'], NULL, array(
+    'property info alter' => 'rules_entity_metadata_wrapper_all_properties_callback',
+  ));
+}
+
+/**
+ * Callback that returns a metadata wrapper with all properties.
+ */
+function rules_entity_metadata_wrapper_all_properties_callback(EntityMetadataWrapper $wrapper, $property_info) {
+  $info = $wrapper->info();
+  $properties = entity_get_all_property_info($info['type']);
+  $property_info['properties'] += $properties;
+  return $property_info;
+}
+
+/**
+ * Helper to enable or disable the invocation of rules events.
+ *
+ * Rules invocation is disabled by default, such that Rules does not operate
+ * when Drupal is not fully bootstrapped. It gets enabled in rules_init() and
+ * rules_enable().
+ *
+ * @param bool|null $enable
+ *   NULL to leave the setting as is and TRUE / FALSE to change the behaviour.
+ *
+ * @return bool
+ *   Whether the rules invocation is enabled or disabled.
+ */
+function rules_event_invocation_enabled($enable = NULL) {
+  static $invocation_enabled = FALSE;
+  if (isset($enable)) {
+    $invocation_enabled = (bool) $enable;
+  }
+  // Disable invocation if configured or if site runs in maintenance mode.
+  return $invocation_enabled && !defined('MAINTENANCE_MODE');
+}

+ 37 - 13
sites/all/modules/rules/rules.rules.inc

@@ -1,7 +1,8 @@
 <?php
 
 /**
- * @file Includes any rules integration provided by the module.
+ * @file
+ * Includes any rules integration provided by the module.
  */
 
 /**
@@ -10,7 +11,6 @@
  */
 foreach (rules_core_modules() as $module) {
   module_load_include('inc', 'rules', "modules/$module.rules");
-  module_load_include('inc', 'rules', 'modules/events');
 }
 
 /**
@@ -21,13 +21,22 @@ foreach (rules_core_modules() as $module) {
  * for providing some general stuff.
  */
 function rules_core_modules() {
-  $return = array('data', 'entity', 'node', 'system', 'user', 'rules_core');
-  foreach (array('comment', 'taxonomy', 'php', 'path') as $module) {
-    if (module_exists($module)) {
-      $return[] = $module;
+  // Make use of the fast, advanced drupal static pattern.
+  static $drupal_static_fast;
+  if (!isset($drupal_static_fast)) {
+    $drupal_static_fast = &drupal_static(__FUNCTION__);
+  }
+  $modules = &$drupal_static_fast;
+
+  if (!isset($modules)) {
+    $modules = array('data', 'entity', 'node', 'system', 'user', 'rules_core');
+    foreach (array('comment', 'taxonomy', 'php', 'path') as $module) {
+      if (module_exists($module)) {
+        $modules[] = $module;
+      }
     }
   }
-  return $return;
+  return $modules;
 }
 
 /**
@@ -47,17 +56,32 @@ function _rules_rules_collect_items($hook) {
  * Implements hook_rules_file_info().
  */
 function rules_rules_file_info() {
-  $items = array();
-  foreach (rules_core_modules() as $module) {
-    if (function_exists($function = "rules_{$module}_file_info")) {
-      $items = array_merge($items, (array)$function());
-      // Automatically add "$module.rules.inc" for each module.
-      $items[] = 'modules/' . $module . '.rules';
+  // Make use of the fast, advanced drupal static pattern.
+  static $drupal_static_fast;
+  if (!isset($drupal_static_fast)) {
+    $drupal_static_fast = &drupal_static(__FUNCTION__);
+  }
+  $items = &$drupal_static_fast;
+  if (!isset($items)) {
+    $items = array();
+    foreach (rules_core_modules() as $module) {
+      if (function_exists($function = "rules_{$module}_file_info")) {
+        $items = array_merge($items, (array) $function());
+        // Automatically add "$module.rules.inc" for each module.
+        $items[] = 'modules/' . $module . '.rules';
+      }
     }
   }
   return $items;
 }
 
+/**
+ * Implements hook_rules_category_info().
+ */
+function rules_rules_category_info() {
+  return _rules_rules_collect_items('category_info');
+}
+
 /**
  * Implements hook_rules_action_info().
  */

+ 28 - 12
sites/all/modules/rules/rules_admin/rules_admin.inc

@@ -1,8 +1,8 @@
 <?php
 
 /**
- * @file Rules Admin UI
- *   Implements rule management and configuration screens.
+ * @file
+ * Implements rule management and configuration screens.
  */
 
 /**
@@ -26,11 +26,12 @@ function rules_admin_reaction_overview($form, &$form_state, $base_path) {
   }
   else {
     $event = $_GET['event'];
-    $conditions['event'] = $event;
+    // Filter using a wildcard suffix so configured event names with suffixes
+    // are found also.
+    $conditions['event'] = $event . '%';
     $collapsed = FALSE;
   }
   $form['help'] = array(
-    '#type' => 'markup',
     '#markup' => t('Reaction rules, listed below, react on selected events on the site. Each reaction rule may fire any number of <em>actions</em>, and may have any number of <em>conditions</em> that must be met for the actions to be executed. You can also set up <a href="@url1">components</a> – stand-alone sets of Rules configuration that can be used in Rules and other parts of your site. See <a href="@url2">the online documentation</a> for an introduction on how to use Rules.',
       array('@url1' => url('admin/config/workflow/rules/components'),
             '@url2' => rules_external_help('rules'))),
@@ -103,7 +104,6 @@ function rules_admin_components_overview($form, &$form_state, $base_path) {
     $collapsed = FALSE;
   }
   $form['help'] = array(
-    '#type' => 'markup',
     '#markup' => t('Components are stand-alone sets of Rules configuration that can be used by Rules and other modules on your site. Components are for example useful if you want to use the same conditions, actions or rules in multiple places, or call them from your custom module. You may also export each component separately. See <a href="@url">the online documentation</a> for more information about how to use components.',
       array('@url' => rules_external_help('components'))),
   );
@@ -160,7 +160,7 @@ function rules_admin_settings($form, &$form_state) {
       $pathauto_help = t("Note that Pathauto's URL path cleaning method can be configured at <a href='!url'>admin/config/search/path/settings</a>.", array('!url' => url('admin/config/search/path/settings')));
     }
     else {
-      $pathauto_help = t('Install the <a href="http://drupal.org/project/pathauto">Pathauto module</a> in order to get a configurable URL path cleaning method.');
+      $pathauto_help = t('Install the <a href="https://www.drupal.org/project/pathauto">Pathauto module</a> in order to get a configurable URL path cleaning method.');
     }
 
     $form['path']['rules_path_cleaning_callback'] = array(
@@ -190,7 +190,7 @@ function rules_admin_settings($form, &$form_state) {
   $form['debug']['rules_debug_log'] = array(
     '#type' => 'checkbox',
     '#title' => t('Log debug information to the system log'),
-    '#default_value' => variable_get('rules_debug_log', 0),
+    '#default_value' => variable_get('rules_debug_log', FALSE),
   );
   $form['debug']['rules_debug'] = array(
     '#type' => 'radios',
@@ -209,7 +209,7 @@ function rules_admin_settings($form, &$form_state) {
     '#states' => array(
       // Hide the regions settings when the debug log is disabled.
       'invisible' => array(
-        'input[name="rules_debug"]' => array('value' => '0'),
+        'input[name="rules_debug"]' => array('value' => 0),
       ),
     ),
   );
@@ -279,7 +279,7 @@ function rules_admin_settings_integrity_check_submit($form, &$form_state) {
     rules_config_update_dirty_flag($rules_config, TRUE, TRUE);
     if ($rules_config->dirty) {
       $count++;
-      $variables = array('%label' => $rules_config->label(), '%name' => $rules_config->name, '@plugin' => $rules_config->plugin(), '!uri'=> url(RulesPluginUI::path($rules_config->name)));
+      $variables = array('%label' => $rules_config->label(), '%name' => $rules_config->name, '@plugin' => $rules_config->plugin(), '!uri' => url(RulesPluginUI::path($rules_config->name)));
       drupal_set_message(t('The @plugin <a href="!uri">%label (%name)</a> fails the integrity check and cannot be executed.', $variables), 'error');
     }
 
@@ -312,7 +312,7 @@ function rules_admin_settings_cache_rebuild_submit($form, &$form_state) {
 function rules_admin_add_reaction_rule($form, &$form_state, $base_path) {
   RulesPluginUI::formDefaults($form, $form_state);
 
-  $rules_config = rules_reaction_rule();
+  $rules_config = isset($form_state['rules_config']) ? $form_state['rules_config'] : rules_reaction_rule();
   $rules_config->form($form, $form_state, array('show settings' => TRUE, 'button' => TRUE));
 
   $form['settings']['#collapsible'] = FALSE;
@@ -329,18 +329,34 @@ function rules_admin_add_reaction_rule($form, &$form_state, $base_path) {
   // Incorporate the form to add the first event.
   $form['settings'] += rules_ui_add_event(array(), $form_state, $rules_config, $base_path);
   $form['settings']['event']['#tree'] = FALSE;
+  $form['settings']['event_settings']['#tree'] = FALSE;
   unset($form['settings']['help']);
 
   unset($form['settings']['submit']);
   $form['submit']['#value'] = t('Save');
 
   $form_state += array('rules_config' => $rules_config);
+  $form['#validate'][] = 'rules_ui_add_reaction_rule_validate';
   $form['#validate'][] = 'rules_ui_edit_element_validate';
-  $form['#submit'][] = 'rules_ui_add_event_apply';
-  $form['#submit'][] = 'rules_ui_edit_element_submit';
+  $form['#submit'][] = 'rules_ui_add_reaction_rule_submit';
   return $form;
 }
 
+/**
+ * Form validation callback.
+ */
+function rules_ui_add_reaction_rule_validate(&$form, &$form_state) {
+  rules_ui_add_event_validate($form['settings'], $form_state);
+}
+
+/**
+ * Form submit callback.
+ */
+function rules_ui_add_reaction_rule_submit(&$form, &$form_state) {
+  rules_ui_add_event_apply($form['settings'], $form_state);
+  rules_ui_edit_element_submit($form, $form_state);
+}
+
 /**
  * Add component form.
  */

+ 7 - 6
sites/all/modules/rules/rules_admin/rules_admin.info

@@ -2,13 +2,14 @@ name = Rules UI
 description = Administrative interface for managing rules.
 package = Rules
 core = 7.x
-files[] = rules_admin.module
-files[] = rules_admin.inc
 dependencies[] = rules
+configure = admin/config/workflow/rules
 
-; Information added by drupal.org packaging script on 2013-03-27
-version = "7.x-2.3"
+; Test cases
+files[] = tests/rules_admin.test
+
+; Information added by Drupal.org packaging script on 2019-01-24
+version = "7.x-2.12"
 core = "7.x"
 project = "rules"
-datestamp = "1364401818"
-
+datestamp = "1548305586"

+ 2 - 1
sites/all/modules/rules/rules_admin/rules_admin.module

@@ -1,7 +1,8 @@
 <?php
 
 /**
- * @file Rules Admin UI
+ * @file
+ * Rules Admin User Interface.
  */
 
 /**

+ 126 - 0
sites/all/modules/rules/rules_admin/tests/rules_admin.test

@@ -0,0 +1,126 @@
+<?php
+
+/**
+ * @file
+ * Rules UI tests.
+ */
+
+/**
+ * Tests for creating rules through the UI.
+ */
+class RulesUiTestCase extends DrupalWebTestCase {
+
+  /**
+   * Declares test metadata.
+   */
+  public static function getInfo() {
+    return array(
+      'name' => 'Rules UI Tests ',
+      'description' => 'Tests Rules UI.',
+      'group' => 'Rules',
+    );
+  }
+
+  /**
+   * Overrides DrupalWebTestCase::setUp().
+   */
+  protected function setUp() {
+    parent::setUp('rules', 'rules_admin', 'rules_test');
+    RulesLog::logger()->clear();
+    variable_set('rules_debug_log', TRUE);
+  }
+
+  /**
+   * Tests that NOT condition labels are not HTML-encoded in the UI.
+   *
+   * @see https://www.drupal.org/project/rules/issues/1945006
+   */
+  public function testConditionLabel() {
+    // Create a simple user account with permission to create a rule.
+    $user = $this->drupalCreateUser(array('access administration pages', 'administer rules'));
+    $this->drupalLogin($user);
+
+    // First we need an event.
+    $this->drupalGet('admin/config/workflow/rules/reaction/add');
+    $edit = array(
+      'settings[label]' => 'Test node event',
+      'settings[name]' => 'test_node_event',
+      'event' => 'node_insert',
+    );
+    $this->drupalPost(NULL, $edit, 'Save');
+    $this->assertText('Editing reaction rule', 'Rule edit page is shown.');
+
+    // Now add a condition with a special character in the label.
+    $this->clickLink('Add condition');
+    $this->assertText('Add a new condition', 'Condition edit page is shown.');
+    $edit = array(
+      'element_name' => 'rules_test_condition_apostrophe',
+    );
+    $this->drupalPost(NULL, $edit, 'Continue');
+
+    // Negate the condition, as this is how it gets improperly HTML encoded.
+    $edit = array(
+      'negate' => TRUE,
+    );
+    $this->drupalPost(NULL, $edit, 'Save');
+    $this->assertNoRaw("&amp;#039;", 'Apostrophe is not HTML-encoded.');
+  }
+
+}
+
+/**
+ * UI test cases for the minimal profile.
+ *
+ * The minimal profile is useful for testing because it has fewer dependencies
+ * so the tests run faster. Also, removing the profile-specific configuration
+ * reveals assumptions in the code. For example, the minimal profile doesn't
+ * define any content types, so when Rules expects to have content types to
+ * operate on that assumpation may cause errors.
+ */
+class RulesMinimalProfileTestCase extends DrupalWebTestCase {
+
+  protected $profile = 'minimal';
+
+  /**
+   * Declares test metadata.
+   */
+  public static function getInfo() {
+    return array(
+      'name' => 'Rules UI Minimal Profile Tests ',
+      'description' => 'Tests UI support for minimal profile.',
+      'group' => 'Rules',
+    );
+  }
+
+  /**
+   * Overrides DrupalWebTestCase::setUp().
+   */
+  protected function setUp() {
+    parent::setUp('rules', 'rules_admin');
+    RulesLog::logger()->clear();
+    variable_set('rules_debug_log', TRUE);
+  }
+
+  /**
+   * Tests node event UI without content types.
+   *
+   * @see https://www.drupal.org/project/rules/issues/2267341
+   */
+  public function testNodeEventUi() {
+    // Create a simple user account with permission to create a rule.
+    $user = $this->drupalCreateUser(array('access administration pages', 'administer rules'));
+    $this->drupalLogin($user);
+
+    $this->drupalGet('admin/config/workflow/rules/reaction/add');
+    $edit = array(
+      'settings[label]' => 'Test node event',
+      'settings[name]' => 'test_node_event',
+      'event' => 'node_insert',
+    );
+    $this->drupalPostAJAX(NULL, $edit, 'event');
+    $this->assertText('Restrict by type', 'Restrict by type selection is visible.');
+    $this->drupalPost(NULL, $edit, 'Save');
+    $this->assertText('Editing reaction rule', 'Rule edit page is shown.');
+  }
+
+}

+ 5 - 3
sites/all/modules/rules/rules_i18n/rules_i18n.i18n.inc

@@ -11,7 +11,7 @@
 class RulesI18nStringController extends EntityDefaultI18nStringController {
 
   /**
-   * Overriden to customize i18n object info.
+   * Overridden to customize i18n object info.
    *
    * @see EntityDefaultI18nStringController::hook_object_info()
    */
@@ -22,7 +22,7 @@ class RulesI18nStringController extends EntityDefaultI18nStringController {
   }
 
   /**
-   * Overriden to customize the used menu wildcard.
+   * Overridden to customize the used menu wildcard.
    */
   protected function menuWildcard() {
     return '%rules_config';
@@ -34,6 +34,7 @@ class RulesI18nStringController extends EntityDefaultI18nStringController {
   protected function menuBasePath() {
     return 'admin/config/workflow/rules/reaction';
   }
+
 }
 
 /**
@@ -42,7 +43,7 @@ class RulesI18nStringController extends EntityDefaultI18nStringController {
 class RulesI18nStringObjectWrapper extends i18n_string_object_wrapper {
 
   /**
-   * Get translatable properties
+   * Get translatable properties.
    */
   protected function build_properties() {
     $strings = parent::build_properties();
@@ -91,4 +92,5 @@ class RulesI18nStringObjectWrapper extends i18n_string_object_wrapper {
       }
     }
   }
+
 }

+ 4 - 4
sites/all/modules/rules/rules_i18n/rules_i18n.info

@@ -7,9 +7,9 @@ core = 7.x
 files[] = rules_i18n.i18n.inc
 files[] = rules_i18n.rules.inc
 files[] = rules_i18n.test
-; Information added by drupal.org packaging script on 2013-03-27
-version = "7.x-2.3"
+
+; Information added by Drupal.org packaging script on 2019-01-24
+version = "7.x-2.12"
 core = "7.x"
 project = "rules"
-datestamp = "1364401818"
-
+datestamp = "1548305586"

+ 19 - 0
sites/all/modules/rules/rules_i18n/rules_i18n.install

@@ -0,0 +1,19 @@
+<?php
+
+/**
+ * @file
+ * Install file for Rules i18n.
+ */
+
+/**
+ * Implements hook_install().
+ */
+function rules_i18n_install() {
+  global $language;
+
+  $langcode = $language->language;
+  drupal_static_reset('i18n_object_info');
+  drupal_static_reset('entity_get_info');
+  drupal_static_reset('entity_i18n_controller');
+  cache_clear_all("entity_info:$langcode", 'cache');
+}

+ 4 - 2
sites/all/modules/rules/rules_i18n/rules_i18n.module

@@ -5,7 +5,6 @@
  * Rules i18n integration.
  */
 
-
 /**
  * Implements hook_menu().
  */
@@ -111,7 +110,10 @@ function rules_i18n_rules_config_update($rules_config, $original = NULL) {
  * Implements hook_rules_config_delete().
  */
 function rules_i18n_rules_config_delete($rules_config) {
-  i18n_string_object_remove('rules_config', $rules_config);
+  // Only react on real delete, not revert.
+  if (!$rules_config->hasStatus(ENTITY_IN_CODE)) {
+    i18n_string_object_remove('rules_config', $rules_config);
+  }
 }
 
 /**

+ 9 - 2
sites/all/modules/rules/rules_i18n/rules_i18n.rules.inc

@@ -115,7 +115,7 @@ function rules_i18n_rules_evaluator_info() {
       'type' => array('text', 'list<text>', 'token', 'list<token>'),
       // Be sure to translate after doing PHP evaluation.
       'weight' => -8,
-     ),
+    ),
   );
 }
 
@@ -128,6 +128,9 @@ class RulesI18nStringEvaluator extends RulesDataInputEvaluator {
     return user_access('translate admin strings');
   }
 
+  /**
+   * Overrides RulesDataInputEvaluator::prepare().
+   */
   public function prepare($text, $var_info, $param_info = NULL) {
     if (!empty($param_info['translatable'])) {
       $this->setting = TRUE;
@@ -177,9 +180,12 @@ class RulesI18nStringEvaluator extends RulesDataInputEvaluator {
     return $value;
   }
 
+  /**
+   * Overrides RulesDataInputEvaluator::help().
+   */
   public static function help($var_info, $param_info = array()) {
     if (!empty($param_info['translatable'])) {
-      if ($param_info['custom translation language']) {
+      if (!empty($param_info['custom translation language'])) {
         $text = t('Translations can be provided at the %translate tab. The argument value is translated to the configured language.', array('%translate' => t('Translate')));
       }
       else {
@@ -193,4 +199,5 @@ class RulesI18nStringEvaluator extends RulesDataInputEvaluator {
       return $render;
     }
   }
+
 }

+ 14 - 7
sites/all/modules/rules/rules_i18n/rules_i18n.test

@@ -10,6 +10,9 @@
  */
 class RulesI18nTestCase extends DrupalWebTestCase {
 
+  /**
+   * Declares test metadata.
+   */
   public static function getInfo() {
     return array(
       'name' => 'Rules I18n',
@@ -19,7 +22,10 @@ class RulesI18nTestCase extends DrupalWebTestCase {
     );
   }
 
-  public function setUp() {
+  /**
+   * Overrides DrupalWebTestCase::setUp().
+   */
+  protected function setUp() {
     parent::setUp('rules_i18n');
     $this->admin_user = $this->drupalCreateUser(array('bypass node access', 'administer nodes', 'administer languages', 'administer content types', 'administer blocks', 'access administration pages'));
     $this->drupalLogin($this->admin_user);
@@ -52,11 +58,11 @@ class RulesI18nTestCase extends DrupalWebTestCase {
     }
     elseif ($this->xpath('//input[@type="checkbox" and @name=:name and @checked="checked"]', array(':name' => 'enabled[' . $language_code . ']'))) {
       // It's installed and enabled. No need to do anything.
-      $this->assertTrue(true, 'Language [' . $language_code . '] already installed and enabled.');
+      $this->assertTrue(TRUE, 'Language [' . $language_code . '] already installed and enabled.');
     }
     else {
       // It's installed but not enabled. Enable it.
-      $this->assertTrue(true, 'Language [' . $language_code . '] already installed.');
+      $this->assertTrue(TRUE, 'Language [' . $language_code . '] already installed.');
       $this->drupalPost(NULL, array('enabled[' . $language_code . ']' => TRUE), t('Save configuration'));
       $this->assertRaw(t('Configuration saved.'), t('Language successfully enabled.'));
     }
@@ -139,7 +145,7 @@ class RulesI18nTestCase extends DrupalWebTestCase {
     $messages = drupal_get_messages();
     $this->assertEqual($messages['status'][0], 'text-de', 'Text has been successfully translated.');
 
-    // Enable the PHP module and make sure PHP in translations is not evaluted.
+    // Enable the PHP module and make sure PHP in translations is not evaluated.
     module_enable(array('php'));
     i18n_string_textgroup('rules')->update_translation("rules_config:{$set->name}:$id:text", 'de', 'text <?php echo "eval";?>');
 
@@ -162,9 +168,9 @@ class RulesI18nTestCase extends DrupalWebTestCase {
 
     $set = rules_action_set(array('node' => array('type' => 'node')));
     $set->action('rules_i18n_select', array(
-        'data:select' => 'node:body:value',
-        'language' => 'de',
-        'data_translated:var' => 'body',
+      'data:select' => 'node:body:value',
+      'language' => 'de',
+      'data_translated:var' => 'body',
     ));
     $set->action('drupal_message', array('message:select' => 'body'));
     $set->save();
@@ -180,4 +186,5 @@ class RulesI18nTestCase extends DrupalWebTestCase {
     $messages = drupal_get_messages();
     $this->assertEqual($messages['status'][0], "German body.\n", 'Translated text has been selected.');
   }
+
 }

+ 104 - 0
sites/all/modules/rules/rules_scheduler/includes/rules_scheduler.handler.inc

@@ -0,0 +1,104 @@
+<?php
+
+/**
+ * @file
+ * Views integration for the rules scheduler module.
+ */
+
+/**
+ * Default scheduled task handler.
+ */
+class RulesSchedulerDefaultTaskHandler implements RulesSchedulerTaskHandlerInterface {
+
+  /**
+   * The task array.
+   *
+   * @var array
+   */
+  protected $task;
+
+  /**
+   * Constructs a repetitive task handler object.
+   */
+  public function __construct(array $task) {
+    $this->task = $task;
+  }
+
+  /**
+   * Implements RulesSchedulerTaskHandlerInterface::runTask().
+   */
+  public function runTask() {
+    if ($component = rules_get_cache('comp_' . $this->task['config'])) {
+      $replacements = array('%label' => $component->label(), '%plugin' => $component->plugin());
+      $replacements['%identifier'] = $this->task['identifier'] ? $this->task['identifier'] : t('without identifier');
+      rules_log('Scheduled evaluation of %plugin %label, task %identifier.', $replacements, RulesLog::INFO, $component, TRUE);
+      $state = unserialize($this->task['data']);
+      $state->restoreBlocks();
+      // Block the config to prevent any future recursion.
+      $state->block($component);
+      // Finally evaluate the component with the given state.
+      $component->evaluate($state);
+      $state->unblock($component);
+      rules_log('Finished evaluation of %plugin %label, task %identifier.', $replacements, RulesLog::INFO, $component, FALSE);
+      $state->cleanUp();
+    }
+  }
+
+  /**
+   * Implements RulesSchedulerTaskHandlerInterface::afterTaskQueued().
+   */
+  public function afterTaskQueued() {
+    // Delete the task from the task list.
+    db_delete('rules_scheduler')
+      ->condition('tid', $this->task['tid'])
+      ->execute();
+  }
+
+  /**
+   * Implements RulesSchedulerTaskHandlerInterface::getTask().
+   */
+  public function getTask() {
+    return $this->task;
+  }
+
+}
+
+/**
+ * Interface for scheduled task handlers.
+ *
+ * Task handlers control the behavior of a task when it's queued or executed.
+ * Unless specified otherwise, the RulesSchedulerDefaultTaskHandler task handler
+ * is used.
+ *
+ * @see rules_scheduler_run_task()
+ * @see rules_scheduler_cron()
+ * @see RulesSchedulerDefaultTaskHandler
+ */
+interface RulesSchedulerTaskHandlerInterface {
+
+  /**
+   * Processes a queue item.
+   *
+   * @throws RulesEvaluationException
+   *   If there are any problems executing the task.
+   *
+   * @see rules_scheduler_run_task()
+   */
+  public function runTask();
+
+  /**
+   * Processes a task after it has been queued.
+   *
+   * @see rules_scheduler_cron()
+   */
+  public function afterTaskQueued();
+
+  /**
+   * Returns the task associated with the task handler.
+   *
+   * @return array
+   *   The task (queue item) array.
+   */
+  public function getTask();
+
+}

+ 4 - 3
sites/all/modules/rules/rules_scheduler/includes/rules_scheduler.views.inc

@@ -6,8 +6,9 @@
  */
 
 /**
- * Implements hook_views_data(). Specifies the list of future scheduled
- * tasks displayed on the schedule page.
+ * Implements hook_views_data().
+ *
+ * Specifies the list of future scheduled tasks displayed on the schedule page.
  */
 function rules_scheduler_views_data() {
   $table = array(
@@ -71,7 +72,7 @@ function rules_scheduler_views_data() {
           'click sortable' => TRUE,
         ),
         'filter' => array(
-          'handler' => 'views_handler_filter',
+          'handler' => 'views_handler_filter_string',
         ),
         'sort' => array(
           'handler' => 'views_handler_sort',

+ 3 - 3
sites/all/modules/rules/rules_scheduler/includes/rules_scheduler.views_default.inc

@@ -9,7 +9,7 @@
  * Implements hook_views_default_views().
  */
 function rules_scheduler_views_default_views() {
-  $view = new view;
+  $view = new view();
   $view->name = 'rules_scheduler';
   $view->description = 'Scheduled Rules components';
   $view->tag = '';
@@ -86,7 +86,7 @@ function rules_scheduler_views_default_views() {
   $handler->display->display_options['fields']['config']['field'] = 'config';
   $handler->display->display_options['fields']['config']['alter']['alter_text'] = 0;
   $handler->display->display_options['fields']['config']['alter']['make_link'] = 1;
-  $handler->display->display_options['fields']['config']['alter']['path'] = 'admin/config/workflow/rules/config/[config]';
+  $handler->display->display_options['fields']['config']['alter']['path'] = 'admin/config/workflow/rules/components/manage/[config]';
   $handler->display->display_options['fields']['config']['alter']['absolute'] = 0;
   $handler->display->display_options['fields']['config']['alter']['trim'] = 0;
   $handler->display->display_options['fields']['config']['alter']['word_boundary'] = 1;
@@ -163,7 +163,7 @@ function rules_scheduler_views_default_views() {
     t('No tasks have been scheduled.'),
     t('Tid'),
     t('Component name'),
-    t('admin/config/workflow/rules/config/[config]'),
+    t('admin/config/workflow/rules/components/manage/[config]'),
     t('Scheduled date'),
     t('User provided identifier'),
     t('Operations'),

+ 5 - 2
sites/all/modules/rules/rules_scheduler/includes/rules_scheduler_views_filter.inc

@@ -4,8 +4,10 @@
  * @file
  * An extended subclass for component filtering.
  */
+
 class rules_scheduler_views_filter extends views_handler_filter_in_operator {
-  function get_value_options() {
+
+  public function get_value_options() {
     if (!isset($this->value_options)) {
       $this->value_title = t('Component');
       $result = db_select('rules_scheduler', 'r')
@@ -19,4 +21,5 @@ class rules_scheduler_views_filter extends views_handler_filter_in_operator {
       $this->value_options = $config_names;
     }
   }
-}
+
+}

+ 5 - 6
sites/all/modules/rules/rules_scheduler/rules_scheduler.admin.inc

@@ -9,7 +9,7 @@
  * Schedule page with a view for the scheduled tasks.
  */
 function rules_scheduler_schedule_page() {
-  // Display view for all scheduled tasks
+  // Display view for all scheduled tasks.
   if (module_exists('views')) {
     // We cannot use views_embed_view() here as we need to set the path for the
     // component filter form.
@@ -18,7 +18,7 @@ function rules_scheduler_schedule_page() {
     $task_list = $view->preview();
   }
   else {
-    $task_list = t('To display scheduled tasks you have to install the <a href="http://drupal.org/project/views">Views</a> module.');
+    $task_list = t('To display scheduled tasks you have to install the <a href="https://www.drupal.org/project/views">Views</a> module.');
   }
   $page['task_view'] = array(
     '#markup' => $task_list,
@@ -44,7 +44,7 @@ function rules_scheduler_form($form, &$form_state) {
   $form['delete_by_config'] = array(
     '#type' => 'fieldset',
     '#title' => t('Delete tasks by component name'),
-    '#disabled' => empty($config_options)
+    '#disabled' => empty($config_options),
   );
   $form['delete_by_config']['config'] = array(
     '#title' => t('Component'),
@@ -57,7 +57,7 @@ function rules_scheduler_form($form, &$form_state) {
     '#type' => 'submit',
     '#value' => t('Delete tasks'),
     '#submit' => array('rules_scheduler_form_delete_by_config_submit'),
-    );
+  );
   return $form;
 }
 
@@ -90,7 +90,6 @@ function rules_scheduler_delete_task($form, &$form_state, $task) {
   else {
     $msg = t('This task executes component %label and will be executed on %date. The action cannot be undone.', array(
       '%label' => $config->label(),
-      '%id' => $task['identifier'],
       '%date' => format_date($task['date']),
     ));
   }
@@ -116,7 +115,7 @@ function rules_scheduler_schedule_form($form, &$form_state, $rules_config, $base
     $form_state['component'] = $rules_config->name;
     $action = rules_action('schedule', array('component' => $rules_config->name));
     $action->form($form, $form_state);
-    // The component should be fixed, so hide the paramter for it.
+    // The component should be fixed, so hide the parameter for it.
     $form['parameter']['component']['#access'] = FALSE;
     $form['submit'] = array(
       '#type' => 'submit',

+ 81 - 0
sites/all/modules/rules/rules_scheduler/rules_scheduler.drush.inc

@@ -0,0 +1,81 @@
+<?php
+
+/**
+ * @file
+ * Rules Scheduler Drush integration.
+ */
+
+/**
+ * Implements hook_drush_command().
+ */
+function rules_scheduler_drush_command() {
+  $items = array();
+
+  $items['rules-scheduler-tasks'] = array(
+    'description' => 'Check for scheduled tasks to be added to the queue.',
+    'options' => array(
+      'claim' => 'Optionally claim tasks from the queue to work on. Any value set will override the default time spent on this queue.',
+    ),
+    'drupal dependencies' => array('rules', 'rules_scheduler'),
+    'aliases' => array('rusch'),
+    'examples' => array(
+      'drush rusch' => 'Add scheduled tasks to the queue.',
+      'drush rusch --claim' => 'Add scheduled tasks to the queue and claim items for the default amount of time.',
+      'drush rusch --claim=30' => 'Add scheduled tasks to the queue and claim items for 30 seconds.',
+    ),
+  );
+
+  return $items;
+}
+
+/**
+ * Implements hook_drush_help().
+ */
+function rules_scheduler_drush_help($section) {
+  switch ($section) {
+    case 'drush:rules-scheduler-tasks':
+      return dt('Checks for scheduled tasks to be added the queue. Can optionally claim tasks from the queue to work on.');
+  }
+}
+
+/**
+ * Command callback for processing the rules_scheduler_tasks queue.
+ *
+ * @see rules_scheduler_cron_queue_info()
+ * @see rules_scheduler_cron()
+ */
+function drush_rules_scheduler_tasks() {
+  if (rules_scheduler_queue_tasks()) {
+    // hook_exit() is not invoked for drush runs, so register it as shutdown
+    // callback for logging the rules log to the watchdog.
+    drupal_register_shutdown_function('rules_exit');
+    // Clear the log before running tasks via the queue to avoid logging
+    // unrelated logs from previous operations.
+    RulesLog::logger()->clear();
+    drush_log(dt('Added scheduled tasks to the queue.'), 'success');
+  }
+
+  $claim = drush_get_option('claim', FALSE);
+  if ($claim) {
+    // Fetch the queue information and let other modules alter it.
+    $queue_name = 'rules_scheduler_tasks';
+    $info = module_invoke('rules_scheduler', 'cron_queue_info');
+    drupal_alter('cron_queue_info', $info);
+
+    $function = $info[$queue_name]['worker callback'];
+    // The drush option can override the default process time.
+    $time = is_numeric($claim) ? (int) $claim : $info[$queue_name]['time'];
+    $end = time() + $time;
+    // Claim items and process the queue.
+    $queue = DrupalQueue::get($queue_name);
+    $claimed = 0;
+    while (time() < $end && ($item = $queue->claimItem())) {
+      $function($item->data);
+      $queue->deleteItem($item);
+      $claimed++;
+    }
+    if ($claimed) {
+      drush_log(dt('Claimed and worked on !claimed scheduled tasks for up to !time seconds.', array('!claimed' => $claimed, '!time' => $time)), 'success');
+    }
+  }
+}

+ 10 - 11
sites/all/modules/rules/rules_scheduler/rules_scheduler.info

@@ -3,18 +3,17 @@ description = Schedule the execution of Rules components using actions.
 dependencies[] = rules
 package = Rules
 core = 7.x
-files[] = rules_scheduler.admin.inc
-files[] = rules_scheduler.module
-files[] = rules_scheduler.install
-files[] = rules_scheduler.rules.inc
-files[] = rules_scheduler.test
-files[] = includes/rules_scheduler.views_default.inc
-files[] = includes/rules_scheduler.views.inc
+files[] = includes/rules_scheduler.handler.inc
+
+; Views handlers
 files[] = includes/rules_scheduler_views_filter.inc
 
-; Information added by drupal.org packaging script on 2013-03-27
-version = "7.x-2.3"
+; Test cases
+files[] = tests/rules_scheduler.test
+files[] = tests/rules_scheduler_test.inc
+
+; Information added by Drupal.org packaging script on 2019-01-24
+version = "7.x-2.12"
 core = "7.x"
 project = "rules"
-datestamp = "1364401818"
-
+datestamp = "1548305586"

+ 71 - 5
sites/all/modules/rules/rules_scheduler/rules_scheduler.install

@@ -30,11 +30,12 @@ function rules_scheduler_schema() {
         'type' => 'int',
         'not null' => TRUE,
       ),
-      'state' => array(
-        'type' => 'text',
+      'data' => array(
+        'type' => 'blob',
+        'size' => 'big',
         'not null' => FALSE,
         'serialize' => TRUE,
-        'description' => 'The whole, serialized evaluation state.',
+        'description' => 'The whole, serialized evaluation data.',
       ),
       'identifier' => array(
         'type' => 'varchar',
@@ -43,6 +44,12 @@ function rules_scheduler_schema() {
         'not null' => FALSE,
         'description' => 'The user defined string identifying this task.',
       ),
+      'handler' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => FALSE,
+        'description' => 'The fully-qualified class name of the queue item handler.',
+      ),
     ),
     'primary key' => array('tid'),
     'indexes' => array(
@@ -55,6 +62,24 @@ function rules_scheduler_schema() {
   return $schema;
 }
 
+/**
+ * Implements hook_install().
+ */
+function rules_scheduler_install() {
+  // Create the queue to hold scheduled tasks.
+  $queue = DrupalQueue::get('rules_scheduler_tasks', TRUE);
+  $queue->createQueue();
+}
+
+/**
+ * Implements hook_uninstall().
+ */
+function rules_scheduler_uninstall() {
+  // Clean up after ourselves by deleting the queue and all items in it.
+  $queue = DrupalQueue::get('rules_scheduler_tasks');
+  $queue->deleteQueue();
+}
+
 /**
  * Upgrade from Rules scheduler 6.x-1.x to 7.x.
  */
@@ -84,11 +109,11 @@ function rules_scheduler_update_7200() {
         'type' => 'int',
         'not null' => TRUE,
       ),
-      'state' => array(
+      'data' => array(
         'type' => 'text',
         'not null' => FALSE,
         'serialize' => TRUE,
-        'description' => 'The whole, serialized evaluation state.',
+        'description' => 'The whole, serialized evaluation data.',
       ),
       'identifier' => array(
         'type' => 'varchar',
@@ -122,6 +147,47 @@ function rules_scheduler_update_7202() {
   db_add_unique_key('rules_scheduler', 'id', array('config', 'identifier'));
 }
 
+/**
+ * Add a database column for specifying a queue item handler.
+ */
+function rules_scheduler_update_7203() {
+  db_add_field('rules_scheduler', 'handler', array(
+    'type' => 'varchar',
+    'length' => '255',
+    'not null' => FALSE,
+    'description' => 'The fully-qualified class name of the queue item handler.',
+  ));
+}
+
+/**
+ * Rename rules_scheduler.state into rules_scheduler.data.
+ */
+function rules_scheduler_update_7204() {
+  if (db_field_exists('rules_scheduler', 'state')) {
+    db_change_field('rules_scheduler', 'state', 'data', array(
+      'type' => 'text',
+      'not null' => FALSE,
+      'serialize' => TRUE,
+      'description' => 'The whole, serialized evaluation data.',
+    ));
+  }
+}
+
+/**
+ * Use blob:big for rules_scheduler.data for compatibility with PostgreSQL.
+ */
+function rules_scheduler_update_7205() {
+  if (db_field_exists('rules_scheduler', 'data')) {
+    db_change_field('rules_scheduler', 'data', 'data', array(
+      'type' => 'blob',
+      'size' => 'big',
+      'not null' => FALSE,
+      'serialize' => TRUE,
+      'description' => 'The whole, serialized evaluation data.',
+    ));
+  }
+}
+
 /**
  * Rules upgrade callback for mapping the action name.
  */

+ 89 - 40
sites/all/modules/rules/rules_scheduler/rules_scheduler.module

@@ -11,25 +11,7 @@ define('RULES_SCHEDULER_PATH', 'admin/config/workflow/rules/schedule');
  * Implements hook_cron().
  */
 function rules_scheduler_cron() {
-  // Limit adding tasks to 1000 per cron run.
-  $result = db_select('rules_scheduler', 'r', array('fetch' => PDO::FETCH_ASSOC))
-    ->fields('r')
-    ->condition('date', time(), '<=')
-    ->range(0, 1000)
-    ->execute();
-
-  $queue = DrupalQueue::get('rules_scheduler_tasks');
-  foreach ($result as $task) {
-    // Add the task to the queue and remove the entry afterwards.
-    if ($queue->createItem($task)) {
-      db_delete('rules_scheduler')
-        ->condition('tid', $task['tid'])
-        ->execute();
-      $task_created = TRUE;
-    }
-  }
-
-  if (!empty($task_created)) {
+  if (rules_scheduler_queue_tasks()) {
     // hook_exit() is not invoked for cron runs, so register it as shutdown
     // callback for logging the rules log to the watchdog.
     drupal_register_shutdown_function('rules_exit');
@@ -52,21 +34,45 @@ function rules_scheduler_cron_queue_info() {
 
 /**
  * Queue worker callback for running a single task.
+ *
+ * @param array $task
+ *   The task to process.
  */
 function rules_scheduler_run_task(array $task) {
-  if ($component = rules_get_cache('comp_' . $task['config'])) {
-    $replacements = array('%label' => $component->label(), '%plugin' => $component->plugin());
-    $replacements['%identifier'] = $task['identifier'] ? $task['identifier'] : t('without identifier');
-    rules_log('Scheduled evaluation of %plugin %label, task %identifier.', $replacements, RulesLog::INFO, $component, TRUE);
-    $state = unserialize($task['state']);
-    $state->restoreBlocks();
-    // Finally evaluate the component with the given state.
-    $component->evaluate($state);
-    rules_log('Finished evaluation of %plugin %label, task %identifier.', $replacements, RulesLog::INFO, $component, FALSE);
-    $state->cleanUp();
+  try {
+    // BC support for tasks that have been already queued, before update
+    // rules_scheduler_update_7204() ran.
+    if (isset($task['state'])) {
+      $task['data'] = $task['state'];
+    }
+    rules_scheduler_task_handler($task)->runTask();
+  }
+  catch (RulesEvaluationException $e) {
+    rules_log($e->msg, $e->args, $e->severity);
+    rules_log('Unable to execute task with identifier %id scheduled on date %date.', array('%id' => $task['identifier'], '%date' => format_date($task['date'])), RulesLog::ERROR);
   }
 }
 
+/**
+ * Returns the task handler for a given task.
+ *
+ * @param array $task
+ *   A task (queue item) array.
+ *
+ * @throws RulesEvaluationException
+ *   If the task handler class is missing.
+ *
+ * @return RulesSchedulerTaskHandlerInterface
+ *   The task handler.
+ */
+function rules_scheduler_task_handler(array $task) {
+  $class = !empty($task['handler']) ? $task['handler'] : 'RulesSchedulerDefaultTaskHandler';
+  if (!class_exists($class)) {
+    throw new RulesEvaluationException('Missing task handler implementation %class.', array('%class' => $class), NULL, RulesLog::ERROR);
+  }
+  return new $class($task);
+}
+
 /**
  * Implements hook_rules_ui_menu_alter().
  *
@@ -97,7 +103,7 @@ function rules_scheduler_menu() {
     'access arguments' => array('administer rules'),
     'file' => 'rules_scheduler.admin.inc',
   );
-  $items[RULES_SCHEDULER_PATH .'/%rules_scheduler_task/delete'] = array(
+  $items[RULES_SCHEDULER_PATH . '/%rules_scheduler_task/delete'] = array(
     'title' => 'Delete a scheduled task',
     'type' => MENU_CALLBACK,
     'page callback' => 'drupal_get_form',
@@ -109,7 +115,10 @@ function rules_scheduler_menu() {
 }
 
 /**
- * Load a task by a given task ID.
+ * Loads a task by a given task ID.
+ *
+ * @param int $tid
+ *   The task ID.
  */
 function rules_scheduler_task_load($tid) {
   $result = db_select('rules_scheduler', 'r')
@@ -120,7 +129,10 @@ function rules_scheduler_task_load($tid) {
 }
 
 /**
- * Delete a task by a given task ID.
+ * Deletes a task by a given task ID.
+ *
+ * @param int $tid
+ *   The task ID.
  */
 function rules_scheduler_task_delete($tid) {
   db_delete('rules_scheduler')
@@ -131,15 +143,22 @@ function rules_scheduler_task_delete($tid) {
 /**
  * Schedule a task to be executed later on.
  *
- * @param $task
+ * @param array $task
  *   An array representing the task with the following keys:
- *   - config: The machine readable name of the to be scheduled component.
+ *   - config: The machine readable name of the to-be-scheduled component.
  *   - date: Timestamp when the component should be executed.
- *   - state: An rules evaluation state to use for scheduling.
+ *   - state: (deprecated) Rules evaluation state to use for scheduling.
+ *   - data: Any additional data to store with the task.
+ *   - handler: The name of the task handler class.
  *   - identifier: User provided string to identify the task per scheduled
  *   configuration.
  */
 function rules_scheduler_schedule_task($task) {
+  // Map the deprecated 'state' property into 'data'.
+  if (isset($task['state'])) {
+    $task['data'] = $task['state'];
+    unset($task['state']);
+  }
   if (!empty($task['identifier'])) {
     // If there is a task with the same identifier and component, we replace it.
     db_delete('rules_scheduler')
@@ -150,14 +169,44 @@ function rules_scheduler_schedule_task($task) {
   drupal_write_record('rules_scheduler', $task);
 }
 
+/**
+ * Queue tasks that are ready for execution.
+ *
+ * @return bool
+ *   TRUE if any queue items where created, otherwise FALSE.
+ */
+function rules_scheduler_queue_tasks() {
+  $items_created = FALSE;
+  // Limit adding tasks to 1000 per cron run.
+  $result = db_select('rules_scheduler', 'r', array('fetch' => PDO::FETCH_ASSOC))
+    ->fields('r')
+    ->condition('date', time(), '<=')
+    ->orderBy('date')
+    ->range(0, 1000)
+    ->execute();
+
+  $queue = DrupalQueue::get('rules_scheduler_tasks');
+  foreach ($result as $task) {
+    // Add the task to the queue and remove the entry afterwards.
+    if ($queue->createItem($task)) {
+      $items_created = TRUE;
+      rules_scheduler_task_handler($task)->afterTaskQueued();
+    }
+  }
+  return $items_created;
+}
+
 /**
  * Implements hook_rules_config_delete().
  */
 function rules_scheduler_rules_config_delete($rules_config) {
-  // Delete all tasks scheduled for this config.
-  db_delete('rules_scheduler')
-    ->condition('config', $rules_config->name)
-    ->execute();
+  // Only react on real delete, not revert.
+  if (!$rules_config->hasStatus(ENTITY_IN_CODE)) {
+    // Delete all tasks scheduled for this config.
+    db_delete('rules_scheduler')
+      ->condition('config', $rules_config->name)
+      ->execute();
+  }
 }
 
 /**
@@ -166,6 +215,6 @@ function rules_scheduler_rules_config_delete($rules_config) {
 function rules_scheduler_views_api() {
   return array(
     'api' => '3.0-alpha1',
-    'path' => drupal_get_path('module', 'rules_scheduler') .'/includes',
+    'path' => drupal_get_path('module', 'rules_scheduler') . '/includes',
   );
 }

+ 8 - 5
sites/all/modules/rules/rules_scheduler/rules_scheduler.rules.inc

@@ -5,6 +5,7 @@
  * Rules integration for the rules scheduler module.
  *
  * @addtogroup rules
+ *
  * @{
  */
 
@@ -89,7 +90,7 @@ function rules_scheduler_action_schedule($args, $element) {
     rules_scheduler_schedule_task(array(
       'date' => $args['date'],
       'config' => $args['component'],
-      'state' => $new_state,
+      'data' => $new_state,
       'identifier' => $args['identifier'],
     ));
   }
@@ -115,7 +116,9 @@ function rules_scheduler_action_schedule_info_alter(&$element_info, RulesPlugin
 }
 
 /**
- * Validate callback for the schedule action to make sure the component exists and is not dirty.
+ * Validate callback for the schedule action.
+ *
+ * Makes sure the component exists and is not dirty.
  *
  * @see rules_element_invoke_component_validate()
  */
@@ -137,7 +140,7 @@ function rules_scheduler_action_schedule_validate(RulesPlugin $element) {
  */
 function rules_scheduler_action_schedule_help() {
   return t("Note that component evaluation is triggered by <em>cron</em> – make sure cron is configured correctly by checking your site's !status. The scheduling time accuracy depends on your configured cron interval. See <a href='@url'>the online documentation</a> for more information on how to schedule evaluation of components.",
-    array('!status' => l('Status report', 'admin/reports/status'),
+    array('!status' => l(t('Status report'), 'admin/reports/status'),
           '@url' => rules_external_help('scheduler')));
 }
 
@@ -192,7 +195,7 @@ function rules_scheduler_action_delete($component_name = NULL, $task_identifier
 }
 
 /**
- * Cancel scheduled task action validation callback.
+ * Cancels scheduled task action validation callback.
  */
 function rules_scheduler_action_delete_validate($element) {
   if (empty($element->settings['task']) && empty($element->settings['task:select']) &&
@@ -206,7 +209,7 @@ function rules_scheduler_action_delete_validate($element) {
  * Help for the cancel action.
  */
 function rules_scheduler_action_delete_help() {
-  return t('This action allows you to delete scheduled tasks that are waiting for future execution.') .' '. t('They can be addressed by an identifier or by the component name, whereas if both are specified only tasks fulfilling both requirements will be deleted.');
+  return t('This action allows you to delete scheduled tasks that are waiting for future execution.') . ' ' . t('They can be addressed by an identifier or by the component name, whereas if both are specified only tasks fulfilling both requirements will be deleted.');
 }
 
 /**

+ 63 - 14
sites/all/modules/rules/rules_scheduler/rules_scheduler.test → sites/all/modules/rules/rules_scheduler/tests/rules_scheduler.test

@@ -5,9 +5,15 @@
  * Rules Scheduler tests.
  */
 
+/**
+ * Test cases for the Rules Scheduler module.
+ */
 class RulesSchedulerTestCase extends DrupalWebTestCase {
 
-  static function getInfo() {
+  /**
+   * Declares test metadata.
+   */
+  public static function getInfo() {
     return array(
       'name' => 'Rules Scheduler tests',
       'description' => 'Test scheduling components.',
@@ -15,10 +21,13 @@ class RulesSchedulerTestCase extends DrupalWebTestCase {
     );
   }
 
-  function setUp() {
-    parent::setUp('rules_scheduler');
+  /**
+   * Overrides DrupalWebTestCase::setUp().
+   */
+  protected function setUp() {
+    parent::setUp('rules_scheduler', 'rules_scheduler_test');
     RulesLog::logger()->clear();
-    variable_set('rules_debug_log', 1);
+    variable_set('rules_debug_log', TRUE);
   }
 
   /**
@@ -27,7 +36,7 @@ class RulesSchedulerTestCase extends DrupalWebTestCase {
    * Note that this also makes sure Rules properly handles timezones, else this
    * test could fail due to a wrong 'now' timestamp.
    */
-  function testComponentSchedule() {
+  public function testComponentSchedule() {
     $set = rules_rule_set(array(
       'node1' => array('type' => 'node', 'label' => 'node'),
     ));
@@ -51,7 +60,7 @@ class RulesSchedulerTestCase extends DrupalWebTestCase {
     $rule->execute($node);
 
     // Run cron to let the rules scheduler do its work.
-    drupal_cron_run();
+    $this->cronRun();
 
     $node = node_load($node->nid, NULL, TRUE);
     $this->assertFalse($node->status, 'The component has been properly scheduled.');
@@ -59,9 +68,9 @@ class RulesSchedulerTestCase extends DrupalWebTestCase {
   }
 
   /**
-   * Make sure recurion prevention is working fine for scheduled rule sets.
+   * Makes sure recursion prevention is working fine for scheduled rule sets.
    */
-  function testRecursionPrevention() {
+  public function testRecursionPrevention() {
     $set = rules_rule_set(array(
       'node1' => array('type' => 'node', 'label' => 'node'),
     ));
@@ -78,7 +87,7 @@ class RulesSchedulerTestCase extends DrupalWebTestCase {
     $rule->event('node_update');
     $rule->action('schedule', array(
       'component' => 'rules_test_set_2',
-      'identifier' => '',
+      'identifier' => 'test_recursion_prevention',
       'date' => 'now',
       'param_node1:select' => 'node',
     ));
@@ -87,14 +96,54 @@ class RulesSchedulerTestCase extends DrupalWebTestCase {
     // Create a node, what triggers the rule.
     $node = $this->drupalCreateNode(array('title' => 'The title.', 'status' => 1));
     // Run cron to let the rules scheduler do its work.
-    drupal_cron_run();
+    $this->cronRun();
 
     $node = node_load($node->nid, NULL, TRUE);
     $this->assertFalse($node->status, 'The component has been properly scheduled.');
-    $text1 = RulesLog::logger()->render();
-    $text2 = RulesTestCase::t('Not evaluating reaction rule %unlabeled to prevent recursion.', array('unlabeled' => $rule->name));
-    $this->assertTrue((strpos($text1, $text2) !== FALSE), "Scheduled recursion prevented.");
+
+    // Create a simple user account with permission to see the dblog.
+    $user = $this->drupalCreateUser(array('access site reports'));
+    $this->drupalLogin($user);
+
+    // View the database log.
+    $this->drupalGet('admin/reports/dblog');
+
+    // Can't use
+    // $this->clickLink('Rules debug information: " Scheduled evaluation...')
+    // because xpath doesn't allow : or " in the string.
+    // So instead, use our own xpath to figure out the href of the second link
+    // on the page (the first link is the most recent log entry, which is the
+    // log entry for the user login, above.)
+
+    // All links.
+    $links = $this->xpath('//a[contains(@href, :href)]', array(':href' => 'admin/reports/event/'));
+    // Strip off /?q= from href.
+    $href = explode('=', $links[1]['href']);
+    // Click the link for the RulesLog entry.
+    $this->drupalGet($href[1]);
+    $this->assertRaw(RulesTestCase::t('Not evaluating reaction rule %unlabeled to prevent recursion.', array('unlabeled' => $rule->name)), "Scheduled recursion prevented.");
     RulesLog::logger()->checkLog();
   }
-}
 
+  /**
+   * Tests that custom task handlers are properly invoked.
+   */
+  public function testCustomTaskHandler() {
+    // Set up a scheduled task that will simply write a variable when executed.
+    $variable = 'rules_schedule_task_handler_variable';
+    rules_scheduler_schedule_task(array(
+      'date' => REQUEST_TIME,
+      'identifier' => '',
+      'config' => '',
+      'data' => array('variable' => $variable),
+      'handler' => 'RulesTestTaskHandler',
+    ));
+
+    // Run cron to let the rules scheduler do its work.
+    $this->cronRun();
+
+    // The task handler should have set the variable to TRUE now.
+    $this->assertTrue(variable_get($variable));
+  }
+
+}

+ 24 - 0
sites/all/modules/rules/rules_scheduler/tests/rules_scheduler_test.inc

@@ -0,0 +1,24 @@
+<?php
+
+/**
+ * @file
+ * Include file for Rules Scheduler tests.
+ */
+
+/**
+ * Test task handler class.
+ */
+class RulesTestTaskHandler extends RulesSchedulerDefaultTaskHandler {
+
+  /**
+   * Overrides RulesSchedulerDefaultTaskHandler::runTask().
+   */
+  public function runTask() {
+    $task = $this->getTask();
+    $data = unserialize($task['data']);
+
+    // Set the variable defined in the test to TRUE.
+    variable_set($data['variable'], TRUE);
+  }
+
+}

+ 12 - 0
sites/all/modules/rules/rules_scheduler/tests/rules_scheduler_test.info

@@ -0,0 +1,12 @@
+name = "Rules Scheduler Tests"
+description = "Support module for the Rules Scheduler tests."
+package = Testing
+core = 7.x
+files[] = rules_scheduler_test.inc
+hidden = TRUE
+
+; Information added by Drupal.org packaging script on 2019-01-24
+version = "7.x-2.12"
+core = "7.x"
+project = "rules"
+datestamp = "1548305586"

+ 6 - 0
sites/all/modules/rules/rules_scheduler/tests/rules_scheduler_test.module

@@ -0,0 +1,6 @@
+<?php
+
+/**
+ * @file
+ * Rules Scheduler test module.
+ */

Plik diff jest za duży
+ 247 - 140
sites/all/modules/rules/tests/rules.test


+ 3 - 5
sites/all/modules/rules/tests/rules_test.info

@@ -3,12 +3,10 @@ description = "Support module for the Rules tests."
 package = Testing
 core = 7.x
 files[] = rules_test.rules.inc
-files[] = rules_test.rules_defaults.inc
 hidden = TRUE
 
-; Information added by drupal.org packaging script on 2013-03-27
-version = "7.x-2.3"
+; Information added by Drupal.org packaging script on 2019-01-24
+version = "7.x-2.12"
 core = "7.x"
 project = "rules"
-datestamp = "1364401818"
-
+datestamp = "1548305586"

+ 5 - 1
sites/all/modules/rules/tests/rules_test.module

@@ -1,7 +1,8 @@
 <?php
 
 /**
- * @file Rules test module.
+ * @file
+ * Rules test module.
  */
 
 /**
@@ -49,6 +50,9 @@ function rules_test_get_referenced_node($node) {
   return array($node->nid);
 }
 
+/**
+ * Access callback. Returns TRUE except when $op == 'edit'.
+ */
 function rules_test_no_access($op) {
   return $op == 'edit' ? FALSE : TRUE;
 }

+ 140 - 2
sites/all/modules/rules/tests/rules_test.rules.inc

@@ -1,9 +1,21 @@
 <?php
 
 /**
- * @file Includes any rules integration provided by the module.
+ * @file
+ * Includes any rules integration provided by the module.
  */
 
+/**
+ * Implements hook_rules_event_info().
+ */
+function rules_test_rules_event_info() {
+  return array(
+    'rules_test_event' => array(
+      'label' => t('Test event'),
+      'class' => 'RulesTestEventHandler',
+    ),
+  );
+}
 
 /**
  * Implements hook_rules_file_info().
@@ -42,6 +54,10 @@ function rules_test_rules_condition_info() {
     'label' => t('Test condition returning false'),
     'group' => t('Rules test'),
   );
+  $items['rules_test_condition_apostrophe'] = array(
+    'label' => t("Test use of an apostrophe (') in a condition label"),
+    'group' => t('Rules test'),
+  );
   // A condition for testing passing entities wrapped.
   $items['rules_test_condition_node_wrapped'] = array(
     'label' => t('Content is published'),
@@ -76,6 +92,20 @@ function rules_test_condition_false() {
   return FALSE;
 }
 
+/**
+ * Condition testing use of an apostrophe in a condition label.
+ *
+ * Specifically, we want to ensure that special characters do not show up as
+ * HTML-encoded in the user interface.
+ */
+function rules_test_condition_apostrophe($settings, $state, $element) {
+  if (!$element instanceof RulesCondition) {
+    throw new Exception('Rules element has not been passed to condition.');
+  }
+  rules_log('condition apostrophe called');
+  return TRUE;
+}
+
 /**
  * Condition implementation receiving the node wrapped.
  */
@@ -189,7 +219,11 @@ function rules_test_rules_action_info() {
       'base' => 'rules_test_type_save',
       'label' => t('Save test type'),
       'parameter' => array(
-        'node' => array('type' => 'rules_test_type', 'label' => t('Test content'), 'save' => TRUE),
+        'node' => array(
+          'type' => 'rules_test_type',
+          'label' => t('Test content'),
+          'save' => TRUE,
+        ),
       ),
       'group' => t('Node'),
     ),
@@ -203,6 +237,37 @@ function rules_test_action() {
   rules_log('action called');
 }
 
+/**
+ * Action for testing writing class-based actions.
+ */
+class RulesTestClassAction extends RulesActionHandlerBase {
+
+  /**
+   * Defines the action.
+   */
+  public static function getInfo() {
+    return array(
+      'name' => 'rules_test_class_action',
+      'label' => t('Test class based action'),
+      'group' => t('Node'),
+      'parameter' => array(
+        'node' => array(
+          'type' => 'node',
+          'label' => t('Node'),
+        ),
+      ),
+    );
+  }
+
+  /**
+   * Executes the action.
+   */
+  public function execute($node) {
+    rules_log('Action called with node ' . $node->nid);
+  }
+
+}
+
 /**
  * Implements hook_rules_data_info().
  */
@@ -230,15 +295,88 @@ function rules_test_rules_data_info_alter(&$data_info) {
  */
 class RulesTestTypeWrapper extends RulesIdentifiableDataWrapper implements RulesDataWrapperSavableInterface {
 
+  /**
+   * Overrides RulesIdentifiableDataWrapper::extractIdentifier().
+   */
   protected function extractIdentifier($data) {
     return $data->nid;
   }
 
+  /**
+   * Overrides RulesIdentifiableDataWrapper::load().
+   */
   protected function load($id) {
     return node_load($id);
   }
 
+  /**
+   * Implements RulesDataWrapperSavableInterface::save().
+   */
   public function save() {
     node_save($this->value());
   }
+
+}
+
+/**
+ * Implements hook_rules_plugin_info().
+ */
+function rules_test_rules_plugin_info() {
+  return array(
+    'rules test container' => array(
+      'label' => t('Test container'),
+      'class' => 'RulesTestContainer',
+      'embeddable' => 'RulesActionContainer',
+    ),
+  );
+}
+
+/**
+ * Test container plugin.
+ */
+class RulesTestContainer extends RulesContainerPlugin {
+  protected $itemName = 'rules test container';
+
+  /**
+   * Evaluate the element on a given rules evaluation state.
+   */
+  public function evaluate(RulesState $state) {
+    // Do nothing.
+  }
+
+}
+
+/**
+ * Test event handler class.
+ */
+class RulesTestEventHandler extends RulesEventDefaultHandler implements RulesEventDispatcherInterface {
+
+  /**
+   * Name of the variable in which to store the state of the event handler.
+   *
+   * @var string
+   */
+  protected $variableName = 'rules_test_event_handler_watch';
+
+  /**
+   * Implements RulesEventDispatcherInterface::startWatching().
+   */
+  public function startWatching() {
+    variable_set($this->variableName, TRUE);
+  }
+
+  /**
+   * Implements RulesEventDispatcherInterface::stopWatching().
+   */
+  public function stopWatching() {
+    variable_set($this->variableName, FALSE);
+  }
+
+  /**
+   * Implements RulesEventDispatcherInterface::isWatching().
+   */
+  public function isWatching() {
+    return (bool) variable_get($this->variableName);
+  }
+
 }

+ 6 - 3
sites/all/modules/rules/tests/rules_test.rules_defaults.inc

@@ -1,16 +1,18 @@
 <?php
 
 /**
- * @file Includes any rules integration provided by the module.
+ * @file
+ * Includes any Rules integration provided by the module.
  */
 
-
 /**
  * Implements hook_default_rules_configuration().
  */
 function rules_test_default_rules_configuration() {
   $rule = rules_reaction_rule();
   $rule->label = 'example default rule';
+  // Add rules tags.
+  $rule->tags = array('Admin', 'Tag2');
   $rule->active = FALSE;
   $rule->event('node_update')
        ->condition(rules_condition('data_is', array('data:select' => 'node:status', 'value' => TRUE))->negate())
@@ -77,9 +79,10 @@ function _rules_export_get_test_export() {
     "PLUGIN" : "reaction rule",
     "WEIGHT" : "-1",
     "ACTIVE" : false,
+    "OWNER" : "rules",
     "TAGS" : [ "bar", "baz", "foo" ],
     "REQUIRES" : [ "rules", "comment" ],
-    "ON" : [ "comment_insert" ],
+    "ON" : { "comment_insert" : [] },
     "IF" : [
       { "OR" : [
           { "NOT node_is_sticky" : { "node" : [ "comment:node" ] } },

+ 7 - 7
sites/all/modules/rules/tests/rules_test.test.inc

@@ -1,7 +1,8 @@
 <?php
 
 /**
- * @file Include file for testing file inclusion.
+ * @file
+ * Include file for testing file inclusion.
  */
 
 /**
@@ -11,16 +12,15 @@ function rules_test_custom_node_save($object) {
   throw new RulesEvaluationException('Custom save method invoked.');
 }
 
-
 /**
- * Custom help callback for the rules_node_publish_action
+ * Custom help callback for the rules_node_publish_action().
  */
 function rules_test_custom_help() {
   return 'custom';
 }
 
 /**
- * Action callback
+ * Action callback.
  */
 function rules_action_test_reference($data) {
   $data['changed'] = TRUE;
@@ -28,21 +28,21 @@ function rules_action_test_reference($data) {
 }
 
 /**
- * Condition: Check for selected content types
+ * Condition: Check for selected content types.
  */
 function rules_condition_content_is_type($node, $type) {
   return in_array($node->type, $type);
 }
 
 /**
- * Condition: Check if the node is published
+ * Condition: Check if the node is published.
  */
 function rules_condition_content_is_published($node, $settings) {
   return $node->status == 1;
 }
 
 /**
- * Loads a node
+ * Loads a node.
  */
 function rules_action_load_node($nid, $vid = NULL) {
   return array('node_loaded' => node_load($nid, $vid ? $vid : NULL));

+ 11 - 0
sites/all/modules/rules/tests/rules_test_invocation.info

@@ -0,0 +1,11 @@
+name = "Rules Test invocation"
+description = "Helper module to test Rules invocations."
+package = Testing
+core = 7.x
+hidden = TRUE
+
+; Information added by Drupal.org packaging script on 2019-01-24
+version = "7.x-2.12"
+core = "7.x"
+project = "rules"
+datestamp = "1548305586"

+ 13 - 0
sites/all/modules/rules/tests/rules_test_invocation.module

@@ -0,0 +1,13 @@
+<?php
+
+/**
+ * @file
+ * Helper module for Rules invocation testing.
+ */
+
+/**
+ * Implements hook_node_load().
+ */
+function rules_test_invocation_node_load($nodes, $types) {
+  rules_invoke_event('rules_test_event');
+}

+ 12 - 5
sites/all/modules/rules/ui/rules.autocomplete.js

@@ -81,7 +81,7 @@ Drupal.rules = Drupal.rules || {};
 
     this.jqObject.bind("autocompleteselect", function(event, ui) {
       // If a group was selected then set the groupSelected to true for the
-      // overriden close function from jquery autocomplete.
+      // overridden close function from jquery autocomplete.
       if (ui.item.value.substring(ui.item.value.length - 1, ui.item.value.length) == ":") {
         instance.groupSelected = true;
       }
@@ -103,14 +103,18 @@ Drupal.rules = Drupal.rules || {};
       });
     });
 
+    // Newer versions of jQuery UI use element.data('ui-autocomplete'), older
+    // versions use element.data('autocomplete').
+    var autocompleteDataKey = typeof(this.jqObject.data('autocomplete')) === 'object' ? 'autocomplete' : 'ui-autocomplete';
+
     // Since jquery autocomplete by default strips html text by using .text()
     // we need our own _renderItem function to display html content.
-    this.jqObject.data("autocomplete")._renderItem = function(ul, item) {
+    this.jqObject.data(autocompleteDataKey)._renderItem = function(ul, item) {
       return $("<li></li>").data("item.autocomplete", item).append("<a>" + item.label + "</a>").appendTo(ul);
     };
 
     // Override close function
-    this.jqObject.data("autocomplete").close = function (event) {
+    this.jqObject.data(autocompleteDataKey).close = function (event) {
       var value = this.element.val();
       // If the selector is not a group, then trigger the close event an and
       // hide the menu.
@@ -119,7 +123,10 @@ Drupal.rules = Drupal.rules || {};
         if (this.menu.element.is(":visible")) {
           this._trigger("close", event);
           this.menu.element.hide();
-          this.menu.deactivate();
+          // Use deactivate for older versions of jQuery UI.
+          if (typeof(this.menu.deactivate) === 'function') {
+            this.menu.deactivate();
+          }
         }
       }
       else {
@@ -173,7 +180,7 @@ Drupal.rules = Drupal.rules || {};
   };
 
   /**
-   * Toogle the autcomplete window.
+   * Toggle the autocomplete window.
    */
   Drupal.rules.autocomplete.prototype.toggle = function() {
     if (this.jqObject.autocomplete("widget").is(":visible")) {

+ 9 - 7
sites/all/modules/rules/ui/rules.ui.css

@@ -1,10 +1,12 @@
-@CHARSET "UTF-8";
+@charset "utf-8";
 
-.rules-show-js, html.js .rules-hide-js {
+.rules-show-js,
+html.js .rules-hide-js {
   display: none;
 }
 
-.rules-hide-js, html.js .rules-show-js {
+.rules-hide-js,
+html.js .rules-show-js {
   display: block;
 }
 
@@ -58,7 +60,8 @@ ul.rules-operations-add li {
   right: 0px;
 }
 
-.rules-elements-table caption, .rules-overview-table caption {
+.rules-elements-table caption,
+.rules-overview-table caption {
   font-size: 110%;
   font-weight: bold;
   padding-bottom: 0.5em;
@@ -85,8 +88,7 @@ ul.rules-operations-add li {
 .rules-debug-collapsible-link {
   position: relative;
   cursor: pointer;
-
-  /* The span element with the icon which opens the log, has a whitepsace.
+  /* The span element with the icon which opens the log, has a whitespace.
      Since we don't want the user to mark this white space, we prevent this
      using the this code.*/
   -moz-user-select: -moz-none;
@@ -191,6 +193,6 @@ ul.rules-autocomplete .ui-corner-all {
 }
 
 /* IE 6 hack for max-height. */
-* html ul.rule-autocomplete{
+* html ul.rule-autocomplete {
   height: 23em;
 }

+ 27 - 15
sites/all/modules/rules/ui/ui.controller.inc

@@ -1,7 +1,8 @@
 <?php
 
 /**
- * @file Contains the UI controller for Rules.
+ * @file
+ * Contains the UI controller for Rules.
  */
 
 /**
@@ -35,8 +36,8 @@ class RulesUIController {
     );
     $items[$base_path . '/manage/%rules_config/add/%rules_element'] = array(
       // Adding another part to the path would hit the menu path-part-limit
-      // for base paths like admin/config/workflow/rules. Therefor we have to
-      // use this fugly way for setting the title.
+      // for base paths like admin/config/workflow/rules. Therefore we have to
+      // use this ugly way for setting the title.
       'title callback' => 'rules_menu_add_element_title',
       // Wrap the integer in an array, so it is passed as is.
       'title arguments' => array(array($base_count + 4)),
@@ -52,7 +53,7 @@ class RulesUIController {
       'title callback' => 'rules_get_title',
       'title arguments' => array('Adding event to !plugin "!label"', $base_count + 1),
       'page callback' => 'drupal_get_form',
-      'page arguments' => array('rules_ui_add_event', $base_count + 1, $base_path),
+      'page arguments' => array('rules_ui_add_event_page', $base_count + 1, $base_path),
       'access callback' => 'rules_config_access',
       'access arguments' => array('update', $base_count + 1),
       'load arguments' => array($base_count + 1),
@@ -60,7 +61,7 @@ class RulesUIController {
       'file path' => drupal_get_path('module', 'rules'),
     );
     $items[$base_path . '/manage/%rules_config/delete/event'] = array(
-      //@todo: improve title.
+      // @todo Improve title.
       'title' => 'Remove event',
       'page callback' => 'drupal_get_form',
       'page arguments' => array('rules_ui_remove_event', $base_count + 1, $base_count + 4, $base_path),
@@ -157,8 +158,10 @@ class RulesUIController {
   }
 
   /**
-   * Generates the render array for a overview configuration table for arbitrary
-   * rule configs that match the given conditions.
+   * Generates the render array for an overview configuration table.
+   *
+   * Generates the render array for an overview configuration table for
+   * arbitrary rule configs that match the given conditions.
    *
    * Note: The generated overview table contains multiple links for editing the
    * rule configurations. For the links to properly work use
@@ -182,7 +185,7 @@ class RulesUIController {
    *     currently set RulesPluginUI::$basePath. If no base path has been set
    *     yet, the current path is used by default.
    *
-   * @return Array
+   * @return array
    *   A renderable array.
    */
   public function overviewTable($conditions = array(), $options = array()) {
@@ -192,10 +195,14 @@ class RulesUIController {
       'show events' => isset($conditions['plugin']) && $conditions['plugin'] == 'reaction rule',
       'show execution op' => !(isset($conditions['plugin']) && $conditions['plugin'] == 'reaction rule'),
     );
+    // By default show only configurations owned by rules.
+    $conditions += array(
+      'owner' => 'rules',
+    );
     if (!empty($options['base path'])) {
       RulesPluginUI::$basePath = $options['base path'];
     }
-    else if (!isset(RulesPluginUI::$basePath)) {
+    elseif (!isset(RulesPluginUI::$basePath)) {
       // Default to the current path, only if no path has been set yet.
       RulesPluginUI::$basePath = current_path();
     }
@@ -237,7 +244,7 @@ class RulesUIController {
     $table['#attributes']['class'][] = 'rules-overview-table';
     $table['#attached']['css'][] = drupal_get_path('module', 'rules') . '/ui/rules.ui.css';
 
-    // TODO: hide configs where access() is FALSE.
+    // @todo Hide configs where access() is FALSE.
     return $table;
   }
 
@@ -257,8 +264,8 @@ class RulesUIController {
       $events = array();
       if ($config instanceof RulesTriggerableInterface) {
         foreach ($config->events() as $event_name) {
-          $this->event_info += array($event_name => array('label' => t('Unknown event "!event_name"', array('!event_name' => $event_name))));
-          $events[] = check_plain($this->event_info[$event_name]['label']);
+          $event_handler = rules_get_event_handler($event_name, $config->getEventSettings($event_name));
+          $events[] = $event_handler->summary();
         }
       }
       $row[] = implode(", ", $events);
@@ -275,12 +282,16 @@ class RulesUIController {
 
     // Add operations depending on the options and the exportable status.
     if (!$config->hasStatus(ENTITY_FIXED)) {
-      $row[] =  l(t('edit'), RulesPluginUI::path($name), array('attributes' => array('class' => array('edit', 'action'))));
-      $row[] =  l(t('translate'), RulesPluginUI::path($name, 'translate'), array('attributes' => array('class' => array('translate', 'action'))));
+      $row[] = l(t('edit'), RulesPluginUI::path($name), array('attributes' => array('class' => array('edit', 'action'))));
+      if (module_exists('rules_i18n')) {
+        $row[] = l(t('translate'), RulesPluginUI::path($name, 'translate'), array('attributes' => array('class' => array('translate', 'action'))));
+      }
     }
     else {
       $row[] = '';
-      $row[] = '';
+      if (module_exists('rules_i18n')) {
+        $row[] = '';
+      }
     }
 
     if (!$options['hide status op']) {
@@ -313,4 +324,5 @@ class RulesUIController {
     $row[] = l(t('export'), RulesPluginUI::path($name, 'export'), array('attributes' => array('class' => array('export', 'action'))));
     return $row;
   }
+
 }

+ 213 - 87
sites/all/modules/rules/ui/ui.core.inc

@@ -1,7 +1,8 @@
 <?php
 
 /**
- * @file Contains core ui functions.
+ * @file
+ * Contains core Rules UI functions.
  */
 
 /**
@@ -10,9 +11,10 @@
 interface RulesPluginUIInterface {
 
   /**
-   * Adds the whole configuration form of this rules configuration. For rule
-   * elements that are part of a configuration this method just adds the
-   * elements configuration form.
+   * Adds the whole configuration form of this rules configuration.
+   *
+   * For rule elements that are part of a configuration this method just adds
+   * the elements configuration form.
    *
    * @param $form
    *   The form array where to add the form.
@@ -37,8 +39,7 @@ interface RulesPluginUIInterface {
    *    - 'restrict events': Optionally set an array of event names to restrict
    *      the events that are available for adding.
    *
-   *  @todo
-   *    Implement the 'restrict *' options.
+   * @todo Implement the 'restrict *' options.
    */
   public function form(&$form, &$form_state, $options = array());
 
@@ -51,6 +52,8 @@ interface RulesPluginUIInterface {
   public function form_validate($form, &$form_state);
 
   /**
+   * Form submit handler for the element configuration form.
+   *
    * Submit the configuration form of this rule element. This makes sure to
    * put the updated configuration in the form state. For saving changes
    * permanently, just call $config->save() afterwards.
@@ -135,11 +138,13 @@ class RulesElementMap {
     }
     return isset($this->index[$id]) ? $this->index[$id] : FALSE;
   }
+
 }
 
 /**
- * Faces UI extender for all kind of Rules plugins. Provides various useful
- * methods for any rules UI.
+ * Faces UI extender for all kind of Rules plugins.
+ *
+ * Provides various useful methods for any rules UI.
  */
 class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
 
@@ -149,10 +154,11 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
   protected $element;
 
   /**
-   * The base path determines where a Rules overview UI lives. All forms that
-   * want to display Rules (overview) forms need to set this variable. This is
-   * necessary in order to get correct operation links, paths, redirects, bread
-   * crumbs etc. for the form() and overviewTable() methods.
+   * The base path determines where a Rules overview UI lives.
+   *
+   * All forms that want to display Rules (overview) forms need to set this
+   * variable. This is necessary in order to get correct operation links,
+   * paths, redirects, breadcrumbs etc. for the form() and overviewTable() methods.
    *
    * @see RulesUIController
    * @see rules_admin_reaction_overview()
@@ -193,8 +199,7 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
   }
 
   /**
-   * Implements RulesPluginUIInterface.
-   * Generates the element edit form.
+   * Implements RulesPluginUIInterface. Generates the element edit form.
    *
    * Note: Make sure that you set RulesPluginUI::$basePath before using this
    * method, otherwise paths, links, redirects etc. won't be correct.
@@ -257,7 +262,7 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
       );
     }
     if (!empty($form['provides'])) {
-      $help = '<div class="description">' . t('Adjust the names and labels of provided variables, but note that renaming of already utilizied variables invalidates the existing uses.') . '</div>';
+      $help = '<div class="description">' . t('Adjust the names and labels of provided variables, but note that renaming of already utilized variables invalidates the existing uses.') . '</div>';
       $form['provides'] += array(
         '#tree' => TRUE,
         '#prefix' => '<h4 class="rules-form-heading">' . t('Provided variables') . '</h4>' . $help,
@@ -304,8 +309,8 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
     }
 
     // For translatable parameters, pre-populate an internal translation source
-    // key so data type forms or input evaluators (i18n) may produce suiting
-    // help.
+    // key so data type forms or input evaluators (i18n) may show a suitable
+    // help message.
     if (drupal_multilingual() && !empty($info['translatable'])) {
       $parameter = $this->element->pluginParameterInfo();
       $info['custom translation language'] = !empty($parameter['language']);
@@ -320,7 +325,7 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
     }
 
     // Add a link for switching the input mode when JS is enabled and a button
-    // to switch it without javascript, in case switching is possible.
+    // to switch it without JavaScript, in case switching is possible.
     if ($supports_input_mode && empty($info['restriction'])) {
       $value = $mode == 'selector' ? t('Switch to the direct input mode') : t('Switch to data selection');
 
@@ -420,6 +425,8 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
       '#required' => TRUE,
       '#weight' => -5,
     );
+    // @todo For Drupal 8 use "owner" for generating machine names and
+    // module only for the modules providing default configurations.
     if (!empty($this->element->module) && !empty($this->element->name) && $this->element->module == 'rules' && strpos($this->element->name, 'rules_') === 0) {
       // Remove the Rules module prefix from the machine name.
       $machine_name = substr($this->element->name, strlen($this->element->module) + 1);
@@ -455,7 +462,7 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
         $description = t('The variables used by the component. They can not be edited for configurations that are provided in code.');
       }
       else {
-        $description = t('Variables are normally input <em>parameters</em> for the component – data that should be available for the component to act on. Additionaly, action components may <em>provide</em> variables back to the caller. Each variable must have a specified data type, a label and a unique machine readable name containing only lowercase alphanumeric characters and underscores. See <a href="@url">the online documentation</a> for more information about variables.',
+        $description = t('Variables are normally input <em>parameters</em> for the component – data that should be available for the component to act on. Additionally, action components may <em>provide</em> variables back to the caller. Each variable must have a specified data type, a label and a unique machine readable name containing only lowercase alphanumeric characters and underscores. See <a href="@url">the online documentation</a> for more information about variables.',
           array('@url' => rules_external_help('variables'))
         );
       }
@@ -500,7 +507,7 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
           '#type' => 'checkbox',
           '#title' => t('Configure access for using this component with a permission.'),
           '#default_value' => !empty($this->element->access_exposed),
-          '#description' => t('By default, the @plugin-type for using this component may be only used by users that have access to configure the component. If checked, access is determined by a permission instead.', array('@plugin-type' => $plugin_type))
+          '#description' => t('By default, the @plugin-type for using this component may be only used by users that have access to configure the component. If checked, access is determined by a permission instead.', array('@plugin-type' => $plugin_type)),
         );
         $form['settings']['access']['permissions'] = array(
           '#type' => 'container',
@@ -514,7 +521,7 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
       }
     }
 
-    // TODO: Attach field form thus description.
+    // @todo Attach field form thus description.
   }
 
   /**
@@ -565,11 +572,11 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
     $form_values = RulesPluginUI::getFormStateValues($form['settings'], $form_state);
     $this->element->label = $form_values['label'];
     // If the name was changed we have to redirect to the URL that contains
-    // the new name, instead of rebuilding on the old URL with the old name
+    // the new name, instead of rebuilding on the old URL with the old name.
     if ($form['settings']['name']['#default_value'] != $form_values['name']) {
       $module = isset($this->element->module) ? $this->element->module : 'rules';
       $this->element->name = $module . '_' . $form_values['name'];
-      $form_state['redirect'] = RulesPluginUI::path($this->element->name);
+      $form_state['redirect'] = RulesPluginUI::path($this->element->name, 'edit', $this->element);
     }
     $this->element->tags = empty($form_values['tags']) ? array() : drupal_explode_tags($form_values['tags']);
 
@@ -667,22 +674,28 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
    * Returns the name of class for the given data type.
    *
    * @param $data_type
-   *   The name of the data typ
+   *   The name of the data type
    * @param $parameter_info
-   *   (optional) An array of info about the to be configured parameter.
+   *   (optional) An array of info about the to be configured parameter. If
+   *   given, this array is complemented with data type defaults also.
    */
-  public function getDataTypeClass($data_type, $parameter_info = array()) {
-    if (!empty($parameter_info['ui class'])) {
-      return $parameter_info['ui class'];
-    }
+  public function getDataTypeClass($data_type, &$parameter_info = array()) {
     $cache = rules_get_cache();
     $data_info = $cache['data_info'];
-    return (is_string($data_type) && isset($data_info[$data_type]['ui class'])) ? $data_info[$data_type]['ui class'] : 'RulesDataUI';
+    // Add in data-type defaults.
+    if (empty($parameter_info['ui class'])) {
+      $parameter_info['ui class'] = (is_string($data_type) && isset($data_info[$data_type]['ui class'])) ? $data_info[$data_type]['ui class'] : 'RulesDataUI';
+    }
+    if (is_subclass_of($parameter_info['ui class'], 'RulesDataInputOptionsListInterface')) {
+      $parameter_info['options list'] = array($parameter_info['ui class'], 'optionsList');
+    }
+    return $parameter_info['ui class'];
   }
 
   /**
    * Implements RulesPluginUIInterface.
-   * Show a preview of the configuration settings.
+   *
+   * Shows a preview of the configuration settings.
    */
   public function buildContent() {
     $config_name = $this->element->root()->name;
@@ -691,7 +704,7 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
       '#title' => $this->element->label(),
       '#href' => $this->element->isRoot() ? RulesPluginUI::path($config_name) : RulesPluginUI::path($config_name, 'edit', $this->element),
       '#prefix' => '<div class="rules-element-label">',
-      '#suffix' => '</div>'
+      '#suffix' => '</div>',
     );
     // Put the elements below in a "description" div.
     $content['description'] = array(
@@ -705,14 +718,14 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
       $element = array();
       if (!empty($this->element->settings[$name . ':select'])) {
         $element['content'] = array(
-         '#markup' => '[' . $this->element->settings[$name . ':select'] . ']',
+          '#markup' => '[' . $this->element->settings[$name . ':select'] . ']',
         );
       }
       elseif (isset($this->element->settings[$name]) && (!isset($parameter['default value']) || $parameter['default value'] != $this->element->settings[$name])) {
-        $method = empty($parameter['options list']) ? 'render' : 'renderOptionsLabel';
         $class = $this->getDataTypeClass($parameter['type'], $parameter);
-        // We cannot use method_exists() here as it would trigger a PHP bug,
-        // @see http://drupal.org/node/1258284
+        $method = empty($parameter['options list']) ? 'render' : 'renderOptionsLabel';
+        // We cannot use method_exists() here as it would trigger a PHP bug.
+        // @see https://www.drupal.org/node/1258284
         $element = call_user_func(array($class, $method), $this->element->settings[$name], $name, $parameter, $this->element);
       }
       // Only add parameters that are really configured / not default.
@@ -795,7 +808,6 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
    */
   public function help() {}
 
-
   /**
    * Deprecated by the controllers overviewTable() method.
    */
@@ -804,6 +816,8 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
   }
 
   /**
+   * Generates an operation path.
+   *
    * Generates a path using the given operation for the element with the given
    * id of the configuration with the given name.
    */
@@ -817,12 +831,19 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
     else {
       $base_path = isset($element) && $element instanceof RulesTriggerableInterface ? 'admin/config/workflow/rules/reaction' : 'admin/config/workflow/rules/components';
     }
-    return implode('/', array_filter(array($base_path . '/manage', $name, $op, $element_id, $parameter)));
+
+    // Only append the '/manage' path if it is not already present.
+    if (substr($base_path, -strlen('/manage')) != '/manage') {
+      $base_path .= '/manage';
+    }
+
+    return implode('/', array_filter(array($base_path, $name, $op, $element_id, $parameter)));
   }
 
   /**
-   * Determines the default redirect target for an edited/deleted element. This
-   * is a parent element which is either a rule or the configuration root.
+   * Determines the default redirect target for an edited/deleted element.
+   *
+   * This is a parent element which is either a rule or the configuration root.
    */
   public static function defaultRedirect(RulesPlugin $element) {
     while (!$element->isRoot()) {
@@ -835,47 +856,10 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
   }
 
   /**
-   * Returns an array of options to use with a select for the items specified
-   * in the given hook.
-   *
-   * @param $item_type
-   *   The item type to get options for. One of 'data', 'event', 'condition' and
-   *   'action'.
-   * @param $items
-   *   (optional) An array of items to restrict the options to.
-   *
-   * @return
-   *   An array of options.
+   * @see RulesUICategory::getOptions()
    */
   public static function getOptions($item_type, $items = NULL) {
-    $sorted_data = array();
-    $ungrouped = array();
-    $data = $items ? $items : rules_fetch_data($item_type . '_info');
-    foreach ($data as $name => $info) {
-      // Verfiy the current user has access to use it.
-      if (!user_access('bypass rules access') && !empty($info['access callback']) && !call_user_func($info['access callback'], $item_type, $name)) {
-        continue;
-      }
-      if (!empty($info['group'])) {
-        $sorted_data[drupal_ucfirst($info['group'])][$name] = drupal_ucfirst($info['label']);
-      }
-      else {
-        $ungrouped[$name] = drupal_ucfirst($info['label']);
-      }
-    }
-    asort($ungrouped);
-    foreach ($sorted_data as $key => $choices) {
-      asort($choices);
-      $sorted_data[$key] = $choices;
-    }
-    ksort($sorted_data);
-    // Always move the 'Components' group down it it exists.
-    if (isset($sorted_data[t('Components')])) {
-      $copy = $sorted_data[t('Components')];
-      unset($sorted_data[t('Components')]);
-      $sorted_data[t('Components')] = $copy;
-    }
-    return $ungrouped + $sorted_data;
+    return RulesUICategory::getOptions($item_type, $items = NULL);
   }
 
   public static function formDefaults(&$form, &$form_state) {
@@ -909,6 +893,7 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
       ->fetchCol('tag');
     return drupal_map_assoc($result);
   }
+
 }
 
 /**
@@ -917,6 +902,8 @@ class RulesPluginUI extends FacesExtender implements RulesPluginUIInterface {
 class RulesAbstractPluginUI extends RulesPluginUI {
 
   /**
+   * Overrides RulesPluginUI::form().
+   *
    * Overridden to invoke the abstract plugins form alter callback and to add
    * the negation checkbox for conditions.
    */
@@ -953,6 +940,7 @@ class RulesAbstractPluginUI extends RulesPluginUI {
       form_set_error(implode('][', $e->keys), $e->getMessage());
     }
   }
+
 }
 
 /**
@@ -971,11 +959,11 @@ class RulesContainerPluginUI extends RulesPluginUI {
       '#tree' => TRUE,
       '#theme' => 'rules_elements',
       '#empty' => t('None'),
-      '#caption' => t('Elements')
+      '#caption' => t('Elements'),
     );
     $form['elements']['#attributes']['class'][] = 'rules-container-plugin';
 
-    // Recurse over all element childrens or use the provided iterator.
+    // Recurse over all element children or use the provided iterator.
     $iterator = isset($iterator) ? $iterator : $this->element->elements();
     $root_depth = $this->element->depth();
     foreach ($iterator as $key => $child) {
@@ -991,11 +979,11 @@ class RulesContainerPluginUI extends RulesPluginUI {
       $form['elements'][$id]['weight'] = array(
         '#type' => 'weight',
         '#default_value' => $child->weight,
-        '#delta' => 20,
+        '#delta' => 50,
       );
       $form['elements'][$id]['parent_id'] = array(
         '#type' => 'hidden',
-        // If another iterator is passed in, the childs parent may not equal
+        // If another iterator is passed in, the child parent may not equal
         // the current element. Thus ask the child for its parent.
         '#default_value' => $child->parentElement()->elementId(),
       );
@@ -1061,12 +1049,10 @@ class RulesContainerPluginUI extends RulesPluginUI {
     return $render;
   }
 
-
   public function buildContent() {
     $content = parent::buildContent();
     // Don't link the title for embedded container plugins, except for rules.
     if (!$this->element->isRoot() && !($this->element instanceof Rule)) {
-      $content['label']['#type'] = 'markup';
       $content['label']['#markup'] = check_plain($content['label']['#title']);
       unset($content['label']['#title']);
     }
@@ -1109,6 +1095,7 @@ class RulesContainerPluginUI extends RulesPluginUI {
     }
     return $content;
   }
+
 }
 
 /**
@@ -1123,7 +1110,7 @@ class RulesConditionContainerUI extends RulesContainerPluginUI {
     $form['elements']['#attributes']['class'][] = 'rules-condition-container';
     $form['elements']['#caption'] = t('Conditions');
 
-    // By default skip
+    // By default skip.
     if (!empty($options['init']) && !$this->element->isRoot()) {
       $config = $this->element->root();
       $form['init_help'] = array(
@@ -1154,6 +1141,7 @@ class RulesConditionContainerUI extends RulesContainerPluginUI {
       $this->element->negate($form_values['negate']);
     }
   }
+
 }
 
 /**
@@ -1162,10 +1150,148 @@ class RulesConditionContainerUI extends RulesContainerPluginUI {
 class RulesActionContainerUI extends RulesContainerPluginUI {
 
   public function form(&$form, &$form_state, $options = array(), $iterator = NULL) {
-    parent::form($form,  $form_state, $options, $iterator);
+    parent::form($form, $form_state, $options, $iterator);
     // Add the add-* operation links.
     $form['elements']['#add'] = self::addOperations();
     $form['elements']['#attributes']['class'][] = 'rules-action-container';
     $form['elements']['#caption'] = t('Actions');
   }
+
+}
+
+/**
+ * Class holding category related methods.
+ */
+class RulesUICategory {
+
+  /**
+   * Gets info about all available categories, or about a specific category.
+   *
+   * @return array
+   */
+  public static function getInfo($category = NULL) {
+    $data = rules_fetch_data('category_info');
+    if (isset($category)) {
+      return $data[$category];
+    }
+    return $data;
+  }
+
+  /**
+   * Returns a group label, e.g. as usable for opt-groups in a select list.
+   *
+   * @param array $item_info
+   *   The info-array of an item, e.g. an entry of hook_rules_action_info().
+   * @param bool $in_category
+   *   (optional) Whether group labels for grouping inside a category should be
+   *   return. Defaults to FALSE.
+   *
+   * @return string|bool
+   *   The group label to use, or FALSE if none can be found.
+   */
+  public static function getItemGroup($item_info, $in_category = FALSE) {
+    if (isset($item_info['category']) && !$in_category) {
+      return self::getCategory($item_info, 'label');
+    }
+    elseif (!empty($item_info['group'])) {
+      return $item_info['group'];
+    }
+    return FALSE;
+  }
+
+  /**
+   * Gets the category for the given item info array.
+   *
+   * @param array $item_info
+   *   The info-array of an item, e.g. an entry of hook_rules_action_info().
+   * @param string|null $key
+   *   (optional) The key of the category info to return, e.g. 'label'. If none
+   *   is given the whole info array is returned.
+   *
+   * @return array|mixed|false
+   *   Either the whole category info array or the value of the given key. If
+   *   no category can be found, FALSE is returned.
+   */
+  public static function getCategory($item_info, $key = NULL) {
+    if (isset($item_info['category'])) {
+      $info = self::getInfo($item_info['category']);
+      return isset($key) ? $info[$key] : $info;
+    }
+    return FALSE;
+  }
+
+  /**
+   * Returns an array of options to use with a select.
+   *
+   * Returns an array of options to use with a selectfor the items specified
+   * in the given hook.
+   *
+   * @param $item_type
+   *   The item type to get options for. One of 'data', 'event', 'condition' and
+   *   'action'.
+   * @param $items
+   *   (optional) An array of items to restrict the options to.
+   *
+   * @return array
+   *   An array of options.
+   */
+  public static function getOptions($item_type, $items = NULL) {
+    $sorted_data = array();
+    $ungrouped = array();
+    $data = $items ? $items : rules_fetch_data($item_type . '_info');
+    foreach ($data as $name => $info) {
+      // Verify the current user has access to use it.
+      if (!user_access('bypass rules access') && !empty($info['access callback']) && !call_user_func($info['access callback'], $item_type, $name)) {
+        continue;
+      }
+      if ($group = RulesUICategory::getItemGroup($info)) {
+        $sorted_data[drupal_ucfirst($group)][$name] = drupal_ucfirst($info['label']);
+      }
+      else {
+        $ungrouped[$name] = drupal_ucfirst($info['label']);
+      }
+    }
+    asort($ungrouped);
+    foreach ($sorted_data as $key => $choices) {
+      asort($choices);
+      $sorted_data[$key] = $choices;
+    }
+
+    // Sort the grouped data by category weights, defaulting to weight 0 for
+    // groups without a respective category.
+    $sorted_groups = array();
+    foreach (array_keys($sorted_data) as $label) {
+      $sorted_groups[$label] = array('weight' => 0, 'label' => $label);
+    }
+    // Add in category weights.
+    foreach (RulesUICategory::getInfo() as $info) {
+      if (isset($sorted_groups[$info['label']])) {
+        $sorted_groups[$info['label']] = $info;
+      }
+    }
+    uasort($sorted_groups, '_rules_ui_sort_categories');
+
+    // Now replace weights with group content.
+    foreach ($sorted_groups as $group => $weight) {
+      $sorted_groups[$group] = $sorted_data[$group];
+    }
+    return $ungrouped + $sorted_groups;
+  }
+
+}
+
+/**
+ * Helper for sorting categories.
+ */
+function _rules_ui_sort_categories($a, $b) {
+  // @see element_sort()
+  $a_weight = isset($a['weight']) ? $a['weight'] : 0;
+  $b_weight = isset($b['weight']) ? $b['weight'] : 0;
+  if ($a_weight == $b_weight) {
+    // @see element_sort_by_title()
+    $a_title = isset($a['label']) ? $a['label'] : '';
+    $b_title = isset($b['label']) ? $b['label'] : '';
+    return strnatcasecmp($a_title, $b_title);
+  }
+  return ($a_weight < $b_weight) ? -1 : 1;
 }

+ 205 - 24
sites/all/modules/rules/ui/ui.data.inc

@@ -1,10 +1,10 @@
 <?php
 
 /**
- * @file Contains data type related forms.
+ * @file
+ * Contains data type related forms.
  */
 
-
 /**
  * Interface for data types providing a direct input form.
  */
@@ -13,21 +13,47 @@ interface RulesDataDirectInputFormInterface {
   /**
    * Constructs the direct input form.
    *
-   * @return Array
-   *  The direct input form.
+   * @return array
+   *   The direct input form.
    */
   public static function inputForm($name, $info, $settings, RulesPlugin $element);
 
   /**
    * Render the configured value.
    *
-   * @return Array
+   * @return array
    *   A renderable array.
    */
   public static function render($value);
 
 }
 
+/**
+ * Interface for data UI classes providing an options list.
+ */
+interface RulesDataInputOptionsListInterface extends RulesDataDirectInputFormInterface {
+
+  /**
+   * Returns the options list for the data type.
+   *
+   * For retrieving information about the used data type and parameter, the
+   * helper RulesDataUI::getTypeInfo() may be used as following:
+   * @code
+   *   list($type, $parameter_info) = RulesDataUI::getTypeInfo($element, $name);
+   * @endcode
+   *
+   * @param RulesPlugin $element
+   *   The rules element to get the options for.
+   * @param string $name
+   *   The name of the parameter for which to get options.
+   *
+   * @return array
+   *   An array of options as used by hook_options_list().
+   */
+  public static function optionsList(RulesPlugin $element, $name);
+
+}
+
 /**
  * Default UI related class for data types.
  */
@@ -113,7 +139,7 @@ class RulesDataUI {
   }
 
   /**
-   * Renders the value by making use of the label if an options list is available.
+   * Renders the value with a label if an options list is available.
    *
    * Used for data UI classes implementing the
    * RulesDataDirectInputFormInterface.
@@ -127,7 +153,7 @@ class RulesDataUI {
   public static function renderOptionsLabel($value, $name, $info, RulesPlugin $element) {
     if (!empty($info['options list'])) {
       $element->call('loadBasicInclude');
-      $options = entity_property_options_flatten($info['options list']($element, $name));
+      $options = entity_property_options_flatten(call_user_func($info['options list'], $element, $name));
       if (!is_array($value) && isset($options[$value])) {
         $value = $options[$value];
       }
@@ -145,6 +171,18 @@ class RulesDataUI {
       );
     }
   }
+
+  /**
+   * Returns the data type and parameter information for the given arguments.
+   *
+   * This helper may be used by options list callbacks operation at data-type
+   * level, see RulesDataInputOptionsListInterface.
+   */
+  public static function getTypeInfo(RulesPlugin $element, $name) {
+    $parameters = $element->pluginParameterInfo();
+    return array($parameters[$name]['type'], $parameters[$name]);
+  }
+
 }
 
 /**
@@ -152,10 +190,16 @@ class RulesDataUI {
  */
 class RulesDataUIText extends RulesDataUI implements RulesDataDirectInputFormInterface {
 
+  /**
+   * Overrides RulesDataUI::getDefaultMode().
+   */
   public static function getDefaultMode() {
     return 'input';
   }
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
     if (!empty($info['options list'])) {
       // Make sure the .rules.inc of the providing module is included as the
@@ -169,6 +213,7 @@ class RulesDataUIText extends RulesDataUI implements RulesDataDirectInputFormInt
     else {
       $form[$name] = array(
         '#type' => 'textarea',
+        '#rows' => 3,
       );
       RulesDataInputEvaluator::attachForm($form, $settings, $info, $element->availableVariables());
     }
@@ -178,17 +223,20 @@ class RulesDataUIText extends RulesDataUI implements RulesDataDirectInputFormInt
       '#default_value' => $settings[$name],
       '#required' => empty($info['optional']),
       '#after_build' => array('rules_ui_element_fix_empty_after_build'),
-      '#rows' => 3,
     );
     return $form;
   }
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::render().
+   */
   public static function render($value) {
     return array(
       'content' => array('#markup' => check_plain($value)),
       '#attributes' => array('class' => array('rules-parameter-text')),
     );
   }
+
 }
 
 /**
@@ -196,6 +244,9 @@ class RulesDataUIText extends RulesDataUI implements RulesDataDirectInputFormInt
  */
 class RulesDataUITextToken extends RulesDataUIText {
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
     $form = parent::inputForm($name, $info, $settings, $element);
     if ($form[$name]['#type'] == 'textarea') {
@@ -205,6 +256,7 @@ class RulesDataUITextToken extends RulesDataUIText {
     }
     return $form;
   }
+
 }
 
 /**
@@ -212,6 +264,9 @@ class RulesDataUITextToken extends RulesDataUIText {
  */
 class RulesDataUITextFormatted extends RulesDataUIText {
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
     $form = parent::inputForm($name, $info, $settings, $element);
     $settings += array($name => isset($info['default value']) ? $info['default value'] : array('value' => NULL, 'format' => NULL));
@@ -223,21 +278,26 @@ class RulesDataUITextFormatted extends RulesDataUIText {
     return $form;
   }
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::render().
+   */
   public static function render($value) {
     return array(
       'content' => array('#markup' => check_plain($value['value'])),
       '#attributes' => array('class' => array('rules-parameter-text-formatted')),
     );
   }
-}
-
 
+}
 
 /**
  * UI for decimal data.
  */
 class RulesDataUIDecimal extends RulesDataUIText {
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
     $form = parent::inputForm($name, $info, $settings, $element);
     if (empty($info['options list'])) {
@@ -247,6 +307,7 @@ class RulesDataUIDecimal extends RulesDataUIText {
     $form[$name]['#rows'] = 1;
     return $form;
   }
+
 }
 
 /**
@@ -254,6 +315,9 @@ class RulesDataUIDecimal extends RulesDataUIText {
  */
 class RulesDataUIInteger extends RulesDataUIText {
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
     $form = parent::inputForm($name, $info, $settings, $element);
     if (empty($info['options list'])) {
@@ -262,6 +326,28 @@ class RulesDataUIInteger extends RulesDataUIText {
     $form[$name]['#element_validate'][] = 'rules_ui_element_integer_validate';
     return $form;
   }
+
+}
+
+/**
+ * UI for IP addresses.
+ */
+class RulesDataUIIPAddress extends RulesDataUIText {
+
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
+  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
+    $form = parent::inputForm($name, $info, $settings, $element);
+    if (empty($info['options list'])) {
+      $form[$name]['#type'] = 'textfield';
+      $form[$name]['#description'] = t('If not provided, the IP address of the current user will be used.');
+    }
+    $form[$name]['#element_validate'][] = 'rules_ui_element_ip_address_validate';
+    $form[$name]['#rows'] = 1;
+    return $form;
+  }
+
 }
 
 /**
@@ -269,27 +355,40 @@ class RulesDataUIInteger extends RulesDataUIText {
  */
 class RulesDataUIBoolean extends RulesDataUI implements RulesDataDirectInputFormInterface {
 
+  /**
+   * Overrides RulesDataUI::getDefaultMode().
+   */
   public static function getDefaultMode() {
     return 'input';
   }
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
     $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
     // Note: Due to the checkbox even optional parameter always receive a value.
     $form[$name] = array(
-      '#type' => 'checkbox',
-      '#title' => check_plain($info['label']),
+      '#type' => 'radios',
       '#default_value' => $settings[$name],
+      '#options' => array(
+        TRUE => t('@label: True.', array('@label' => $info['label'])),
+        FALSE => t('@label: False.', array('@label' => $info['label'])),
+      ),
     );
     return $form;
   }
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::render().
+   */
   public static function render($value) {
     return array(
       'content' => array('#markup' => !empty($value) ? t('true') : t('false')),
       '#attributes' => array('class' => array('rules-parameter-boolean')),
     );
   }
+
 }
 
 /**
@@ -297,6 +396,9 @@ class RulesDataUIBoolean extends RulesDataUI implements RulesDataDirectInputForm
  */
 class RulesDataUIDate extends RulesDataUIText {
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
     $settings += array($name => isset($info['default value']) ? $info['default value'] : (empty($info['optional']) ? gmdate('Y-m-d H:i:s', time()) : NULL));
 
@@ -313,11 +415,14 @@ class RulesDataUIDate extends RulesDataUIText {
       array('%format' => gmdate('Y-m-d H:i:s', time() + 86400),
             '!strtotime' => l('strtotime()', 'http://php.net/strtotime')));
 
-    //TODO: Leverage the jquery datepicker+timepicker once a module providing
-    //the timpeicker is available.
+    // @todo Leverage the jquery datepicker+timepicker once a module providing
+    // The timepicker is available.
     return $form;
   }
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::render().
+   */
   public static function render($value) {
     $value = is_numeric($value) ? format_date($value, 'short') : check_plain($value);
     return array(
@@ -325,6 +430,7 @@ class RulesDataUIDate extends RulesDataUIText {
       '#attributes' => array('class' => array('rules-parameter-date')),
     );
   }
+
 }
 
 /**
@@ -332,6 +438,9 @@ class RulesDataUIDate extends RulesDataUIText {
  */
 class RulesDataUIDuration extends RulesDataUIText {
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
     $form = parent::inputForm($name, $info, $settings, $element);
     $form[$name]['#type'] = 'rules_duration';
@@ -339,6 +448,9 @@ class RulesDataUIDuration extends RulesDataUIText {
     return $form;
   }
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::render().
+   */
   public static function render($value) {
     $value = is_numeric($value) ? format_interval($value) : check_plain($value);
     return array(
@@ -346,6 +458,7 @@ class RulesDataUIDuration extends RulesDataUIText {
       '#attributes' => array('class' => array('rules-parameter-duration')),
     );
   }
+
 }
 
 /**
@@ -353,12 +466,16 @@ class RulesDataUIDuration extends RulesDataUIText {
  */
 class RulesDataUIURI extends RulesDataUIText {
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
     $form = parent::inputForm($name, $info, $settings, $element);
     $form[$name]['#rows'] = 1;
-    $form[$name]['#description'] = t('You may enter relative URLs like %url as well as absolute URLs like %absolute-url.', array('%url' => 'user/login?destination=node', '%absolute-url' => 'http://drupal.org'));
+    $form[$name]['#description'] = t('You may enter relative URLs like %url as well as absolute URLs like %absolute-url.', array('%url' => 'user/login?destination=node', '%absolute-url' => 'https://www.drupal.org'));
     return $form;
   }
+
 }
 
 /**
@@ -366,11 +483,16 @@ class RulesDataUIURI extends RulesDataUIText {
  */
 class RulesDataUIListText extends RulesDataUIText {
 
+  /**
+   * Overrides RulesDataUI::getDefaultMode().
+   */
   public static function getDefaultMode() {
     return 'input';
   }
 
   /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   *
    * @todo This does not work for inputting textual values including "\n".
    */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
@@ -391,12 +513,16 @@ class RulesDataUIListText extends RulesDataUIText {
     return $form;
   }
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::render().
+   */
   public static function render($value) {
     return array(
       'content' => array('#markup' => check_plain(implode(', ', $value))),
       '#attributes' => array('class' => array('rules-parameter-list')),
     );
   }
+
 }
 
 /**
@@ -404,6 +530,9 @@ class RulesDataUIListText extends RulesDataUIText {
  */
 class RulesDataUIListInteger extends RulesDataUIListText {
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
     $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
     $form = parent::inputForm($name, $info, $settings, $element);
@@ -417,6 +546,7 @@ class RulesDataUIListInteger extends RulesDataUIListText {
     }
     return $form;
   }
+
 }
 
 /**
@@ -424,6 +554,9 @@ class RulesDataUIListInteger extends RulesDataUIListText {
  */
 class RulesDataUIListToken extends RulesDataUIListInteger {
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
     $form = parent::inputForm($name, $info, $settings, $element);
 
@@ -433,6 +566,7 @@ class RulesDataUIListToken extends RulesDataUIListInteger {
     }
     return $form;
   }
+
 }
 
 /**
@@ -440,10 +574,16 @@ class RulesDataUIListToken extends RulesDataUIListInteger {
  */
 class RulesDataUIEntity extends RulesDataUIText {
 
+  /**
+   * Overrides RulesDataUI::getDefaultMode().
+   */
   public static function getDefaultMode() {
     return 'selector';
   }
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
     $form = parent::inputForm($name, $info, $settings, $element);
     if (empty($info['options list'])) {
@@ -459,6 +599,7 @@ class RulesDataUIEntity extends RulesDataUIText {
     }
     return $form;
   }
+
 }
 
 /**
@@ -466,37 +607,73 @@ class RulesDataUIEntity extends RulesDataUIText {
  */
 class RulesDataUIEntityExportable extends RulesDataUIEntity {
 
+  /**
+   * Overrides RulesDataUI::getDefaultMode().
+   */
   public static function getDefaultMode() {
     return 'input';
   }
+
 }
 
 /**
- * UI for taxonomy vocabularies.
+ * Data UI variant displaying a select list of available bundle entities.
  *
- * @see RulesTaxonomyVocabularyWrapper
+ * This is used for "bundle entities" implemented via the 'bundle of' feature
+ * of entity.module.
  */
-class RulesDataUITaxonomyVocabulary extends RulesDataUIEntity {
+class RulesDataUIBundleEntity extends RulesDataUIEntity implements RulesDataInputOptionsListInterface {
 
+  /**
+   * Overrides RulesDataUI::getDefaultMode().
+   */
   public static function getDefaultMode() {
     return 'input';
   }
 
-  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
-    // Add an options list of all vocabularies if there is none yet.
-    if (!isset($info['options list'])) {
-      $info['options list'] = array('RulesDataUITaxonomyVocabulary', 'optionsList');
+  /**
+   * Implements RulesDataInputOptionsListInterface::optionsList().
+   */
+  public static function optionsList(RulesPlugin $element, $name) {
+    list($data_type, $parameter_info) = RulesDataUI::getTypeInfo($element, $name);
+    $bundles = array();
+    $entity_info = entity_get_info();
+    $bundle_of_type = $entity_info[$data_type]['bundle of'];
+    if (isset($entity_info[$bundle_of_type]['bundles'])) {
+      foreach ($entity_info[$bundle_of_type]['bundles'] as $bundle_name => $bundle_info) {
+        $bundles[$bundle_name] = $bundle_info['label'];
+      }
     }
-    return parent::inputForm($name, $info, $settings, $element);
+    return $bundles;
   }
 
-  public static function optionsList() {
+}
+
+/**
+ * UI for taxonomy vocabularies.
+ *
+ * @see RulesTaxonomyVocabularyWrapper
+ */
+class RulesDataUITaxonomyVocabulary extends RulesDataUIEntity implements RulesDataInputOptionsListInterface {
+
+  /**
+   * Overrides RulesDataUI::getDefaultMode().
+   */
+  public static function getDefaultMode() {
+    return 'input';
+  }
+
+  /**
+   * Implements RulesDataInputOptionsListInterface::optionsList().
+   */
+  public static function optionsList(RulesPlugin $element, $name) {
     $options = array();
     foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocab) {
       $options[$machine_name] = $vocab->name;
     }
     return $options;
   }
+
 }
 
 /**
@@ -504,6 +681,9 @@ class RulesDataUITaxonomyVocabulary extends RulesDataUIEntity {
  */
 class RulesDataUIListEntity extends RulesDataUIListInteger {
 
+  /**
+   * Implements RulesDataDirectInputFormInterface::inputForm().
+   */
   public static function inputForm($name, $info, $settings, RulesPlugin $element) {
     $form = parent::inputForm($name, $info, $settings, $element);
     if (empty($info['options list'])) {
@@ -518,4 +698,5 @@ class RulesDataUIListEntity extends RulesDataUIListInteger {
     }
     return $form;
   }
+
 }

+ 129 - 57
sites/all/modules/rules/ui/ui.forms.inc

@@ -1,7 +1,8 @@
 <?php
 
 /**
- * @file Rules UI forms
+ * @file
+ * Rules User Interface forms.
  */
 
 /**
@@ -35,7 +36,7 @@ function rules_ui_parameter_replace_submit($form, &$form_state) {
 }
 
 /**
- * General form submit handler, that rebuilds the form
+ * General form submit handler, that rebuilds the form.
  */
 function rules_form_submit_rebuild($form, &$form_state) {
   $form_state['rebuild'] = TRUE;
@@ -54,8 +55,9 @@ function rules_ui_form_edit_rules_config($form, &$form_state, $rules_config, $ba
 }
 
 /**
- * General rules configuration form validation callback. Also populates the
- * rules configuration with the form values.
+ * General rules configuration form validation callback.
+ *
+ * Also populates the rules configuration with the form values.
  */
 function rules_ui_form_rules_config_validate($form, &$form_state) {
   $form_state['rules_element']->form_validate($form, $form_state);
@@ -78,7 +80,6 @@ function rules_ui_form_edit_rules_config_submit($form, &$form_state) {
 function rules_ui_form_clone_rules_config($form, &$form_state, $rules_config, $base_path) {
   RulesPluginUI::$basePath = $base_path;
   $rules_config = clone $rules_config;
-  $rules_config->module = 'rules';
   $rules_config->id = NULL;
   $rules_config->name = '';
   $rules_config->label .= ' (' . t('cloned') . ')';
@@ -166,13 +167,29 @@ function rules_ui_confirm_operations($op, $rules_config) {
 
   switch ($op) {
     case 'enable':
-      return array(t('Are you sure you want to enable the %plugin %label?', $vars), '');
+      return array(
+        t('Are you sure you want to enable the %plugin %label?', $vars),
+        '',
+      );
+
     case 'disable':
-      return array(t('Are you sure you want to disable the %plugin %label?', $vars), '');
+      return array(
+        t('Are you sure you want to disable the %plugin %label?', $vars),
+        '',
+      );
+
     case 'revert':
-      return array(t('Are you sure you want to revert the %plugin %label?', $vars), t('This action cannot be undone.'));
+      return array(
+        t('Are you sure you want to revert the %plugin %label?', $vars),
+        t('This action cannot be undone.'),
+      );
+
     case 'delete':
-      return array(t('Are you sure you want to delete the %plugin %label?', $vars), t('This action cannot be undone.'));
+      return array(
+        t('Are you sure you want to delete the %plugin %label?', $vars),
+        t('This action cannot be undone.'),
+      );
+
     default:
       return FALSE;
   }
@@ -194,9 +211,10 @@ function rules_ui_form_rules_config_confirm_op($form, &$form_state, $rules_confi
 }
 
 /**
- * Applies the operation and returns the message to show to the user. Also the
- * operation is logged to the watchdog. Note that the string is defined two
- * times so that the translation extractor can find it.
+ * Applies the operation and returns the message to show to the user.
+ *
+ * The operation is also logged to the watchdog. Note that the string is
+ * defined two times so that the translation extractor can find it.
  */
 function rules_ui_confirm_operation_apply($op, $rules_config) {
   $vars = array('%plugin' => $rules_config->plugin(), '%label' => $rules_config->label());
@@ -291,8 +309,9 @@ function rules_ui_add_element($form, &$form_state, $rules_config, $plugin_name,
 
 /**
  * Add element submit callback.
+ *
  * Used for "abstract plugins" to create the initial element object with the
- * given implemenation name and rebuild the form.
+ * given implementation name and rebuild the form.
  */
 function rules_ui_add_element_submit($form, &$form_state) {
   $element = rules_plugin_factory($form_state['plugin'], $form_state['values']['element_name']);
@@ -349,7 +368,10 @@ function rules_ui_delete_element($form, &$form_state, $rules_config, $rules_elem
     }
   }
 
-  $confirm_question = t('Are you sure you want to delete the %element_plugin %element_name?', array('%element_plugin' => $rules_element->plugin(), '%element_name' => $rules_element->label(), '%plugin' => $rules_config->plugin(), '%label' => $rules_config->label()));
+  $confirm_question = t('Are you sure you want to delete the %element_plugin %element_name?', array(
+    '%element_plugin' => $rules_element->plugin(),
+    '%element_name' => $rules_element->label(),
+  ));
   return confirm_form($form, $confirm_question, RulesPluginUI::path($rules_config->name), t('This action cannot be undone.'), t('Delete'), t('Cancel'));
 }
 
@@ -364,7 +386,6 @@ function rules_ui_delete_element_submit($form, &$form_state) {
   }
 }
 
-
 /**
  * Configure a rule element.
  */
@@ -393,11 +414,46 @@ function rules_ui_edit_element_submit($form, &$form_state) {
   }
 }
 
+/**
+ * Form builder for the "add event" page.
+ */
+function rules_ui_add_event_page($form, &$form_state, RulesTriggerableInterface $rules_config, $base_path) {
+  RulesPluginUI::$basePath = $base_path;
+  RulesPluginUI::formDefaults($form, $form_state);
+  $form = rules_ui_add_event($form, $form_state, $rules_config, $base_path);
+  $form['#validate'][] = 'rules_ui_add_event_validate';
+  return $form;
+}
+
+/**
+ * Submit the event configuration.
+ */
+function rules_ui_add_event_page_submit($form, &$form_state) {
+  rules_ui_add_event_apply($form, $form_state);
+  $rules_config = $form_state['rules_config'];
+
+  // Tell the user if this breaks something, but let him proceed.
+  if (empty($rules_config->dirty)) {
+    try {
+      $rules_config->integrityCheck();
+    }
+    catch (RulesIntegrityException $e) {
+      $warning = TRUE;
+      drupal_set_message(t('Added the event, but it does not provide all variables utilized.'), 'warning');
+    }
+  }
+  $rules_config->save();
+  if (!isset($warning)) {
+    $events = rules_fetch_data('event_info');
+    $label = $events[$form_state['values']['event']]['label'];
+    drupal_set_message(t('Added event %event.', array('%event' => $label)));
+  }
+}
+
 /**
  * Add a new event.
  */
 function rules_ui_add_event($form, &$form_state, RulesReactionRule $rules_config, $base_path) {
-  RulesPluginUI::$basePath = $base_path;
   $form_state += array('rules_config' => $rules_config);
   $events = array_diff_key(rules_fetch_data('event_info'), array_flip($rules_config->events()));
 
@@ -409,7 +465,16 @@ function rules_ui_add_event($form, &$form_state, RulesReactionRule $rules_config
     '#title' => t('React on event'),
     '#options' => RulesPluginUI::getOptions('event', $events),
     '#description' => t('Whenever the event occurs, rule evaluation is triggered.'),
+    '#ajax' => rules_ui_form_default_ajax(),
+    '#required' => TRUE,
   );
+  if (!empty($form_state['values']['event'])) {
+    $handler = rules_get_event_handler($form_state['values']['event']);
+    $form['event_settings'] = $handler->buildForm($form_state);
+  }
+  else {
+    $form['event_settings'] = array();
+  }
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Add'),
@@ -419,47 +484,38 @@ function rules_ui_add_event($form, &$form_state, RulesReactionRule $rules_config
 }
 
 /**
- * Submit callback that just adds the selected event.
- *
- * @see rules_admin_add_reaction_rule()
+ * Validation callback for adding an event.
  */
-function rules_ui_add_event_apply($form, &$form_state) {
-  $form_state['rules_config']->event($form_state['values']['event']);
+function rules_ui_add_event_validate($form, $form_state) {
+  $handler = rules_get_event_handler($form_state['values']['event']);
+  $handler->extractFormValues($form['event_settings'], $form_state);
+  try {
+    $handler->validate();
+  }
+  catch (RulesIntegrityException $e) {
+    form_set_error(implode('][', $e->keys), $e->getMessage());
+  }
 }
 
 /**
- * Submit the event configuration.
+ * Submit callback that just adds the selected event.
+ *
+ * @see rules_admin_add_reaction_rule()
  */
-function rules_ui_add_event_submit($form, &$form_state) {
-  rules_ui_add_event_apply($form, $form_state);
-  $rules_config = $form_state['rules_config'];
-
-  // Tell the user if this breaks something, but let him proceed.
-  if (empty($rules_config->dirty)) {
-    try {
-      $rules_config->integrityCheck();
-    }
-    catch (RulesIntegrityException $e) {
-      $warning = TRUE;
-      drupal_set_message(t('Added the event, but it does not provide all variables utilized.'), 'warning');
-    }
-  }
-  $rules_config->save();
-  if (!isset($warning)) {
-    $events = rules_fetch_data('event_info');
-    $label = $events[$form_state['values']['event']]['label'];
-    drupal_set_message(t('Added event %event.', array('%event' => $label)));
-  }
+function rules_ui_add_event_apply($form, &$form_state) {
+  $handler = rules_get_event_handler($form_state['values']['event']);
+  $handler->extractFormValues($form['event_settings'], $form_state);
+  $form_state['rules_config']->event($form_state['values']['event'], $handler->getSettings());
 }
 
 /**
- * Form to remove a event from a rule.
+ * Form to remove an event from a rule.
  */
 function rules_ui_remove_event($form, &$form_state, $rules_config, $event, $base_path) {
   RulesPluginUI::$basePath = $base_path;
   $form_state += array('rules_config' => $rules_config, 'rules_event' => $event);
-  $events = rules_fetch_data('event_info');
-  $form_state['event_label'] = $events[$event]['label'];
+  $event_info = rules_get_event_info($event);
+  $form_state['event_label'] = $event_info['label'];
   $confirm_question = t('Are you sure you want to remove the event?');
   return confirm_form($form, $confirm_question, RulesPluginUI::path($rules_config->name), t('You are about to remove the event %event.', array('%event' => $form_state['event_label'])), t('Remove'), t('Cancel'));
 }
@@ -564,6 +620,7 @@ function rules_ui_import_form_submit($form, &$form_state) {
 
 /**
  * FAPI process callback for the data selection widget.
+ *
  * This finalises the auto completion callback path by appending the form build
  * id.
  */
@@ -623,12 +680,12 @@ function rules_ui_form_data_selection_auto_completion($parameter, $form_build_id
   drupal_json_output($matches);
 }
 
-
 /**
- * FAPI validation of an integer element. Copy of the private function
- * _element_validate_integer().
+ * FAPI validation of an integer element.
+ *
+ * Copy of the core Drupal private function _element_validate_integer().
  */
-function rules_ui_element_integer_validate($element, &$form_state) {;
+function rules_ui_element_integer_validate($element, &$form_state) {
   $value = $element['#value'];
   if (isset($value) && $value !== '' && (!is_numeric($value) || intval($value) != $value)) {
     form_error($element, t('%name must be an integer value.', array('%name' => isset($element['#title']) ? $element['#title'] : t('Element'))));
@@ -636,8 +693,9 @@ function rules_ui_element_integer_validate($element, &$form_state) {;
 }
 
 /**
- * FAPI validation of a decimal element. Improved version of the private
- * function _element_validate_number().
+ * FAPI validation of a decimal element.
+ *
+ * Improved version of the private function _element_validate_number().
  */
 function rules_ui_element_decimal_validate($element, &$form_state) {
   // Substitute the decimal separator ",".
@@ -651,9 +709,21 @@ function rules_ui_element_decimal_validate($element, &$form_state) {
 }
 
 /**
- * FAPI validation of a date element. Makes sure the specified date format is
- * correct and converts date values specifiy a fixed (= non relative) date to
- * a timestamp. Relative dates are handled by the date input evaluator.
+ * FAPI callback to validate an IP address.
+ */
+function rules_ui_element_ip_address_validate($element, &$form_state) {
+  $value = $element['#value'];
+  if ($value != '' && !filter_var($value, FILTER_VALIDATE_IP)) {
+    form_error($element, t('%name is not a valid IP address.', array('%name' => $element['#title'])));
+  }
+}
+
+/**
+ * FAPI validation of a date element.
+ *
+ * Makes sure the specified date format is correct and converts date values
+ * specify a fixed (= non relative) date to a timestamp. Relative dates are
+ * handled by the date input evaluator.
  */
 function rules_ui_element_date_validate($element, &$form_state) {
   $value = $element['#value'];
@@ -722,8 +792,9 @@ function rules_ui_element_duration_multipliers() {
 }
 
 /**
- * Helper function to determine the value for a rules duration form
- * element.
+ * Helper function a rules duration form element.
+ *
+ * Determines the value for a rules duration form element.
  */
 function rules_ui_element_duration_value($element, $input = FALSE) {
   // This runs before child elements are processed, so we cannot calculate the
@@ -737,6 +808,7 @@ function rules_ui_element_duration_value($element, $input = FALSE) {
 
 /**
  * FAPI after build callback for the duration parameter type form.
+ *
  * Fixes up the form value by applying the multiplier.
  */
 function rules_ui_element_duration_after_build($element, &$form_state) {
@@ -766,7 +838,6 @@ function rules_ui_element_fix_empty_after_build($element, &$form_state) {
   return $element;
 }
 
-
 /**
  * FAPI after build callback for specifying a list of values.
  *
@@ -833,6 +904,7 @@ function rules_ui_element_machine_name_validate($element, &$form_state) {
 
 /**
  * FAPI callback to validate the form for editing variable info.
+ *
  * @see RulesPluginUI::getVariableForm()
  */
 function rules_ui_element_variable_form_validate($elements, &$form_state) {

+ 43 - 13
sites/all/modules/rules/ui/ui.plugins.inc

@@ -1,7 +1,8 @@
 <?php
 
 /**
- * @file Contains UI for diverse plugins provided by Rules.
+ * @file
+ * Contains UI for diverse plugins provided by Rules.
  */
 
 /**
@@ -9,15 +10,21 @@
  */
 class RulesRuleUI extends RulesActionContainerUI {
 
-  protected $rule, $conditions;
+  protected $rule;
+  protected $conditions;
 
+  /**
+   * Constructs a RulesRuleUI object.
+   *
+   * @param FacesExtendable $object
+   */
   public function __construct(FacesExtendable $object) {
     parent::__construct($object);
     $this->rule = $object;
     $this->conditions = $this->rule->conditionContainer();
   }
 
-  public function form(&$form, &$form_state, $options = array()) {
+  public function form(&$form, &$form_state, $options = array(), $iterator = NULL) {
     $form_state['rules_element'] = $this->rule;
     $label = $this->element->label();
     // Automatically add a counter to unlabelled rules.
@@ -58,7 +65,7 @@ class RulesRuleUI extends RulesActionContainerUI {
   /**
    * Applies the values of the form to the rule configuration.
    */
-  function form_extract_values($form, &$form_state) {
+  public function form_extract_values($form, &$form_state) {
     $form_values = RulesPluginUI::getFormStateValues($form, $form_state);
     // Run condition and action container value extraction.
     if (isset($form['conditions'])) {
@@ -70,13 +77,13 @@ class RulesRuleUI extends RulesActionContainerUI {
     parent::form_extract_values($form, $form_state);
   }
 
-
   public function operations() {
     // When rules are listed only show the edit and delete operations.
     $ops = parent::operations();
     $ops['#links'] = array_intersect_key($ops['#links'], array_flip(array('edit', 'delete')));
     return $ops;
   }
+
 }
 
 /**
@@ -84,26 +91,46 @@ class RulesRuleUI extends RulesActionContainerUI {
  */
 class RulesReactionRuleUI extends RulesRuleUI {
 
-  public function form(&$form, &$form_state, $options = array()) {
+  public function form(&$form, &$form_state, $options = array(), $iterator = NULL) {
     $form['events'] = array(
       '#type' => 'container',
       '#weight' => -10,
       '#access' => empty($options['init']),
     );
 
-    $event_info = rules_fetch_data('event_info');
     $form['events']['table'] = array(
       '#theme' => 'table',
       '#caption' => 'Events',
-      '#header' => array('Event', 'Operations'),
+      '#header' => array(t('Event'), t('Operations')),
       '#empty' => t('None'),
     );
     $form['events']['table']['#attributes']['class'][] = 'rules-elements-table';
     foreach ($this->rule->events() as $event_name) {
-      $event_info += array($event_name => array('label' => t('Unknown event "!event_name"', array('!event_name' => $event_name))));
+      $event_handler = rules_get_event_handler($event_name, $this->rule->getEventSettings($event_name));
+
+      $event_operations = array(
+        '#theme' => 'links__rules',
+        '#attributes' => array(
+          'class' => array(
+            'rules-operations',
+            'action-links',
+            'rules_rule_event',
+          ),
+        ),
+        '#links' => array(
+          'delete_event' => array(
+            'title' => t('delete'),
+            'href' => RulesPluginUI::path($this->rule->name, 'delete/event/' . $event_name),
+            'query' => drupal_get_destination(),
+          ),
+        ),
+      );
+
       $form['events']['table']['#rows'][$event_name] = array(
-        check_plain($event_info[$event_name]['label']),
-        '<span class="rules_rule_event">' . l(t('delete'), RulesPluginUI::path($this->rule->name, 'delete/event/' . $event_name)) . '</span>',
+        'data' => array(
+          $event_handler->summary(),
+          array('data' => $event_operations),
+        ),
       );
     }
 
@@ -150,6 +177,7 @@ class RulesReactionRuleUI extends RulesRuleUI {
     $this->rule->active = $form_values['active'];
     $this->rule->weight = $form_values['weight'];
   }
+
 }
 
 /**
@@ -166,6 +194,7 @@ class RulesRuleSetUI extends RulesActionContainerUI {
     $form['elements']['#attributes']['class'][] = 'rules-rule-set';
     $form['elements']['#caption'] = t('Rules');
   }
+
 }
 
 /**
@@ -173,7 +202,7 @@ class RulesRuleSetUI extends RulesActionContainerUI {
  */
 class RulesLoopUI extends RulesActionContainerUI {
 
-  public function form(&$form, &$form_state, $options = array()) {
+  public function form(&$form, &$form_state, $options = array(), $iterator = NULL) {
     parent::form($form, $form_state, $options);
     $settings = $this->element->settings;
 
@@ -199,7 +228,7 @@ class RulesLoopUI extends RulesActionContainerUI {
     );
   }
 
-  function form_extract_values($form, &$form_state) {
+  public function form_extract_values($form, &$form_state) {
     parent::form_extract_values($form, $form_state);
     $form_values = RulesPluginUI::getFormStateValues($form, $form_state);
 
@@ -231,4 +260,5 @@ class RulesLoopUI extends RulesActionContainerUI {
     );
     return $content;
   }
+
 }

+ 23 - 4
sites/all/modules/rules/ui/ui.theme.inc

@@ -1,10 +1,10 @@
 <?php
 
 /**
- * @file Rules theme functions
+ * @file
+ * Rules theme functions.
  */
 
-
 /**
  * Themes a tree of rule elements in a draggable table.
  *
@@ -58,13 +58,20 @@ function theme_rules_elements($variables) {
  * Themes the rules form for editing the used variables.
  *
  * @see RulesPluginUI::getVariableForm()
+ *
  * @ingroup themeable
  */
 function theme_rules_ui_variable_form($variables) {
   $elements = $variables['element'];
 
   $table['#theme'] = 'table';
-  $table['#header'] = array(t('Data type'), t('Label'), t('Machine name'), t('Usage'), array('data' => t('Weight'), 'class' => array('tabledrag-hide')));
+  $table['#header'] = array(
+    t('Data type'),
+    t('Label'),
+    t('Machine name'),
+    t('Usage'),
+    array('data' => t('Weight'), 'class' => array('tabledrag-hide')),
+  );
   $table['#attributes']['id'] = 'rules-' . drupal_html_id($elements['#title']) . '-id';
 
   foreach (element_children($elements['items']) as $key) {
@@ -107,6 +114,7 @@ function theme_rules_ui_variable_form($variables) {
 
 /**
  * Themes a view of multiple configuration items.
+ *
  * @ingroup themeable
  */
 function theme_rules_content_group($variables) {
@@ -125,6 +133,7 @@ function theme_rules_content_group($variables) {
 
 /**
  * Themes the view of a single parameter configuration.
+ *
  * @ingroup themeable
  */
 function theme_rules_parameter_configuration($variables) {
@@ -149,6 +158,7 @@ function theme_rules_parameter_configuration($variables) {
 
 /**
  * Themes info about variables.
+ *
  * @ingroup themeable
  */
 function theme_rules_variable_view($variables) {
@@ -169,6 +179,7 @@ function theme_rules_variable_view($variables) {
 
 /**
  * Themes help for using the data selector.
+ *
  * @ingroup themeable
  */
 function theme_rules_data_selector_help($variables) {
@@ -193,13 +204,18 @@ function theme_rules_data_selector_help($variables) {
   );
   foreach (RulesData::matchingDataSelector($variables_info, $param_info) as $selector => $info) {
     $info += array('label' => '', 'description' => '');
-    $render['table']['#rows'][] = array(check_plain($selector), check_plain(drupal_ucfirst($info['label'])), check_plain($info['description']));
+    $render['table']['#rows'][] = array(
+      check_plain($selector),
+      check_plain(drupal_ucfirst($info['label'])),
+      check_plain($info['description']),
+    );
   }
   return drupal_render($render);
 }
 
 /**
  * Themes the rules log debug output.
+ *
  * @ingroup themeable
  */
 function theme_rules_log($variables) {
@@ -230,6 +246,7 @@ function theme_rules_log($variables) {
 
 /**
  * Theme rules debug log elements.
+ *
  * @ingroup themeable
  */
 function theme_rules_debug_element($variables) {
@@ -248,6 +265,7 @@ function theme_rules_debug_element($variables) {
 
 /**
  * Themes rules autocomplete forms.
+ *
  * @ingroup themeable
  */
 function theme_rules_autocomplete($variables) {
@@ -278,6 +296,7 @@ function theme_rules_autocomplete($variables) {
 
 /**
  * General theme function for displaying settings related help.
+ *
  * @ingroup themeable
  */
 function theme_rules_settings_help($variables) {

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików