コード例 #1
0
 /**
  * Update registry of metaplates
  *
  * Note: Does not save the metaplate itself.
  *
  * @param array $new_value The new item to add.
  * @param string $id Id of new item to add.
  *
  * @return bool
  */
 private function update_registry($new_value, $id)
 {
     return data::update_registry($new_value, $id);
 }
コード例 #2
0
ファイル: render.php プロジェクト: calderawp/metaplate-core
 /**
  * Return the content with metaplate applied.
  *
  * @uses "the_content" filter
  *
  * @param string $content Post content
  * @param array|null $meta_stalk Optional. The metaplate to use to render the data. If is null, the default, one will be load, if possible, based on current global $post object.
  * @param array|null $template_data Optional. Prepared field data to render metaplate with. If is null, the default, meta stalk will be retrieved, if possible, based on current global $post object.
  * @param string|null $placement Optional. Where to put the template output, before, after or in place of $content. If is null, option from metaplate is used. Default is--funcitonally speaking--replace. Options: prepend|append|replace|null.
  *
  * @return    string    Rendered HTML with templates applied--if templates and data were provided.
  */
 public function render_metaplate($content, $meta_stack = null, $template_data = null, $placement = null)
 {
     if (is_null($meta_stack)) {
         $meta_stack = data::get_active_metaplates();
     }
     // clear out <!--metaplate-->
     $content = str_replace('<p><!--metaplate--></p>', '', $content);
     // in case the wpautop didnt detect the tag.
     $content = str_replace('<!--metaplate-->', '', $content);
     if (empty($meta_stack)) {
         return $content;
     }
     if (is_null($template_data)) {
         global $post;
         if (caldera_metaplate_pods_mode()) {
             $instance_id = $template_data['ID'];
         } else {
             $instance_id = $post->ID;
             $template_data = data::get_custom_field_data($post->ID);
         }
     } else {
         $instance_id = md5(json_encode((array) $template_data));
     }
     // setup the instance record
     if (!isset($this->rendered_posts[$instance_id])) {
         $this->rendered_posts[$instance_id] = array();
     }
     if (!$template_data || empty($template_data)) {
         return $content;
     }
     // unserilize if needed.
     if (true != caldera_metaplate_pods_mode()) {
         foreach ($template_data as &$meta_item) {
             $meta_item = maybe_unserialize($meta_item);
         }
     }
     // add filter.
     $magic = new filter\magictag();
     $content = $magic->do_magic_tag(trim($content));
     $style_data = null;
     $script_data = null;
     $engine = new Handlebars();
     $engine = $this->helpers($engine);
     foreach ($meta_stack as $metaplate) {
         if (!isset($metaplate['id'])) {
             $metaplate['id'] = md5(json_encode((array) $template_data));
         }
         // add metaplate_id to rendered plates for this post
         if (isset($this->rendered_posts[$instance_id])) {
             if (!empty($this->rendered_posts[$instance_id][$metaplate['id']])) {
                 continue;
             }
             $this->rendered_posts[$instance_id][$metaplate['id']] = true;
         }
         // apply filter to data for this metaplate
         $template_data = apply_filters('metaplate_data', $template_data, $metaplate);
         // check CSS
         if (isset($metaplate['css']['code'])) {
             $style_data .= $engine->render($metaplate['css']['code'], $template_data);
         } else {
             $style_data = '';
         }
         if (isset($metaplate['js']['code'])) {
             // check JS
             $script_data .= $engine->render($metaplate['js']['code'], $template_data);
         } else {
             $script_data = '';
         }
         if (!is_null($placement)) {
             $metaplate['placement'] = $placement;
         }
         if (!isset($metaplate['placement']) || !in_array($metaplate['placement'], array('prepend', 'append', 'replace'))) {
             $metaplate['placement'] = false;
         }
         $template = $metaplate['html']['code'];
         switch ($metaplate['placement']) {
             case 'prepend':
                 $content = $engine->render($template, $template_data) . $content;
                 break;
             case 'append':
                 $content .= $engine->render($template, $template_data);
                 break;
             case 'replace':
                 $content = $engine->render(str_replace('{{content}}', $content, $template), $template_data);
                 break;
             default:
                 $content = $engine->render(str_replace('{{content}}', $content, $template), $template_data);
         }
     }
     // insert CSS
     if (!empty($style_data)) {
         $content = '<style>' . $style_data . '</style>' . $content;
     }
     // insert JS
     if (!empty($script_data)) {
         $content .= '<script type="text/javascript">' . $script_data . '</script>';
     }
     // add magic filter.
     $magic = new filter\magictag();
     $content = $magic->do_magic_tag($content);
     return $content;
 }