Exemplo n.º 1
0
 private function findPostType($tax)
 {
     $models = Store::get('models');
     foreach ($models as $serialized) {
         $model = unserialize($serialized);
         if (in_array($tax->slug, $model['taxonomies'])) {
             $this->do_register($tax, $model['post_type']);
         }
     }
 }
Exemplo n.º 2
0
 private static function getModel()
 {
     $models = Store::get('models');
     foreach ($models as $serialized) {
         $model = unserialize($serialized);
         if ($model['post_type'] == strtolower(get_called_class())) {
             return $model;
         }
     }
 }
Exemplo n.º 3
0
 private function setup($exclude_relations = [], $qrfields = [])
 {
     $postObject = get_post($this->id);
     $post_type = strtolower(get_called_class());
     $_class = get_called_class();
     $image_sizes = array_merge(['thumbnail', 'medium', 'large', 'full'], $this->getSizes());
     $fields = $this->getFields();
     $taxonomies = $this->getTaxonomies();
     $modelDefaultFields = ['title', 'thumbnail', 'editor'];
     $multipleFields = ['checkbox_list', 'plupload_image', 'checkbox_tree'];
     // Post default properties
     foreach ($postObject as $key => $value) {
         $chave = str_replace('post_', '', $key);
         if ($this->shouldMount("post", $qrfields) || $this->shouldMount($chave, $qrfields)) {
             $this->{$chave} = $value;
         }
     }
     // Permalink
     if ($this->shouldMount('permalink', $qrfields)) {
         $this->permalink = get_permalink($this->ID);
     }
     // Default post taxonomies
     if ($post_type == "post" && empty($taxonomies)) {
         $taxonomies = array("post_tag", "category");
     }
     // Author
     $author = new \stdClass();
     if ($this->shouldMount('author', $qrfields)) {
         foreach (array('ID', 'display_name', 'nickname', 'first_name', 'last_name', 'description', 'email') as $field) {
             $author->{$field} = get_the_author_meta($field, $this->author);
         }
     }
     if ($this->shouldMount('author', $qrfields)) {
         $this->author = $author;
     }
     if ($post_type == "post" || $this->shouldMount('content', $qrfields)) {
         $this->content = apply_filters('the_content', $this->content);
     }
     // Terms
     if (!empty($taxonomies) && $this->shouldMount('taxonomies', $qrfields)) {
         foreach ($taxonomies as $taxonomy) {
             $terms = array();
             $obj = get_the_terms($this->ID, $taxonomy);
             if (is_array($obj)) {
                 foreach ($obj as $term) {
                     $term->link = get_term_link($term->term_id, $taxonomy);
                     array_push($terms, $term);
                 }
             }
             $this->{$taxonomy} = $terms;
         }
     }
     // Custom fields
     foreach ($fields as $key => $value) {
         $is_multiple = !empty($value['multiple']) && $value['multiple'];
         if (!in_array($key, $modelDefaultFields) && $this->shouldMount($key, $qrfields)) {
             if ($value['type'] !== 'image' && $value['type'] !== 'file') {
                 if ($is_multiple || in_array($value['type'], $multipleFields)) {
                     $this->{$key} = get_post_meta($postObject->ID, $key);
                 } else {
                     $this->{$key} = get_post_meta($postObject->ID, $key, true);
                 }
             } else {
                 switch ($value['type']) {
                     case 'image':
                         $this->{$key} = $this->getImage($postObject, $key, $value);
                         break;
                     case 'file':
                         $this->{$key} = $this->getFile($postObject, $key, $value);
                         break;
                 }
             }
         }
     }
     // Relations
     $has_many = Store::get('relation_has_many');
     if ($this->shouldMount('relations', $qrfields)) {
         foreach ($has_many as $many) {
             if ($many['target'] == $post_type && !in_array($many['model'], $exclude_relations)) {
                 $manyqr = new \WP_Query(['post_type' => $many['model'], 'meta_key' => $many['target'], 'meta_value' => $this->ID]);
                 if ($manyqr->have_posts()) {
                     $ids = [];
                     foreach ($manyqr->posts as $_post) {
                         $klass = $this->getClass($many['model']);
                         array_push($ids, new $klass($_post->ID, [$many['target']]));
                     }
                     $this->{$many['model']} = $ids;
                 } else {
                     $this->{$many['model']} = [];
                 }
             } else {
                 if ($many['model'] == $post_type) {
                     if (is_array($this->{$many['target']})) {
                         $ids = [];
                         foreach ($this->{$many['target']} as $item) {
                             $klass = $this->getClass($many['target']);
                             array_push($ids, new $klass($item, [$many['model']]));
                         }
                         $this->{$many['target']} = $ids;
                     } else {
                         $klass = $this->getClass($many['target']);
                         $this->{$many['target']} = new $klass($this->{$many['target']}, [$many['model']]);
                     }
                 }
             }
         }
     }
     $belongs_to = Store::get('relation_belongs_to');
     if ($this->shouldMount('relations', $qrfields)) {
         foreach ($belongs_to as $bel) {
             if ($bel['target'] == $post_type && !in_array($bel['model'], $exclude_relations)) {
                 $belqr = new \WP_Query(['post_type' => $bel['model'], 'meta_key' => $bel['target'], 'meta_value' => $this->ID]);
                 if ($belqr->have_posts()) {
                     $klass = $this->getClass($bel['model']);
                     $this->{$bel['model']} = new $klass($belqr->posts[0], [$bel['target']]);
                 } else {
                     $this->{$bel['model']} = null;
                 }
             } else {
                 if ($bel['model'] == $post_type) {
                     $klass = $this->getClass($bel['target']);
                     $this->{$bel['target']} = new $klass($this->{$bel['target']}, [$bel['model']]);
                 }
             }
         }
     }
     // Include subpages
     if ($post_type == 'page' && $this->shouldMount('children', $qrfields)) {
         $my_wp_query = new \WP_Query();
         $all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
         // Filter through all pages and find Portfolio's children
         $children = get_page_children($this->ID, $all_wp_pages);
         $this->children = array();
         foreach ($children as $child) {
             array_push($this->children, \Page::findById($child->ID));
         }
     }
     // Set the thumbnail
     if ($this->shouldMount('thumbnail', $qrfields)) {
         $image = get_post_thumbnail_id($postObject->ID);
         $img = new \stdClass();
         foreach ($image_sizes as $s) {
             $wp_image = wp_get_attachment_image_src($image, $s);
             $img->{$s} = $wp_image[0];
         }
         $this->thumbnail = $img;
     }
 }