protected function prepareStandardEmbed($embedUrl, $urlQuery, array $ignore = [])
 {
     $userOpts = [];
     if (!empty($urlQuery)) {
         parse_str(trim($urlQuery, '?&'), $userOpts);
     }
     $userOpts = array_merge((array) $this->config->get('embed_options', []), $userOpts);
     $userOpts = array_diff_key($userOpts, array_flip($ignore));
     if (!empty($userOpts)) {
         $embedUrl .= '?' . http_build_query($userOpts);
     }
     $this->embed->setAttribute('src', $embedUrl);
     return $this->embed;
 }
 /**
  * Load shortcodes already provided by this plugin.
  */
 protected function loadShortcodes()
 {
     $iterator = new \FilesystemIterator(__DIR__ . '/Shortcodes');
     foreach ($iterator as $fileinfo) {
         $name = $fileinfo->getBasename('.php');
         // Load shortcodes in directory "Shortcodes"
         $class = __NAMESPACE__ . "\\Shortcodes\\{$name}";
         $defaults = $this->config->get('plugins.shortcodes.shortcodes.' . strtolower($name), []);
         if (empty($defaults) || $defaults['enabled']) {
             $options = isset($defaults['options']) ? $defaults['options'] : [];
             $shortcode = new $class($options);
             $this->register($shortcode);
         }
     }
     // Fire event
     self::getGrav()->fireEvent('onShortcodesInitialized', new Event(['shortcodes' => $this]));
     $this->shortcodes = $this->twig->getShortcodes();
     return array_keys($this->shortcodes);
 }
Example #3
0
 /** -------------------------------
  * Private/protected helper methods
  * --------------------------------
  */
 protected function format($string, $params = [])
 {
     $keys = ['id', 'name', 'url'];
     foreach ($keys as $key) {
         if (!isset($params["{:{$key}}"])) {
             $params["{:{$key}}"] = $this->{$key}();
         }
     }
     $params += ['{:canonical}' => $this->config->get('canonical', '')];
     // Format URL placeholder with params
     $keys = ['{:url}', '{:canonical}'];
     foreach ($keys as $key) {
         $params[$key] = urlencode(str_ireplace(array_keys($params), $params, $params[$key]));
     }
     // Replace OEmbed calls with response
     $string = preg_replace_callback('~\\{\\:oembed(?:\\.(?=\\w))([\\.\\w_]+)?\\}~i', function ($match) {
         $ombed = $this->getOEmbed();
         return $oembed ? $oembed->get($match[1], '') : $match[0];
     }, $string);
     return str_ireplace(array_keys($params), $params, $string);
 }
 /**
  * @param Data $config
  * @return \DOMElement|null
  */
 protected function getEmbedContainer(Data $config)
 {
     $container = null;
     if ($cElem = $config->get('container.element')) {
         $document = new \DOMDocument();
         $container = $document->createElement($cElem);
         $containerAttr = (array) $config->get('container.html_attr', []);
         foreach ($containerAttr as $htmlAttr => $attrValue) {
             $container->setAttribute($htmlAttr, $attrValue);
         }
     }
     return $container;
 }
Example #5
0
 /** -------------------------------
  * Private/protected helper methods
  * --------------------------------
  */
 protected function format($string, $params = [])
 {
     $params += ['{:id}' => $this->id(), '{:name}' => $this->name(), '{:url}' => urlencode($this->config->get('website', ''))];
     // Format URL placeholder with params
     $params['{:url}'] = urlencode(str_ireplace(array_keys($params), $params, $params['{:url}']));
     $string = preg_replace_callback('~\\{\\:oembed(?:\\.(?=\\w))([\\.\\w_]+)?\\}~i', function ($match) {
         static $oembed;
         if (is_null($oembed)) {
             $oembed = new Data($this->getOEmbed());
         }
         return $oembed->get($match[1], '');
     }, $string);
     return str_ireplace(array_keys($params), $params, $string);
 }
