• Resolved snoepys

    (@snoepys)


    Hi! First of all, your plugin is the best review plugin that I have found! Great work!

    I want to send via Slack Webhook URL the info from reviews. I am able to get that but I need to retrieve the data from the email user too and this is the only data that I didn’t get. Is there any way to achieve that?

    Also, I receive the user name alongside IP address, is there any possibility to only get the name without the IP?

    Thank you!

Viewing 15 replies - 1 through 15 (of 17 total)
  • Plugin Author Gemini Labs

    (@geminilabs)

    1. To edit the Slack notification, you will need to use the site-reviews/slack/compose filter hook.

    In the filter hook, you have access to the $notification array and the $slack class object which contains the review as a property (i.e. $slack->review).

    See also: https://api.slack.com/messaging/composing/layouts#building-attachments

    2. If you don’t want to save the IP address with the review, please see the FAQ:

    Thread Starter snoepys

    (@snoepys)

    Hi! Thank you for your fast answer.

    I cannot achieve that in functions.php of my theme. But it works if I modify the plugin in Slack.php adding these lines:

    protected function buildFields()
    {
    $fields = [
    $this->buildStarsField(),
    $this->buildTitleField(),
    $this->buildContentField(),
    $this->buildAuthorField(),
    $this->buildEmailField(),
    $this->buildUrlField(),
    ];
    return array_filter($fields);
    }

    protected function buildEmailField()
    {
    $email = !empty($this->review->email)
    ? ‘<‘.$this->review->email.’>’
    : ”;
    $author = trim(rtrim($this->review->author).’ ‘.$email);
    return [‘value’ => implode(‘ – ‘, array_filter([$this->review->email]))];
    }

    /**
    * @return array
    */
    protected function buildUrlField()
    {
    $posts = $this->review->assignedPosts();
    $postTitles = array_filter(wp_list_pluck($posts, ‘post_title’));
    return Str::naturalJoin($postTitles);
    }

    The problem is that I can’t get the Post_Title. When I use the buildUrlField Slack didn’t receive the webhook. But, if I delete buildUrlField it sends to slack without the URL.

    I have tried to retreive only the URL via:
    global $wp;
    $current_url = home_url( add_query_arg( array(), $wp->request ) );

    But only get the home URL, not the post URL.

    And with this code:
    $actual_link = “https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&#8221;;
    return $actual_link;

    It prints https://mydomain.com/wp-cron.php

    What I am doing wrong?

    Thread Starter snoepys

    (@snoepys)

    Hi again, and sorry for the double post. Finally, I have found the solution after adding at the top of Slack.php these 2 lines:

    use WP_Post;
    use GeminiLabs\SiteReviews\Helpers\Str;

    ??

    At this moment, only need to make it works in functions.php instead of modifying the slack.php file.

    Plugin Author Gemini Labs

    (@geminilabs)

    1. You should never directly edit a plugin. All changes will be removed when the plugin is updated. That’s what the filter hooks are for.

    2. The submitted email address is already included in the slack notification.

    3. You can remove the IP address like this:

    function site_reviews_modify_slack_notification($notification, $slack) {
        $fields = $notification['attachments']['fields'];
        // Remove the IP address
        if (isset($fields[3]['value'])) {
            $fields[3]['value'] = str_replace($slack->review->ip_address, '', $fields[3]['value']);
            $fields[3]['value'] = trim($fields[3]['value'], ' -');
        }
        $notification['attachments']['fields'] = $fields;
        return $notification;
    }
    add_filter('site-reviews/slack/compose', 'site_reviews_modify_slack_notification', 10, 2);
    Thread Starter snoepys

    (@snoepys)

    Hi!

    1.- Yes, I Know. But really don’t know how to add the new field with the link in functions.php. Maybe is related to these 2 lines?
    use WP_Post;
    use GeminiLabs\SiteReviews\Helpers\Str;

    2.- Ok!

    3.- Nice! I can use the same function in order to add the post title in a new field, like fields[4]? Or it’s only to modify an existing field?

    Sorry about so many questions, I am new in php.

    Thank you a lot!

    Plugin Author Gemini Labs

    (@geminilabs)

    $notification['attachments']['fields'][] = [
        'value' => ''; // add a new line to the notification here
    ];
    Thread Starter snoepys

    (@snoepys)

    Hi! I have tried with:

    function site_reviews_modify_slack_notification($notification, $slack) {
    $fields = $notification[‘attachments’][‘fields’][] = [
    ‘value’ => ‘post_title’ // add a new line to the notification here
    ];

    }
    add_filter(‘site-reviews/slack/compose’, ‘site_reviews_modify_slack_notification’, 10, 2);

    And the webhook prints: No data received

    if i add the ; after ‘value’ => ‘post_title’;];
    The editor sais that expecting error with the ;

    Also, I have tried with ‘value’ => ‘$this->review->email’

    And same, no data received. ??

    Plugin Author Gemini Labs

    (@geminilabs)

    That’s because you are modifying the $notification array, but you are not returning it.

    You need to add that bit of code to the original snippet before the return $notification; part.

    • This reply was modified 2 years, 10 months ago by Gemini Labs.
    Thread Starter snoepys

    (@snoepys)

    Sorry again for my inexperience and for disturbing you with so many posts, English is not my native language. But after almost an hour I still can’t get it works. If I put this code:

    function site_reviews_modify_slack_notification($notification, $slack) {

    $fields = $notification[‘attachments’][‘fields’][] = [
    ‘value’ => $this->review->assignedPosts() // add a new line to the notification here
    ];
    $posts = $this->review->assignedPosts();
    $postTitles = array_filter(wp_list_pluck($posts, ‘post_title’));

    $notification[‘attachments’][‘fields’] = $fields;
    return $notification;
    }
    add_filter(‘site-reviews/slack/compose’, ‘site_reviews_modify_slack_notification’, 10, 2);

    It doesn’t send the data to Slack.

    Only send the data with plain text with this code:

    function site_reviews_modify_slack_notification($notification, $slack) {

    $fields = $notification[‘attachments’][‘fields’][] = [
    ‘value’ => ‘$this->review->assignedPosts()’ // add a new line to the notification here
    ];
    $notification[‘attachments’][‘fields’] = $fields;
    return $notification;
    }
    add_filter(‘site-reviews/slack/compose’, ‘site_reviews_modify_slack_notification’, 10, 2);

    And, the webhook only receive this text:
    attachments/fields: $this->review->assignedPosts()
    attachments/fields/value: $this->review->assignedPosts()

    Plugin Author Gemini Labs

    (@geminilabs)

    function site_reviews_modify_slack_notification($notification, $slack) {
        $fields = $notification['attachments']['fields'];
    
        // START: Remove the IP address
        if (isset($fields[3]['value'])) {
            $fields[3]['value'] = str_replace($slack->review->ip_address, '', $fields[3]['value']);
            $fields[3]['value'] = trim($fields[3]['value'], ' -');
        }
        // END: Remove the IP address
    
        // START: Add the assigned post titles
        $posts = $this->review->assignedPosts();
        $postTitles = array_filter(wp_list_pluck($posts, 'post_title'));
        if (!empty($postTitles)) {
            $titles = \GeminiLabs\SiteReviews\Helpers\Str::naturalJoin($postTitles);
            $fields = $notification['attachments']['fields'][] = [
                'value' => 'Review of: '.$titles,
            ];
        }
        // END: Add the assigned post titles
    
        $notification['attachments']['fields'] = $fields;
        return $notification;
    }
    add_filter('site-reviews/slack/compose', 'site_reviews_modify_slack_notification', 10, 2);
    Thread Starter snoepys

    (@snoepys)

    Hi! Thank you a lot for the code!

    Maybe is some incompatibility with my child theme, because it doesn’t send data to the webhook. ??

    Don’t worry. I will use the core and make changes in every plugin update.

    Anyways, thank you a lot again for your help. it has been the best!!! ??

    Plugin Author Gemini Labs

    (@geminilabs)

    Did you remove your other code and replace with the snippet provided?

    Plugin Author Gemini Labs

    (@geminilabs)

    Oh, there is a mistake in the code snippet.

    In the provided snippet, replace this:

            $fields = $notification['attachments']['fields'][] = [
                'value' => 'Review of: '.$titles,
            ];
    

    With this:

            $fields[] = [
                'value' => 'Review of: '.$titles,
            ];
    
    Thread Starter snoepys

    (@snoepys)

    Yes, I have deleted all and put only your snippet.

    Thread Starter snoepys

    (@snoepys)

    Same ??

    This is the code implemented:

    function site_reviews_modify_slack_notification($notification, $slack) {
    $fields = $notification[‘attachments’][‘fields’];

    // START: Remove the IP address
    /* if (isset($fields[3][‘value’])) {
    $fields[3][‘value’] = str_replace($slack->review->ip_address, ”, $fields[3][‘value’]);
    $fields[3][‘value’] = trim($fields[3][‘value’], ‘ -‘);
    }*/
    // END: Remove the IP address

    // START: Add the assigned post titles
    $posts = $this->review->assignedPosts();
    $postTitles = array_filter(wp_list_pluck($posts, ‘post_title’));
    if (!empty($postTitles)) {
    $titles = \GeminiLabs\SiteReviews\Helpers\Str::naturalJoin($postTitles);
    $fields[] = [
    ‘value’ => ‘Review of: ‘.$titles,
    ];
    }
    // END: Add the assigned post titles

    $notification[‘attachments’][‘fields’] = $fields;
    return $notification;
    }
    add_filter(‘site-reviews/slack/compose’, ‘site_reviews_modify_slack_notification’, 10, 2);

    Maybe I need to add something in the functions.php like in core slack.php?

    use GeminiLabs\SiteReviews\Database\OptionManager;
    use GeminiLabs\SiteReviews\Defaults\SlackDefaults;
    use GeminiLabs\SiteReviews\Review;
    use WP_Error;
    use WP_Post;
    use GeminiLabs\SiteReviews\Helpers\Str;

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Slack Integration’ is closed to new replies.