PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / trunk
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages vtrunk
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / includes / class-convertkit-broadcasts-exporter.php

class-convertkit-broadcasts-exporter.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages trunk, at includes/class-convertkit-broadcasts-exporter.php

259 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Broadcasts Exporter class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Class to create ConvertKit Broadcasts from WordPress Posts.
11 *
12 * @since 2.4.0
13 */
14 class ConvertKit_Broadcasts_Exporter {
15
16 /**
17 * Holds the action name in the WP_List_Table.
18 *
19 * @since 2.4.0
20 *
21 * @var string
22 */
23 private $action_name = 'broadcast-export';
24
25 /**
26 * Holds the Broadcast ID that was created on ConvertKit.
27 *
28 * @since 2.4.0
29 *
30 * @var bool|int
31 */
32 private $broadcast_id = false;
33
34 /**
35 * Holds the Broadcasts Settings class.
36 *
37 * @since 2.4.0
38 *
39 * @var bool|ConvertKit_Settings_Broadcasts
40 */
41 private $broadcasts_settings = false;
42
43 /**
44 * Holds the Settings class.
45 *
46 * @since 2.4.0
47 *
48 * @var bool|ConvertKit_Settings
49 */
50 private $settings = false;
51
52 /**
53 * Constructor
54 *
55 * @since 2.4.0
56 */
57 public function __construct() {
58
59 // Bail if no API credentials have been set.
60 $this->settings = new ConvertKit_Settings();
61 if ( ! $this->settings->has_access_and_refresh_token() ) {
62 return;
63 }
64
65 // Bail if the Enable Export Actions setting is not enabled.
66 $this->broadcasts_settings = new ConvertKit_Settings_Broadcasts();
67 if ( ! $this->broadcasts_settings->enabled_export() ) {
68 return;
69 }
70
71 // Run action, if selected.
72 add_action( 'init', array( $this, 'run_row_action' ) );
73
74 // Add action below Post Title in WP_List_Table classes.
75 add_filter( 'page_row_actions', array( $this, 'add_row_action' ), 10, 2 );
76 add_filter( 'post_row_actions', array( $this, 'add_row_action' ), 10, 2 );
77
78 }
79
80 /**
81 * Checks if a Plugin row action was clicked by the User, and if so performs that action.
82 *
83 * @since 2.4.0
84 */
85 public function run_row_action() {
86
87 // Bail if no nonce exists or fails verification.
88 if ( ! array_key_exists( 'nonce', $_REQUEST ) ) {
89 return;
90 }
91 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['nonce'] ) ), 'action-convertkit-' . $this->action_name ) ) {
92 return;
93 }
94
95 // If no action or ID specified, return.
96 if ( ! isset( $_REQUEST['convertkit-action'] ) ) {
97 return;
98 }
99 if ( ! isset( $_REQUEST['id'] ) ) {
100 return;
101 }
102
103 // Fetch action and post ID.
104 $action = sanitize_text_field( wp_unslash( $_REQUEST['convertkit-action'] ) );
105 $post_id = absint( $_REQUEST['id'] );
106
107 // Bail if the action isn't for exporting a post.
108 if ( $action !== $this->action_name ) {
109 return;
110 }
111
112 // Bail if the current user cannot edit the Post being exported.
113 if ( ! current_user_can( 'edit_post', $post_id ) ) {
114 wp_die( esc_html__( 'Sorry, you are not allowed to export this Post.', 'convertkit' ) );
115 }
116
117 // Export Post to a draft ConvertKit Broadcast.
118 $result = $this->export_post_to_broadcast( $post_id );
119
120 // If an error occurred, display an error message.
121 if ( is_wp_error( $result ) ) {
122 wp_die( esc_html( $result->get_error_message() ) );
123 }
124
125 // Store Broadcast ID for success notice.
126 $this->broadcast_id = $result['id'];
127
128 // Display a success notice with a link to editing the Broadcast on ConvertKit.
129 add_action( 'admin_notices', array( $this, 'output_success_notice' ) );
130
131 }
132
133 /**
134 * Outputs a success notice in the WordPress Administration interface that the Post was
135 * successfully created in ConvertKit as a Broadcast, with a link to edit in ConvertKit.
136 *
137 * @since 2.4.0
138 */
139 public function output_success_notice() {
140
141 // Bail if no Broadcast ID specified.
142 if ( ! $this->broadcast_id ) {
143 return;
144 }
145
146 // Output success notice.
147 ?>
148 <div class="notice notice-success is-dismissible">
149 <p>
150 <?php
151 printf(
152 '%s <a href="%s" target="_blank">%s</a> %s',
153 esc_html__( 'Successfully created Kit Broadcast from Post.', 'convertkit' ),
154 esc_url( convertkit_get_edit_broadcast_url( $this->broadcast_id ) ),
155 esc_html__( 'Click here', 'convertkit' ),
156 esc_html__( 'to edit and send the broadcast in Kit.', 'convertkit' )
157 );
158 ?>
159 </p>
160 </div>
161 <?php
162
163 }
164
165 /**
166 * Adds a 'Create ConvertKit Broadcast' action below the Post Title in WP_List_Table classes.
167 *
168 * @since 2.4.0
169 *
170 * @param array $actions Row Actions.
171 * @param WP_Post $post WordPress Post.
172 * @return array Row Actions
173 */
174 public function add_row_action( $actions, $post ) {
175
176 // Build URL.
177 $url = add_query_arg(
178 array(
179 'post_type' => $post->post_type,
180 'convertkit-action' => 'broadcast-export',
181 'id' => $post->ID,
182 'nonce' => wp_create_nonce( 'action-convertkit-broadcast-export' ),
183 ),
184 'edit.php'
185 );
186
187 // Add action.
188 $actions['convertkit_broadcast_export'] = '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Create as Kit Broadcast', 'convertkit' ) . '</a>';
189
190 // Return.
191 return $actions;
192
193 }
194
195 /**
196 * Creates a draft Broadcast in ConvertKit from the given WordPress Post ID,
197 * storing the Broadcast ID against in the Post's meta.
198 *
199 * @since 2.4.0
200 *
201 * @param int $post_id Post ID.
202 * @return WP_Error|array
203 */
204 public function export_post_to_broadcast( $post_id ) {
205
206 // Get Post.
207 $post = get_post( $post_id );
208
209 // Return an error if the Post could not be fetched.
210 if ( ! $post ) {
211 return new WP_Error(
212 'convertkit_broadcasts_exporter_export_post_to_broadcast',
213 sprintf(
214 /* translators: WordPress Post ID */
215 esc_html__( 'Could not fetch Post ID %s.', 'convertkit' ),
216 $post_id
217 )
218 );
219 }
220
221 // Fetch post's content by running it through the_content filter.
222 // This will convert e.g. page builders and blocks to HTML.
223 $content = apply_filters( 'the_content', $post->post_content );
224
225 // Remove HTML tags that are not supported in ConvertKit Broadcasts.
226 $content = WP_ConvertKit()->get_class( 'broadcasts_importer' )->get_permitted_html( $content, $this->broadcasts_settings->no_styles() );
227
228 // Initialize the API.
229 $api = new ConvertKit_API_V4(
230 CONVERTKIT_OAUTH_CLIENT_ID,
231 CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI,
232 $this->settings->get_access_token(),
233 $this->settings->get_refresh_token(),
234 $this->settings->debug_enabled(),
235 'settings'
236 );
237
238 // Create draft Broadcast in ConvertKit.
239 $result = $api->create_broadcast(
240 $post->post_title,
241 $content,
242 $post->post_excerpt
243 );
244
245 // If an error occurred, return it now.
246 if ( is_wp_error( $result ) ) {
247 return $result;
248 }
249
250 // Store the Broadcast ID against the WordPress Post.
251 update_post_meta( $post->ID, '_convertkit_broadcast_export_id', $result['broadcast']['id'] );
252
253 // Return result.
254 return $result['broadcast'];
255
256 }
257
258 }
259