/** * @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])]; }
/** * Test if attaching data filter listeners * will initiate registering them inside the * wordpress filters by add_filter procedure */ public function testAttachDataFilterListeners() { $actionListener = $this->getMockBuilder(DataFilterInterface::class)->getMock(); $listenersFirst = $this->getListeners(['t1', 't2']); $listenersSecond = $this->getListeners(['t1', 't2']); $actionListener->expects($this->once())->method('getDataFilterListeners')->willReturn([$listenersFirst, $listenersSecond]); $this->wpStub->expects($this->exactly(4))->method('__call')->withConsecutive([$this->equalTo('add_filter'), $this->equalTo(['t1', $listenersFirst, 96, 2])], [$this->equalTo('add_filter'), $this->equalTo(['t2', $listenersFirst, 96, 2])], [$this->equalTo('add_filter'), $this->equalTo(['t1', $listenersSecond, 96, 2])], [$this->equalTo('add_filter'), $this->equalTo(['t2', $listenersSecond, 96, 2])]); $eventManager = new EventManager($this->wpStub); $eventManager->attachListeners($actionListener); }
/** * @param string $name * @param number $postId * * @return mixed */ public function getPostFields($idOrName, $postId = null) { if (!class_exists('acf')) { throw new RuntimeException('ACF plugin not active.'); } if (is_numeric($idOrName) && $postId === null) { return $this->wpService->get_fields($idOrName); } return $this->wpService->get_field($idOrName, $postId); }
/** * @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 ListenerInterface[] | array $listeners * @param string $type one of filter or action types */ protected function register(array $listeners, $type) { foreach ($listeners as $listener) { $listener = $this->listenerFactory->makeListener($type, $listener); foreach ($listener->getNames() as $name) { /** * @see WP::add_action * @see WP::add_filter */ $this->wpService->__call('add_' . strtolower($type), [$name, $listener->getCallable(), $listener->getPriority(), $listener->getArgc()]); } } }
protected function setRouteForPostTypes($postType, $defaultController) { $controllerPageMapping = $this->app->getContainer()->get(ControllerPageMappingField::class); /** * @FIXME: In future versions, need to change * adding routes to the map of get|post. All WP PAGES and POSTS must * coresponds to only GET method. Because it is has content only for reading. * All other logic like writing or another logic should be implemented in WIDGETS * or in controllers via declarring new routes and handlers for them. */ foreach ($this->wpService->get_posts(['numberposts' => -1, 'post_type' => $postType]) as $post) { $controller = $controllerPageMapping->getValue($post->ID); $this->app->map(['get', 'post'], parse_url(get_permalink($post), PHP_URL_PATH), $this->app->getContainer()->get(empty($controller) ? $defaultController : $controller))->setArgument('requestedEntity', $post); } }
/** * @param string $template Relative path to the template. * * @return string * @throws RuntimeException Throws if template has incorrect format or template could not be found. */ protected function getTemplatePath($template) { $parts = explode('/', $template); if (count($parts) < 1) { throw new RuntimeException("Template path [{$template}] has incorrect format, it is must start from theme-name or alias path"); } $extracted = array_splice($parts, 0, 1); $alias = $extracted[0]; if (!array_key_exists($alias, $this->templateConfig)) { $alias = $this->wp->wp_get_theme()->template; array_unshift($parts, $extracted[0]); } $fullTemplatePath = rtrim($this->templateConfig[$alias], '/') . '/' . join('/', $parts); if (!file_exists($fullTemplatePath)) { throw new RuntimeException("Could not found template [{$fullTemplatePath}] please check requested template name"); } return $fullTemplatePath; }
/** * 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(); }