3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Utils_Hook_WordPress
extends CRM_Utils_Hook
{
40 private $isBuilt = FALSE;
45 private $allModules = NULL;
50 private $civiModules = NULL;
55 private $wordpressModules = NULL;
60 private $hooksThatReturn = array(
62 'civicrm_caseSummary',
69 * @param int $numParams Number of parameters to pass to the hook
70 * @param mixed $arg1 parameter to be passed to the hook
71 * @param mixed $arg2 parameter to be passed to the hook
72 * @param mixed $arg3 parameter to be passed to the hook
73 * @param mixed $arg4 parameter to be passed to the hook
74 * @param mixed $arg5 parameter to be passed to the hook
75 * @param mixed $arg6 parameter to be passed to the hook
76 * @param string $fnSuffix function suffix, this is effectively the hook name
80 public function invoke(
82 &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6,
87 * do_action_ref_array is the default way of calling WordPress hooks
88 * because for the most part no return value is wanted. However, this is
89 * only generally true, so using do_action_ref_array() is only called for those
90 * hooks which do not require a return value. We exclude the following, which
91 * are incompatible with the WordPress Plugin API:
94 * http://wiki.civicrm.org/confluence/display/CRMDOC43/hook_civicrm_upgrade
97 * http://wiki.civicrm.org/confluence/display/CRMDOC43/hook_civicrm_caseSummary
100 * http://wiki.civicrm.org/confluence/display/CRMDOC43/hook_civicrm_dashboard
103 // distinguish between types of hook
104 if ( ! in_array( $fnSuffix, $this->hooksThatReturn
) ) {
106 // only pass the arguments that have values
108 array( &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6 ),
114 * Use WordPress Plugins API to modify $args
116 * Because $args are passed as references to the WordPress callbacks,
117 * runHooks subsequently receives appropriately modified parameters.
120 // protect from REST calls
121 if (function_exists('do_action_ref_array')) {
122 do_action_ref_array( $fnSuffix, $args );
130 * The following is based on the logic of the Joomla hook file by allowing
131 * WordPress callbacks to do their stuff before runHooks gets called.
133 * It also follows the logic of the Drupal hook file by building the "module"
134 * (read "plugin") list and then calling runHooks directly. This should avoid
135 * the need for the post-processing that the Joomla hook file does.
137 * Note that hooks which require a return value are incompatible with the
138 * signature of apply_filters_ref_array and must therefore be called in
139 * global scope, like in Drupal. It's not ideal, but plugins can always route
140 * these calls to methods in their classes.
142 * At some point, those hooks could be pre-processed and called via the WordPress
143 * Plugin API, but it would change their signature and require the CiviCRM docs
144 * to be rewritten for those calls in WordPress. So it's been done this way for
145 * now. Ideally these hooks will be deprecated in favour of hooks that do not
146 * require return values.
149 // build list of registered plugin codes
150 $this->buildModuleList();
152 // Call runHooks the same way Drupal does
153 $moduleResult = $this->runHooks(
157 $arg1, $arg2, $arg3, $arg4, $arg5, $arg6
161 return empty($moduleResult) ?
TRUE : $moduleResult;
168 * Build the list of plugins ("modules" in CiviCRM terminology) to be processed for hooks.
169 * We need to do this to preserve the CiviCRM hook signatures for hooks that require
170 * a return value, since the WordPress Plugin API seems to be incompatible with them.
172 * Copied and adapted from: CRM/Utils/Hook/Drupal6.php
174 function buildModuleList() {
175 if ($this->isBuilt
=== FALSE) {
177 if ($this->wordpressModules
=== NULL) {
179 // include custom PHP file - copied from parent->commonBuildModuleList()
180 $config = CRM_Core_Config
::singleton();
181 if (!empty($config->customPHPPathDir
) &&
182 file_exists("{$config->customPHPPathDir}/civicrmHooks.php")
184 @include_once
('civicrmHooks.php');
187 // initialise with the pre-existing 'wordpress' prefix
188 $this->wordpressModules
= array('wordpress');
191 * Use WordPress Plugin API to build list
192 * a plugin simply needs to declare its "unique_plugin_code" thus:
193 * add_filter('civicrm_wp_plugin_codes', 'function_that_returns_my_unique_plugin_code');
196 // protect from REST calls
197 if (function_exists('apply_filters')) {
198 $this->wordpressModules
= apply_filters('civicrm_wp_plugin_codes', $this->wordpressModules
);
203 if ($this->civiModules
=== NULL) {
204 $this->civiModules
= array();
205 $this->requireCiviModules($this->civiModules
);
208 $this->allModules
= array_merge((array)$this->wordpressModules
, (array)$this->civiModules
);
209 if ($this->wordpressModules
!== NULL && $this->civiModules
!== NULL) {
210 // both CRM and CMS have bootstrapped, so this is the final list
211 $this->isBuilt
= TRUE;