/** * @param string $type * @param array | ListenerInterface $listener * * @return mixed */ public function makeListener($type, $listener) { if (!in_array($type, [WP::EVENT_TYPE_FILTER, WP::EVENT_TYPE_ACTION])) { throw new RuntimeException('Unsupported type of the event [' . $type . '], now supports only filter or action types'); } $listenerHandler = ''; if (is_array($listener) && $type == WP::EVENT_TYPE_ACTION) { if (array_key_exists('ajax', $listener) && $listener['ajax'] === true) { if (array_key_exists('callable', $listener) && !$listener['callable'] instanceof HandlerInterface) { $listener['callable'] = $this->factory->make(GenericAjaxHandler::class, ['callable' => $listener['callable']]); } $listener = $this->factory->make('AjaxActionListener', $listener); } else { $listener = $this->factory->make('ActionListener', $listener); } } else { if (is_array($listener) && $type == WP::EVENT_TYPE_FILTER) { $listener = $this->factory->make('DataFilterListener', $listener); } } if (!$listener instanceof ListenerInterface) { throw new RuntimeException('Listener must be type of ListenerInterface'); } return $listener; }
/** * @return ListenerInterface[] */ public function getActionListeners() { $menusInit = function () { foreach ($this->attached as $attachedItem) { $this->wpService->register_nav_menu($attachedItem->getId(), $attachedItem->getDescription()); } }; return [$this->factory->make('ActionListener', ['names' => 'after_setup_theme', 'callable' => $menusInit])]; }
/** * @return ListenerInterface[] */ public function getActionListeners() { $widgetsInit = function () { foreach ($this->attached as $k => $attachedItem) { $this->widgetFactory->register($attachedItem, $attachedItem->getId()); } }; return [$this->factory->make('ActionListener', ['names' => 'widgets_init', 'callable' => $widgetsInit])]; }
/** * @return ListenerInterface[] */ public function getActionListeners() { $sidebarsInit = function () { foreach ($this->attached as $attachedItem) { $args = array('id' => $attachedItem->getId(), 'name' => $attachedItem->getName(), 'description' => $attachedItem->getDescription(), 'before_title' => $attachedItem->getBeforeTitle(), 'after_title' => $attachedItem->getAfterTitle(), 'before_widget' => $attachedItem->getBeforeWidget(), 'after_widget' => $attachedItem->getAfterWidget()); $this->wpService->register_sidebar($attachedItem->getCustomFields() + $args); } }; return [$this->factory->make('ActionListener', ['names' => 'widgets_init', 'callable' => $sidebarsInit])]; }
/** * @param string $name * * @return HelperInterface * @throws \Exception */ protected function getHelper($name) { if (array_key_exists($name, $this->helpers)) { if (!is_object($this->helpers[$name])) { $this->helpers[$name] = $this->factory->make($this->helpers[$name][0], $this->helpers[$name][1]); } return $this->helpers[$name]; } else { throw new RuntimeException("Could not found view helper for name " . $name); } }
/** * @return ListenerInterface[] */ public function getActionListeners() { $routeDispatcher = function () { $postTypeSettings = $this->app->getContainer()->get('postTypes'); foreach ($postTypeSettings as $postType => $settings) { $this->setRouteForPostTypes($postType, $settings['defaultController']); } $this->app->run(); }; return [$this->factory->make('ActionListener', ['names' => 'template_include', 'callable' => $routeDispatcher, 'priority' => 99])]; }
/** * https://qiita.com/api/v2/docs#patch-apiv2itemsitem_id * @param ItemInterface $item */ public function patchItemsItemId(ItemInterface $item) { $itemId = $item->getId(); $response = $this->httpClient->patch($this->apiEntryPoint() . 'items/' . $itemId, ['json' => ['body' => $item->getBody(), 'coediting' => $item->isCoediting(), 'private' => $item->isPrivate(), 'tags' => $item->getTags(), 'title' => $item->getTitle()]]); $parameters = self::objectToArray($response); return $this->factory->make('item', ['parameters' => $parameters]); }
/** * This method will be called when WP will execute * ajax action handler, and will be stop whole PHP * process execution at the end of execution of this * method, because WP work in such a way. */ public function __invoke() { $response = $this->factory->make(Response::class); $request = Request::createFromEnvironment($this->factory->make(Environment::class, ['items' => $_SERVER + $_REQUEST])); $callable = $this->callable; $result = $callable($request, $response); if (!$result instanceof MessageInterface) { echo $result; } else { foreach ($result->getHeaders() as $name => $headers) { foreach ($headers as $header) { @header($name . ': ' . $header); } } echo (string) $result->getBody(); } $this->wpService->wp_die(); }
/** * Render a template * * $data cannot contain template as a key * * throws RuntimeException if $templatePath . $template does not exist * * @param string $template Relative path to the template to render in current context. * @param array $data Variables and different data which must be included into rendering context. * * @return string * @throws RuntimeException Throws if template has incorrect format or template could not be found. */ public function render($template, array $data = []) { $closure = $this->factory->make(Closure::class, ['closure' => function ($template, $data) { extract($data); include $template; }]); $render = $closure->bindTo($this->helperManager, get_class($this->helperManager)); ob_start(); $render($this->getTemplatePath($template), $data); return ob_get_clean(); }
/** * @param string $collectionPath * @param string $collectionItemName * @param array $data * * @return array|null */ public function create($collectionPath, $collectionItemName, array $data) { $uri = $this->factory->make(Uri::class, ['uri' => $this->configuration->getServiceUri() . $collectionPath]); $result = $this->transport->post($uri, $data); if (empty($result)) { return null; } if (empty($result->{$collectionItemName})) { throw new UnexpectedResponseException('Empty itemName ' . $collectionItemName . ' from ' . $collectionPath . ' after POST/create'); } return (array) $result->{$collectionItemName}; }
/** * @return ListenerInterface[] */ public function getActionListeners() { return [$this->factory->make('ActionListener', ['names' => 'init', 'callable' => function () { return $this->init(); }, 'priority' => 10])]; }
private function createCoreHomeController() { return $this->abstractFactory->make('Piwik\\Plugins\\CoreHome\\Controller'); }
protected function make($name, array $parameters = []) { return static::$factory->make($name, $parameters); }
/** * @return ListenerInterface[] */ public function getDataFilterListeners() { return [$this->factory->make('DataFilterListener', ['names' => ['manage_page_posts_columns', 'manage_post_posts_columns'], 'callable' => function ($defaults) { return $this->showColumnsHead($defaults); }, 'priority' => 10])]; }
/** * @return ListenerInterface[] */ public function getActionListeners() { return [$this->factory->make('ActionListener', ['names' => 'setup_theme', 'callable' => function () { $GLOBALS['wp_widget_factory'] = $this; }])]; }