Example #6
0
 /**
  * Handle form processing on POST action.
  */
 public function post()
 {
     $grav = Grav::instance();
     $uri = $grav['uri']->url;
     $session = $grav['session'];
     if (isset($_POST)) {
         $this->values = new Data(isset($_POST) ? (array) $_POST : []);
         $data = $this->values->get('data');
         // Add post data to form dataset
         if (!$data) {
             $data = $this->values->toArray();
         }
         if (method_exists('Grav\\Common\\Utils', 'getNonce')) {
             if (!$this->values->get('form-nonce') || !Utils::verifyNonce($this->values->get('form-nonce'), 'form')) {
                 $event = new Event(['form' => $this, 'message' => $grav['language']->translate('PLUGIN_FORM.NONCE_NOT_VALIDATED')]);
                 $grav->fireEvent('onFormValidationError', $event);
                 return;
             }
         }
         $i = 0;
         foreach ($this->items['fields'] as $key => $field) {
             $name = isset($field['name']) ? $field['name'] : $key;
             if (!isset($field['name'])) {
                 if (isset($data[$i])) {
                     //Handle input@ false fields
                     $data[$name] = $data[$i];
                     unset($data[$i]);
                 }
             }
             if ($field['type'] == 'checkbox') {
                 $data[$name] = isset($data[$name]) ? true : false;
             }
             $i++;
         }
         $this->data->merge($data);
     }
     // Validate and filter data
     try {
         $this->data->validate();
         $this->data->filter();
         $grav->fireEvent('onFormValidationProcessed', new Event(['form' => $this]));
     } catch (\RuntimeException $e) {
         $event = new Event(['form' => $this, 'message' => $e->getMessage(), 'messages' => $e->getMessages()]);
         $grav->fireEvent('onFormValidationError', $event);
         if ($event->isPropagationStopped()) {
             return;
         }
     }
     // Process previously uploaded files for the current URI
     // and finally store them. Everything else will get discarded
     $queue = $session->getFlashObject('files-upload');
     $queue = $queue[base64_encode($uri)];
     if (is_array($queue)) {
         foreach ($queue as $key => $files) {
             foreach ($files as $destination => $file) {
                 if (!rename($file['tmp_name'], $destination)) {
                     throw new \RuntimeException(sprintf($grav['language']->translate('PLUGIN_FORM.FILEUPLOAD_UNABLE_TO_MOVE', null, true), '"' . $file['tmp_name'] . '"', $destination));
                 }
                 unset($files[$destination]['tmp_name']);
             }
             $this->data->merge([$key => $files]);
         }
     }
     $process = isset($this->items['process']) ? $this->items['process'] : [];
     if (is_array($process)) {
         $event = null;
         foreach ($process as $action => $data) {
             if (is_numeric($action)) {
                 $action = \key($data);
                 $data = $data[$action];
             }
             $previousEvent = $event;
             $event = new Event(['form' => $this, 'action' => $action, 'params' => $data]);
             if ($previousEvent) {
                 if (!$previousEvent->isPropagationStopped()) {
                     $grav->fireEvent('onFormProcessed', $event);
                 } else {
                     break;
                 }
             } else {
                 $grav->fireEvent('onFormProcessed', $event);
             }
         }
     } else {
         // Default action.
     }
 }
 /**
  * process the content by running over all the known shortcodes with the
  * chosen parser
  * 
  * @param  Page   $page   the page to work on
  * @param  Data   $config configuration merged with the page config
  */
 public function processContent(Page $page, Data $config)
 {
     switch ($config->get('parser')) {
         case 'regular':
             $parser = 'Thunder\\Shortcode\\Parser\\RegularParser';
             break;
         case 'wordpress':
             $parser = 'Thunder\\Shortcode\\Parser\\WordpressParser';
             break;
         default:
             $parser = 'Thunder\\Shortcode\\Parser\\RegexParser';
             break;
     }
     if ($page && $config->get('enabled')) {
         $content = $page->getRawContent();
         $processor = new Processor(new $parser(new CommonSyntax()), $this->handlers);
         $processor = $processor->withEventContainer($this->events);
         $processed_content = $processor->process($content);
         return $processed_content;
     }
 }