/** * Return form for the widget (filled with data coming from POST). * * @param string $name * @return JsonResponse * @throws \RuntimeException */ public function widget($name) { $data = $this->request->post['item']; if ($data) { $data = json_decode($data, true); } else { $data = $this->request->post->getArray(); $cast = true; } if ($data && (!isset($data['type']) || $data['type'] !== 'widget' || !isset($data['widget']))) { throw new \RuntimeException('Bad request data.', 400); } $instance = isset($data['options']['widget']) ? $data['options']['widget'] : []; if (isset($this->params['scope'])) { $scope = $this->params['scope']; $file = CompiledYamlFile::instance("gantry-admin://blueprints/{$scope}/block.yaml"); $block = new BlueprintsForm($file->content()); $file->free(); // Load particle blueprints. $validator = $this->loadBlueprints($scope); $callable = function () use($validator) { return $validator; }; } else { $block = null; $callable = null; } if (!empty($cast)) { $instance = $this->castInput($instance); } $widgetType = $this->getWidgetType($name); $widgetType->number = 0; ob_start(); // TODO: We might want to add the filters back; for now we just assume that widget works like the_widget(). //$instance = apply_filters( 'widget_form_callback', $instance, $data ); if (false !== $instance) { $return = $widgetType->form($instance); //do_action_ref_array( 'in_widget_form', array( &$widgetType, &$return, $instance ) ); } $form = ob_get_clean(); // Create configuration from the defaults. $item = new Config($data, $callable); $item->def('type', 'particle'); $item->def('title', $widgetType->name); $item->def('options.type', $widgetType->id_base); $item->def('options.particle', []); $item->def('options.block', []); $this->params += ['item' => $item, 'block' => $block, 'data' => $data, 'form' => $form, 'prefix' => "widget.{$name}.", 'route' => "configurations.default.settings", 'action' => "widget/{$name}/validate"]; return new JsonResponse(['html' => $this->container['admin.theme']->render('@gantry-admin/modals/widget.html.twig', $this->params)]); }
public function validateParticle($name) { // Validate only exists for JSON. if (empty($this->params['ajax'])) { $this->undefined(); } // Load particle blueprints and default settings. $validator = new Blueprints(); $validator->embed('options', $this->container['particles']->get($name)); $blueprints = new BlueprintsForm($this->container['particles']->get($name)); // Create configuration from the defaults. $data = new Config([], function () use($validator) { return $validator; }); $data->set('type', 'particle'); $data->set('particle', $name); $data->set('title', $this->request->post['title'] ?: $blueprints->post['name']); $data->set('options.particle', $this->request->post->getArray("particles.{$name}")); $data->def('options.particle.enabled', 1); $block = $this->request->post->getArray('block'); foreach ($block as $key => $param) { if ($param === '') { unset($block[$key]); } } $data->join('options.block', $block); // TODO: validate // Fill parameters to be passed to the template file. $this->params['item'] = (object) $data->toArray(); $html = $this->container['admin.theme']->render('@gantry-admin/menu/item.html.twig', $this->params); return new JsonResponse(['item' => $data->toArray(), 'html' => $html]); }
/** * Validate data for the atom. * * @param string $name * @return JsonResponse */ public function atomValidate($name) { // Load particle blueprints and default settings. $validator = new Blueprints(); $validator->embed('options', $this->container['particles']->get($name)); $blueprints = new BlueprintsForm($this->container['particles']->get($name)); // Create configuration from the defaults. $data = new Config([], function () use($validator) { return $validator; }); $data->set('type', $name); $data->set('title', $this->request->post['title'] ?: $blueprints->get('name')); $data->set('attributes', $this->request->post->getArray("particles.{$name}")); $data->def('attributes.enabled', 1); $block = $this->request->post->getArray('block'); foreach ($block as $key => $param) { if ($param === '') { unset($block[$key]); } } if ($block) { $data->join('options.block', $block); } // TODO: validate // Fill parameters to be passed to the template file. $this->params['item'] = (object) $data->toArray(); return new JsonResponse(['item' => $data->toArray()]); }
/** * Validate data for the widget. * * @param string $name * @return JsonResponse */ public function validate($name) { $widgetType = $this->getWidgetType($name); $old_instance = []; $new_instance = $this->request->post->getArray("widget-{$widgetType->id_base}.0"); // Prevent caching. $cache_state = \wp_suspend_cache_addition(); \wp_suspend_cache_addition(true); // Update widget by using its own method. $instance = $widgetType->update($new_instance, $old_instance); // Restore caching. \wp_suspend_cache_addition($cache_state); // Apply widget filters. // TODO: We might want to add the filters back; for now we just assume that widget works like the_widget(). //$instance = \apply_filters('widget_update_callback', $instance, $new_instance, $old_instance, $widgetType); if ($instance === false) { throw new \RuntimeException('Filter prevented widget from being saved.', 403); } // Create configuration from the defaults. $data = new Config(['type' => 'widget', 'widget' => $name, 'title' => $this->request->post['title'] ?: $widgetType->name]); $data->set('options.widget', $instance); $data->def('options.enabled', 1); return new JsonResponse(['item' => $data->toArray()]); }