3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Utils_Hook_WordPress
extends CRM_Utils_Hook
{
22 private $isBuilt = FALSE;
27 private $allModules = NULL;
32 private $civiModules = NULL;
37 private $wordpressModules = NULL;
42 private $hooksThatReturn = [
44 'civicrm_caseSummary',
51 * @param int $numParams
52 * Number of parameters to pass to the hook.
54 * Parameter to be passed to the hook.
56 * Parameter to be passed to the hook.
58 * Parameter to be passed to the hook.
60 * Parameter to be passed to the hook.
62 * Parameter to be passed to the hook.
64 * Parameter to be passed to the hook.
65 * @param string $fnSuffix
66 * Function suffix, this is effectively the hook name.
70 public function invokeViaUF(
72 &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6,
76 // do_action_ref_array is the default way of calling WordPress hooks
77 // because for the most part no return value is wanted. However, this is
78 // only generally true, so using do_action_ref_array() is only called for those
79 // hooks which do not require a return value. We exclude the following, which
80 // are incompatible with the WordPress Plugin API:
83 // http://wiki.civicrm.org/confluence/display/CRMDOC43/hook_civicrm_upgrade
85 // civicrm_caseSummary
86 // http://wiki.civicrm.org/confluence/display/CRMDOC43/hook_civicrm_caseSummary
89 // http://wiki.civicrm.org/confluence/display/CRMDOC43/hook_civicrm_dashboard
91 // distinguish between types of hook
92 if (!in_array($fnSuffix, $this->hooksThatReturn
)) {
94 // only pass the arguments that have values
96 array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6),
101 // Use WordPress Plugins API to modify $args
103 // Because $args are passed as references to the WordPress callbacks,
104 // runHooks subsequently receives appropriately modified parameters.
106 // protect from REST calls
107 if (function_exists('do_action_ref_array')) {
108 do_action_ref_array($fnSuffix, $args);
113 // The following is based on the logic of the Joomla hook file by allowing
114 // WordPress callbacks to do their stuff before runHooks gets called.
116 // It also follows the logic of the Drupal hook file by building the "module"
117 // (read "plugin") list and then calling runHooks directly. This should avoid
118 // the need for the post-processing that the Joomla hook file does.
120 // Note that hooks which require a return value are incompatible with the
121 // signature of apply_filters_ref_array and must therefore be called in
122 // global scope, like in Drupal. It's not ideal, but plugins can always route
123 // these calls to methods in their classes.
125 // At some point, those hooks could be pre-processed and called via the WordPress
126 // Plugin API, but it would change their signature and require the CiviCRM docs
127 // to be rewritten for those calls in WordPress. So it's been done this way for
128 // now. Ideally these hooks will be deprecated in favour of hooks that do not
129 // require return values.
131 // build list of registered plugin codes
132 $this->buildModuleList();
134 // Call runHooks the same way Drupal does
135 $moduleResult = $this->runHooks(
139 $arg1, $arg2, $arg3, $arg4, $arg5, $arg6
143 return empty($moduleResult) ?
TRUE : $moduleResult;
148 * Build the list of plugins ("modules" in CiviCRM terminology) to be processed for hooks.
150 * We need to do this to preserve the CiviCRM hook signatures for hooks that require
151 * a return value, since the WordPress Plugin API seems to be incompatible with them.
153 * Copied and adapted from: CRM/Utils/Hook/Drupal6.php
155 public function buildModuleList() {
156 if ($this->isBuilt
=== FALSE) {
158 if ($this->wordpressModules
=== NULL) {
160 // include custom PHP file - copied from parent->commonBuildModuleList()
161 $config = CRM_Core_Config
::singleton();
162 if (!empty($config->customPHPPathDir
) &&
163 file_exists("{$config->customPHPPathDir}/civicrmHooks.php")
165 @include_once
"{$config->customPHPPathDir}/civicrmHooks.php";
168 // initialise with the pre-existing 'wordpress' prefix
169 $this->wordpressModules
= ['wordpress'];
171 // Use WordPress Plugin API to build list
172 // a plugin simply needs to declare its "unique_plugin_code" thus:
173 // add_filter('civicrm_wp_plugin_codes', 'function_that_returns_my_unique_plugin_code');
175 // protect from REST calls
176 if (function_exists('apply_filters')) {
177 $this->wordpressModules
= apply_filters('civicrm_wp_plugin_codes', $this->wordpressModules
);
182 if ($this->civiModules
=== NULL) {
183 $this->civiModules
= [];
184 $this->requireCiviModules($this->civiModules
);
187 $this->allModules
= array_merge((array) $this->wordpressModules
, (array) $this->civiModules
);
188 if ($this->wordpressModules
!== NULL && $this->civiModules
!== NULL) {
189 // both CRM and CMS have bootstrapped, so this is the final list
190 $this->isBuilt
= TRUE;