private function mergeConfig(Page $page) { $defaults = (array) $this->grav['config']->get('plugins.jscomments'); if (isset($page->header()->jscomments)) { if (is_array($page->header()->jscomments)) { $this->grav['config']->set('plugins.jscomments', array_replace_recursive($defaults, $page->header()->jscomments)); } } }
private function mergeConfig(Page $page, $params = []) { $this->config = new Data((array) $this->grav['config']->get('plugins.simple_form')); if (isset($page->header()->simple_form)) { if (is_array($page->header()->simple_form)) { $this->config = new Data(array_replace_recursive($this->config->toArray(), $page->header()->simple_form)); } else { $this->config->set('enabled', $page->header()->simple_form); } } $this->config = new Data(array_replace_recursive($this->config->toArray(), $params)); }
/** * Create form for the given page. * * @param Page $page * @param null $name * @param null $form */ public function __construct(Page $page, $name = null, $form = null) { parent::__construct(); $this->page = $page->route(); $header = $page->header(); $this->rules = isset($header->rules) ? $header->rules : []; $this->header_data = isset($header->data) ? $header->data : []; if ($form) { $this->items = $form; } else { if (isset($header->form)) { $this->items = $header->form; // for backwards compatibility } } // Add form specific rules. if (!empty($this->items['rules']) && is_array($this->items['rules'])) { $this->rules += $this->items['rules']; } // Set form name if not set. if ($name && !is_int($name)) { $this->items['name'] = $name; } elseif (empty($this->items['name'])) { $this->items['name'] = $page->slug(); } // Set form id if not set. if (empty($this->items['id'])) { $inflector = new Inflector(); $this->items['id'] = $inflector->hyphenize($this->items['name']); } // Reset and initialize the form $this->reset(); }
/** * Create form for the given page. * * @param Page $page */ public function __construct(Page $page) { $this->page = $page; $header = $page->header(); $this->rules = isset($header->rules) ? $header->rules : array(); $this->data = isset($header->data) ? $header->data : array(); $this->items = $header->form; // Set form name if not set. if (empty($this->items['name'])) { $this->items['name'] = $page->slug(); } }
/** * Add extra items to the shortcodes stream. * * @param string $group The group name to add the extra items to. * @param any $extra The item to store. */ public function addExtra($group, $method, $arguments = null) { $header = $this->page->header(); $arguments = is_array($arguments) ? $arguments : [$arguments]; // Modify page header $shortcodes = isset($header->shortcodes) ? $header->shortcodes : []; $shortcodes['extra'][$group][] = [$method, $arguments]; // Temporally store Shortcode extras in page header $this->page->modifyHeader('shortcodes', $shortcodes); if ($this->page->id() == self::getGrav()['page']->id()) { $object = $group != 'page' ? self::getGrav()[$group] : $page; call_user_func_array([$object, $method], $arguments); } }
/** * Create form for the given page. * * @param Page $page */ public function __construct(Page $page) { $this->page = $page; $header = $page->header(); $this->rules = isset($header->rules) ? $header->rules : array(); $this->data = isset($header->data) ? $header->data : array(); $this->items = $header->form; // Set form name if not set. if (empty($this->items['name'])) { $this->items['name'] = $page->slug(); } $this->reset(); // Fire event self::getGrav()->fireEvent('onFormInitialized', new Event(['form' => $this])); }
/** * Twig process that renders a page item. It supports two variations: * 1) Handles modular pages by rendering a specific page based on its modular twig template * 2) Renders individual page items for twig processing before the site rendering * * @param Page $item The page item to render * @param string $content Optional content override * @return string The rendered output * @throws \Twig_Error_Loader */ public function processPage(Page $item, $content = null) { $content = $content !== null ? $content : $item->content(); // override the twig header vars for local resolution $this->grav->fireEvent('onTwigPageVariables'); $twig_vars = $this->twig_vars; $twig_vars['page'] = $item; $twig_vars['media'] = $item->media(); $twig_vars['header'] = $item->header(); $local_twig = clone $this->twig; $modular_twig = $item->modularTwig(); $process_twig = isset($item->header()->process['twig']) ? $item->header()->process['twig'] : false; try { // Process Modular Twig if ($modular_twig) { $twig_vars['content'] = $content; $template = $item->template() . TEMPLATE_EXT; $output = $content = $local_twig->render($template, $twig_vars); } // Process in-page Twig if (!$modular_twig || $modular_twig && $process_twig) { $name = '@Page:' . $item->path(); $this->setTemplate($name, $content); $output = $local_twig->render($name, $twig_vars); } } catch (\Twig_Error_Loader $e) { throw new \RuntimeException($e->getRawMessage(), 404, $e); } return $output; }
/** * Prepare a page to be stored: update its folder, name, template, header and content * * @param \Grav\Common\Page\Page $page * @param bool $clean_header */ protected function preparePage(\Grav\Common\Page\Page $page, $clean_header = false, $language = null) { $input = $this->post; if (isset($input['order'])) { $order = max(0, (int) isset($input['order']) ? $input['order'] : $page->value('order')); $ordering = $order ? sprintf('%02d.', $order) : ''; $slug = empty($input['folder']) ? $page->value('folder') : (string) $input['folder']; $page->folder($ordering . $slug); } if (isset($input['name']) && !empty($input['name'])) { $type = (string) strtolower($input['name']); $name = preg_replace('|.*/|', '', $type); if ($language) { $name .= '.' . $language; } else { $language = $this->grav['language']; if ($language->enabled()) { $name .= '.' . $language->getLanguage(); } } $name .= '.md'; $page->name($name); $page->template($type); // unset some header things, template for now as we've just set that if (isset($input['header']['template'])) { unset($input['header']['template']); } } // Special case for Expert mode: build the raw, unset content if (isset($input['frontmatter']) && isset($input['content'])) { $page->raw("---\n" . (string) $input['frontmatter'] . "\n---\n" . (string) $input['content']); unset($input['content']); } if (isset($input['header'])) { $header = $input['header']; foreach ($header as $key => $value) { if ($key == 'metadata') { foreach ($header['metadata'] as $key2 => $value2) { if (isset($input['toggleable_header']['metadata'][$key2]) && !$input['toggleable_header']['metadata'][$key2]) { $header['metadata'][$key2] = ''; } } } elseif ($key == 'taxonomy') { foreach ($header[$key] as $taxkey => $taxonomy) { if (is_array($taxonomy) && count($taxonomy) == 1 && trim($taxonomy[0]) == '') { unset($header[$key][$taxkey]); } } } else { if (isset($input['toggleable_header'][$key]) && !$input['toggleable_header'][$key]) { $header[$key] = null; } } } if ($clean_header) { $header = Utils::arrayFilterRecursive($header, function ($k, $v) { return !(is_null($v) || $v === ''); }); } $page->header((object) $header); $page->frontmatter(Yaml::dump((array) $page->header())); } // Fill content last because it also renders the output. if (isset($input['content'])) { $page->rawMarkdown((string) $input['content']); } }
/** * Save the current page in a different language. Automatically switches to that language. * * @return bool True if the action was performed. */ protected function taskSaveas() { if (!$this->authorizeTask('save', $this->dataPermissions())) { return false; } $data = (array) $this->data; $language = $data['lang']; if ($language) { $this->grav['session']->admin_lang = $language ?: 'en'; } $uri = $this->grav['uri']; $obj = $this->admin->page($uri->route()); $this->preparePage($obj, false, $language); $file = $obj->file(); if ($file) { $filename = $this->determineFilenameIncludingLanguage($obj->name(), $language); $path = $obj->path() . DS . $filename; $aFile = File::instance($path); $aFile->save(); $aPage = new Page(); $aPage->init(new \SplFileInfo($path), $language . '.md'); $aPage->header($obj->header()); $aPage->rawMarkdown($obj->rawMarkdown()); $aPage->validate(); $aPage->filter(); $aPage->save(); $this->grav->fireEvent('onAdminAfterSave', new Event(['page' => $obj])); } $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.SUCCESSFULLY_SWITCHED_LANGUAGE'), 'info'); $this->setRedirect('/' . $language . $uri->route()); return true; }
/** * @param \Grav\Common\Page\Page|\Grav\Common\Data\Data $obj * * @return \Grav\Common\Page\Page|\Grav\Common\Data\Data */ protected function storeFiles($obj) { // Process previously uploaded files for the current URI // and finally store them. Everything else will get discarded $queue = $this->admin->session()->getFlashObject('files-upload'); $queue = $queue[base64_encode($this->grav['uri']->url())]; if (is_array($queue)) { foreach ($queue as $key => $files) { foreach ($files as $destination => $file) { if (!rename($file['tmp_name'], $destination)) { throw new \RuntimeException(sprintf($this->admin->translate('PLUGIN_ADMIN.FILEUPLOAD_UNABLE_TO_MOVE', null), '"' . $file['tmp_name'] . '"', $destination)); } unset($files[$destination]['tmp_name']); } if ($this->view == 'pages') { $keys = explode('.', preg_replace('/^header./', '', $key)); $init_key = array_shift($keys); if (count($keys) > 0) { $new_data = isset($obj->header()->{$init_key}) ? $obj->header()->{$init_key} : []; Utils::setDotNotation($new_data, implode('.', $keys), $files, true); } else { $new_data = $files; } if (isset($data['header'][$init_key])) { $obj->modifyHeader($init_key, array_replace_recursive([], $data['header'][$init_key], $new_data)); } else { $obj->modifyHeader($init_key, $new_data); } } else { // TODO: [this is JS handled] if it's single file, remove existing and use set, if it's multiple, use join $obj->join($key, $files); // stores } } } return $obj; }
/** * Return an array with the routes of other translated languages * @return array the page translated languages */ public function translatedLanguages() { $filename = substr($this->name, 0, -strlen($this->extension())); $config = self::getGrav()['config']; $languages = $config->get('system.languages.supported', []); $translatedLanguages = []; foreach ($languages as $language) { $path = $this->path . DS . $this->folder . DS . $filename . '.' . $language . '.md'; if (file_exists($path)) { $aPage = new Page(); $aPage->init(new \SplFileInfo($path), $language . '.md'); $route = isset($aPage->header()->routes['default']) ? $aPage->header()->routes['default'] : $aPage->rawRoute(); if (!$route) { $route = $aPage->slug(); } $translatedLanguages[$language] = $route; } } return $translatedLanguages; }
/** * Add plugin specific assets * @param Page $page * @param Assets $assets */ protected function addAssets(Page $page, Assets $assets) { $pluginAssets = []; if (!empty($page->header()->videoembed['assets'])) { $pluginAssets = (array) $page->header()->videoembed['assets']; } foreach ($pluginAssets as $asset) { $assets->add($asset); } }
private function mergeConfig(Page $page) { $defaults = (array) $this->grav['config']->get('plugins.simple_contact'); if (isset($page->header()->simple_contact)) { if (is_array($page->header()->simple_contact)) { $this->grav['config']->set('plugins.simple_contact', array_replace_recursive($defaults, $page->header()->simplecontact)); } else { $this->grav['config']->set('plugins.simple_contact.enabled', $page->header()->simplecontact); } } else { $this->grav['config']->set('plugins.simple_contact.enabled', false); } }
protected function mergePluginConfig(Page $page) { $defaults = (array) $this->grav['config']->get('plugins.recaptchacontact'); if (isset($page->header()->recaptchacontact)) { if (is_array($page->header()->recaptchacontact)) { $this->grav['config']->set('plugins.recaptchacontact', array_replace_recursive($defaults, $page->header()->recaptchacontact)); } else { $this->grav['config']->set('plugins.recaptchacontact.enabled', $page->header()->recaptchacontact); } } else { $this->grav['config']->set('plugins.recaptchacontact.enabled', false); } $this->enableCssLoading(); }
/** * Twig process that renders a page item. It supports two variations: * 1) Handles modular pages by rendering a specific page based on its modular twig template * 2) Renders individual page items for twig processing before the site rendering * * @param Page $item The page item to render * @param string $content Optional content override * @return string The rendered output * @throws \Twig_Error_Loader */ public function processPage(Page $item, $content = null) { $content = $content !== null ? $content : $item->content(); // override the twig header vars for local resolution $this->grav->fireEvent('onTwigPageVariables'); $twig_vars = $this->twig_vars; $twig_vars['page'] = $item; $twig_vars['media'] = $item->media(); $twig_vars['header'] = $item->header(); $local_twig = clone $this->twig; // Get Twig template layout if ($item->modularTwig()) { $twig_vars['content'] = $content; $template = $item->template() . TEMPLATE_EXT; $output = $local_twig->render($template, $twig_vars); } else { $name = '@Page:' . $item->path(); $this->setTemplate($name, $content); $output = $local_twig->render($name, $twig_vars); } return $output; }
/** * Prepare a page to be stored: update its folder, name, template, header and content * * @param \Grav\Common\Page\Page $page * @param bool $clean_header */ protected function preparePage(\Grav\Common\Page\Page $page, $clean_header = false) { $input = $this->post; $order = max(0, (int) isset($input['order']) ? $input['order'] : $page->value('order')); $ordering = $order ? sprintf('%02d.', $order) : ''; $slug = empty($input['folder']) ? $page->value('folder') : (string) $input['folder']; $page->folder($ordering . $slug); if (isset($input['type']) && !empty($input['type'])) { $type = (string) strtolower($input['type']); $name = preg_replace('|.*/|', '', $type) . '.md'; $page->name($name); $page->template($type); } // Special case for Expert mode: build the raw, unset content if (isset($input['frontmatter']) && isset($input['content'])) { $page->raw("---\n" . (string) $input['frontmatter'] . "\n---\n" . (string) $input['content']); unset($input['content']); } if (isset($input['header'])) { $header = $input['header']; if ($clean_header) { $header = Utils::arrayFilterRecursive($header, function ($k, $v) { return !(is_null($v) || $v === ''); }); } $page->header((object) $header); } // Fill content last because it also renders the output. if (isset($input['content'])) { $page->rawMarkdown((string) $input['content']); } }
/** * Merge global and page theme settings * * @param Page $page The page to merge the page theme configurations * with the theme settings. * @param bool $default The default value in case no theme setting was * found. * * @return array */ protected function mergeThemeConfig(Page $page, $default = null) { while ($page && !$page->root()) { if (isset($page->header()->theme)) { $theme = $page->header()->theme; if ($theme === '@default') { $theme = $default; } return $theme; } $page = $page->parent(); } return $default; }
/** * Merge global and page configurations. * * @param Page $page The page to merge the configurations with the * plugin settings. * @param bool $deep Should you use deep or shallow merging * @param array $params Array of additional configuration options to * merge with the plugin settings. * * @return \Grav\Common\Data\Data */ protected function mergeConfig(Page $page, $deep = false, $params = []) { $class_name = $this->name; $class_name_merged = $class_name . '.merged'; $defaults = $this->config->get('plugins.' . $class_name, []); $page_header = $page->header(); $header = []; if (!isset($page_header->{$class_name_merged}) && isset($page_header->{$class_name})) { // Get default plugin configurations and retrieve page header configuration $config = $page_header->{$class_name}; if (is_bool($config)) { // Overwrite enabled option with boolean value in page header $config = ['enabled' => $config]; } // Merge page header settings using deep or shallow merging technique if ($deep) { $header = array_replace_recursive($defaults, $config); } else { $header = array_merge($defaults, $config); } // Create new config object and set it on the page object so it's cached for next time $page->modifyHeader($class_name_merged, new Data($header)); } else { if (isset($page_header->{$class_name_merged})) { $merged = $page_header->{$class_name_merged}; $header = $merged->toArray(); } } if (empty($header)) { $header = $defaults; } // Merge additional parameter with configuration options if ($deep) { $header = array_replace_recursive($header, $params); } else { $header = array_merge($header, $params); } // Return configurations as a new data config class return new Data($header); }
/** * Merge global and page configurations. * * @param Page $page The page to merge the configurations with the * plugin settings. * * @param bool $deep Should you use deep or shallow merging * * @return \Grav\Common\Data\Data */ protected function mergeConfig(Page $page, $deep = false) { $class_name = $this->name; $class_name_merged = $class_name . '.merged'; $defaults = $this->config->get('plugins.' . $class_name, array()); $header = array(); if (isset($page->header()->{$class_name_merged})) { $merged = $page->header()->{$class_name_merged}; if (count($merged) > 0) { return $merged; } else { return new Data($defaults); } } // Get default plugin configurations and retrieve page header configuration if (isset($page->header()->{$class_name})) { if ($deep) { $header = array_replace_recursive($defaults, $page->header()->{$class_name}); } else { $header = array_merge($defaults, $page->header()->{$class_name}); } } else { $header = $defaults; } // Create new config object and set it on the page object so it's cached for next time $config = new Data($header); $page->modifyHeader($class_name_merged, $config); // Return configurations as a new data config class return $config; }
/** * Add Register page */ public function addRegisterPage() { $route = $this->config->get('plugins.login.route_register'); /** @var Pages $pages */ $pages = $this->grav['pages']; $page = new Page(); $page->init(new \SplFileInfo(__DIR__ . "/pages/register.md")); $page->template('form'); $page->slug(basename($route)); $twig = $this->grav['twig']; $twig->twig_vars['form'] = $page->header()->form; $pages->addPage($page, $route); }