Module: Wpxf::WordPress::Comments

Includes:
Wpxf
Defined in:
lib/wpxf/wordpress/comments.rb

Overview

Provides functionality for gathering and posting comments.

Instance Method Summary collapse

Instance Method Details

#initializeObject

Initialises a new instance of Wpxf::WordPress::Comments



8
9
10
11
12
# File 'lib/wpxf/wordpress/comments.rb', line 8

def initialize
  super

  _register_comment_options if should_register_comment_posting_options
end

#post_wordpress_comment(post_id, content, author, email, website) ⇒ Integer

Post a comment.

Parameters:

  • post_id (Integer)

    the post ID to comment on.

  • content (String)

    the content of the comment.

  • author (String)

    the author's name.

  • email (String)

    the author's e-mail address.

  • website (String)

    the author's website.

Returns:

  • (Integer)

    the ID of the comment, or -1 if the comment failed to post.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wpxf/wordpress/comments.rb', line 27

def post_wordpress_comment(post_id, content, author, email, website)
  comment_id = -1

  scoped_option_change('follow_http_redirection', false) do
    res = execute_post_request(
      url: wordpress_url_comments_post,
      cookie: session_cookie,
      body: {
        author: author,
        comment: content,
        email: email,
        url: website,
        submit: 'Post Comment',
        comment_post_ID: post_id,
        comment_parent: 0
      }
    )

    if res&.code == 302
      id = res.headers['Location'][/#comment-([0-9]+)/i, 1]
      comment_id = id.to_i if id
    end
  end

  comment_id
end

#should_register_comment_posting_optionsBoolean

Returns a value indicating whether or not to register the options required to post a WordPress comment.

Returns:

  • (Boolean)

    a value indicating whether or not to register the options required to post a WordPress comment.



16
17
18
# File 'lib/wpxf/wordpress/comments.rb', line 16

def should_register_comment_posting_options
  true
end