/
facebook-group-publish-link.php
165 lines (122 loc) · 4.41 KB
/
facebook-group-publish-link.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
namespace Uncanny_Automator;
/**
* Class FACEBOOK_GROUP_PUBLISH_LINK
*
* @package Uncanny_Automator
*/
class FACEBOOK_GROUP_PUBLISH_LINK {
use Recipe\Actions;
use Recipe\Action_Tokens;
const INTEGRATION = 'FACEBOOK_GROUPS';
const CODE = 'FACEBOOK_GROUPS_PUBLISH_LINK';
const META = 'FACEBOOK_GROUPS_PUBLISH_LINK_META';
public function __construct() {
$this->setup_action();
}
/**
* Setups our action.
*
* @return void
*/
public function setup_action() {
$this->set_action_code( self::CODE );
$this->set_action_meta( self::META );
$this->set_integration( self::INTEGRATION );
$this->set_is_pro( false );
$this->set_support_link( Automator()->get_author_support_link( $this->get_action_code(), 'knowledge-base/facebook-groups' ) );
$this->set_requires_user( false );
/* translators: Action - WordPress */
$this->set_sentence(
/* translators:The action sentence */
sprintf( esc_attr__( 'Share a link with a message to {{a Facebook group:%1$s}}', 'uncanny-automator' ), $this->get_action_meta() )
);
/* translators: Action - WordPress */
$this->set_readable_sentence(
esc_attr__(
'Share a link with a message to {{a Facebook group}}',
'uncanny-automator'
)
);
$this->set_buttons(
Automator()->helpers->recipe->facebook_groups->options->buttons(
$this->get_action_meta(),
automator_utm_parameters( $this->get_support_link(), 'facebook-group_publish_post', 'help_button' )
)
);
$this->set_options_group( $this->get_options_group() );
$this->set_background_processing( true );
$this->set_wpautop( false );
$this->set_action_tokens(
array(
'POST_LINK' => array(
'name' => __( 'Link to Facebook post', 'uncanny-automator' ),
'type' => 'url',
),
),
$this->get_action_code()
);
$this->register_action();
}
/**
* Proccess our action.
*
* @return void.
*/
public function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {
$helper = Automator()->helpers->recipe->facebook_groups->options;
// Group ID.
$group_id = isset( $parsed[ $this->get_action_meta() ] ) ? sanitize_text_field( $parsed[ $this->get_action_meta() ] ) : 0;
// Message.
$message = isset( $parsed['FACEBOOK_GROUPS_PUBLISH_LINK_MESSAGE'] ) ? sanitize_textarea_field( $parsed['FACEBOOK_GROUPS_PUBLISH_LINK_MESSAGE'] ) : '';
// Link.
$link = isset( $parsed['FACEBOOK_GROUPS_PUBLISH_LINK_URL'] ) ? sanitize_text_field( $parsed['FACEBOOK_GROUPS_PUBLISH_LINK_URL'] ) : '';
$body = array(
'access_token' => $helper->get_user_access_token(),
'message' => $message,
'link' => $link,
'action' => 'send_link',
'group_id' => $group_id,
);
try {
$response = $helper->api_request( $body, $action_data );
$post_id = isset( $response['data']['id'] ) ? $response['data']['id'] : 0;
if ( 0 !== $post_id ) {
$this->hydrate_tokens( array( 'POST_LINK' => 'https://www.facebook.com/' . $post_id ) );
}
Automator()->complete->action( $user_id, $action_data, $recipe_id );
} catch ( \Exception $e ) {
$action_data['complete_with_errors'] = true;
// Log error if there are any error messages.
Automator()->complete->action( $user_id, $action_data, $recipe_id, $e->getMessage() );
}
}
/**
* Returns the list of options for our action.
*
* @return array The fields.
*/
public function get_options_group() {
$facebook_groups = Automator()->helpers->recipe->facebook_groups->options;
return array(
$this->get_action_meta() => array(
Automator()->helpers->recipe->facebook_groups->options->get_groups_field( $this->get_action_meta() ),
array(
'option_code' => 'FACEBOOK_GROUPS_PUBLISH_LINK_URL',
'label' => esc_attr__( 'External link URL', 'uncanny-automator' ),
'placeholder' => esc_attr__( 'https://', 'uncanny-automator' ),
'description' => esc_attr__( "Enter the URL of the site you want to share to your Facebook Group. Start with 'https://' to share an external link.", 'uncanny-automator' ),
'input_type' => 'url',
'supports_custom_value' => false,
'required' => true,
),
array(
'option_code' => 'FACEBOOK_GROUPS_PUBLISH_LINK_MESSAGE',
'label' => esc_attr__( 'Message', 'uncanny-automator' ),
'input_type' => 'textarea',
'required' => true,
),
),
);
}
}