Exemplo n.º 1
0
 public function onMasterRequest(MWP_Event_MasterRequest $event)
 {
     if (!$event->getRequest()->isAuthenticated()) {
         return;
     }
     if (!$event->isMuContext()) {
         // Set the user on the earliest hook after pluggable.php is loaded.
         $hookProxy = new MWP_WordPress_HookProxy(array($this, 'setCurrentUserFromEvent'), $event);
         $this->context->addAction('plugins_loaded', $hookProxy->getCallable(), -9999);
         return;
     }
     // We're inside the MU context, so set the user immediately.
     $this->setCurrentUserFromEvent($event);
 }
Exemplo n.º 2
0
 /**
  * @param MWP_Worker_Request $request
  * @param callable           $deferredCallback
  * @param bool               $catch
  *
  * @throws Exception
  * @throws MWP_Worker_Exception
  */
 public function handleRequest(MWP_Worker_Request $request, $deferredCallback, $catch = true)
 {
     $request->initialize();
     $this->requestStack->push($request);
     $this->responseCallback->set($deferredCallback);
     $container = $this->getContainer();
     $actionName = $request->getAction();
     $params = $request->getParams();
     $context = $container->getWordPressContext();
     if (!$request->isMasterRequest()) {
         // This is a public request. Allow the plugin to hook onto WordPress.
         $publicRequestEvent = new MWP_Event_PublicRequest($request);
         $this->dispatcher->dispatch(MWP_Event_Events::PUBLIC_REQUEST, $publicRequestEvent);
         if ($publicRequestEvent->hasResponse()) {
             call_user_func($deferredCallback, null, $publicRequestEvent->getResponse());
         }
         return;
     }
     try {
         // This is a master request. Allow early hooks to verify and do everything required with the request.
         $masterRequestEvent = new MWP_Event_MasterRequest($request, $params);
         $this->dispatcher->dispatch(MWP_Event_Events::MASTER_REQUEST, $masterRequestEvent);
         if ($masterRequestEvent->hasResponse()) {
             call_user_func($deferredCallback, null, $masterRequestEvent->getResponse());
             return;
         }
         $params = $masterRequestEvent->getParams();
         // Get action info.
         $actionRegistry = $container->getActionRegistry();
         $actionDefinition = $actionRegistry->getDefinition($actionName);
         $callback = $actionDefinition->getCallback();
         // If the callback is an array with two members (['ClassName, 'methodName']) and implements ContainerAware,
         // inject the container before executing it.
         if (is_array($callback) && is_string($callback[0])) {
             $callback[0] = new $callback[0]();
         }
         if (is_array($callback) && $callback[0] instanceof MWP_ServiceContainer_ContainerAwareInterface) {
             $callbackObject = $callback[0];
             /** @var MWP_ServiceContainer_ContainerAwareInterface $callbackObject */
             $callbackObject->setContainer($container);
         }
         // Check if the action call should be deferred.
         $hookName = $actionDefinition->getOption('hook_name');
         if ($hookName !== null && $deferredCallback !== null) {
             $proxy = new MWP_WordPress_HookProxy(array($this, 'hookResponse'), $request, $callback, $params, $actionDefinition, $deferredCallback);
             $context->addAction($hookName, $proxy->getCallable(), $actionDefinition->getOption('hook_priority'));
             return;
         }
         // Allow listeners to modify action parameters.
         $actionRequestEvent = new MWP_Event_ActionRequest($request, $params, $actionDefinition);
         $this->dispatcher->dispatch(MWP_Event_Events::ACTION_REQUEST, $actionRequestEvent);
         $params = $actionRequestEvent->getParams();
         try {
             $data = call_user_func($callback, $params, $request);
         } catch (MWP_Worker_ActionResponse $actionResponse) {
             $data = $actionResponse->getData();
         }
         $response = $this->handleResponse($request, $params, $data);
         call_user_func($deferredCallback, null, $response);
     } catch (Exception $e) {
         if (!$catch) {
             throw $e;
         }
         $response = $this->handleException($request, $e);
         call_user_func($deferredCallback, $e, $response);
     }
 }