From 8277f45588c8f851aaac24376af6f4bd0a4633e8 Mon Sep 17 00:00:00 2001 From: Bob Fanger Date: Wed, 20 Jun 2018 17:35:45 +0200 Subject: [PATCH] Added remaining models & relations --- src/Model/Comment.php | 5 +- src/Model/CommentMeta.php | 17 +++ src/Model/Link.php | 22 ++++ src/Model/Post.php | 3 - src/Model/PostMeta.php | 17 +++ src/Model/Taxonomy.php | 25 ++++ src/Model/Term.php | 2 - src/Model/TermMeta.php | 17 +++ src/Model/UserMeta.php | 17 +++ src/WordpressRepositoryBackend.php | 205 +++++++++++++++++------------ 10 files changed, 240 insertions(+), 90 deletions(-) create mode 100644 src/Model/CommentMeta.php create mode 100644 src/Model/Link.php create mode 100644 src/Model/PostMeta.php create mode 100644 src/Model/Taxonomy.php create mode 100644 src/Model/TermMeta.php create mode 100644 src/Model/UserMeta.php diff --git a/src/Model/Comment.php b/src/Model/Comment.php index c920012..a853b84 100644 --- a/src/Model/Comment.php +++ b/src/Model/Comment.php @@ -21,8 +21,11 @@ class Comment extends Base implements ArrayAccess public $approved; public $useragent; public $type; + public $parent_id; - public $user_id; + + + public $user; /** * @var Post diff --git a/src/Model/CommentMeta.php b/src/Model/CommentMeta.php new file mode 100644 index 0000000..fefc590 --- /dev/null +++ b/src/Model/CommentMeta.php @@ -0,0 +1,17 @@ + [ + 'comment_ID' => 'id', + 'comment_author' => 'author', + 'comment_author_email' => 'email', + 'comment_author_url' => 'url', + 'comment_date' => 'date', + 'comment_date_gmt' => 'date_gmt', + 'comment_content' => 'content', + 'comment_karma' => 'karma', + 'comment_approved' => 'approved', + 'comment_author_IP' => 'ip', + 'comment_agent' => 'useragent', + 'comment_type' => 'type', + 'comment_parent' => 'parent_id', + ], + 'Commentmetum' => [ + 'meta_id' => 'id', + 'meta_key' => 'key', + 'meta_value' => 'value', + ], + 'Link'=> [ + 'link_id' => 'id', + 'link_url' => 'url', + 'link_name' => 'name', + 'link_image' => 'image', + 'link_target' => 'target', + 'link_description' => 'description', + 'link_visible' => 'visible', + 'link_owner' => 'owner', + 'link_rating' => 'rating', + 'link_updated' => 'updated', + 'link_rel' => 'rel', + 'link_notes' => 'notes', + 'link_rss' => 'rss', + ], + 'Option' => [ + 'option_id' => 'id', + 'option_name' => 'key', + 'option_value' => 'value', + ], 'Post' => [ 'iD' => 'id', 'post_title' => 'title', @@ -58,27 +103,6 @@ public function __construct() 'meta_key' => 'key', 'meta_value' => 'value', ], - 'User' => [ - 'iD' => 'id', - 'user_login' => 'login', - 'user_pass' => 'password', - 'user_nicename' => 'nickname', - 'user_email' => 'email', - 'user_url' => 'url', - 'user_registered' => 'registered', - 'user_activation_key' => 'activation_key', - 'user_status' => 'status', - ], - 'Usermetum' => [ - 'umeta_id' => 'id', - 'meta_key' => 'key', - 'meta_value' => 'value', - ], - 'Option' => [ - 'option_id' => 'id', - 'option_name' => 'key', - 'option_value' => 'value', - ], 'Term' => [ 'term_id' => 'id', 'term_group' => 'group', @@ -92,23 +116,19 @@ public function __construct() 'term_taxonomy_id' => 'id', 'parent' => 'parent_id', ], - 'Comment' => [ - 'comment_ID' => 'id', - 'comment_author' => 'author', - 'comment_author_email' => 'email', - 'comment_author_url' => 'url', - 'comment_date' => 'date', - 'comment_date_gmt' => 'date_gmt', - 'comment_content' => 'content', - 'comment_karma' => 'karma', - 'comment_approved' => 'approved', - 'comment_author_IP' => 'ip', - 'comment_agent' => 'useragent', - 'comment_type' => 'type', - 'comment_parent' => 'parent_id', + 'User' => [ + 'iD' => 'id', + 'user_login' => 'login', + 'user_pass' => 'password', + 'user_nicename' => 'nickname', + 'user_email' => 'email', + 'user_url' => 'url', + 'user_registered' => 'registered', + 'user_activation_key' => 'activation_key', + 'user_status' => 'status', ], - 'Commentmetum' => [ - 'meta_id' => 'id', + 'Usermetum' => [ + 'umeta_id' => 'id', 'meta_key' => 'key', 'meta_value' => 'value', ], @@ -119,12 +139,13 @@ public function __construct() $this->renameProperty($model, $from, $to); } } - // read & write filters + // read & write array filters $mayContainArray = [ 'Option' => 'option_value', 'Postmetum' => 'meta_value', 'Termmetum' => 'meta_value', 'Commentmetum' => 'meta_value', + 'Usermetum' => 'meta_value', ]; foreach ($mayContainArray as $model => $column) { @@ -132,7 +153,42 @@ public function __construct() $this->configs[$model]->writeFilters[$column] = [self::class, 'arrayWriteFilter']; } - // Post tweaks + // Comment + $this->skipProperty('Comment', 'comment_post_ID'); + $this->configs['Comment']->class = Comment::class; + $this->configs['Comment']->belongsTo['post'] = [ + 'model' => 'Post', + 'reference' => 'comment_post_ID', + ]; + $this->configs['Comment']->belongsTo['user'] = [ + 'model' => 'User', + 'reference' => 'user_id', + ]; + $this->configs['Comment']->hasMany['meta'] = [ + 'model' => 'CommentMeta', + 'reference' => 'comment_id', + 'id' => 'comment_id', + 'belongsTo' => 'comment', + ]; + $this->configs['Comment']->defaults = array_merge($this->configs['Comment']->defaults, [ + 'meta' => [], + ]); + $this->skipProperty('Comment', 'user_id'); + $this->configs['Commentmetum']->class = CommentMeta::class; + $this->configs['Commentmetum']->belongsTo['comment'] = [ + 'model' => 'Comment', + 'reference' => 'comment_id', + ]; + $this->skipProperty('Commentmetum', 'comment_id'); + + // Link + $this->configs['Link']->class = Link::class; + + // Option + $this->configs['Option']->class = Option::class; + + // Post + $this->configs['Post']->class = Post::class; $this->configs['Post']->belongsTo['author'] = [ 'model' => 'User', 'reference' => 'post_author', @@ -158,7 +214,6 @@ public function __construct() ]; $this->skipProperty('Post', 'post_author'); - $this->configs['Post']->class = Post::class; $this->configs['Post']->defaults = array_merge($this->configs['Post']->defaults, [ 'excerpt' => '', 'to_ping' => '', @@ -174,13 +229,29 @@ public function __construct() 'modified_gmt' => current_time('mysql', true), ]); - // PostMeta tweaks + $this->configs['Postmetum']->class = PostMeta::class; $this->configs['Postmetum']->belongsTo['post'] = [ 'model' => 'Post', 'reference' => 'post_id', ]; $this->skipProperty('Postmetum', 'post_id'); + // Taxonomy + $this->skipProperty('TermTaxonomy', 'term_id'); + $this->configs['TermTaxonomy']->belongsTo['term'] = [ + 'model' => 'Term', + 'reference' => 'term_id', + ]; + $this->configs['TermTaxonomy']->defaults['description'] = ''; + $this->configs['TermTaxonomy']->hasMany['posts'] = [ + 'model' => 'Post', + 'through' => 'TermRelationship', + 'reference' => 'term_taxonomy_id', + 'id' => 'object_id', + 'fields' => ['term_order' => 'order'], + ]; + $this->configs['TermTaxonomy']->defaults['posts'] = []; + // Term $this->configs['Term']->class = Term::class; $this->configs['Term']->hasMany['taxonomy'] = [ @@ -197,49 +268,13 @@ public function __construct() ]; $this->configs['Term']->defaults['meta'] = []; - // TermMeta + $this->configs['Termmetum']->class = TermMeta::class; $this->configs['Termmetum']->belongsTo['term'] = [ 'model' => 'Term', 'reference' => 'term_id', ]; $this->skipProperty('Termmetum', 'term_id'); - // Taxonomy - $this->skipProperty('TermTaxonomy', 'term_id'); - $this->configs['TermTaxonomy']->belongsTo['term'] = [ - 'model' => 'Term', - 'reference' => 'term_id', - ]; - $this->configs['TermTaxonomy']->defaults['description'] = ''; -// $this->configs['TermTaxonomy']->hasMany['posts'] = [ -// 'model' => 'Post', -// 'through' => 'TermRelationship', -// 'reference' => 'term_taxonomy_id', -// 'id' => 'object_id', -// 'fields' => ['term_order' => 'order'], -// ]; -// $this->configs['TermTaxonomy']->defaults['posts'] = []; - - // Comment - $this->skipProperty('Comment', 'comment_post_ID'); - $this->configs['Comment']->class = Comment::class; - $this->configs['Comment']->belongsTo['post'] = [ - 'model' => 'Post', - 'reference' => 'comment_post_ID', - ]; - $this->configs['Comment']->hasMany['meta'] = [ - 'model' => 'CommentMeta', - 'reference' => 'comment_id', - 'id' => 'comment_id', - 'belongsTo' => 'comment', - ]; - $this->configs['Comment']->defaults = array_merge($this->configs['Comment']->defaults, [ - 'meta' => [], - ]); - $this->configs['Commentmetum']->belongsTo['comment'] = [ - 'model' => 'Comment', - 'reference' => 'comment_id', - ]; // User $this->configs['User']->class = User::class; $this->configs['User']->hasMany['meta'] = [ @@ -262,11 +297,15 @@ public function __construct() ]; $this->configs['User']->defaults['posts'] = []; - // Option - $this->configs['Option']->class = Option::class; + $this->configs['Usermetum']->class = UserMeta::class; + $this->configs['Usermetum']->belongsTo['user'] = [ + 'model' => 'User', + 'reference' => 'user_id', + ]; + $this->skipProperty('Usermetum', 'user_id'); - // - if (array_key_exists('Podsrel', $this->configs)) { // Pods plugin enabled? + // Pods plugin enabled? + if (array_key_exists('Podsrel', $this->configs)) { $this->renameModel('Podsrel', 'PodsRelationship'); $this->skipProperty('Podsrel', 'pod_id'); $this->skipProperty('Podsrel', 'field_id'); @@ -296,7 +335,6 @@ public static function arrayReadFilter($value) return $array; } } - return $value; } @@ -312,7 +350,6 @@ public static function arrayWriteFilter($value) if (is_array($value)) { return serialize($value); } - return $value; }