| 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( 'post_row_actions', array( $this, 'add_row_action' ), 10, 2 ); |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Checks if a Plugin row action was clicked by the User, and if so performs that action. |
| 81 |
* |
| 82 |
* @since 2.4.0 |
| 83 |
*/ |
| 84 |
public function run_row_action() { |
| 85 |
|
| 86 |
// Bail if no nonce exists or fails verification. |
| 87 |
if ( ! array_key_exists( 'nonce', $_REQUEST ) ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'action-convertkit-' . $this->action_name ) ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// If no action specified, return. |
| 95 |
if ( ! isset( $_REQUEST['convertkit-action'] ) ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
// Fetch action and post ID. |
| 100 |
$action = sanitize_text_field( $_REQUEST['convertkit-action'] ); |
| 101 |
$post_id = absint( $_REQUEST['id'] ); |
| 102 |
|
| 103 |
// Bail if the action isn't for exporting a post. |
| 104 |
if ( $action !== $this->action_name ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
// Export Post to a draft ConvertKit Broadcast. |
| 109 |
$result = $this->export_post_to_broadcast( $post_id ); |
| 110 |
|
| 111 |
// If an error occured, display an error message. |
| 112 |
if ( is_wp_error( $result ) ) { |
| 113 |
wp_die( esc_html( $result->get_error_message() ) ); |
| 114 |
} |
| 115 |
|
| 116 |
// Store Broadcast ID for success notice. |
| 117 |
$this->broadcast_id = $result['id']; |
| 118 |
|
| 119 |
// Display a success notice with a link to editing the Broadcast on ConvertKit. |
| 120 |
add_action( 'admin_notices', array( $this, 'output_success_notice' ) ); |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Outputs a success notice in the WordPress Administration interface that the Post was |
| 126 |
* successfully created in ConvertKit as a Broadcast, with a link to edit in ConvertKit. |
| 127 |
* |
| 128 |
* @since 2.4.0 |
| 129 |
*/ |
| 130 |
public function output_success_notice() { |
| 131 |
|
| 132 |
// Bail if no Broadcast ID specified. |
| 133 |
if ( ! $this->broadcast_id ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
// Output success notice. |
| 138 |
?> |
| 139 |
<div class="notice notice-success is-dismissible"> |
| 140 |
<p> |
| 141 |
<?php |
| 142 |
printf( |
| 143 |
'%s <a href="%s" target="_blank">%s</a> %s', |
| 144 |
esc_html__( 'Successfully created ConvertKit Broadcast from Post.', 'convertkit' ), |
| 145 |
esc_url( convertkit_get_edit_broadcast_url( $this->broadcast_id ) ), |
| 146 |
esc_html__( 'Click here', 'convertkit' ), |
| 147 |
esc_html__( 'to edit and send the broadcast in ConvertKit.', 'convertkit' ) |
| 148 |
); |
| 149 |
?> |
| 150 |
</p> |
| 151 |
</div> |
| 152 |
<?php |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Adds a 'Create ConvertKit Broadcast' action below the Post Title in WP_List_Table classes. |
| 158 |
* |
| 159 |
* @since 2.4.0 |
| 160 |
* |
| 161 |
* @param array $actions Row Actions. |
| 162 |
* @param WP_Post $post WordPress Post. |
| 163 |
* @return array Row Actions |
| 164 |
*/ |
| 165 |
public function add_row_action( $actions, $post ) { |
| 166 |
|
| 167 |
// Build URL. |
| 168 |
$url = add_query_arg( |
| 169 |
array( |
| 170 |
'convertkit-action' => 'broadcast-export', |
| 171 |
'id' => $post->ID, |
| 172 |
'nonce' => wp_create_nonce( 'action-convertkit-broadcast-export' ), |
| 173 |
), |
| 174 |
'edit.php' |
| 175 |
); |
| 176 |
|
| 177 |
// Add action. |
| 178 |
$actions['convertkit_broadcast_export'] = '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Create as ConvertKit Broadcast', 'convertkit' ) . '</a>'; |
| 179 |
|
| 180 |
// Return. |
| 181 |
return $actions; |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Creates a draft Broadcast in ConvertKit from the given WordPress Post ID, |
| 187 |
* storing the Broadcast ID against in the Post's meta. |
| 188 |
* |
| 189 |
* @since 2.4.0 |
| 190 |
* |
| 191 |
* @param int $post_id Post ID. |
| 192 |
* @return WP_Error|array |
| 193 |
*/ |
| 194 |
public function export_post_to_broadcast( $post_id ) { |
| 195 |
|
| 196 |
// Get Post. |
| 197 |
$post = get_post( $post_id ); |
| 198 |
|
| 199 |
// Return an error if the Post could not be fetched. |
| 200 |
if ( ! $post ) { |
| 201 |
return new WP_Error( |
| 202 |
'convertkit_broadcasts_exporter_export_post_to_broadcast', |
| 203 |
sprintf( |
| 204 |
/* translators: WordPress Post ID */ |
| 205 |
esc_html__( 'Could not fetch Post ID %s.', 'convertkit' ), |
| 206 |
$post_id |
| 207 |
) |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
// Fetch post's content by running it through the_content filter. |
| 212 |
// This will convert e.g. page builders and blocks to HTML. |
| 213 |
$content = apply_filters( 'the_content', $post->post_content ); |
| 214 |
|
| 215 |
// Remove HTML tags that are not supported in ConvertKit Broadcasts. |
| 216 |
$content = WP_ConvertKit()->get_class( 'broadcasts_importer' )->get_permitted_html( $content, $this->broadcasts_settings->no_styles() ); |
| 217 |
|
| 218 |
// Initialize the API. |
| 219 |
$api = new ConvertKit_API_V4( |
| 220 |
CONVERTKIT_OAUTH_CLIENT_ID, |
| 221 |
CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, |
| 222 |
$this->settings->get_access_token(), |
| 223 |
$this->settings->get_refresh_token(), |
| 224 |
$this->settings->debug_enabled(), |
| 225 |
'settings' |
| 226 |
); |
| 227 |
|
| 228 |
// Create draft Broadcast in ConvertKit. |
| 229 |
$result = $api->create_broadcast( |
| 230 |
$post->post_title, |
| 231 |
$content, |
| 232 |
$post->post_excerpt |
| 233 |
); |
| 234 |
|
| 235 |
// If an error occured, return it now. |
| 236 |
if ( is_wp_error( $result ) ) { |
| 237 |
return $result; |
| 238 |
} |
| 239 |
|
| 240 |
// Store the Broadcast ID against the WordPress Post. |
| 241 |
update_post_meta( $post->ID, '_convertkit_broadcast_export_id', $result['broadcast']['id'] ); |
| 242 |
|
| 243 |
// Return result. |
| 244 |
return $result['broadcast']; |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
} |
| 249 |
|