Class: Compostr::Syncer

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/compostr/syncer.rb

Overview

Magically syncs custom posts between json, intermediate representation or other forms with data in wordpress. Scary API award.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #error, #fatal, #info, #warn

Constructor Details

#initialize(image_uploader, force = false) ⇒ Syncer

Returns a new instance of Syncer.



12
13
14
15
16
17
# File 'lib/compostr/syncer.rb', line 12

def initialize image_uploader, force=false
  @image_uploader = image_uploader
  @synced_uuids   = []
  @updated_uuids  = []
  @force          = force
end

Instance Attribute Details

#forceObject

Returns the value of attribute force.



10
11
12
# File 'lib/compostr/syncer.rb', line 10

def force
  @force
end

#image_uploaderObject

Returns the value of attribute image_uploader.



7
8
9
# File 'lib/compostr/syncer.rb', line 7

def image_uploader
  @image_uploader
end

#synced_uuidsObject

Returns the value of attribute synced_uuids.



8
9
10
# File 'lib/compostr/syncer.rb', line 8

def synced_uuids
  @synced_uuids
end

#updated_uuidsObject

Returns the value of attribute updated_uuids.



9
10
11
# File 'lib/compostr/syncer.rb', line 9

def updated_uuids
  @updated_uuids
end

Instance Method Details

#adjust_content(content) ⇒ Object

Add language term and post author data to WP content hash.



89
90
91
92
93
# File 'lib/compostr/syncer.rb', line 89

def adjust_content content
  content[:terms_names]  = { 'language' => [ Compostr::config.language_term || 'Deutsch' ] }
  content[:post_author] = Compostr::config.author_id || 1
  # publish? Date ... ?
end

#merge_push(new_post, old_post) ⇒ Object

Updates or creates a Custom Post Types Post.

The post will be identified by uuid (or not).

new_post_content (a CustomPostType)
  The data that **should** be in wordpress (without knowing
  of wordpress post or custom field ids).
  Usually descendant of Compostr::CustomPostType.

old_post (a hash)
  The data currently available in wordpress (including
  wordpress post id, custom field ids).

Returns post_id (of WordPress) or nil (if failure).



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/compostr/syncer.rb', line 33

def merge_push new_post, old_post
  if old_post && old_post.in_wordpress?
    info "#{new_post.class.name} with UUID #{new_post.uuid} found, updating"
    debug old_post.fields.inspect

    new_post.different_from? old_post

    new_post.post_id = old_post.post_id
    new_post.integrate_field_ids old_post

    # TODO unclear how to deal with images
    #attachment_id = @image_uploader.process json['image_url']
    #new_entity.featured_image_id = attachment_id

    #TODO one (cat) is missing when updating single via bundle json

    content = new_post.to_content_hash
    adjust_content content
    # TODO and image ...

    debug "Upload Post ##{new_post.post_id} with wp-content: #{content}"

    post_id = Compostr::wp.editPost(blog_id: 0,
                                    post_id: new_post.post_id,
                                    content: content)
    if post_id
      info "#{new_post.class} ##{new_post.post_id} updated"
    else
      info "#{new_post.class} ##{new_post.post_id} not updated!"
    end
    return post_id
  else
    # Easy, create new one
    info "#{new_post.class.name} with UUID #{new_post.uuid} not found, creating"
    content = new_post.to_content_hash
    adjust_content content

    # Ouch ....
    #attachment_id = @image_uploader.process json['image_url']
    #custom_post.featured_image_id = attachment_id

    debug "Create Post with wp-content: #{content}"

    new_post_id = Compostr::wp.newPost(blog_id: 0,
                                       content: content)
    if new_post_id
      info "#{new_post.class} with WP ID #{new_post_id} created"
    else
      info "#{new_post.class} not created!"
    end
    new_post.post_id = new_post_id
    return new_post_id
  end
end